Clean up core (#10305)
* Clean up core * Lint * Fix tests * Address comment * Update entity.py * romve test for forward update to async_update * fix lint
This commit is contained in:
parent
9b8c64c8b6
commit
4e8e04fe66
7 changed files with 52 additions and 102 deletions
|
@ -387,7 +387,7 @@ class EventBus(object):
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_fire(self, event_type: str, event_data=None,
|
def async_fire(self, event_type: str, event_data=None,
|
||||||
origin=EventOrigin.local, wait=False):
|
origin=EventOrigin.local):
|
||||||
"""Fire an event.
|
"""Fire an event.
|
||||||
|
|
||||||
This method must be run in the event loop.
|
This method must be run in the event loop.
|
||||||
|
@ -395,8 +395,10 @@ class EventBus(object):
|
||||||
listeners = self._listeners.get(event_type, [])
|
listeners = self._listeners.get(event_type, [])
|
||||||
|
|
||||||
# EVENT_HOMEASSISTANT_CLOSE should go only to his listeners
|
# EVENT_HOMEASSISTANT_CLOSE should go only to his listeners
|
||||||
if event_type != EVENT_HOMEASSISTANT_CLOSE:
|
match_all_listeners = self._listeners.get(MATCH_ALL)
|
||||||
listeners = self._listeners.get(MATCH_ALL, []) + listeners
|
if (match_all_listeners is not None and
|
||||||
|
event_type != EVENT_HOMEASSISTANT_CLOSE):
|
||||||
|
listeners = match_all_listeners + listeners
|
||||||
|
|
||||||
event = Event(event_type, event_data, origin)
|
event = Event(event_type, event_data, origin)
|
||||||
|
|
||||||
|
@ -673,15 +675,6 @@ class StateMachine(object):
|
||||||
state_obj = self.get(entity_id)
|
state_obj = self.get(entity_id)
|
||||||
return state_obj is not None and state_obj.state == state
|
return state_obj is not None and state_obj.state == state
|
||||||
|
|
||||||
def is_state_attr(self, entity_id, name, value):
|
|
||||||
"""Test if entity exists and has a state attribute set to value.
|
|
||||||
|
|
||||||
Async friendly.
|
|
||||||
"""
|
|
||||||
state_obj = self.get(entity_id)
|
|
||||||
return state_obj is not None and \
|
|
||||||
state_obj.attributes.get(name, None) == value
|
|
||||||
|
|
||||||
def remove(self, entity_id):
|
def remove(self, entity_id):
|
||||||
"""Remove the state of an entity.
|
"""Remove the state of an entity.
|
||||||
|
|
||||||
|
|
|
@ -168,15 +168,9 @@ class Entity(object):
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Retrieve latest state.
|
"""Retrieve latest state.
|
||||||
|
|
||||||
When not implemented, will forward call to async version if available.
|
For asyncio use coroutine async_update.
|
||||||
"""
|
"""
|
||||||
async_update = getattr(self, 'async_update', None)
|
pass
|
||||||
|
|
||||||
if async_update is None:
|
|
||||||
return
|
|
||||||
|
|
||||||
# pylint: disable=not-callable
|
|
||||||
run_coroutine_threadsafe(async_update(), self.hass.loop).result()
|
|
||||||
|
|
||||||
# DO NOT OVERWRITE
|
# DO NOT OVERWRITE
|
||||||
# These properties and methods are either managed by Home Assistant or they
|
# These properties and methods are either managed by Home Assistant or they
|
||||||
|
|
|
@ -107,7 +107,8 @@ class Template(object):
|
||||||
|
|
||||||
This method must be run in the event loop.
|
This method must be run in the event loop.
|
||||||
"""
|
"""
|
||||||
self._ensure_compiled()
|
if self._compiled is None:
|
||||||
|
self._ensure_compiled()
|
||||||
|
|
||||||
if variables is not None:
|
if variables is not None:
|
||||||
kwargs.update(variables)
|
kwargs.update(variables)
|
||||||
|
@ -135,7 +136,8 @@ class Template(object):
|
||||||
|
|
||||||
This method must be run in the event loop.
|
This method must be run in the event loop.
|
||||||
"""
|
"""
|
||||||
self._ensure_compiled()
|
if self._compiled is None:
|
||||||
|
self._ensure_compiled()
|
||||||
|
|
||||||
variables = {
|
variables = {
|
||||||
'value': value
|
'value': value
|
||||||
|
@ -154,20 +156,17 @@ class Template(object):
|
||||||
|
|
||||||
def _ensure_compiled(self):
|
def _ensure_compiled(self):
|
||||||
"""Bind a template to a specific hass instance."""
|
"""Bind a template to a specific hass instance."""
|
||||||
if self._compiled is not None:
|
|
||||||
return
|
|
||||||
|
|
||||||
self.ensure_valid()
|
self.ensure_valid()
|
||||||
|
|
||||||
assert self.hass is not None, 'hass variable not set on template'
|
assert self.hass is not None, 'hass variable not set on template'
|
||||||
|
|
||||||
location_methods = LocationMethods(self.hass)
|
template_methods = TemplateMethods(self.hass)
|
||||||
|
|
||||||
global_vars = ENV.make_globals({
|
global_vars = ENV.make_globals({
|
||||||
'closest': location_methods.closest,
|
'closest': template_methods.closest,
|
||||||
'distance': location_methods.distance,
|
'distance': template_methods.distance,
|
||||||
'is_state': self.hass.states.is_state,
|
'is_state': self.hass.states.is_state,
|
||||||
'is_state_attr': self.hass.states.is_state_attr,
|
'is_state_attr': template_methods.is_state_attr,
|
||||||
'states': AllStates(self.hass),
|
'states': AllStates(self.hass),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -272,11 +271,11 @@ def _wrap_state(state):
|
||||||
return None if state is None else TemplateState(state)
|
return None if state is None else TemplateState(state)
|
||||||
|
|
||||||
|
|
||||||
class LocationMethods(object):
|
class TemplateMethods(object):
|
||||||
"""Class to expose distance helpers to templates."""
|
"""Class to expose helpers to templates."""
|
||||||
|
|
||||||
def __init__(self, hass):
|
def __init__(self, hass):
|
||||||
"""Initialize the distance helpers."""
|
"""Initialize the helpers."""
|
||||||
self._hass = hass
|
self._hass = hass
|
||||||
|
|
||||||
def closest(self, *args):
|
def closest(self, *args):
|
||||||
|
@ -390,6 +389,12 @@ class LocationMethods(object):
|
||||||
return self._hass.config.units.length(
|
return self._hass.config.units.length(
|
||||||
loc_util.distance(*locations[0] + locations[1]), 'm')
|
loc_util.distance(*locations[0] + locations[1]), 'm')
|
||||||
|
|
||||||
|
def is_state_attr(self, entity_id, name, value):
|
||||||
|
"""Test if a state is a specific attribute."""
|
||||||
|
state_obj = self._hass.states.get(entity_id)
|
||||||
|
return state_obj is not None and \
|
||||||
|
state_obj.attributes.get(name) == value
|
||||||
|
|
||||||
def _resolve_state(self, entity_id_or_state):
|
def _resolve_state(self, entity_id_or_state):
|
||||||
"""Return state or entity_id if given."""
|
"""Return state or entity_id if given."""
|
||||||
if isinstance(entity_id_or_state, State):
|
if isinstance(entity_id_or_state, State):
|
||||||
|
|
|
@ -72,10 +72,8 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
||||||
self.assertEqual(STATE_ALARM_PENDING,
|
self.assertEqual(STATE_ALARM_PENDING,
|
||||||
self.hass.states.get(entity_id).state)
|
self.hass.states.get(entity_id).state)
|
||||||
|
|
||||||
self.assertTrue(
|
state = self.hass.states.get(entity_id)
|
||||||
self.hass.states.is_state_attr(entity_id,
|
assert state.attributes['post_pending_state'] == STATE_ALARM_ARMED_HOME
|
||||||
'post_pending_state',
|
|
||||||
STATE_ALARM_ARMED_HOME))
|
|
||||||
|
|
||||||
future = dt_util.utcnow() + timedelta(seconds=1)
|
future = dt_util.utcnow() + timedelta(seconds=1)
|
||||||
with patch(('homeassistant.components.alarm_control_panel.manual.'
|
with patch(('homeassistant.components.alarm_control_panel.manual.'
|
||||||
|
@ -83,8 +81,8 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
||||||
fire_time_changed(self.hass, future)
|
fire_time_changed(self.hass, future)
|
||||||
self.hass.block_till_done()
|
self.hass.block_till_done()
|
||||||
|
|
||||||
self.assertEqual(STATE_ALARM_ARMED_HOME,
|
state = self.hass.states.get(entity_id)
|
||||||
self.hass.states.get(entity_id).state)
|
assert state.state == STATE_ALARM_ARMED_HOME
|
||||||
|
|
||||||
def test_arm_home_with_invalid_code(self):
|
def test_arm_home_with_invalid_code(self):
|
||||||
"""Attempt to arm home without a valid code."""
|
"""Attempt to arm home without a valid code."""
|
||||||
|
@ -155,10 +153,8 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
||||||
self.assertEqual(STATE_ALARM_PENDING,
|
self.assertEqual(STATE_ALARM_PENDING,
|
||||||
self.hass.states.get(entity_id).state)
|
self.hass.states.get(entity_id).state)
|
||||||
|
|
||||||
self.assertTrue(
|
state = self.hass.states.get(entity_id)
|
||||||
self.hass.states.is_state_attr(entity_id,
|
assert state.attributes['post_pending_state'] == STATE_ALARM_ARMED_AWAY
|
||||||
'post_pending_state',
|
|
||||||
STATE_ALARM_ARMED_AWAY))
|
|
||||||
|
|
||||||
future = dt_util.utcnow() + timedelta(seconds=1)
|
future = dt_util.utcnow() + timedelta(seconds=1)
|
||||||
with patch(('homeassistant.components.alarm_control_panel.manual.'
|
with patch(('homeassistant.components.alarm_control_panel.manual.'
|
||||||
|
@ -166,8 +162,8 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
||||||
fire_time_changed(self.hass, future)
|
fire_time_changed(self.hass, future)
|
||||||
self.hass.block_till_done()
|
self.hass.block_till_done()
|
||||||
|
|
||||||
self.assertEqual(STATE_ALARM_ARMED_AWAY,
|
state = self.hass.states.get(entity_id)
|
||||||
self.hass.states.get(entity_id).state)
|
assert state.state == STATE_ALARM_ARMED_AWAY
|
||||||
|
|
||||||
def test_arm_away_with_invalid_code(self):
|
def test_arm_away_with_invalid_code(self):
|
||||||
"""Attempt to arm away without a valid code."""
|
"""Attempt to arm away without a valid code."""
|
||||||
|
@ -238,10 +234,9 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
||||||
self.assertEqual(STATE_ALARM_PENDING,
|
self.assertEqual(STATE_ALARM_PENDING,
|
||||||
self.hass.states.get(entity_id).state)
|
self.hass.states.get(entity_id).state)
|
||||||
|
|
||||||
self.assertTrue(
|
state = self.hass.states.get(entity_id)
|
||||||
self.hass.states.is_state_attr(entity_id,
|
assert state.attributes['post_pending_state'] == \
|
||||||
'post_pending_state',
|
STATE_ALARM_ARMED_NIGHT
|
||||||
STATE_ALARM_ARMED_NIGHT))
|
|
||||||
|
|
||||||
future = dt_util.utcnow() + timedelta(seconds=1)
|
future = dt_util.utcnow() + timedelta(seconds=1)
|
||||||
with patch(('homeassistant.components.alarm_control_panel.manual.'
|
with patch(('homeassistant.components.alarm_control_panel.manual.'
|
||||||
|
@ -249,8 +244,8 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
||||||
fire_time_changed(self.hass, future)
|
fire_time_changed(self.hass, future)
|
||||||
self.hass.block_till_done()
|
self.hass.block_till_done()
|
||||||
|
|
||||||
self.assertEqual(STATE_ALARM_ARMED_NIGHT,
|
state = self.hass.states.get(entity_id)
|
||||||
self.hass.states.get(entity_id).state)
|
assert state.state == STATE_ALARM_ARMED_NIGHT
|
||||||
|
|
||||||
def test_arm_night_with_invalid_code(self):
|
def test_arm_night_with_invalid_code(self):
|
||||||
"""Attempt to night home without a valid code."""
|
"""Attempt to night home without a valid code."""
|
||||||
|
@ -329,10 +324,8 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
||||||
self.assertEqual(STATE_ALARM_PENDING,
|
self.assertEqual(STATE_ALARM_PENDING,
|
||||||
self.hass.states.get(entity_id).state)
|
self.hass.states.get(entity_id).state)
|
||||||
|
|
||||||
self.assertTrue(
|
state = self.hass.states.get(entity_id)
|
||||||
self.hass.states.is_state_attr(entity_id,
|
assert state.attributes['post_pending_state'] == STATE_ALARM_TRIGGERED
|
||||||
'post_pending_state',
|
|
||||||
STATE_ALARM_TRIGGERED))
|
|
||||||
|
|
||||||
future = dt_util.utcnow() + timedelta(seconds=2)
|
future = dt_util.utcnow() + timedelta(seconds=2)
|
||||||
with patch(('homeassistant.components.alarm_control_panel.manual.'
|
with patch(('homeassistant.components.alarm_control_panel.manual.'
|
||||||
|
@ -340,8 +333,8 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
||||||
fire_time_changed(self.hass, future)
|
fire_time_changed(self.hass, future)
|
||||||
self.hass.block_till_done()
|
self.hass.block_till_done()
|
||||||
|
|
||||||
self.assertEqual(STATE_ALARM_TRIGGERED,
|
state = self.hass.states.get(entity_id)
|
||||||
self.hass.states.get(entity_id).state)
|
assert state.state == STATE_ALARM_TRIGGERED
|
||||||
|
|
||||||
future = dt_util.utcnow() + timedelta(seconds=5)
|
future = dt_util.utcnow() + timedelta(seconds=5)
|
||||||
with patch(('homeassistant.components.alarm_control_panel.manual.'
|
with patch(('homeassistant.components.alarm_control_panel.manual.'
|
||||||
|
@ -349,8 +342,8 @@ class TestAlarmControlPanelManual(unittest.TestCase):
|
||||||
fire_time_changed(self.hass, future)
|
fire_time_changed(self.hass, future)
|
||||||
self.hass.block_till_done()
|
self.hass.block_till_done()
|
||||||
|
|
||||||
self.assertEqual(STATE_ALARM_DISARMED,
|
state = self.hass.states.get(entity_id)
|
||||||
self.hass.states.get(entity_id).state)
|
assert state.state == STATE_ALARM_DISARMED
|
||||||
|
|
||||||
def test_armed_home_with_specific_pending(self):
|
def test_armed_home_with_specific_pending(self):
|
||||||
"""Test arm home method."""
|
"""Test arm home method."""
|
||||||
|
|
|
@ -100,10 +100,8 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase):
|
||||||
self.assertEqual(STATE_ALARM_PENDING,
|
self.assertEqual(STATE_ALARM_PENDING,
|
||||||
self.hass.states.get(entity_id).state)
|
self.hass.states.get(entity_id).state)
|
||||||
|
|
||||||
self.assertTrue(
|
state = self.hass.states.get(entity_id)
|
||||||
self.hass.states.is_state_attr(entity_id,
|
assert state.attributes['post_pending_state'] == STATE_ALARM_ARMED_HOME
|
||||||
'post_pending_state',
|
|
||||||
STATE_ALARM_ARMED_HOME))
|
|
||||||
|
|
||||||
future = dt_util.utcnow() + timedelta(seconds=1)
|
future = dt_util.utcnow() + timedelta(seconds=1)
|
||||||
with patch(('homeassistant.components.alarm_control_panel.manual_mqtt.'
|
with patch(('homeassistant.components.alarm_control_panel.manual_mqtt.'
|
||||||
|
@ -189,10 +187,8 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase):
|
||||||
self.assertEqual(STATE_ALARM_PENDING,
|
self.assertEqual(STATE_ALARM_PENDING,
|
||||||
self.hass.states.get(entity_id).state)
|
self.hass.states.get(entity_id).state)
|
||||||
|
|
||||||
self.assertTrue(
|
state = self.hass.states.get(entity_id)
|
||||||
self.hass.states.is_state_attr(entity_id,
|
assert state.attributes['post_pending_state'] == STATE_ALARM_ARMED_AWAY
|
||||||
'post_pending_state',
|
|
||||||
STATE_ALARM_ARMED_AWAY))
|
|
||||||
|
|
||||||
future = dt_util.utcnow() + timedelta(seconds=1)
|
future = dt_util.utcnow() + timedelta(seconds=1)
|
||||||
with patch(('homeassistant.components.alarm_control_panel.manual_mqtt.'
|
with patch(('homeassistant.components.alarm_control_panel.manual_mqtt.'
|
||||||
|
@ -278,10 +274,9 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase):
|
||||||
self.assertEqual(STATE_ALARM_PENDING,
|
self.assertEqual(STATE_ALARM_PENDING,
|
||||||
self.hass.states.get(entity_id).state)
|
self.hass.states.get(entity_id).state)
|
||||||
|
|
||||||
self.assertTrue(
|
state = self.hass.states.get(entity_id)
|
||||||
self.hass.states.is_state_attr(entity_id,
|
assert state.attributes['post_pending_state'] == \
|
||||||
'post_pending_state',
|
STATE_ALARM_ARMED_NIGHT
|
||||||
STATE_ALARM_ARMED_NIGHT))
|
|
||||||
|
|
||||||
future = dt_util.utcnow() + timedelta(seconds=1)
|
future = dt_util.utcnow() + timedelta(seconds=1)
|
||||||
with patch(('homeassistant.components.alarm_control_panel.manual_mqtt.'
|
with patch(('homeassistant.components.alarm_control_panel.manual_mqtt.'
|
||||||
|
@ -375,10 +370,8 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase):
|
||||||
self.assertEqual(STATE_ALARM_PENDING,
|
self.assertEqual(STATE_ALARM_PENDING,
|
||||||
self.hass.states.get(entity_id).state)
|
self.hass.states.get(entity_id).state)
|
||||||
|
|
||||||
self.assertTrue(
|
state = self.hass.states.get(entity_id)
|
||||||
self.hass.states.is_state_attr(entity_id,
|
assert state.attributes['post_pending_state'] == STATE_ALARM_TRIGGERED
|
||||||
'post_pending_state',
|
|
||||||
STATE_ALARM_TRIGGERED))
|
|
||||||
|
|
||||||
future = dt_util.utcnow() + timedelta(seconds=2)
|
future = dt_util.utcnow() + timedelta(seconds=2)
|
||||||
with patch(('homeassistant.components.alarm_control_panel.manual_mqtt.'
|
with patch(('homeassistant.components.alarm_control_panel.manual_mqtt.'
|
||||||
|
|
|
@ -100,22 +100,6 @@ class TestHelpersEntity(object):
|
||||||
fmt, 'overwrite hidden true',
|
fmt, 'overwrite hidden true',
|
||||||
hass=self.hass) == 'test.overwrite_hidden_true_2'
|
hass=self.hass) == 'test.overwrite_hidden_true_2'
|
||||||
|
|
||||||
def test_update_calls_async_update_if_available(self):
|
|
||||||
"""Test async update getting called."""
|
|
||||||
async_update = []
|
|
||||||
|
|
||||||
class AsyncEntity(entity.Entity):
|
|
||||||
hass = self.hass
|
|
||||||
entity_id = 'sensor.test'
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
|
||||||
def async_update(self):
|
|
||||||
async_update.append([1])
|
|
||||||
|
|
||||||
ent = AsyncEntity()
|
|
||||||
ent.update()
|
|
||||||
assert len(async_update) == 1
|
|
||||||
|
|
||||||
def test_device_class(self):
|
def test_device_class(self):
|
||||||
"""Test device class attribute."""
|
"""Test device class attribute."""
|
||||||
state = self.hass.states.get(self.entity.entity_id)
|
state = self.hass.states.get(self.entity.entity_id)
|
||||||
|
|
|
@ -495,18 +495,6 @@ class TestStateMachine(unittest.TestCase):
|
||||||
self.assertFalse(self.states.is_state('light.Bowl', 'off'))
|
self.assertFalse(self.states.is_state('light.Bowl', 'off'))
|
||||||
self.assertFalse(self.states.is_state('light.Non_existing', 'on'))
|
self.assertFalse(self.states.is_state('light.Non_existing', 'on'))
|
||||||
|
|
||||||
def test_is_state_attr(self):
|
|
||||||
"""Test is_state_attr method."""
|
|
||||||
self.states.set("light.Bowl", "on", {"brightness": 100})
|
|
||||||
self.assertTrue(
|
|
||||||
self.states.is_state_attr('light.Bowl', 'brightness', 100))
|
|
||||||
self.assertFalse(
|
|
||||||
self.states.is_state_attr('light.Bowl', 'friendly_name', 200))
|
|
||||||
self.assertFalse(
|
|
||||||
self.states.is_state_attr('light.Bowl', 'friendly_name', 'Bowl'))
|
|
||||||
self.assertFalse(
|
|
||||||
self.states.is_state_attr('light.Non_existing', 'brightness', 100))
|
|
||||||
|
|
||||||
def test_entity_ids(self):
|
def test_entity_ids(self):
|
||||||
"""Test get_entity_ids method."""
|
"""Test get_entity_ids method."""
|
||||||
ent_ids = self.states.entity_ids()
|
ent_ids = self.states.entity_ids()
|
||||||
|
|
Loading…
Add table
Reference in a new issue