Merge remote-tracking branch 'balloob/dev' into automation-decorator
# Conflicts: # homeassistant/helpers/service.py # tests/helpers/test_service.py
This commit is contained in:
commit
60dd2d441d
28 files changed
+374
-191
No files matched your search
@@ -3,42 +3,7 @@ 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
|
||||
|
||||
|
||||
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 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)]
|
||||
from homeassistant.const import CONF_PLATFORM
|
||||
|
||||
|
||||
def validate_config(config, items, logger):
|
||||
|
||||
@@ -6,8 +6,10 @@ Provides ABC for entities in HA.
|
||||
"""
|
||||
|
||||
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,
|
||||
@@ -17,6 +19,32 @@ from homeassistant.const import (
|
||||
# Dict mapping entity_id to a boolean that overwrites the hidden property
|
||||
_OVERWRITE = defaultdict(dict)
|
||||
|
||||
# Pattern for validating entity IDs (format: <domain>.<entity>)
|
||||
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)
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
class Entity(object):
|
||||
""" ABC for Home Assistant entities. """
|
||||
|
||||
@@ -7,9 +7,10 @@ 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 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
|
||||
from homeassistant.const import ATTR_ENTITY_ID
|
||||
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
import functools
|
||||
import logging
|
||||
|
||||
from homeassistant.util import split_entity_id
|
||||
from homeassistant.const import ATTR_ENTITY_ID
|
||||
from homeassistant.helpers.entity import split_entity_id
|
||||
from homeassistant.loader import get_component
|
||||
|
||||
HASS = None
|
||||
|
||||
@@ -61,3 +62,22 @@ def call_from_config(hass, config, blocking=False):
|
||||
service_data[ATTR_ENTITY_ID] = entity_id
|
||||
|
||||
hass.services.call(domain, service_name, 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)]
|
||||
Reference in new issue
Block a user