From 2c6aa80bc7db5e473f02d0eebafc1910db3c3e43 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 13 Jan 2024 10:28:43 -1000 Subject: [PATCH] Use more shorthand attributes in ESPHome fans (#107923) --- homeassistant/components/esphome/fan.py | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/esphome/fan.py b/homeassistant/components/esphome/fan.py index 08135e1a702..4c44134374a 100644 --- a/homeassistant/components/esphome/fan.py +++ b/homeassistant/components/esphome/fan.py @@ -53,10 +53,7 @@ _FAN_DIRECTIONS: EsphomeEnumMapper[FanDirection, str] = EsphomeEnumMapper( class EsphomeFan(EsphomeEntity[FanInfo, FanState], FanEntity): """A fan implementation for ESPHome.""" - @property - def _supports_speed_levels(self) -> bool: - api_version = self._api_version - return api_version.major == 1 and api_version.minor > 3 + _supports_speed_levels: bool = True async def async_set_percentage(self, percentage: int) -> None: """Set the speed percentage of the fan.""" @@ -129,13 +126,6 @@ class EsphomeFan(EsphomeEntity[FanInfo, FanState], FanEntity): (1, self._static_info.supported_speed_levels), self._state.speed_level ) - @property - def speed_count(self) -> int: - """Return the number of speeds the fan supports.""" - if not self._supports_speed_levels: - return len(ORDERED_NAMED_FAN_SPEEDS) - return self._static_info.supported_speed_levels - @property @esphome_state_property def oscillating(self) -> bool | None: @@ -154,16 +144,14 @@ class EsphomeFan(EsphomeEntity[FanInfo, FanState], FanEntity): """Return the current fan preset mode.""" return self._state.preset_mode - @property - def preset_modes(self) -> list[str] | None: - """Return the supported fan preset modes.""" - return self._static_info.supported_preset_modes - @callback def _on_static_info_update(self, static_info: EntityInfo) -> None: """Set attrs from static info.""" super()._on_static_info_update(static_info) static_info = self._static_info + api_version = self._api_version + supports_speed_levels = api_version.major == 1 and api_version.minor > 3 + self._supports_speed_levels = supports_speed_levels flags = FanEntityFeature(0) if static_info.supports_oscillation: flags |= FanEntityFeature.OSCILLATE @@ -174,3 +162,8 @@ class EsphomeFan(EsphomeEntity[FanInfo, FanState], FanEntity): if static_info.supported_preset_modes: flags |= FanEntityFeature.PRESET_MODE self._attr_supported_features = flags + self._attr_preset_modes = static_info.supported_preset_modes + if not supports_speed_levels: + self._attr_speed_count = len(ORDERED_NAMED_FAN_SPEEDS) + else: + self._attr_speed_count = static_info.supported_speed_levels