From 7ac4fe5a119c7b1f6ff9c0b835e273a57b3b7362 Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 26 Oct 2021 21:41:51 +1030 Subject: [PATCH 01/15] Revert "correctly split preset and speed in updates" This reverts commit 0c59ddc7e0e008de8236658eae51ccf8ac56c485. --- custom_components/localtuya/fan.py | 35 ++++++++++++++++++------------ 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/custom_components/localtuya/fan.py b/custom_components/localtuya/fan.py index 89024c3..4aa2f45 100644 --- a/custom_components/localtuya/fan.py +++ b/custom_components/localtuya/fan.py @@ -20,7 +20,6 @@ from .common import LocalTuyaEntity, async_setup_entry from .const import ( CONF_FAN_OSCILLATING_CONTROL, CONF_FAN_SPEED_CONTROL, - CONF_FAN_PRESET_CONTROL, CONF_FAN_DIRECTION, CONF_FAN_DIRECTION_FWD, CONF_FAN_DIRECTION_REV, @@ -85,8 +84,7 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): self._preset_list = self._config.get(CONF_FAN_PRESET_LIST).replace(" ","").split(",") self._ordered_speed_dps_type = self._config.get(CONF_FAN_SPEED_DPS_TYPE) - if (self._ordered_speed_dps_type == "list" - and type(self._ordered_list) is list + if (self._ordered_speed_dps_type == "list" and type(self._ordered_list) is list and len(self._ordered_list) > 1): _LOGGER.debug("Fan _use_ordered_list: %s", self._ordered_list) else: @@ -168,6 +166,23 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): ) _LOGGER.debug("Fan async_set_percentage: %s > %s", percentage, percentage_to_ordered_list_item(self._ordered_list, percentage)) + # if self._use_ordered_list: + # 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)) + + # 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)) self.schedule_update_ha_state() @@ -234,22 +249,15 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): self._is_on = self.dps(self._dp_id) - if self.has_config(CONF_FAN_PRESET_CONTROL): - current_preset = self.dps_conf(CONF_FAN_PRESET_CONTROL) - if current_preset is not None and current_preset in self._preset_list: - _LOGGER.debug("Fan current_preset in preset list: %s from %s", current_preset, self._preset_list) - self._preset = current_preset - + # check for speed and preset. current_speed = self.dps_conf(CONF_FAN_SPEED_CONTROL) if current_speed is not None: - if (self.has_config(CONF_FAN_PRESET_CONTROL) - and (CONF_FAN_SPEED_CONTROL == CONF_FAN_PRESET_CONTROL) - and (current_speed in self._preset_list)): + if current_speed in self._preset_list: _LOGGER.debug("Fan current_speed in preset list: %s from %s", current_speed, self._preset_list) self._preset = current_speed - elif self._ordered_speed_dps_type == "list": + if self._ordered_speed_dps_type == "list": _LOGGER.debug("Fan current_speed ordered_list_item_to_percentage: %s from %s", current_speed, self._ordered_list) self._percentage = ordered_list_item_to_percentage(self._ordered_list, current_speed) @@ -258,7 +266,6 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): self._percentage = ranged_value_to_percentage(self._speed_range, int(current_speed)) _LOGGER.debug("Fan current_percentage: %s", self._percentage) - _LOGGER.debug("Fan current_preset: %s", self._preset) if self.has_config(CONF_FAN_OSCILLATING_CONTROL): self._oscillating = self.dps_conf(CONF_FAN_OSCILLATING_CONTROL) From 0f43923c2e3d9c652306909313db170b7fa7f0e0 Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 26 Oct 2021 21:41:56 +1030 Subject: [PATCH 02/15] Revert "missing imports" This reverts commit 6f281a08fc0825e6b718f251b7a36ad30c88767f. --- custom_components/localtuya/fan.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/custom_components/localtuya/fan.py b/custom_components/localtuya/fan.py index 4aa2f45..4693764 100644 --- a/custom_components/localtuya/fan.py +++ b/custom_components/localtuya/fan.py @@ -12,7 +12,6 @@ from homeassistant.components.fan import ( SUPPORT_DIRECTION, SUPPORT_OSCILLATE, SUPPORT_SET_SPEED, - SUPPORT_PRESET_MODE, FanEntity, ) @@ -26,8 +25,7 @@ from .const import ( CONF_FAN_SPEED_MIN, CONF_FAN_SPEED_MAX, CONF_FAN_ORDERED_LIST, - CONF_FAN_SPEED_DPS_TYPE, - CONF_FAN_PRESET_LIST + CONF_FAN_SPEED_DPS_TYPE ) from homeassistant.util.percentage import ( From c8e660dfac689440a4bcbd3a68c45af39b775451 Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 26 Oct 2021 21:41:59 +1030 Subject: [PATCH 03/15] Revert "add presets and integer/string option" This reverts commit ec223ba73d35d7ea2de64eff11ca647468fb43ae. --- custom_components/localtuya/const.py | 4 +- custom_components/localtuya/fan.py | 111 +++++------------- .../localtuya/translations/en.json | 10 +- 3 files changed, 34 insertions(+), 91 deletions(-) diff --git a/custom_components/localtuya/const.py b/custom_components/localtuya/const.py index 3d3ac05..9662b3b 100644 --- a/custom_components/localtuya/const.py +++ b/custom_components/localtuya/const.py @@ -40,9 +40,7 @@ CONF_FAN_ORDERED_LIST = "fan_speed_ordered_list" CONF_FAN_DIRECTION = "fan_direction" CONF_FAN_DIRECTION_FWD = "fan_direction_forward" CONF_FAN_DIRECTION_REV = "fan_direction_reverse" -CONF_FAN_SPEED_DPS_TYPE = "fan_speed_dps_type" -CONF_FAN_PRESET_CONTROL = "fan_preset_control" -CONF_FAN_PRESET_LIST = "fan_preset_list" + # sensor CONF_SCALING = "scaling" diff --git a/custom_components/localtuya/fan.py b/custom_components/localtuya/fan.py index 4693764..43377c9 100644 --- a/custom_components/localtuya/fan.py +++ b/custom_components/localtuya/fan.py @@ -24,8 +24,7 @@ from .const import ( CONF_FAN_DIRECTION_REV, CONF_FAN_SPEED_MIN, CONF_FAN_SPEED_MAX, - CONF_FAN_ORDERED_LIST, - CONF_FAN_SPEED_DPS_TYPE + CONF_FAN_ORDERED_LIST ) from homeassistant.util.percentage import ( @@ -45,15 +44,11 @@ def flow_schema(dps): vol.Optional(CONF_FAN_SPEED_CONTROL): vol.In(dps), vol.Optional(CONF_FAN_OSCILLATING_CONTROL): vol.In(dps), vol.Optional(CONF_FAN_DIRECTION): vol.In(dps), - vol.Optional(CONF_FAN_PRESET_CONTROL): vol.In(dps), vol.Optional(CONF_FAN_DIRECTION_FWD, default="forward"): cv.string, vol.Optional(CONF_FAN_DIRECTION_REV, default="reverse"): cv.string, vol.Optional(CONF_FAN_SPEED_MIN, default=1): cv.positive_int, vol.Optional(CONF_FAN_SPEED_MAX, default=9): cv.positive_int, - vol.Optional(CONF_FAN_ORDERED_LIST): cv.string, - vol.Optional(CONF_FAN_PRESET_LIST): cv.string, - vol.Optional(CONF_FAN_SPEED_DPS_TYPE, default="string"): vol.In( - ["string", "integer", "list"]), + vol.Optional(CONF_FAN_ORDERED_LIST, default="disabled"): cv.string, } @@ -73,21 +68,21 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): self._oscillating = None self._direction = None self._percentage = None - self._preset = None self._speed_range = ( self._config.get(CONF_FAN_SPEED_MIN), self._config.get(CONF_FAN_SPEED_MAX), ) - self._ordered_list = self._config.get(CONF_FAN_ORDERED_LIST).replace(" ","").split(",") - self._preset_list = self._config.get(CONF_FAN_PRESET_LIST).replace(" ","").split(",") - self._ordered_speed_dps_type = self._config.get(CONF_FAN_SPEED_DPS_TYPE) - - if (self._ordered_speed_dps_type == "list" and type(self._ordered_list) is list - and len(self._ordered_list) > 1): - _LOGGER.debug("Fan _use_ordered_list: %s", self._ordered_list) + 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): + self._use_ordered_list = True + _LOGGER.debug("Fan _use_ordered_list: %s > %s", self._use_ordered_list, self._ordered_list) + else: - _LOGGER.debug("Fan _use_ordered_list: Not a valid list") - + self._use_ordered_list = False + _LOGGER.debug("Fan _use_ordered_list: %s", self._use_ordered_list) + @property def oscillating(self): @@ -130,32 +125,13 @@ 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() elif not self.is_on: await self.async_turn_on() - - if self._ordered_speed_dps_type == "string": - 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)) - - elif self._ordered_speed_dps_type == "integer": - await self._device.set_dp( - int(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)) - - elif self._ordered_speed_dps_type == "list": + if self._use_ordered_list: await self._device.set_dp( str( percentage_to_ordered_list_item(self._ordered_list, percentage) @@ -164,23 +140,14 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): ) _LOGGER.debug("Fan async_set_percentage: %s > %s", percentage, percentage_to_ordered_list_item(self._ordered_list, percentage)) - # if self._use_ordered_list: - # 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)) - - # 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)) + 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)) self.schedule_update_ha_state() @@ -205,15 +172,7 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): await self._device.set_dp( value, self._config.get(CONF_FAN_DIRECTION) ) - self.schedule_update_ha_state() - - async def async_set_preset_mode(self, preset_mode: str) -> None: - """Set the preset mode of the fan.""" - _LOGGER.debug("Fan set preset: %s", preset_mode) - await self._device.set_dp( - preset_mode, self._config.get(CONF_FAN_PRESET_CONTROL) - ) - self.schedule_update_ha_state() + self.schedule_update_ha_state() @property def supported_features(self) -> int: @@ -229,9 +188,6 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): if self.has_config(CONF_FAN_DIRECTION): features |= SUPPORT_DIRECTION - if self.has_config(CONF_FAN_PRESET_CONTROL): - features |= SUPPORT_PRESET_MODE - return features @property @@ -247,23 +203,18 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): self._is_on = self.dps(self._dp_id) - # check for speed and preset. current_speed = self.dps_conf(CONF_FAN_SPEED_CONTROL) - if current_speed is not None: - - if current_speed in self._preset_list: - _LOGGER.debug("Fan current_speed in preset list: %s from %s", current_speed, self._preset_list) - self._preset = current_speed - - if self._ordered_speed_dps_type == "list": - _LOGGER.debug("Fan current_speed ordered_list_item_to_percentage: %s from %s", current_speed, self._ordered_list) + if self._use_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) - - elif self._ordered_speed_dps_type == "string" or self._ordered_speed_dps_type == "integer" : - _LOGGER.debug("Fan current_speed ranged_value_to_percentage: %s from %s", current_speed, self._speed_range) + + else: + _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)) - _LOGGER.debug("Fan current_percentage: %s", self._percentage) + _LOGGER.debug("Fan current_percentage: %s", self._percentage) if self.has_config(CONF_FAN_OSCILLATING_CONTROL): self._oscillating = self.dps_conf(CONF_FAN_OSCILLATING_CONTROL) diff --git a/custom_components/localtuya/translations/en.json b/custom_components/localtuya/translations/en.json index e465784..2cb903e 100644 --- a/custom_components/localtuya/translations/en.json +++ b/custom_components/localtuya/translations/en.json @@ -77,10 +77,7 @@ "fan_speed_ordered_list": "Fan speed modes list (overrides speed min/max)", "fan_direction":"fan direction dps", "fan_direction_forward": "forward dps string", - "fan_direction_reverse": "reverse dps string", - "fan_speed_dps_type": "type of speed dps control. integer/string/list", - "fan_preset_control": "Fan preset dps. CAn be the same as speed", - "fan_preset_list": "FAn preset list. Comma separated" + "fan_direction_reverse": "reverse dps string" } } } @@ -135,10 +132,7 @@ "fan_speed_ordered_list": "Fan speed modes list (overrides speed min/max)", "fan_direction":"fan direction dps", "fan_direction_forward": "forward dps string", - "fan_direction_reverse": "reverse dps string", - "fan_speed_dps_type": "type of speed dps control. integer/string/list", - "fan_preset_control": "Fan preset dps. CAn be the same as speed", - "fan_preset_list": "FAn preset list. Comma separated" + "fan_direction_reverse": "reverse dps string" } }, "yaml_import": { From c99a93bd6c3f43ba17107720a97bd289a79c59e6 Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 10 Nov 2021 06:59:58 +1030 Subject: [PATCH 04/15] Fix speed variable error --- custom_components/localtuya/fan.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/localtuya/fan.py b/custom_components/localtuya/fan.py index 43377c9..37ff370 100644 --- a/custom_components/localtuya/fan.py +++ b/custom_components/localtuya/fan.py @@ -109,7 +109,7 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): _LOGGER.debug("Fan async_turn_on") await self._device.set_dp(True, self._dp_id) if percentage is not None: - await self.async_set_percentage(speed) + await self.async_set_percentage(percentage) else: self.schedule_update_ha_state() From 9bf9b9f55dc1b4942fdfcc48dc3d6524a7f0aabe Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 11 Nov 2021 11:12:43 +1030 Subject: [PATCH 05/15] fix formatting errors --- .vscode/settings.json | 5 ++++ custom_components/localtuya/fan.py | 40 ++++++++++++++++-------------- 2 files changed, 26 insertions(+), 19 deletions(-) create mode 100644 .vscode/settings.json 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..1a5b8d8 100644 --- a/custom_components/localtuya/fan.py +++ b/custom_components/localtuya/fan.py @@ -69,7 +69,7 @@ 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(",") @@ -77,12 +77,12 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): 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 +125,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() @@ -137,8 +136,9 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): 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)) + ) + _LOGGER.debug("Fan async_set_percentage: %s > %s", + percentage, percentage_to_ordered_list_item(self._ordered_list, percentage)) else: await self._device.set_dp( @@ -146,11 +146,11 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): 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)) + ) + _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 +165,14 @@ 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() + self.schedule_update_ha_state() @property def supported_features(self) -> int: @@ -188,7 +188,7 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): if self.has_config(CONF_FAN_DIRECTION): features |= SUPPORT_DIRECTION - return features + return features @property def speed_count(self) -> int: @@ -197,7 +197,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,12 +204,17 @@ 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)) @@ -220,7 +224,6 @@ 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: @@ -230,6 +233,5 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): 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) +async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaFan, flow_schema) \ No newline at end of file From af9f11ad10707d1e1dbaf686ea930727cfedf8f5 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 11 Nov 2021 11:12:43 +1030 Subject: [PATCH 06/15] fix formatting errors --- .vscode/settings.json | 5 ++ custom_components/localtuya/fan.py | 114 +++++++++++++++++------------ 2 files changed, 71 insertions(+), 48 deletions(-) create mode 100644 .vscode/settings.json 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) From 77f91590048b0e5b35aca36ce0628d9d092c24dc Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 11 Nov 2021 11:54:26 +1030 Subject: [PATCH 07/15] workflow_dispatch --- .github/workflows/tox.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tox.yaml b/.github/workflows/tox.yaml index 40ca8d9..8fc6768 100644 --- a/.github/workflows/tox.yaml +++ b/.github/workflows/tox.yaml @@ -1,6 +1,6 @@ name: Tox PR CI -on: [pull_request] +on: [pull_request, workflow_dispatch] jobs: build: From eda45fc896f52bcb357977d316901fef9631d21f Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 11 Nov 2021 12:14:49 +1030 Subject: [PATCH 08/15] fix tox errors --- custom_components/localtuya/fan.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/custom_components/localtuya/fan.py b/custom_components/localtuya/fan.py index 1a5b8d8..e02fd25 100644 --- a/custom_components/localtuya/fan.py +++ b/custom_components/localtuya/fan.py @@ -122,7 +122,6 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): async def async_set_percentage(self, percentage): """Set the speed of the fan.""" - _LOGGER.debug("Fan async_set_percentage: %s", percentage) if percentage is not None: @@ -199,7 +198,6 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): def status_updated(self): """Get state of Tuya fan.""" - self._is_on = self.dps(self._dp_id) current_speed = self.dps_conf(CONF_FAN_SPEED_CONTROL) From f1753945ca5f6d26a6dc5fc281593e015a0106ce Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 11 Nov 2021 13:08:32 +1030 Subject: [PATCH 09/15] checks updates and tox fixes --- requirements_test.txt | 6 +++--- tox.ini | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/requirements_test.txt b/requirements_test.txt index df62e77..8a2a53d 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -1,9 +1,9 @@ black==21.4b0 codespell==2.0.0 flake8==3.9.2 -mypy==0.901 +mypy==0.910 pydocstyle==6.1.1 cryptography==3.2 -pylint==2.8.2 +pylint==2.11.1 pylint-strict-informational==0.1 -homeassistant==2021.1.4 +homeassistant==2021.11.2 diff --git a/tox.ini b/tox.ini index 49ccccb..75c6e56 100644 --- a/tox.ini +++ b/tox.ini @@ -1,13 +1,13 @@ [tox] skipsdist = true -envlist = py{37,38}, lint, typing +envlist = py{38,39}, lint, typing skip_missing_interpreters = True cs_exclude_words = hass,unvalid [gh-actions] python = - 3.7: clean, py37, lint, typing 3.8: clean, py38, lint, typing + 3.9: clean, py38, lint, typing [testenv] passenv = TOXENV CI From c30c68dbf540a70691845b0754853bf83cb0ad96 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 11 Nov 2021 13:09:01 +1030 Subject: [PATCH 10/15] tox --- .github/workflows/tox.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tox.yaml b/.github/workflows/tox.yaml index 8fc6768..67c0209 100644 --- a/.github/workflows/tox.yaml +++ b/.github/workflows/tox.yaml @@ -14,8 +14,8 @@ jobs: platform: - ubuntu-latest python-version: - - 3.7 - 3.8 + - 3.9 steps: - uses: actions/checkout@v2 - name: Set up Python ${{ matrix.python-version }} From 01b9b00050c5970654aab7a2d03959fba169a074 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 11 Nov 2021 13:12:31 +1030 Subject: [PATCH 11/15] fix requirements --- requirements_test.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements_test.txt b/requirements_test.txt index 8a2a53d..e65a9a5 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -3,7 +3,7 @@ codespell==2.0.0 flake8==3.9.2 mypy==0.910 pydocstyle==6.1.1 -cryptography==3.2 +cryptography==3.4.8 pylint==2.11.1 pylint-strict-informational==0.1 homeassistant==2021.11.2 From 6e6cd0861fa79d89cc66cecf1cdf1bfa5cb5b214 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 11 Nov 2021 13:15:09 +1030 Subject: [PATCH 12/15] fix tox errors --- custom_components/localtuya/fan.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/custom_components/localtuya/fan.py b/custom_components/localtuya/fan.py index 31076a3..6a1e219 100644 --- a/custom_components/localtuya/fan.py +++ b/custom_components/localtuya/fan.py @@ -245,4 +245,5 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): 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) \ No newline at end of file + +async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaFan, flow_schema) From 95ef271dab36fcd7b204aec952e15796d895f7fc Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 11 Nov 2021 13:50:18 +1030 Subject: [PATCH 13/15] more tox fixes --- custom_components/localtuya/fan.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/localtuya/fan.py b/custom_components/localtuya/fan.py index 6a1e219..51d9d5e 100644 --- a/custom_components/localtuya/fan.py +++ b/custom_components/localtuya/fan.py @@ -74,7 +74,7 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): 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 isinstance(self._ordered_list, list) and len(self._ordered_list) > 1: self._use_ordered_list = True _LOGGER.debug( "Fan _use_ordered_list: %s > %s", @@ -105,7 +105,7 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): """Return the current percentage.""" return self._percentage - async def async_turn_on(self, percentage: str = None, **kwargs) -> None: + async def async_turn_on(self, percentage: str = None, preset_mode: str = None, **kwargs: Any) -> None: """Turn on the entity.""" _LOGGER.debug("Fan async_turn_on") await self._device.set_dp(True, self._dp_id) From a171430cc877051582196a3456ecdaecdca864ec Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 11 Nov 2021 13:53:51 +1030 Subject: [PATCH 14/15] fix error --- custom_components/localtuya/fan.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/localtuya/fan.py b/custom_components/localtuya/fan.py index 51d9d5e..17f0c32 100644 --- a/custom_components/localtuya/fan.py +++ b/custom_components/localtuya/fan.py @@ -105,7 +105,7 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): """Return the current percentage.""" return self._percentage - async def async_turn_on(self, percentage: str = None, preset_mode: str = None, **kwargs: Any) -> None: + async def async_turn_on(self, percentage: str = None, preset_mode: str = None, **kwargs) -> None: """Turn on the entity.""" _LOGGER.debug("Fan async_turn_on") await self._device.set_dp(True, self._dp_id) From e55cd2013faf9d37924cda0b51bf7c4d4283630d Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 11 Nov 2021 13:57:18 +1030 Subject: [PATCH 15/15] line length issue --- custom_components/localtuya/fan.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/custom_components/localtuya/fan.py b/custom_components/localtuya/fan.py index 17f0c32..553f577 100644 --- a/custom_components/localtuya/fan.py +++ b/custom_components/localtuya/fan.py @@ -105,7 +105,8 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): """Return the current percentage.""" return self._percentage - async def async_turn_on(self, percentage: str = None, preset_mode: str = None, **kwargs) -> None: + async def async_turn_on(self, percentage: str = None, + preset_mode: str = None, **kwargs) -> None: """Turn on the entity.""" _LOGGER.debug("Fan async_turn_on") await self._device.set_dp(True, self._dp_id)