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: 2 additions & 6 deletions homeassistant/components/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from itertools import groupby
import voluptuous as vol

from homeassistant.const import HTTP_BAD_REQUEST
from homeassistant.const import (
HTTP_BAD_REQUEST, CONF_DOMAINS, CONF_ENTITIES, CONF_EXCLUDE, CONF_INCLUDE)
import homeassistant.helpers.config_validation as cv
import homeassistant.util.dt as dt_util
from homeassistant.components import recorder, script
Expand All @@ -21,11 +22,6 @@
DOMAIN = 'history'
DEPENDENCIES = ['recorder', 'http']

CONF_EXCLUDE = 'exclude'
CONF_INCLUDE = 'include'
CONF_ENTITIES = 'entities'
CONF_DOMAINS = 'domains'

CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
CONF_EXCLUDE: vol.Schema({
Expand Down
44 changes: 38 additions & 6 deletions homeassistant/components/recorder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
import threading
import time
from datetime import timedelta, datetime
from typing import Any, Union, Optional, List
from typing import Any, Union, Optional, List, Dict

import voluptuous as vol

from homeassistant.core import HomeAssistant, callback
from homeassistant.const import (EVENT_HOMEASSISTANT_START,
EVENT_HOMEASSISTANT_STOP, EVENT_STATE_CHANGED,
EVENT_TIME_CHANGED, MATCH_ALL)
from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_DOMAIN, CONF_ENTITIES, CONF_EXCLUDE, CONF_DOMAINS,
CONF_INCLUDE, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP,
EVENT_STATE_CHANGED, EVENT_TIME_CHANGED, MATCH_ALL)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import track_point_in_utc_time
from homeassistant.helpers.typing import ConfigType, QueryType
Expand All @@ -44,6 +45,16 @@
vol.Optional(CONF_PURGE_DAYS):
vol.All(vol.Coerce(int), vol.Range(min=1)),
vol.Optional(CONF_DB_URL): cv.string,
vol.Optional(CONF_EXCLUDE, default={}): vol.Schema({
vol.Optional(CONF_ENTITIES, default=[]): cv.entity_ids,
vol.Optional(CONF_DOMAINS, default=[]):
vol.All(cv.ensure_list, [cv.string])
}),
vol.Optional(CONF_INCLUDE, default={}): vol.Schema({
vol.Optional(CONF_ENTITIES, default=[]): cv.entity_ids,
vol.Optional(CONF_DOMAINS, default=[]):
vol.All(cv.ensure_list, [cv.string])
})
})
}, extra=vol.ALLOW_EXTRA)

Expand Down Expand Up @@ -110,7 +121,10 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool:
db_url = DEFAULT_URL.format(
hass_config_path=hass.config.path(DEFAULT_DB_FILE))

_INSTANCE = Recorder(hass, purge_days=purge_days, uri=db_url)
include = config.get(DOMAIN, {}).get(CONF_INCLUDE, {})
exclude = config.get(DOMAIN, {}).get(CONF_EXCLUDE, {})
_INSTANCE = Recorder(hass, purge_days=purge_days, uri=db_url,
include=include, exclude=exclude)

return True

Expand Down Expand Up @@ -153,7 +167,8 @@ def log_error(e: Exception, retry_wait: Optional[float]=0,
class Recorder(threading.Thread):
"""A threaded recorder class."""

def __init__(self, hass: HomeAssistant, purge_days: int, uri: str) -> None:
def __init__(self, hass: HomeAssistant, purge_days: int, uri: str,
include: Dict, exclude: Dict) -> None:
"""Initialize the recorder."""
threading.Thread.__init__(self)

Expand All @@ -166,6 +181,11 @@ def __init__(self, hass: HomeAssistant, purge_days: int, uri: str) -> None:
self.engine = None # type: Any
self._run = None # type: Any

self.include = include.get(CONF_ENTITIES, []) + \
include.get(CONF_DOMAINS, [])
self.exclude = exclude.get(CONF_ENTITIES, []) + \
exclude.get(CONF_DOMAINS, [])

def start_recording(event):
"""Start recording."""
self.start()
Expand Down Expand Up @@ -210,6 +230,18 @@ def purge_ticker(event):
self.queue.task_done()
continue

entity_id = event.data.get(ATTR_ENTITY_ID)
domain = event.data.get(ATTR_DOMAIN)

if entity_id in self.exclude or domain in self.exclude:
self.queue.task_done()
continue

if (self.include and entity_id not in self.include and
domain not in self.include):
self.queue.task_done()
continue

dbevent = Events.from_event(event)
self._commit(dbevent)

Expand Down
4 changes: 4 additions & 0 deletions homeassistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,22 @@
CONF_DISARM_AFTER_TRIGGER = 'disarm_after_trigger'
CONF_DISCOVERY = 'discovery'
CONF_DISPLAY_OPTIONS = 'display_options'
CONF_DOMAINS = 'domains'
CONF_ELEVATION = 'elevation'
CONF_EMAIL = 'email'
CONF_ENTITIES = 'entities'
CONF_ENTITY_ID = 'entity_id'
CONF_ENTITY_NAMESPACE = 'entity_namespace'
CONF_EVENT = 'event'
CONF_EXCLUDE = 'exclude'
CONF_FILE_PATH = 'file_path'
CONF_FILENAME = 'filename'
CONF_FRIENDLY_NAME = 'friendly_name'
CONF_HEADERS = 'headers'
CONF_HOST = 'host'
CONF_HOSTS = 'hosts'
CONF_ICON = 'icon'
CONF_INCLUDE = 'include'
CONF_ID = 'id'
CONF_LATITUDE = 'latitude'
CONF_LONGITUDE = 'longitude'
Expand Down