diff --git a/custom_components/xiaomi_miot/fan.py b/custom_components/xiaomi_miot/fan.py index 74c1a54e1..ada6d2e3b 100644 --- a/custom_components/xiaomi_miot/fan.py +++ b/custom_components/xiaomi_miot/fan.py @@ -4,6 +4,8 @@ from homeassistant.components.fan import ( DOMAIN as ENTITY_DOMAIN, + DIRECTION_FORWARD, + DIRECTION_REVERSE, FanEntity as BaseEntity, FanEntityFeature, ) @@ -67,6 +69,11 @@ class FanEntity(XEntity, BaseEntity): _speed_list = None _speed_range = None _conv_oscillate = None + _act_turn_left = None + _act_turn_right = None + _prop_direction = None + _direction_left = None + _direction_right = None _prop_speed = None _prop_percentage = None @@ -100,6 +107,26 @@ def on_init(self): self._conv_oscillate = conv self._attr_supported_features |= FanEntityFeature.OSCILLATE + if self._miot_service: + self._act_turn_left = self._miot_service.get_action('turn_left') + self._act_turn_right = self._miot_service.get_action('turn_right') + direction_actions = (self._act_turn_left, self._act_turn_right) + if all(action and not action.ins for action in direction_actions): + self._attr_supported_features |= FanEntityFeature.DIRECTION + else: + self._act_turn_left = None + self._act_turn_right = None + for prop in self._miot_service.spec.get_properties('motor_control'): + left = prop.list_first('left') + right = prop.list_first('right') + if not prop.writeable or left is None or right is None: + continue + self._prop_direction = prop + self._direction_left = left + self._direction_right = right + self._attr_supported_features |= FanEntityFeature.DIRECTION + break + # issues/617 if self.custom_config_bool('disable_preset_modes'): self._attr_supported_features &= ~FanEntityFeature.PRESET_MODE @@ -224,6 +251,22 @@ async def async_oscillate(self, oscillating: bool): return await self.device.async_write({self._conv_oscillate.full_name: oscillating}) + async def async_set_direction(self, direction: str): + if direction in [DIRECTION_REVERSE, 'left']: + action = self._act_turn_left + value = self._direction_left + elif direction in [DIRECTION_FORWARD, 'right']: + action = self._act_turn_right + value = self._direction_right + else: + return + if self._conv_oscillate and self._attr_oscillating: + await self.async_oscillate(False) + if action: + return await self.async_call_action(action) + if self._prop_direction: + return await self.async_set_property(self._prop_direction, value) + XEntity.CLS[ENTITY_DOMAIN] = FanEntity