Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions homeassistant/helpers/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ def __iter__(self):
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)
Expand All @@ -213,6 +217,10 @@ def __iter__(self):
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."""
Expand Down
14 changes: 14 additions & 0 deletions tests/helpers/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'