Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Huawei LTE buttons."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from huawei_lte_api.enums.device import ControlModeEnum
8 
10  ButtonDeviceClass,
11  ButtonEntity,
12  ButtonEntityDescription,
13 )
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.const import EntityCategory
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers import entity_platform
18 
19 from .const import DOMAIN
20 from .entity import HuaweiLteBaseEntityWithDevice
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 
26  hass: HomeAssistant,
27  config_entry: ConfigEntry,
28  async_add_entities: entity_platform.AddEntitiesCallback,
29 ) -> None:
30  """Set up Huawei LTE buttons."""
31  router = hass.data[DOMAIN].routers[config_entry.entry_id]
32  buttons = [
34  RestartButton(router),
35  ]
36  async_add_entities(buttons)
37 
38 
40  """Huawei LTE button base class."""
41 
42  @property
43  def _device_unique_id(self) -> str:
44  """Return unique ID for entity within a router."""
45  return f"button-{self.entity_description.key}"
46 
47  async def async_update(self) -> None:
48  """Update is not necessary for button entities."""
49 
50  def press(self) -> None:
51  """Press button."""
52  if self.router.suspended:
53  _LOGGER.debug(
54  "%s: ignored, integration suspended", self.entity_description.key
55  )
56  return
57  result = self._press()
58  _LOGGER.debug("%s: %s", self.entity_description.key, result)
59 
60  def _press(self) -> str:
61  """Invoke low level action of button press."""
62  raise NotImplementedError
63 
64 
65 BUTTON_KEY_CLEAR_TRAFFIC_STATISTICS = "clear_traffic_statistics"
66 
67 
69  """Huawei LTE clear traffic statistics button."""
70 
71  entity_description = ButtonEntityDescription(
72  key=BUTTON_KEY_CLEAR_TRAFFIC_STATISTICS,
73  name="Clear traffic statistics",
74  entity_category=EntityCategory.CONFIG,
75  )
76 
77  def _press(self) -> str:
78  """Call clear traffic statistics endpoint."""
79  return self.routerrouter.client.monitoring.set_clear_traffic()
80 
81 
82 BUTTON_KEY_RESTART = "restart"
83 
84 
86  """Huawei LTE restart button."""
87 
88  entity_description = ButtonEntityDescription(
89  key=BUTTON_KEY_RESTART,
90  name="Restart",
91  device_class=ButtonDeviceClass.RESTART,
92  entity_category=EntityCategory.CONFIG,
93  )
94 
95  def _press(self) -> str:
96  """Call restart endpoint."""
97  return self.routerrouter.client.device.set_control(ControlModeEnum.REBOOT)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, entity_platform.AddEntitiesCallback async_add_entities)
Definition: button.py:29