diff --git a/homeassistant/components/__init__.py b/homeassistant/components/__init__.py index 3c38e908b16..0d82e1d2882 100644 --- a/homeassistant/components/__init__.py +++ b/homeassistant/components/__init__.py @@ -16,8 +16,8 @@ import itertools as it import logging import homeassistant.core as ha -from homeassistant.helpers import extract_entity_ids from homeassistant.helpers.entity import split_entity_id +from homeassistant.helpers.service import extract_entity_ids from homeassistant.loader import get_component from homeassistant.const import ( ATTR_ENTITY_ID, SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE) diff --git a/homeassistant/helpers/__init__.py b/homeassistant/helpers/__init__.py index 95dfe7dd65e..ab5f3df6563 100644 --- a/homeassistant/helpers/__init__.py +++ b/homeassistant/helpers/__init__.py @@ -3,7 +3,6 @@ Helper methods for components within Home Assistant. """ import re -from homeassistant.loader import get_component from homeassistant.const import ( ATTR_ENTITY_ID, CONF_PLATFORM, DEVICE_DEFAULT_NAME) from homeassistant.util import ensure_unique_string, slugify @@ -22,25 +21,6 @@ def generate_entity_id(entity_id_format, name, current_ids=None, hass=None): entity_id_format.format(slugify(name.lower())), current_ids) -def extract_entity_ids(hass, service): - """ - Helper method to extract a list of entity ids from a service call. - Will convert group entity ids to the entity ids it represents. - """ - if not (service.data and ATTR_ENTITY_ID in service.data): - return [] - - group = get_component('group') - - # Entity ID attr can be a list or a string - service_ent_id = service.data[ATTR_ENTITY_ID] - - if isinstance(service_ent_id, str): - return group.expand_entity_ids(hass, [service_ent_id]) - - return [ent_id for ent_id in group.expand_entity_ids(hass, service_ent_id)] - - def validate_config(config, items, logger): """ Validates if all items are available in the configuration. diff --git a/homeassistant/helpers/entity_component.py b/homeassistant/helpers/entity_component.py index 4cf44737f90..a6b2ab4c886 100644 --- a/homeassistant/helpers/entity_component.py +++ b/homeassistant/helpers/entity_component.py @@ -7,9 +7,9 @@ Provides helpers for components that manage entities. from threading import Lock from homeassistant.bootstrap import prepare_setup_platform -from homeassistant.helpers import ( - generate_entity_id, config_per_platform, extract_entity_ids) +from homeassistant.helpers import generate_entity_id, config_per_platform from homeassistant.helpers.event import track_utc_time_change +from homeassistant.helpers.service import extract_entity_ids from homeassistant.components import group, discovery from homeassistant.const import ATTR_ENTITY_ID diff --git a/homeassistant/helpers/service.py b/homeassistant/helpers/service.py index 941227d79cd..a1ba45a491f 100644 --- a/homeassistant/helpers/service.py +++ b/homeassistant/helpers/service.py @@ -3,6 +3,7 @@ import logging from homeassistant.const import ATTR_ENTITY_ID from homeassistant.helpers.entity import split_entity_id +from homeassistant.loader import get_component CONF_SERVICE = 'service' CONF_SERVICE_ENTITY_ID = 'entity_id' @@ -41,3 +42,22 @@ def call_from_config(hass, config, blocking=False): service_data[ATTR_ENTITY_ID] = entity_id hass.services.call(domain, service, service_data, blocking) + + +def extract_entity_ids(hass, service): + """ + Helper method to extract a list of entity ids from a service call. + Will convert group entity ids to the entity ids it represents. + """ + if not (service.data and ATTR_ENTITY_ID in service.data): + return [] + + group = get_component('group') + + # Entity ID attr can be a list or a string + service_ent_id = service.data[ATTR_ENTITY_ID] + + if isinstance(service_ent_id, str): + return group.expand_entity_ids(hass, [service_ent_id]) + + return [ent_id for ent_id in group.expand_entity_ids(hass, service_ent_id)] diff --git a/tests/helpers/test_init.py b/tests/helpers/test_init.py index 5899ef3a943..9d8afeb2b6d 100644 --- a/tests/helpers/test_init.py +++ b/tests/helpers/test_init.py @@ -7,45 +7,22 @@ Tests component helpers. # pylint: disable=protected-access,too-many-public-methods import unittest -import homeassistant.core as ha -from homeassistant import loader, helpers -from homeassistant.const import STATE_ON, STATE_OFF, ATTR_ENTITY_ID +from homeassistant import helpers from tests.common import get_test_home_assistant -class TestComponentsCore(unittest.TestCase): - """ Tests homeassistant.components module. """ +class TestHelpers(unittest.TestCase): + """ Tests homeassistant.helpers module. """ def setUp(self): # pylint: disable=invalid-name """ Init needed objects. """ self.hass = get_test_home_assistant() - self.hass.states.set('light.Bowl', STATE_ON) - self.hass.states.set('light.Ceiling', STATE_OFF) - self.hass.states.set('light.Kitchen', STATE_OFF) - - loader.get_component('group').setup_group( - self.hass, 'test', ['light.Ceiling', 'light.Kitchen']) - def tearDown(self): # pylint: disable=invalid-name """ Stop down stuff we started. """ self.hass.stop() - def test_extract_entity_ids(self): - """ Test extract_entity_ids method. """ - call = ha.ServiceCall('light', 'turn_on', - {ATTR_ENTITY_ID: 'light.Bowl'}) - - self.assertEqual(['light.bowl'], - helpers.extract_entity_ids(self.hass, call)) - - call = ha.ServiceCall('light', 'turn_on', - {ATTR_ENTITY_ID: 'group.test'}) - - self.assertEqual(['light.ceiling', 'light.kitchen'], - helpers.extract_entity_ids(self.hass, call)) - def test_extract_domain_configs(self): config = { 'zone': None, diff --git a/tests/helpers/test_service.py b/tests/helpers/test_service.py index aa2cab07d0d..659ab6e3ace 100644 --- a/tests/helpers/test_service.py +++ b/tests/helpers/test_service.py @@ -7,7 +7,8 @@ Test service helpers. import unittest from unittest.mock import patch -from homeassistant.const import SERVICE_TURN_ON +from homeassistant import core as ha, loader +from homeassistant.const import STATE_ON, STATE_OFF, ATTR_ENTITY_ID from homeassistant.helpers import service from tests.common import get_test_home_assistant, mock_service @@ -66,3 +67,24 @@ class TestServiceHelpers(unittest.TestCase): 'service': 'invalid' }) self.assertEqual(3, mock_log.call_count) + + def test_extract_entity_ids(self): + """ Test extract_entity_ids method. """ + self.hass.states.set('light.Bowl', STATE_ON) + self.hass.states.set('light.Ceiling', STATE_OFF) + self.hass.states.set('light.Kitchen', STATE_OFF) + + loader.get_component('group').setup_group( + self.hass, 'test', ['light.Ceiling', 'light.Kitchen']) + + call = ha.ServiceCall('light', 'turn_on', + {ATTR_ENTITY_ID: 'light.Bowl'}) + + self.assertEqual(['light.bowl'], + service.extract_entity_ids(self.hass, call)) + + call = ha.ServiceCall('light', 'turn_on', + {ATTR_ENTITY_ID: 'group.test'}) + + self.assertEqual(['light.ceiling', 'light.kitchen'], + service.extract_entity_ids(self.hass, call))