From f879ac0993f0650ce89dbc513362a2663c0ee528 Mon Sep 17 00:00:00 2001 From: cdce8p <30130371+cdce8p@users.noreply.github.com> Date: Thu, 27 Sep 2018 23:14:09 +0200 Subject: [PATCH] Remove service helper (6) (#16920) * Update automation * Update group * Async_create_task --- .../components/automation/__init__.py | 15 ----- homeassistant/components/config/automation.py | 11 ++-- .../components/device_tracker/__init__.py | 22 ++++--- homeassistant/components/group/__init__.py | 57 ------------------ homeassistant/helpers/entity_component.py | 15 +++-- tests/components/group/common.py | 60 ++++++++++++++++++- tests/components/group/test_init.py | 12 ++-- 7 files changed, 95 insertions(+), 97 deletions(-) diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index e657409ea01..b34a6b7cf40 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -115,21 +115,6 @@ def is_on(hass, entity_id): return hass.states.is_state(entity_id, STATE_ON) -@bind_hass -def reload(hass): - """Reload the automation from config.""" - hass.services.call(DOMAIN, SERVICE_RELOAD) - - -@bind_hass -def async_reload(hass): - """Reload the automation from config. - - Returns a coroutine object. - """ - return hass.services.async_call(DOMAIN, SERVICE_RELOAD) - - async def async_setup(hass, config): """Set up the automation.""" component = EntityComponent(_LOGGER, DOMAIN, hass, diff --git a/homeassistant/components/config/automation.py b/homeassistant/components/config/automation.py index 223159eb415..2d13ac07025 100644 --- a/homeassistant/components/config/automation.py +++ b/homeassistant/components/config/automation.py @@ -3,10 +3,9 @@ import asyncio from collections import OrderedDict import uuid -from homeassistant.const import CONF_ID from homeassistant.components.config import EditIdBasedConfigView -from homeassistant.components.automation import ( - PLATFORM_SCHEMA, DOMAIN, async_reload) +from homeassistant.const import CONF_ID, SERVICE_RELOAD +from homeassistant.components.automation import DOMAIN, PLATFORM_SCHEMA import homeassistant.helpers.config_validation as cv @@ -16,9 +15,13 @@ CONFIG_PATH = 'automations.yaml' @asyncio.coroutine def async_setup(hass): """Set up the Automation config API.""" + async def hook(hass): + """post_write_hook for Config View that reloads automations.""" + await hass.services.async_call(DOMAIN, SERVICE_RELOAD) + hass.http.register_view(EditAutomationConfigView( DOMAIN, 'config', CONFIG_PATH, cv.string, - PLATFORM_SCHEMA, post_write_hook=async_reload + PLATFORM_SCHEMA, post_write_hook=hook )) return True diff --git a/homeassistant/components/device_tracker/__init__.py b/homeassistant/components/device_tracker/__init__.py index 408672a974f..af1bb1cd9b5 100644 --- a/homeassistant/components/device_tracker/__init__.py +++ b/homeassistant/components/device_tracker/__init__.py @@ -15,6 +15,7 @@ from homeassistant.setup import async_prepare_setup_platform from homeassistant.core import callback from homeassistant.loader import bind_hass from homeassistant.components import group, zone +from homeassistant.components.group import DOMAIN as DOMAIN_GROUP, SERVICE_SET from homeassistant.components.zone.zone import async_active_zone from homeassistant.config import load_yaml_config_file, async_log_exception from homeassistant.exceptions import HomeAssistantError @@ -319,9 +320,13 @@ class DeviceTracker: # During init, we ignore the group if self.group and self.track_new: - self.group.async_set_group( - util.slugify(GROUP_NAME_ALL_DEVICES), visible=False, - name=GROUP_NAME_ALL_DEVICES, add=[device.entity_id]) + self.hass.async_create_task( + self.hass.async_call( + DOMAIN_GROUP, SERVICE_SET, dict( + object_id=util.slugify(GROUP_NAME_ALL_DEVICES), + visible=False, + name=GROUP_NAME_ALL_DEVICES, + add=[device.entity_id]))) self.hass.bus.async_fire(EVENT_NEW_DEVICE, { ATTR_ENTITY_ID: device.entity_id, @@ -354,10 +359,13 @@ class DeviceTracker: entity_ids = [dev.entity_id for dev in self.devices.values() if dev.track] - self.group = self.hass.components.group - self.group.async_set_group( - util.slugify(GROUP_NAME_ALL_DEVICES), visible=False, - name=GROUP_NAME_ALL_DEVICES, entity_ids=entity_ids) + self.hass.async_create_task( + self.hass.services.async_call( + DOMAIN_GROUP, SERVICE_SET, dict( + object_id=util.slugify(GROUP_NAME_ALL_DEVICES), + visible=False, + name=GROUP_NAME_ALL_DEVICES, + entities=entity_ids))) @callback def async_update_stale(self, now: dt_util.dt.datetime): diff --git a/homeassistant/components/group/__init__.py b/homeassistant/components/group/__init__.py index 7b5a75dc13f..39fd7567c98 100644 --- a/homeassistant/components/group/__init__.py +++ b/homeassistant/components/group/__init__.py @@ -120,63 +120,6 @@ def is_on(hass, entity_id): return False -@bind_hass -def reload(hass): - """Reload the automation from config.""" - hass.add_job(async_reload, hass) - - -@callback -@bind_hass -def async_reload(hass): - """Reload the automation from config.""" - hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_RELOAD)) - - -@bind_hass -def set_group(hass, object_id, name=None, entity_ids=None, visible=None, - icon=None, view=None, control=None, add=None): - """Create/Update a group.""" - hass.add_job( - async_set_group, hass, object_id, name, entity_ids, visible, icon, - view, control, add) - - -@callback -@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/Update a group.""" - data = { - key: value for key, value in [ - (ATTR_OBJECT_ID, object_id), - (ATTR_NAME, name), - (ATTR_ENTITIES, entity_ids), - (ATTR_VISIBLE, visible), - (ATTR_ICON, icon), - (ATTR_VIEW, view), - (ATTR_CONTROL, control), - (ATTR_ADD_ENTITIES, add), - ] if value is not None - } - - hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_SET, data)) - - -@bind_hass -def remove(hass, name): - """Remove a user group.""" - hass.add_job(async_remove, hass, name) - - -@callback -@bind_hass -def async_remove(hass, object_id): - """Remove a user group.""" - data = {ATTR_OBJECT_ID: object_id} - hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_REMOVE, data)) - - @bind_hass def expand_entity_ids(hass, entity_ids): """Return entity_ids with group entity ids replaced by their members. diff --git a/homeassistant/helpers/entity_component.py b/homeassistant/helpers/entity_component.py index 09f8838b160..c2ab8722c97 100644 --- a/homeassistant/helpers/entity_component.py +++ b/homeassistant/helpers/entity_component.py @@ -190,10 +190,13 @@ class EntityComponent: sorted(self.entities, key=lambda entity: entity.name or entity.entity_id)] - self.hass.components.group.async_set_group( - slugify(self.group_name), name=self.group_name, - visible=False, entity_ids=ids - ) + self.hass.async_create_task( + self.hass.services.async_call( + 'group', 'set', dict( + object_id=slugify(self.group_name), + name=self.group_name, + visible=False, + entities=ids))) async def _async_reset(self): """Remove entities and reset the entity component to initial values. @@ -212,7 +215,9 @@ class EntityComponent: self.config = None if self.group_name is not None: - self.hass.components.group.async_remove(slugify(self.group_name)) + await self.hass.services.async_call( + 'group', 'remove', dict( + object_id=slugify(self.group_name))) async def async_remove_entity(self, entity_id): """Remove an entity managed by one of the platforms.""" diff --git a/tests/components/group/common.py b/tests/components/group/common.py index 1a59eb265e0..380586e3854 100644 --- a/tests/components/group/common.py +++ b/tests/components/group/common.py @@ -3,12 +3,66 @@ All containing methods are legacy helpers that should not be used by new components. Instead call the service directly. """ -from homeassistant.components.group import ATTR_VISIBLE, DOMAIN, \ - SERVICE_SET_VISIBILITY -from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.components.group import ( + ATTR_ADD_ENTITIES, ATTR_CONTROL, ATTR_ENTITIES, ATTR_OBJECT_ID, ATTR_VIEW, + ATTR_VISIBLE, DOMAIN, SERVICE_REMOVE, SERVICE_SET, SERVICE_SET_VISIBILITY) +from homeassistant.const import ( + ATTR_ENTITY_ID, ATTR_ICON, ATTR_NAME, SERVICE_RELOAD) +from homeassistant.core import callback from homeassistant.loader import bind_hass +@bind_hass +def reload(hass): + """Reload the automation from config.""" + hass.add_job(async_reload, hass) + + +@callback +@bind_hass +def async_reload(hass): + """Reload the automation from config.""" + hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_RELOAD)) + + +@bind_hass +def set_group(hass, object_id, name=None, entity_ids=None, visible=None, + icon=None, view=None, control=None, add=None): + """Create/Update a group.""" + hass.add_job( + async_set_group, hass, object_id, name, entity_ids, visible, icon, + view, control, add) + + +@callback +@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/Update a group.""" + data = { + key: value for key, value in [ + (ATTR_OBJECT_ID, object_id), + (ATTR_NAME, name), + (ATTR_ENTITIES, entity_ids), + (ATTR_VISIBLE, visible), + (ATTR_ICON, icon), + (ATTR_VIEW, view), + (ATTR_CONTROL, control), + (ATTR_ADD_ENTITIES, add), + ] if value is not None + } + + hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_SET, data)) + + +@callback +@bind_hass +def async_remove(hass, object_id): + """Remove a user group.""" + data = {ATTR_OBJECT_ID: object_id} + hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_REMOVE, data)) + + @bind_hass def set_visibility(hass, entity_id=None, visible=True): """Hide or shows a group.""" diff --git a/tests/components/group/test_init.py b/tests/components/group/test_init.py index 619ce86583a..55c8a7778cb 100644 --- a/tests/components/group/test_init.py +++ b/tests/components/group/test_init.py @@ -368,7 +368,7 @@ class TestComponentsGroup(unittest.TestCase): }}}): with patch('homeassistant.config.find_config_file', return_value=''): - group.reload(self.hass) + common.reload(self.hass) self.hass.block_till_done() assert sorted(self.hass.states.entity_ids()) == \ @@ -409,7 +409,7 @@ class TestComponentsGroup(unittest.TestCase): # 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") + common.set_group(self.hass, 'modify_group', icon="mdi:play") self.hass.block_till_done() group_state = self.hass.states.get( @@ -442,7 +442,7 @@ def test_service_group_set_group_remove_group(hass): 'group': {} }) - group.async_set_group(hass, 'user_test_group', name="Test") + common.async_set_group(hass, 'user_test_group', name="Test") yield from hass.async_block_till_done() group_state = hass.states.get('group.user_test_group') @@ -450,7 +450,7 @@ def test_service_group_set_group_remove_group(hass): assert group_state.attributes[group.ATTR_AUTO] assert group_state.attributes['friendly_name'] == "Test" - group.async_set_group( + common.async_set_group( hass, 'user_test_group', view=True, visible=False, entity_ids=['test.entity_bla1']) yield from hass.async_block_till_done() @@ -463,7 +463,7 @@ def test_service_group_set_group_remove_group(hass): assert group_state.attributes['friendly_name'] == "Test" assert list(group_state.attributes['entity_id']) == ['test.entity_bla1'] - group.async_set_group( + common.async_set_group( hass, 'user_test_group', icon="mdi:camera", name="Test2", control="hidden", add=['test.entity_id2']) yield from hass.async_block_till_done() @@ -479,7 +479,7 @@ def test_service_group_set_group_remove_group(hass): assert sorted(list(group_state.attributes['entity_id'])) == sorted([ 'test.entity_bla1', 'test.entity_id2']) - group.async_remove(hass, 'user_test_group') + common.async_remove(hass, 'user_test_group') yield from hass.async_block_till_done() group_state = hass.states.get('group.user_test_group')