diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..a04b218 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "*.yaml": "home-assistant" + } +} \ No newline at end of file diff --git a/custom_components/localtuya/fan.py b/custom_components/localtuya/fan.py index 37ff370..51a2191 100644 --- a/custom_components/localtuya/fan.py +++ b/custom_components/localtuya/fan.py @@ -1,38 +1,37 @@ """Platform to locally control Tuya-based fan devices.""" import logging -from functools import partial import math +from functools import partial import homeassistant.helpers.config_validation as cv import voluptuous as vol from homeassistant.components.fan import ( - DOMAIN, DIRECTION_FORWARD, DIRECTION_REVERSE, + DOMAIN, SUPPORT_DIRECTION, SUPPORT_OSCILLATE, SUPPORT_SET_SPEED, FanEntity, ) - -from .common import LocalTuyaEntity, async_setup_entry -from .const import ( - CONF_FAN_OSCILLATING_CONTROL, - CONF_FAN_SPEED_CONTROL, - CONF_FAN_DIRECTION, - CONF_FAN_DIRECTION_FWD, - CONF_FAN_DIRECTION_REV, - CONF_FAN_SPEED_MIN, - CONF_FAN_SPEED_MAX, - CONF_FAN_ORDERED_LIST -) - from homeassistant.util.percentage import ( + int_states_in_range, ordered_list_item_to_percentage, percentage_to_ordered_list_item, percentage_to_ranged_value, ranged_value_to_percentage, - int_states_in_range +) + +from .common import LocalTuyaEntity, async_setup_entry +from .const import ( + CONF_FAN_DIRECTION, + CONF_FAN_DIRECTION_FWD, + CONF_FAN_DIRECTION_REV, + CONF_FAN_ORDERED_LIST, + CONF_FAN_OSCILLATING_CONTROL, + CONF_FAN_SPEED_CONTROL, + CONF_FAN_SPEED_MAX, + CONF_FAN_SPEED_MIN, ) _LOGGER = logging.getLogger(__name__) @@ -69,20 +68,23 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): self._direction = None self._percentage = None self._speed_range = ( - self._config.get(CONF_FAN_SPEED_MIN), + self._config.get(CONF_FAN_SPEED_MIN), self._config.get(CONF_FAN_SPEED_MAX), ) self._ordered_list = self._config.get(CONF_FAN_ORDERED_LIST).split(",") self._ordered_list_mode = None - if (type(self._ordered_list) is list and len(self._ordered_list) > 1): + if type(self._ordered_list) is list and len(self._ordered_list) > 1: self._use_ordered_list = True - _LOGGER.debug("Fan _use_ordered_list: %s > %s", self._use_ordered_list, self._ordered_list) + _LOGGER.debug( + "Fan _use_ordered_list: %s > %s", + self._use_ordered_list, + self._ordered_list, + ) else: self._use_ordered_list = False _LOGGER.debug("Fan _use_ordered_list: %s", self._use_ordered_list) - @property def oscillating(self): @@ -125,7 +127,6 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): _LOGGER.debug("Fan async_set_percentage: %s", percentage) - if percentage is not None: if percentage == 0: return await self.async_turn_off() @@ -135,22 +136,31 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): await self._device.set_dp( str( percentage_to_ordered_list_item(self._ordered_list, percentage) - ), - self._config.get(CONF_FAN_SPEED_CONTROL) - ) - _LOGGER.debug("Fan async_set_percentage: %s > %s", percentage, percentage_to_ordered_list_item(self._ordered_list, percentage)) + ), + self._config.get(CONF_FAN_SPEED_CONTROL), + ) + _LOGGER.debug( + "Fan async_set_percentage: %s > %s", + percentage, + percentage_to_ordered_list_item(self._ordered_list, percentage), + ) else: await self._device.set_dp( - str(math.ceil( - percentage_to_ranged_value(self._speed_range, percentage) - )), - self._config.get(CONF_FAN_SPEED_CONTROL) - ) - _LOGGER.debug("Fan async_set_percentage: %s > %s", percentage, percentage_to_ranged_value(self._speed_range, percentage)) + str( + math.ceil( + percentage_to_ranged_value(self._speed_range, percentage) + ) + ), + self._config.get(CONF_FAN_SPEED_CONTROL), + ) + _LOGGER.debug( + "Fan async_set_percentage: %s > %s", + percentage, + percentage_to_ranged_value(self._speed_range, percentage), + ) self.schedule_update_ha_state() - async def async_oscillate(self, oscillating: bool) -> None: """Set oscillation.""" _LOGGER.debug("Fan async_oscillate: %s", oscillating) @@ -165,14 +175,12 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): if direction == DIRECTION_FORWARD: value = self._config.get(CONF_FAN_DIRECTION_FWD) - + if direction == DIRECTION_REVERSE: value = self._config.get(CONF_FAN_DIRECTION_REV) - await self._device.set_dp( - value, self._config.get(CONF_FAN_DIRECTION) - ) - self.schedule_update_ha_state() + await self._device.set_dp(value, self._config.get(CONF_FAN_DIRECTION)) + self.schedule_update_ha_state() @property def supported_features(self) -> int: @@ -188,8 +196,8 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): if self.has_config(CONF_FAN_DIRECTION): features |= SUPPORT_DIRECTION - return features - + return features + @property def speed_count(self) -> int: """Speed count for the fan.""" @@ -197,7 +205,6 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): _LOGGER.debug("Fan speed_count: %s", speed_count) return speed_count - def status_updated(self): """Get state of Tuya fan.""" @@ -205,14 +212,26 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): current_speed = self.dps_conf(CONF_FAN_SPEED_CONTROL) if self._use_ordered_list: - _LOGGER.debug("Fan current_speed ordered_list_item_to_percentage: %s from %s", current_speed, self._ordered_list) + _LOGGER.debug( + "Fan current_speed ordered_list_item_to_percentage: %s from %s", + current_speed, + self._ordered_list, + ) if current_speed is not None: - self._percentage = ordered_list_item_to_percentage(self._ordered_list, current_speed) - + self._percentage = ordered_list_item_to_percentage( + self._ordered_list, current_speed + ) + else: - _LOGGER.debug("Fan current_speed ranged_value_to_percentage: %s from %s", current_speed, self._speed_range) + _LOGGER.debug( + "Fan current_speed ranged_value_to_percentage: %s from %s", + current_speed, + self._speed_range, + ) if current_speed is not None: - self._percentage = ranged_value_to_percentage(self._speed_range, int(current_speed)) + self._percentage = ranged_value_to_percentage( + self._speed_range, int(current_speed) + ) _LOGGER.debug("Fan current_percentage: %s", self._percentage) @@ -220,16 +239,15 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): self._oscillating = self.dps_conf(CONF_FAN_OSCILLATING_CONTROL) _LOGGER.debug("Fan current_oscillating : %s", self._oscillating) - if self.has_config(CONF_FAN_DIRECTION): value = self.dps_conf(CONF_FAN_DIRECTION) if value is not None: if value == self._config.get(CONF_FAN_DIRECTION_FWD): self._direction = DIRECTION_FORWARD - + if value == self._config.get(CONF_FAN_DIRECTION_REV): self._direction = DIRECTION_REVERSE _LOGGER.debug("Fan current_direction : %s > %s", value, self._direction) - + async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaFan, flow_schema)