-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathjuicebox_mqtthandler.py
601 lines (540 loc) · 22.6 KB
/
juicebox_mqtthandler.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
import asyncio
import logging
import time
import ha_mqtt_discoverable.sensors as ha_mqtt
from const import ERROR_LOOKBACK_MIN, VERSION # MAX_ERROR_COUNT,
from ha_mqtt_discoverable import DeviceInfo, Settings
from paho.mqtt.client import Client, MQTTMessage
from juicebox_message import JuiceboxStatusMessage, JuiceboxDebugMessage, JuiceboxEncryptedMessage
_LOGGER = logging.getLogger(__name__)
MQTT_SENDING_ENTITIES = ["text", "number", "switch", "button"]
class JuiceboxMQTTEntity:
def __init__(
self,
name,
**kwargs,
):
# _LOGGER.debug(f"Entity Init: {name}")
self.name = name
self._kwargs = kwargs
self._process_kwargs()
self._state = None
self.attributes = {}
self._mqtt = None
self._loop = asyncio.get_running_loop()
# self.entity_type # Each use of this class to create a child class needs to set this variable in __init__
# self._set_func # Each use of this class to create a child class needs to set this variable in __init__
def add_kwargs(self, **kwargs):
self._kwargs.update(kwargs)
self._process_kwargs()
def _process_kwargs(self):
self._kwargs.update(
{
"name": self.name,
"unique_id": f"{self._kwargs.get('juicebox_id', None)} {self.name}",
}
)
self.experimental = self._kwargs.get("experimental", False)
self._unique_id = f"{self._kwargs.get('juicebox_id', None)} {self.name}"
self._mitm_handler = self._kwargs.get("mitm_handler", None)
self._add_error = self._kwargs.get("add_error_func", None)
@property
def state(self):
return self._state
async def start(self):
entity_info_keys = getattr(
ha_mqtt, f"{self.entity_type.title()}Info"
).__fields__.keys()
entity_info = {}
for key in entity_info_keys:
if self._kwargs.get(key, None) is not None:
entity_info.update({key: self._kwargs.get(key, None)})
self._mqtt = getattr(ha_mqtt, f"{self.entity_type.title()}")(
Settings(
mqtt=self._kwargs.get("mqtt", self._kwargs.get("mqtt_settings", None)),
entity=getattr(ha_mqtt, f"{self.entity_type.title()}Info").parse_obj(
entity_info
),
)
)
if self._kwargs.get("initial_state", None) is not None:
await self.set(self._kwargs.get("initial_state", None))
async def close(self):
if self._mqtt is not None:
self._mqtt.mqtt_client.disconnect()
async def set_state(self, state):
await self.set(state)
async def set(self, state=None):
self._state = state
try:
if self.entity_type == 'number':
# float to be used by any number, JuiceboxMessage will use int
getattr(self._mqtt, self._set_func)(float(state))
elif self.entity_type == 'switch':
# This works with ha-mqtt-discoverable after v0.14.0
# but for ARM we need older version
# getattr(self._mqtt, self._set_func)(state.lower() == 'on')
# workaround calling the on/off methods
if (state.lower() == 'on'):
self._mqtt.on()
else:
self._mqtt.off()
else:
getattr(self._mqtt, self._set_func)(state)
except AttributeError as e:
if self._add_error is not None:
await self._add_error()
_LOGGER.warning(
f"Can't update attributes for {self.name} "
"as MQTT isn't connected/started. "
f"({e.__class__.__qualname__}: {e})"
)
async def set_attributes(self, attr={}):
self.attributes = attr
try:
self._mqtt.set_attributes(attr)
except AttributeError as e:
if self._add_error is not None:
await self._add_error()
_LOGGER.warning(
f"Can't update attributes for {self.name} "
"as MQTT isn't connected/started. "
f"({e.__class__.__qualname__}: {e})"
)
class JuiceboxMQTTSendingEntity(JuiceboxMQTTEntity):
def __init__(
self,
name,
**kwargs,
):
# _LOGGER.debug(f"SendingEntity Init: {name}")
super().__init__(name, **kwargs)
self.command_timestamp = None
async def start(self):
entity_info_keys = getattr(
ha_mqtt, f"{self.entity_type.title()}Info"
).__fields__.keys()
entity_info = {}
for key in entity_info_keys:
if self._kwargs.get(key, None) is not None:
entity_info.update({key: self._kwargs.get(key, None)})
self._mqtt = getattr(ha_mqtt, f"{self.entity_type.title()}")(
Settings(
mqtt=self._kwargs.get("mqtt", self._kwargs.get("mqtt_settings", None)),
entity=getattr(ha_mqtt, f"{self.entity_type.title()}Info").parse_obj(
entity_info
),
),
command_callback=self._callback,
user_data=self._kwargs.get("user_data", None),
)
if self._kwargs.get("initial_state", None) is not None:
await self.set(self._kwargs.get("initial_state", None))
elif self.entity_type == 'number':
# The state will came on juicebox messages
_LOGGER.warning(f"{self.name} has no initial_state")
else:
await self.set(self.name)
def _callback(self, client: Client, user_data, message: MQTTMessage):
self._loop.create_task(self._callback_async(client, user_data, message))
async def _callback_async(self, client: Client, user_data, message: MQTTMessage):
"""
Currently, this just sends the received message to the JuiceBox.
Likely, this callback will either need to be built out to build a working
CMD or the callback will call a method that builds a working CMD.
"""
state = message.payload.decode()
_LOGGER.info(
f"{self.entity_type.title()} Callback ({self.name}): "
f"{state}. User Data: {user_data}"
)
if self._mitm_handler:
if user_data == 'RAW':
_LOGGER.debug(f"Sending to MITM: {state}")
await self._mitm_handler.send_data_to_juicebox(state.encode("utf-8"))
else:
# Internal state must be set before sending message to juicebox
await self.set(state)
self.command_timestamp = time.time()
await self._mitm_handler.send_cmd_message_to_juicebox(new_values=True)
else:
if self._add_error is not None:
await self._add_error()
_LOGGER.warning(
f"Cannot send to MITM. mitm_handler type: {type(self._mitm_handler)}"
)
await self.set(state)
class JuiceboxMQTTSensor(JuiceboxMQTTEntity):
def __init__(
self,
name,
**kwargs,
):
# _LOGGER.debug(f"Sensor Init: {name}")
self.entity_type = "sensor"
self._set_func = "set_state"
super().__init__(name, **kwargs)
class JuiceboxMQTTNumber(JuiceboxMQTTSendingEntity):
def __init__(
self,
name,
**kwargs,
):
# _LOGGER.debug(f"Number Init: {name}")
self.entity_type = "number"
self._set_func = "set_value"
super().__init__(name, **kwargs)
class JuiceboxMQTTSwitch(JuiceboxMQTTSendingEntity):
def __init__(
self,
name,
**kwargs,
):
# _LOGGER.debug(f"Switch Init: {name}")
self.entity_type = "switch"
self._set_func = "update_state"
super().__init__(name, **kwargs)
def is_on(self):
if type(self.state) is str:
return self.state.lower() == 'on'
return self.state
class JuiceboxMQTTText(JuiceboxMQTTSendingEntity):
def __init__(
self,
name,
**kwargs,
):
# _LOGGER.debug(f"Text Init: {name}")
self.entity_type = "text"
self._set_func = "set_text"
super().__init__(name, **kwargs)
async def set_text(self, state):
await self.set(state)
class JuiceboxMQTTHandler:
def __init__(
self,
device_name,
mqtt_settings,
experimental,
config,
juicebox_id=None,
mitm_handler=None,
loglevel=None,
):
if loglevel is not None:
_LOGGER.setLevel(loglevel)
self._mqtt_settings = mqtt_settings
self._device_name = device_name
self._juicebox_id = juicebox_id
self._experimental = experimental
self._config = config
self._mitm_handler = mitm_handler
# Try to use first the MAX_CURRENT as maximum, if not found use the previous run current_rating or default of 48 which is safe and not so big
self._max_current = config.get_device(self._juicebox_id, "MAX_CURRENT", config.get_device(self._juicebox_id, "current_rating", 48))
_LOGGER.info(f"max_current: {self._max_current}")
self._error_count = 0
self._error_timestamp_list = []
self._device = DeviceInfo(
name=self._device_name,
identifiers=(
[self._juicebox_id]
if self._juicebox_id is not None
else [self._device_name]
),
connections=[
(
["JuiceBox ID", self._juicebox_id]
if self._juicebox_id is not None
else []
)
],
manufacturer="EnelX",
model="JuiceBox",
sw_version=VERSION,
via_device="JuicePass Proxy",
)
self._entities = {
"status": JuiceboxMQTTSensor(
name="Status",
icon="mdi:ev-station",
expire_after=7200,
),
"current": JuiceboxMQTTSensor(
name="Current",
state_class="measurement",
device_class="current",
unit_of_measurement="A",
expire_after=7200,
),
# Maximum supported by device
"current_rating": JuiceboxMQTTSensor(
name="Current Rating",
device_class="current",
unit_of_measurement="A",
expire_after=7200,
),
# Offline max
"current_max_offline": JuiceboxMQTTSensor(
name="Max Current(Offline/Device)",
state_class="measurement",
device_class="current",
unit_of_measurement="A",
expire_after=7200,
),
"current_max_offline_set": JuiceboxMQTTNumber(
name="Max Current(Offline/Wanted)",
device_class="current",
unit_of_measurement="A",
min=0,
max=self._max_current,
# no initial state, to use the value that will be received from juicebox or from config
# because of this, the entity will only show later (first time) on homeassistant when value is set
# and can change the homeassistant value
),
# Instant / Charging current
"current_max_online": JuiceboxMQTTSensor(
name="Max Current(Online/Device)",
state_class="measurement",
device_class="current",
unit_of_measurement="A",
expire_after=7200,
),
"current_max_online_set": JuiceboxMQTTNumber(
name="Max Current(Online/Wanted)",
device_class="current",
unit_of_measurement="A",
min=0,
max=self._max_current,
# no initial state, to use the value that will be received from juicebox or from config
# because of this, the entity will only show later (first time) on homeassistant when value is set
# and can change the homeassistant value
),
"frequency": JuiceboxMQTTSensor(
name="Frequency",
state_class="measurement",
device_class="frequency",
unit_of_measurement="Hz",
expire_after=7200,
),
"energy_lifetime": JuiceboxMQTTSensor(
name="Energy (Lifetime)",
state_class="total_increasing",
device_class="energy",
unit_of_measurement="Wh",
expire_after=7200,
),
"energy_session": JuiceboxMQTTSensor(
name="Energy (Session)",
state_class="total_increasing",
device_class="energy",
unit_of_measurement="Wh",
expire_after=7200,
),
"temperature": JuiceboxMQTTSensor(
name="Temperature",
state_class="measurement",
device_class="temperature",
unit_of_measurement="°F",
expire_after=7200,
),
"voltage": JuiceboxMQTTSensor(
name="Voltage",
state_class="measurement",
device_class="voltage",
unit_of_measurement="V",
expire_after=7200,
),
"power": JuiceboxMQTTSensor(
name="Power",
state_class="measurement",
device_class="power",
unit_of_measurement="W",
expire_after=7200,
),
"power_factor": JuiceboxMQTTSensor(
name="Power Factor",
state_class="measurement",
device_class="power_factor",
expire_after=7200,
),
# Make possible to control from HA when juicepassproxy will act as ENEL X server for the juicebox
# Will only work when ignoring ENEL X server
"act_as_server": JuiceboxMQTTSwitch(
name="Act as Server",
enabled_by_default=False,
# As will only work when ignoring ENEL X server, True appear to be good for initial state
initial_state="ON",
),
"debug_message": JuiceboxMQTTSensor(
name="Last Debug Message",
enabled_by_default=False,
icon="mdi:bug",
entity_category="diagnostic",
initial_state=f"INFO: Starting JuicePass Proxy {VERSION}",
expire_after=0, # Keep last message available
),
"data_from_juicebox": JuiceboxMQTTSensor(
name="Data from JuiceBox",
experimental=True,
enabled_by_default=False,
entity_category="diagnostic",
expire_after=0, # Keep last message available
),
"data_from_enelx": JuiceboxMQTTSensor(
name="Data from EnelX",
experimental=True,
enabled_by_default=False,
entity_category="diagnostic",
expire_after=0, # Keep last message available
),
"send_to_juicebox": JuiceboxMQTTText(
name="Send Command to JuiceBox",
user_data="RAW",
experimental=True,
enabled_by_default=False,
),
}
_LOGGER.info("Checking for initial_states on config")
for key in self._entities.keys():
initial_state = self._config.get_device(self._juicebox_id, key + "_initial_state", None)
if initial_state:
_LOGGER.info(f"got initial_state on config : {key} -> {initial_state}")
self._entities[key].add_kwargs(initial_state=initial_state)
for entity in self._entities.values():
entity.add_kwargs(
juicebox_id=self._juicebox_id,
device=self._device,
mqtt_settings=self._mqtt_settings,
add_error_func=self._add_error,
)
if entity.entity_type in MQTT_SENDING_ENTITIES:
entity.add_kwargs(mitm_handler=self._mitm_handler)
def get_entity(self, name):
return self._entities[name]
async def start(self):
_LOGGER.info("Starting JuiceboxMQTTHandler")
# while self._error_count < MAX_ERROR_COUNT:
mqtt_task_list = []
for entity in self._entities.values():
if entity.experimental is False or self._experimental is True:
mqtt_task_list.append(asyncio.create_task(entity.start()))
await asyncio.gather(
*mqtt_task_list,
)
async def close(self):
for entity in self._entities.values():
await entity.close()
async def set_mitm_handler(self, mitm_handler):
self._mitm_handler = mitm_handler
for entity in self._entities.values():
if entity.entity_type in MQTT_SENDING_ENTITIES:
entity.add_kwargs(mitm_handler=mitm_handler)
async def _udp_mitm_oserror_message_parse(self, data):
message = {"type": "udp_mitm_oserror"}
err_data = str(data).split("|")
message["status"] = "unavailable"
message["debug_message"] = (
f"JuiceboxMITM {err_data[1].title()} OSError {err_data[3]} "
f"[{err_data[2]}]: {err_data[4]}"
)
return message
async def _store_if_on_message(self, message, key):
if key in message:
self._config.update_device_value(self._juicebox_id, key, message[key])
await self._config.write_if_changed()
async def _basic_message_publish(self, message):
_LOGGER.debug(f"Publish {message.get('type').title()} Message: {message}")
# try:
attributes = {}
# This values are usefull when JPP starts again to start fast
await self._store_if_on_message(message, "current_rating")
await self._store_if_on_message(message, "current_max_offline")
for k in message:
entity = self._entities.get(k, None)
if entity and (entity.experimental is False or self._experimental is True):
await entity.set_state(message.get(k, None))
attributes[k] = message.get(k, None)
if (
self._experimental
and self._entities.get("data_from_juicebox", None) is not None
):
attributes.pop("data_from_juicebox", None)
attr_sorted = dict(sorted(attributes.items()))
unknown_attr = {}
for key in list(attr_sorted.keys()):
if key.startswith("unknown"):
unknown_attr.update({key: attr_sorted.pop(key, None)})
attr_sorted.update(unknown_attr)
await self._entities.get("data_from_juicebox").set_attributes(attr_sorted)
# except Exception as e:
# _LOGGER.exception(
# f"Failed to publish sensor data to MQTT. ({e.__class__.__qualname__}: {
# e})"
# )
async def remote_mitm_handler(self, data):
try:
_LOGGER.debug(f"From EnelX: {data}")
if (
self._experimental
and self._entities.get("data_from_enelx", None) is not None
):
await self._entities.get("data_from_enelx").set_state(
data.decode("utf-8")
)
return data
except IndexError as e:
await self._add_error()
_LOGGER.warning(
"Index error when handling remote data, probably wrong number of items in list. "
"Nothing to worry about unless this happens a lot. "
f"({e.__class__.__qualname__}: {e})"
)
# except Exception as e:
# _LOGGER.exception(
# f"Exception handling remote data. ({e.__class__.__qualname__}: {e})"
# )
async def local_mitm_handler(self, data, decoded_message):
message = None
try:
_LOGGER.debug(f"From JuiceBox: {data} decoded={decoded_message}")
if "JuiceboxMITM_OSERROR" in str(data):
message = await self._udp_mitm_oserror_message_parse(data)
# Now using the classes as priority over older code
elif isinstance(decoded_message, JuiceboxEncryptedMessage):
_LOGGER.warning(f"Encrypted messages will not be sent to mqtt - {decoded_message}")
elif isinstance(decoded_message, JuiceboxStatusMessage):
message = decoded_message.to_simple_format()
elif isinstance(decoded_message, JuiceboxDebugMessage):
message = decoded_message.to_simple_format()
else:
_LOGGER.error(f"should never arrive here, message is unsupported {data} {decoded_message}")
_LOGGER.debug(f"decode/parsed message = {message}")
if message:
# Something is wrong if device is changed
# as the entities use the juicebox_id as unique_id this should not happen
if "serial" in message:
if message["serial"] != self._juicebox_id:
_LOGGER.error(f"serial {message['serial']} on received message does not match juicebox_id {self._juicebox_id}")
# For now just give the error, but will be better to dont send values on entities and return
await self._basic_message_publish(message)
return data
except IndexError as e:
await self._add_error()
_LOGGER.warning(
"Index error when handling local data, probably wrong number of items in list"
"Nothing to worry about unless this happens a lot. "
f"({e.__class__.__qualname__}: {e})"
)
# except Exception as e:
# _LOGGER.exception(
# f"Exception handling local data. ({e.__class__.__qualname__}: {e})"
# )
async def _add_error(self):
self._error_timestamp_list.append(time.time())
time_cutoff = time.time() - (ERROR_LOOKBACK_MIN * 60)
temp_list = list(
filter(lambda el: el > time_cutoff, self._error_timestamp_list)
)
self._error_timestamp_list = temp_list
self._error_count = len(self._error_timestamp_list)
_LOGGER.debug(f"Errors in last {ERROR_LOOKBACK_MIN} min: {self._error_count}")