fix tox errors
This commit is contained in:
@@ -1,46 +1,46 @@
|
|||||||
"""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_PRESET_MODE,
|
SUPPORT_PRESET_MODE,
|
||||||
|
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_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 (
|
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_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__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def flow_schema(dps):
|
def flow_schema(dps):
|
||||||
"""Return schema used in config flow."""
|
"""Return schema used in config flow."""
|
||||||
return {
|
return {
|
||||||
@@ -55,9 +55,11 @@ def flow_schema(dps):
|
|||||||
vol.Optional(CONF_FAN_ORDERED_LIST): cv.string,
|
vol.Optional(CONF_FAN_ORDERED_LIST): cv.string,
|
||||||
vol.Optional(CONF_FAN_PRESET_LIST): cv.string,
|
vol.Optional(CONF_FAN_PRESET_LIST): cv.string,
|
||||||
vol.Optional(CONF_FAN_SPEED_DPS_TYPE, default="string"): vol.In(
|
vol.Optional(CONF_FAN_SPEED_DPS_TYPE, default="string"): vol.In(
|
||||||
["string", "integer", "list"]),
|
["string", "integer", "list"]
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class LocaltuyaFan(LocalTuyaEntity, FanEntity):
|
class LocaltuyaFan(LocalTuyaEntity, FanEntity):
|
||||||
"""Representation of a Tuya fan."""
|
"""Representation of a Tuya fan."""
|
||||||
|
|
||||||
@@ -76,20 +78,26 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity):
|
|||||||
self._percentage = None
|
self._percentage = None
|
||||||
self._preset = None
|
self._preset = None
|
||||||
self._speed_range = (
|
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._config.get(CONF_FAN_SPEED_MAX),
|
||||||
)
|
)
|
||||||
self._ordered_list = self._config.get(CONF_FAN_ORDERED_LIST).replace(" ","").split(",")
|
self._ordered_list = (
|
||||||
self._preset_list = self._config.get(CONF_FAN_PRESET_LIST).replace(" ","").split(",")
|
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)
|
self._ordered_speed_dps_type = self._config.get(CONF_FAN_SPEED_DPS_TYPE)
|
||||||
|
|
||||||
if (self._ordered_speed_dps_type == "list"
|
if (
|
||||||
and isinstance(self._ordered_list, list)
|
self._ordered_speed_dps_type == "list"
|
||||||
and len(self._ordered_list) > 1):
|
and isinstance(self._ordered_list, list)
|
||||||
|
and len(self._ordered_list) > 1
|
||||||
|
):
|
||||||
_LOGGER.debug("Fan _use_ordered_list: %s", self._ordered_list)
|
_LOGGER.debug("Fan _use_ordered_list: %s", self._ordered_list)
|
||||||
else:
|
else:
|
||||||
_LOGGER.debug("Fan _use_ordered_list: Not a valid list")
|
_LOGGER.debug("Fan _use_ordered_list: Not a valid list")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def oscillating(self):
|
def oscillating(self):
|
||||||
"""Return current oscillating status."""
|
"""Return current oscillating status."""
|
||||||
@@ -115,7 +123,7 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity):
|
|||||||
_LOGGER.debug("Fan async_turn_on")
|
_LOGGER.debug("Fan async_turn_on")
|
||||||
await self._device.set_dp(True, self._dp_id)
|
await self._device.set_dp(True, self._dp_id)
|
||||||
if percentage is not None:
|
if percentage is not None:
|
||||||
await self.async_set_percentage(speed)
|
await self.async_set_percentage(percentage)
|
||||||
else:
|
else:
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
@@ -128,44 +136,50 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity):
|
|||||||
|
|
||||||
async def async_set_percentage(self, percentage):
|
async def async_set_percentage(self, percentage):
|
||||||
"""Set the speed of the fan."""
|
"""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 is not None:
|
||||||
if percentage == 0:
|
if percentage == 0:
|
||||||
return await self.async_turn_off()
|
return await self.async_turn_off()
|
||||||
|
|
||||||
if not self.is_on:
|
if not self.is_on:
|
||||||
await self.async_turn_on()
|
await self.async_turn_on()
|
||||||
|
|
||||||
if self._ordered_speed_dps_type == "string":
|
if self._ordered_speed_dps_type == "string":
|
||||||
send_speed = str(math.ceil(
|
send_speed = str(
|
||||||
percentage_to_ranged_value(self._speed_range, percentage)))
|
math.ceil(percentage_to_ranged_value(self._speed_range, percentage))
|
||||||
|
)
|
||||||
await self._device.set_dp(
|
await self._device.set_dp(
|
||||||
send_speed, self._config.get(CONF_FAN_SPEED_CONTROL))
|
send_speed, self._config.get(CONF_FAN_SPEED_CONTROL)
|
||||||
_LOGGER.debug("Fan async_set_percentage: %s > %s",
|
)
|
||||||
percentage, send_speed)
|
_LOGGER.debug(
|
||||||
|
"Fan async_set_percentage: %s > %s", percentage, send_speed
|
||||||
|
)
|
||||||
|
|
||||||
elif self._ordered_speed_dps_type == "integer":
|
elif self._ordered_speed_dps_type == "integer":
|
||||||
send_speed = int(math.ceil(
|
send_speed = int(
|
||||||
percentage_to_ranged_value(self._speed_range, percentage)))
|
math.ceil(percentage_to_ranged_value(self._speed_range, percentage))
|
||||||
|
)
|
||||||
await self._device.set_dp(
|
await self._device.set_dp(
|
||||||
send_speed, self._config.get(CONF_FAN_SPEED_CONTROL))
|
send_speed, self._config.get(CONF_FAN_SPEED_CONTROL)
|
||||||
_LOGGER.debug("Fan async_set_percentage: %s > %s",
|
)
|
||||||
percentage, send_speed)
|
_LOGGER.debug(
|
||||||
|
"Fan async_set_percentage: %s > %s", percentage, send_speed
|
||||||
|
)
|
||||||
|
|
||||||
elif self._ordered_speed_dps_type == "list":
|
elif self._ordered_speed_dps_type == "list":
|
||||||
send_speed = str(
|
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(
|
await self._device.set_dp(
|
||||||
send_speed, self._config.get(CONF_FAN_SPEED_CONTROL))
|
send_speed, self._config.get(CONF_FAN_SPEED_CONTROL)
|
||||||
_LOGGER.debug("Fan async_set_percentage: %s > %s",
|
)
|
||||||
percentage, send_speed)
|
_LOGGER.debug(
|
||||||
|
"Fan async_set_percentage: %s > %s", percentage, send_speed
|
||||||
|
)
|
||||||
|
|
||||||
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:
|
||||||
"""Set oscillation."""
|
"""Set oscillation."""
|
||||||
_LOGGER.debug("Fan async_oscillate: %s", oscillating)
|
_LOGGER.debug("Fan async_oscillate: %s", oscillating)
|
||||||
@@ -180,17 +194,15 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity):
|
|||||||
|
|
||||||
if direction == DIRECTION_FORWARD:
|
if direction == DIRECTION_FORWARD:
|
||||||
value = self._config.get(CONF_FAN_DIRECTION_FWD)
|
value = self._config.get(CONF_FAN_DIRECTION_FWD)
|
||||||
|
|
||||||
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(
|
await self._device.set_dp(value, self._config.get(CONF_FAN_DIRECTION))
|
||||||
value, self._config.get(CONF_FAN_DIRECTION)
|
self.schedule_update_ha_state()
|
||||||
)
|
|
||||||
self.schedule_update_ha_state()
|
|
||||||
|
|
||||||
async def async_set_preset_mode(self, preset_mode: str) -> None:
|
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)
|
_LOGGER.debug("Fan set preset: %s", preset_mode)
|
||||||
await self._device.set_dp(
|
await self._device.set_dp(
|
||||||
preset_mode, self._config.get(CONF_FAN_PRESET_CONTROL)
|
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):
|
if self.has_config(CONF_FAN_PRESET_CONTROL):
|
||||||
features |= SUPPORT_PRESET_MODE
|
features |= SUPPORT_PRESET_MODE
|
||||||
|
|
||||||
return features
|
return features
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def speed_count(self) -> int:
|
def speed_count(self) -> int:
|
||||||
"""Speed count for the fan."""
|
"""Speed count for the fan."""
|
||||||
@@ -223,38 +235,57 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity):
|
|||||||
_LOGGER.debug("Fan speed_count: %s", speed_count)
|
_LOGGER.debug("Fan speed_count: %s", speed_count)
|
||||||
return speed_count
|
return speed_count
|
||||||
|
|
||||||
|
|
||||||
def status_updated(self):
|
def status_updated(self):
|
||||||
"""Get state of Tuya fan."""
|
"""Get state of Tuya fan."""
|
||||||
|
|
||||||
self._is_on = self.dps(self._dp_id)
|
self._is_on = self.dps(self._dp_id)
|
||||||
|
|
||||||
if self.has_config(CONF_FAN_PRESET_CONTROL):
|
if self.has_config(CONF_FAN_PRESET_CONTROL):
|
||||||
current_preset = self.dps_conf(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:
|
if current_preset is not None and current_preset in self._preset_list:
|
||||||
_LOGGER.debug("Fan current_preset in preset list: %s from %s",
|
_LOGGER.debug(
|
||||||
current_preset, self._preset_list)
|
"Fan current_preset in preset list: %s from %s",
|
||||||
|
current_preset,
|
||||||
|
self._preset_list,
|
||||||
|
)
|
||||||
self._preset = current_preset
|
self._preset = current_preset
|
||||||
|
|
||||||
current_speed = self.dps_conf(CONF_FAN_SPEED_CONTROL)
|
current_speed = self.dps_conf(CONF_FAN_SPEED_CONTROL)
|
||||||
if current_speed is not None:
|
if current_speed is not None:
|
||||||
|
|
||||||
if (self.has_config(CONF_FAN_PRESET_CONTROL)
|
if (
|
||||||
and (CONF_FAN_SPEED_CONTROL == CONF_FAN_PRESET_CONTROL)
|
self.has_config(CONF_FAN_PRESET_CONTROL)
|
||||||
and (current_speed in self._preset_list)):
|
and (CONF_FAN_SPEED_CONTROL == CONF_FAN_PRESET_CONTROL)
|
||||||
_LOGGER.debug("Fan current_speed in preset list: %s from %s",
|
and (current_speed in self._preset_list)
|
||||||
current_speed, self._preset_list)
|
):
|
||||||
|
_LOGGER.debug(
|
||||||
|
"Fan current_speed in preset list: %s from %s",
|
||||||
|
current_speed,
|
||||||
|
self._preset_list,
|
||||||
|
)
|
||||||
self._preset = current_speed
|
self._preset = current_speed
|
||||||
|
|
||||||
elif self._ordered_speed_dps_type == "list":
|
elif self._ordered_speed_dps_type == "list":
|
||||||
_LOGGER.debug("Fan current_speed ordered_list_item_to_percentage: %s from %s",
|
_LOGGER.debug(
|
||||||
current_speed, self._ordered_list)
|
"Fan current_speed ordered_list_item_to_percentage: %s from %s",
|
||||||
self._percentage = ordered_list_item_to_percentage(self._ordered_list, current_speed)
|
current_speed,
|
||||||
|
self._ordered_list,
|
||||||
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",
|
self._percentage = ordered_list_item_to_percentage(
|
||||||
current_speed, self._speed_range)
|
self._ordered_list, current_speed
|
||||||
self._percentage = ranged_value_to_percentage(self._speed_range, int(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_percentage: %s", self._percentage)
|
||||||
_LOGGER.debug("Fan current_preset: %s", self._preset)
|
_LOGGER.debug("Fan current_preset: %s", self._preset)
|
||||||
@@ -268,10 +299,10 @@ class LocaltuyaFan(LocalTuyaEntity, FanEntity):
|
|||||||
if value is not None:
|
if value is not None:
|
||||||
if value == self._config.get(CONF_FAN_DIRECTION_FWD):
|
if value == self._config.get(CONF_FAN_DIRECTION_FWD):
|
||||||
self._direction = DIRECTION_FORWARD
|
self._direction = DIRECTION_FORWARD
|
||||||
|
|
||||||
if value == self._config.get(CONF_FAN_DIRECTION_REV):
|
if value == self._config.get(CONF_FAN_DIRECTION_REV):
|
||||||
self._direction = DIRECTION_REVERSE
|
self._direction = DIRECTION_REVERSE
|
||||||
_LOGGER.debug("Fan current_direction : %s > %s", value, self._direction)
|
_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)
|
||||||
|
Reference in New Issue
Block a user