Skip to content

Commit

Permalink
Change Set_Delta to Increment_Delta; Don't Remove DB File on Clear
Browse files Browse the repository at this point in the history
Increment is really what was needed.  Gracefully handles a delta
of None for example if a user used refresh=False

Partially Fixes #292
Fixes #243
  • Loading branch information
krkeegan committed Dec 31, 2020
1 parent 519c3fc commit b885376
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 23 deletions.
26 changes: 11 additions & 15 deletions insteon_mqtt/db/Device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion insteon_mqtt/db/DeviceModifyManagerI1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

#-------------------------------------------------------------------
Expand Down
5 changes: 1 addition & 4 deletions insteon_mqtt/db/Modem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion insteon_mqtt/handler/DeviceDbModify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion insteon_mqtt/handler/DeviceRefresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion tests/db/test_Device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
31 changes: 31 additions & 0 deletions tests/db/test_Modem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b885376

Please sign in to comment.