mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Allow getting number of available states in template (#9158)
This commit is contained in:
parent
c367021aa4
commit
5629157740
@ -187,6 +187,10 @@ class AllStates(object):
|
||||
sorted(self._hass.states.async_all(),
|
||||
key=lambda state: state.entity_id))
|
||||
|
||||
def __len__(self):
|
||||
"""Return number of states."""
|
||||
return len(self._hass.states.async_entity_ids())
|
||||
|
||||
def __call__(self, entity_id):
|
||||
"""Return the states."""
|
||||
state = self._hass.states.get(entity_id)
|
||||
@ -213,6 +217,10 @@ class DomainStates(object):
|
||||
if state.domain == self._domain),
|
||||
key=lambda state: state.entity_id))
|
||||
|
||||
def __len__(self):
|
||||
"""Return number of states."""
|
||||
return len(self._hass.states.async_entity_ids(self._domain))
|
||||
|
||||
|
||||
class TemplateState(State):
|
||||
"""Class to represent a state object in a template."""
|
||||
|
@ -782,3 +782,17 @@ def test_state_with_unit(hass):
|
||||
hass)
|
||||
|
||||
assert tpl.async_render() == ''
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def test_length_of_states(hass):
|
||||
"""Test fetching the length of states."""
|
||||
hass.states.async_set('sensor.test', '23')
|
||||
hass.states.async_set('sensor.test2', 'wow')
|
||||
hass.states.async_set('climate.test2', 'cooling')
|
||||
|
||||
tpl = template.Template('{{ states | length }}', hass)
|
||||
assert tpl.async_render() == '3'
|
||||
|
||||
tpl = template.Template('{{ states.sensor | length }}', hass)
|
||||
assert tpl.async_render() == '2'
|
||||
|
Loading…
x
Reference in New Issue
Block a user