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:
Paulus Schoutsen 2018-09-04 21:16:24 +02:00 committed by Pascal Vizeli
parent e1501c83f8
commit 746f4ac158
17 changed files with 164 additions and 144 deletions

View file

@ -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)