forked from elad-bar/ha-hpprinter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathha_coordinator.py
359 lines (279 loc) · 11.2 KB
/
ha_coordinator.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import logging
import sys
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import Event, callback
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect,
async_dispatcher_send,
)
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.util import slugify
from ..common.consts import (
DOMAIN,
MODEL_PROPERTY,
PRINTER_MAIN_DEVICE,
SIGNAL_HA_DEVICE_CREATED,
SIGNAL_HA_DEVICE_DISCOVERED,
)
from .ha_config_manager import HAConfigManager
from .rest_api import RestAPIv2
_LOGGER = logging.getLogger(__name__)
class HACoordinator(DataUpdateCoordinator):
"""My custom coordinator."""
def __init__(
self,
hass,
config_manager: HAConfigManager,
):
"""Initialize my coordinator."""
super().__init__(
hass,
_LOGGER,
name=config_manager.entry_title,
update_interval=config_manager.minimum_update_interval,
update_method=self._async_update_data,
)
self._api = RestAPIv2(hass, config_manager)
self._config_manager = config_manager
self._devices: dict[str, DeviceInfo] = {}
self._main_device_data: dict | None = None
self._main_device_id: str | None = None
self._device_handlers = {
PRINTER_MAIN_DEVICE: self.create_main_device,
"Consumable": self.create_consumable_device,
"Adapter": self.create_adapter_device,
}
self._load_signal_handlers()
@property
def api(self) -> RestAPIv2:
return self._api
@property
def config_manager(self) -> HAConfigManager:
return self._config_manager
@property
def entry_id(self) -> str:
return self._config_manager.entry_id
@property
def entry_title(self) -> str:
return self._config_manager.entry_title
async def on_home_assistant_start(self, _event_data: Event):
await self.initialize()
async def on_home_assistant_stop(self, _event_data: Event):
await self._api.terminate()
async def initialize(self):
_LOGGER.debug("Initializing coordinator")
entry = self.config_manager.entry
platforms = self.config_manager.platforms
await self.hass.config_entries.async_forward_entry_setups(entry, platforms)
_LOGGER.info(f"Start loading {DOMAIN} integration, Entry ID: {entry.entry_id}")
await self._api.initialize()
if entry.state == ConfigEntryState.LOADED:
await self.async_refresh()
else:
await self.async_config_entry_first_refresh()
def _load_signal_handlers(self):
loop = self.hass.loop
@callback
def on_device_discovered(
entry_id: str, device_key: str, device_data: dict, device_config: dict
):
loop.create_task(
self._on_device_discovered(
entry_id, device_key, device_data, device_config
)
).__await__()
self.config_entry.async_on_unload(
async_dispatcher_connect(
self.hass, SIGNAL_HA_DEVICE_DISCOVERED, self._on_device_discovered
)
)
def create_main_device(
self, device_key: str, device_data: dict, device_config: dict
):
try:
self._main_device_data = device_data
self._main_device_id = device_key
model = device_data.get(MODEL_PROPERTY)
serial_number = device_data.get("serial_number")
manufacturer = device_data.get("manufacturer_name")
device_identifier = (DOMAIN, self._main_device_id)
device_info = DeviceInfo(
identifiers={device_identifier},
name=self.entry_title,
model=model,
serial_number=serial_number,
manufacturer=manufacturer,
)
self._devices[device_key] = device_info
except Exception as ex:
exc_type, exc_obj, tb = sys.exc_info()
line_number = tb.tb_lineno
_LOGGER.error(
f"Failed to create main device, "
f"Device Key: {device_key}, "
f"Data: {device_data}, "
f"Config: {device_config}, "
f"Error: {ex}, "
f"Line: {line_number}"
)
def create_sub_unit_device(
self, device_key: str, device_data: dict, device_config: dict
):
try:
model = self._main_device_data.get(MODEL_PROPERTY)
serial_number = self._main_device_data.get("serial_number")
manufacturer = self._main_device_data.get("manufacturer_name")
device_type = device_config.get("device_type")
device_unique_id = slugify(f"{self.entry_id}.{device_key}")
sub_unit_device_name = f"{self.entry_title} {device_type}"
device_identifier = (DOMAIN, device_unique_id)
device_info = DeviceInfo(
identifiers={device_identifier},
name=sub_unit_device_name,
model=model,
serial_number=serial_number,
manufacturer=manufacturer,
via_device=(DOMAIN, self._main_device_id),
)
self._devices[device_key] = device_info
except Exception as ex:
exc_type, exc_obj, tb = sys.exc_info()
line_number = tb.tb_lineno
_LOGGER.error(
f"Failed to sub unit device, "
f"Device Key: {device_key}, "
f"Data: {device_data}, "
f"Config: {device_config}, "
f"Error: {ex}, "
f"Line: {line_number}"
)
def create_consumable_device(
self, device_key: str, device_data: dict, device_config: dict
):
try:
printer_device_unique_id = slugify(f"{self.entry_id}.printer")
device_name_parts = [self.entry_title]
cartridge_type: str = device_data.get("consumable_type_enum")
cartridge_color = device_data.get("marker_color")
manufacturer = device_data.get("consumable_life_state_brand")
serial_number = device_data.get("serial_number")
model = device_data.get("consumable_selectibility_number")
if cartridge_type == "printhead":
model = cartridge_type.capitalize()
else:
device_name_parts.append(cartridge_color)
if cartridge_type is not None:
device_name_parts.append(cartridge_type.capitalize())
device_name_parts = [
device_name_part
for device_name_part in device_name_parts
if device_name_part is not None
]
device_unique_id = slugify(f"{self.entry_id}.{device_key}")
cartridge_device_name = " ".join(device_name_parts)
device_identifier = (DOMAIN, device_unique_id)
device_info = DeviceInfo(
identifiers={device_identifier},
name=cartridge_device_name,
model=model,
serial_number=serial_number,
manufacturer=manufacturer,
via_device=(DOMAIN, printer_device_unique_id),
)
self._devices[device_key] = device_info
except Exception as ex:
exc_type, exc_obj, tb = sys.exc_info()
line_number = tb.tb_lineno
_LOGGER.error(
f"Failed to consumable device, "
f"Device Key: {device_key}, "
f"Data: {device_data}, "
f"Config: {device_config}, "
f"Error: {ex}, "
f"Line: {line_number}"
)
def create_adapter_device(
self, device_key: str, device_data: dict, device_config: dict
):
try:
serial_number = self._main_device_data.get("serial_number")
manufacturer = self._main_device_data.get("manufacturer_name")
adapter_name = device_data.get("hardware_config_name").upper()
port_type_key = "hardware_config_device_connectivity_port_type"
model = device_data.get(port_type_key).replace("Embedded", "").upper()
device_type = device_config.get("device_type")
device_unique_id = slugify(f"{self.entry_id}.{device_key}")
adapter_device_name = f"{self.entry_title} {device_type} {adapter_name}"
device_identifier = (DOMAIN, device_unique_id)
device_info = DeviceInfo(
identifiers={device_identifier},
name=adapter_device_name,
model=model,
serial_number=serial_number,
manufacturer=manufacturer,
via_device=(DOMAIN, self._main_device_id),
)
self._devices[device_key] = device_info
except Exception as ex:
exc_type, exc_obj, tb = sys.exc_info()
line_number = tb.tb_lineno
_LOGGER.error(
f"Failed to adapter device, "
f"Device Key: {device_key}, "
f"Data: {device_data}, "
f"Config: {device_config}, "
f"Error: {ex}, "
f"Line: {line_number}"
)
def get_device(self, device_key: str) -> DeviceInfo | None:
result = self._devices.get(device_key)
return result
def get_device_data(self, device_key: str):
data = self._api.data.get(device_key, {})
return data
def get_device_value(self, device_key: str, key: str | None):
data = self.get_device_data(device_key)
if key and data is not None:
return data.get(key)
return data
def get_debug_data(self) -> dict:
data = {
"rawData": self._api.raw_data,
"devicesData": self._api.data,
"devicesConfig": self._api.data_config,
}
return data
def get_devices(self) -> dict[str, DeviceInfo]:
return self._devices
async def _async_update_data(self):
try:
await self._api.update()
return self._api.data
except Exception as err:
raise UpdateFailed(f"Error communicating with API: {err}")
async def _on_device_discovered(
self, entry_id: str, device_key: str, device_data: dict, device_config: dict
):
if entry_id != self.config_entry.entry_id:
return
handlers = [
device_prefix
for device_prefix in self._device_handlers
if device_key.startswith(device_prefix)
]
if handlers:
handler_key = handlers[0]
handler = self._device_handlers[handler_key]
handler(device_key, device_data, device_config)
else:
self.create_sub_unit_device(device_key, device_data, device_config)
async_dispatcher_send(
self.hass,
SIGNAL_HA_DEVICE_CREATED,
self.entry_id,
device_key,
device_data,
device_config,
)
self.hass.create_task(self.async_request_refresh())