Fix race condition in template components.
This commit is contained in:
parent
179c6efff7
commit
4e7160139e
4 changed files with 5 additions and 5 deletions
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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."""
|
||||
|
|
Loading…
Add table
Reference in a new issue