Fix state automation configuration

This commit is contained in:
Paulus Schoutsen 2015-09-14 18:22:49 -07:00
parent bf64956265
commit 2fe8b154f1
2 changed files with 42 additions and 43 deletions

View File

@ -14,6 +14,7 @@ CONF_ENTITY_ID = "state_entity_id"
CONF_FROM = "state_from" CONF_FROM = "state_from"
CONF_TO = "state_to" CONF_TO = "state_to"
CONF_STATE = "state" CONF_STATE = "state"
CONF_IF_ENTITY_ID = "entity_id"
def trigger(hass, config, action): def trigger(hass, config, action):
@ -40,13 +41,13 @@ def trigger(hass, config, action):
def if_action(hass, config, action): def if_action(hass, config, action):
""" Wraps action method with state based condition. """ """ Wraps action method with state based condition. """
entity_id = config.get(CONF_ENTITY_ID) entity_id = config.get(CONF_IF_ENTITY_ID)
state = config.get(CONF_STATE) state = config.get(CONF_STATE)
if entity_id is None or state is None: if entity_id is None or state is None:
logging.getLogger(__name__).error( logging.getLogger(__name__).error(
"Missing if-condition configuration key %s or %s", CONF_ENTITY_ID, "Missing if-condition configuration key %s or %s",
CONF_STATE) CONF_IF_ENTITY_ID, CONF_STATE)
return action return action
def state_if(): def state_if():

View File

@ -8,8 +8,6 @@ import unittest
import homeassistant.core as ha import homeassistant.core as ha
import homeassistant.components.automation as automation import homeassistant.components.automation as automation
from homeassistant.components.automation import event, state
from homeassistant.const import CONF_PLATFORM
class TestAutomationState(unittest.TestCase): class TestAutomationState(unittest.TestCase):
@ -32,17 +30,17 @@ class TestAutomationState(unittest.TestCase):
def test_setup_fails_if_no_entity_id(self): def test_setup_fails_if_no_entity_id(self):
self.assertFalse(automation.setup(self.hass, { self.assertFalse(automation.setup(self.hass, {
automation.DOMAIN: { automation.DOMAIN: {
CONF_PLATFORM: 'state', 'platform': 'state',
automation.CONF_SERVICE: 'test.automation' 'execute_service': 'test.automation'
} }
})) }))
def test_if_fires_on_entity_change(self): def test_if_fires_on_entity_change(self):
self.assertTrue(automation.setup(self.hass, { self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: { automation.DOMAIN: {
CONF_PLATFORM: 'state', 'platform': 'state',
state.CONF_ENTITY_ID: 'test.entity', 'state_entity_id': 'test.entity',
automation.CONF_SERVICE: 'test.automation' 'execute_service': 'test.automation'
} }
})) }))
@ -53,10 +51,10 @@ class TestAutomationState(unittest.TestCase):
def test_if_fires_on_entity_change_with_from_filter(self): def test_if_fires_on_entity_change_with_from_filter(self):
self.assertTrue(automation.setup(self.hass, { self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: { automation.DOMAIN: {
CONF_PLATFORM: 'state', 'platform': 'state',
state.CONF_ENTITY_ID: 'test.entity', 'state_entity_id': 'test.entity',
state.CONF_FROM: 'hello', 'state_from': 'hello',
automation.CONF_SERVICE: 'test.automation' 'execute_service': 'test.automation'
} }
})) }))
@ -67,10 +65,10 @@ class TestAutomationState(unittest.TestCase):
def test_if_fires_on_entity_change_with_to_filter(self): def test_if_fires_on_entity_change_with_to_filter(self):
self.assertTrue(automation.setup(self.hass, { self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: { automation.DOMAIN: {
CONF_PLATFORM: 'state', 'platform': 'state',
state.CONF_ENTITY_ID: 'test.entity', 'state_entity_id': 'test.entity',
state.CONF_TO: 'world', 'state_to': 'world',
automation.CONF_SERVICE: 'test.automation' 'execute_service': 'test.automation'
} }
})) }))
@ -81,11 +79,11 @@ class TestAutomationState(unittest.TestCase):
def test_if_fires_on_entity_change_with_both_filters(self): def test_if_fires_on_entity_change_with_both_filters(self):
self.assertTrue(automation.setup(self.hass, { self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: { automation.DOMAIN: {
CONF_PLATFORM: 'state', 'platform': 'state',
state.CONF_ENTITY_ID: 'test.entity', 'state_entity_id': 'test.entity',
state.CONF_FROM: 'hello', 'state_from': 'hello',
state.CONF_TO: 'world', 'state_to': 'world',
automation.CONF_SERVICE: 'test.automation' 'execute_service': 'test.automation'
} }
})) }))
@ -96,11 +94,11 @@ class TestAutomationState(unittest.TestCase):
def test_if_not_fires_if_to_filter_not_match(self): def test_if_not_fires_if_to_filter_not_match(self):
self.assertTrue(automation.setup(self.hass, { self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: { automation.DOMAIN: {
CONF_PLATFORM: 'state', 'platform': 'state',
state.CONF_ENTITY_ID: 'test.entity', 'state_entity_id': 'test.entity',
state.CONF_FROM: 'hello', 'state_from': 'hello',
state.CONF_TO: 'world', 'state_to': 'world',
automation.CONF_SERVICE: 'test.automation' 'execute_service': 'test.automation'
} }
})) }))
@ -113,11 +111,11 @@ class TestAutomationState(unittest.TestCase):
self.assertTrue(automation.setup(self.hass, { self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: { automation.DOMAIN: {
CONF_PLATFORM: 'state', 'platform': 'state',
state.CONF_ENTITY_ID: 'test.entity', 'state_entity_id': 'test.entity',
state.CONF_FROM: 'hello', 'state_from': 'hello',
state.CONF_TO: 'world', 'state_to': 'world',
automation.CONF_SERVICE: 'test.automation' 'execute_service': 'test.automation'
} }
})) }))
@ -128,9 +126,9 @@ class TestAutomationState(unittest.TestCase):
def test_if_not_fires_if_entity_not_match(self): def test_if_not_fires_if_entity_not_match(self):
self.assertTrue(automation.setup(self.hass, { self.assertTrue(automation.setup(self.hass, {
automation.DOMAIN: { automation.DOMAIN: {
CONF_PLATFORM: 'state', 'platform': 'state',
state.CONF_ENTITY_ID: 'test.another_entity', 'state_entity_id': 'test.another_entity',
automation.CONF_SERVICE: 'test.automation' 'execute_service': 'test.automation'
} }
})) }))
@ -143,13 +141,13 @@ class TestAutomationState(unittest.TestCase):
test_state = 'new_state' test_state = 'new_state'
automation.setup(self.hass, { automation.setup(self.hass, {
automation.DOMAIN: { automation.DOMAIN: {
CONF_PLATFORM: 'event', 'platform': 'event',
event.CONF_EVENT_TYPE: 'test_event', 'event_type': 'test_event',
automation.CONF_SERVICE: 'test.automation', 'execute_service': 'test.automation',
automation.CONF_IF: [{ 'if': [{
CONF_PLATFORM: 'state', 'platform': 'state',
state.CONF_ENTITY_ID: entity_id, 'entity_id': entity_id,
state.CONF_STATE: test_state, 'state': test_state,
}] }]
} }
}) })