Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions custom_components/xiaomi_miot/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from homeassistant.components.fan import (
DOMAIN as ENTITY_DOMAIN,
DIRECTION_FORWARD,
DIRECTION_REVERSE,
FanEntity as BaseEntity,
FanEntityFeature,
)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
Loading