diff --git a/src/energenie/Devices.py b/src/energenie/Devices.py index 9c07b56..5f5d7a2 100644 --- a/src/energenie/Devices.py +++ b/src/energenie/Devices.py @@ -277,6 +277,9 @@ self.capabilities = Capabilities() self.updated_cb = None + def get_config(self): + raise RuntimeError("There is no configuration for a base Device") + def has_switch(self): return hasattr(self.capabilities, "switch") @@ -353,6 +356,14 @@ def __repr__(self): return "LegacyDevice(%s)" % str(self.device_id) + def get_config(self): + """Get the persistable config, enough to reconstruct this class from a factory""" + return { + "type": self.__class__.__name__, + "device_id": self.device_id + } + + def send_message(self, payload): #TODO: interface with air_interface # Encode the payload two bits per byte as per OOK spec @@ -388,6 +399,15 @@ #self.config.encryptPID = CRYPT_PID #self.config.encryptPIP = CRYPT_PIP + def get_config(self): + """Get the persistable config, enough to reconstruct this class from a factory""" + return { + "type": self.__class__.__name__, + "manufacturer_id": self.manufacturer_id, + "product_id": self.product_id, + "device_id": self.device_id + } + def __repr__(self): return "MiHomeDevice(%s,%s,%s)" % (str(self.manufacturer_id), str(self.product_id), str(self.device_id)) diff --git a/src/energenie/Registry.py b/src/energenie/Registry.py index 53f1dac..094cfa7 100644 --- a/src/energenie/Registry.py +++ b/src/energenie/Registry.py @@ -158,7 +158,7 @@ print("####HERE") print(values, type(values)) """Append a new record to the persistent file""" - with open(self.filename, 'w+') as f: + with open(self.filename, 'a') as f: f.write("ADD %s\n" % key) for k in values: v = values[k] @@ -238,19 +238,7 @@ def add(self, device, name): """Add a device class instance to the registry, with a friendly name""" - ####HERE - #TODO: this is a Device class instance - #need to get appropriate data out from it as a map - #TODO: This is correct for a MiHomeDevice - #but not for a LegacyDevice - #TODO: Also, need the class name - values = { - "type": Devices.MiHomeDevice, ##TODO MIHO005 ?? - "manufacturer_id": device.manufacturer_id, - "product_id": device.product_id, - "device_id": device.device_id - } - self.store[name] = values + self.store[name] = device.get_config() def get(self, name): # -> Device """Get the description for a device class from the store, and construct a class instance""" diff --git a/src/energenie/Registry_test.py b/src/energenie/Registry_test.py index 0597cb4..dd6b77c 100644 --- a/src/energenie/Registry_test.py +++ b/src/energenie/Registry_test.py @@ -19,6 +19,8 @@ @test_1 def test_create(self): + import os + os.unlink('registry.kvs') registry = DeviceRegistry("registry.kvs") # add some devices to the registry, it should auto update the file @@ -27,7 +29,7 @@ #Perhaps in Device(), LegacyDevice() and MiHomeDevice() it does this for us #and returns a map we can just persist and create from later? - ##registry.add(Devices.ENER002(device_id=(0xC8C8C, 1)), "fan") + registry.add(Devices.ENER002(device_id=(0xC8C8C, 1)), "fan") # see what the file looks like with open(registry.DEFAULT_FILENAME) as f: