Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Support for TPLink button entities."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from typing import Final
7 
8 from kasa import Feature
9 
11  DOMAIN as BUTTON_DOMAIN,
12  ButtonDeviceClass,
13  ButtonEntity,
14  ButtonEntityDescription,
15 )
16 from homeassistant.components.siren import DOMAIN as SIREN_DOMAIN
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from . import TPLinkConfigEntry
21 from .deprecate import DeprecatedInfo, async_cleanup_deprecated
22 from .entity import CoordinatedTPLinkFeatureEntity, TPLinkFeatureEntityDescription
23 
24 
25 @dataclass(frozen=True, kw_only=True)
27  ButtonEntityDescription, TPLinkFeatureEntityDescription
28 ):
29  """Base class for a TPLink feature based button entity description."""
30 
31 
32 BUTTON_DESCRIPTIONS: Final = [
34  key="test_alarm",
35  deprecated_info=DeprecatedInfo(
36  platform=BUTTON_DOMAIN,
37  new_platform=SIREN_DOMAIN,
38  breaks_in_ha_version="2025.4.0",
39  ),
40  ),
42  key="stop_alarm",
43  deprecated_info=DeprecatedInfo(
44  platform=BUTTON_DOMAIN,
45  new_platform=SIREN_DOMAIN,
46  breaks_in_ha_version="2025.4.0",
47  ),
48  ),
50  key="reboot",
51  device_class=ButtonDeviceClass.RESTART,
52  ),
53 ]
54 
55 BUTTON_DESCRIPTIONS_MAP = {desc.key: desc for desc in BUTTON_DESCRIPTIONS}
56 
57 
59  hass: HomeAssistant,
60  config_entry: TPLinkConfigEntry,
61  async_add_entities: AddEntitiesCallback,
62 ) -> None:
63  """Set up buttons."""
64  data = config_entry.runtime_data
65  parent_coordinator = data.parent_coordinator
66  children_coordinators = data.children_coordinators
67  device = parent_coordinator.device
68 
69  entities = CoordinatedTPLinkFeatureEntity.entities_for_device_and_its_children(
70  hass=hass,
71  device=device,
72  coordinator=parent_coordinator,
73  feature_type=Feature.Type.Action,
74  entity_class=TPLinkButtonEntity,
75  descriptions=BUTTON_DESCRIPTIONS_MAP,
76  child_coordinators=children_coordinators,
77  )
78  async_cleanup_deprecated(hass, BUTTON_DOMAIN, config_entry.entry_id, entities)
79  async_add_entities(entities)
80 
81 
83  """Representation of a TPLink button entity."""
84 
85  entity_description: TPLinkButtonEntityDescription
86 
87  async def async_press(self) -> None:
88  """Execute action."""
89  await self._feature_feature.set_value(True)
90 
91  def _async_update_attrs(self) -> None:
92  """No need to update anything."""