1 """Component to pressing a button as platforms."""
3 from __future__
import annotations
5 from datetime
import timedelta
6 from enum
import StrEnum
8 from typing
import final
10 from propcache
import cached_property
11 import voluptuous
as vol
24 from .const
import DOMAIN, SERVICE_PRESS
26 _LOGGER = logging.getLogger(__name__)
28 DATA_COMPONENT: HassKey[EntityComponent[ButtonEntity]] =
HassKey(DOMAIN)
29 ENTITY_ID_FORMAT = DOMAIN +
".{}"
30 PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA
31 PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE
38 """Device class for buttons."""
45 DEVICE_CLASSES_SCHEMA = vol.All(vol.Lower, vol.Coerce(ButtonDeviceClass))
50 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
51 """Set up Button entities."""
52 component = hass.data[DATA_COMPONENT] = EntityComponent[ButtonEntity](
53 _LOGGER, DOMAIN, hass, SCAN_INTERVAL
55 await component.async_setup(config)
57 component.async_register_entity_service(
60 "_async_press_action",
67 """Set up a config entry."""
72 """Unload a config entry."""
77 """A class that describes button entities."""
79 device_class: ButtonDeviceClass |
None =
None
82 CACHED_PROPERTIES_WITH_ATTR_ = {
88 """Representation of a Button entity."""
90 entity_description: ButtonEntityDescription
91 _attr_should_poll =
False
92 _attr_device_class: ButtonDeviceClass |
None
93 _attr_state:
None =
None
94 __last_pressed_isoformat: str |
None =
None
97 """Return True if an unnamed entity should be named by its device class.
99 For buttons this is True if the entity has a device class.
105 """Return the class of this entity."""
106 if hasattr(self,
"_attr_device_class"):
107 return self._attr_device_class
108 if hasattr(self,
"entity_description"):
109 return self.entity_description.device_class
115 """Return the entity state."""
119 """Set the entity state."""
121 self.__dict__.pop(
"state",
None)
126 """Press the button (from e.g., service call).
128 Should not be overridden, handle setting last press timestamp.
130 self.
__set_state__set_state(dt_util.utcnow().isoformat())
135 """Call when the button is added to hass."""
138 if state
is not None and state.state
not in (STATE_UNAVAILABLE,
None):
142 """Press the button."""
143 raise NotImplementedError
146 """Press the button."""
147 await self.
hasshass.async_add_executor_job(self.
presspress)
str|None device_class(self)
None async_write_ha_state(self)
State|None async_get_last_state(self)