From e979a4b5cda15670b9f7127c3eb651f54f507d02 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 11 Nov 2021 22:02:36 +1030 Subject: [PATCH 1/5] update dev checker tools --- .github/workflows/tox.yaml | 4 ++-- requirements_test.txt | 8 ++++---- tox.ini | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/tox.yaml b/.github/workflows/tox.yaml index 40ca8d9..67c0209 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: @@ -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 }} diff --git a/requirements_test.txt b/requirements_test.txt index df62e77..e65a9a5 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 +cryptography==3.4.8 +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 445b62efd8a247bacf89ebc5a425426808add541 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 11 Nov 2021 22:11:34 +1030 Subject: [PATCH 2/5] tox.yaml --- .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 67c0209..afa1588 100644 --- a/.github/workflows/tox.yaml +++ b/.github/workflows/tox.yaml @@ -1,6 +1,6 @@ name: Tox PR CI -on: [pull_request, workflow_dispatch] +on: [push, pull_request, workflow_dispatch] jobs: build: From 1c2f4ba0843c76deafe2b05efd21ea10501ce95e Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 11 Nov 2021 22:30:57 +1030 Subject: [PATCH 3/5] Delete settings.json --- .vscode/settings.json | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index a04b218..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "files.associations": { - "*.yaml": "home-assistant" - } -} \ No newline at end of file From e8aa5ac9a8852a0e0f971ef247676eb80c6fe8c7 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 11 Nov 2021 22:46:55 +1030 Subject: [PATCH 4/5] integration inputs --- custom_components/localtuya/const.py | 3 + custom_components/localtuya/fan.py | 197 ++++++++++-------- .../localtuya/translations/en.json | 10 +- 3 files changed, 123 insertions(+), 87 deletions(-) diff --git a/custom_components/localtuya/const.py b/custom_components/localtuya/const.py index 9662b3b..36db866 100644 --- a/custom_components/localtuya/const.py +++ b/custom_components/localtuya/const.py @@ -40,6 +40,9 @@ 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 553f577..efd2058 100644 --- a/custom_components/localtuya/fan.py +++ b/custom_components/localtuya/fan.py @@ -1,56 +1,63 @@ """Platform to locally control Tuya-based fan devices.""" import logging -import math from functools import partial +import math 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, + SUPPORT_PRESET_MODE, FanEntity, ) -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, -) 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, - CONF_FAN_ORDERED_LIST, - CONF_FAN_OSCILLATING_CONTROL, - CONF_FAN_SPEED_CONTROL, - CONF_FAN_SPEED_MAX, CONF_FAN_SPEED_MIN, + CONF_FAN_SPEED_MAX, + CONF_FAN_ORDERED_LIST, + CONF_FAN_SPEED_DPS_TYPE, + CONF_FAN_PRESET_LIST +) + +from homeassistant.util.percentage import ( + ordered_list_item_to_percentage, + percentage_to_ordered_list_item, + percentage_to_ranged_value, + ranged_value_to_percentage, + int_states_in_range ) _LOGGER = logging.getLogger(__name__) - def flow_schema(dps): """Return schema used in config flow.""" return { 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, default="disabled"): cv.string, + 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"]), } - class LocaltuyaFan(LocalTuyaEntity, FanEntity): """Representation of a Tuya fan.""" @@ -67,24 +74,22 @@ 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_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 isinstance(self._ordered_list, 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, - ) + 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 isinstance(self._ordered_list, list) + and len(self._ordered_list) > 1): + _LOGGER.debug("Fan _use_ordered_list: %s", self._ordered_list) else: - self._use_ordered_list = False - _LOGGER.debug("Fan _use_ordered_list: %s", self._use_ordered_list) - + _LOGGER.debug("Fan _use_ordered_list: Not a valid list") + @property def oscillating(self): """Return current oscillating status.""" @@ -105,13 +110,12 @@ 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, **kwargs) -> None: """Turn on the entity.""" _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(percentage) + await self.async_set_percentage(speed) else: self.schedule_update_ha_state() @@ -124,42 +128,44 @@ 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) + _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: + + if not self.is_on: await self.async_turn_on() - if self._use_ordered_list: + + if self._ordered_speed_dps_type == "string": + send_speed = str(math.ceil( + percentage_to_ranged_value(self._speed_range, percentage))) 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), - ) + send_speed, self._config.get(CONF_FAN_SPEED_CONTROL)) + _LOGGER.debug("Fan async_set_percentage: %s > %s", + percentage, send_speed) + + elif self._ordered_speed_dps_type == "integer": + send_speed = int(math.ceil( + percentage_to_ranged_value(self._speed_range, percentage))) + await self._device.set_dp( + send_speed, self._config.get(CONF_FAN_SPEED_CONTROL)) + _LOGGER.debug("Fan async_set_percentage: %s > %s", + percentage, send_speed) - else: - await self._device.set_dp( - str( - math.ceil( - percentage_to_ranged_value(self._speed_range, percentage) + elif self._ordered_speed_dps_type == "list": + send_speed = 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_ranged_value(self._speed_range, percentage), - ) + await self._device.set_dp( + send_speed, self._config.get(CONF_FAN_SPEED_CONTROL)) + _LOGGER.debug("Fan async_set_percentage: %s > %s", + percentage, send_speed) + self.schedule_update_ha_state() + async def async_oscillate(self, oscillating: bool) -> None: """Set oscillation.""" _LOGGER.debug("Fan async_oscillate: %s", oscillating) @@ -174,10 +180,21 @@ 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)) + + 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() @property @@ -194,8 +211,11 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): if self.has_config(CONF_FAN_DIRECTION): features |= SUPPORT_DIRECTION - return features + if self.has_config(CONF_FAN_PRESET_CONTROL): + features |= SUPPORT_PRESET_MODE + return features + @property def speed_count(self) -> int: """Speed count for the fan.""" @@ -203,34 +223,41 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): _LOGGER.debug("Fan speed_count: %s", speed_count) return speed_count + def status_updated(self): """Get state of Tuya fan.""" + 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 + 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, - ) - if current_speed is not None: - self._percentage = ordered_list_item_to_percentage( - self._ordered_list, current_speed - ) + if current_speed is not None: - 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) - ) + if (self.has_config(CONF_FAN_PRESET_CONTROL) + and (CONF_FAN_SPEED_CONTROL == CONF_FAN_PRESET_CONTROL) + and (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 - _LOGGER.debug("Fan current_percentage: %s", self._percentage) + elif 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) + + 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) + 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) @@ -241,10 +268,10 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): 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) diff --git a/custom_components/localtuya/translations/en.json b/custom_components/localtuya/translations/en.json index 2cb903e..2014b0b 100644 --- a/custom_components/localtuya/translations/en.json +++ b/custom_components/localtuya/translations/en.json @@ -77,7 +77,10 @@ "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_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" } } } @@ -132,7 +135,10 @@ "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_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" } }, "yaml_import": { From d600cba89cffec158c2d6d1049e5802ab76c7e63 Mon Sep 17 00:00:00 2001 From: Tim Date: Thu, 11 Nov 2021 22:56:34 +1030 Subject: [PATCH 5/5] fix tox errors --- custom_components/localtuya/fan.py | 187 +++++++++++++++++------------ 1 file changed, 109 insertions(+), 78 deletions(-) diff --git a/custom_components/localtuya/fan.py b/custom_components/localtuya/fan.py index efd2058..6896831 100644 --- a/custom_components/localtuya/fan.py +++ b/custom_components/localtuya/fan.py @@ -1,46 +1,46 @@ """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, SUPPORT_PRESET_MODE, + SUPPORT_SET_SPEED, FanEntity, ) - -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, - CONF_FAN_SPEED_MIN, - CONF_FAN_SPEED_MAX, - CONF_FAN_ORDERED_LIST, - CONF_FAN_SPEED_DPS_TYPE, - CONF_FAN_PRESET_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_PRESET_CONTROL, + CONF_FAN_PRESET_LIST, + CONF_FAN_SPEED_CONTROL, + CONF_FAN_SPEED_DPS_TYPE, + CONF_FAN_SPEED_MAX, + CONF_FAN_SPEED_MIN, ) _LOGGER = logging.getLogger(__name__) + def flow_schema(dps): """Return schema used in config flow.""" return { @@ -55,9 +55,11 @@ def flow_schema(dps): 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"]), + ["string", "integer", "list"] + ), } + class LocaltuyaFan(LocalTuyaEntity, FanEntity): """Representation of a Tuya fan.""" @@ -76,20 +78,26 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): self._percentage = None self._preset = 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).replace(" ","").split(",") - self._preset_list = self._config.get(CONF_FAN_PRESET_LIST).replace(" ","").split(",") + 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 isinstance(self._ordered_list, list) - and len(self._ordered_list) > 1): + + if ( + self._ordered_speed_dps_type == "list" + and isinstance(self._ordered_list, list) + and len(self._ordered_list) > 1 + ): _LOGGER.debug("Fan _use_ordered_list: %s", self._ordered_list) else: _LOGGER.debug("Fan _use_ordered_list: Not a valid list") - + @property def oscillating(self): """Return current oscillating status.""" @@ -115,7 +123,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() @@ -128,44 +136,50 @@ 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: if percentage == 0: return await self.async_turn_off() - + if not self.is_on: await self.async_turn_on() - + if self._ordered_speed_dps_type == "string": - send_speed = str(math.ceil( - percentage_to_ranged_value(self._speed_range, percentage))) + send_speed = str( + math.ceil(percentage_to_ranged_value(self._speed_range, percentage)) + ) await self._device.set_dp( - send_speed, self._config.get(CONF_FAN_SPEED_CONTROL)) - _LOGGER.debug("Fan async_set_percentage: %s > %s", - percentage, send_speed) - + send_speed, self._config.get(CONF_FAN_SPEED_CONTROL) + ) + _LOGGER.debug( + "Fan async_set_percentage: %s > %s", percentage, send_speed + ) + elif self._ordered_speed_dps_type == "integer": - send_speed = int(math.ceil( - percentage_to_ranged_value(self._speed_range, percentage))) + send_speed = int( + math.ceil(percentage_to_ranged_value(self._speed_range, percentage)) + ) await self._device.set_dp( - send_speed, self._config.get(CONF_FAN_SPEED_CONTROL)) - _LOGGER.debug("Fan async_set_percentage: %s > %s", - percentage, send_speed) + send_speed, self._config.get(CONF_FAN_SPEED_CONTROL) + ) + _LOGGER.debug( + "Fan async_set_percentage: %s > %s", percentage, send_speed + ) elif self._ordered_speed_dps_type == "list": send_speed = str( - percentage_to_ordered_list_item(self._ordered_list, percentage) - ) + percentage_to_ordered_list_item(self._ordered_list, percentage) + ) await self._device.set_dp( - send_speed, self._config.get(CONF_FAN_SPEED_CONTROL)) - _LOGGER.debug("Fan async_set_percentage: %s > %s", - percentage, send_speed) + send_speed, self._config.get(CONF_FAN_SPEED_CONTROL) + ) + _LOGGER.debug( + "Fan async_set_percentage: %s > %s", percentage, send_speed + ) self.schedule_update_ha_state() - async def async_oscillate(self, oscillating: bool) -> None: """Set oscillation.""" _LOGGER.debug("Fan async_oscillate: %s", oscillating) @@ -180,17 +194,15 @@ 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() async def async_set_preset_mode(self, preset_mode: str) -> None: - """Set the preset mode of the fan.""" + """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) @@ -214,8 +226,8 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): if self.has_config(CONF_FAN_PRESET_CONTROL): features |= SUPPORT_PRESET_MODE - return features - + return features + @property def speed_count(self) -> int: """Speed count for the fan.""" @@ -223,38 +235,57 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): _LOGGER.debug("Fan speed_count: %s", speed_count) return speed_count - def status_updated(self): """Get state of Tuya fan.""" - 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) + _LOGGER.debug( + "Fan current_preset in preset list: %s from %s", + current_preset, + self._preset_list, + ) self._preset = current_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)): - _LOGGER.debug("Fan current_speed in preset list: %s from %s", - current_speed, self._preset_list) + if ( + self.has_config(CONF_FAN_PRESET_CONTROL) + and (CONF_FAN_SPEED_CONTROL == CONF_FAN_PRESET_CONTROL) + and (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": - _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) - - 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) - self._percentage = ranged_value_to_percentage(self._speed_range, int(current_speed)) + _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 + ) + + 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, + ) + 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) @@ -268,10 +299,10 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity): 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)