Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Button platform for Sensibo integration."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 from typing import Any
7 
8 from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
9 from homeassistant.const import EntityCategory
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from . import SensiboConfigEntry
14 from .coordinator import SensiboDataUpdateCoordinator
15 from .entity import SensiboDeviceBaseEntity, async_handle_api_call
16 
17 PARALLEL_UPDATES = 0
18 
19 
20 @dataclass(frozen=True, kw_only=True)
22  """Class describing Sensibo Button entities."""
23 
24  data_key: str
25 
26 
27 DEVICE_BUTTON_TYPES = SensiboButtonEntityDescription(
28  key="reset_filter",
29  translation_key="reset_filter",
30  entity_category=EntityCategory.CONFIG,
31  data_key="filter_clean",
32 )
33 
34 
36  hass: HomeAssistant,
37  entry: SensiboConfigEntry,
38  async_add_entities: AddEntitiesCallback,
39 ) -> None:
40  """Set up Sensibo binary sensor platform."""
41 
42  coordinator = entry.runtime_data
43 
45  SensiboDeviceButton(coordinator, device_id, DEVICE_BUTTON_TYPES)
46  for device_id, device_data in coordinator.data.parsed.items()
47  )
48 
49 
51  """Representation of a Sensibo Device Binary Sensor."""
52 
53  entity_description: SensiboButtonEntityDescription
54 
55  def __init__(
56  self,
57  coordinator: SensiboDataUpdateCoordinator,
58  device_id: str,
59  entity_description: SensiboButtonEntityDescription,
60  ) -> None:
61  """Initiate Sensibo Device Button."""
62  super().__init__(
63  coordinator,
64  device_id,
65  )
66  self.entity_descriptionentity_description = entity_description
67  self._attr_unique_id_attr_unique_id = f"{device_id}-{entity_description.key}"
68 
69  async def async_press(self) -> None:
70  """Press the button."""
71  await self.async_send_api_callasync_send_api_call(
72  key=self.entity_descriptionentity_description.data_key,
73  value=False,
74  )
75 
76  @async_handle_api_call
77  async def async_send_api_call(self, key: str, value: Any) -> bool:
78  """Make service call to api."""
79  result = await self._client_client.async_reset_filter(
80  self._device_id_device_id,
81  )
82  return bool(result.get("status") == "success")
None __init__(self, SensiboDataUpdateCoordinator coordinator, str device_id, SensiboButtonEntityDescription entity_description)
Definition: button.py:60
bool async_send_api_call(self, str key, Any value)
Definition: button.py:77
None async_setup_entry(HomeAssistant hass, SensiboConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: button.py:39