diff --git a/doc/devices_classes_branch.txt b/doc/devices_classes_branch.txt index 8ee52d7..3c2f38b 100644 --- a/doc/devices_classes_branch.txt +++ b/doc/devices_classes_branch.txt @@ -409,7 +409,10 @@ -------------------------------------------------------------------------------- TODO NEXT -Code up join auto and join ask, and get tests passing. +joinauto with join_ack is working + +Code up and test join_autoask + diff --git a/src/energenie/Devices.py b/src/energenie/Devices.py index 5c3f07a..f7a2762 100644 --- a/src/energenie/Devices.py +++ b/src/energenie/Devices.py @@ -383,7 +383,9 @@ def join_ack(self): """Send a join-ack to the real device""" - self.send_message("join ack") # TODO + msg = OpenThings.Message(header_mfrid=MFRID_ENERGENIE, header_productid=self.product_id, header_sensorid=self.device_id) + msg[OpenThings.PARAM_JOIN] = {"wr":False, "typeid":OpenThings.Value.UINT, "length":0} + self.send_message(msg) #def handle_message(self, payload): #override for any specific handling diff --git a/src/energenie/OpenThings.py b/src/energenie/OpenThings.py index 4f3f319..5b27efe 100644 --- a/src/energenie/OpenThings.py +++ b/src/energenie/OpenThings.py @@ -655,7 +655,7 @@ paramid = rec["paramid"] if paramid == key: return rec - raise RuntimeError("no paramid found for %s" % str(hex(key))) + raise KeyError("no paramid found for %s" % str(hex(key))) def __setitem__(self, key, value): diff --git a/src/energenie/Registry.py b/src/energenie/Registry.py index 29a1b83..c6ff641 100644 --- a/src/energenie/Registry.py +++ b/src/energenie/Registry.py @@ -8,11 +8,14 @@ import time try: - import Devices # python 2 + # Python 2 + import Devices + import OpenThings except ImportError: - from . import Devices # python 3 + # Python 3 + from . import Devices + from . import OpenThings -import Devices directory = {} @@ -294,7 +297,7 @@ # default action is to drop message # override this method if you want special processing - def accept_device(self, address, message): + def accept_device(self, address, message, forward=True): print("accept_device:%s" % str(address)) # At moment, intentionally assume everything is mfrid=Energenie product_id = address[1] @@ -305,11 +308,13 @@ self.router.add(address, ci) # Finally, forward the first message to the new device class instance - print("**** routing first message to class instance") - ci.incoming_message(message) + if forward: + print("**** routing first message to class instance") + ci.incoming_message(message) ##self.registry.list() ##self.router.list() + return ci # The new device class instance that we created class AutoDiscovery(Discovery): @@ -341,13 +346,22 @@ Discovery.__init__(self, registry, router) def unknown_device(self, address, message): - print("TODO: unknown device auto join %s" % str(address)) - # if it is not a join req - # route to unhandled message handler - # if it is a join req - # accept the device - # send join ack back to device (using new device class instance?) - pass #TODO + print("unknown device auto join %s" % str(address)) + + #TODO: need to make this work with correct meta methods + ##if not OpenThings.PARAM_JOIN in message: + try: + j = message[OpenThings.PARAM_JOIN] + except KeyError: + j = None + + if j == None: # not a join + self.unknown_device(address, message) + else: # it is a join + # but don't forward the join request as it will be malformed with no value + ci = self.accept_device(address, message, forward=False) + ci.join_ack() # Ask new class instance to send a join_ack back to physical device + #TODO: MiHomeDevice needs this join_ack() added to it class JoinConfirmedDiscovery(Discovery): diff --git a/src/energenie/Registry_test.py b/src/energenie/Registry_test.py index 3a38177..2cdf0a1 100644 --- a/src/energenie/Registry_test.py +++ b/src/energenie/Registry_test.py @@ -145,9 +145,10 @@ # Poke synthetic unknown JOIN into the router and let it route to unknown handler self.msg = OpenThings.Message(header_mfrid=Devices.MFRID_ENERGENIE, - header_productid=Devices.MIHO005, + header_productid=Devices.PRODUCTID_MIHO005, header_sensorid=UNKNOWN_SENSOR_ID) self.msg[OpenThings.PARAM_JOIN] = {} + ##print(self.msg) fsk_router.incoming_message( (Devices.MFRID_ENERGENIE, Devices.PRODUCTID_MIHO005, UNKNOWN_SENSOR_ID), self.msg)