Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load devices from saved devices and device overrides before ALDB #55

Merged
merged 4 commits into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion insteonplm/aldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ def __repr__(self):
attrs = vars(self)
return ', '.join("%s: %r" % item for item in attrs.items())

@property
def saved_devices(self):
"""Return the device info from the saved devices file."""
return self._saved_devices

@property
def overrides(self):
"""Return the device overrides."""
return self._overrides

def add_device_callback(self, callback):
"""Register a callback to be invoked when a new device appears."""
self.log.debug('Added new callback %s ',
Expand All @@ -69,7 +79,8 @@ def add_override(self, addr, key, value):

def add_saved_device_info(self, **kwarg):
"""Register device info from the saved data file."""
addr = kwarg.get('address', None)
addr = kwarg.get('address')
self.log.debug('Found saved device with address %s', addr)
info = {}
if addr is not None:
info['cat'] = kwarg.get('cat', None)
Expand Down Expand Up @@ -106,3 +117,32 @@ def has_override(self, addr):
if self._overrides.get(addr, None) is not None:
override = True
return override

def add_known_devices(self, plm):
"""Add devices from the saved devices or from the device overrides."""
for addr in self._saved_devices:
if not self._devices.get(addr):
saved_device = self._saved_devices.get(Address(addr).hex, {})
cat = saved_device.get('cat')
subcat = saved_device.get('subcat')
product_key = saved_device.get('firmware')
product_key = saved_device.get('product_key', product_key)
if cat and subcat:
self.log.info('Device with id %s added to device list '
'from saved device data.',
addr)
self[addr] = self.create_device_from_category(
plm, addr, cat,subcat, product_key)
for addr in self._overrides:
if not self._devices.get(addr):
device_override = self._overrides.get(Address(addr).hex, {})
cat = device_override.get('cat')
subcat = device_override.get('subcat')
product_key = device_override.get('firmware')
product_key = device_override.get('product_key', product_key)
if cat and subcat:
self.log.info('Device with id %s added to device list '
'from device override data.',
addr)
self[addr] = self.create_device_from_category(
plm, addr, cat,subcat, product_key)
9 changes: 8 additions & 1 deletion insteonplm/plm.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,9 @@ def _setup_devices(self):
saved_device_info = yield from self._load_saved_device_info()
for savedDevice in saved_device_info:
self.devices.add_saved_device_info(**savedDevice)
self.log.debug('Found %d saved devices', len(self._saved_device_info))
self.log.debug('Found %d saved devices', len(self.devices.saved_devices))
self._get_plm_info()
self._add_known_devices()
self._load_all_link_database()

@asyncio.coroutine
Expand Down Expand Up @@ -417,6 +418,11 @@ def _handle_get_plm_info(self, msg):
self._product_key = msg.firmware
self.log.debug('Ending _handle_get_plm_info')

def _add_known_devices(self):
self.log.debug("Adding known devices.")
self.devices.add_known_devices(self)
self.log.debug("Completed adding known devices.")

def _load_all_link_database(self):
"""Load the ALL-Link Database into object."""
self.log.debug("Starting: _load_all_link_database")
Expand Down Expand Up @@ -470,6 +476,7 @@ def _peel_messages_from_buffer(self):

@asyncio.coroutine
def _load_saved_device_info(self):
self.log.debug("Loading saved device info.")
deviceinfo = []
if self._workdir is not None:
try:
Expand Down