Automation: Add trigger context and expose to action

This commit is contained in:
Paulus Schoutsen committed 2016-04-21 13:59:42 -07:00
1 parent c4913a87e4
commit 4e568f8b99
20 files changed
+232 -69

No files matched your search

+5 -2
View File
@@ -51,7 +51,10 @@ class TestAutomation(unittest.TestCase):
},
'action': {
'service': 'test.automation',
'data': {'some': 'data'}
'data_template': {
'some': '{{ trigger.platform }} - '
'{{ trigger.event.event_type }}'
},
}
}
})
@@ -59,7 +62,7 @@ class TestAutomation(unittest.TestCase):
self.hass.bus.fire('test_event')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
self.assertEqual('data', self.calls[0].data['some'])
self.assertEqual('event - test_event', self.calls[0].data['some'])
def test_service_specify_entity_id(self):
"""Test service data."""
+8 -2
View File
@@ -35,14 +35,20 @@ class TestAutomationMQTT(unittest.TestCase):
'topic': 'test-topic'
},
'action': {
'service': 'test.automation'
'service': 'test.automation',
'data_template': {
'some': '{{ trigger.platform }} - {{ trigger.topic }}'
' - {{ trigger.payload }}'
},
}
}
})
fire_mqtt_message(self.hass, 'test-topic', '')
fire_mqtt_message(self.hass, 'test-topic', 'test_payload')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
self.assertEqual('mqtt - test-topic - test_payload',
self.calls[0].data['some'])
def test_if_fires_on_topic_and_payload_match(self):
"""Test if message is fired on topic and payload match."""
@@ -437,15 +437,28 @@ class TestAutomationNumericState(unittest.TestCase):
'below': 10,
},
'action': {
'service': 'test.automation'
'service': 'test.automation',
'data_template': {
'some': '{{ trigger.%s }}' % '}} - {{ trigger.'.join((
'platform', 'entity_id', 'below', 'above',
'from_state.state', 'from_value',
'to_state.state', 'to_value'))
},
}
}
})
# 9 is below 10
self.hass.states.set('test.entity', 'entity',
self.hass.states.set('test.entity', 'test state 1',
{'test_attribute': '1.2'})
self.hass.pool.block_till_done()
self.hass.states.set('test.entity', 'test state 2',
{'test_attribute': '0.9'})
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
self.assertEqual(
'numeric_state - test.entity - 10 - None - test state 1 - 12.0 - '
'test state 2 - 9.0',
self.calls[0].data['some'])
def test_not_fires_on_attr_change_with_attr_not_below_multiple_attr(self):
""""Test if not fired changed attributes."""
+13 -1
View File
@@ -31,6 +31,9 @@ class TestAutomationState(unittest.TestCase):
def test_if_fires_on_entity_change(self):
"""Test for firing on entity change."""
self.hass.states.set('test.entity', 'hello')
self.hass.pool.block_till_done()
assert _setup_component(self.hass, automation.DOMAIN, {
automation.DOMAIN: {
'trigger': {
@@ -38,7 +41,13 @@ class TestAutomationState(unittest.TestCase):
'entity_id': 'test.entity',
},
'action': {
'service': 'test.automation'
'service': 'test.automation',
'data_template': {
'some': '{{ trigger.%s }}' % '}} - {{ trigger.'.join((
'platform', 'entity_id',
'from_state.state', 'to_state.state',
'for'))
},
}
}
})
@@ -46,6 +55,9 @@ class TestAutomationState(unittest.TestCase):
self.hass.states.set('test.entity', 'world')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
self.assertEqual(
'state - test.entity - hello - world - None',
self.calls[0].data['some'])
def test_if_fires_on_entity_change_with_from_filter(self):
"""Test for firing on entity change with filter."""
+6
View File
@@ -105,6 +105,11 @@ class TestAutomationSun(unittest.TestCase):
},
'action': {
'service': 'test.automation',
'data_template': {
'some':
'{{ trigger.%s }}' % '}} - {{ trigger.'.join((
'platform', 'event', 'offset'))
},
}
}
})
@@ -112,6 +117,7 @@ class TestAutomationSun(unittest.TestCase):
fire_time_changed(self.hass, trigger_time)
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
self.assertEqual('sun - sunset - 0:30:00', self.calls[0].data['some'])
def test_sunrise_trigger_with_offset(self):
"""Test the runrise trigger with offset."""
+11 -2
View File
@@ -1,4 +1,4 @@
"""The tests fr the Template automation."""
"""The tests for the Template automation."""
import unittest
from homeassistant.bootstrap import _setup_component
@@ -226,7 +226,13 @@ class TestAutomationTemplate(unittest.TestCase):
{%- endif -%}''',
},
'action': {
'service': 'test.automation'
'service': 'test.automation',
'data_template': {
'some':
'{{ trigger.%s }}' % '}} - {{ trigger.'.join((
'platform', 'entity_id', 'from_state.state',
'to_state.state'))
},
}
}
})
@@ -234,6 +240,9 @@ class TestAutomationTemplate(unittest.TestCase):
self.hass.states.set('test.entity', 'world')
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
self.assertEqual(
'template - test.entity - hello - world',
self.calls[0].data['some'])
def test_if_fires_on_no_change_with_template_advanced(self):
"""Test for firing on no change with template advanced."""
+6 -1
View File
@@ -176,7 +176,11 @@ class TestAutomationTime(unittest.TestCase):
'after': '5:00:00',
},
'action': {
'service': 'test.automation'
'service': 'test.automation',
'data_template': {
'some': '{{ trigger.platform }} - '
'{{ trigger.now.hour }}'
},
}
}
})
@@ -186,6 +190,7 @@ class TestAutomationTime(unittest.TestCase):
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
self.assertEqual('time - 5', self.calls[0].data['some'])
def test_if_not_working_if_no_values_in_conf_provided(self):
"""Test for failure if no configuration."""
+10
View File
@@ -52,6 +52,13 @@ class TestAutomationZone(unittest.TestCase):
},
'action': {
'service': 'test.automation',
'data_template': {
'some': '{{ trigger.%s }}' % '}} - {{ trigger.'.join((
'platform', 'entity_id',
'from_state.state', 'to_state.state',
'zone.name'))
},
}
}
})
@@ -63,6 +70,9 @@ class TestAutomationZone(unittest.TestCase):
self.hass.pool.block_till_done()
self.assertEqual(1, len(self.calls))
self.assertEqual(
'zone - test.entity - hello - hello - test',
self.calls[0].data['some'])
def test_if_not_fires_for_enter_on_zone_leave(self):
"""Test for not firing on zone leave."""
+22 -2
View File
@@ -7,6 +7,7 @@ from datetime import datetime, timedelta
from astral import Astral
import homeassistant.core as ha
from homeassistant.const import MATCH_ALL
from homeassistant.helpers.event import (
track_point_in_utc_time,
track_point_in_time,
@@ -93,6 +94,7 @@ class TestEventHelpers(unittest.TestCase):
# 2 lists to track how often our callbacks get called
specific_runs = []
wildcard_runs = []
wildercard_runs = []
track_state_change(
self.hass, 'light.Bowl', lambda a, b, c: specific_runs.append(1),
@@ -100,14 +102,18 @@ class TestEventHelpers(unittest.TestCase):
track_state_change(
self.hass, 'light.Bowl',
lambda _, old_s, new_s: wildcard_runs.append((old_s, new_s)),
ha.MATCH_ALL, ha.MATCH_ALL)
lambda _, old_s, new_s: wildcard_runs.append((old_s, new_s)))
track_state_change(
self.hass, MATCH_ALL,
lambda _, old_s, new_s: wildercard_runs.append((old_s, new_s)))
# Adding state to state machine
self.hass.states.set("light.Bowl", "on")
self.hass.pool.block_till_done()
self.assertEqual(0, len(specific_runs))
self.assertEqual(1, len(wildcard_runs))
self.assertEqual(1, len(wildercard_runs))
self.assertIsNone(wildcard_runs[-1][0])
self.assertIsNotNone(wildcard_runs[-1][1])
@@ -116,31 +122,45 @@ class TestEventHelpers(unittest.TestCase):
self.hass.pool.block_till_done()
self.assertEqual(0, len(specific_runs))
self.assertEqual(1, len(wildcard_runs))
self.assertEqual(1, len(wildercard_runs))
# State change off -> on
self.hass.states.set('light.Bowl', 'off')
self.hass.pool.block_till_done()
self.assertEqual(1, len(specific_runs))
self.assertEqual(2, len(wildcard_runs))
self.assertEqual(2, len(wildercard_runs))
# State change off -> off
self.hass.states.set('light.Bowl', 'off', {"some_attr": 1})
self.hass.pool.block_till_done()
self.assertEqual(1, len(specific_runs))
self.assertEqual(3, len(wildcard_runs))
self.assertEqual(3, len(wildercard_runs))
# State change off -> on
self.hass.states.set('light.Bowl', 'on')
self.hass.pool.block_till_done()
self.assertEqual(1, len(specific_runs))
self.assertEqual(4, len(wildcard_runs))
self.assertEqual(4, len(wildercard_runs))
self.hass.states.remove('light.bowl')
self.hass.pool.block_till_done()
self.assertEqual(1, len(specific_runs))
self.assertEqual(5, len(wildcard_runs))
self.assertEqual(5, len(wildercard_runs))
self.assertIsNotNone(wildcard_runs[-1][0])
self.assertIsNone(wildcard_runs[-1][1])
self.assertIsNotNone(wildercard_runs[-1][0])
self.assertIsNone(wildercard_runs[-1][1])
# Set state for different entity id
self.hass.states.set('switch.kitchen', 'on')
self.hass.pool.block_till_done()
self.assertEqual(1, len(specific_runs))
self.assertEqual(5, len(wildcard_runs))
self.assertEqual(6, len(wildercard_runs))
def test_track_sunrise(self):
"""Test track the sunrise."""
+2 -1
View File
@@ -2,7 +2,8 @@
import unittest
from unittest.mock import patch
import homeassistant.components # noqa - to prevent circular import
# To prevent circular import when running just this file
import homeassistant.components # noqa
from homeassistant import core as ha, loader
from homeassistant.const import STATE_ON, STATE_OFF, ATTR_ENTITY_ID
from homeassistant.helpers import service