Make automation async
This commit is contained in:
1 parent
16ff68ca84
commit
7ab7edd81c
19 files changed
+205
-346
No files matched your search
@@ -2,7 +2,7 @@
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.bootstrap import _setup_component
|
||||
from homeassistant.bootstrap import setup_component
|
||||
import homeassistant.components.automation as automation
|
||||
from homeassistant.const import ATTR_ENTITY_ID
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
@@ -31,7 +31,7 @@ class TestAutomation(unittest.TestCase):
|
||||
|
||||
def test_service_data_not_a_dict(self):
|
||||
"""Test service data not dict."""
|
||||
assert not _setup_component(self.hass, automation.DOMAIN, {
|
||||
assert not setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
@@ -46,7 +46,7 @@ class TestAutomation(unittest.TestCase):
|
||||
|
||||
def test_service_specify_data(self):
|
||||
"""Test service data."""
|
||||
assert _setup_component(self.hass, automation.DOMAIN, {
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'trigger': {
|
||||
@@ -77,7 +77,7 @@ class TestAutomation(unittest.TestCase):
|
||||
|
||||
def test_service_specify_entity_id(self):
|
||||
"""Test service data."""
|
||||
assert _setup_component(self.hass, automation.DOMAIN, {
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
@@ -98,7 +98,7 @@ class TestAutomation(unittest.TestCase):
|
||||
|
||||
def test_service_specify_entity_id_list(self):
|
||||
"""Test service data."""
|
||||
assert _setup_component(self.hass, automation.DOMAIN, {
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
@@ -119,7 +119,7 @@ class TestAutomation(unittest.TestCase):
|
||||
|
||||
def test_two_triggers(self):
|
||||
"""Test triggers."""
|
||||
assert _setup_component(self.hass, automation.DOMAIN, {
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': [
|
||||
{
|
||||
@@ -147,7 +147,7 @@ class TestAutomation(unittest.TestCase):
|
||||
def test_two_conditions_with_and(self):
|
||||
"""Test two and conditions."""
|
||||
entity_id = 'test.entity'
|
||||
assert _setup_component(self.hass, automation.DOMAIN, {
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': [
|
||||
{
|
||||
@@ -188,123 +188,9 @@ class TestAutomation(unittest.TestCase):
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(1, len(self.calls))
|
||||
|
||||
def test_two_conditions_with_or(self):
|
||||
"""Test two or conditions."""
|
||||
entity_id = 'test.entity'
|
||||
assert _setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': [
|
||||
{
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
],
|
||||
'condition_type': 'OR',
|
||||
'condition': [
|
||||
{
|
||||
'platform': 'state',
|
||||
'entity_id': entity_id,
|
||||
'state': '200'
|
||||
},
|
||||
{
|
||||
'platform': 'numeric_state',
|
||||
'entity_id': entity_id,
|
||||
'below': 150
|
||||
}
|
||||
],
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.states.set(entity_id, 200)
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(1, len(self.calls))
|
||||
|
||||
self.hass.states.set(entity_id, 100)
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(2, len(self.calls))
|
||||
|
||||
self.hass.states.set(entity_id, 250)
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(2, len(self.calls))
|
||||
|
||||
def test_using_trigger_as_condition(self):
|
||||
"""Test triggers as condition."""
|
||||
entity_id = 'test.entity'
|
||||
assert _setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': [
|
||||
{
|
||||
'platform': 'state',
|
||||
'entity_id': entity_id,
|
||||
'from': '120',
|
||||
'state': '100'
|
||||
},
|
||||
{
|
||||
'platform': 'numeric_state',
|
||||
'entity_id': entity_id,
|
||||
'below': 150
|
||||
}
|
||||
],
|
||||
'condition': 'use_trigger_values',
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.states.set(entity_id, 100)
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(1, len(self.calls))
|
||||
|
||||
self.hass.states.set(entity_id, 120)
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(1, len(self.calls))
|
||||
|
||||
self.hass.states.set(entity_id, 100)
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(2, len(self.calls))
|
||||
|
||||
self.hass.states.set(entity_id, 151)
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(2, len(self.calls))
|
||||
|
||||
def test_using_trigger_as_condition_with_invalid_condition(self):
|
||||
"""Event is not a valid condition."""
|
||||
entity_id = 'test.entity'
|
||||
self.hass.states.set(entity_id, 100)
|
||||
assert _setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': [
|
||||
{
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
{
|
||||
'platform': 'numeric_state',
|
||||
'entity_id': entity_id,
|
||||
'below': 150
|
||||
}
|
||||
],
|
||||
'condition': 'use_trigger_values',
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
self.assertEqual(1, len(self.calls))
|
||||
|
||||
def test_automation_list_setting(self):
|
||||
"""Event is not a valid condition."""
|
||||
self.assertTrue(_setup_component(self.hass, automation.DOMAIN, {
|
||||
self.assertTrue(setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: [{
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
@@ -335,7 +221,7 @@ class TestAutomation(unittest.TestCase):
|
||||
|
||||
def test_automation_calling_two_actions(self):
|
||||
"""Test if we can call two actions from automation definition."""
|
||||
self.assertTrue(_setup_component(self.hass, automation.DOMAIN, {
|
||||
self.assertTrue(setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
@@ -366,7 +252,7 @@ class TestAutomation(unittest.TestCase):
|
||||
assert self.hass.states.get(entity_id) is None
|
||||
assert not automation.is_on(self.hass, entity_id)
|
||||
|
||||
assert _setup_component(self.hass, automation.DOMAIN, {
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'trigger': {
|
||||
@@ -433,7 +319,7 @@ class TestAutomation(unittest.TestCase):
|
||||
})
|
||||
def test_reload_config_service(self, mock_load_yaml):
|
||||
"""Test the reload config service."""
|
||||
assert _setup_component(self.hass, automation.DOMAIN, {
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'trigger': {
|
||||
@@ -483,7 +369,7 @@ class TestAutomation(unittest.TestCase):
|
||||
})
|
||||
def test_reload_config_when_invalid_config(self, mock_load_yaml):
|
||||
"""Test the reload config service handling invalid config."""
|
||||
assert _setup_component(self.hass, automation.DOMAIN, {
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'trigger': {
|
||||
@@ -517,7 +403,7 @@ class TestAutomation(unittest.TestCase):
|
||||
|
||||
def test_reload_config_handles_load_fails(self):
|
||||
"""Test the reload config service."""
|
||||
assert _setup_component(self.hass, automation.DOMAIN, {
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'alias': 'hello',
|
||||
'trigger': {
|
||||
|
||||
@@ -499,7 +499,7 @@ class TestAutomationNumericState(unittest.TestCase):
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'condition': {
|
||||
'platform': 'numeric_state',
|
||||
'condition': 'numeric_state',
|
||||
'entity_id': entity_id,
|
||||
'above': test_state,
|
||||
'below': test_state + 2
|
||||
|
||||
@@ -213,7 +213,7 @@ class TestAutomationState(unittest.TestCase):
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'condition': [{
|
||||
'platform': 'state',
|
||||
'condition': 'state',
|
||||
'entity_id': entity_id,
|
||||
'state': test_state
|
||||
}],
|
||||
@@ -360,7 +360,7 @@ class TestAutomationState(unittest.TestCase):
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'condition': {
|
||||
'platform': 'state',
|
||||
'condition': 'state',
|
||||
'entity_id': 'test.entity',
|
||||
'state': 'on',
|
||||
'for': {
|
||||
|
||||
@@ -172,7 +172,7 @@ class TestAutomationSun(unittest.TestCase):
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'condition': {
|
||||
'platform': 'sun',
|
||||
'condition': 'sun',
|
||||
'before': 'sunrise',
|
||||
},
|
||||
'action': {
|
||||
@@ -208,7 +208,7 @@ class TestAutomationSun(unittest.TestCase):
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'condition': {
|
||||
'platform': 'sun',
|
||||
'condition': 'sun',
|
||||
'after': 'sunrise',
|
||||
},
|
||||
'action': {
|
||||
@@ -244,7 +244,7 @@ class TestAutomationSun(unittest.TestCase):
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'condition': {
|
||||
'platform': 'sun',
|
||||
'condition': 'sun',
|
||||
'before': 'sunrise',
|
||||
'before_offset': '+1:00:00'
|
||||
},
|
||||
@@ -281,7 +281,7 @@ class TestAutomationSun(unittest.TestCase):
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'condition': {
|
||||
'platform': 'sun',
|
||||
'condition': 'sun',
|
||||
'after': 'sunrise',
|
||||
'after_offset': '+1:00:00'
|
||||
},
|
||||
@@ -319,7 +319,7 @@ class TestAutomationSun(unittest.TestCase):
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'condition': {
|
||||
'platform': 'sun',
|
||||
'condition': 'sun',
|
||||
'after': 'sunrise',
|
||||
'before': 'sunset'
|
||||
},
|
||||
@@ -365,7 +365,7 @@ class TestAutomationSun(unittest.TestCase):
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'condition': {
|
||||
'platform': 'sun',
|
||||
'condition': 'sun',
|
||||
'after': 'sunset',
|
||||
},
|
||||
'action': {
|
||||
|
||||
@@ -339,7 +339,7 @@ class TestAutomationTemplate(unittest.TestCase):
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'condition': [{
|
||||
'platform': 'template',
|
||||
'condition': 'template',
|
||||
'value_template': '{{ is_state("test.entity", "world") }}'
|
||||
}],
|
||||
'action': {
|
||||
|
||||
@@ -250,7 +250,7 @@ class TestAutomationTime(unittest.TestCase):
|
||||
'event_type': 'test_event'
|
||||
},
|
||||
'condition': {
|
||||
'platform': 'time',
|
||||
'condition': 'time',
|
||||
'before': '10:00',
|
||||
},
|
||||
'action': {
|
||||
@@ -285,7 +285,7 @@ class TestAutomationTime(unittest.TestCase):
|
||||
'event_type': 'test_event'
|
||||
},
|
||||
'condition': {
|
||||
'platform': 'time',
|
||||
'condition': 'time',
|
||||
'after': '10:00',
|
||||
},
|
||||
'action': {
|
||||
@@ -320,7 +320,7 @@ class TestAutomationTime(unittest.TestCase):
|
||||
'event_type': 'test_event'
|
||||
},
|
||||
'condition': {
|
||||
'platform': 'time',
|
||||
'condition': 'time',
|
||||
'weekday': 'mon',
|
||||
},
|
||||
'action': {
|
||||
@@ -356,7 +356,7 @@ class TestAutomationTime(unittest.TestCase):
|
||||
'event_type': 'test_event'
|
||||
},
|
||||
'condition': {
|
||||
'platform': 'time',
|
||||
'condition': 'time',
|
||||
'weekday': ['mon', 'tue'],
|
||||
},
|
||||
'action': {
|
||||
|
||||
@@ -197,7 +197,7 @@ class TestAutomationZone(unittest.TestCase):
|
||||
'event_type': 'test_event'
|
||||
},
|
||||
'condition': {
|
||||
'platform': 'zone',
|
||||
'condition': 'zone',
|
||||
'entity_id': 'test.entity',
|
||||
'zone': 'zone.test',
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user