This commit is contained in:
Tim
2021-11-11 12:20:08 +10:30
2 changed files with 57 additions and 44 deletions

View File

@@ -1,6 +1,6 @@
name: Tox PR CI name: Tox PR CI
on: [pull_request] on: [pull_request, workflow_dispatch]
jobs: jobs:
build: build:

View File

@@ -1,38 +1,37 @@
"""Platform to locally control Tuya-based fan devices.""" """Platform to locally control Tuya-based fan devices."""
import logging import logging
from functools import partial
import math import math
from functools import partial
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
import voluptuous as vol import voluptuous as vol
from homeassistant.components.fan import ( from homeassistant.components.fan import (
DOMAIN,
DIRECTION_FORWARD, DIRECTION_FORWARD,
DIRECTION_REVERSE, DIRECTION_REVERSE,
DOMAIN,
SUPPORT_DIRECTION, SUPPORT_DIRECTION,
SUPPORT_OSCILLATE, SUPPORT_OSCILLATE,
SUPPORT_SET_SPEED, SUPPORT_SET_SPEED,
FanEntity, 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 ( from homeassistant.util.percentage import (
int_states_in_range,
ordered_list_item_to_percentage, ordered_list_item_to_percentage,
percentage_to_ordered_list_item, percentage_to_ordered_list_item,
percentage_to_ranged_value, percentage_to_ranged_value,
ranged_value_to_percentage, 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__) _LOGGER = logging.getLogger(__name__)
@@ -75,11 +74,13 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity):
self._ordered_list = self._config.get(CONF_FAN_ORDERED_LIST).split(",") self._ordered_list = self._config.get(CONF_FAN_ORDERED_LIST).split(",")
self._ordered_list_mode = None 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 self._use_ordered_list = True
_LOGGER.debug("Fan _use_ordered_list: %s > %s", _LOGGER.debug(
self._use_ordered_list, self._ordered_list) "Fan _use_ordered_list: %s > %s",
self._use_ordered_list,
self._ordered_list,
)
else: else:
self._use_ordered_list = False self._use_ordered_list = False
_LOGGER.debug("Fan _use_ordered_list: %s", self._use_ordered_list) _LOGGER.debug("Fan _use_ordered_list: %s", self._use_ordered_list)
@@ -133,21 +134,29 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity):
await self._device.set_dp( await self._device.set_dp(
str( str(
percentage_to_ordered_list_item(self._ordered_list, percentage) percentage_to_ordered_list_item(self._ordered_list, percentage)
), ),
self._config.get(CONF_FAN_SPEED_CONTROL) 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: else:
await self._device.set_dp( await self._device.set_dp(
str(math.ceil( str(
percentage_to_ranged_value(self._speed_range, percentage) math.ceil(
)), percentage_to_ranged_value(self._speed_range, percentage)
self._config.get(CONF_FAN_SPEED_CONTROL) )
),
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() self.schedule_update_ha_state()
async def async_oscillate(self, oscillating: bool) -> None: async def async_oscillate(self, oscillating: bool) -> None:
@@ -167,10 +176,7 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity):
if direction == DIRECTION_REVERSE: if direction == DIRECTION_REVERSE:
value = self._config.get(CONF_FAN_DIRECTION_REV) 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() self.schedule_update_ha_state()
@property @property
@@ -203,18 +209,25 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity):
current_speed = self.dps_conf(CONF_FAN_SPEED_CONTROL) current_speed = self.dps_conf(CONF_FAN_SPEED_CONTROL)
if self._use_ordered_list: if self._use_ordered_list:
_LOGGER.debug( _LOGGER.debug(
"Fan current_speed ordered_list_item_to_percentage: %s from %s", "Fan current_speed ordered_list_item_to_percentage: %s from %s",
current_speed, self._ordered_list current_speed,
self._ordered_list,
) )
if current_speed is not None: if current_speed is not None:
self._percentage = ordered_list_item_to_percentage( self._percentage = ordered_list_item_to_percentage(
self._ordered_list, current_speed) self._ordered_list, current_speed
)
else: else:
_LOGGER.debug("Fan current_speed ranged_value_to_percentage: %s from %s", _LOGGER.debug(
current_speed, self._speed_range) "Fan current_speed ranged_value_to_percentage: %s from %s",
current_speed,
self._speed_range,
)
if current_speed is not None: 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) _LOGGER.debug("Fan current_percentage: %s", self._percentage)