Add context to scripts and automations (#16415)
* Add context to script helper * Update script component * Add context to automations * Lint
This commit is contained in:
1 parent
e1501c83f8
commit
746f4ac158
17 files changed
+164
-144
No files matched your search
@@ -1,7 +1,7 @@
|
||||
"""The tests for the Event automation."""
|
||||
import unittest
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.core import Context, callback
|
||||
from homeassistant.setup import setup_component
|
||||
import homeassistant.components.automation as automation
|
||||
|
||||
@@ -31,6 +31,8 @@ class TestAutomationEvent(unittest.TestCase):
|
||||
|
||||
def test_if_fires_on_event(self):
|
||||
"""Test the firing of events."""
|
||||
context = Context()
|
||||
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
@@ -43,9 +45,10 @@ class TestAutomationEvent(unittest.TestCase):
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.bus.fire('test_event', context=context)
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(1, len(self.calls))
|
||||
assert self.calls[0].context is context
|
||||
|
||||
automation.turn_off(self.hass)
|
||||
self.hass.block_till_done()
|
||||
|
||||
@@ -4,7 +4,7 @@ import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
import homeassistant.components.automation as automation
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.core import Context, callback
|
||||
from homeassistant.setup import setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
@@ -36,6 +36,7 @@ class TestAutomationNumericState(unittest.TestCase):
|
||||
|
||||
def test_if_fires_on_entity_change_below(self):
|
||||
"""Test the firing with changed entity."""
|
||||
context = Context()
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
@@ -49,9 +50,10 @@ class TestAutomationNumericState(unittest.TestCase):
|
||||
}
|
||||
})
|
||||
# 9 is below 10
|
||||
self.hass.states.set('test.entity', 9)
|
||||
self.hass.states.set('test.entity', 9, context=context)
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(1, len(self.calls))
|
||||
assert self.calls[0].context is context
|
||||
|
||||
# Set above 12 so the automation will fire again
|
||||
self.hass.states.set('test.entity', 12)
|
||||
@@ -116,6 +118,7 @@ class TestAutomationNumericState(unittest.TestCase):
|
||||
|
||||
def test_if_not_fires_on_entity_change_below_to_below(self):
|
||||
"""Test the firing with changed entity."""
|
||||
context = Context()
|
||||
self.hass.states.set('test.entity', 11)
|
||||
self.hass.block_till_done()
|
||||
|
||||
@@ -133,9 +136,10 @@ class TestAutomationNumericState(unittest.TestCase):
|
||||
})
|
||||
|
||||
# 9 is below 10 so this should fire
|
||||
self.hass.states.set('test.entity', 9)
|
||||
self.hass.states.set('test.entity', 9, context=context)
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(1, len(self.calls))
|
||||
assert self.calls[0].context is context
|
||||
|
||||
# already below so should not fire again
|
||||
self.hass.states.set('test.entity', 5)
|
||||
|
||||
@@ -4,7 +4,7 @@ from datetime import timedelta
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.core import Context, callback
|
||||
from homeassistant.setup import setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
import homeassistant.components.automation as automation
|
||||
@@ -38,6 +38,7 @@ class TestAutomationState(unittest.TestCase):
|
||||
|
||||
def test_if_fires_on_entity_change(self):
|
||||
"""Test for firing on entity change."""
|
||||
context = Context()
|
||||
self.hass.states.set('test.entity', 'hello')
|
||||
self.hass.block_till_done()
|
||||
|
||||
@@ -59,9 +60,10 @@ class TestAutomationState(unittest.TestCase):
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.states.set('test.entity', 'world')
|
||||
self.hass.states.set('test.entity', 'world', context=context)
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(1, len(self.calls))
|
||||
assert self.calls[0].context is context
|
||||
self.assertEqual(
|
||||
'state - test.entity - hello - world - None',
|
||||
self.calls[0].data['some'])
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""The tests for the Template automation."""
|
||||
import unittest
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.core import Context, callback
|
||||
from homeassistant.setup import setup_component
|
||||
import homeassistant.components.automation as automation
|
||||
|
||||
@@ -232,15 +232,12 @@ class TestAutomationTemplate(unittest.TestCase):
|
||||
|
||||
def test_if_fires_on_change_with_template_advanced(self):
|
||||
"""Test for firing on change with template advanced."""
|
||||
context = Context()
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'template',
|
||||
'value_template': '''{%- if is_state("test.entity", "world") -%}
|
||||
true
|
||||
{%- else -%}
|
||||
false
|
||||
{%- endif -%}''',
|
||||
'value_template': '{{ is_state("test.entity", "world") }}'
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
@@ -257,9 +254,10 @@ class TestAutomationTemplate(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
self.calls = []
|
||||
|
||||
self.hass.states.set('test.entity', 'world')
|
||||
self.hass.states.set('test.entity', 'world', context=context)
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(1, len(self.calls))
|
||||
assert self.calls[0].context is context
|
||||
self.assertEqual(
|
||||
'template - test.entity - hello - world',
|
||||
self.calls[0].data['some'])
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""The tests for the location automation."""
|
||||
import unittest
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.core import Context, callback
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant.components import automation, zone
|
||||
|
||||
@@ -40,6 +40,7 @@ class TestAutomationZone(unittest.TestCase):
|
||||
|
||||
def test_if_fires_on_zone_enter(self):
|
||||
"""Test for firing on zone enter."""
|
||||
context = Context()
|
||||
self.hass.states.set('test.entity', 'hello', {
|
||||
'latitude': 32.881011,
|
||||
'longitude': -117.234758
|
||||
@@ -70,10 +71,11 @@ class TestAutomationZone(unittest.TestCase):
|
||||
self.hass.states.set('test.entity', 'hello', {
|
||||
'latitude': 32.880586,
|
||||
'longitude': -117.237564
|
||||
})
|
||||
}, context=context)
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.assertEqual(1, len(self.calls))
|
||||
assert self.calls[0].context is context
|
||||
self.assertEqual(
|
||||
'zone - test.entity - hello - hello - test',
|
||||
self.calls[0].data['some'])
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.core import Context, callback
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant.components import script
|
||||
|
||||
@@ -134,6 +134,7 @@ class TestScriptComponent(unittest.TestCase):
|
||||
def test_passing_variables(self):
|
||||
"""Test different ways of passing in variables."""
|
||||
calls = []
|
||||
context = Context()
|
||||
|
||||
@callback
|
||||
def record_call(service):
|
||||
@@ -157,21 +158,23 @@ class TestScriptComponent(unittest.TestCase):
|
||||
|
||||
script.turn_on(self.hass, ENTITY_ID, {
|
||||
'greeting': 'world'
|
||||
})
|
||||
}, context=context)
|
||||
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
assert calls[-1].data['hello'] == 'world'
|
||||
assert calls[0].context is context
|
||||
assert calls[0].data['hello'] == 'world'
|
||||
|
||||
self.hass.services.call('script', 'test', {
|
||||
'greeting': 'universe',
|
||||
})
|
||||
}, context=context)
|
||||
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(calls) == 2
|
||||
assert calls[-1].data['hello'] == 'universe'
|
||||
assert calls[1].context is context
|
||||
assert calls[1].data['hello'] == 'universe'
|
||||
|
||||
def test_reload_service(self):
|
||||
"""Verify that the turn_on service."""
|
||||
|
||||
Reference in new issue
Block a user