From 53484e46a31a8b8908aef4094553c211f36490f9 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 23 Jan 2016 23:00:46 -0800 Subject: [PATCH] Move generate_entity_id to entity helpers --- homeassistant/components/configurator.py | 2 +- homeassistant/components/group.py | 4 ++-- homeassistant/components/zone.py | 4 ++-- homeassistant/helpers/__init__.py | 17 +---------------- homeassistant/helpers/entity.py | 14 ++++++++++++++ homeassistant/helpers/entity_component.py | 3 ++- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/configurator.py b/homeassistant/components/configurator.py index 515daffc71c..6fb584635f9 100644 --- a/homeassistant/components/configurator.py +++ b/homeassistant/components/configurator.py @@ -11,7 +11,7 @@ the user has submitted configuration information. """ import logging -from homeassistant.helpers import generate_entity_id +from homeassistant.helpers.entity import generate_entity_id from homeassistant.const import EVENT_TIME_CHANGED DOMAIN = "configurator" diff --git a/homeassistant/components/group.py b/homeassistant/components/group.py index 78729d1d3ba..5d19f313323 100644 --- a/homeassistant/components/group.py +++ b/homeassistant/components/group.py @@ -7,9 +7,9 @@ For more details about this component, please refer to the documentation at 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, split_entity_id +from homeassistant.helpers.entity import ( + Entity, split_entity_id, generate_entity_id) from homeassistant.const import ( ATTR_ENTITY_ID, STATE_ON, STATE_OFF, STATE_HOME, STATE_NOT_HOME, STATE_OPEN, STATE_CLOSED, diff --git a/homeassistant/components/zone.py b/homeassistant/components/zone.py index 86e2cf5062e..3dcc3dc0f07 100644 --- a/homeassistant/components/zone.py +++ b/homeassistant/components/zone.py @@ -10,8 +10,8 @@ import logging from homeassistant.const import ( ATTR_HIDDEN, ATTR_ICON, ATTR_LATITUDE, ATTR_LONGITUDE, CONF_NAME) -from homeassistant.helpers import extract_domain_configs, generate_entity_id -from homeassistant.helpers.entity import Entity +from homeassistant.helpers import extract_domain_configs +from homeassistant.helpers.entity import Entity, generate_entity_id from homeassistant.util.location import distance DOMAIN = "zone" diff --git a/homeassistant/helpers/__init__.py b/homeassistant/helpers/__init__.py index ab5f3df6563..ec7996ff6df 100644 --- a/homeassistant/helpers/__init__.py +++ b/homeassistant/helpers/__init__.py @@ -3,22 +3,7 @@ Helper methods for components within Home Assistant. """ import re -from homeassistant.const import ( - ATTR_ENTITY_ID, CONF_PLATFORM, DEVICE_DEFAULT_NAME) -from homeassistant.util import ensure_unique_string, slugify - - -def generate_entity_id(entity_id_format, name, current_ids=None, hass=None): - """ Generate a unique entity ID based on given entity IDs or used ids. """ - name = name.lower() or DEVICE_DEFAULT_NAME.lower() - if current_ids is None: - if hass is None: - raise RuntimeError("Missing required parameter currentids or hass") - - current_ids = hass.states.entity_ids() - - return ensure_unique_string( - entity_id_format.format(slugify(name.lower())), current_ids) +from homeassistant.const import CONF_PLATFORM def validate_config(config, items, logger): diff --git a/homeassistant/helpers/entity.py b/homeassistant/helpers/entity.py index a4f964f40b9..ab5707a0121 100644 --- a/homeassistant/helpers/entity.py +++ b/homeassistant/helpers/entity.py @@ -9,6 +9,7 @@ from collections import defaultdict import re from homeassistant.exceptions import NoEntitySpecifiedError +from homeassistant.util import ensure_unique_string, slugify from homeassistant.const import ( ATTR_FRIENDLY_NAME, ATTR_HIDDEN, ATTR_UNIT_OF_MEASUREMENT, ATTR_ICON, @@ -22,6 +23,19 @@ _OVERWRITE = defaultdict(dict) ENTITY_ID_PATTERN = re.compile(r"^(\w+)\.(\w+)$") +def generate_entity_id(entity_id_format, name, current_ids=None, hass=None): + """ Generate a unique entity ID based on given entity IDs or used ids. """ + name = name.lower() or DEVICE_DEFAULT_NAME.lower() + if current_ids is None: + if hass is None: + raise RuntimeError("Missing required parameter currentids or hass") + + current_ids = hass.states.entity_ids() + + return ensure_unique_string( + entity_id_format.format(slugify(name.lower())), current_ids) + + def split_entity_id(entity_id): """ Splits a state entity_id into domain, object_id. """ return entity_id.split(".", 1) diff --git a/homeassistant/helpers/entity_component.py b/homeassistant/helpers/entity_component.py index a6b2ab4c886..0450a788809 100644 --- a/homeassistant/helpers/entity_component.py +++ b/homeassistant/helpers/entity_component.py @@ -7,7 +7,8 @@ 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 +from homeassistant.helpers import config_per_platform +from homeassistant.helpers.entity import generate_entity_id from homeassistant.helpers.event import track_utc_time_change from homeassistant.helpers.service import extract_entity_ids from homeassistant.components import group, discovery