core/homeassistant/components/history.py

200 lines
6.3 KiB
Python
Raw Normal View History

"""
Provide pre-made queries on top of the recorder component.
2015-10-25 14:04:37 +00:00
For more details about this component, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/history/
"""
2016-02-19 05:27:50 +00:00
from collections import defaultdict
from datetime import timedelta
2015-02-02 02:00:30 +00:00
from itertools import groupby
2016-02-19 05:27:50 +00:00
import homeassistant.util.dt as dt_util
from homeassistant.components import recorder, script
from homeassistant.components.frontend import register_built_in_panel
2016-05-14 07:58:36 +00:00
from homeassistant.components.http import HomeAssistantView
DOMAIN = 'history'
DEPENDENCIES = ['recorder', 'http']
SIGNIFICANT_DOMAINS = ('thermostat',)
2016-03-05 17:49:04 +00:00
IGNORE_DOMAINS = ('zone', 'scene',)
def last_5_states(entity_id):
2016-03-07 17:49:31 +00:00
"""Return the last 5 states for entity_id."""
2015-02-02 02:00:30 +00:00
entity_id = entity_id.lower()
states = recorder.get_model('States')
return recorder.execute(
recorder.query('States').filter(
(states.entity_id == entity_id) &
(states.last_changed == states.last_updated)
).order_by(states.state_id.desc()).limit(5))
def get_significant_states(start_time, end_time=None, entity_id=None):
2016-03-07 17:49:31 +00:00
"""
Return states changes during UTC period start_time - end_time.
Significant states are all states where there is a state change,
as well as all states from certain domains (for instance
thermostat so that we get current temperature in our graphs).
"""
states = recorder.get_model('States')
query = recorder.query('States').filter(
(states.domain.in_(SIGNIFICANT_DOMAINS) |
(states.last_changed == states.last_updated)) &
((~states.domain.in_(IGNORE_DOMAINS)) &
(states.last_updated > start_time)))
if end_time is not None:
query = query.filter(states.last_updated < end_time)
if entity_id is not None:
query = query.filter_by(entity_id=entity_id.lower())
states = (
state for state in recorder.execute(
query.order_by(states.entity_id, states.last_updated))
if _is_significant(state))
return states_to_json(states, start_time, entity_id)
2015-02-02 02:00:30 +00:00
def state_changes_during_period(start_time, end_time=None, entity_id=None):
2016-03-08 16:55:57 +00:00
"""Return states changes during UTC period start_time - end_time."""
states = recorder.get_model('States')
query = recorder.query('States').filter(
(states.last_changed == states.last_updated) &
(states.last_changed > start_time))
2015-02-02 02:00:30 +00:00
if end_time is not None:
query = query.filter(states.last_updated < end_time)
2015-02-02 02:00:30 +00:00
if entity_id is not None:
query = query.filter_by(entity_id=entity_id.lower())
2015-02-02 02:00:30 +00:00
states = recorder.execute(
query.order_by(states.entity_id, states.last_updated))
2015-02-02 02:00:30 +00:00
return states_to_json(states, start_time, entity_id)
def get_states(utc_point_in_time, entity_ids=None, run=None):
2016-03-08 16:55:57 +00:00
"""Return the states at a specific point in time."""
if run is None:
run = recorder.run_information(utc_point_in_time)
# History did not run before utc_point_in_time
if run is None:
return []
from sqlalchemy import and_, func
states = recorder.get_model('States')
most_recent_state_ids = recorder.query(
func.max(states.state_id).label('max_state_id')
).filter(
(states.created >= run.start) &
(states.created < utc_point_in_time)
)
if entity_ids is not None:
most_recent_state_ids = most_recent_state_ids.filter(
states.entity_id.in_(entity_ids))
most_recent_state_ids = most_recent_state_ids.group_by(
states.entity_id).subquery()
query = recorder.query('States').join(most_recent_state_ids, and_(
states.state_id == most_recent_state_ids.c.max_state_id))
return recorder.execute(query)
def states_to_json(states, start_time, entity_id):
2016-03-08 16:55:57 +00:00
"""Convert SQL results into JSON friendly data structure.
This takes our state list and turns it into a JSON friendly data
structure {'entity_id': [list of states], 'entity_id2': [list of states]}
We also need to go back and create a synthetic zero data point for
each list of states, otherwise our graphs won't start on the Y
axis correctly.
"""
result = defaultdict(list)
entity_ids = [entity_id] if entity_id is not None else None
# Get the states at the start time
for state in get_states(start_time, entity_ids):
state.last_changed = start_time
state.last_updated = start_time
result[state.entity_id].append(state)
# Append all changes to it
for entity_id, group in groupby(states, lambda state: state.entity_id):
result[entity_id].extend(group)
return result
def get_state(utc_point_in_time, entity_id, run=None):
2016-03-07 17:49:31 +00:00
"""Return a state at a specific point in time."""
states = get_states(utc_point_in_time, (entity_id,), run)
return states[0] if states else None
2015-02-02 02:00:30 +00:00
2015-04-07 08:01:23 +00:00
# pylint: disable=unused-argument
def setup(hass, config):
2016-03-08 16:55:57 +00:00
"""Setup the history hooks."""
2016-05-14 07:58:36 +00:00
hass.wsgi.register_view(Last5StatesView)
hass.wsgi.register_view(HistoryPeriodView)
register_built_in_panel(hass, 'history', 'History', 'mdi:poll-box')
2015-02-02 02:00:30 +00:00
return True
2016-05-14 07:58:36 +00:00
class Last5StatesView(HomeAssistantView):
"""Handle last 5 state view requests."""
2016-05-15 04:18:46 +00:00
url = '/api/history/entity/<entity:entity_id>/recent_states'
2016-05-14 07:58:36 +00:00
name = 'api:history:entity-recent-states'
def get(self, request, entity_id):
"""Retrieve last 5 states of entity."""
return self.json(last_5_states(entity_id))
2016-05-14 07:58:36 +00:00
class HistoryPeriodView(HomeAssistantView):
"""Handle history period requests."""
2015-02-02 02:00:30 +00:00
2016-05-14 07:58:36 +00:00
url = '/api/history/period'
2016-05-28 17:37:22 +00:00
name = 'api:history:view-period'
extra_urls = ['/api/history/period/<datetime:datetime>']
2015-02-02 02:00:30 +00:00
def get(self, request, datetime=None):
2016-05-14 07:58:36 +00:00
"""Return history over a period of time."""
2016-05-28 17:37:22 +00:00
one_day = timedelta(days=1)
if datetime:
start_time = dt_util.as_utc(datetime)
2016-05-14 07:58:36 +00:00
else:
start_time = dt_util.utcnow() - one_day
2016-05-14 07:58:36 +00:00
end_time = start_time + one_day
entity_id = request.args.get('filter_entity_id')
2015-02-02 02:00:30 +00:00
2016-05-14 07:58:36 +00:00
return self.json(
get_significant_states(start_time, end_time, entity_id).values())
def _is_significant(state):
2016-03-08 16:55:57 +00:00
"""Test if state is significant for history charts.
Will only test for things that are not filtered out in SQL.
"""
# scripts that are not cancellable will never change state
return (state.domain != 'script' or
state.attributes.get(script.ATTR_CAN_CANCEL))