From 4e3c8a8697e2db75d2b01b4712e98892f3bc375c Mon Sep 17 00:00:00 2001 From: pavoni Date: Sun, 6 Mar 2016 19:19:07 +0000 Subject: [PATCH] Fix noisy error on startup. Make callbacks code consistent. --- homeassistant/components/binary_sensor/template.py | 2 ++ homeassistant/components/sensor/template.py | 11 ++++++----- homeassistant/components/switch/template.py | 11 ++++++----- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/binary_sensor/template.py b/homeassistant/components/binary_sensor/template.py index f5a8899af96..901943d6671 100644 --- a/homeassistant/components/binary_sensor/template.py +++ b/homeassistant/components/binary_sensor/template.py @@ -90,6 +90,8 @@ class BinarySensorTemplate(BinarySensorDevice): hass.bus.listen(EVENT_STATE_CHANGED, self._event_listener) def _event_listener(self, event): + if not hasattr(self, 'hass'): + return self.update_ha_state(True) @property diff --git a/homeassistant/components/sensor/template.py b/homeassistant/components/sensor/template.py index 65df431d2b9..fb5060744eb 100644 --- a/homeassistant/components/sensor/template.py +++ b/homeassistant/components/sensor/template.py @@ -86,12 +86,13 @@ class SensorTemplate(Entity): self._unit_of_measurement = unit_of_measurement self._template = state_template self.update() + self.hass.bus.listen(EVENT_STATE_CHANGED, self._event_listener) - def _update_callback(_event): - """ Called when the target device changes state. """ - self.update_ha_state(True) - - self.hass.bus.listen(EVENT_STATE_CHANGED, _update_callback) + def _event_listener(self, event): + """ Called when the target device changes state. """ + if not hasattr(self, 'hass'): + return + self.update_ha_state(True) @property def name(self): diff --git a/homeassistant/components/switch/template.py b/homeassistant/components/switch/template.py index ec6cb7d2e8e..64d58b64fcd 100644 --- a/homeassistant/components/switch/template.py +++ b/homeassistant/components/switch/template.py @@ -100,12 +100,13 @@ class SwitchTemplate(SwitchDevice): self._on_action = on_action self._off_action = off_action self.update() + self.hass.bus.listen(EVENT_STATE_CHANGED, self._event_listener) - def _update_callback(_event): - """Called when the target device changes state.""" - self.update_ha_state(True) - - self.hass.bus.listen(EVENT_STATE_CHANGED, _update_callback) + def _event_listener(self, event): + """ Called when the target device changes state. """ + if not hasattr(self, 'hass'): + return + self.update_ha_state(True) @property def name(self):