Move split_entity_id to helpers

This commit is contained in:
Paulus Schoutsen 2016-01-23 22:49:49 -08:00
parent de08f0afaa
commit bc19ef66bf
10 changed files with 24 additions and 26 deletions

View file

@ -16,8 +16,8 @@ import itertools as it
import logging import logging
import homeassistant.core as ha import homeassistant.core as ha
import homeassistant.util as util
from homeassistant.helpers import extract_entity_ids from homeassistant.helpers import extract_entity_ids
from homeassistant.helpers.entity import split_entity_id
from homeassistant.loader import get_component from homeassistant.loader import get_component
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE) 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() entity_ids = hass.states.entity_ids()
for entity_id in 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) module = get_component(domain)
@ -92,7 +92,7 @@ def setup(hass, config):
# Group entity_ids by domain. groupby requires sorted data. # Group entity_ids by domain. groupby requires sorted data.
by_domain = it.groupby(sorted(entity_ids), 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: for domain, ent_ids in by_domain:
# We want to block for all calls and only return when all calls # We want to block for all calls and only return when all calls

View file

@ -9,8 +9,7 @@ https://home-assistant.io/components/group/
import homeassistant.core as ha import homeassistant.core as ha
from homeassistant.helpers import generate_entity_id from homeassistant.helpers import generate_entity_id
from homeassistant.helpers.event import track_state_change from homeassistant.helpers.event import track_state_change
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity, split_entity_id
import homeassistant.util as util
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, STATE_ON, STATE_OFF, ATTR_ENTITY_ID, STATE_ON, STATE_OFF,
STATE_HOME, STATE_NOT_HOME, STATE_OPEN, STATE_CLOSED, STATE_HOME, STATE_NOT_HOME, STATE_OPEN, STATE_CLOSED,
@ -62,7 +61,7 @@ def expand_entity_ids(hass, entity_ids):
try: try:
# If entity_id points at a group, expand it # 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: if domain == DOMAIN:
found_ids.extend( found_ids.extend(
@ -75,7 +74,7 @@ def expand_entity_ids(hass, entity_ids):
found_ids.append(entity_id) found_ids.append(entity_id)
except AttributeError: 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 pass
return found_ids return found_ids

View file

@ -14,10 +14,9 @@ from homeassistant.core import State, DOMAIN as HA_DOMAIN
from homeassistant.const import ( from homeassistant.const import (
EVENT_STATE_CHANGED, STATE_NOT_HOME, STATE_ON, STATE_OFF, EVENT_STATE_CHANGED, STATE_NOT_HOME, STATE_ON, STATE_OFF,
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, HTTP_BAD_REQUEST) EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, HTTP_BAD_REQUEST)
from homeassistant import util
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from homeassistant.components import recorder, sun from homeassistant.components import recorder, sun
from homeassistant.helpers.entity import split_entity_id
DOMAIN = "logbook" DOMAIN = "logbook"
DEPENDENCIES = ['recorder', 'http'] DEPENDENCIES = ['recorder', 'http']
@ -209,7 +208,7 @@ def humanify(events):
entity_id = event.data.get(ATTR_ENTITY_ID) entity_id = event.data.get(ATTR_ENTITY_ID)
if domain is None and entity_id is not None: if domain is None and entity_id is not None:
try: try:
domain = util.split_entity_id(str(entity_id))[0] domain = split_entity_id(str(entity_id))[0]
except IndexError: except IndexError:
pass pass

View file

@ -13,10 +13,10 @@ from itertools import islice
import threading import threading
from homeassistant.helpers.entity_component import EntityComponent 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.event import track_point_in_utc_time
from homeassistant.helpers.service import call_from_config 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 import homeassistant.util.dt as date_util
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, EVENT_TIME_CHANGED, STATE_ON, SERVICE_TURN_ON, ATTR_ENTITY_ID, EVENT_TIME_CHANGED, STATE_ON, SERVICE_TURN_ON,

View file

@ -25,7 +25,7 @@ from homeassistant.exceptions import (
import homeassistant.util as util import homeassistant.util as util
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
import homeassistant.util.location as location 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 import homeassistant.helpers.temperature as temp_helper
from homeassistant.config import get_default_config_dir from homeassistant.config import get_default_config_dir
@ -357,12 +357,12 @@ class State(object):
@property @property
def domain(self): def domain(self):
"""Domain of this state.""" """Domain of this state."""
return util.split_entity_id(self.entity_id)[0] return split_entity_id(self.entity_id)[0]
@property @property
def object_id(self): def object_id(self):
"""Object id of this state.""" """Object id of this state."""
return util.split_entity_id(self.entity_id)[1] return split_entity_id(self.entity_id)[1]
@property @property
def name(self): def name(self):

View file

@ -22,6 +22,11 @@ _OVERWRITE = defaultdict(dict)
ENTITY_ID_PATTERN = re.compile(r"^(\w+)\.(\w+)$") 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): def valid_entity_id(entity_id):
"""Test if an entity ID is a valid format.""" """Test if an entity ID is a valid format."""
return ENTITY_ID_PATTERN.match(entity_id) is not None return ENTITY_ID_PATTERN.match(entity_id) is not None

View file

@ -1,8 +1,8 @@
"""Service calling related helpers.""" """Service calling related helpers."""
import logging import logging
from homeassistant.util import split_entity_id
from homeassistant.const import ATTR_ENTITY_ID from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.helpers.entity import split_entity_id
CONF_SERVICE = 'service' CONF_SERVICE = 'service'
CONF_SERVICE_ENTITY_ID = 'entity_id' CONF_SERVICE_ENTITY_ID = 'entity_id'

View file

@ -41,11 +41,6 @@ def slugify(text):
return RE_SLUGIFY.sub("", 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): def repr_helper(inp):
""" Helps creating a more readable string representation of objects. """ """ Helps creating a more readable string representation of objects. """
if isinstance(inp, dict): if isinstance(inp, dict):

View file

@ -42,3 +42,8 @@ class TestHelpersEntity(unittest.TestCase):
state = self.hass.states.get(self.entity.entity_id) state = self.hass.states.get(self.entity.entity_id)
self.assertTrue(state.attributes.get(ATTR_HIDDEN)) 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'))

View file

@ -36,11 +36,6 @@ class TestUtil(unittest.TestCase):
self.assertEqual("test_more", util.slugify("Test More")) self.assertEqual("test_more", util.slugify("Test More"))
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): def test_repr_helper(self):
""" Test repr_helper. """ """ Test repr_helper. """
self.assertEqual("A", util.repr_helper("A")) self.assertEqual("A", util.repr_helper("A"))