2018-12-17 20:50:56 +01:00
|
|
|
"""Support for ESPHome switches."""
|
2021-03-17 23:49:01 +01:00
|
|
|
from __future__ import annotations
|
2019-05-29 13:33:49 +02:00
|
|
|
|
2021-07-12 22:56:10 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2023-06-21 15:41:24 +02:00
|
|
|
from aioesphomeapi import EntityInfo, SwitchInfo, SwitchState
|
2018-12-17 20:50:56 +01:00
|
|
|
|
2022-11-29 14:07:56 +01:00
|
|
|
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
|
2018-12-17 20:50:56 +01:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-06-21 15:41:24 +02:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-07-12 22:56:10 +02:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-02-02 12:34:01 +01:00
|
|
|
from homeassistant.util.enum import try_parse_enum
|
2018-12-17 20:50:56 +01:00
|
|
|
|
2023-08-10 14:27:03 +02:00
|
|
|
from .entity import EsphomeEntity, esphome_state_property, platform_async_setup_entry
|
2018-12-17 20:50:56 +01:00
|
|
|
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
async def async_setup_entry(
|
2021-07-12 22:56:10 +02:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2019-07-31 12:25:30 -07:00
|
|
|
) -> None:
|
2018-12-17 20:50:56 +01:00
|
|
|
"""Set up ESPHome switches based on a config entry."""
|
|
|
|
await platform_async_setup_entry(
|
2019-07-31 12:25:30 -07:00
|
|
|
hass,
|
|
|
|
entry,
|
|
|
|
async_add_entities,
|
|
|
|
info_type=SwitchInfo,
|
|
|
|
entity_type=EsphomeSwitch,
|
|
|
|
state_type=SwitchState,
|
2018-12-17 20:50:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-07-12 22:56:10 +02:00
|
|
|
class EsphomeSwitch(EsphomeEntity[SwitchInfo, SwitchState], SwitchEntity):
|
|
|
|
"""A switch implementation for ESPHome."""
|
2018-12-17 20:50:56 +01:00
|
|
|
|
2023-06-21 15:41:24 +02:00
|
|
|
@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
|
|
|
|
)
|
2018-12-18 19:04:50 +01:00
|
|
|
|
2022-09-26 22:10:06 +02:00
|
|
|
@property
|
2019-04-16 22:48:46 +02:00
|
|
|
@esphome_state_property
|
2022-01-23 11:31:01 +01:00
|
|
|
def is_on(self) -> bool | None:
|
2018-12-17 20:50:56 +01:00
|
|
|
"""Return true if the switch is on."""
|
|
|
|
return self._state.state
|
|
|
|
|
2021-07-12 22:56:10 +02:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2018-12-17 20:50:56 +01:00
|
|
|
"""Turn the entity on."""
|
2023-06-21 15:41:24 +02:00
|
|
|
await self._client.switch_command(self._key, True)
|
2018-12-17 20:50:56 +01:00
|
|
|
|
2021-07-12 22:56:10 +02:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2018-12-17 20:50:56 +01:00
|
|
|
"""Turn the entity off."""
|
2023-06-21 15:41:24 +02:00
|
|
|
await self._client.switch_command(self._key, False)
|