Merge pull request #412 from balloob/dev

0.7.3rc2
pull/496/head
Paulus Schoutsen 2015-09-19 21:07:24 -07:00
commit 9736761968
24 changed files with 281 additions and 137 deletions

7
.gitignore vendored
View File

@ -15,10 +15,6 @@ tests/config/home-assistant.log
*.sublime-project
*.sublime-workspace
# Hide code validator output
pep8.txt
pylint.txt
# Hide some OS X stuff
.DS_Store
.AppleDouble
@ -30,6 +26,9 @@ Icon
.idea
# pytest
.cache
# GITHUB Proposed Python stuff:
*.py[cod]

View File

@ -16,9 +16,9 @@ DOMAIN = 'automation'
DEPENDENCIES = ['group']
CONF_ALIAS = 'alias'
CONF_SERVICE = 'execute_service'
CONF_SERVICE_ENTITY_ID = 'service_entity_id'
CONF_SERVICE_DATA = 'service_data'
CONF_SERVICE = 'service'
CONF_SERVICE_ENTITY_ID = 'entity_id'
CONF_SERVICE_DATA = 'data'
CONF_CONDITION = 'condition'
CONF_ACTION = 'action'
@ -40,25 +40,45 @@ def setup(hass, config):
found = 1
while config_key in config:
p_config = _migrate_old_config(config[config_key])
# check for one block syntax
if isinstance(config[config_key], dict):
config_block = _migrate_old_config(config[config_key])
name = config_block.get(CONF_ALIAS, config_key)
_setup_automation(hass, config_block, name, config)
# check for multiple block syntax
elif isinstance(config[config_key], list):
for list_no, config_block in enumerate(config[config_key]):
name = config_block.get(CONF_ALIAS,
"{}, {}".format(config_key, list_no))
_setup_automation(hass, config_block, name, config)
# any scalar value is incorrect
else:
_LOGGER.error('Error in config in section %s.', config_key)
found += 1
config_key = "{} {}".format(DOMAIN, found)
name = p_config.get(CONF_ALIAS, config_key)
action = _get_action(hass, p_config.get(CONF_ACTION, {}), name)
return True
def _setup_automation(hass, config_block, name, config):
""" Setup one instance of automation """
action = _get_action(hass, config_block.get(CONF_ACTION, {}), name)
if action is None:
return False
if CONF_CONDITION in config_block or CONF_CONDITION_TYPE in config_block:
action = _process_if(hass, config, config_block, action)
if action is None:
continue
if CONF_CONDITION in p_config or CONF_CONDITION_TYPE in p_config:
action = _process_if(hass, config, p_config, action)
if action is None:
continue
_process_trigger(hass, config, p_config.get(CONF_TRIGGER, []), name,
action)
return False
_process_trigger(hass, config, config_block.get(CONF_TRIGGER, []), name,
action)
return True
@ -118,7 +138,10 @@ def _migrate_old_config(config):
('trigger', 'state_from', 'from'),
('trigger', 'state_hours', 'hours'),
('trigger', 'state_minutes', 'minutes'),
('trigger', 'state_seconds', 'seconds')):
('trigger', 'state_seconds', 'seconds'),
('action', 'execute_service', 'service'),
('action', 'service_entity_id', 'entity_id'),
('action', 'service_data', 'data')):
if key in new_conf[cat]:
new_conf[cat][new_key] = new_conf[cat].pop(key)

View File

@ -20,11 +20,12 @@ def trigger(hass, config, action):
_LOGGER.error("Missing configuration key %s", CONF_EVENT_TYPE)
return False
event_data = config.get(CONF_EVENT_DATA, {})
event_data = config.get(CONF_EVENT_DATA)
def handle_event(event):
""" Listens for events and calls the action when data matches. """
if event_data == event.data:
if not event_data or all(val == event.data.get(key) for key, val
in event_data.items()):
action()
hass.bus.listen(event_type, handle_event)

View File

@ -27,8 +27,7 @@ def trigger(hass, config, action):
if CONF_AFTER in config:
after = dt_util.parse_time_str(config[CONF_AFTER])
if after is None:
_LOGGER.error(
'Received invalid after value: %s', config[CONF_AFTER])
_error_time(config[CONF_AFTER], CONF_AFTER)
return False
hours, minutes, seconds = after.hour, after.minute, after.second
elif (CONF_HOURS in config or CONF_MINUTES in config
@ -63,28 +62,28 @@ def if_action(hass, config):
CONF_BEFORE, CONF_AFTER, CONF_WEEKDAY)
return None
if before is not None:
before = dt_util.parse_time_str(before)
if before is None:
_error_time(before, CONF_BEFORE)
return None
if after is not None:
after = dt_util.parse_time_str(after)
if after is None:
_error_time(after, CONF_AFTER)
return None
def time_if():
""" Validate time based if-condition """
now = dt_util.now()
if before is not None:
time = dt_util.parse_time_str(before)
if time is None:
return False
if before is not None and now > now.replace(hour=before.hour,
minute=before.minute):
return False
before_point = now.replace(hour=time.hour, minute=time.minute)
if now > before_point:
return False
if after is not None:
time = dt_util.parse_time_str(after)
if time is None:
return False
after_point = now.replace(hour=time.hour, minute=time.minute)
if now < after_point:
return False
if after is not None and now < now.replace(hour=after.hour,
minute=after.minute):
return False
if weekday is not None:
now_weekday = WEEKDAYS[now.weekday()]
@ -96,3 +95,11 @@ def if_action(hass, config):
return True
return time_if
def _error_time(value, key):
""" Helper method to print error. """
_LOGGER.error(
"Received invalid value for '%s': %s", key, value)
if isinstance(value, int):
_LOGGER.error('Make sure you wrap time values in quotes')

View File

@ -352,7 +352,7 @@ def load_config(path, hass, consider_home):
Device(hass, consider_home, device.get('track', False),
str(dev_id).lower(), str(device.get('mac')).upper(),
device.get('name'), device.get('picture'),
device.get(CONF_AWAY_HIDE, False))
device.get(CONF_AWAY_HIDE, DEFAULT_AWAY_HIDE))
for dev_id, device in load_yaml_config_file(path).items()]

@ -1 +1 @@
Subproject commit 63e039a221ae6771e0d7c6990d9a93b7cc22fc64
Subproject commit 68f6c6ae5d37a1f0fcd1c36a8803581f9367ac5f

View File

@ -147,8 +147,6 @@ def _api_history_period(handler, path_match, data):
end_time = start_time + one_day
print("Fetching", start_time, end_time)
entity_id = data.get('filter_entity_id')
handler.write_json(

View File

@ -132,7 +132,10 @@ def humanify(events):
# Process events
for event in events_batch:
if event.event_type == EVENT_STATE_CHANGED:
entity_id = event.data['entity_id']
entity_id = event.data.get('entity_id')
if entity_id is None:
continue
if entity_id.startswith('sensor.'):
last_sensor_event[entity_id] = event

View File

@ -1,7 +1,7 @@
"""
homeassistant.components.script
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
entity_id
Scripts are a sequence of actions that can be triggered manually
by the user or automatically based upon automation events, etc.
"""
@ -25,6 +25,7 @@ CONF_SEQUENCE = "sequence"
CONF_EVENT = "event"
CONF_EVENT_DATA = "event_data"
CONF_DELAY = "delay"
ATTR_ENTITY_ID = "entity_id"
_LOGGER = logging.getLogger(__name__)
@ -43,15 +44,22 @@ def setup(hass, config):
hass.services.register(DOMAIN, name, script)
scripts.append(script)
def _get_entities(service):
""" Make sure that we always get a list of entities """
if isinstance(service.data[ATTR_ENTITY_ID], list):
return service.data[ATTR_ENTITY_ID]
else:
return [service.data[ATTR_ENTITY_ID]]
def turn_on(service):
""" Calls a script. """
for entity_id in service.data['entity_id']:
for entity_id in _get_entities(service):
domain, service = split_entity_id(entity_id)
hass.services.call(domain, service, {})
def turn_off(service):
""" Cancels a script. """
for entity_id in service.data['entity_id']:
for entity_id in _get_entities(service):
for script in scripts:
if script.entity_id == entity_id:
script.cancel()

View File

@ -70,7 +70,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
data,
config.get('name', DEFAULT_NAME),
config.get('unit_of_measurement'),
config.get('correction_factor', None),
config.get('correction_factor', 1.0),
config.get('decimal_places', 0)
)])
@ -108,12 +108,15 @@ class CommandSensor(Entity):
self.data.update()
value = self.data.value
if value is not None:
if self._corr_factor is not None:
self._state = round((int(value) * self._corr_factor),
self._decimal_places)
else:
self._state = value
try:
if value is not None:
if self._corr_factor is not None:
self._state = round((float(value) * self._corr_factor),
self._decimal_places)
else:
self._state = value
except ValueError:
self._state = value
# pylint: disable=too-few-public-methods

View File

@ -31,7 +31,7 @@ Details for the API : http://transport.opendata.ch
"""
import logging
from datetime import timedelta
from requests import get
import requests
from homeassistant.util import Throttle
import homeassistant.util.dt as dt_util
@ -53,8 +53,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
try:
for location in [config.get('from', None), config.get('to', None)]:
# transport.opendata.ch doesn't play nice with requests.Session
result = get(_RESOURCE + 'locations?query=%s' % location,
timeout=10)
result = requests.get(_RESOURCE + 'locations?query=%s' % location,
timeout=10)
journey.append(result.json()['stations'][0]['name'])
except KeyError:
_LOGGER.exception(
@ -110,7 +110,7 @@ class PublicTransportData(object):
def update(self):
""" Gets the latest data from opendata.ch. """
response = get(
response = requests.get(
_RESOURCE +
'connections?' +
'from=' + self.start + '&' +

2
pytest.ini Normal file
View File

@ -0,0 +1,2 @@
[pytest]
testpaths = tests

View File

@ -4,4 +4,9 @@
# designed to run on the continuous integration server.
script/test coverage
STATUS=$?
coveralls
exit $STATUS

View File

@ -34,25 +34,27 @@ RUN_AS="USER"
PID_FILE="/var/run/hass.pid"
CONFIG_DIR="/var/opt/homeassistant"
FLAGS="-v --config $CONFIG_DIR --pid-file $PID_FILE --daemon"
REDIRECT="> $CONFIG_DIR/home-assistant.log 2>&1"
start() {
if [ -f $PID_FILE ] && kill -0 $(cat $PID_FILE); then
if [ -f $PID_FILE ] && kill -0 $(cat $PID_FILE) 2> /dev/null; then
echo 'Service already running' >&2
return 1
fi
echo 'Starting service…' >&2
local CMD="$PRE_EXEC hass $FLAGS;"
local CMD="$PRE_EXEC hass $FLAGS $REDIRECT;"
su -c "$CMD" $RUN_AS
echo 'Service started' >&2
}
stop() {
if [ ! -f "$PID_FILE" ] || ! kill -0 $(cat "$PID_FILE"); then
if [ ! -f "$PID_FILE" ] || ! kill -0 $(cat "$PID_FILE") 2> /dev/null; then
echo 'Service not running' >&2
return 1
fi
echo 'Stopping service…' >&2
kill -3 $(cat "$PID_FILE")
while ps -p $(cat "$PID_FILE") > /dev/null 2>&1; do sleep 1;done;
echo 'Service stopped' >&2
}

View File

@ -3,7 +3,16 @@
cd "$(dirname "$0")/.."
echo "Checking style with flake8..."
flake8 homeassistant
flake8 --exclude www_static homeassistant
STATUS=$?
echo "Checking style with pylint..."
pylint homeassistant
if [ $STATUS -eq 0 ]
then
exit $?
else
exit $STATUS
fi

View File

@ -2,3 +2,4 @@ cd "$(dirname "$0")/.."
git submodule init
script/bootstrap
python3 setup.py develop

View File

@ -7,10 +7,19 @@ cd "$(dirname "$0")/.."
script/lint
STATUS=$?
echo "Running tests..."
if [ "$1" = "coverage" ]; then
py.test --cov homeassistant tests
py.test --cov --cov-report=
else
py.test tests
py.test
fi
if [ $STATUS -eq 0 ]
then
exit $?
else
exit $STATUS
fi

View File

@ -75,7 +75,7 @@ class TestAutomationEvent(unittest.TestCase):
'event_type': 'test_event',
},
'action': {
'execute_service': 'test.automation',
'service': 'test.automation',
}
}
}))
@ -93,12 +93,13 @@ class TestAutomationEvent(unittest.TestCase):
'event_data': {'some_attr': 'some_value'}
},
'action': {
'execute_service': 'test.automation',
'service': 'test.automation',
}
}
}))
self.hass.bus.fire('test_event', {'some_attr': 'some_value'})
self.hass.bus.fire('test_event', {'some_attr': 'some_value',
'another': 'value'})
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
@ -111,7 +112,7 @@ class TestAutomationEvent(unittest.TestCase):
'event_data': {'some_attr': 'some_value'}
},
'action': {
'execute_service': 'test.automation',
'service': 'test.automation',
}
}
}))

View File

@ -10,7 +10,7 @@ import homeassistant.components.automation as automation
from homeassistant.const import ATTR_ENTITY_ID
class TestAutomationEvent(unittest.TestCase):
class TestAutomation(unittest.TestCase):
""" Test the event automation. """
def setUp(self): # pylint: disable=invalid-name
@ -26,7 +26,7 @@ class TestAutomationEvent(unittest.TestCase):
""" Stop down stuff we started. """
self.hass.stop()
def test_service_data_not_a_dict(self):
def test_old_config_service_data_not_a_dict(self):
automation.setup(self.hass, {
automation.DOMAIN: {
'platform': 'event',
@ -87,6 +87,24 @@ class TestAutomationEvent(unittest.TestCase):
self.assertEqual(['hello.world', 'hello.world2'],
self.calls[0].data.get(ATTR_ENTITY_ID))
def test_service_data_not_a_dict(self):
automation.setup(self.hass, {
automation.DOMAIN: {
'trigger': {
'platform': 'event',
'event_type': 'test_event',
},
'action': {
'service': 'test.automation',
'data': 100,
}
}
})
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
def test_service_specify_data(self):
automation.setup(self.hass, {
automation.DOMAIN: {
@ -95,8 +113,8 @@ class TestAutomationEvent(unittest.TestCase):
'event_type': 'test_event',
},
'action': {
'execute_service': 'test.automation',
'service_data': {'some': 'data'}
'service': 'test.automation',
'data': {'some': 'data'}
}
}
})
@ -114,8 +132,8 @@ class TestAutomationEvent(unittest.TestCase):
'event_type': 'test_event',
},
'action': {
'execute_service': 'test.automation',
'service_entity_id': 'hello.world'
'service': 'test.automation',
'entity_id': 'hello.world'
}
}
})
@ -134,8 +152,8 @@ class TestAutomationEvent(unittest.TestCase):
'event_type': 'test_event',
},
'action': {
'execute_service': 'test.automation',
'service_entity_id': ['hello.world', 'hello.world2']
'service': 'test.automation',
'entity_id': ['hello.world', 'hello.world2']
}
}
})
@ -160,7 +178,7 @@ class TestAutomationEvent(unittest.TestCase):
}
],
'action': {
'execute_service': 'test.automation',
'service': 'test.automation',
}
}
})
@ -195,7 +213,7 @@ class TestAutomationEvent(unittest.TestCase):
}
],
'action': {
'execute_service': 'test.automation',
'service': 'test.automation',
}
}
})
@ -239,7 +257,7 @@ class TestAutomationEvent(unittest.TestCase):
}
],
'action': {
'execute_service': 'test.automation',
'service': 'test.automation',
}
}
})
@ -278,7 +296,7 @@ class TestAutomationEvent(unittest.TestCase):
],
'condition': 'use_trigger_values',
'action': {
'execute_service': 'test.automation',
'service': 'test.automation',
}
}
})
@ -314,7 +332,7 @@ class TestAutomationEvent(unittest.TestCase):
],
'condition': 'use_trigger_values',
'action': {
'execute_service': 'test.automation',
'service': 'test.automation',
}
}
})
@ -322,3 +340,34 @@ class TestAutomationEvent(unittest.TestCase):
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
def test_automation_list_setting(self):
""" Event is not a valid condition. Will it still work? """
self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: [{
'trigger': {
'platform': 'event',
'event_type': 'test_event',
},
'action': {
'service': 'test.automation',
}
}, {
'trigger': {
'platform': 'event',
'event_type': 'test_event_2',
},
'action': {
'service': 'test.automation',
}
}]
}))
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
self.hass.bus.fire('test_event_2')
self.hass.pool.block_till_done()
self.assertEqual(2, len(self.calls))

View File

@ -77,7 +77,7 @@ class TestAutomationState(unittest.TestCase):
'topic': 'test-topic'
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -95,7 +95,7 @@ class TestAutomationState(unittest.TestCase):
'payload': 'hello'
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -113,7 +113,7 @@ class TestAutomationState(unittest.TestCase):
'payload': 'hello'
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))

View File

@ -35,7 +35,7 @@ class TestAutomationNumericState(unittest.TestCase):
'below': 10,
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -56,7 +56,7 @@ class TestAutomationNumericState(unittest.TestCase):
'below': 10,
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -78,7 +78,7 @@ class TestAutomationNumericState(unittest.TestCase):
'below': 10,
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -97,7 +97,7 @@ class TestAutomationNumericState(unittest.TestCase):
'above': 10,
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -119,7 +119,7 @@ class TestAutomationNumericState(unittest.TestCase):
'above': 10,
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -142,7 +142,7 @@ class TestAutomationNumericState(unittest.TestCase):
'above': 10,
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -162,7 +162,7 @@ class TestAutomationNumericState(unittest.TestCase):
'above': 5,
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -181,7 +181,7 @@ class TestAutomationNumericState(unittest.TestCase):
'above': 5,
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -203,7 +203,7 @@ class TestAutomationNumericState(unittest.TestCase):
'above': 5,
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -226,7 +226,7 @@ class TestAutomationNumericState(unittest.TestCase):
'above': 5,
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -244,7 +244,7 @@ class TestAutomationNumericState(unittest.TestCase):
'entity_id': 'test.another_entity',
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -269,7 +269,7 @@ class TestAutomationNumericState(unittest.TestCase):
'below': test_state + 2
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
})

View File

@ -164,7 +164,7 @@ class TestAutomationState(unittest.TestCase):
'entity_id': 'test.entity',
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -182,7 +182,7 @@ class TestAutomationState(unittest.TestCase):
'from': 'hello'
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -200,7 +200,7 @@ class TestAutomationState(unittest.TestCase):
'to': 'world'
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -218,7 +218,7 @@ class TestAutomationState(unittest.TestCase):
'state': 'world'
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -237,7 +237,7 @@ class TestAutomationState(unittest.TestCase):
'to': 'world'
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -256,7 +256,7 @@ class TestAutomationState(unittest.TestCase):
'to': 'world'
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -277,7 +277,7 @@ class TestAutomationState(unittest.TestCase):
'to': 'world'
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -294,7 +294,7 @@ class TestAutomationState(unittest.TestCase):
'entity_id': 'test.anoter_entity',
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -318,7 +318,7 @@ class TestAutomationState(unittest.TestCase):
'state': test_state
}],
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
})

View File

@ -51,7 +51,7 @@ class TestAutomationSun(unittest.TestCase):
'event': 'sunset',
},
'action': {
'execute_service': 'test.automation',
'service': 'test.automation',
}
}
}))
@ -77,7 +77,7 @@ class TestAutomationSun(unittest.TestCase):
'event': 'sunrise',
},
'action': {
'execute_service': 'test.automation',
'service': 'test.automation',
}
}
}))
@ -104,7 +104,7 @@ class TestAutomationSun(unittest.TestCase):
'offset': '0:30:00'
},
'action': {
'execute_service': 'test.automation',
'service': 'test.automation',
}
}
}))
@ -131,7 +131,7 @@ class TestAutomationSun(unittest.TestCase):
'offset': '-0:30:00'
},
'action': {
'execute_service': 'test.automation',
'service': 'test.automation',
}
}
}))

View File

@ -36,9 +36,9 @@ class TestAutomationTime(unittest.TestCase):
def test_old_config_if_fires_when_hour_matches(self):
self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: {
CONF_PLATFORM: 'time',
'platform': 'time',
time.CONF_HOURS: 0,
automation.CONF_SERVICE: 'test.automation'
'execute_service': 'test.automation'
}
}))
@ -51,9 +51,9 @@ class TestAutomationTime(unittest.TestCase):
def test_old_config_if_fires_when_minute_matches(self):
self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: {
CONF_PLATFORM: 'time',
'platform': 'time',
time.CONF_MINUTES: 0,
automation.CONF_SERVICE: 'test.automation'
'execute_service': 'test.automation'
}
}))
@ -66,9 +66,9 @@ class TestAutomationTime(unittest.TestCase):
def test_old_config_if_fires_when_second_matches(self):
self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: {
CONF_PLATFORM: 'time',
'platform': 'time',
time.CONF_SECONDS: 0,
automation.CONF_SERVICE: 'test.automation'
'execute_service': 'test.automation'
}
}))
@ -85,7 +85,7 @@ class TestAutomationTime(unittest.TestCase):
time.CONF_HOURS: 0,
time.CONF_MINUTES: 0,
time.CONF_SECONDS: 0,
automation.CONF_SERVICE: 'test.automation'
'execute_service': 'test.automation'
}
}))
@ -101,7 +101,7 @@ class TestAutomationTime(unittest.TestCase):
automation.DOMAIN: {
CONF_PLATFORM: 'event',
event.CONF_EVENT_TYPE: 'test_event',
automation.CONF_SERVICE: 'test.automation',
'execute_service': 'test.automation',
'if': {
CONF_PLATFORM: 'time',
time.CONF_BEFORE: '10:00'
@ -131,7 +131,7 @@ class TestAutomationTime(unittest.TestCase):
automation.DOMAIN: {
CONF_PLATFORM: 'event',
event.CONF_EVENT_TYPE: 'test_event',
automation.CONF_SERVICE: 'test.automation',
'execute_service': 'test.automation',
'if': {
CONF_PLATFORM: 'time',
time.CONF_AFTER: '10:00'
@ -161,7 +161,7 @@ class TestAutomationTime(unittest.TestCase):
automation.DOMAIN: {
CONF_PLATFORM: 'event',
event.CONF_EVENT_TYPE: 'test_event',
automation.CONF_SERVICE: 'test.automation',
'execute_service': 'test.automation',
'if': {
CONF_PLATFORM: 'time',
time.CONF_WEEKDAY: 'mon',
@ -192,7 +192,7 @@ class TestAutomationTime(unittest.TestCase):
automation.DOMAIN: {
CONF_PLATFORM: 'event',
event.CONF_EVENT_TYPE: 'test_event',
automation.CONF_SERVICE: 'test.automation',
'execute_service': 'test.automation',
'if': {
CONF_PLATFORM: 'time',
time.CONF_WEEKDAY: ['mon', 'tue'],
@ -234,7 +234,7 @@ class TestAutomationTime(unittest.TestCase):
'hours': 0,
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -252,7 +252,7 @@ class TestAutomationTime(unittest.TestCase):
'minutes': 0,
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -270,7 +270,7 @@ class TestAutomationTime(unittest.TestCase):
'seconds': 0,
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -285,18 +285,18 @@ class TestAutomationTime(unittest.TestCase):
automation.DOMAIN: {
'trigger': {
'platform': 'time',
'hours': 0,
'minutes': 0,
'seconds': 0,
'hours': 1,
'minutes': 2,
'seconds': 3,
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
fire_time_changed(self.hass, dt_util.utcnow().replace(
hour=0, minute=0, second=0))
hour=1, minute=2, second=3))
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
@ -309,7 +309,7 @@ class TestAutomationTime(unittest.TestCase):
'after': '5:00:00',
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
}))
@ -320,6 +320,30 @@ class TestAutomationTime(unittest.TestCase):
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
@patch('homeassistant.components.automation.time._LOGGER.error')
def test_if_not_fires_using_wrong_after(self, mock_error):
""" YAML translates time values to total seconds. This should break the
before rule. """
self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: {
'trigger': {
'platform': 'time',
'after': 3605,
# Total seconds. Hour = 3600 second
},
'action': {
'service': 'test.automation'
}
}
}))
fire_time_changed(self.hass, dt_util.utcnow().replace(
hour=1, minute=0, second=5))
self.hass.pool.block_till_done()
self.assertEqual(0, len(self.calls))
self.assertEqual(2, mock_error.call_count)
def test_if_action_before(self):
automation.setup(self.hass, {
automation.DOMAIN: {
@ -332,7 +356,7 @@ class TestAutomationTime(unittest.TestCase):
'before': '10:00',
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
})
@ -366,7 +390,7 @@ class TestAutomationTime(unittest.TestCase):
'after': '10:00',
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
})
@ -400,7 +424,7 @@ class TestAutomationTime(unittest.TestCase):
'weekday': 'mon',
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
})
@ -435,7 +459,7 @@ class TestAutomationTime(unittest.TestCase):
'weekday': ['mon', 'tue'],
},
'action': {
'execute_service': 'test.automation'
'service': 'test.automation'
}
}
})