Address various review notes

This commit is contained in:
Michael Kowalchuk
2021-11-15 20:50:37 -08:00
parent 468b004652
commit aed4df6cb7
2 changed files with 39 additions and 28 deletions

View File

@@ -265,11 +265,20 @@ class FanSpeedDataTemplate:
actual speed. actual speed.
""" """
# pylint: disable=no-self-use # pylint: disable=no-self-use
return None raise NotImplementedError
@dataclass @dataclass
class FixedFanSpeedDataTemplate(BaseDiscoverySchemaDataTemplate, FanSpeedDataTemplate): class FixedFanSpeedValueMix:
"""Mixin data class for defining supported fan speeds."""
speeds: list[int]
@dataclass
class FixedFanSpeedDataTemplate(
BaseDiscoverySchemaDataTemplate, FanSpeedDataTemplate, FixedFanSpeedValueMix
):
""" """
Specifies a fixed set of fan speeds. Specifies a fixed set of fan speeds.
@@ -287,16 +296,22 @@ class FixedFanSpeedDataTemplate(BaseDiscoverySchemaDataTemplate, FanSpeedDataTem
for each actual speed. for each actual speed.
""" """
speeds: list[int] = field(default_factory=list)
def get_speed_config(self, resolved_data: dict[str, Any]) -> list[int] | None: def get_speed_config(self, resolved_data: dict[str, Any]) -> list[int] | None:
"""Get the fan speed configuration for this device.""" """Get the fan speed configuration for this device."""
return self.speeds return self.speeds
@dataclass
class ConfigurableFanSpeedValueMix:
"""Mixin data class for defining configurable fan speeds."""
configuration_option: ZwaveValueID
configuration_value_to_speeds: dict[int, list[int]]
@dataclass @dataclass
class ConfigurableFanSpeedDataTemplate( class ConfigurableFanSpeedDataTemplate(
BaseDiscoverySchemaDataTemplate, FanSpeedDataTemplate BaseDiscoverySchemaDataTemplate, FanSpeedDataTemplate, ConfigurableFanSpeedValueMix
): ):
""" """
Gets fan speeds based on a configuration value. Gets fan speeds based on a configuration value.
@@ -322,9 +337,6 @@ class ConfigurableFanSpeedDataTemplate(
the underlying switch for each actual speed. the underlying switch for each actual speed.
""" """
configuration_option: ZwaveValueID | None = None
configuration_value_to_speeds: dict[int, list[int]] = field(default_factory=dict)
def resolve_data(self, value: ZwaveValue) -> dict[str, Any]: def resolve_data(self, value: ZwaveValue) -> dict[str, Any]:
"""Resolve helper class data for a discovered value.""" """Resolve helper class data for a discovered value."""
if not self.configuration_option: if not self.configuration_option:

View File

@@ -63,21 +63,23 @@ async def async_setup_entry(
class ZwaveFan(ZWaveBaseEntity, FanEntity): class ZwaveFan(ZWaveBaseEntity, FanEntity):
"""Representation of a Z-Wave fan.""" """Representation of a Z-Wave fan."""
async def async_set_percentage(self, percentage: int | None) -> None: def __init__(
"""Set the speed percentage of the fan.""" self, config_entry: ConfigEntry, client: ZwaveClient, info: ZwaveDiscoveryInfo
target_value = self.get_zwave_value(TARGET_VALUE_PROPERTY) ) -> None:
"""Initialize the fan."""
super().__init__(config_entry, client, info)
self._target_value = self.get_zwave_value(TARGET_VALUE_PROPERTY)
if percentage is None: async def async_set_percentage(self, percentage: int) -> None:
# Value 255 tells device to return to previous value """Set the speed percentage of the fan."""
zwave_speed = 255 if percentage == 0:
elif percentage == 0:
zwave_speed = 0 zwave_speed = 0
else: else:
zwave_speed = math.ceil( zwave_speed = math.ceil(
percentage_to_ranged_value(DEFAULT_SPEED_RANGE, percentage) percentage_to_ranged_value(DEFAULT_SPEED_RANGE, percentage)
) )
await self.info.node.async_set_value(target_value, zwave_speed) await self.info.node.async_set_value(self._target_value, zwave_speed)
async def async_turn_on( async def async_turn_on(
self, self,
@@ -87,12 +89,15 @@ class ZwaveFan(ZWaveBaseEntity, FanEntity):
**kwargs: Any, **kwargs: Any,
) -> None: ) -> None:
"""Turn the device on.""" """Turn the device on."""
await self.async_set_percentage(percentage) if percentage is None:
# Value 255 tells device to return to previous value
await self.info.node.async_set_value(self._target_value, 255)
else:
await self.async_set_percentage(percentage)
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the device off.""" """Turn the device off."""
target_value = self.get_zwave_value(TARGET_VALUE_PROPERTY) await self.info.node.async_set_value(self._target_value, 0)
await self.info.node.async_set_value(target_value, 0)
@property @property
def is_on(self) -> bool | None: # type: ignore def is_on(self) -> bool | None: # type: ignore
@@ -140,25 +145,19 @@ class ConfiguredSpeedRangeZwaveFan(ZwaveFan):
FanSpeedDataTemplate, self.info.platform_data_template FanSpeedDataTemplate, self.info.platform_data_template
) )
async def async_set_percentage(self, percentage: int | None) -> None: async def async_set_percentage(self, percentage: int) -> None:
"""Set the speed percentage of the fan.""" """Set the speed percentage of the fan."""
target_value = self.get_zwave_value(TARGET_VALUE_PROPERTY)
# Entity should be unavailable if this isn't set # Entity should be unavailable if this isn't set
assert self.speed_configuration assert self.speed_configuration
if percentage is None: if percentage == 0:
# Value 255 tells device to return to previous value
zwave_speed = 255
elif percentage == 0:
zwave_speed = 0 zwave_speed = 0
else: else:
assert 0 <= percentage <= 100
zwave_speed = self.speed_configuration[ zwave_speed = self.speed_configuration[
math.ceil(percentage / self.percentage_step) - 1 math.ceil(percentage / self.percentage_step) - 1
] ]
await self.info.node.async_set_value(target_value, zwave_speed) await self.info.node.async_set_value(self._target_value, zwave_speed)
@property @property
def available(self) -> bool: def available(self) -> bool: