-
Notifications
You must be signed in to change notification settings - Fork 332
/
helpers.py
248 lines (191 loc) · 7.46 KB
/
helpers.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import logging
from dataclasses import dataclass, field
from typing import *
from homeassistant.config import DATA_CUSTOMIZE
from homeassistant.core import callback
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.helpers.entity import Entity
from . import bluetooth, zigbee
from .utils import DOMAIN, attributes_template
_LOGGER = logging.getLogger(__name__)
# TODO: rewrite all usage to dataclass
@dataclass
class XiaomiDevice:
did: str # unique Xiaomi did
model: str # Xiaomi model
mac: str
type: str # gateway, zigbee, ble, mesh
online: bool
lumi_spec: list
miot_spec: list
device_info: Dict[str, Any]
stats: 'XiaomiEntity'
extra: Dict[str, Any] = field(default_factory=dict)
# all device entities except stats
entities: Dict[str, 'XiaomiEntity'] = field(default_factory=dict)
gateways: List['Gateway3'] = field(default_factory=list)
class DevicesRegistry:
"""Global registry for all gateway devices. Because BLE devices updates
from all gateway simultaniosly.
Key - device did, `numb` for wifi and mesh devices, `lumi.ieee` for zigbee
devices, `blt.3.alphanum` for ble devices, `group.numb` for mesh groups.
"""
devices: Dict[str, dict] = {}
setups: Dict[str, Callable] = None
defaults: Dict[str, dict] = {}
def add_setup(self, domain: str, handler):
"""Add hass device setup funcion."""
self.setups[domain] = handler
def add_entity(self, domain: str, device: dict, attr: str):
if self not in device['gateways']:
device['gateways'].append(self)
if domain is None or attr in device['entities']:
return
# instant add entity to prevent double setup
device['entities'][attr] = None
self.setups[domain](self, device, attr)
def set_entity(self, entity: 'XiaomiEntity'):
entity.device['entities'][entity.attr] = entity
def remove_entity(self, entity: 'XiaomiEntity'):
entity.device['entities'].pop(entity.attr)
def add_stats(self, device: dict):
if 'stats' in device:
return
device['stats'] = None
self.setups['sensor'](self, device, device['type'])
def set_stats(self, entity: 'XiaomiEntity'):
entity.device['stats'] = entity
def remove_stats(self, entity: 'XiaomiEntity'):
entity.device.pop('stats')
def find_or_create_device(self, device: dict) -> dict:
type_ = device['type']
did = device['did'] if type_ != 'ble' else device['mac'].lower()
if did in self.devices:
return self.devices[did]
self.devices[did] = device
# update device with specs
if type_ in ('gateway', 'zigbee'):
device.update(zigbee.get_device(device['model']))
elif type_ == 'mesh':
device.update(bluetooth.get_device(device['model'], 'Mesh'))
elif type_ == 'ble':
device.update(bluetooth.get_device(device['model'], 'BLE'))
model = device['model']
if model in self.defaults:
device.update(self.defaults[model])
if did in self.defaults:
device.update(self.defaults[did])
mac = device['mac'].lower()
if did != mac and mac in self.defaults:
device.update(self.defaults[mac])
device['entities'] = {}
device['gateways'] = []
return device
class XiaomiEntity(Entity):
_ignore_offline = None
_state = None
def __init__(self, gateway: 'Gateway3', device: dict, attr: str):
self.gw = gateway
self.device = device
self.attr = attr
self._attrs = {}
self._unique_id = f"{device.get('entity_name', device['mac'])}_{attr}"
self._name = (device['device_name'] + ' ' +
attr.replace('_', ' ').title())
self.entity_id = f"{DOMAIN}.{self._unique_id}"
def debug(self, message: str):
self.gw.debug(f"{self.entity_id} | {message}")
def parent(self, did: str = None):
if did is None:
did = self.device['init'].get('parent')
if did == '':
return '-'
try:
return self.gw.devices[did]['nwk']
except:
return None
async def async_added_to_hass(self):
"""Also run when rename entity_id"""
custom: dict = self.hass.data[DATA_CUSTOMIZE].get(self.entity_id)
self._ignore_offline = custom.get('ignore_offline')
if 'init' in self.device and self._state is None:
await self.async_update(self.device['init'])
self.render_attributes_template()
self.gw.set_entity(self)
async def async_will_remove_from_hass(self) -> None:
"""Also run when rename entity_id"""
self.gw.remove_entity(self)
# @property
# def entity_registry_enabled_default(self):
# return False
@property
def should_poll(self) -> bool:
return False
@property
def unique_id(self):
return self._unique_id
@property
def name(self):
return self._name
@property
def available(self) -> bool:
gw_available = any(
gateway.available for gateway in self.device['gateways']
)
return gw_available and (self.device.get('online', True) or
self._ignore_offline)
@property
def device_state_attributes(self):
return self._attrs
@property
def device_info(self):
"""
https://developers.home-assistant.io/docs/device_registry_index/
"""
type_ = self.device['type']
if type_ == 'gateway':
return {
'connections': {
(CONNECTION_NETWORK_MAC, self.device['wlan_mac'])
},
'identifiers': {(DOMAIN, self.device['mac'])},
'manufacturer': self.device['device_manufacturer'],
'model': self.device['device_model'],
'name': self.device['device_name'],
'sw_version': self.device['fw_ver'],
}
elif type_ == 'zigbee':
return {
'connections': {(type_, self.device['mac'])},
'identifiers': {(DOMAIN, self.device['mac'])},
'manufacturer': self.device.get('device_manufacturer'),
'model': self.device['device_model'],
'name': self.device['device_name'],
'sw_version': self.device['fw_ver'],
'via_device': (DOMAIN, self.gw.device['mac'])
}
else: # ble and mesh
return {
'connections': {('bluetooth', self.device['mac'])},
'identifiers': {(DOMAIN, self.device['mac'])},
'manufacturer': self.device.get('device_manufacturer'),
'model': self.device['device_model'],
'name': self.device['device_name'],
'via_device': (DOMAIN, self.gw.device['mac'])
}
async def async_update(self, data: dict):
raise NotImplementedError
@callback
def render_attributes_template(self):
try:
attrs = attributes_template(self.hass).async_render({
'attr': self.attr,
'device': self.device,
'gateway': self.gw.device
})
if isinstance(attrs, dict):
self._attrs.update(attrs)
except AttributeError:
pass
except:
_LOGGER.exception("Can't render attributes")