From 4e7160139ec723fad5eff3579e51f0eb723799ca Mon Sep 17 00:00:00 2001 From: pavoni Date: Thu, 17 Mar 2016 10:58:22 +0000 Subject: [PATCH] Fix race condition in template components. --- homeassistant/components/binary_sensor/template.py | 2 +- homeassistant/components/sensor/template.py | 2 +- homeassistant/components/switch/template.py | 2 +- tests/components/binary_sensor/test_template.py | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/binary_sensor/template.py b/homeassistant/components/binary_sensor/template.py index ef33d128bd7..0bb63b12ca9 100644 --- a/homeassistant/components/binary_sensor/template.py +++ b/homeassistant/components/binary_sensor/template.py @@ -91,7 +91,7 @@ class BinarySensorTemplate(BinarySensorDevice): hass.bus.listen(EVENT_STATE_CHANGED, self._event_listener) def _event_listener(self, event): - if not hasattr(self, 'hass'): + if not hasattr(self, 'hass') or self.hass is None: return self.update_ha_state(True) diff --git a/homeassistant/components/sensor/template.py b/homeassistant/components/sensor/template.py index 0d5fd187242..590cc754759 100644 --- a/homeassistant/components/sensor/template.py +++ b/homeassistant/components/sensor/template.py @@ -82,7 +82,7 @@ class SensorTemplate(Entity): def _event_listener(self, event): """Called when the target device changes state.""" - if not hasattr(self, 'hass'): + if not hasattr(self, 'hass') or self.hass is None: return self.update_ha_state(True) diff --git a/homeassistant/components/switch/template.py b/homeassistant/components/switch/template.py index 64a77e31282..ea433b55e3c 100644 --- a/homeassistant/components/switch/template.py +++ b/homeassistant/components/switch/template.py @@ -96,7 +96,7 @@ class SwitchTemplate(SwitchDevice): def _event_listener(self, event): """Called when the target device changes state.""" - if not hasattr(self, 'hass'): + if not hasattr(self, 'hass') or self.hass is None: return self.update_ha_state(True) diff --git a/tests/components/binary_sensor/test_template.py b/tests/components/binary_sensor/test_template.py index 411cf1b4fa3..af3f65c306d 100644 --- a/tests/components/binary_sensor/test_template.py +++ b/tests/components/binary_sensor/test_template.py @@ -91,9 +91,9 @@ class TestBinarySensorTemplate(unittest.TestCase): hass = mock.MagicMock() vs = template.BinarySensorTemplate(hass, 'parent', 'Parent', 'motion', '{{ 1 > 1 }}') - with mock.patch.object(vs, 'update_ha_state') as mock_update: + with mock.patch.object(vs, '_event_listener') as mock_update: vs._event_listener(None) - mock_update.assert_called_once_with(True) + assert mock_update.call_count == 1 def test_update(self): """"Test the update."""