diff --git a/homeassistant/components/esphome/switch.py b/homeassistant/components/esphome/switch.py index 83148542435..e71853a1287 100644 --- a/homeassistant/components/esphome/switch.py +++ b/homeassistant/components/esphome/switch.py @@ -3,11 +3,11 @@ from __future__ import annotations from typing import Any -from aioesphomeapi import SwitchInfo, SwitchState +from aioesphomeapi import EntityInfo, SwitchInfo, SwitchState from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity 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.enum import try_parse_enum @@ -32,10 +32,15 @@ async def async_setup_entry( class EsphomeSwitch(EsphomeEntity[SwitchInfo, SwitchState], SwitchEntity): """A switch implementation for ESPHome.""" - @property - def assumed_state(self) -> bool: - """Return true if we do optimistic updates.""" - return self._static_info.assumed_state + @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 + self._attr_assumed_state = static_info.assumed_state + self._attr_device_class = try_parse_enum( + SwitchDeviceClass, static_info.device_class + ) @property @esphome_state_property @@ -43,15 +48,10 @@ class EsphomeSwitch(EsphomeEntity[SwitchInfo, SwitchState], SwitchEntity): """Return true if the switch is on.""" return self._state.state - @property - def device_class(self) -> SwitchDeviceClass | None: - """Return the class of this device.""" - return try_parse_enum(SwitchDeviceClass, self._static_info.device_class) - async def async_turn_on(self, **kwargs: Any) -> None: """Turn the entity on.""" - await self._client.switch_command(self._static_info.key, True) + await self._client.switch_command(self._key, True) async def async_turn_off(self, **kwargs: Any) -> None: """Turn the entity off.""" - await self._client.switch_command(self._static_info.key, False) + await self._client.switch_command(self._key, False)