2018-12-17 19:50:56 +00:00
|
|
|
"""Support for ESPHome switches."""
|
2021-03-17 22:49:01 +00:00
|
|
|
from __future__ import annotations
|
2019-05-29 11:33:49 +00:00
|
|
|
|
2021-07-12 20:56:10 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2019-05-29 11:33:49 +00:00
|
|
|
from aioesphomeapi import SwitchInfo, SwitchState
|
2018-12-17 19:50:56 +00:00
|
|
|
|
2022-02-03 20:23:30 +00:00
|
|
|
from homeassistant.components.switch import DEVICE_CLASSES, SwitchEntity
|
2018-12-17 19:50:56 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-04-21 10:18:42 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-07-12 20:56:10 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-12-17 19:50:56 +00:00
|
|
|
|
2019-05-29 11:33:49 +00:00
|
|
|
from . import EsphomeEntity, esphome_state_property, platform_async_setup_entry
|
2018-12-17 19:50:56 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_entry(
|
2021-07-12 20:56:10 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2019-07-31 19:25:30 +00:00
|
|
|
) -> None:
|
2018-12-17 19:50:56 +00:00
|
|
|
"""Set up ESPHome switches based on a config entry."""
|
|
|
|
await platform_async_setup_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass,
|
|
|
|
entry,
|
|
|
|
async_add_entities,
|
|
|
|
component_key="switch",
|
|
|
|
info_type=SwitchInfo,
|
|
|
|
entity_type=EsphomeSwitch,
|
|
|
|
state_type=SwitchState,
|
2018-12-17 19:50:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-07-12 20:56:10 +00:00
|
|
|
class EsphomeSwitch(EsphomeEntity[SwitchInfo, SwitchState], SwitchEntity):
|
|
|
|
"""A switch implementation for ESPHome."""
|
2018-12-17 19:50:56 +00:00
|
|
|
|
2018-12-18 18:04:50 +00:00
|
|
|
@property
|
|
|
|
def assumed_state(self) -> bool:
|
|
|
|
"""Return true if we do optimistic updates."""
|
2019-04-08 13:44:24 +00:00
|
|
|
return self._static_info.assumed_state
|
2018-12-18 18:04:50 +00:00
|
|
|
|
2022-08-26 08:52:05 +00:00
|
|
|
@property # type: ignore[misc]
|
2019-04-16 20:48:46 +00:00
|
|
|
@esphome_state_property
|
2022-01-23 10:31:01 +00:00
|
|
|
def is_on(self) -> bool | None:
|
2018-12-17 19:50:56 +00:00
|
|
|
"""Return true if the switch is on."""
|
|
|
|
return self._state.state
|
|
|
|
|
2022-02-03 20:23:30 +00:00
|
|
|
@property
|
|
|
|
def device_class(self) -> str | None:
|
|
|
|
"""Return the class of this device."""
|
|
|
|
if self._static_info.device_class not in DEVICE_CLASSES:
|
|
|
|
return None
|
|
|
|
return self._static_info.device_class
|
|
|
|
|
2021-07-12 20:56:10 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2018-12-17 19:50:56 +00:00
|
|
|
"""Turn the entity on."""
|
|
|
|
await self._client.switch_command(self._static_info.key, True)
|
|
|
|
|
2021-07-12 20:56:10 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2018-12-17 19:50:56 +00:00
|
|
|
"""Turn the entity off."""
|
|
|
|
await self._client.switch_command(self._static_info.key, False)
|