Migrate esphome fan platform to use _on_static_info_update (#95031)

This commit is contained in:
J. Nick Koston 2023-06-22 09:07:13 +02:00 committed by GitHub
parent b700400183
commit 27da7d68de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,7 +4,7 @@ from __future__ import annotations
import math import math
from typing import Any from typing import Any
from aioesphomeapi import FanDirection, FanInfo, FanSpeed, FanState from aioesphomeapi import EntityInfo, FanDirection, FanInfo, FanSpeed, FanState
from homeassistant.components.fan import ( from homeassistant.components.fan import (
DIRECTION_FORWARD, DIRECTION_FORWARD,
@ -13,7 +13,7 @@ from homeassistant.components.fan import (
FanEntityFeature, FanEntityFeature,
) )
from homeassistant.config_entries import ConfigEntry 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.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.percentage import ( from homeassistant.util.percentage import (
ordered_list_item_to_percentage, ordered_list_item_to_percentage,
@ -68,7 +68,7 @@ class EsphomeFan(EsphomeEntity[FanInfo, FanState], FanEntity):
await self.async_turn_off() await self.async_turn_off()
return 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 percentage is not None:
if self._supports_speed_levels: if self._supports_speed_levels:
data["speed_level"] = math.ceil( data["speed_level"] = math.ceil(
@ -94,18 +94,16 @@ class EsphomeFan(EsphomeEntity[FanInfo, FanState], FanEntity):
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the fan.""" """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: async def async_oscillate(self, oscillating: bool) -> None:
"""Oscillate the fan.""" """Oscillate the fan."""
await self._client.fan_command( await self._client.fan_command(key=self._key, oscillating=oscillating)
key=self._static_info.key, oscillating=oscillating
)
async def async_set_direction(self, direction: str) -> None: async def async_set_direction(self, direction: str) -> None:
"""Set direction of the fan.""" """Set direction of the fan."""
await self._client.fan_command( 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 @property
@ -153,14 +151,16 @@ class EsphomeFan(EsphomeEntity[FanInfo, FanState], FanEntity):
return None return None
return _FAN_DIRECTIONS.from_esphome(self._state.direction) return _FAN_DIRECTIONS.from_esphome(self._state.direction)
@property @callback
def supported_features(self) -> FanEntityFeature: def _on_static_info_update(self, static_info: EntityInfo) -> None:
"""Flag supported features.""" """Set attrs from static info."""
super()._on_static_info_update(static_info)
static_info = self._static_info
flags = FanEntityFeature(0) flags = FanEntityFeature(0)
if self._static_info.supports_oscillation: if static_info.supports_oscillation:
flags |= FanEntityFeature.OSCILLATE flags |= FanEntityFeature.OSCILLATE
if self._static_info.supports_speed: if static_info.supports_speed:
flags |= FanEntityFeature.SET_SPEED flags |= FanEntityFeature.SET_SPEED
if self._static_info.supports_direction: if static_info.supports_direction:
flags |= FanEntityFeature.DIRECTION flags |= FanEntityFeature.DIRECTION
return flags self._attr_supported_features = flags