mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 06:17:07 +00:00
Migrate esphome switch platform to use _on_static_info_update (#94962)
This commit is contained in:
parent
e24c2ae55c
commit
367644afe1
@ -3,11 +3,11 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from aioesphomeapi import SwitchInfo, SwitchState
|
from aioesphomeapi import EntityInfo, SwitchInfo, SwitchState
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
|
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
|
||||||
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.enum import try_parse_enum
|
from homeassistant.util.enum import try_parse_enum
|
||||||
|
|
||||||
@ -32,10 +32,15 @@ async def async_setup_entry(
|
|||||||
class EsphomeSwitch(EsphomeEntity[SwitchInfo, SwitchState], SwitchEntity):
|
class EsphomeSwitch(EsphomeEntity[SwitchInfo, SwitchState], SwitchEntity):
|
||||||
"""A switch implementation for ESPHome."""
|
"""A switch implementation for ESPHome."""
|
||||||
|
|
||||||
@property
|
@callback
|
||||||
def assumed_state(self) -> bool:
|
def _on_static_info_update(self, static_info: EntityInfo) -> None:
|
||||||
"""Return true if we do optimistic updates."""
|
"""Set attrs from static info."""
|
||||||
return self._static_info.assumed_state
|
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
|
@property
|
||||||
@esphome_state_property
|
@esphome_state_property
|
||||||
@ -43,15 +48,10 @@ class EsphomeSwitch(EsphomeEntity[SwitchInfo, SwitchState], SwitchEntity):
|
|||||||
"""Return true if the switch is on."""
|
"""Return true if the switch is on."""
|
||||||
return self._state.state
|
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:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the entity on."""
|
"""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:
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
"""Turn the entity off."""
|
"""Turn the entity off."""
|
||||||
await self._client.switch_command(self._static_info.key, False)
|
await self._client.switch_command(self._key, False)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user