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.
"""
# pylint: disable=no-self-use
return None
raise NotImplementedError
@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.
@@ -287,16 +296,22 @@ class FixedFanSpeedDataTemplate(BaseDiscoverySchemaDataTemplate, FanSpeedDataTem
for each actual speed.
"""
speeds: list[int] = field(default_factory=list)
def get_speed_config(self, resolved_data: dict[str, Any]) -> list[int] | None:
"""Get the fan speed configuration for this device."""
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
class ConfigurableFanSpeedDataTemplate(
BaseDiscoverySchemaDataTemplate, FanSpeedDataTemplate
BaseDiscoverySchemaDataTemplate, FanSpeedDataTemplate, ConfigurableFanSpeedValueMix
):
"""
Gets fan speeds based on a configuration value.
@@ -322,9 +337,6 @@ class ConfigurableFanSpeedDataTemplate(
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]:
"""Resolve helper class data for a discovered value."""
if not self.configuration_option:

View File

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