diff --git a/insteon_mqtt/db/Device.py b/insteon_mqtt/db/Device.py index 357d78d4..7258a84c 100644 --- a/insteon_mqtt/db/Device.py +++ b/insteon_mqtt/db/Device.py @@ -176,17 +176,14 @@ def is_current(self, delta): return delta == self.delta #----------------------------------------------------------------------- - def set_delta(self, delta): - """Set the current database delta. + def increment_delta(self): + """Increments the current database delta by 1 - This records the input delta as the current value. If the input - isn't None, the database is also saved to record this value. - - Args: - delta: (int) The database delta. None to clear the delta. + Bumps up the delta by one, rolling it over to 0 if necessary. Will + ignore a delta which is None. """ - self.delta = delta - if delta is not None: + if self.delta is not None: + self.delta += 1 self.delta = self.delta % 256 # Roll over db if it goes past 256 self.save() @@ -275,19 +272,18 @@ def get_meta(self, key): #----------------------------------------------------------------------- def clear(self): - """Clear the complete database of entries. + """Clear the cached database of entries - This also removes the saved file if it exists. It does NOT modify - the database on the device. + This also saves the empty database to file. It does NOT modify + the database on the device. Nor does it clear the meta entries in + the database file. """ self.delta = None self.entries.clear() self.unused.clear() self.groups.clear() self.last.mem_loc = START_MEM_LOC - - if self.save_path and os.path.exists(self.save_path): - os.remove(self.save_path) + self.save() #----------------------------------------------------------------------- def set_path(self, path): diff --git a/insteon_mqtt/db/DeviceModifyManagerI1.py b/insteon_mqtt/db/DeviceModifyManagerI1.py index 71a7ac37..a652eaff 100644 --- a/insteon_mqtt/db/DeviceModifyManagerI1.py +++ b/insteon_mqtt/db/DeviceModifyManagerI1.py @@ -164,7 +164,7 @@ def handle_lsb_write_response(self, msg, on_done): completion of the modification """ # Increment the delta 1 - self.db.set_delta(self.db.delta + 1) + self.db.increment_delta() self.handle_lsb_response(msg, on_done) #------------------------------------------------------------------- diff --git a/insteon_mqtt/db/Modem.py b/insteon_mqtt/db/Modem.py index e43856ee..1c13d975 100644 --- a/insteon_mqtt/db/Modem.py +++ b/insteon_mqtt/db/Modem.py @@ -196,10 +196,7 @@ def clear(self): self.entries = [] self.groups = {} self.aliases = {} - self._meta = {} - - if self.save_path and os.path.exists(self.save_path): - os.remove(self.save_path) + self.save() #----------------------------------------------------------------------- def find_group(self, group): diff --git a/insteon_mqtt/handler/DeviceDbModify.py b/insteon_mqtt/handler/DeviceDbModify.py index 8d0ad5f7..bc53771b 100644 --- a/insteon_mqtt/handler/DeviceDbModify.py +++ b/insteon_mqtt/handler/DeviceDbModify.py @@ -73,7 +73,7 @@ def msg_received(self, protocol, msg): LOG.info("Updating entry: %s", self.entry) self.db.add_entry(self.entry) # Increment the delta 1 - self.db.set_delta(self.db.delta + 1) + self.db.increment_delta() self.on_done(True, "Device database update complete", self.entry) diff --git a/insteon_mqtt/handler/DeviceRefresh.py b/insteon_mqtt/handler/DeviceRefresh.py index 6cc3ec9f..9f69c54b 100644 --- a/insteon_mqtt/handler/DeviceRefresh.py +++ b/insteon_mqtt/handler/DeviceRefresh.py @@ -105,7 +105,7 @@ def msg_received(self, protocol, msg): # w/ the current value and save the database. def on_done(success, message, data): if success: - self.device.db.set_delta(msg.cmd1) + self.device.db.delta = msg.cmd1 LOG.ui("%s database download complete\n%s", self.addr, self.device.db) self.on_done(success, message, data) diff --git a/tests/db/test_Device.py b/tests/db/test_Device.py index 425b061e..1aaaa287 100644 --- a/tests/db/test_Device.py +++ b/tests/db/test_Device.py @@ -17,8 +17,13 @@ def test_basic(self): assert obj.is_current(0) is False obj.delta = 1 assert obj.is_current(1) is True - obj.set_delta(None) + obj.increment_delta() assert obj.is_current(1) is False + assert obj.is_current(2) is True + # test roll over at 256 + obj.delta = 255 + obj.increment_delta() + assert obj.is_current(0) is True assert obj.engine is None obj.set_engine(1) @@ -109,11 +114,15 @@ def test_basic(self): assert len(obj2.unused) == 1 assert len(obj2.groups) == 2 + obj2.set_meta('test', 2) obj2.clear() assert len(obj2) == 0 assert len(obj2.entries) == 0 assert len(obj2.unused) == 0 assert len(obj2.groups) == 0 + assert len(obj2._meta) == 1 + assert obj2.get_meta('test') == 2 + #----------------------------------------------------------------------- def test_add_multi_group(self): diff --git a/tests/db/test_Modem.py b/tests/db/test_Modem.py index 173e5fc2..64826a3d 100644 --- a/tests/db/test_Modem.py +++ b/tests/db/test_Modem.py @@ -77,6 +77,37 @@ def test_basic(self): assert obj2.entries[1] == obj.entries[1] assert obj2.entries[2] == obj.entries[2] + #----------------------------------------------------------------------- + def test_clear(self): + obj = IM.db.Modem() + assert len(obj) == 0 + + addr = IM.Address('12.34.ab') + data = bytes([0xff, 0x00, 0x00]) + e = IM.db.ModemEntry(addr, 0x01, True, data, db=obj) + obj.add_entry(e) + + addr = IM.Address('12.34.ac') + data = bytes([0xff, 0x00, 0x00]) + e = IM.db.ModemEntry(addr, 0x01, True, data, db=obj) + obj.add_entry(e) + + addr = IM.Address('12.34.ad') + data = bytes([0xff, 0x00, 0x00]) + e = IM.db.ModemEntry(addr, 0x02, True, data, db=obj) + obj.add_entry(e) + + assert len(obj) == 3 + + obj.set_meta('test', 2) + obj.clear() + assert len(obj) == 0 + assert len(obj.entries) == 0 + assert len(obj.groups) == 0 + assert len(obj.aliases) == 0 + assert len(obj._meta) == 1 + assert obj.get_meta('test') == 2 + #----------------------------------------------------------------------- def test_add_on_device_empty_ctrl(self, test_device, test_entry_dev1_ctrl): # add_on_device(self, entry, on_done=None)