Files
core/homeassistant/components/esphome/switch.py
T

66 lines
1.8 KiB
Python

"""Support for ESPHome switches."""
from functools import partial
from typing import Any, override
from aioesphomeapi import EntityInfo, SwitchInfo, SwitchState
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
from homeassistant.core import callback
from homeassistant.util.enum import try_parse_enum
from .entity import (
EsphomeEntity,
convert_api_error_ha_error,
esphome_state_property,
platform_async_setup_entry,
)
PARALLEL_UPDATES = 0
class EsphomeSwitch(EsphomeEntity[SwitchInfo, SwitchState], SwitchEntity):
"""A switch implementation for ESPHome."""
@callback
@override
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
@override
def is_on(self) -> bool:
"""Return true if the switch is on."""
return self._state.state
@convert_api_error_ha_error
@override
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the entity on."""
self._client.switch_command(
self._key, True, device_id=self._static_info.device_id
)
@convert_api_error_ha_error
@override
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the entity off."""
self._client.switch_command(
self._key, False, device_id=self._static_info.device_id
)
async_setup_entry = partial(
platform_async_setup_entry,
info_type=SwitchInfo,
entity_type=EsphomeSwitch,
state_type=SwitchState,
)