mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Streamlined child state lookups in universal media player
1) Removed children property because it was only being used by one method. 2) Removed option to return state as object from _entity_lkp as it was no longer needed. 3) Used hass.states.get to get entity state objects. 4) Revised test to remove children property.
This commit is contained in:
parent
8f3e8d29f0
commit
85d732a45a
@ -147,7 +147,7 @@ class UniversalMediaPlayer(MediaPlayerDevice):
|
|||||||
|
|
||||||
track_state_change(hass, self.dependencies, self.update_state)
|
track_state_change(hass, self.dependencies, self.update_state)
|
||||||
|
|
||||||
def _entity_lkp(self, entity_id=None, state_attr=None, state_as_obj=True):
|
def _entity_lkp(self, entity_id=None, state_attr=None):
|
||||||
""" Looks up an entity state from hass """
|
""" Looks up an entity state from hass """
|
||||||
if entity_id is None:
|
if entity_id is None:
|
||||||
return
|
return
|
||||||
@ -159,15 +159,12 @@ class UniversalMediaPlayer(MediaPlayerDevice):
|
|||||||
|
|
||||||
if state_attr:
|
if state_attr:
|
||||||
return state_obj.attributes.get(state_attr)
|
return state_obj.attributes.get(state_attr)
|
||||||
if state_as_obj:
|
|
||||||
return state_obj
|
|
||||||
return state_obj.state
|
return state_obj.state
|
||||||
|
|
||||||
def _override_or_child_attr(self, attr_name):
|
def _override_or_child_attr(self, attr_name):
|
||||||
""" returns either the override or the active child for attr_name """
|
""" returns either the override or the active child for attr_name """
|
||||||
if attr_name in self._attrs:
|
if attr_name in self._attrs:
|
||||||
return self._entity_lkp(*self._attrs[attr_name],
|
return self._entity_lkp(*self._attrs[attr_name])
|
||||||
state_as_obj=False)
|
|
||||||
|
|
||||||
return self._child_attr(attr_name)
|
return self._child_attr(attr_name)
|
||||||
|
|
||||||
@ -205,23 +202,16 @@ class UniversalMediaPlayer(MediaPlayerDevice):
|
|||||||
def master_state(self):
|
def master_state(self):
|
||||||
""" gets the master state from entity or none """
|
""" gets the master state from entity or none """
|
||||||
if CONF_STATE in self._attrs:
|
if CONF_STATE in self._attrs:
|
||||||
master_state = self._entity_lkp(*self._attrs[CONF_STATE],
|
master_state = self._entity_lkp(*self._attrs[CONF_STATE])
|
||||||
state_as_obj=False)
|
|
||||||
return master_state if master_state else STATE_OFF
|
return master_state if master_state else STATE_OFF
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
|
||||||
def children(self):
|
|
||||||
""" Gets children and their current states """
|
|
||||||
return {child: self._entity_lkp(child) for child in self._children}
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def active_child_state(self):
|
def active_child_state(self):
|
||||||
""" The state of the active child or None """
|
""" The state of the active child or None """
|
||||||
children = self.children
|
|
||||||
for child_name in self._children:
|
for child_name in self._children:
|
||||||
child_state = children[child_name]
|
child_state = self.hass.states.get(child_name)
|
||||||
if child_state and child_state.state not in OFF_STATES:
|
if child_state and child_state.state not in OFF_STATES:
|
||||||
return child_state
|
return child_state
|
||||||
|
|
||||||
|
@ -184,24 +184,6 @@ class TestMediaPlayer(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(STATE_OFF, ump.master_state)
|
self.assertEqual(STATE_OFF, ump.master_state)
|
||||||
|
|
||||||
def test_children(self):
|
|
||||||
""" test children property """
|
|
||||||
config = self.config_children_only
|
|
||||||
universal.validate_config(config)
|
|
||||||
|
|
||||||
ump = universal.UniversalMediaPlayer(self.hass, **config)
|
|
||||||
children = ump.children
|
|
||||||
|
|
||||||
check_children_ids = config['children']
|
|
||||||
check_children_ids.sort()
|
|
||||||
children_ids = list(children.keys())
|
|
||||||
children_ids.sort()
|
|
||||||
self.assertEqual(check_children_ids, children_ids)
|
|
||||||
|
|
||||||
check_children_states = [STATE_OFF, STATE_OFF]
|
|
||||||
children_states = [val.state for val in children.values()]
|
|
||||||
self.assertEqual(check_children_states, children_states)
|
|
||||||
|
|
||||||
def test_active_child_state(self):
|
def test_active_child_state(self):
|
||||||
""" test active child state property """
|
""" test active child state property """
|
||||||
config = self.config_children_only
|
config = self.config_children_only
|
||||||
|
Loading…
x
Reference in New Issue
Block a user