mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Make Service.call_from_config async
This commit is contained in:
parent
4198c42736
commit
33a51623f8
@ -1,4 +1,5 @@
|
|||||||
"""Service calling related helpers."""
|
"""Service calling related helpers."""
|
||||||
|
import asyncio
|
||||||
import functools
|
import functools
|
||||||
import logging
|
import logging
|
||||||
# pylint: disable=unused-import
|
# pylint: disable=unused-import
|
||||||
@ -11,6 +12,7 @@ from homeassistant.core import HomeAssistant # NOQA
|
|||||||
from homeassistant.exceptions import TemplateError
|
from homeassistant.exceptions import TemplateError
|
||||||
from homeassistant.loader import get_component
|
from homeassistant.loader import get_component
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.util.async import run_coroutine_threadsafe
|
||||||
|
|
||||||
HASS = None # type: Optional[HomeAssistant]
|
HASS = None # type: Optional[HomeAssistant]
|
||||||
|
|
||||||
@ -37,6 +39,15 @@ def service(domain, service_name):
|
|||||||
def call_from_config(hass, config, blocking=False, variables=None,
|
def call_from_config(hass, config, blocking=False, variables=None,
|
||||||
validate_config=True):
|
validate_config=True):
|
||||||
"""Call a service based on a config hash."""
|
"""Call a service based on a config hash."""
|
||||||
|
run_coroutine_threadsafe(
|
||||||
|
async_call_from_config(hass, config, blocking, variables,
|
||||||
|
validate_config), hass.loop).result()
|
||||||
|
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def async_call_from_config(hass, config, blocking=False, variables=None,
|
||||||
|
validate_config=True):
|
||||||
|
"""Call a service based on a config hash."""
|
||||||
if validate_config:
|
if validate_config:
|
||||||
try:
|
try:
|
||||||
config = cv.SERVICE_SCHEMA(config)
|
config = cv.SERVICE_SCHEMA(config)
|
||||||
@ -49,7 +60,8 @@ def call_from_config(hass, config, blocking=False, variables=None,
|
|||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
config[CONF_SERVICE_TEMPLATE].hass = hass
|
config[CONF_SERVICE_TEMPLATE].hass = hass
|
||||||
domain_service = config[CONF_SERVICE_TEMPLATE].render(variables)
|
domain_service = config[CONF_SERVICE_TEMPLATE].async_render(
|
||||||
|
variables)
|
||||||
domain_service = cv.service(domain_service)
|
domain_service = cv.service(domain_service)
|
||||||
except TemplateError as ex:
|
except TemplateError as ex:
|
||||||
_LOGGER.error('Error rendering service name template: %s', ex)
|
_LOGGER.error('Error rendering service name template: %s', ex)
|
||||||
@ -71,14 +83,15 @@ def call_from_config(hass, config, blocking=False, variables=None,
|
|||||||
return {key: _data_template_creator(item)
|
return {key: _data_template_creator(item)
|
||||||
for key, item in value.items()}
|
for key, item in value.items()}
|
||||||
value.hass = hass
|
value.hass = hass
|
||||||
return value.render(variables)
|
return value.async_render(variables)
|
||||||
service_data.update(_data_template_creator(
|
service_data.update(_data_template_creator(
|
||||||
config[CONF_SERVICE_DATA_TEMPLATE]))
|
config[CONF_SERVICE_DATA_TEMPLATE]))
|
||||||
|
|
||||||
if CONF_SERVICE_ENTITY_ID in config:
|
if CONF_SERVICE_ENTITY_ID in config:
|
||||||
service_data[ATTR_ENTITY_ID] = config[CONF_SERVICE_ENTITY_ID]
|
service_data[ATTR_ENTITY_ID] = config[CONF_SERVICE_ENTITY_ID]
|
||||||
|
|
||||||
hass.services.call(domain, service_name, service_data, blocking)
|
yield from hass.services.async_call(
|
||||||
|
domain, service_name, service_data, blocking)
|
||||||
|
|
||||||
|
|
||||||
def extract_entity_ids(hass, service_call):
|
def extract_entity_ids(hass, service_call):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user