-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsed.py
More file actions
638 lines (556 loc) · 23.9 KB
/
Copy pathsed.py
File metadata and controls
638 lines (556 loc) · 23.9 KB
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
"""Plugwise SED (Sleeping Endpoint Device) base object."""
from __future__ import annotations
from asyncio import Task, create_task, gather, sleep
from collections.abc import Awaitable, Callable, Coroutine
from dataclasses import replace
from datetime import datetime, timedelta
import logging
from typing import Any, Final
from ..api import BatteryConfig, NodeEvent, NodeFeature, NodeType
from ..connection import StickController
from ..constants import MAX_UINT_2, MAX_UINT_4
from ..exceptions import MessageError, NodeError
from ..messages.requests import NodeSleepConfigRequest
from ..messages.responses import (
NODE_AWAKE_RESPONSE_ID,
NodeAwakeResponse,
NodeAwakeResponseType,
NodeResponseType,
PlugwiseResponse,
)
from .node import PlugwiseBaseNode
CACHE_SED_AWAKE_DURATION = "awake_duration"
CACHE_SED_CLOCK_INTERVAL = "clock_interval"
CACHE_SED_SLEEP_DURATION = "sleep_duration"
CACHE_SED_DIRTY = "sed_dirty"
CACHE_SED_CLOCK_SYNC = "clock_sync"
CACHE_SED_MAINTENANCE_INTERVAL = "maintenance_interval"
CACHE_SED_AWAKE_TIMESTAMP = "awake_timestamp"
CACHE_SED_AWAKE_REASON = "awake_reason"
# Number of seconds to ignore duplicate awake messages
AWAKE_RETRY: Final = 5
# Defaults for 'Sleeping End Devices'
# Time in seconds the SED keep itself awake to receive
# and respond to other messages
SED_DEFAULT_AWAKE_DURATION: Final = 10
# 7 days, duration in minutes the node synchronize its clock
SED_DEFAULT_CLOCK_INTERVAL: Final = 25200
# Enable or disable synchronizing clock
SED_DEFAULT_CLOCK_SYNC: Final = False
# Interval in minutes the SED will awake for maintenance purposes
# Source [5min - 24h]
SED_DEFAULT_MAINTENANCE_INTERVAL: Final = 60 # Assume standard interval of 1 hour
SED_MAX_MAINTENANCE_INTERVAL_OFFSET: Final = 30 # seconds
# Time in minutes the SED will sleep
SED_DEFAULT_SLEEP_DURATION: Final = 60
# Default firmware if not known
DEFAULT_FIRMWARE: Final = None
# SED BaseNode Features
SED_FEATURES: Final = (NodeFeature.BATTERY,)
# Value limits
MAX_MINUTE_INTERVAL: Final = 1440
_LOGGER = logging.getLogger(__name__)
class NodeSED(PlugwiseBaseNode):
"""provides base class for SED based nodes like Scan, Sense & Switch."""
# Maintenance
_awake_timer_task: Task[None] | None = None
_ping_at_awake: bool = False
_awake_subscription: Callable[[], None] | None = None
def __init__(
self,
mac: str,
node_type: NodeType,
controller: StickController,
loaded_callback: Callable[[NodeEvent, str], Awaitable[None]],
):
"""Initialize base class for Sleeping End Device."""
super().__init__(mac, node_type, controller, loaded_callback)
self._node_info.is_battery_powered = True
# Configure SED
self._battery_config = BatteryConfig()
self._sed_node_info_update_task_scheduled = False
self._delayed_task: Task[None] | None = None
self._last_awake: dict[NodeAwakeResponseType, datetime] = {}
self._last_awake_reason: str = "Unknown"
# Maintenance
self._maintenance_last_awake: datetime | None = None
self._maintenance_interval_restored_from_cache = False
async def load(self) -> bool:
"""Load and activate SED node features."""
if self._loaded:
return True
_LOGGER.debug("Load SED node %s from cache", self._node_info.mac)
if await self._load_from_cache():
self._loaded = True
if not self._loaded:
_LOGGER.debug("Load SED node %s defaults", self._node_info.mac)
await self._load_defaults()
self._loaded = True
self._features += SED_FEATURES
return self._loaded
async def unload(self) -> None:
"""Deactivate and unload node features."""
if self._awake_timer_task is not None and not self._awake_timer_task.done():
self._awake_timer_task.cancel()
if self._awake_subscription is not None:
self._awake_subscription()
if self._delayed_task is not None and not self._delayed_task.done():
await self._delayed_task
await super().unload()
async def initialize(self) -> None:
"""Initialize SED node."""
if self._initialized:
return
self._awake_subscription = await self._message_subscribe(
self._awake_response,
self._mac_in_bytes,
(NODE_AWAKE_RESPONSE_ID,),
)
await super().initialize()
async def _load_defaults(self) -> None:
"""Load default configuration settings."""
async def _load_from_cache(self) -> bool:
"""Load states from previous cached information. Returns True if successful."""
super_load_success = True
if not await super()._load_from_cache():
super_load_success = False
dirty = False
if (awake_duration := self._awake_duration_from_cache()) is None:
dirty = True
awake_duration = SED_DEFAULT_AWAKE_DURATION
if (clock_interval := self._clock_interval_from_cache()) is None:
dirty = True
clock_interval = SED_DEFAULT_CLOCK_INTERVAL
if (clock_sync := self._clock_sync_from_cache()) is None:
dirty = True
clock_sync = SED_DEFAULT_CLOCK_SYNC
if (maintenance_interval := self._maintenance_interval_from_cache()) is None:
dirty = True
maintenance_interval = SED_DEFAULT_MAINTENANCE_INTERVAL
if (sleep_duration := self._sleep_duration_from_cache()) is None:
dirty = True
sleep_duration = SED_DEFAULT_SLEEP_DURATION
dirty |= self._sed_config_dirty_from_cache()
self._battery_config = BatteryConfig(
awake_duration=awake_duration,
clock_interval=clock_interval,
clock_sync=clock_sync,
maintenance_interval=maintenance_interval,
sleep_duration=sleep_duration,
dirty=dirty,
)
if dirty:
await self._sed_configure_update()
self._awake_timestamp_from_cache()
self._awake_reason_from_cache()
return super_load_success
def _awake_duration_from_cache(self) -> int | None:
"""Load awake duration from cache."""
if (awake_duration := self._get_cache(CACHE_SED_AWAKE_DURATION)) is not None:
return int(awake_duration)
return None
def _clock_interval_from_cache(self) -> int | None:
"""Load clock interval from cache."""
if (clock_interval := self._get_cache(CACHE_SED_CLOCK_INTERVAL)) is not None:
return int(clock_interval)
return None
def _clock_sync_from_cache(self) -> bool | None:
"""Load clock sync state from cache."""
return self._get_cache_as_bool(CACHE_SED_CLOCK_SYNC)
def _maintenance_interval_from_cache(self) -> int | None:
"""Load maintenance interval from cache."""
if (
maintenance_interval := self._get_cache(CACHE_SED_MAINTENANCE_INTERVAL)
) is not None:
self._maintenance_interval_restored_from_cache = True
return int(maintenance_interval)
return None
def _sleep_duration_from_cache(self) -> int | None:
"""Load sleep duration from cache."""
if (sleep_duration := self._get_cache(CACHE_SED_SLEEP_DURATION)) is not None:
return int(sleep_duration)
return None
def _awake_timestamp_from_cache(self) -> datetime | None:
"""Load last awake timestamp from cache."""
return self._get_cache_as_datetime(CACHE_SED_AWAKE_TIMESTAMP)
def _awake_reason_from_cache(self) -> str | None:
"""Load last awake state from cache."""
return self._get_cache(CACHE_SED_AWAKE_REASON)
def _sed_config_dirty_from_cache(self) -> bool:
"""Load battery config dirty from cache."""
if (dirty := self._get_cache_as_bool(CACHE_SED_DIRTY)) is not None:
return dirty
return True
# region Configuration actions
async def set_awake_duration(self, seconds: int) -> bool:
"""Change the awake duration."""
_LOGGER.debug(
"set_awake_duration | Device %s | %s -> %s",
self.name,
self._battery_config.awake_duration,
seconds,
)
if seconds < 1 or seconds > MAX_UINT_2:
raise ValueError(
f"Invalid awake duration ({seconds}). It must be between 1 and 255 seconds."
)
if self._battery_config.awake_duration == seconds:
return False
self._battery_config = replace(
self._battery_config,
awake_duration=seconds,
dirty=True,
)
await self._sed_configure_update()
return True
async def set_clock_interval(self, minutes: int) -> bool:
"""Change the clock interval."""
_LOGGER.debug(
"set_clock_interval | Device %s | %s -> %s",
self.name,
self._battery_config.clock_interval,
minutes,
)
if minutes < 1 or minutes > MAX_UINT_4:
raise ValueError(
f"Invalid clock interval ({minutes}). It must be between 1 and 65535 minutes."
)
if self.battery_config.clock_interval == minutes:
return False
self._battery_config = replace(
self._battery_config, clock_interval=minutes, dirty=True
)
await self._sed_configure_update()
return True
async def set_clock_sync(self, sync: bool) -> bool:
"""Change the clock synchronization setting."""
_LOGGER.debug(
"set_clock_sync | Device %s | %s -> %s",
self.name,
self._battery_config.clock_sync,
sync,
)
if self._battery_config.clock_sync == sync:
return False
self._battery_config = replace(
self._battery_config, clock_sync=sync, dirty=True
)
await self._sed_configure_update()
return True
async def set_maintenance_interval(self, minutes: int) -> bool:
"""Change the maintenance interval."""
_LOGGER.debug(
"set_maintenance_interval | Device %s | %s -> %s",
self.name,
self._battery_config.maintenance_interval,
minutes,
)
if minutes < 1 or minutes > MAX_MINUTE_INTERVAL:
raise ValueError(
f"Invalid maintenance interval ({minutes}). It must be between 1 and 1440 minutes."
)
if self.battery_config.maintenance_interval == minutes:
return False
self._battery_config = replace(
self._battery_config, maintenance_interval=minutes, dirty=True
)
await self._sed_configure_update()
return True
async def set_sleep_duration(self, minutes: int) -> bool:
"""Reconfigure the sleep duration in minutes for a Sleeping Endpoint Device.
Configuration will be applied next time when node is awake for maintenance.
"""
_LOGGER.debug(
"set_sleep_duration | Device %s | %s -> %s",
self.name,
self._battery_config.sleep_duration,
minutes,
)
if minutes < 1 or minutes > MAX_UINT_4:
raise ValueError(
f"Invalid sleep duration ({minutes}). It must be between 1 and 65535 minutes."
)
if self._battery_config.sleep_duration == minutes:
return False
self._battery_config = replace(
self._battery_config, sleep_duration=minutes, dirty=True
)
await self._sed_configure_update()
return True
# endregion
# region Properties
@property
def dirty(self) -> bool:
"""Battery configuration dirty flag."""
return self._battery_config.dirty
@property
def awake_duration(self) -> int:
"""Duration in seconds a battery powered devices is awake."""
if self._battery_config.awake_duration is not None:
return self._battery_config.awake_duration
return SED_DEFAULT_AWAKE_DURATION
@property
def battery_config(self) -> BatteryConfig:
"""Battery related configuration settings."""
return BatteryConfig(
awake_duration=self.awake_duration,
clock_interval=self.clock_interval,
clock_sync=self.clock_sync,
maintenance_interval=self.maintenance_interval,
sleep_duration=self.sleep_duration,
dirty=self.dirty,
)
@property
def clock_interval(self) -> int:
"""Return the clock interval value."""
if self._battery_config.clock_interval is not None:
return self._battery_config.clock_interval
return SED_DEFAULT_CLOCK_INTERVAL
@property
def clock_sync(self) -> bool:
"""Indicate if the internal clock must be synced."""
if self._battery_config.clock_sync is not None:
return self._battery_config.clock_sync
return SED_DEFAULT_CLOCK_SYNC
@property
def maintenance_interval(self) -> int:
"""Return the maintenance interval value.
When value is scheduled to be changed the return value is the optimistic value.
"""
if self._battery_config.maintenance_interval is not None:
return self._battery_config.maintenance_interval
return SED_DEFAULT_MAINTENANCE_INTERVAL
@property
def sleep_duration(self) -> int:
"""Return the sleep duration value in minutes.
When value is scheduled to be changed the return value is the optimistic value.
"""
if self._battery_config.sleep_duration is not None:
return self._battery_config.sleep_duration
return SED_DEFAULT_SLEEP_DURATION
# endregion
async def _configure_sed_task(self) -> bool:
"""Configure SED settings. Returns True if successful."""
if not self._battery_config.dirty:
return True
_LOGGER.debug(
"_configure_sed_task | Node %s | request change",
self._mac_in_str,
)
if not await self.sed_configure():
_LOGGER.debug("Battery Configuration for %s failed", self._mac_in_str)
return False
return True
async def _awake_response(self, response: PlugwiseResponse) -> bool:
"""Process awake message."""
if not isinstance(response, NodeAwakeResponse):
raise MessageError(
f"Invalid response message type ({response.__class__.__name__}) received, expected NodeAwakeResponse"
)
_LOGGER.debug("Device %s is awake for %s", self.name, response.awake_type)
self._set_cache(CACHE_SED_AWAKE_TIMESTAMP, response.timestamp)
await self._available_update_state(True, response.timestamp)
# Pre populate the last awake timestamp
if self._last_awake.get(response.awake_type) is None:
self._last_awake[response.awake_type] = response.timestamp
# Skip awake messages when they are shortly after each other
elif (
self._last_awake[response.awake_type] + timedelta(seconds=AWAKE_RETRY)
> response.timestamp
):
return True
self._last_awake[response.awake_type] = response.timestamp
tasks: list[Coroutine[Any, Any, None]] = [
self._reset_awake(response.timestamp),
self.publish_feature_update_to_subscribers(
NodeFeature.BATTERY,
self._battery_config,
),
self.save_cache(),
]
self._delayed_task = create_task(
self._run_awake_tasks(), name=f"Delayed update for {self._mac_in_str}"
)
if response.awake_type == NodeAwakeResponseType.MAINTENANCE:
self._last_awake_reason = "Maintenance"
self._set_cache(CACHE_SED_AWAKE_REASON, "Maintenance")
if not self._maintenance_interval_restored_from_cache:
self._detect_maintenance_interval(response.timestamp)
if self._ping_at_awake:
tasks.append(self.update_ping_at_awake())
elif response.awake_type == NodeAwakeResponseType.FIRST:
self._last_awake_reason = "First"
self._set_cache(CACHE_SED_AWAKE_REASON, "First")
elif response.awake_type == NodeAwakeResponseType.STARTUP:
self._last_awake_reason = "Startup"
self._set_cache(CACHE_SED_AWAKE_REASON, "Startup")
elif response.awake_type == NodeAwakeResponseType.STATE:
self._last_awake_reason = "State update"
self._set_cache(CACHE_SED_AWAKE_REASON, "State update")
elif response.awake_type == NodeAwakeResponseType.BUTTON:
self._last_awake_reason = "Button press"
self._set_cache(CACHE_SED_AWAKE_REASON, "Button press")
if self._ping_at_awake:
tasks.append(self.update_ping_at_awake())
await gather(*tasks)
return True
async def update_ping_at_awake(self) -> None:
"""Get ping statistics."""
await self.ping_update()
def _detect_maintenance_interval(self, timestamp: datetime) -> None:
"""Detect current maintenance interval."""
if self._last_awake[NodeAwakeResponseType.MAINTENANCE] == timestamp:
return
new_interval_in_sec = (
timestamp - self._last_awake[NodeAwakeResponseType.MAINTENANCE]
).seconds
new_interval_in_min = round(new_interval_in_sec // 60)
_LOGGER.warning(
"Detect current maintenance interval for %s: %s (seconds), current %s (min)",
self.name,
new_interval_in_sec,
self._battery_config.maintenance_interval,
)
# Validate new maintenance interval in seconds but store it in minutes
if (new_interval_in_sec + SED_MAX_MAINTENANCE_INTERVAL_OFFSET) < (
SED_DEFAULT_MAINTENANCE_INTERVAL * 60
):
self._battery_config = replace(
self._battery_config, maintenance_interval=new_interval_in_min
)
self._set_cache(CACHE_SED_MAINTENANCE_INTERVAL, new_interval_in_min)
elif (new_interval_in_sec - SED_MAX_MAINTENANCE_INTERVAL_OFFSET) > (
SED_DEFAULT_MAINTENANCE_INTERVAL * 60
):
self._battery_config = replace(
self._battery_config, maintenance_interval=new_interval_in_min
)
self._set_cache(CACHE_SED_MAINTENANCE_INTERVAL, new_interval_in_min)
else:
# Within off-set margin of default, so use the default
self._battery_config = replace(
self._battery_config,
maintenance_interval=SED_DEFAULT_MAINTENANCE_INTERVAL,
)
self._set_cache(
CACHE_SED_MAINTENANCE_INTERVAL, SED_DEFAULT_MAINTENANCE_INTERVAL
)
self._maintenance_interval_restored_from_cache = True
async def _reset_awake(self, last_alive: datetime) -> None:
"""Reset node alive state."""
if self._awake_timer_task is not None and not self._awake_timer_task.done():
self._awake_timer_task.cancel()
# Setup new maintenance timer
self._awake_timer_task = create_task(
self._awake_timer(), name=f"Node awake timer for {self._mac_in_str}"
)
async def _awake_timer(self) -> None:
"""Task to monitor to get next awake in time. If not it sets device to be unavailable."""
# wait for next maintenance timer, but allow missing one
timeout_interval = self.maintenance_interval * 60 * 2.1
await sleep(timeout_interval)
# No maintenance awake message within expected time frame
# Mark node as unavailable
if self._available:
last_awake = self._last_awake.get(NodeAwakeResponseType.MAINTENANCE)
_LOGGER.warning(
"No awake message received from %s | last_maintenance_awake=%s | interval=%s (%s) | Marking node as unavailable",
self.name,
last_awake,
self.maintenance_interval,
timeout_interval,
)
await self._available_update_state(False)
async def _run_awake_tasks(self) -> None:
"""Execute all awake tasks."""
_LOGGER.debug("_run_awake_tasks | Device %s", self.name)
if (
self._sed_node_info_update_task_scheduled
and await self.node_info_update(None) is not None
):
self._sed_node_info_update_task_scheduled = False
if self._battery_config.dirty:
await self._configure_sed_task()
async def sed_configure(self) -> bool:
"""Reconfigure the sleep/awake settings for a SED send at next awake of SED."""
request = NodeSleepConfigRequest(
self._send,
self._mac_in_bytes,
self._battery_config.awake_duration,
self._battery_config.maintenance_interval,
self._battery_config.sleep_duration,
self._battery_config.clock_sync,
self._battery_config.clock_interval,
)
_LOGGER.debug(
"sed_configure | Device %s | awake_duration=%s | clock_interval=%s | clock_sync=%s | maintenance_interval=%s | sleep_duration=%s",
self.name,
self._battery_config.awake_duration,
self._battery_config.clock_interval,
self._battery_config.clock_sync,
self._battery_config.maintenance_interval,
self._battery_config.sleep_duration,
)
if (response := await request.send()) is None:
_LOGGER.warning(
"No response from %s to configure sleep settings request", self.name
)
return False
if response.response_type == NodeResponseType.SED_CONFIG_FAILED:
_LOGGER.warning("Failed to configure sleep settings for %s", self.name)
return False
if response.response_type == NodeResponseType.SED_CONFIG_ACCEPTED:
self._battery_config = replace(self._battery_config, dirty=False)
await self._sed_configure_update()
return True
_LOGGER.warning(
"Unexpected response type %s for %s",
response.response_type,
self.name,
)
return False
async def _sed_configure_update(self) -> None:
"""Process result of SED configuration update."""
self._set_cache(
CACHE_SED_MAINTENANCE_INTERVAL,
str(self._battery_config.maintenance_interval),
)
self._set_cache(
CACHE_SED_AWAKE_DURATION, str(self._battery_config.awake_duration)
)
self._set_cache(
CACHE_SED_CLOCK_INTERVAL, str(self._battery_config.clock_interval)
)
self._set_cache(
CACHE_SED_SLEEP_DURATION, str(self._battery_config.sleep_duration)
)
self._set_cache(CACHE_SED_CLOCK_SYNC, str(self._battery_config.clock_sync))
self._set_cache(CACHE_SED_DIRTY, str(self._battery_config.dirty))
await gather(
*[
self.save_cache(),
self.publish_feature_update_to_subscribers(
NodeFeature.BATTERY,
self._battery_config,
),
]
)
async def get_state(self, features: tuple[NodeFeature]) -> dict[NodeFeature, Any]:
"""Update latest state for given feature."""
states: dict[NodeFeature, Any] = {}
for feature in features:
if feature not in self._features:
raise NodeError(
f"Update of feature '{feature.name}' is "
+ f"not supported for {self.mac}"
)
match feature:
case NodeFeature.INFO:
states[NodeFeature.INFO] = await self.node_info_update()
case NodeFeature.BATTERY:
states[NodeFeature.BATTERY] = self._battery_config
case _:
state_result = await super().get_state((feature,))
if feature in state_result:
states[feature] = state_result[feature]
return states