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

Tracking all groups to allow changing of existing groups #11444

Merged
merged 3 commits into from Jan 10, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 12 additions & 5 deletions homeassistant/components/group/__init__.py
Expand Up @@ -43,6 +43,8 @@
ATTR_VIEW = 'view'
ATTR_VISIBLE = 'visible'

DATA_ALL_GROUPS = 'data_all_groups'

SERVICE_SET_VISIBILITY = 'set_visibility'
SERVICE_SET = 'set'
SERVICE_REMOVE = 'remove'
Expand Down Expand Up @@ -146,7 +148,7 @@ def set_visibility(hass, entity_id=None, visible=True):
@bind_hass
def set_group(hass, object_id, name=None, entity_ids=None, visible=None,
icon=None, view=None, control=None, add=None):
"""Create a new user group."""
"""Create/Update a group."""
hass.add_job(
async_set_group, hass, object_id, name, entity_ids, visible, icon,
view, control, add)
Expand All @@ -156,7 +158,7 @@ def set_group(hass, object_id, name=None, entity_ids=None, visible=None,
@bind_hass
def async_set_group(hass, object_id, name=None, entity_ids=None, visible=None,
icon=None, view=None, control=None, add=None):
"""Create a new user group."""
"""Create/Update a group."""
data = {
key: value for key, value in [
(ATTR_OBJECT_ID, object_id),
Expand Down Expand Up @@ -250,7 +252,7 @@ def get_entity_ids(hass, entity_id, domain_filter=None):
def async_setup(hass, config):
"""Set up all groups found definded in the configuration."""
component = EntityComponent(_LOGGER, DOMAIN, hass)
service_groups = {}
hass.data[DATA_ALL_GROUPS] = {}

yield from _async_process_config(hass, config, component)

Expand All @@ -275,6 +277,7 @@ def reload_service_handler(service):
def groups_service_handler(service):
"""Handle dynamic group service functions."""
object_id = service.data[ATTR_OBJECT_ID]
service_groups = hass.data[DATA_ALL_GROUPS]

# new group
if service.service == SERVICE_SET and object_id not in service_groups:
Expand All @@ -285,15 +288,14 @@ def groups_service_handler(service):
ATTR_VISIBLE, ATTR_ICON, ATTR_VIEW, ATTR_CONTROL
) if service.data.get(attr) is not None}

new_group = yield from Group.async_create_group(
yield from Group.async_create_group(
hass, service.data.get(ATTR_NAME, object_id),
object_id=object_id,
entity_ids=entity_ids,
user_defined=False,
**extra_arg
)

service_groups[object_id] = new_group
return

# update group
Expand Down Expand Up @@ -456,6 +458,11 @@ def async_create_group(hass, name, entity_ids=None, user_defined=True,
else:
yield from group.async_update_ha_state(True)

# If called before the platform async_setup is called (test cases)
if DATA_ALL_GROUPS not in hass.data:
hass.data[DATA_ALL_GROUPS] = {}

hass.data[DATA_ALL_GROUPS][object_id] = group
return group

@property
Expand Down
25 changes: 24 additions & 1 deletion tests/components/group/test_init.py
Expand Up @@ -8,7 +8,7 @@
from homeassistant.setup import setup_component, async_setup_component
from homeassistant.const import (
STATE_ON, STATE_OFF, STATE_HOME, STATE_UNKNOWN, ATTR_ICON, ATTR_HIDDEN,
ATTR_ASSUMED_STATE, STATE_NOT_HOME)
ATTR_ASSUMED_STATE, STATE_NOT_HOME, ATTR_FRIENDLY_NAME)
import homeassistant.components.group as group

from tests.common import get_test_home_assistant, assert_setup_component
Expand Down Expand Up @@ -395,6 +395,29 @@ def test_changing_group_visibility(self):
group_state = self.hass.states.get(group_entity_id)
self.assertIsNone(group_state.attributes.get(ATTR_HIDDEN))

def test_modify_group(self):
"""Test modifying a group."""
group_conf = OrderedDict()
group_conf['modify_group'] = {
'name': 'friendly_name',
'icon': 'mdi:work'
}

assert setup_component(self.hass, 'group', {'group': group_conf})

# The old way would create a new group modify_group1 because
# internally it didn't know anything about those created in the config
group.set_group(self.hass, 'modify_group', icon="mdi:play")
self.hass.block_till_done()

group_state = self.hass.states.get(
group.ENTITY_ID_FORMAT.format('modify_group'))

assert self.hass.states.entity_ids() == ['group.modify_group']
assert group_state.attributes.get(ATTR_ICON) == 'mdi:play'
assert group_state.attributes.get(ATTR_FRIENDLY_NAME) == \
'friendly_name'


@asyncio.coroutine
def test_service_group_services(hass):
Expand Down