-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
/
Copy pathbinary_sensor.py
226 lines (174 loc) · 7.24 KB
/
binary_sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""Support for Huawei LTE binary sensors."""
from __future__ import annotations
from dataclasses import dataclass, field
import logging
from typing import Any
from huawei_lte_api.enums.cradle import ConnectionStatusEnum
from homeassistant.components.binary_sensor import (
DOMAIN as BINARY_SENSOR_DOMAIN,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import HuaweiLteBaseEntityWithDevice
from .const import (
DOMAIN,
KEY_MONITORING_CHECK_NOTIFICATIONS,
KEY_MONITORING_STATUS,
KEY_WLAN_WIFI_FEATURE_SWITCH,
)
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up from config entry."""
router = hass.data[DOMAIN].routers[config_entry.entry_id]
entities: list[Entity] = []
if router.data.get(KEY_MONITORING_STATUS):
entities.append(HuaweiLteMobileConnectionBinarySensor(router))
entities.append(HuaweiLteWifiStatusBinarySensor(router))
entities.append(HuaweiLteWifi24ghzStatusBinarySensor(router))
entities.append(HuaweiLteWifi5ghzStatusBinarySensor(router))
if router.data.get(KEY_MONITORING_CHECK_NOTIFICATIONS):
entities.append(HuaweiLteSmsStorageFullBinarySensor(router))
async_add_entities(entities, True)
@dataclass
class HuaweiLteBaseBinarySensor(HuaweiLteBaseEntityWithDevice, BinarySensorEntity):
"""Huawei LTE binary sensor device base class."""
_attr_entity_registry_enabled_default = False
key: str = field(init=False)
item: str = field(init=False)
_raw_state: str | None = field(default=None, init=False)
@property
def _device_unique_id(self) -> str:
return f"{self.key}.{self.item}"
async def async_added_to_hass(self) -> None:
"""Subscribe to needed data on add."""
await super().async_added_to_hass()
self.router.subscriptions[self.key].add(f"{BINARY_SENSOR_DOMAIN}/{self.item}")
async def async_will_remove_from_hass(self) -> None:
"""Unsubscribe from needed data on remove."""
await super().async_will_remove_from_hass()
self.router.subscriptions[self.key].remove(
f"{BINARY_SENSOR_DOMAIN}/{self.item}"
)
async def async_update(self) -> None:
"""Update state."""
try:
value = self.router.data[self.key][self.item]
except KeyError:
value = None
_LOGGER.debug("%s[%s] not in data", self.key, self.item)
if value is None:
self._raw_state = value
self._available = False
else:
self._raw_state = str(value)
self._available = True
CONNECTION_STATE_ATTRIBUTES = {
str(ConnectionStatusEnum.CONNECTING): "Connecting",
str(ConnectionStatusEnum.DISCONNECTING): "Disconnecting",
str(ConnectionStatusEnum.CONNECT_FAILED): "Connect failed",
str(ConnectionStatusEnum.CONNECT_STATUS_NULL): "Status not available",
str(ConnectionStatusEnum.CONNECT_STATUS_ERROR): "Status error",
}
@dataclass
class HuaweiLteMobileConnectionBinarySensor(HuaweiLteBaseBinarySensor):
"""Huawei LTE mobile connection binary sensor."""
_attr_name: str = field(default="Mobile connection", init=False)
_attr_entity_registry_enabled_default = True
def __post_init__(self) -> None:
"""Initialize identifiers."""
self.key = KEY_MONITORING_STATUS
self.item = "ConnectionStatus"
@property
def is_on(self) -> bool:
"""Return whether the binary sensor is on."""
return bool(
self._raw_state
and int(self._raw_state)
in (ConnectionStatusEnum.CONNECTED, ConnectionStatusEnum.DISCONNECTING)
)
@property
def assumed_state(self) -> bool:
"""Return True if real state is assumed, not known."""
return not self._raw_state or int(self._raw_state) not in (
ConnectionStatusEnum.CONNECT_FAILED,
ConnectionStatusEnum.CONNECTED,
ConnectionStatusEnum.DISCONNECTED,
)
@property
def icon(self) -> str:
"""Return mobile connectivity sensor icon."""
return "mdi:signal" if self.is_on else "mdi:signal-off"
@property
def extra_state_attributes(self) -> dict[str, Any] | None:
"""Get additional attributes related to connection status."""
attributes = {}
if self._raw_state in CONNECTION_STATE_ATTRIBUTES:
attributes["additional_state"] = CONNECTION_STATE_ATTRIBUTES[
self._raw_state
]
return attributes
class HuaweiLteBaseWifiStatusBinarySensor(HuaweiLteBaseBinarySensor):
"""Huawei LTE WiFi status binary sensor base class."""
@property
def is_on(self) -> bool:
"""Return whether the binary sensor is on."""
return self._raw_state is not None and int(self._raw_state) == 1
@property
def assumed_state(self) -> bool:
"""Return True if real state is assumed, not known."""
return self._raw_state is None
@property
def icon(self) -> str:
"""Return WiFi status sensor icon."""
return "mdi:wifi" if self.is_on else "mdi:wifi-off"
@dataclass
class HuaweiLteWifiStatusBinarySensor(HuaweiLteBaseWifiStatusBinarySensor):
"""Huawei LTE WiFi status binary sensor."""
_attr_name: str = field(default="WiFi status", init=False)
def __post_init__(self) -> None:
"""Initialize identifiers."""
self.key = KEY_MONITORING_STATUS
self.item = "WifiStatus"
@dataclass
class HuaweiLteWifi24ghzStatusBinarySensor(HuaweiLteBaseWifiStatusBinarySensor):
"""Huawei LTE 2.4GHz WiFi status binary sensor."""
_attr_name: str = field(default="2.4GHz WiFi status", init=False)
def __post_init__(self) -> None:
"""Initialize identifiers."""
self.key = KEY_WLAN_WIFI_FEATURE_SWITCH
self.item = "wifi24g_switch_enable"
@dataclass
class HuaweiLteWifi5ghzStatusBinarySensor(HuaweiLteBaseWifiStatusBinarySensor):
"""Huawei LTE 5GHz WiFi status binary sensor."""
_attr_name: str = field(default="5GHz WiFi status", init=False)
def __post_init__(self) -> None:
"""Initialize identifiers."""
self.key = KEY_WLAN_WIFI_FEATURE_SWITCH
self.item = "wifi5g_enabled"
@dataclass
class HuaweiLteSmsStorageFullBinarySensor(HuaweiLteBaseBinarySensor):
"""Huawei LTE SMS storage full binary sensor."""
_attr_name: str = field(default="SMS storage full", init=False)
def __post_init__(self) -> None:
"""Initialize identifiers."""
self.key = KEY_MONITORING_CHECK_NOTIFICATIONS
self.item = "SmsStorageFull"
@property
def is_on(self) -> bool:
"""Return whether the binary sensor is on."""
return self._raw_state is not None and int(self._raw_state) != 0
@property
def assumed_state(self) -> bool:
"""Return True if real state is assumed, not known."""
return self._raw_state is None
@property
def icon(self) -> str:
"""Return WiFi status sensor icon."""
return "mdi:email-alert" if self.is_on else "mdi:email-off"