Moved service decorator to service helpers
Moved the service decorator to the service helpers module and moved the associated tests.
This commit is contained in:
parent
f66aeb2e73
commit
5830da63b1
5 changed files with 40 additions and 17 deletions
|
@ -31,7 +31,8 @@ import logging
|
||||||
from homeassistant.const import STATE_HOME, STATE_NOT_HOME, STATE_ON, STATE_OFF
|
from homeassistant.const import STATE_HOME, STATE_NOT_HOME, STATE_ON, STATE_OFF
|
||||||
from homeassistant.helpers import validate_config
|
from homeassistant.helpers import validate_config
|
||||||
from homeassistant.helpers.event_decorators import \
|
from homeassistant.helpers.event_decorators import \
|
||||||
track_state_change, track_time_change, service
|
track_state_change, track_time_change
|
||||||
|
from homeassistant.helpers.service import service
|
||||||
import homeassistant.components as core
|
import homeassistant.components as core
|
||||||
from homeassistant.components import device_tracker
|
from homeassistant.components import device_tracker
|
||||||
from homeassistant.components import light
|
from homeassistant.components import light
|
||||||
|
|
|
@ -24,7 +24,7 @@ import homeassistant.config as config_util
|
||||||
import homeassistant.loader as loader
|
import homeassistant.loader as loader
|
||||||
import homeassistant.components as core_components
|
import homeassistant.components as core_components
|
||||||
import homeassistant.components.group as group
|
import homeassistant.components.group as group
|
||||||
from homeassistant.helpers import event_decorators
|
from homeassistant.helpers import event_decorators, service
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
__version__, EVENT_COMPONENT_LOADED, CONF_LATITUDE, CONF_LONGITUDE,
|
__version__, EVENT_COMPONENT_LOADED, CONF_LATITUDE, CONF_LONGITUDE,
|
||||||
|
@ -202,6 +202,7 @@ def from_config_dict(config, hass=None, config_dir=None, enable_log=True,
|
||||||
|
|
||||||
# give event decorators access to HASS
|
# give event decorators access to HASS
|
||||||
event_decorators.HASS = hass
|
event_decorators.HASS = hass
|
||||||
|
service.HASS = hass
|
||||||
|
|
||||||
# Setup the components
|
# Setup the components
|
||||||
for domain in loader.load_order_components(components):
|
for domain in loader.load_order_components(components):
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
"""Service calling related helpers."""
|
"""Service calling related helpers."""
|
||||||
|
import functools
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.util import split_entity_id
|
from homeassistant.util import split_entity_id
|
||||||
from homeassistant.const import ATTR_ENTITY_ID
|
from homeassistant.const import ATTR_ENTITY_ID
|
||||||
|
|
||||||
|
HASS = None
|
||||||
|
|
||||||
CONF_SERVICE = 'service'
|
CONF_SERVICE = 'service'
|
||||||
CONF_SERVICE_ENTITY_ID = 'entity_id'
|
CONF_SERVICE_ENTITY_ID = 'entity_id'
|
||||||
CONF_SERVICE_DATA = 'data'
|
CONF_SERVICE_DATA = 'data'
|
||||||
|
@ -11,6 +14,23 @@ CONF_SERVICE_DATA = 'data'
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def _callback(action, *args, **kwargs):
|
||||||
|
""" adds HASS to callback arguments """
|
||||||
|
action(HASS, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def service(domain, service_name):
|
||||||
|
""" Decorator factory to register a service """
|
||||||
|
|
||||||
|
def register_service_decorator(action):
|
||||||
|
""" Decorator to register a service """
|
||||||
|
HASS.services.register(domain, service_name,
|
||||||
|
functools.partial(_callback, action))
|
||||||
|
return action
|
||||||
|
|
||||||
|
return register_service_decorator
|
||||||
|
|
||||||
|
|
||||||
def call_from_config(hass, config, blocking=False):
|
def call_from_config(hass, config, blocking=False):
|
||||||
"""Call a service based on a config hash."""
|
"""Call a service based on a config hash."""
|
||||||
if not isinstance(config, dict) or CONF_SERVICE not in config:
|
if not isinstance(config, dict) or CONF_SERVICE not in config:
|
||||||
|
@ -18,7 +38,7 @@ def call_from_config(hass, config, blocking=False):
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
domain, service = split_entity_id(config[CONF_SERVICE])
|
domain, service_name = split_entity_id(config[CONF_SERVICE])
|
||||||
except ValueError:
|
except ValueError:
|
||||||
_LOGGER.error('Invalid service specified: %s', config[CONF_SERVICE])
|
_LOGGER.error('Invalid service specified: %s', config[CONF_SERVICE])
|
||||||
return
|
return
|
||||||
|
@ -40,4 +60,4 @@ def call_from_config(hass, config, blocking=False):
|
||||||
elif entity_id is not None:
|
elif entity_id is not None:
|
||||||
service_data[ATTR_ENTITY_ID] = entity_id
|
service_data[ATTR_ENTITY_ID] = entity_id
|
||||||
|
|
||||||
hass.services.call(domain, service, service_data, blocking)
|
hass.services.call(domain, service_name, service_data, blocking)
|
||||||
|
|
|
@ -15,7 +15,7 @@ import homeassistant.core as ha
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
from homeassistant.helpers import event_decorators
|
from homeassistant.helpers import event_decorators
|
||||||
from homeassistant.helpers.event_decorators import (
|
from homeassistant.helpers.event_decorators import (
|
||||||
track_time_change, track_utc_time_change, track_state_change, service,
|
track_time_change, track_utc_time_change, track_state_change,
|
||||||
track_sunrise, track_sunset)
|
track_sunrise, track_sunset)
|
||||||
from homeassistant.components import sun
|
from homeassistant.components import sun
|
||||||
|
|
||||||
|
@ -37,17 +37,6 @@ class TestEventDecoratorHelpers(unittest.TestCase):
|
||||||
""" Stop down stuff we started. """
|
""" Stop down stuff we started. """
|
||||||
self.hass.stop()
|
self.hass.stop()
|
||||||
|
|
||||||
def test_service(self):
|
|
||||||
""" Test service registration decorator. """
|
|
||||||
runs = []
|
|
||||||
|
|
||||||
decor = service('test', 'test')
|
|
||||||
decor(lambda x, y: runs.append(1))
|
|
||||||
|
|
||||||
self.hass.services.call('test', 'test')
|
|
||||||
self.hass.pool.block_till_done()
|
|
||||||
self.assertEqual(1, len(runs))
|
|
||||||
|
|
||||||
def test_track_sunrise(self):
|
def test_track_sunrise(self):
|
||||||
""" Test track sunrise decorator """
|
""" Test track sunrise decorator """
|
||||||
latitude = 32.87336
|
latitude = 32.87336
|
||||||
|
|
|
@ -7,7 +7,6 @@ Test service helpers.
|
||||||
import unittest
|
import unittest
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from homeassistant.const import SERVICE_TURN_ON
|
|
||||||
from homeassistant.helpers import service
|
from homeassistant.helpers import service
|
||||||
|
|
||||||
from tests.common import get_test_home_assistant, mock_service
|
from tests.common import get_test_home_assistant, mock_service
|
||||||
|
@ -23,10 +22,23 @@ class TestServiceHelpers(unittest.TestCase):
|
||||||
self.hass = get_test_home_assistant()
|
self.hass = get_test_home_assistant()
|
||||||
self.calls = mock_service(self.hass, 'test_domain', 'test_service')
|
self.calls = mock_service(self.hass, 'test_domain', 'test_service')
|
||||||
|
|
||||||
|
service.HASS = self.hass
|
||||||
|
|
||||||
def tearDown(self): # pylint: disable=invalid-name
|
def tearDown(self): # pylint: disable=invalid-name
|
||||||
""" Stop down stuff we started. """
|
""" Stop down stuff we started. """
|
||||||
self.hass.stop()
|
self.hass.stop()
|
||||||
|
|
||||||
|
def test_service(self):
|
||||||
|
""" Test service registration decorator. """
|
||||||
|
runs = []
|
||||||
|
|
||||||
|
decor = service.service('test', 'test')
|
||||||
|
decor(lambda x, y: runs.append(1))
|
||||||
|
|
||||||
|
self.hass.services.call('test', 'test')
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual(1, len(runs))
|
||||||
|
|
||||||
def test_split_entity_string(self):
|
def test_split_entity_string(self):
|
||||||
service.call_from_config(self.hass, {
|
service.call_from_config(self.hass, {
|
||||||
'service': 'test_domain.test_service',
|
'service': 'test_domain.test_service',
|
||||||
|
|
Loading…
Add table
Reference in a new issue