diff --git a/homeassistant/components/__init__.py b/homeassistant/components/__init__.py index cfc8acb133e..3c38e908b16 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 -import homeassistant.util as util from homeassistant.helpers import extract_entity_ids +from homeassistant.helpers.entity import split_entity_id from homeassistant.loader import get_component from homeassistant.const import ( ATTR_ENTITY_ID, SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE) @@ -36,7 +36,7 @@ def is_on(hass, entity_id=None): entity_ids = hass.states.entity_ids() for entity_id in entity_ids: - domain = util.split_entity_id(entity_id)[0] + domain = split_entity_id(entity_id)[0] module = get_component(domain) @@ -92,7 +92,7 @@ def setup(hass, config): # Group entity_ids by domain. groupby requires sorted data. by_domain = it.groupby(sorted(entity_ids), - lambda item: util.split_entity_id(item)[0]) + lambda item: split_entity_id(item)[0]) for domain, ent_ids in by_domain: # We want to block for all calls and only return when all calls diff --git a/homeassistant/components/group.py b/homeassistant/components/group.py index 52ffe824e42..78729d1d3ba 100644 --- a/homeassistant/components/group.py +++ b/homeassistant/components/group.py @@ -9,8 +9,7 @@ https://home-assistant.io/components/group/ import homeassistant.core as ha from homeassistant.helpers import generate_entity_id from homeassistant.helpers.event import track_state_change -from homeassistant.helpers.entity import Entity -import homeassistant.util as util +from homeassistant.helpers.entity import Entity, split_entity_id from homeassistant.const import ( ATTR_ENTITY_ID, STATE_ON, STATE_OFF, STATE_HOME, STATE_NOT_HOME, STATE_OPEN, STATE_CLOSED, @@ -62,7 +61,7 @@ def expand_entity_ids(hass, entity_ids): try: # If entity_id points at a group, expand it - domain, _ = util.split_entity_id(entity_id) + domain, _ = split_entity_id(entity_id) if domain == DOMAIN: found_ids.extend( @@ -75,7 +74,7 @@ def expand_entity_ids(hass, entity_ids): found_ids.append(entity_id) except AttributeError: - # Raised by util.split_entity_id if entity_id is not a string + # Raised by split_entity_id if entity_id is not a string pass return found_ids diff --git a/homeassistant/components/logbook.py b/homeassistant/components/logbook.py index 16159404dec..98d02af5eb0 100644 --- a/homeassistant/components/logbook.py +++ b/homeassistant/components/logbook.py @@ -14,10 +14,9 @@ from homeassistant.core import State, DOMAIN as HA_DOMAIN from homeassistant.const import ( EVENT_STATE_CHANGED, STATE_NOT_HOME, STATE_ON, STATE_OFF, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, HTTP_BAD_REQUEST) -from homeassistant import util import homeassistant.util.dt as dt_util from homeassistant.components import recorder, sun - +from homeassistant.helpers.entity import split_entity_id DOMAIN = "logbook" DEPENDENCIES = ['recorder', 'http'] @@ -209,7 +208,7 @@ def humanify(events): entity_id = event.data.get(ATTR_ENTITY_ID) if domain is None and entity_id is not None: try: - domain = util.split_entity_id(str(entity_id))[0] + domain = split_entity_id(str(entity_id))[0] except IndexError: pass diff --git a/homeassistant/components/script.py b/homeassistant/components/script.py index 52dfdc49281..167ddb55957 100644 --- a/homeassistant/components/script.py +++ b/homeassistant/components/script.py @@ -13,10 +13,10 @@ from itertools import islice import threading from homeassistant.helpers.entity_component import EntityComponent -from homeassistant.helpers.entity import ToggleEntity +from homeassistant.helpers.entity import ToggleEntity, split_entity_id from homeassistant.helpers.event import track_point_in_utc_time from homeassistant.helpers.service import call_from_config -from homeassistant.util import slugify, split_entity_id +from homeassistant.util import slugify import homeassistant.util.dt as date_util from homeassistant.const import ( ATTR_ENTITY_ID, EVENT_TIME_CHANGED, STATE_ON, SERVICE_TURN_ON, diff --git a/homeassistant/core.py b/homeassistant/core.py index 79fc0cfeb36..853d09020ce 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -25,7 +25,7 @@ from homeassistant.exceptions import ( import homeassistant.util as util import homeassistant.util.dt as dt_util import homeassistant.util.location as location -from homeassistant.helpers.entity import valid_entity_id +from homeassistant.helpers.entity import valid_entity_id, split_entity_id import homeassistant.helpers.temperature as temp_helper from homeassistant.config import get_default_config_dir @@ -357,12 +357,12 @@ class State(object): @property def domain(self): """Domain of this state.""" - return util.split_entity_id(self.entity_id)[0] + return split_entity_id(self.entity_id)[0] @property def object_id(self): """Object id of this state.""" - return util.split_entity_id(self.entity_id)[1] + return split_entity_id(self.entity_id)[1] @property def name(self): diff --git a/homeassistant/helpers/entity.py b/homeassistant/helpers/entity.py index e700a316667..a4f964f40b9 100644 --- a/homeassistant/helpers/entity.py +++ b/homeassistant/helpers/entity.py @@ -22,6 +22,11 @@ _OVERWRITE = defaultdict(dict) ENTITY_ID_PATTERN = re.compile(r"^(\w+)\.(\w+)$") +def split_entity_id(entity_id): + """ Splits a state entity_id into domain, object_id. """ + return entity_id.split(".", 1) + + def valid_entity_id(entity_id): """Test if an entity ID is a valid format.""" return ENTITY_ID_PATTERN.match(entity_id) is not None diff --git a/homeassistant/helpers/service.py b/homeassistant/helpers/service.py index 15cfe9b97c6..941227d79cd 100644 --- a/homeassistant/helpers/service.py +++ b/homeassistant/helpers/service.py @@ -1,8 +1,8 @@ """Service calling related helpers.""" import logging -from homeassistant.util import split_entity_id from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.helpers.entity import split_entity_id CONF_SERVICE = 'service' CONF_SERVICE_ENTITY_ID = 'entity_id' diff --git a/homeassistant/util/__init__.py b/homeassistant/util/__init__.py index ada6d150188..b2b4b037384 100644 --- a/homeassistant/util/__init__.py +++ b/homeassistant/util/__init__.py @@ -41,11 +41,6 @@ def slugify(text): return RE_SLUGIFY.sub("", text) -def split_entity_id(entity_id): - """ Splits a state entity_id into domain, object_id. """ - return entity_id.split(".", 1) - - def repr_helper(inp): """ Helps creating a more readable string representation of objects. """ if isinstance(inp, dict): diff --git a/tests/helpers/test_entity.py b/tests/helpers/test_entity.py index cdca36a9701..8845bb622dc 100644 --- a/tests/helpers/test_entity.py +++ b/tests/helpers/test_entity.py @@ -42,3 +42,8 @@ class TestHelpersEntity(unittest.TestCase): state = self.hass.states.get(self.entity.entity_id) self.assertTrue(state.attributes.get(ATTR_HIDDEN)) + + def test_split_entity_id(self): + """ Test split_entity_id. """ + self.assertEqual(['domain', 'object_id'], + entity.split_entity_id('domain.object_id')) diff --git a/tests/util/test_init.py b/tests/util/test_init.py index 2e520ac4980..f0a3eb8a109 100644 --- a/tests/util/test_init.py +++ b/tests/util/test_init.py @@ -36,11 +36,6 @@ class TestUtil(unittest.TestCase): self.assertEqual("test_more", util.slugify("Test More")) self.assertEqual("test_more", util.slugify("Test_(More)")) - def test_split_entity_id(self): - """ Test split_entity_id. """ - self.assertEqual(['domain', 'object_id'], - util.split_entity_id('domain.object_id')) - def test_repr_helper(self): """ Test repr_helper. """ self.assertEqual("A", util.repr_helper("A"))