diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index 2cca8e1495b..e05fff98865 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -21,7 +21,6 @@ import homeassistant.loader as loader from homeassistant.util.logging import AsyncHandler from homeassistant.util.yaml import clear_secret_cache from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers import event_decorators, service from homeassistant.helpers.signal import async_register_signal_handling _LOGGER = logging.getLogger(__name__) @@ -127,10 +126,6 @@ def async_from_config_dict(config: Dict[str, Any], _LOGGER.info('Home Assistant core initialized') - # Give event decorators access to HASS - event_decorators.HASS = hass - service.HASS = hass - # stage 1 for component in components: if component not in FIRST_INIT_COMPONENT: diff --git a/homeassistant/helpers/event_decorators.py b/homeassistant/helpers/event_decorators.py deleted file mode 100644 index d8c24544b7c..00000000000 --- a/homeassistant/helpers/event_decorators.py +++ /dev/null @@ -1,85 +0,0 @@ -"""Event Decorators for custom components.""" -import functools -import logging - -# pylint: disable=unused-import -from typing import Optional # NOQA - -from homeassistant.core import HomeAssistant # NOQA -from homeassistant.helpers import event - -HASS = None # type: Optional[HomeAssistant] -_LOGGER = logging.getLogger(__name__) -_MSG = 'Event decorators are deprecated. Support will be removed in 0.40.' - - -def track_state_change(entity_ids, from_state=None, to_state=None): - """Decorator factory to track state changes for entity id.""" - _LOGGER.warning(_MSG) - - def track_state_change_decorator(action): - """Decorator to track state changes.""" - event.track_state_change(HASS, entity_ids, - functools.partial(action, HASS), - from_state, to_state) - return action - - return track_state_change_decorator - - -def track_sunrise(offset=None): - """Decorator factory to track sunrise events.""" - _LOGGER.warning(_MSG) - - def track_sunrise_decorator(action): - """Decorator to track sunrise events.""" - event.track_sunrise(HASS, - functools.partial(action, HASS), - offset) - return action - - return track_sunrise_decorator - - -def track_sunset(offset=None): - """Decorator factory to track sunset events.""" - _LOGGER.warning(_MSG) - - def track_sunset_decorator(action): - """Decorator to track sunset events.""" - event.track_sunset(HASS, - functools.partial(action, HASS), - offset) - return action - - return track_sunset_decorator - - -def track_time_change(year=None, month=None, day=None, hour=None, minute=None, - second=None): - """Decorator factory to track time changes.""" - _LOGGER.warning(_MSG) - - def track_time_change_decorator(action): - """Decorator to track time changes.""" - event.track_time_change(HASS, - functools.partial(action, HASS), - year, month, day, hour, minute, second) - return action - - return track_time_change_decorator - - -def track_utc_time_change(year=None, month=None, day=None, hour=None, - minute=None, second=None): - """Decorator factory to track time changes.""" - _LOGGER.warning(_MSG) - - def track_utc_time_change_decorator(action): - """Decorator to track time changes.""" - event.track_utc_time_change(HASS, - functools.partial(action, HASS), - year, month, day, hour, minute, second) - return action - - return track_utc_time_change_decorator diff --git a/homeassistant/helpers/service.py b/homeassistant/helpers/service.py index 21df1244872..ce0d4f6c8a3 100644 --- a/homeassistant/helpers/service.py +++ b/homeassistant/helpers/service.py @@ -1,6 +1,5 @@ """Service calling related helpers.""" import asyncio -import functools import logging # pylint: disable=unused-import from typing import Optional # NOQA @@ -14,8 +13,6 @@ from homeassistant.loader import get_component import homeassistant.helpers.config_validation as cv from homeassistant.util.async import run_coroutine_threadsafe -HASS = None # type: Optional[HomeAssistant] - CONF_SERVICE = 'service' CONF_SERVICE_TEMPLATE = 'service_template' CONF_SERVICE_ENTITY_ID = 'entity_id' @@ -25,17 +22,6 @@ CONF_SERVICE_DATA_TEMPLATE = 'data_template' _LOGGER = logging.getLogger(__name__) -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(action, HASS)) - return action - - return register_service_decorator - - def call_from_config(hass, config, blocking=False, variables=None, validate_config=True): """Call a service based on a config hash.""" diff --git a/tests/components/test_device_sun_light_trigger.py b/tests/components/test_device_sun_light_trigger.py index 2b11a420fdc..2d2f7313199 100644 --- a/tests/components/test_device_sun_light_trigger.py +++ b/tests/components/test_device_sun_light_trigger.py @@ -8,7 +8,6 @@ import homeassistant.loader as loader from homeassistant.const import CONF_PLATFORM, STATE_HOME, STATE_NOT_HOME from homeassistant.components import ( device_tracker, light, sun, device_sun_light_trigger) -from homeassistant.helpers import event_decorators from tests.common import ( get_test_config_dir, get_test_home_assistant, ensure_sun_risen, @@ -45,7 +44,6 @@ class TestDeviceSunLightTrigger(unittest.TestCase): def setUp(self): # pylint: disable=invalid-name """Setup things to be run when tests are started.""" self.hass = get_test_home_assistant() - event_decorators.HASS = self.hass self.scanner = loader.get_component( 'device_tracker.test').get_scanner(None, None) @@ -69,7 +67,6 @@ class TestDeviceSunLightTrigger(unittest.TestCase): def tearDown(self): # pylint: disable=invalid-name """Stop everything that was started.""" self.hass.stop() - event_decorators.HASS = None def test_lights_on_when_sun_sets(self): """Test lights go on when there is someone home and the sun sets.""" diff --git a/tests/helpers/test_event_decorators.py b/tests/helpers/test_event_decorators.py deleted file mode 100644 index 798db1128a5..00000000000 --- a/tests/helpers/test_event_decorators.py +++ /dev/null @@ -1,197 +0,0 @@ -"""Test event decorator helpers.""" -# pylint: disable=protected-access -import unittest -from datetime import datetime, timedelta - -from astral import Astral - -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, - track_sunrise, track_sunset) -from homeassistant.components import sun - -from tests.common import get_test_home_assistant - - -class TestEventDecoratorHelpers(unittest.TestCase): - """Test the Home Assistant event helpers.""" - - # pylint: disable=invalid-name - def setUp(self): - """Setup things to be run when tests are started.""" - self.hass = get_test_home_assistant() - self.hass.states.set("light.Bowl", "on") - self.hass.states.set("switch.AC", "off") - - event_decorators.HASS = self.hass - - # pylint: disable=invalid-name - def tearDown(self): - """Stop everything that was started.""" - self.hass.stop() - event_decorators.HASS = None - - def test_track_sunrise(self): - """Test track sunrise decorator.""" - latitude = 32.87336 - longitude = 117.22743 - - # Setup sun component - self.hass.config.latitude = latitude - self.hass.config.longitude = longitude - sun.setup(self.hass, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}}) - - # Get next sunrise/sunset - astral = Astral() - utc_now = dt_util.utcnow() - - mod = -1 - while True: - next_rising = (astral.sunrise_utc(utc_now + - timedelta(days=mod), latitude, longitude)) - if next_rising > utc_now: - break - mod += 1 - - # Use decorator - runs = [] - decor = track_sunrise() - decor(lambda x: runs.append(1)) - - offset_runs = [] - offset = timedelta(minutes=30) - decor = track_sunrise(offset) - decor(lambda x: offset_runs.append(1)) - - # Run tests - self._send_time_changed(next_rising - offset) - self.hass.block_till_done() - self.assertEqual(0, len(runs)) - self.assertEqual(0, len(offset_runs)) - - self._send_time_changed(next_rising) - self.hass.block_till_done() - self.assertEqual(1, len(runs)) - self.assertEqual(0, len(offset_runs)) - - self._send_time_changed(next_rising + offset) - self.hass.block_till_done() - self.assertEqual(2, len(runs)) - self.assertEqual(1, len(offset_runs)) - - def test_track_sunset(self): - """Test track sunset decorator.""" - latitude = 32.87336 - longitude = 117.22743 - - # Setup sun component - self.hass.config.latitude = latitude - self.hass.config.longitude = longitude - sun.setup(self.hass, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}}) - - # Get next sunrise/sunset - astral = Astral() - utc_now = dt_util.utcnow() - - mod = -1 - while True: - next_setting = (astral.sunset_utc(utc_now + - timedelta(days=mod), latitude, longitude)) - if next_setting > utc_now: - break - mod += 1 - - # Use decorator - runs = [] - decor = track_sunset() - decor(lambda x: runs.append(1)) - - offset_runs = [] - offset = timedelta(minutes=30) - decor = track_sunset(offset) - decor(lambda x: offset_runs.append(1)) - - # run tests - self._send_time_changed(next_setting - offset) - self.hass.block_till_done() - self.assertEqual(0, len(runs)) - self.assertEqual(0, len(offset_runs)) - - self._send_time_changed(next_setting) - self.hass.block_till_done() - self.assertEqual(1, len(runs)) - self.assertEqual(0, len(offset_runs)) - - self._send_time_changed(next_setting + offset) - self.hass.block_till_done() - self.assertEqual(2, len(runs)) - self.assertEqual(1, len(offset_runs)) - - def test_track_time_change(self): - """Test tracking time change.""" - wildcard_runs = [] - specific_runs = [] - - decor = track_time_change() - decor(lambda x, y: wildcard_runs.append(1)) - - decor = track_utc_time_change(second=[0, 30]) - decor(lambda x, y: specific_runs.append(1)) - - self._send_time_changed(datetime(2014, 5, 24, 12, 0, 0)) - self.hass.block_till_done() - self.assertEqual(1, len(specific_runs)) - self.assertEqual(1, len(wildcard_runs)) - - self._send_time_changed(datetime(2014, 5, 24, 12, 0, 15)) - self.hass.block_till_done() - self.assertEqual(1, len(specific_runs)) - self.assertEqual(2, len(wildcard_runs)) - - self._send_time_changed(datetime(2014, 5, 24, 12, 0, 30)) - self.hass.block_till_done() - self.assertEqual(2, len(specific_runs)) - self.assertEqual(3, len(wildcard_runs)) - - def test_track_state_change(self): - """Test track_state_change.""" - # 2 lists to track how often our callbacks get called - specific_runs = [] - wildcard_runs = [] - - decor = track_state_change('light.Bowl', 'on', 'off') - decor(lambda a, b, c, d: specific_runs.append(1)) - - decor = track_state_change('light.Bowl', ha.MATCH_ALL, ha.MATCH_ALL) - decor(lambda a, b, c, d: wildcard_runs.append(1)) - - # Set same state should not trigger a state change/listener - self.hass.states.set('light.Bowl', 'on') - self.hass.block_till_done() - self.assertEqual(0, len(specific_runs)) - self.assertEqual(0, len(wildcard_runs)) - - # State change off -> on - self.hass.states.set('light.Bowl', 'off') - self.hass.block_till_done() - self.assertEqual(1, len(specific_runs)) - self.assertEqual(1, len(wildcard_runs)) - - # State change off -> off - self.hass.states.set('light.Bowl', 'off', {"some_attr": 1}) - self.hass.block_till_done() - self.assertEqual(1, len(specific_runs)) - self.assertEqual(2, len(wildcard_runs)) - - # State change off -> on - self.hass.states.set('light.Bowl', 'on') - self.hass.block_till_done() - self.assertEqual(1, len(specific_runs)) - self.assertEqual(3, len(wildcard_runs)) - - def _send_time_changed(self, now): - """Send a time changed event.""" - self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: now}) diff --git a/tests/helpers/test_service.py b/tests/helpers/test_service.py index 45b9a4919f4..a5aa093bcd5 100644 --- a/tests/helpers/test_service.py +++ b/tests/helpers/test_service.py @@ -21,23 +21,10 @@ 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 everything that was 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.block_till_done() - self.assertEqual(1, len(runs)) - def test_template_service_call(self): """Test service call with tempating.""" config = { @@ -52,18 +39,14 @@ class TestServiceHelpers(unittest.TestCase): 'list': ['{{ \'list\' }}', '2'], }, } - runs = [] - - decor = service.service('test_domain', 'test_service') - decor(lambda x, y: runs.append(y)) service.call_from_config(self.hass, config) self.hass.block_till_done() - self.assertEqual('goodbye', runs[0].data['hello']) - self.assertEqual('complex', runs[0].data['data']['value']) - self.assertEqual('simple', runs[0].data['data']['simple']) - self.assertEqual('list', runs[0].data['list'][0]) + self.assertEqual('goodbye', self.calls[0].data['hello']) + self.assertEqual('complex', self.calls[0].data['data']['value']) + self.assertEqual('simple', self.calls[0].data['data']['simple']) + self.assertEqual('list', self.calls[0].data['list'][0]) def test_passing_variables_to_templates(self): """Test passing variables to templates.""" @@ -74,10 +57,6 @@ class TestServiceHelpers(unittest.TestCase): 'hello': '{{ var_data }}', }, } - runs = [] - - decor = service.service('test_domain', 'test_service') - decor(lambda x, y: runs.append(y)) service.call_from_config(self.hass, config, variables={ 'var_service': 'test_domain.test_service', @@ -85,7 +64,7 @@ class TestServiceHelpers(unittest.TestCase): }) self.hass.block_till_done() - self.assertEqual('goodbye', runs[0].data['hello']) + self.assertEqual('goodbye', self.calls[0].data['hello']) def test_split_entity_string(self): """Test splitting of entity string."""