Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Huawei LTE binary sensors."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from huawei_lte_api.enums.cradle import ConnectionStatusEnum
9 
11  DOMAIN as BINARY_SENSOR_DOMAIN,
12  BinarySensorEntity,
13 )
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity import Entity
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 from .const import (
20  DOMAIN,
21  KEY_MONITORING_CHECK_NOTIFICATIONS,
22  KEY_MONITORING_STATUS,
23  KEY_WLAN_WIFI_FEATURE_SWITCH,
24 )
25 from .entity import HuaweiLteBaseEntityWithDevice
26 
27 _LOGGER = logging.getLogger(__name__)
28 
29 
31  hass: HomeAssistant,
32  config_entry: ConfigEntry,
33  async_add_entities: AddEntitiesCallback,
34 ) -> None:
35  """Set up from config entry."""
36  router = hass.data[DOMAIN].routers[config_entry.entry_id]
37  entities: list[Entity] = []
38 
39  if router.data.get(KEY_MONITORING_STATUS):
40  entities.append(HuaweiLteMobileConnectionBinarySensor(router))
41  entities.append(HuaweiLteWifiStatusBinarySensor(router))
42  entities.append(HuaweiLteWifi24ghzStatusBinarySensor(router))
43  entities.append(HuaweiLteWifi5ghzStatusBinarySensor(router))
44 
45  if router.data.get(KEY_MONITORING_CHECK_NOTIFICATIONS):
46  entities.append(HuaweiLteSmsStorageFullBinarySensor(router))
47 
48  async_add_entities(entities, True)
49 
50 
52  """Huawei LTE binary sensor device base class."""
53 
54  _attr_entity_registry_enabled_default = False
55 
56  key: str
57  item: str
58  _raw_state: str | None = None
59 
60  @property
61  def _device_unique_id(self) -> str:
62  return f"{self.key}.{self.item}"
63 
64  async def async_added_to_hass(self) -> None:
65  """Subscribe to needed data on add."""
66  await super().async_added_to_hass()
67  self.routerrouter.subscriptions[self.key].append(
68  f"{BINARY_SENSOR_DOMAIN}/{self.item}"
69  )
70 
71  async def async_will_remove_from_hass(self) -> None:
72  """Unsubscribe from needed data on remove."""
73  await super().async_will_remove_from_hass()
74  self.routerrouter.subscriptions[self.key].remove(
75  f"{BINARY_SENSOR_DOMAIN}/{self.item}"
76  )
77 
78  async def async_update(self) -> None:
79  """Update state."""
80  try:
81  value = self.routerrouter.data[self.key][self.item]
82  except KeyError:
83  value = None
84  _LOGGER.debug("%s[%s] not in data", self.key, self.item)
85  if value is None:
86  self._raw_state_raw_state = value
87  self._available_available_available = False
88  else:
89  self._raw_state_raw_state = str(value)
90  self._available_available_available = True
91 
92 
93 CONNECTION_STATE_ATTRIBUTES = {
94  str(ConnectionStatusEnum.CONNECTING): "Connecting",
95  str(ConnectionStatusEnum.DISCONNECTING): "Disconnecting",
96  str(ConnectionStatusEnum.CONNECT_FAILED): "Connect failed",
97  str(ConnectionStatusEnum.CONNECT_STATUS_NULL): "Status not available",
98  str(ConnectionStatusEnum.CONNECT_STATUS_ERROR): "Status error",
99 }
100 
101 
103  """Huawei LTE mobile connection binary sensor."""
104 
105  _attr_translation_key = "mobile_connection"
106  _attr_entity_registry_enabled_default = True
107 
108  key = KEY_MONITORING_STATUS
109  item = "ConnectionStatus"
110 
111  @property
112  def is_on(self) -> bool:
113  """Return whether the binary sensor is on."""
114  return bool(
115  self._raw_state_raw_state
116  and int(self._raw_state_raw_state)
117  in (ConnectionStatusEnum.CONNECTED, ConnectionStatusEnum.DISCONNECTING)
118  )
119 
120  @property
121  def assumed_state(self) -> bool:
122  """Return True if real state is assumed, not known."""
123  return not self._raw_state_raw_state or int(self._raw_state_raw_state) not in (
124  ConnectionStatusEnum.CONNECT_FAILED,
125  ConnectionStatusEnum.CONNECTED,
126  ConnectionStatusEnum.DISCONNECTED,
127  )
128 
129  @property
130  def extra_state_attributes(self) -> dict[str, Any] | None:
131  """Get additional attributes related to connection status."""
132  attributes = {}
133  if self._raw_state_raw_state in CONNECTION_STATE_ATTRIBUTES:
134  attributes["additional_state"] = CONNECTION_STATE_ATTRIBUTES[
135  self._raw_state_raw_state
136  ]
137  return attributes
138 
139 
141  """Huawei LTE WiFi status binary sensor base class."""
142 
143  @property
144  def is_on(self) -> bool:
145  """Return whether the binary sensor is on."""
146  return self._raw_state_raw_state is not None and int(self._raw_state_raw_state) == 1
147 
148  @property
149  def assumed_state(self) -> bool:
150  """Return True if real state is assumed, not known."""
151  return self._raw_state_raw_state is None
152 
153 
155  """Huawei LTE WiFi status binary sensor."""
156 
157  _attr_translation_key: str = "wifi_status"
158 
159  key = KEY_MONITORING_STATUS
160  item = "WifiStatus"
161 
162 
164  """Huawei LTE 2.4GHz WiFi status binary sensor."""
165 
166  _attr_translation_key: str = "24ghz_wifi_status"
167 
168  key = KEY_WLAN_WIFI_FEATURE_SWITCH
169  item = "wifi24g_switch_enable"
170 
171 
173  """Huawei LTE 5GHz WiFi status binary sensor."""
174 
175  _attr_translation_key: str = "5ghz_wifi_status"
176 
177  key = KEY_WLAN_WIFI_FEATURE_SWITCH
178  item = "wifi5g_enabled"
179 
180 
182  """Huawei LTE SMS storage full binary sensor."""
183 
184  _attr_translation_key: str = "sms_storage_full"
185 
186  key = KEY_MONITORING_CHECK_NOTIFICATIONS
187  item = "SmsStorageFull"
188 
189  @property
190  def is_on(self) -> bool:
191  """Return whether the binary sensor is on."""
192  return self._raw_state_raw_state is not None and int(self._raw_state_raw_state) != 0
193 
194  @property
195  def assumed_state(self) -> bool:
196  """Return True if real state is assumed, not known."""
197  return self._raw_state_raw_state is None
bool remove(self, _T matcher)
Definition: match.py:214
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)