Automation: Add trigger context and expose to action
This commit is contained in:
1 parent
c4913a87e4
commit
4e568f8b99
20 files changed
+232
-69
No files matched your search
@@ -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."""
|
||||
|
||||
@@ -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."""
|
||||
|
||||
@@ -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."""
|
||||
|
||||
@@ -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."""
|
||||
|
||||
@@ -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."""
|
||||
|
||||
@@ -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."""
|
||||
|
||||
@@ -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."""
|
||||
|
||||
Reference in new issue
Block a user