From 27da7d68de5c29eabc113f74b1b0d0a44e13bfb4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 22 Jun 2023 09:07:13 +0200 Subject: [PATCH] Migrate esphome fan platform to use _on_static_info_update (#95031) --- homeassistant/components/esphome/fan.py | 30 ++++++++++++------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/esphome/fan.py b/homeassistant/components/esphome/fan.py index 01060630964..092bdc11811 100644 --- a/homeassistant/components/esphome/fan.py +++ b/homeassistant/components/esphome/fan.py @@ -4,7 +4,7 @@ from __future__ import annotations import math from typing import Any -from aioesphomeapi import FanDirection, FanInfo, FanSpeed, FanState +from aioesphomeapi import EntityInfo, FanDirection, FanInfo, FanSpeed, FanState from homeassistant.components.fan import ( DIRECTION_FORWARD, @@ -13,7 +13,7 @@ from homeassistant.components.fan import ( FanEntityFeature, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.util.percentage import ( ordered_list_item_to_percentage, @@ -68,7 +68,7 @@ class EsphomeFan(EsphomeEntity[FanInfo, FanState], FanEntity): await self.async_turn_off() return - data: dict[str, Any] = {"key": self._static_info.key, "state": True} + data: dict[str, Any] = {"key": self._key, "state": True} if percentage is not None: if self._supports_speed_levels: data["speed_level"] = math.ceil( @@ -94,18 +94,16 @@ class EsphomeFan(EsphomeEntity[FanInfo, FanState], FanEntity): async def async_turn_off(self, **kwargs: Any) -> None: """Turn off the fan.""" - await self._client.fan_command(key=self._static_info.key, state=False) + await self._client.fan_command(key=self._key, state=False) async def async_oscillate(self, oscillating: bool) -> None: """Oscillate the fan.""" - await self._client.fan_command( - key=self._static_info.key, oscillating=oscillating - ) + await self._client.fan_command(key=self._key, oscillating=oscillating) async def async_set_direction(self, direction: str) -> None: """Set direction of the fan.""" await self._client.fan_command( - key=self._static_info.key, direction=_FAN_DIRECTIONS.from_hass(direction) + key=self._key, direction=_FAN_DIRECTIONS.from_hass(direction) ) @property @@ -153,14 +151,16 @@ class EsphomeFan(EsphomeEntity[FanInfo, FanState], FanEntity): return None return _FAN_DIRECTIONS.from_esphome(self._state.direction) - @property - def supported_features(self) -> FanEntityFeature: - """Flag supported features.""" + @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 flags = FanEntityFeature(0) - if self._static_info.supports_oscillation: + if static_info.supports_oscillation: flags |= FanEntityFeature.OSCILLATE - if self._static_info.supports_speed: + if static_info.supports_speed: flags |= FanEntityFeature.SET_SPEED - if self._static_info.supports_direction: + if static_info.supports_direction: flags |= FanEntityFeature.DIRECTION - return flags + self._attr_supported_features = flags