Skip to content

Commit

Permalink
Merge pull request #239 from krkeegan/Dev_Name_in_DB
Browse files Browse the repository at this point in the history
Print Device Names When Printing a Device's Database
  • Loading branch information
krkeegan authored Dec 13, 2020
2 parents 256ebbd + c1611a0 commit 0ccf11e
Show file tree
Hide file tree
Showing 18 changed files with 191 additions and 59 deletions.
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
- Added support for Smartenit EZIO4O 4 relay output module (thanks @embak)
([PR 219][P219])

- Device names are now printed when printing the database. This makes reading
the database output much easier. ([PR 239][P239])

### Fixes

- Database delta is updated on database writes. This eliminates a number of
Expand Down Expand Up @@ -438,3 +441,4 @@ will add new features.
[P240]: https://github.com/TD22057/insteon-mqtt/pull/240
[P248]: https://github.com/TD22057/insteon-mqtt/pull/248
[P219]: https://github.com/TD22057/insteon-mqtt/pull/219
[P239]: https://github.com/TD22057/insteon-mqtt/pull/239
15 changes: 8 additions & 7 deletions insteon_mqtt/db/Device.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ def from_json(data, path, device):
obj._meta = data.get('meta', {})

for d in data['used']:
obj.add_entry(DeviceEntry.from_json(d), save=False)
obj.add_entry(DeviceEntry.from_json(d, db=obj), save=False)

for d in data['unused']:
obj.add_entry(DeviceEntry.from_json(d), save=False)
obj.add_entry(DeviceEntry.from_json(d, db=obj), save=False)

if "last" in data:
obj.last = DeviceEntry.from_json(data["last"])
obj.last = DeviceEntry.from_json(data["last"], db=obj)

# When loading db's <= ver 0.6, no last field was saved to create
# one at the correct location.
Expand Down Expand Up @@ -146,7 +146,7 @@ def __init__(self, addr, path=None, device=None):
flags = Msg.DbFlags(in_use=False, is_controller=False,
is_last_rec=True)
self.last = DeviceEntry(Address(0, 0, 0), 0, START_MEM_LOC, flags,
None)
None, db=self)

# Map of all link group number to DeviceEntry objects that respond to
# that group command.
Expand Down Expand Up @@ -651,7 +651,7 @@ def __str__(self):

o.write("GroupMap\n")
for grp, elem in self.groups.items():
o.write(" %s -> %s\n" % (grp, [i.addr.hex for i in elem]))
o.write(" %s -> %s\n" % (grp, [i.label for i in elem]))

return o.getvalue()

Expand Down Expand Up @@ -731,7 +731,7 @@ def add_from_config(self, remote, local):
if remote.is_controller:
group = remote.group
entry = DeviceEntry(remote.addr, group, mem_loc, db_flags,
local.link_data)
local.link_data, db=self)

# Add the Entry to the DB
self.add_entry(entry, save=False)
Expand Down Expand Up @@ -809,7 +809,8 @@ def _add_using_new(self, addr, group, is_controller, data,
# Create the new entry at the current last memory location.
db_flags = Msg.DbFlags(in_use=True, is_controller=is_controller,
is_last_rec=False)
entry = DeviceEntry(addr, group, self.last.mem_loc, db_flags, data)
entry = DeviceEntry(addr, group, self.last.mem_loc, db_flags, data,
db=self)

if self.engine == 0:
# on_done is passed by the sequence manager inside seq.add()
Expand Down
44 changes: 34 additions & 10 deletions insteon_mqtt/db/DeviceEntry.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class DeviceEntry:
"""

@staticmethod
def from_json(data):
def from_json(data, db=None):
"""Read a DeviceEntry from a JSON input.
The inverse of this is to_json().
Expand All @@ -50,23 +50,26 @@ def from_json(data):
Returns:
DeviceEntry: Returns the created DeviceEntry object.
db: (db.Device) The parent database which contains this entry
"""
return DeviceEntry(Address.from_json(data['addr']),
data['group'],
data['mem_loc'],
Msg.DbFlags.from_json(data['db_flags']),
bytes(data['data']))
bytes(data['data']),
db=db)

#-----------------------------------------------------------------------
@staticmethod
def from_bytes(data):
def from_bytes(data, db=None):
"""Read a DeviceEntry from a byte array.
This is used to read an entry from an InpExtended insteon message
object. See p162 of the Insteon dev guide for the byte array layout.
Args:
data: (bytes) The data 14 byte array from an InpExtended message.
db: (db.device) The parent database containing this entry
Returns:
DeviceEntry: Returns the created DeviceEntry object.
Expand All @@ -81,11 +84,12 @@ def from_bytes(data):
link_addr = Address.from_bytes(data, 7)
link_data = data[10:13]

return DeviceEntry(link_addr, group, mem_loc, db_flags, link_data)
return DeviceEntry(link_addr, group, mem_loc, db_flags,
link_data, db=db)

#-----------------------------------------------------------------------
@staticmethod
def from_i1_bytes(data):
def from_i1_bytes(data, db=None):
"""Read a DeviceEntry from an i1 device byte array.
This is used to read an entry from the DeviceScanManagerI1 handler for
Expand All @@ -95,6 +99,7 @@ def from_i1_bytes(data):
Args:
data: (bytes) The 8 byte record, preceeded by the 2 byte
location.
db: (db.device) The parent database containing this entry
Returns:
DeviceEntry: Returns the created DeviceEntry object.
Expand All @@ -105,10 +110,11 @@ def from_i1_bytes(data):
link_addr = Address.from_bytes(data, 4)
link_data = data[7:10]

return DeviceEntry(link_addr, group, mem_loc, db_flags, link_data)
return DeviceEntry(link_addr, group, mem_loc, db_flags,
link_data, db=db)

#-----------------------------------------------------------------------
def __init__(self, addr, group, mem_loc, db_flags, data):
def __init__(self, addr, group, mem_loc, db_flags, data, db=None):
"""Constructor
Args:
Expand All @@ -118,6 +124,7 @@ def __init__(self, addr, group, mem_loc, db_flags, data):
db_flags: (message.DbFlags) The db controler record flags.
data: (bytes) 3 data bytes. [0] is the on level, [1] is the
ramp rate.
db: (db.device) The parent database containing this entry
"""
# Accept either bytes, list of ints, or None for the data input.
if data is not None:
Expand All @@ -132,6 +139,23 @@ def __init__(self, addr, group, mem_loc, db_flags, data):
self.db_flags = db_flags
self.is_controller = db_flags.is_controller
self.data = data
self.db = db

#-----------------------------------------------------------------------
@property
def label(self):
"""Returns the label of the device that the address in this entry is
associated with or the address if the device cannot be found.
Returns:
(str) A label or address for the entry
"""
# We allow for no db to be set
if self.db is not None and self.db.device is not None:
device = self.db.device.modem.find(self.addr)
if device is not None:
return device.label
return str(self.addr)

#-----------------------------------------------------------------------
def copy(self):
Expand All @@ -141,7 +165,7 @@ def copy(self):
Returns a copy of the DeviceEntry object.
"""
return DeviceEntry(self.addr, self.group, self.mem_loc,
self.db_flags.copy(), self.data[:])
self.db_flags.copy(), self.data[:], db=self.db)

#-----------------------------------------------------------------------
def update_from(self, addr, group, is_controller, data):
Expand Down Expand Up @@ -277,8 +301,8 @@ def __str__(self):
last = " (LAST)" if self.db_flags.is_last_rec else ""
unused = " (UNUSED)" if not self.db_flags.in_use else ""

return ("%04x: %s grp: %3s type: %s data: %#04x %#04x %#04x%s%s" %
(self.mem_loc, self.addr.hex, self.group,
return ("%04x: %-25s grp: %3s type: %s data: %#04x %#04x %#04x%s%s" %
(self.mem_loc, self.label[:25], self.group,
util.ctrl_str(self.db_flags.is_controller),
self.data[0], self.data[1], self.data[2], unused, last))

Expand Down
2 changes: 1 addition & 1 deletion insteon_mqtt/db/DeviceScanManagerI1.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def handle_get_lsb(self, msg, on_done):

# Convert the message to a database device entry.
entry = DeviceEntry.from_i1_bytes(bytes([self.msb, self.lsb] +
self.record))
self.record), db=self.db)
LOG.ui("Entry: %s", entry)
self.db.add_entry(entry)

Expand Down
6 changes: 3 additions & 3 deletions insteon_mqtt/db/Modem.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def from_json(data, path=None, device=None):
"""
obj = Modem(path, device)
for d in data['entries']:
obj.add_entry(ModemEntry.from_json(d), save=False)
obj.add_entry(ModemEntry.from_json(d, db=obj), save=False)

# pylint: disable=protected-access
obj._meta = data.get('meta', {})
Expand Down Expand Up @@ -502,7 +502,7 @@ def __str__(self):

o.write("GroupMap\n")
for grp, elem in self.groups.items():
o.write(" %s -> %s\n" % (grp, [i.addr.hex for i in elem]))
o.write(" %s -> %s\n" % (grp, [i.label for i in elem]))

return o.getvalue()

Expand Down Expand Up @@ -554,7 +554,7 @@ def add_from_config(self, remote, local):
if remote.is_controller:
group = remote.group
entry = ModemEntry(remote.addr, group, local.is_controller,
local.link_data)
local.link_data, db=self)

# Add the Entry to the DB
self.add_entry(entry, save=False)
Expand Down
31 changes: 26 additions & 5 deletions insteon_mqtt/db/ModemEntry.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,26 @@ class ModemEntry:
"""

@staticmethod
def from_json(data):
def from_json(data, db=None):
"""Read a ModemEntry from a JSON input.
The inverse of this is to_json().
Args:
data: (dict): The data to read from.
db: (db.Modem): The Modem database which this entry belongs
Returns:
ModemEntry: Returns the created ModemEntry object.
"""
return ModemEntry(Address.from_json(data['addr']),
data['group'],
data['is_controller'],
bytes(data['data']))
bytes(data['data']),
db=db)

#-----------------------------------------------------------------------
def __init__(self, addr, group, is_controller, data=None):
def __init__(self, addr, group, is_controller, data=None, db=None):
"""Constructor
Args:
Expand All @@ -47,6 +49,8 @@ def __init__(self, addr, group, is_controller, data=None):
False if this device is a responder of addr.
data: (bytes) 3 data bytes. [0] is the on level, [1]
is the ramp rate.
db: (db.Modem): The Modem database which this entry
belongs
"""
# Accept either bytes, list of ints, or None for the data input.
if data is not None:
Expand All @@ -60,6 +64,7 @@ def __init__(self, addr, group, is_controller, data=None):
self.group = int(group)
self.is_controller = is_controller
self.data = data
self.db = db

#-----------------------------------------------------------------------
def to_json(self):
Expand All @@ -75,6 +80,22 @@ def to_json(self):
'data' : list(self.data)
}

#-----------------------------------------------------------------------
@property
def label(self):
"""Returns the label of the device that the address in this entry is
associated with or the address if the device cannot be found.
Returns:
(str) A label or address for the entry
"""
# We allow for no db to be set
if self.db is not None and self.db.device is not None:
device = self.db.device.find(self.addr)
if device is not None:
return device.label
return str(self.addr)

#-----------------------------------------------------------------------
def __eq__(self, rhs):
"""Check for equality.
Expand All @@ -99,8 +120,8 @@ def __lt__(self, rhs):

#-----------------------------------------------------------------------
def __str__(self):
return ("ID: %s grp: %s type: %s data: %#04x %#04x %#04x" %
(self.addr.hex, self.group, util.ctrl_str(self.is_controller),
return ("ID: %-25s grp: %3s type: %s data: %#04x %#04x %#04x" %
(self.label[:25], self.group, util.ctrl_str(self.is_controller),
self.data[0], self.data[1], self.data[2]))

#-----------------------------------------------------------------------
2 changes: 1 addition & 1 deletion insteon_mqtt/handler/DeviceDbGet.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def msg_received(self, protocol, msg):
return Msg.UNKNOWN

# Convert the message to a database device entry.
entry = db.DeviceEntry.from_bytes(msg.data)
entry = db.DeviceEntry.from_bytes(msg.data, db=self.db)
LOG.ui("Entry: %s", entry)

# Skip entries w/ a null memory location.
Expand Down
3 changes: 2 additions & 1 deletion insteon_mqtt/handler/ModemDbGet.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def msg_received(self, protocol, msg):
# Create a modem database entry from the message data and
# write it into the database.
entry = db.ModemEntry(msg.addr, msg.group,
msg.db_flags.is_controller, msg.data)
msg.db_flags.is_controller, msg.data,
db=self.db)
self.db.add_entry(entry)
LOG.ui("Entry: %s", entry)

Expand Down
3 changes: 2 additions & 1 deletion insteon_mqtt/handler/ModemLinkComplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ def msg_received(self, protocol, msg):
else:
data = self.modem.link_data(msg.group, is_ctrl)

entry = db.ModemEntry(msg.addr, msg.group, is_ctrl, data)
entry = db.ModemEntry(msg.addr, msg.group, is_ctrl, data,
db=self.modem.db)
self.modem.db.add_entry(entry)

return Msg.FINISHED
Expand Down
Loading

0 comments on commit 0ccf11e

Please sign in to comment.