From a089b9c09560f4af1838a32c01592ddba7408684 Mon Sep 17 00:00:00 2001 From: KRKeegan Date: Tue, 12 Jan 2021 16:46:01 -0800 Subject: [PATCH 1/2] Enable Adding Multiple Modem Controlled Scenes at Once Fixes #321 --- insteon_mqtt/Scenes.py | 12 ++++++++++-- insteon_mqtt/db/Modem.py | 11 ++++++----- tests/test_Scenes.py | 19 +++++++++++++++++++ 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/insteon_mqtt/Scenes.py b/insteon_mqtt/Scenes.py index 6383b98a..ec99eaa7 100644 --- a/insteon_mqtt/Scenes.py +++ b/insteon_mqtt/Scenes.py @@ -379,6 +379,8 @@ def _assign_modem_group(self): Args: config (object): Configuration object. """ + # Get a list of the empty groups + empty_groups = self.modem.db.empty_groups() updated = False for scene in self.entries: for controller in scene.controllers: @@ -387,8 +389,14 @@ def _assign_modem_group(self): controller.group <= 0x01): updated = True - # Get and set the next available group id - controller.group = self.modem.db.next_group() + if len(empty_groups) >= 1: + # Get and set the next available group id + controller.group = empty_groups.pop(0) + else: + LOG.critical("Unable to add the modem as a controller " + "of a new scene. All 255 groups have " + "been used on your modem.") + return # All done save the config file if necessary if updated: diff --git a/insteon_mqtt/db/Modem.py b/insteon_mqtt/db/Modem.py index 2452c696..2168150f 100644 --- a/insteon_mqtt/db/Modem.py +++ b/insteon_mqtt/db/Modem.py @@ -200,8 +200,8 @@ def __len__(self): return len(self.entries) #----------------------------------------------------------------------- - def next_group(self): - """Find the next free internal PLM group number that is available. + def empty_groups(self): + """Get a list of the unused internal PLM group numbers This is used to find an available group number for creating a virtual modem scene. The modem supports 1-255 for groups. Groups 1-8 are @@ -209,13 +209,14 @@ def next_group(self): device for the device button group to send it a simulated scene. Returns: - (int) Returns the next group number of None if there are none. + (array) Returns a list of the empty groups """ + ret = [] for i in range(GROUP_START, 255): if i not in self.groups: - return i + ret.append(i) - return None + return ret #----------------------------------------------------------------------- def delete_entry(self, entry): diff --git a/tests/test_Scenes.py b/tests/test_Scenes.py index 6a0e7ab2..2984efe0 100644 --- a/tests/test_Scenes.py +++ b/tests/test_Scenes.py @@ -155,6 +155,25 @@ def test_assign_modem_group(self): # 20 is the current lowest allowed group number assert scenes.data[0]['controllers'][0]['modem'] == 20 + def test_assign_modem_group_mulitple(self): + modem = MockModem() + scenes = Scenes.SceneManager(modem, None) + scenes.data = [{'controllers': ['ff.ff.ff'], + 'responders': ['cc.bb.22'], + 'name': 'test'}, + {'controllers': ['ff.ff.ff'], + 'responders': ['cc.bb.23'], + 'name': 'test2'}, + {'controllers': ['ff.ff.ff'], + 'responders': ['cc.bb.24'], + 'name': 'test3'}] + scenes._init_scene_entries() + scenes._assign_modem_group() + # 20 is the current lowest allowed group number + assert scenes.data[0]['controllers'][0]['modem'] == 20 + assert scenes.data[1]['controllers'][0]['modem'] == 21 + assert scenes.data[2]['controllers'][0]['modem'] == 22 + def test_bad_config(self): modem = MockModem() scenes = Scenes.SceneManager(modem, None) From 3c4670b455f4f6d54bcb3a7ba8f3f0ca2a4d5366 Mon Sep 17 00:00:00 2001 From: KRKeegan Date: Tue, 12 Jan 2021 16:49:15 -0800 Subject: [PATCH 2/2] Update Changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f6a33be..2ca374aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,9 @@ - Fixed error where 'device' reason was not set by default in scene command. ([PR 308][P308]) +- Fix bug where multiple new Modem controller entries would end up with the + same group number. ([PR 326][P326]) + ## [0.7.5] This is another significant update that both improves the user experience, @@ -582,3 +585,4 @@ will add new features. [P308]: https://github.com/TD22057/insteon-mqtt/pull/308 [P313]: https://github.com/TD22057/insteon-mqtt/pull/313 [P213]: https://github.com/TD22057/insteon-mqtt/pull/213 +[P326]: https://github.com/TD22057/insteon-mqtt/pull/326