diff --git a/homeassistant/components/binary_sensor/template.py b/homeassistant/components/binary_sensor/template.py index 8a3dba8dec9..ee68e817275 100644 --- a/homeassistant/components/binary_sensor/template.py +++ b/homeassistant/components/binary_sensor/template.py @@ -89,8 +89,7 @@ class BinarySensorTemplate(BinarySensorDevice): self.update() - def template_bsensor_state_listener(self, entity, old_state, - new_state): + def template_bsensor_state_listener(entity, old_state, new_state): """Called when the target device changes state.""" self.update_ha_state(True) diff --git a/homeassistant/components/sensor/template.py b/homeassistant/components/sensor/template.py index 804ed9502d9..668db816619 100644 --- a/homeassistant/components/sensor/template.py +++ b/homeassistant/components/sensor/template.py @@ -81,7 +81,7 @@ class SensorTemplate(Entity): self.update() - def template_sensor_state_listener(self, entity, old_state, new_state): + def template_sensor_state_listener(entity, old_state, new_state): """Called when the target device changes state.""" self.update_ha_state(True) diff --git a/homeassistant/components/switch/template.py b/homeassistant/components/switch/template.py index 4f5b9481c2f..6a28d46fc21 100644 --- a/homeassistant/components/switch/template.py +++ b/homeassistant/components/switch/template.py @@ -96,7 +96,7 @@ class SwitchTemplate(SwitchDevice): self.update() - def template_switch_state_listener(self, entity, old_state, new_state): + def template_switch_state_listener(entity, old_state, new_state): """Called when the target device changes state.""" self.update_ha_state(True) diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index aab1178d634..fe57c95b284 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -32,23 +32,23 @@ def track_state_change(hass, entity_ids, action, from_state=None, def state_change_listener(event): """The listener that listens for specific state changes.""" if entity_ids != MATCH_ALL and \ - event.data['entity_id'] not in entity_ids: + event.data.get('entity_id') not in entity_ids: return - if event.data['old_state'] is None: - old_state = None - else: + if event.data.get('old_state') is not None: old_state = event.data['old_state'].state - - if event.data['new_state'] is None: - new_state = None else: + old_state = None + + if event.data.get('new_state') is not None: new_state = event.data['new_state'].state + else: + new_state = None if _matcher(old_state, from_state) and _matcher(new_state, to_state): - action(event.data['entity_id'], - event.data['old_state'], - event.data['new_state']) + action(event.data.get('entity_id'), + event.data.get('old_state'), + event.data.get('new_state')) hass.bus.listen(EVENT_STATE_CHANGED, state_change_listener) diff --git a/tests/components/binary_sensor/test_template.py b/tests/components/binary_sensor/test_template.py index 262d011f90d..634834779d5 100644 --- a/tests/components/binary_sensor/test_template.py +++ b/tests/components/binary_sensor/test_template.py @@ -2,7 +2,7 @@ import unittest from unittest import mock -from homeassistant.const import EVENT_STATE_CHANGED +from homeassistant.const import EVENT_STATE_CHANGED, MATCH_ALL from homeassistant.components.binary_sensor import template from homeassistant.exceptions import TemplateError @@ -29,7 +29,7 @@ class TestBinarySensorTemplate(unittest.TestCase): result = template.setup_platform(hass, config, add_devices) self.assertTrue(result) mock_template.assert_called_once_with(hass, 'test', 'virtual thingy', - 'motion', '{{ foo }}', None) + 'motion', '{{ foo }}', MATCH_ALL) add_devices.assert_called_once_with([mock_template.return_value]) def test_setup_no_sensors(self): @@ -77,7 +77,7 @@ class TestBinarySensorTemplate(unittest.TestCase): """"Test the attributes.""" hass = mock.MagicMock() vs = template.BinarySensorTemplate(hass, 'parent', 'Parent', - 'motion', '{{ 1 > 1 }}', None) + 'motion', '{{ 1 > 1 }}', MATCH_ALL) self.assertFalse(vs.should_poll) self.assertEqual('motion', vs.sensor_class) self.assertEqual('Parent', vs.name) @@ -93,7 +93,7 @@ class TestBinarySensorTemplate(unittest.TestCase): """"Test the event.""" hass = get_test_home_assistant() vs = template.BinarySensorTemplate(hass, 'parent', 'Parent', - 'motion', '{{ 1 > 1 }}', None) + 'motion', '{{ 1 > 1 }}', MATCH_ALL) vs.update_ha_state() hass.pool.block_till_done() @@ -110,7 +110,7 @@ class TestBinarySensorTemplate(unittest.TestCase): """"Test the template update error.""" hass = mock.MagicMock() vs = template.BinarySensorTemplate(hass, 'parent', 'Parent', - 'motion', '{{ 1 > 1 }}', None) + 'motion', '{{ 1 > 1 }}', MATCH_ALL) mock_render.side_effect = TemplateError('foo') vs.update() mock_render.side_effect = TemplateError(