Skip to content

Commit

Permalink
↕️ refactor for cover
Browse files Browse the repository at this point in the history
  • Loading branch information
al-one committed Dec 26, 2024
1 parent 26b7556 commit fe21241
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
10 changes: 9 additions & 1 deletion custom_components/xiaomi_miot/core/device_customizes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2170,10 +2170,14 @@
],
},
'*.airer.*': {
'interval_seconds': 60,
'position_reverse': True,
'sensor_properties': 'left_time',
'switch_properties': 'dryer,uv',
'select_properties': 'drying_level',
'chunk_coordinators': [
{'interval': 15, 'props': 'status,current_position,target_position,dryer,drying_level'},
],
},
'*.airrtc.*': {
'switch_properties': 'air_conditioner.on',
Expand Down Expand Up @@ -2228,8 +2232,12 @@
],
},
'*.curtain.*': {
'interval_seconds': 60,
'switch_properties': 'motor_reverse',
'select_properties': 'mode',
'chunk_coordinators': [
{'interval': 10, 'props': 'status,current_position,target_position'},
],
},
'*.derh.*': {
'select_properties': 'fan_level',
Expand Down Expand Up @@ -2265,7 +2273,7 @@
'number_properties': 'off_delay_time',
'switch_properties': 'fan_init_power_opt',
'chunk_coordinators': [
{'interval': 10, 'props': 'on,fan_level'},
{'interval': 10, 'props': 'on,mode,fan_level'},
],
},
'*.fishbowl.*': {
Expand Down
44 changes: 28 additions & 16 deletions custom_components/xiaomi_miot/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ def on_init(self):
self._close_texts = self.custom_config_list('close_texts', self._close_texts)
if self._motor_reverse:
self._open_texts, self._close_texts = self._close_texts, self._open_texts
self._target2current_position = self.custom_config_bool('target2current_position')

for conv in self.device.converters:
prop = getattr(conv, 'prop', None)
Expand All @@ -77,9 +76,9 @@ def on_init(self):
self._attr_supported_features |= CoverEntityFeature.CLOSE
if prop.list_first('Stop', 'Pause') != None:
self._attr_supported_features |= CoverEntityFeature.STOP
elif prop.in_list(['current_position']) and prop.value_range:
elif prop.value_range and prop.in_list(['current_position']):
self._conv_current_position = conv
self._current_range = (prop.range_min, prop.range_max)
self._current_range = (prop.range_min(), prop.range_max())
elif prop.value_range and isinstance(conv, MiotTargetPositionConv):
self._conv_target_position = conv
self._target_range = conv.ranged
Expand All @@ -89,13 +88,22 @@ def on_init(self):
self._target_range = (prop.range_min(), prop.range_max())
self._attr_supported_features |= CoverEntityFeature.SET_POSITION

self._deviated_position = self.custom_config_integer('deviated_position', 1)
self._deviated_position = self.custom_config_integer('deviated_position', 2)
if self._current_range:
if self._position_reverse:
pos = self._current_range[1] - self._deviated_position
else:
pos = self._current_range[0] + self._deviated_position
pos = self._current_range[0] + self._deviated_position
self._closed_position = self.custom_config_integer('closed_position', pos)
self._target2current_position = self.custom_config_bool('target2current_position', not self._conv_current_position)

if self._motor_reverse or self._position_reverse:
self._attr_extra_state_attributes.update({
'motor_reverse': self._motor_reverse,
'position_reverse': self._position_reverse,
})
if self._closed_position:
self._attr_extra_state_attributes.update({
'closed_position': self._closed_position,
'deviated_position': self._deviated_position,
})

def set_state(self, data: dict):
prop_status = getattr(self._conv_status, 'prop', None) if self._conv_status else None
Expand All @@ -107,20 +115,24 @@ def set_state(self, data: dict):
if self._conv_current_position:
val = self._conv_current_position.value_from_dict(data)
if val is not None:
self._attr_current_cover_position = int(val)
val = int(val)
if self._position_reverse:
val = self._current_range[1] - val
self._attr_current_cover_position = val
if self._conv_target_position:
val = self._conv_target_position.value_from_dict(data)
if val is not None:
self._attr_target_cover_position = int(val)
if not self._conv_current_position:
self._attr_current_cover_position = self._attr_target_cover_position
val = int(val)
if self._position_reverse:
val = self._target_range[1] - val
self._attr_target_cover_position = val
if self._target2current_position:
self._attr_current_cover_position = self._attr_target_cover_position
self._attr_extra_state_attributes.update({
'target2current_position': True,
})
if (val := self._attr_current_cover_position) != None:
if self._position_reverse:
self._attr_is_closed = val >= self._closed_position
else:
self._attr_is_closed = val <= self._closed_position
self._attr_is_closed = val <= self._closed_position

async def async_open_cover(self, **kwargs):
if self._conv_motor:
Expand Down

1 comment on commit fe21241

@al-one
Copy link
Owner Author

@al-one al-one commented on fe21241 Dec 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.