2021-11-29 08:57:37 +00:00
|
|
|
"""Support for ESPHome buttons."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-11-30 10:54:06 +00:00
|
|
|
from contextlib import suppress
|
2021-11-29 08:57:37 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2021-11-30 07:33:14 +00:00
|
|
|
from aioesphomeapi import ButtonInfo, EntityState
|
2021-11-29 08:57:37 +00:00
|
|
|
|
2021-11-30 10:54:06 +00:00
|
|
|
from homeassistant.components.button import ButtonDeviceClass, ButtonEntity
|
2021-11-29 08:57:37 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
|
|
|
from . import EsphomeEntity, platform_async_setup_entry
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
|
|
|
"""Set up ESPHome buttons based on a config entry."""
|
|
|
|
await platform_async_setup_entry(
|
|
|
|
hass,
|
|
|
|
entry,
|
|
|
|
async_add_entities,
|
|
|
|
component_key="button",
|
|
|
|
info_type=ButtonInfo,
|
|
|
|
entity_type=EsphomeButton,
|
|
|
|
state_type=EntityState,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class EsphomeButton(EsphomeEntity[ButtonInfo, EntityState], ButtonEntity):
|
|
|
|
"""A button implementation for ESPHome."""
|
|
|
|
|
2021-11-30 07:33:14 +00:00
|
|
|
@property
|
2021-11-30 10:54:06 +00:00
|
|
|
def device_class(self) -> ButtonDeviceClass | None:
|
|
|
|
"""Return the class of this entity."""
|
|
|
|
with suppress(ValueError):
|
|
|
|
return ButtonDeviceClass(self._static_info.device_class)
|
|
|
|
return None
|
2021-11-30 07:33:14 +00:00
|
|
|
|
2021-11-29 08:57:37 +00:00
|
|
|
@callback
|
|
|
|
def _on_device_update(self) -> None:
|
|
|
|
"""Update the entity state when device info has changed."""
|
|
|
|
# This override the EsphomeEntity method as the button entity
|
|
|
|
# never gets a state update.
|
|
|
|
self._on_state_update()
|
|
|
|
|
|
|
|
async def async_press(self, **kwargs: Any) -> None:
|
|
|
|
"""Press the button."""
|
|
|
|
await self._client.button_command(self._static_info.key)
|