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
|
|
|
|
|
2019-05-29 13:33:49 +02:00
|
|
|
from aioesphomeapi import SwitchInfo, SwitchState
|
2018-12-17 20:50:56 +01:00
|
|
|
|
2020-04-26 18:50:37 +02:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2018-12-17 20:50:56 +01:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-04-21 12:18:42 +02:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-07-12 22:56:10 +02:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-12-17 20:50:56 +01:00
|
|
|
|
2019-05-29 13:33:49 +02:00
|
|
|
from . 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,
|
|
|
|
component_key="switch",
|
|
|
|
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
|
|
|
# https://github.com/PyCQA/pylint/issues/3150 for all @esphome_state_property
|
2021-07-15 06:44:57 +02:00
|
|
|
# pylint: disable=invalid-overridden-method
|
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
|
|
|
|
2018-12-18 19:04:50 +01:00
|
|
|
@property
|
|
|
|
def assumed_state(self) -> bool:
|
|
|
|
"""Return true if we do optimistic updates."""
|
2019-04-08 15:44:24 +02:00
|
|
|
return self._static_info.assumed_state
|
2018-12-18 19:04:50 +01:00
|
|
|
|
2019-04-16 22:48:46 +02:00
|
|
|
@esphome_state_property
|
2021-07-12 22:56:10 +02:00
|
|
|
def is_on(self) -> bool | None: # type: ignore[override]
|
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."""
|
|
|
|
await self._client.switch_command(self._static_info.key, True)
|
|
|
|
|
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."""
|
|
|
|
await self._client.switch_command(self._static_info.key, False)
|