From 5830da63b134fbc83de6e177ac1f19055d8ead2e Mon Sep 17 00:00:00 2001 From: Ryan Kraus Date: Sun, 24 Jan 2016 22:46:30 -0500 Subject: [PATCH] Moved service decorator to service helpers Moved the service decorator to the service helpers module and moved the associated tests. --- config/custom_components/example.py | 3 ++- homeassistant/bootstrap.py | 3 ++- homeassistant/helpers/service.py | 24 ++++++++++++++++++++++-- tests/helpers/test_event_decorators.py | 13 +------------ tests/helpers/test_service.py | 14 +++++++++++++- 5 files changed, 40 insertions(+), 17 deletions(-) diff --git a/config/custom_components/example.py b/config/custom_components/example.py index 3fb46d18792..08b3f4c2a83 100644 --- a/config/custom_components/example.py +++ b/config/custom_components/example.py @@ -31,7 +31,8 @@ import logging from homeassistant.const import STATE_HOME, STATE_NOT_HOME, STATE_ON, STATE_OFF from homeassistant.helpers import validate_config 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 from homeassistant.components import device_tracker from homeassistant.components import light diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index 132178361e0..dbec25b99b6 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -24,7 +24,7 @@ import homeassistant.config as config_util import homeassistant.loader as loader import homeassistant.components as core_components 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.const import ( __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 event_decorators.HASS = hass + service.HASS = hass # Setup the components for domain in loader.load_order_components(components): diff --git a/homeassistant/helpers/service.py b/homeassistant/helpers/service.py index 15cfe9b97c6..952de383444 100644 --- a/homeassistant/helpers/service.py +++ b/homeassistant/helpers/service.py @@ -1,9 +1,12 @@ """Service calling related helpers.""" +import functools import logging from homeassistant.util import split_entity_id from homeassistant.const import ATTR_ENTITY_ID +HASS = None + CONF_SERVICE = 'service' CONF_SERVICE_ENTITY_ID = 'entity_id' CONF_SERVICE_DATA = 'data' @@ -11,6 +14,23 @@ CONF_SERVICE_DATA = 'data' _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): """Call a service based on a config hash.""" if not isinstance(config, dict) or CONF_SERVICE not in config: @@ -18,7 +38,7 @@ def call_from_config(hass, config, blocking=False): return try: - domain, service = split_entity_id(config[CONF_SERVICE]) + domain, service_name = split_entity_id(config[CONF_SERVICE]) except ValueError: _LOGGER.error('Invalid service specified: %s', config[CONF_SERVICE]) return @@ -40,4 +60,4 @@ def call_from_config(hass, config, blocking=False): elif entity_id is not None: service_data[ATTR_ENTITY_ID] = entity_id - hass.services.call(domain, service, service_data, blocking) + hass.services.call(domain, service_name, service_data, blocking) diff --git a/tests/helpers/test_event_decorators.py b/tests/helpers/test_event_decorators.py index d246cf1844c..db836e372ae 100644 --- a/tests/helpers/test_event_decorators.py +++ b/tests/helpers/test_event_decorators.py @@ -15,7 +15,7 @@ import homeassistant.core as ha import homeassistant.util.dt as dt_util from homeassistant.helpers import event_decorators 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) from homeassistant.components import sun @@ -37,17 +37,6 @@ class TestEventDecoratorHelpers(unittest.TestCase): """ Stop down stuff we started. """ 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): """ Test track sunrise decorator """ latitude = 32.87336 diff --git a/tests/helpers/test_service.py b/tests/helpers/test_service.py index aa2cab07d0d..d0bd1669f07 100644 --- a/tests/helpers/test_service.py +++ b/tests/helpers/test_service.py @@ -7,7 +7,6 @@ Test service helpers. import unittest from unittest.mock import patch -from homeassistant.const import SERVICE_TURN_ON from homeassistant.helpers import 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.calls = mock_service(self.hass, 'test_domain', 'test_service') + service.HASS = self.hass + def tearDown(self): # pylint: disable=invalid-name """ Stop down stuff we started. """ 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): service.call_from_config(self.hass, { 'service': 'test_domain.test_service',