Skip to content

Commit bc8637d

Browse files
committed
Make devices a property
1 parent c76e4ce commit bc8637d

File tree

2 files changed

+59
-50
lines changed

2 files changed

+59
-50
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Added: New property attributes for USB-stick.
99
The old methods are still available but will give a deprecate warning
1010
- Stick
11+
- `devices` (dict) - All discovered and supported plugwise devices with the MAC address as their key
1112
- `discovered_nodes` (list) - List of MAC addresses of all discovered nodes
1213
- `joined_nodes` (integer) - Total number of registered nodes at Plugwise Circle+
1314
- `mac` (string) - The MAC address of the USB-Stick

plugwise/stick.py

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def __init__(self, port, callback=None):
7676
self._circle_plus_retries = 0
7777
self.scan_callback = None
7878
self._network_id = None
79-
self._plugwise_nodes = {}
79+
self._device_nodes = {}
8080
self._joined_nodes = 0
8181
self._nodes_to_discover = {}
8282
self._nodes_not_discovered = {}
@@ -97,12 +97,17 @@ def __init__(self, port, callback=None):
9797
if callback:
9898
self.auto_initialize(callback)
9999

100+
@property
101+
def devices(self) -> dict:
102+
"""All discovered and supported plugwise devices with the MAC address as their key"""
103+
return self._device_nodes
104+
100105
@property
101106
def discovered_nodes(self) -> list:
102107
"""Return a list of mac addresses of all discovered and supported plugwise nodes."""
103108
return list(
104109
dict(
105-
filter(lambda item: item[1] is not None, self._plugwise_nodes.items())
110+
filter(lambda item: item[1] is not None, self._device_nodes.items())
106111
).keys()
107112
)
108113

@@ -251,9 +256,9 @@ def scan(self, callback=None):
251256

252257
def scan_circle_plus(self):
253258
"""Scan the Circle+ memory for registered nodes."""
254-
if self._plugwise_nodes.get(self.circle_plus_mac):
259+
if self._device_nodes.get(self.circle_plus_mac):
255260
_LOGGER.debug("Scan Circle+ for linked nodes...")
256-
self._plugwise_nodes[self.circle_plus_mac].scan_for_nodes(
261+
self._device_nodes[self.circle_plus_mac].scan_for_nodes(
257262
self.discover_nodes
258263
)
259264
else:
@@ -262,7 +267,7 @@ def scan_circle_plus(self):
262267
def scan_for_registered_nodes(self):
263268
"""Discover Circle+ and all registered nodes at Circle+."""
264269
if self.circle_plus_mac:
265-
if self._plugwise_nodes.get(self.circle_plus_mac):
270+
if self._device_nodes.get(self.circle_plus_mac):
266271
self.scan_circle_plus()
267272
else:
268273
_LOGGER.debug("Discover Circle+ at %s", self.circle_plus_mac)
@@ -295,19 +300,19 @@ def node_discovered_by_scan(self, nodes_off_line=False):
295300
self._nodes_discovered += 1
296301
_LOGGER.debug(
297302
"Discovered Plugwise node %s (%s off-line) of %s",
298-
str(len(self._plugwise_nodes)),
303+
str(len(self._device_nodes)),
299304
str(self._nodes_off_line),
300305
str(len(self._nodes_to_discover)),
301306
)
302-
if (len(self._plugwise_nodes) - 1 + self._nodes_off_line) >= len(
307+
if (len(self._device_nodes) - 1 + self._nodes_off_line) >= len(
303308
self._nodes_to_discover
304309
):
305310
if self._nodes_off_line == 0:
306311
self._nodes_to_discover = {}
307312
self._nodes_not_discovered = {}
308313
else:
309314
for mac in self._nodes_to_discover:
310-
if mac not in self._plugwise_nodes.keys():
315+
if mac not in self._device_nodes.keys():
311316
_LOGGER.info(
312317
"Failed to discover node type for registered MAC '%s'. This is expected for battery powered nodes, they will be discovered at their first awake",
313318
str(mac),
@@ -323,7 +328,7 @@ def scan_timeout_expired(self):
323328
"""Timeout for initial scan."""
324329
if not self.msg_controller.discovery_finished:
325330
for mac in self._nodes_to_discover:
326-
if mac not in self._plugwise_nodes.keys():
331+
if mac not in self._device_nodes.keys():
327332
_LOGGER.info(
328333
"Failed to discover node type for registered MAC '%s'. This is expected for battery powered nodes, they will be discovered at their first awake",
329334
str(mac),
@@ -342,50 +347,46 @@ def _append_node(self, mac, address, node_type):
342347
mac,
343348
)
344349
if node_type == NODE_TYPE_CIRCLE_PLUS:
345-
self._plugwise_nodes[mac] = PlugwiseCirclePlus(
350+
self._device_nodes[mac] = PlugwiseCirclePlus(
346351
mac, address, self.msg_controller.send
347352
)
348353
elif node_type == NODE_TYPE_CIRCLE:
349-
self._plugwise_nodes[mac] = PlugwiseCircle(
354+
self._device_nodes[mac] = PlugwiseCircle(
350355
mac, address, self.msg_controller.send
351356
)
352357
elif node_type == NODE_TYPE_SWITCH:
353-
self._plugwise_nodes[mac] = None
358+
self._device_nodes[mac] = None
354359
elif node_type == NODE_TYPE_SENSE:
355-
self._plugwise_nodes[mac] = PlugwiseSense(
360+
self._device_nodes[mac] = PlugwiseSense(
356361
mac, address, self.msg_controller.send
357362
)
358363
elif node_type == NODE_TYPE_SCAN:
359-
self._plugwise_nodes[mac] = PlugwiseScan(
364+
self._device_nodes[mac] = PlugwiseScan(
360365
mac, address, self.msg_controller.send
361366
)
362367
elif node_type == NODE_TYPE_CELSIUS_SED:
363-
self._plugwise_nodes[mac] = None
368+
self._device_nodes[mac] = None
364369
elif node_type == NODE_TYPE_CELSIUS_NR:
365-
self._plugwise_nodes[mac] = None
370+
self._device_nodes[mac] = None
366371
elif node_type == NODE_TYPE_STEALTH:
367-
self._plugwise_nodes[mac] = PlugwiseStealth(
372+
self._device_nodes[mac] = PlugwiseStealth(
368373
mac, address, self.msg_controller.send
369374
)
370375
else:
371376
_LOGGER.warning("Unsupported node type '%s'", str(node_type))
372-
self._plugwise_nodes[mac] = None
377+
self._device_nodes[mac] = None
373378

374379
# process previous missed messages
375380
msg_to_process = self._messages_for_undiscovered_nodes[:]
376381
self._messages_for_undiscovered_nodes = []
377382
for msg in msg_to_process:
378383
self.message_processor(msg)
379384

380-
def node(self, mac: str) -> PlugwiseNode:
381-
"""Return a specific node object"""
382-
return self._plugwise_nodes.get(mac)
383-
384385
def node_state_updates(self, mac, state: bool):
385386
"""Update availability state of a node"""
386-
if mac in self._plugwise_nodes:
387-
if not self._plugwise_nodes[mac].battery_powered:
388-
self._plugwise_nodes[mac].available = state
387+
if mac in self._device_nodes:
388+
if not self._device_nodes[mac].battery_powered:
389+
self._device_nodes[mac].available = state
389390

390391
def node_join(self, mac: str, callback=None) -> bool:
391392
"""Accept node to join Plugwise network by register mac in Circle+ memory"""
@@ -413,8 +414,8 @@ def node_unjoin(self, mac: str, callback=None) -> bool:
413414

414415
def _remove_node(self, mac):
415416
"""Remove node from list of controllable nodes."""
416-
if mac in self._plugwise_nodes:
417-
del self._plugwise_nodes[mac]
417+
if mac in self._device_nodes:
418+
del self._device_nodes[mac]
418419
else:
419420
_LOGGER.warning("Node %s does not exists, unable to remove node.", mac)
420421

@@ -487,7 +488,7 @@ def _process_node_join_request(self, node_join_request, mac):
487488
Process NodeJoinAvailableResponse message from a node that
488489
is not part of a plugwise network yet and wants to join
489490
"""
490-
if not self._plugwise_nodes.get(mac):
491+
if not self._device_nodes.get(mac):
491492
if self._accept_join_requests:
492493
# Send accept join request
493494
_LOGGER.info(
@@ -515,8 +516,8 @@ def _process_node_remove(self, node_remove_response):
515516
"""
516517
unjoined_mac = node_remove_response.node_mac_id.value
517518
if node_remove_response.status.value == 1:
518-
if self._plugwise_nodes.get(unjoined_mac):
519-
del self._plugwise_nodes[unjoined_mac]
519+
if self._device_nodes.get(unjoined_mac):
520+
del self._device_nodes[unjoined_mac]
520521
_LOGGER.info(
521522
"Received NodeRemoveResponse from node %s it has been unjoined from Plugwise network",
522523
unjoined_mac,
@@ -538,8 +539,8 @@ def _pass_message_to_node(self, message, mac, discover=True):
538539
539540
Returns True if message has passed onto existing known node
540541
"""
541-
if self._plugwise_nodes.get(mac):
542-
self._plugwise_nodes[mac].message_for_node(message)
542+
if self._device_nodes.get(mac):
543+
self._device_nodes[mac].message_for_node(message)
543544
return True
544545
if discover:
545546
_LOGGER.info(
@@ -635,22 +636,22 @@ def _update_loop(self):
635636
day_of_month = datetime.now().day
636637
try:
637638
while self._run_update_thread:
638-
for mac in self._plugwise_nodes:
639-
if self._plugwise_nodes[mac]:
640-
if self._plugwise_nodes[mac].battery_powered:
639+
for mac in self._device_nodes:
640+
if self._device_nodes[mac]:
641+
if self._device_nodes[mac].battery_powered:
641642
# Check availability state of SED's
642643
self._check_availability_of_seds(mac)
643644
else:
644645
# Do ping request for all non SED's
645-
self._plugwise_nodes[mac]._request_ping(None, True)
646+
self._device_nodes[mac]._request_ping(None, True)
646647

647-
if self._plugwise_nodes[mac].measures_power:
648+
if self._device_nodes[mac].measures_power:
648649
# Request current power usage
649-
self._plugwise_nodes[mac]._request_power_update()
650+
self._device_nodes[mac]._request_power_update()
650651
# Sync internal clock of power measure nodes once a day
651652
if datetime.now().day != day_of_month:
652653
day_of_month = datetime.now().day
653-
self._plugwise_nodes[mac].sync_clock()
654+
self._device_nodes[mac].sync_clock()
654655

655656
# Do a single ping for undiscovered nodes once per 10 update cycles
656657
if _discover_counter == 10:
@@ -692,7 +693,7 @@ def auto_update(self, timer=None):
692693
if not self._auto_update_manually:
693694
count_nodes = 0
694695
for mac in self.discovered_nodes:
695-
if self._plugwise_nodes[mac].measures_power:
696+
if self._device_nodes[mac].measures_power:
696697
count_nodes += 1
697698
self._auto_update_timer = 5 + (count_nodes * 1)
698699
_LOGGER.info(
@@ -717,32 +718,32 @@ def do_callback(self, callback_type, callback_arg=None):
717718

718719
def _check_availability_of_seds(self, mac):
719720
"""Helper to check if SED device is still sending its hartbeat."""
720-
if self._plugwise_nodes[mac].available:
721-
if self._plugwise_nodes[mac].last_update < (
721+
if self._device_nodes[mac].available:
722+
if self._device_nodes[mac].last_update < (
722723
datetime.now()
723724
- timedelta(
724-
minutes=(self._plugwise_nodes[mac].maintenance_interval + 1)
725+
minutes=(self._device_nodes[mac].maintenance_interval + 1)
725726
)
726727
):
727728
_LOGGER.info(
728729
"No messages received within (%s minutes) of expected maintenance interval from node %s, mark as unavailable [%s > %s]",
729-
str(self._plugwise_nodes[mac].maintenance_interval),
730+
str(self._device_nodes[mac].maintenance_interval),
730731
mac,
731-
str(self._plugwise_nodes[mac].last_update),
732+
str(self._device_nodes[mac].last_update),
732733
str(
733734
datetime.now()
734735
- timedelta(
735-
minutes=(self._plugwise_nodes[mac].maintenance_interval + 1)
736+
minutes=(self._device_nodes[mac].maintenance_interval + 1)
736737
)
737738
),
738739
)
739-
self._plugwise_nodes[mac].available = False
740+
self._device_nodes[mac].available = False
740741

741742
def _discover_after_scan(self):
742743
"""Helper to do callback for new node."""
743744
node_discovered = None
744745
for mac in self._nodes_not_discovered:
745-
if self._plugwise_nodes.get(mac):
746+
if self._device_nodes.get(mac):
746747
node_discovered = mac
747748
break
748749
if node_discovered:
@@ -752,7 +753,7 @@ def _discover_after_scan(self):
752753

753754
def discover_node(self, mac: str, callback=None, force_discover=False):
754755
"""Helper to try to discovery the node (type) based on mac."""
755-
if not validate_mac(mac) or self._plugwise_nodes.get(mac):
756+
if not validate_mac(mac) or self._device_nodes.get(mac):
756757
return
757758
if mac not in self._nodes_not_discovered:
758759
self._nodes_not_discovered[mac] = (
@@ -802,6 +803,13 @@ def nodes(self) -> list:
802803
)
803804
return list(
804805
dict(
805-
filter(lambda item: item[1] is not None, self._plugwise_nodes.items())
806+
filter(lambda item: item[1] is not None, self._device_nodes.items())
806807
).keys()
807808
)
809+
810+
def node(self, mac: str) -> PlugwiseNode:
811+
"""Return a specific node object"""
812+
_LOGGER.warning(
813+
"Function 'node' will be removed in future, use the 'devices' property (dict) instead !",
814+
)
815+
return self._device_nodes.get(mac)

0 commit comments

Comments
 (0)