diff --git a/src/energenie/Registry.py b/src/energenie/Registry.py index 212afe7..53f1dac 100644 --- a/src/energenie/Registry.py +++ b/src/energenie/Registry.py @@ -153,15 +153,18 @@ def size(self): return len(self.store) - @unimplemented + @untested def append(self, key, values): + print("####HERE") + print(values, type(values)) """Append a new record to the persistent file""" - pass #TODO - # open file for append - # write ADD key - # for all values - # write k=v - # close file + with open(self.filename, 'w+') as f: + f.write("ADD %s\n" % key) + for k in values: + v = values[k] + f.write("%s=%s\n" % (k, v)) + f.write("\n") + @unimplemented def remove(self, key): @@ -235,7 +238,19 @@ def add(self, device, name): """Add a device class instance to the registry, with a friendly name""" - self.store[name] = device + ####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 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 c2332ce..0597cb4 100644 --- a/src/energenie/Registry_test.py +++ b/src/energenie/Registry_test.py @@ -23,11 +23,17 @@ # add some devices to the registry, it should auto update the file registry.add(Devices.MIHO005(device_id=0x68b), "tv") - registry.add(Devices.ENER002(device_id=(0xC8C8C, 1)), "fan") + #TODO: Need to have a way to get the persistent summary of a device class + #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") # see what the file looks like with open(registry.DEFAULT_FILENAME) as f: - print(f.readlines()) + for l in f.readlines(): + l = l.strip() # remove nl + print(l) @test_0 def test_load(self):