Skip to content

Commit

Permalink
Add automation platform to listen for start/shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob committed Apr 5, 2017
1 parent deee087 commit 3b46e44
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
55 changes: 55 additions & 0 deletions homeassistant/components/automation/homeassistant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
Offer Home Assistant core automation rules.
For more details about this automation rule, please refer to the documentation
at https://home-assistant.io/components/automation/#homeassistant-trigger
"""
import asyncio
import logging

import voluptuous as vol

from homeassistant.core import callback, CoreState
from homeassistant.const import (
CONF_PLATFORM, CONF_EVENT, EVENT_HOMEASSISTANT_STOP)

EVENT_START = 'start'
EVENT_SHUTDOWN = 'shutdown'
_LOGGER = logging.getLogger(__name__)

TRIGGER_SCHEMA = vol.Schema({
vol.Required(CONF_PLATFORM): 'homeassistant',
vol.Required(CONF_EVENT): vol.Any(EVENT_START, EVENT_SHUTDOWN),
})


@asyncio.coroutine
def async_trigger(hass, config, action):
"""Listen for events based on configuration."""
event = config.get(CONF_EVENT)

if event == EVENT_SHUTDOWN:
@callback
def hass_shutdown(event):
"""Called when Home Assistant is shutting down."""
hass.async_run_job(action, {
'trigger': {
'platform': 'homeassistant',
'event': event,
},
})

return hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP,
hass_shutdown)

# Automation are enabled while hass is starting up, fire right away
# Check state because a config reload shouldn't trigger it.
elif hass.state == CoreState.starting:
hass.async_run_job(action, {
'trigger': {
'platform': 'homeassistant',
'event': event,
},
})

return lambda: None
84 changes: 84 additions & 0 deletions tests/components/automation/test_homeassistant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""The tests for the Event automation."""
import asyncio
from unittest.mock import patch, Mock

from homeassistant.core import CoreState
from homeassistant.setup import async_setup_component
import homeassistant.components.automation as automation

from tests.common import mock_service, mock_coro


@asyncio.coroutine
def test_if_fires_on_hass_start(hass):
"""Test the firing when HASS starts."""
calls = mock_service(hass, 'test', 'automation')
hass.state = CoreState.not_running
config = {
automation.DOMAIN: {
'alias': 'hello',
'trigger': {
'platform': 'homeassistant',
'event': 'start',
},
'action': {
'service': 'test.automation',
}
}
}

res = yield from async_setup_component(hass, automation.DOMAIN, config)
assert res
assert not automation.is_on(hass, 'automation.hello')
assert len(calls) == 0

yield from hass.async_start()
assert automation.is_on(hass, 'automation.hello')
assert len(calls) == 1

with patch('homeassistant.config.async_hass_config_yaml',
Mock(return_value=mock_coro(config))):
yield from hass.services.async_call(
automation.DOMAIN, automation.SERVICE_RELOAD, blocking=True)

assert automation.is_on(hass, 'automation.hello')
assert len(calls) == 1


@asyncio.coroutine
def test_if_fires_on_hass_shutdown(hass):
"""Test the firing when HASS starts."""
calls = mock_service(hass, 'test', 'automation')
hass.state = CoreState.not_running

res = yield from async_setup_component(hass, automation.DOMAIN, {
automation.DOMAIN: {
'alias': 'hello',
'trigger': {
'platform': 'homeassistant',
'event': 'shutdown',
},
'action': {
'service': 'test.automation',
}
}
})
assert res
assert not automation.is_on(hass, 'automation.hello')
assert len(calls) == 0

yield from hass.async_start()
assert automation.is_on(hass, 'automation.hello')
assert len(calls) == 0

with patch.object(hass.loop, 'stop'), patch.object(hass.executor, 'shutdown'):
yield from hass.async_stop()
assert len(calls) == 1

# with patch('homeassistant.config.async_hass_config_yaml',
# Mock(return_value=mock_coro(config))):
# yield from hass.services.async_call(
# automation.DOMAIN, automation.SERVICE_RELOAD, blocking=True)

# assert automation.is_on(hass, 'automation.hello')
# assert len(calls) == 1

0 comments on commit 3b46e44

Please sign in to comment.