Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 7 additions & 5 deletions homeassistant/helpers/entity_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,17 @@ def async_update_group(self):

This method must be run in the event loop.
"""
def sorted_ids():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't instantiate a method on each call to this method. Instead just copy paste the method in both places.

You should also be able to do just this: sorted(self.entities, key=lambda x: self.entities.name)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that key extraction ended up a bit redundant. I inlined the short version now.

"""Return our entity ids sorted by entity name."""
return sorted((ent_id for ent_id in self.entities.keys()),
key=lambda x: self.entities[x].name)

if self.group is None and self.group_name is not None:
group = get_component('group')
self.group = yield from group.Group.async_create_group(
self.hass, self.group_name, self.entities.keys(),
user_defined=False
)
self.hass, self.group_name, sorted_ids(), user_defined=False)
elif self.group is not None:
yield from self.group.async_update_tracked_entity_ids(
self.entities.keys())
yield from self.group.async_update_tracked_entity_ids(sorted_ids())

def reset(self):
"""Remove entities and reset the entity component to initial values."""
Expand Down
7 changes: 4 additions & 3 deletions tests/helpers/test_entity_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,14 @@ def test_setting_up_group(self):
assert group.attributes.get('entity_id') == ('test_domain.hello',)

# group extended
component.add_entities([EntityTest(name='hello2')])
component.add_entities([EntityTest(name='goodbye')])

assert len(self.hass.states.entity_ids()) == 3
group = self.hass.states.get('group.everyone')

assert sorted(group.attributes.get('entity_id')) == \
['test_domain.hello', 'test_domain.hello2']
# Sorted order
assert group.attributes.get('entity_id') == \
('test_domain.goodbye', 'test_domain.hello')

def test_polling_only_updates_entities_it_should_poll(self):
"""Test the polling of only updated entities."""
Expand Down