Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for SLZB-06 binary sensors."""
2 
3 from __future__ import annotations
4 
5 from _collections_abc import Callable
6 from dataclasses import dataclass
7 
8 from pysmlight import Sensors
9 from pysmlight.const import Events as SmEvents
10 from pysmlight.sse import MessageEvent
11 
13  BinarySensorDeviceClass,
14  BinarySensorEntity,
15  BinarySensorEntityDescription,
16 )
17 from homeassistant.config_entries import ConfigEntry
18 from homeassistant.const import EntityCategory
19 from homeassistant.core import HomeAssistant, callback
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 
22 from .const import SCAN_INTERNET_INTERVAL
23 from .coordinator import SmDataUpdateCoordinator
24 from .entity import SmEntity
25 
26 SCAN_INTERVAL = SCAN_INTERNET_INTERVAL
27 
28 
29 @dataclass(frozen=True, kw_only=True)
31  """Class describing SMLIGHT binary sensor entities."""
32 
33  value_fn: Callable[[Sensors], bool]
34 
35 
36 SENSORS = [
38  key="ethernet",
39  translation_key="ethernet",
40  value_fn=lambda x: x.ethernet,
41  ),
43  key="vpn",
44  translation_key="vpn",
45  entity_registry_enabled_default=False,
46  value_fn=lambda x: x.vpn_status,
47  ),
49  key="wifi",
50  translation_key="wifi",
51  entity_registry_enabled_default=False,
52  value_fn=lambda x: x.wifi_connected,
53  ),
54 ]
55 
56 
58  hass: HomeAssistant,
59  entry: ConfigEntry,
60  async_add_entities: AddEntitiesCallback,
61 ) -> None:
62  """Set up SMLIGHT sensor based on a config entry."""
63  coordinator = entry.runtime_data.data
64 
66  [
67  *(
68  SmBinarySensorEntity(coordinator, description)
69  for description in SENSORS
70  ),
71  SmInternetSensorEntity(coordinator),
72  ]
73  )
74 
75 
77  """Representation of a slzb binary sensor."""
78 
79  entity_description: SmBinarySensorEntityDescription
80  _attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
81  _attr_entity_category = EntityCategory.DIAGNOSTIC
82 
83  def __init__(
84  self,
85  coordinator: SmDataUpdateCoordinator,
86  description: SmBinarySensorEntityDescription,
87  ) -> None:
88  """Initialize slzb binary sensor."""
89  super().__init__(coordinator)
90 
91  self.entity_descriptionentity_description = description
92  self._attr_unique_id_attr_unique_id = f"{coordinator.unique_id}_{description.key}"
93 
94  @property
95  def is_on(self) -> bool:
96  """Return the state of the sensor."""
97  return self.entity_descriptionentity_description.value_fn(self.coordinator.data.sensors)
98 
99 
101  """Representation of the SLZB internet sensor."""
102 
103  _attr_translation_key = "internet"
104  _attr_device_class = BinarySensorDeviceClass.CONNECTIVITY
105  _attr_entity_category = EntityCategory.DIAGNOSTIC
106 
107  def __init__(
108  self,
109  coordinator: SmDataUpdateCoordinator,
110  ) -> None:
111  """Initialize slzb binary sensor."""
112  super().__init__(coordinator)
113  self._attr_unique_id_attr_unique_id = f"{coordinator.unique_id}_{self._attr_translation_key}"
114 
115  async def async_added_to_hass(self) -> None:
116  """Run when entity about to be added to hass."""
117  await super().async_added_to_hass()
118  self.async_on_removeasync_on_remove(
119  self.coordinator.client.sse.register_callback(
120  SmEvents.EVENT_INET_STATE, self.internet_callbackinternet_callback
121  )
122  )
123  await self.async_updateasync_updateasync_update()
124 
125  @callback
126  def internet_callback(self, event: MessageEvent) -> None:
127  """Update internet state from event."""
128  self._attr_is_on_attr_is_on = event.data == "ok"
129  self.async_write_ha_stateasync_write_ha_state()
130 
131  @property
132  def should_poll(self) -> bool:
133  """Poll entity for internet connected updates."""
134  return True
135 
136  async def async_update(self) -> None:
137  """Update the sensor.
138 
139  This is an async api, device will respond with EVENT_INET_STATE event.
140  """
141  await self.coordinator.client.get_param("inetState")
None __init__(self, SmDataUpdateCoordinator coordinator, SmBinarySensorEntityDescription description)
None __init__(self, SmDataUpdateCoordinator coordinator)
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)