From aed4df6cb72f6fa912de27a3548e4fb586ba40d6 Mon Sep 17 00:00:00 2001 From: Michael Kowalchuk Date: Mon, 15 Nov 2021 20:50:37 -0800 Subject: [PATCH] Address various review notes --- .../zwave_js/discovery_data_template.py | 28 +++++++++---- homeassistant/components/zwave_js/fan.py | 39 +++++++++---------- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/homeassistant/components/zwave_js/discovery_data_template.py b/homeassistant/components/zwave_js/discovery_data_template.py index 6d13e823d59..49637b24a3e 100644 --- a/homeassistant/components/zwave_js/discovery_data_template.py +++ b/homeassistant/components/zwave_js/discovery_data_template.py @@ -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: diff --git a/homeassistant/components/zwave_js/fan.py b/homeassistant/components/zwave_js/fan.py index bfe9a2de5d9..ca059e0bfd2 100644 --- a/homeassistant/components/zwave_js/fan.py +++ b/homeassistant/components/zwave_js/fan.py @@ -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: