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

Enable Adding Multiple Modem Controlled Scenes at Once #326

Merged
merged 2 commits into from
Jan 13, 2021
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
12 changes: 10 additions & 2 deletions insteon_mqtt/Scenes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
11 changes: 6 additions & 5 deletions insteon_mqtt/db/Modem.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,22 +200,23 @@ 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
needed for simulated scenes - the modem must be a controller of a
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):
Expand Down
19 changes: 19 additions & 0 deletions tests/test_Scenes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down