diff --git a/doc/devices_classes_branch.txt b/doc/devices_classes_branch.txt index 7ec4d06..ea898fc 100644 --- a/doc/devices_classes_branch.txt +++ b/doc/devices_classes_branch.txt @@ -408,14 +408,17 @@ -------------------------------------------------------------------------------- TODO NEXT - DONE -* consider a callback in the router for new_data, for all messages - regardless of if known or not (good for generic data logging) - - * design a way that a MIHO005 class instance can be auto generated from a product_id field +DeviceFactory.get_device_from_name(name, device_id) +DeviceFactory.get_device_from_id(productid) -> Device class instance, configured + +Will have to beef this up later with mfrid and other details such as the radio interface +identifier, protocol identier, etc. But this is good enough (for now) + + + ---- * implement auto device registration in the router (poor mans discovery) diff --git a/src/energenie/Devices.py b/src/energenie/Devices.py index 1a2588e..33186a2 100644 --- a/src/energenie/Devices.py +++ b/src/energenie/Devices.py @@ -442,6 +442,10 @@ return "ENER002(%s,%s)" % (str(hex(self.device_id[0]), str(self.device_id[1]))) +class MIHO004(MiHomeDevice): + """Monitor only Adaptor""" + pass #TODO + class MIHO005(MiHomeDevice): """An Energenie MiHome Adaptor Plus""" @@ -653,16 +657,29 @@ # and a DeviceFactory could auto configure it's set of devices # with a specific air_interface for us. # i.e. this might be the EnergenieDeviceFactory, there might be others +# for other product ranges like wirefree doorbells class DeviceFactory(): """A place to come to, to get instances of device classes""" - devices = { + # If you know the name of the device, use this table + device_from_name = { # official name friendly name "ENER002": ENER002, "GreenButton": ENER002, "MIHO005": MIHO005, "AdaptorPlus": MIHO005, "MIHO006": MIHO006, "HomeMonitor": MIHO006, "MIHO013": MIHO013, "eTRV": MIHO013, } + + #TODO: These are MiHome devices only, but might add in mfrid prefix too + # If you know the mfrid, productid of the device, use this table + device_from_id = { + PRODUCTID_MIHO004: MIHO004, + PRODUCTID_MIHO005: MIHO005, + PRODUCTID_MIHO006: MIHO006, + PRODUCTID_MIHO013: MIHO013 + #ENER product range does not have deviceid, as it does not transmit + } + default_air_interface = None @staticmethod @@ -671,19 +688,33 @@ @staticmethod def keys(): - return DeviceFactory.devices.keys() + return DeviceFactory.device_from_name.keys() @staticmethod - def get_device(name, air_interface=None, device_id=None): + def get_device_from_name(name, device_id=None, air_interface=None): """Get a device by name, construct a new instance""" - if not name in DeviceFactory.devices: + # e.g. This is useful when creating device class instances from a human readable config + if not name in DeviceFactory.device_from_name: raise ValueError("Unsupported device:%s" % name) - c = DeviceFactory.devices[name] + c = DeviceFactory.device_from_name[name] if air_interface == None: air_interface = DeviceFactory.default_air_interface - #TODO: different devices have different init signatures at moment - return c(air_interface, device_id) + return c(device_id, air_interface) + + @staticmethod + def get_device_from_id(id, device_id=None, air_interface=None): + """Get a device by it's id, construct a new instance""" + # e.g. This is useful when recreating device class instances from a persisted registry + if not id in DeviceFactory.device_from_id: + raise ValueError("Unsupported device id:%s" % id) + + c = DeviceFactory.device_from_id[id] + if air_interface == None: + air_interface = DeviceFactory.default_air_interface + i = c(device_id, air_interface) + print(i) + return i # END diff --git a/src/energenie/Devices_test.py b/src/energenie/Devices_test.py index cd38e3e..6e437df 100644 --- a/src/energenie/Devices_test.py +++ b/src/energenie/Devices_test.py @@ -17,18 +17,21 @@ def test_without_registry(): - tv = DeviceFactory.get_device("GreenButton", device_id=(0xC8C8C, 1)) - fan = DeviceFactory.get_device("AdaptorPlus", device_id=0x68b) + tv = DeviceFactory.get_device_from_name("GreenButton", device_id=(0xC8C8C, 1)) + fan = DeviceFactory.get_device_from_name("AdaptorPlus", device_id=0x68b) + xbox = DeviceFactory.get_device_from_id(PRODUCTID_MIHO005, device_id=10) while True: print("ON") tv.turn_on() fan.turn_off() + xbox.turn_off() time.sleep(2) print("OFF") tv.turn_off() fan.turn_on() + xbox.turn_on() time.sleep(1) diff --git a/src/energenie/radio.py b/src/energenie/radio.py index dcaf44c..d2cf295 100644 --- a/src/energenie/radio.py +++ b/src/energenie/radio.py @@ -114,8 +114,10 @@ if DEBUG: print("***TX %s" % payload) - ##import OpenThings - ##print(OpenThings.decode(payload)) + ##import OpenThings + ##if payload[0] < 20: # crude way to reject ook messages + ## print("PAYLOAD: %s" % OpenThings.decode(payload)) + # remember that the sensorId is encrypted too framelen = len(payload) if framelen < 1 or framelen > 255: