Refactor automation configuration
This commit is contained in:
parent
fe2a9bb83e
commit
68c1dd7cd4
11 changed files with 836 additions and 223 deletions
|
@ -8,8 +8,6 @@ import unittest
|
|||
|
||||
import homeassistant.core as ha
|
||||
import homeassistant.components.automation as automation
|
||||
from homeassistant.components.automation import event, numeric_state
|
||||
from homeassistant.const import CONF_PLATFORM
|
||||
|
||||
|
||||
class TestAutomationNumericState(unittest.TestCase):
|
||||
|
@ -28,31 +26,17 @@ class TestAutomationNumericState(unittest.TestCase):
|
|||
""" Stop down stuff we started. """
|
||||
self.hass.stop()
|
||||
|
||||
def test_setup_fails_if_no_entity_id(self):
|
||||
self.assertFalse(automation.setup(self.hass, {
|
||||
automation.DOMAIN: {
|
||||
CONF_PLATFORM: 'numeric_state',
|
||||
numeric_state.CONF_BELOW: 10,
|
||||
automation.CONF_SERVICE: 'test.automation'
|
||||
}
|
||||
}))
|
||||
|
||||
def test_setup_fails_if_no_condition(self):
|
||||
self.assertFalse(automation.setup(self.hass, {
|
||||
automation.DOMAIN: {
|
||||
CONF_PLATFORM: 'numeric_state',
|
||||
numeric_state.CONF_ENTITY_ID: 'test.entity',
|
||||
automation.CONF_SERVICE: 'test.automation'
|
||||
}
|
||||
}))
|
||||
|
||||
def test_if_fires_on_entity_change_below(self):
|
||||
self.assertTrue(automation.setup(self.hass, {
|
||||
automation.DOMAIN: {
|
||||
CONF_PLATFORM: 'numeric_state',
|
||||
numeric_state.CONF_ENTITY_ID: 'test.entity',
|
||||
numeric_state.CONF_BELOW: 10,
|
||||
automation.CONF_SERVICE: 'test.automation'
|
||||
'trigger': {
|
||||
'platform': 'numeric_state',
|
||||
'entity_id': 'test.entity',
|
||||
'below': 10,
|
||||
},
|
||||
'action': {
|
||||
'execute_service': 'test.automation'
|
||||
}
|
||||
}
|
||||
}))
|
||||
# 9 is below 10
|
||||
|
@ -66,10 +50,14 @@ class TestAutomationNumericState(unittest.TestCase):
|
|||
|
||||
self.assertTrue(automation.setup(self.hass, {
|
||||
automation.DOMAIN: {
|
||||
CONF_PLATFORM: 'numeric_state',
|
||||
numeric_state.CONF_ENTITY_ID: 'test.entity',
|
||||
numeric_state.CONF_BELOW: 10,
|
||||
automation.CONF_SERVICE: 'test.automation'
|
||||
'trigger': {
|
||||
'platform': 'numeric_state',
|
||||
'entity_id': 'test.entity',
|
||||
'below': 10,
|
||||
},
|
||||
'action': {
|
||||
'execute_service': 'test.automation'
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
||||
|
@ -78,17 +66,20 @@ class TestAutomationNumericState(unittest.TestCase):
|
|||
self.hass.pool.block_till_done()
|
||||
self.assertEqual(1, len(self.calls))
|
||||
|
||||
|
||||
def test_if_not_fires_on_entity_change_below_to_below(self):
|
||||
self.hass.states.set('test.entity', 9)
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
self.assertTrue(automation.setup(self.hass, {
|
||||
automation.DOMAIN: {
|
||||
CONF_PLATFORM: 'numeric_state',
|
||||
numeric_state.CONF_ENTITY_ID: 'test.entity',
|
||||
numeric_state.CONF_BELOW: 10,
|
||||
automation.CONF_SERVICE: 'test.automation'
|
||||
'trigger': {
|
||||
'platform': 'numeric_state',
|
||||
'entity_id': 'test.entity',
|
||||
'below': 10,
|
||||
},
|
||||
'action': {
|
||||
'execute_service': 'test.automation'
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
||||
|
@ -97,14 +88,17 @@ class TestAutomationNumericState(unittest.TestCase):
|
|||
self.hass.pool.block_till_done()
|
||||
self.assertEqual(0, len(self.calls))
|
||||
|
||||
|
||||
def test_if_fires_on_entity_change_above(self):
|
||||
self.assertTrue(automation.setup(self.hass, {
|
||||
automation.DOMAIN: {
|
||||
CONF_PLATFORM: 'numeric_state',
|
||||
numeric_state.CONF_ENTITY_ID: 'test.entity',
|
||||
numeric_state.CONF_ABOVE: 10,
|
||||
automation.CONF_SERVICE: 'test.automation'
|
||||
'trigger': {
|
||||
'platform': 'numeric_state',
|
||||
'entity_id': 'test.entity',
|
||||
'above': 10,
|
||||
},
|
||||
'action': {
|
||||
'execute_service': 'test.automation'
|
||||
}
|
||||
}
|
||||
}))
|
||||
# 11 is above 10
|
||||
|
@ -119,10 +113,14 @@ class TestAutomationNumericState(unittest.TestCase):
|
|||
|
||||
self.assertTrue(automation.setup(self.hass, {
|
||||
automation.DOMAIN: {
|
||||
CONF_PLATFORM: 'numeric_state',
|
||||
numeric_state.CONF_ENTITY_ID: 'test.entity',
|
||||
numeric_state.CONF_ABOVE: 10,
|
||||
automation.CONF_SERVICE: 'test.automation'
|
||||
'trigger': {
|
||||
'platform': 'numeric_state',
|
||||
'entity_id': 'test.entity',
|
||||
'above': 10,
|
||||
},
|
||||
'action': {
|
||||
'execute_service': 'test.automation'
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
||||
|
@ -131,7 +129,6 @@ class TestAutomationNumericState(unittest.TestCase):
|
|||
self.hass.pool.block_till_done()
|
||||
self.assertEqual(1, len(self.calls))
|
||||
|
||||
|
||||
def test_if_not_fires_on_entity_change_above_to_above(self):
|
||||
# set initial state
|
||||
self.hass.states.set('test.entity', 11)
|
||||
|
@ -139,10 +136,14 @@ class TestAutomationNumericState(unittest.TestCase):
|
|||
|
||||
self.assertTrue(automation.setup(self.hass, {
|
||||
automation.DOMAIN: {
|
||||
CONF_PLATFORM: 'numeric_state',
|
||||
numeric_state.CONF_ENTITY_ID: 'test.entity',
|
||||
numeric_state.CONF_ABOVE: 10,
|
||||
automation.CONF_SERVICE: 'test.automation'
|
||||
'trigger': {
|
||||
'platform': 'numeric_state',
|
||||
'entity_id': 'test.entity',
|
||||
'above': 10,
|
||||
},
|
||||
'action': {
|
||||
'execute_service': 'test.automation'
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
||||
|
@ -154,11 +155,15 @@ class TestAutomationNumericState(unittest.TestCase):
|
|||
def test_if_fires_on_entity_change_below_range(self):
|
||||
self.assertTrue(automation.setup(self.hass, {
|
||||
automation.DOMAIN: {
|
||||
CONF_PLATFORM: 'numeric_state',
|
||||
numeric_state.CONF_ENTITY_ID: 'test.entity',
|
||||
numeric_state.CONF_ABOVE: 5,
|
||||
numeric_state.CONF_BELOW: 10,
|
||||
automation.CONF_SERVICE: 'test.automation'
|
||||
'trigger': {
|
||||
'platform': 'numeric_state',
|
||||
'entity_id': 'test.entity',
|
||||
'below': 10,
|
||||
'above': 5,
|
||||
},
|
||||
'action': {
|
||||
'execute_service': 'test.automation'
|
||||
}
|
||||
}
|
||||
}))
|
||||
# 9 is below 10
|
||||
|
@ -169,11 +174,15 @@ class TestAutomationNumericState(unittest.TestCase):
|
|||
def test_if_fires_on_entity_change_below_above_range(self):
|
||||
self.assertTrue(automation.setup(self.hass, {
|
||||
automation.DOMAIN: {
|
||||
CONF_PLATFORM: 'numeric_state',
|
||||
numeric_state.CONF_ENTITY_ID: 'test.entity',
|
||||
numeric_state.CONF_ABOVE: 5,
|
||||
numeric_state.CONF_BELOW: 10,
|
||||
automation.CONF_SERVICE: 'test.automation'
|
||||
'trigger': {
|
||||
'platform': 'numeric_state',
|
||||
'entity_id': 'test.entity',
|
||||
'below': 10,
|
||||
'above': 5,
|
||||
},
|
||||
'action': {
|
||||
'execute_service': 'test.automation'
|
||||
}
|
||||
}
|
||||
}))
|
||||
# 4 is below 5
|
||||
|
@ -187,11 +196,15 @@ class TestAutomationNumericState(unittest.TestCase):
|
|||
|
||||
self.assertTrue(automation.setup(self.hass, {
|
||||
automation.DOMAIN: {
|
||||
CONF_PLATFORM: 'numeric_state',
|
||||
numeric_state.CONF_ENTITY_ID: 'test.entity',
|
||||
numeric_state.CONF_ABOVE: 5,
|
||||
numeric_state.CONF_BELOW: 10,
|
||||
automation.CONF_SERVICE: 'test.automation'
|
||||
'trigger': {
|
||||
'platform': 'numeric_state',
|
||||
'entity_id': 'test.entity',
|
||||
'below': 10,
|
||||
'above': 5,
|
||||
},
|
||||
'action': {
|
||||
'execute_service': 'test.automation'
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
||||
|
@ -206,11 +219,15 @@ class TestAutomationNumericState(unittest.TestCase):
|
|||
|
||||
self.assertTrue(automation.setup(self.hass, {
|
||||
automation.DOMAIN: {
|
||||
CONF_PLATFORM: 'numeric_state',
|
||||
numeric_state.CONF_ENTITY_ID: 'test.entity',
|
||||
numeric_state.CONF_ABOVE: 5,
|
||||
numeric_state.CONF_BELOW: 10,
|
||||
automation.CONF_SERVICE: 'test.automation'
|
||||
'trigger': {
|
||||
'platform': 'numeric_state',
|
||||
'entity_id': 'test.entity',
|
||||
'below': 10,
|
||||
'above': 5,
|
||||
},
|
||||
'action': {
|
||||
'execute_service': 'test.automation'
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
||||
|
@ -222,10 +239,13 @@ class TestAutomationNumericState(unittest.TestCase):
|
|||
def test_if_not_fires_if_entity_not_match(self):
|
||||
self.assertTrue(automation.setup(self.hass, {
|
||||
automation.DOMAIN: {
|
||||
CONF_PLATFORM: 'numeric_state',
|
||||
numeric_state.CONF_ENTITY_ID: 'test.another_entity',
|
||||
numeric_state.CONF_ABOVE: 10,
|
||||
automation.CONF_SERVICE: 'test.automation'
|
||||
'trigger': {
|
||||
'platform': 'numeric_state',
|
||||
'entity_id': 'test.another_entity',
|
||||
},
|
||||
'action': {
|
||||
'execute_service': 'test.automation'
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
||||
|
@ -238,19 +258,23 @@ class TestAutomationNumericState(unittest.TestCase):
|
|||
test_state = 10
|
||||
automation.setup(self.hass, {
|
||||
automation.DOMAIN: {
|
||||
CONF_PLATFORM: 'event',
|
||||
event.CONF_EVENT_TYPE: 'test_event',
|
||||
automation.CONF_SERVICE: 'test.automation',
|
||||
automation.CONF_IF: [{
|
||||
CONF_PLATFORM: 'numeric_state',
|
||||
numeric_state.CONF_ENTITY_ID: entity_id,
|
||||
numeric_state.CONF_ABOVE: test_state,
|
||||
numeric_state.CONF_BELOW: test_state + 2,
|
||||
}]
|
||||
'trigger': {
|
||||
'platform': 'event',
|
||||
'event_type': 'test_event',
|
||||
},
|
||||
'condition': {
|
||||
'platform': 'numeric_state',
|
||||
'entity_id': entity_id,
|
||||
'above': test_state,
|
||||
'below': test_state + 2
|
||||
},
|
||||
'action': {
|
||||
'execute_service': 'test.automation'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.states.set(entity_id, test_state )
|
||||
self.hass.states.set(entity_id, test_state)
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.pool.block_till_done()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue