2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the logbook component."""
|
2017-02-21 07:40:27 +00:00
|
|
|
# pylint: disable=protected-access,invalid-name
|
2019-12-09 13:22:30 +00:00
|
|
|
from datetime import datetime, timedelta
|
2020-03-10 00:43:26 +00:00
|
|
|
from functools import partial
|
2017-02-21 07:40:27 +00:00
|
|
|
import logging
|
2016-09-30 19:57:24 +00:00
|
|
|
import unittest
|
2015-05-01 04:47:20 +00:00
|
|
|
|
2018-11-30 20:28:35 +00:00
|
|
|
import pytest
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-12-09 13:22:30 +00:00
|
|
|
from homeassistant.components import logbook, recorder, sun
|
|
|
|
from homeassistant.components.alexa.smart_home import EVENT_ALEXA_SMART_HOME
|
2015-05-01 04:47:20 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ENTITY_ID,
|
2019-12-09 13:22:30 +00:00
|
|
|
ATTR_HIDDEN,
|
2019-07-31 19:25:30 +00:00
|
|
|
EVENT_HOMEASSISTANT_START,
|
|
|
|
EVENT_HOMEASSISTANT_STOP,
|
2019-12-09 13:22:30 +00:00
|
|
|
EVENT_STATE_CHANGED,
|
2019-07-31 19:25:30 +00:00
|
|
|
STATE_NOT_HOME,
|
|
|
|
STATE_OFF,
|
2019-12-09 13:22:30 +00:00
|
|
|
STATE_ON,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-12-09 13:22:30 +00:00
|
|
|
import homeassistant.core as ha
|
|
|
|
from homeassistant.setup import async_setup_component, setup_component
|
2015-05-01 04:47:20 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2017-02-21 07:40:27 +00:00
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
from tests.async_mock import patch
|
2019-12-09 13:22:30 +00:00
|
|
|
from tests.common import get_test_home_assistant, init_recorder_component
|
2020-03-10 00:43:26 +00:00
|
|
|
from tests.components.recorder.common import trigger_db_commit
|
2017-02-21 07:40:27 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-05-01 04:59:24 +00:00
|
|
|
|
2015-05-01 04:47:20 +00:00
|
|
|
|
2016-05-14 07:58:36 +00:00
|
|
|
class TestComponentLogbook(unittest.TestCase):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test the History component."""
|
2015-05-01 04:47:20 +00:00
|
|
|
|
2016-09-30 19:57:24 +00:00
|
|
|
EMPTY_CONFIG = logbook.CONFIG_SCHEMA({logbook.DOMAIN: {}})
|
2016-09-21 03:07:26 +00:00
|
|
|
|
2016-01-30 21:14:10 +00:00
|
|
|
def setUp(self):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up things to be run when tests are started."""
|
2016-02-14 23:08:23 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
2017-02-21 07:40:27 +00:00
|
|
|
init_recorder_component(self.hass) # Force an in memory DB
|
2020-06-02 18:54:11 +00:00
|
|
|
with patch("homeassistant.components.http.start_http_server_and_save_config"):
|
|
|
|
assert setup_component(self.hass, logbook.DOMAIN, self.EMPTY_CONFIG)
|
2020-06-08 19:26:40 +00:00
|
|
|
self.addCleanup(self.hass.stop)
|
2016-01-30 21:14:10 +00:00
|
|
|
|
|
|
|
def test_service_call_create_logbook_entry(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if service call create log book entry."""
|
2016-01-30 21:14:10 +00:00
|
|
|
calls = []
|
|
|
|
|
2016-11-04 04:58:18 +00:00
|
|
|
@ha.callback
|
2016-01-30 21:14:10 +00:00
|
|
|
def event_listener(event):
|
2017-02-21 07:40:27 +00:00
|
|
|
"""Append on event."""
|
2016-01-30 21:14:10 +00:00
|
|
|
calls.append(event)
|
|
|
|
|
|
|
|
self.hass.bus.listen(logbook.EVENT_LOGBOOK_ENTRY, event_listener)
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.services.call(
|
|
|
|
logbook.DOMAIN,
|
|
|
|
"log",
|
|
|
|
{
|
|
|
|
logbook.ATTR_NAME: "Alarm",
|
|
|
|
logbook.ATTR_MESSAGE: "is triggered",
|
|
|
|
logbook.ATTR_DOMAIN: "switch",
|
|
|
|
logbook.ATTR_ENTITY_ID: "switch.test_switch",
|
|
|
|
},
|
|
|
|
True,
|
|
|
|
)
|
2016-01-30 21:14:10 +00:00
|
|
|
|
2016-10-04 05:39:27 +00:00
|
|
|
# Logbook entry service call results in firing an event.
|
|
|
|
# Our service call will unblock when the event listeners have been
|
|
|
|
# scheduled. This means that they may not have been processed yet.
|
|
|
|
self.hass.block_till_done()
|
2018-11-26 18:53:24 +00:00
|
|
|
self.hass.data[recorder.DATA_INSTANCE].block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
events = list(
|
|
|
|
logbook._get_events(
|
|
|
|
self.hass,
|
|
|
|
{},
|
|
|
|
dt_util.utcnow() - timedelta(hours=1),
|
|
|
|
dt_util.utcnow() + timedelta(hours=1),
|
|
|
|
)
|
|
|
|
)
|
2020-01-14 21:03:02 +00:00
|
|
|
assert len(events) == 1
|
2016-10-04 05:39:27 +00:00
|
|
|
|
2020-04-06 10:51:48 +00:00
|
|
|
assert len(calls) == 1
|
2016-01-30 21:14:10 +00:00
|
|
|
last_call = calls[-1]
|
|
|
|
|
2020-04-06 10:51:48 +00:00
|
|
|
assert last_call.data.get(logbook.ATTR_NAME) == "Alarm"
|
|
|
|
assert last_call.data.get(logbook.ATTR_MESSAGE) == "is triggered"
|
|
|
|
assert last_call.data.get(logbook.ATTR_DOMAIN) == "switch"
|
|
|
|
assert last_call.data.get(logbook.ATTR_ENTITY_ID) == "switch.test_switch"
|
2016-01-30 21:14:10 +00:00
|
|
|
|
|
|
|
def test_service_call_create_log_book_entry_no_message(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if service call create log book entry without message."""
|
2016-01-30 21:14:10 +00:00
|
|
|
calls = []
|
|
|
|
|
2016-11-04 04:58:18 +00:00
|
|
|
@ha.callback
|
2016-01-30 21:14:10 +00:00
|
|
|
def event_listener(event):
|
2017-02-21 07:40:27 +00:00
|
|
|
"""Append on event."""
|
2016-01-30 21:14:10 +00:00
|
|
|
calls.append(event)
|
|
|
|
|
|
|
|
self.hass.bus.listen(logbook.EVENT_LOGBOOK_ENTRY, event_listener)
|
2018-11-30 20:28:35 +00:00
|
|
|
|
|
|
|
with pytest.raises(vol.Invalid):
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass.services.call(logbook.DOMAIN, "log", {}, True)
|
2016-01-30 21:14:10 +00:00
|
|
|
|
2016-10-04 05:39:27 +00:00
|
|
|
# Logbook entry service call results in firing an event.
|
|
|
|
# Our service call will unblock when the event listeners have been
|
|
|
|
# scheduled. This means that they may not have been processed yet.
|
|
|
|
self.hass.block_till_done()
|
|
|
|
|
2020-04-06 10:51:48 +00:00
|
|
|
assert len(calls) == 0
|
2015-05-01 04:47:20 +00:00
|
|
|
|
|
|
|
def test_humanify_filter_sensor(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test humanify filter too frequent sensor values."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id = "sensor.bla"
|
2015-05-01 04:47:20 +00:00
|
|
|
|
2016-04-16 07:55:35 +00:00
|
|
|
pointA = dt_util.utcnow().replace(minute=2)
|
2015-05-01 04:47:20 +00:00
|
|
|
pointB = pointA.replace(minute=5)
|
|
|
|
pointC = pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)
|
|
|
|
|
|
|
|
eventA = self.create_state_changed_event(pointA, entity_id, 10)
|
|
|
|
eventB = self.create_state_changed_event(pointB, entity_id, 20)
|
|
|
|
eventC = self.create_state_changed_event(pointC, entity_id, 30)
|
|
|
|
|
2018-10-01 14:12:25 +00:00
|
|
|
entries = list(logbook.humanify(self.hass, (eventA, eventB, eventC)))
|
2015-05-01 04:47:20 +00:00
|
|
|
|
2020-04-06 10:51:48 +00:00
|
|
|
assert len(entries) == 2
|
2015-05-01 04:47:20 +00:00
|
|
|
self.assert_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
entries[0], pointB, "bla", domain="sensor", entity_id=entity_id
|
|
|
|
)
|
2015-05-01 04:47:20 +00:00
|
|
|
|
|
|
|
self.assert_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
entries[1], pointC, "bla", domain="sensor", entity_id=entity_id
|
|
|
|
)
|
2015-05-01 04:47:20 +00:00
|
|
|
|
2016-08-09 15:01:02 +00:00
|
|
|
def test_filter_continuous_sensor_values(self):
|
2016-08-10 15:07:50 +00:00
|
|
|
"""Test remove continuous sensor events from logbook."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id = "sensor.bla"
|
2016-08-09 15:01:02 +00:00
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"unit_of_measurement": "foo"}
|
|
|
|
eventA = self.create_state_changed_event(pointA, entity_id, 10, attributes)
|
2016-08-09 15:01:02 +00:00
|
|
|
|
2018-10-01 14:12:25 +00:00
|
|
|
entries = list(logbook.humanify(self.hass, (eventA,)))
|
2016-08-09 15:01:02 +00:00
|
|
|
|
2020-04-06 10:51:48 +00:00
|
|
|
assert len(entries) == 0
|
2016-08-09 15:01:02 +00:00
|
|
|
|
2017-01-20 07:30:47 +00:00
|
|
|
def test_exclude_new_entities(self):
|
|
|
|
"""Test if events are excluded on first update."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id = "sensor.bla"
|
|
|
|
entity_id2 = "sensor.blu"
|
2017-01-20 07:30:47 +00:00
|
|
|
pointA = dt_util.utcnow()
|
|
|
|
pointB = pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)
|
|
|
|
|
|
|
|
eventA = self.create_state_changed_event(pointA, entity_id, 10)
|
|
|
|
eventB = self.create_state_changed_event(pointB, entity_id2, 20)
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA.data["old_state"] = None
|
2017-01-20 07:30:47 +00:00
|
|
|
|
2019-03-02 07:23:45 +00:00
|
|
|
entities_filter = logbook._generate_filter_from_config({})
|
2019-07-31 19:25:30 +00:00
|
|
|
events = [
|
|
|
|
e
|
|
|
|
for e in (ha.Event(EVENT_HOMEASSISTANT_STOP), eventA, eventB)
|
2020-03-05 19:55:50 +00:00
|
|
|
if logbook._keep_event(self.hass, e, entities_filter)
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
2018-10-01 14:12:25 +00:00
|
|
|
entries = list(logbook.humanify(self.hass, events))
|
2017-01-20 07:30:47 +00:00
|
|
|
|
2020-04-06 10:51:48 +00:00
|
|
|
assert len(entries) == 2
|
2017-01-20 07:30:47 +00:00
|
|
|
self.assert_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
entries[0], name="Home Assistant", message="stopped", domain=ha.DOMAIN
|
|
|
|
)
|
2017-01-20 07:30:47 +00:00
|
|
|
self.assert_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
entries[1], pointB, "blu", domain="sensor", entity_id=entity_id2
|
|
|
|
)
|
2017-01-20 07:30:47 +00:00
|
|
|
|
|
|
|
def test_exclude_removed_entities(self):
|
|
|
|
"""Test if events are excluded on last update."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id = "sensor.bla"
|
|
|
|
entity_id2 = "sensor.blu"
|
2017-01-20 07:30:47 +00:00
|
|
|
pointA = dt_util.utcnow()
|
|
|
|
pointB = pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)
|
|
|
|
|
|
|
|
eventA = self.create_state_changed_event(pointA, entity_id, 10)
|
|
|
|
eventB = self.create_state_changed_event(pointB, entity_id2, 20)
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA.data["new_state"] = None
|
2017-01-20 07:30:47 +00:00
|
|
|
|
2019-03-02 07:23:45 +00:00
|
|
|
entities_filter = logbook._generate_filter_from_config({})
|
2019-07-31 19:25:30 +00:00
|
|
|
events = [
|
|
|
|
e
|
|
|
|
for e in (ha.Event(EVENT_HOMEASSISTANT_STOP), eventA, eventB)
|
2020-03-05 19:55:50 +00:00
|
|
|
if logbook._keep_event(self.hass, e, entities_filter)
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
2018-10-01 14:12:25 +00:00
|
|
|
entries = list(logbook.humanify(self.hass, events))
|
2017-01-20 07:30:47 +00:00
|
|
|
|
2020-04-06 10:51:48 +00:00
|
|
|
assert len(entries) == 2
|
2017-01-20 07:30:47 +00:00
|
|
|
self.assert_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
entries[0], name="Home Assistant", message="stopped", domain=ha.DOMAIN
|
|
|
|
)
|
2017-01-20 07:30:47 +00:00
|
|
|
self.assert_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
entries[1], pointB, "blu", domain="sensor", entity_id=entity_id2
|
|
|
|
)
|
2017-01-20 07:30:47 +00:00
|
|
|
|
2016-09-21 03:07:26 +00:00
|
|
|
def test_exclude_events_hidden(self):
|
|
|
|
"""Test if events are excluded if entity is hidden."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id = "sensor.bla"
|
|
|
|
entity_id2 = "sensor.blu"
|
2016-09-21 03:07:26 +00:00
|
|
|
pointA = dt_util.utcnow()
|
|
|
|
pointB = pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, entity_id, 10, {ATTR_HIDDEN: "true"}
|
|
|
|
)
|
2016-09-21 03:07:26 +00:00
|
|
|
eventB = self.create_state_changed_event(pointB, entity_id2, 20)
|
|
|
|
|
2019-03-02 07:23:45 +00:00
|
|
|
entities_filter = logbook._generate_filter_from_config({})
|
2019-07-31 19:25:30 +00:00
|
|
|
events = [
|
|
|
|
e
|
|
|
|
for e in (ha.Event(EVENT_HOMEASSISTANT_STOP), eventA, eventB)
|
2020-03-05 19:55:50 +00:00
|
|
|
if logbook._keep_event(self.hass, e, entities_filter)
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
2018-10-01 14:12:25 +00:00
|
|
|
entries = list(logbook.humanify(self.hass, events))
|
2016-09-21 03:07:26 +00:00
|
|
|
|
2020-04-06 10:51:48 +00:00
|
|
|
assert len(entries) == 2
|
2016-09-21 03:07:26 +00:00
|
|
|
self.assert_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
entries[0], name="Home Assistant", message="stopped", domain=ha.DOMAIN
|
|
|
|
)
|
2016-09-21 03:07:26 +00:00
|
|
|
self.assert_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
entries[1], pointB, "blu", domain="sensor", entity_id=entity_id2
|
|
|
|
)
|
2016-09-21 03:07:26 +00:00
|
|
|
|
|
|
|
def test_exclude_events_entity(self):
|
|
|
|
"""Test if events are filtered if entity is excluded in config."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id = "sensor.bla"
|
|
|
|
entity_id2 = "sensor.blu"
|
2016-09-21 03:07:26 +00:00
|
|
|
pointA = dt_util.utcnow()
|
|
|
|
pointB = pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)
|
|
|
|
|
|
|
|
eventA = self.create_state_changed_event(pointA, entity_id, 10)
|
|
|
|
eventB = self.create_state_changed_event(pointB, entity_id2, 20)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
config = logbook.CONFIG_SCHEMA(
|
|
|
|
{
|
|
|
|
ha.DOMAIN: {},
|
|
|
|
logbook.DOMAIN: {
|
|
|
|
logbook.CONF_EXCLUDE: {logbook.CONF_ENTITIES: [entity_id]}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
entities_filter = logbook._generate_filter_from_config(config[logbook.DOMAIN])
|
|
|
|
events = [
|
|
|
|
e
|
|
|
|
for e in (ha.Event(EVENT_HOMEASSISTANT_STOP), eventA, eventB)
|
2020-03-05 19:55:50 +00:00
|
|
|
if logbook._keep_event(self.hass, e, entities_filter)
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
2018-10-01 14:12:25 +00:00
|
|
|
entries = list(logbook.humanify(self.hass, events))
|
2016-09-21 03:07:26 +00:00
|
|
|
|
2020-04-06 10:51:48 +00:00
|
|
|
assert len(entries) == 2
|
2016-09-21 03:07:26 +00:00
|
|
|
self.assert_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
entries[0], name="Home Assistant", message="stopped", domain=ha.DOMAIN
|
|
|
|
)
|
2016-09-21 03:07:26 +00:00
|
|
|
self.assert_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
entries[1], pointB, "blu", domain="sensor", entity_id=entity_id2
|
|
|
|
)
|
2016-09-21 03:07:26 +00:00
|
|
|
|
|
|
|
def test_exclude_events_domain(self):
|
|
|
|
"""Test if events are filtered if domain is excluded in config."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id = "switch.bla"
|
|
|
|
entity_id2 = "sensor.blu"
|
2016-09-21 03:07:26 +00:00
|
|
|
pointA = dt_util.utcnow()
|
|
|
|
pointB = pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)
|
|
|
|
|
|
|
|
eventA = self.create_state_changed_event(pointA, entity_id, 10)
|
|
|
|
eventB = self.create_state_changed_event(pointB, entity_id2, 20)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
config = logbook.CONFIG_SCHEMA(
|
|
|
|
{
|
|
|
|
ha.DOMAIN: {},
|
|
|
|
logbook.DOMAIN: {
|
2020-04-21 00:48:09 +00:00
|
|
|
logbook.CONF_EXCLUDE: {logbook.CONF_DOMAINS: ["switch", "alexa"]}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
entities_filter = logbook._generate_filter_from_config(config[logbook.DOMAIN])
|
|
|
|
events = [
|
|
|
|
e
|
|
|
|
for e in (
|
|
|
|
ha.Event(EVENT_HOMEASSISTANT_START),
|
|
|
|
ha.Event(EVENT_ALEXA_SMART_HOME),
|
|
|
|
eventA,
|
|
|
|
eventB,
|
|
|
|
)
|
2020-03-05 19:55:50 +00:00
|
|
|
if logbook._keep_event(self.hass, e, entities_filter)
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
2018-10-01 14:12:25 +00:00
|
|
|
entries = list(logbook.humanify(self.hass, events))
|
2016-09-21 03:07:26 +00:00
|
|
|
|
2020-04-06 10:51:48 +00:00
|
|
|
assert len(entries) == 2
|
2019-07-31 19:25:30 +00:00
|
|
|
self.assert_entry(
|
|
|
|
entries[0], name="Home Assistant", message="started", domain=ha.DOMAIN
|
|
|
|
)
|
|
|
|
self.assert_entry(
|
|
|
|
entries[1], pointB, "blu", domain="sensor", entity_id=entity_id2
|
|
|
|
)
|
2016-09-21 03:07:26 +00:00
|
|
|
|
2016-10-08 18:26:14 +00:00
|
|
|
def test_include_events_entity(self):
|
|
|
|
"""Test if events are filtered if entity is included in config."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id = "sensor.bla"
|
|
|
|
entity_id2 = "sensor.blu"
|
2016-10-08 18:26:14 +00:00
|
|
|
pointA = dt_util.utcnow()
|
|
|
|
pointB = pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)
|
|
|
|
|
|
|
|
eventA = self.create_state_changed_event(pointA, entity_id, 10)
|
|
|
|
eventB = self.create_state_changed_event(pointB, entity_id2, 20)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
config = logbook.CONFIG_SCHEMA(
|
|
|
|
{
|
|
|
|
ha.DOMAIN: {},
|
|
|
|
logbook.DOMAIN: {
|
|
|
|
logbook.CONF_INCLUDE: {logbook.CONF_ENTITIES: [entity_id2]}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
entities_filter = logbook._generate_filter_from_config(config[logbook.DOMAIN])
|
|
|
|
events = [
|
|
|
|
e
|
|
|
|
for e in (ha.Event(EVENT_HOMEASSISTANT_STOP), eventA, eventB)
|
2020-03-05 19:55:50 +00:00
|
|
|
if logbook._keep_event(self.hass, e, entities_filter)
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
2018-10-01 14:12:25 +00:00
|
|
|
entries = list(logbook.humanify(self.hass, events))
|
2016-10-08 18:26:14 +00:00
|
|
|
|
2020-04-06 10:51:48 +00:00
|
|
|
assert len(entries) == 2
|
2016-10-08 18:26:14 +00:00
|
|
|
self.assert_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
entries[0], name="Home Assistant", message="stopped", domain=ha.DOMAIN
|
|
|
|
)
|
2016-10-08 18:26:14 +00:00
|
|
|
self.assert_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
entries[1], pointB, "blu", domain="sensor", entity_id=entity_id2
|
|
|
|
)
|
2016-10-08 18:26:14 +00:00
|
|
|
|
|
|
|
def test_include_events_domain(self):
|
|
|
|
"""Test if events are filtered if domain is included in config."""
|
2020-03-05 19:55:50 +00:00
|
|
|
assert setup_component(self.hass, "alexa", {})
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id = "switch.bla"
|
|
|
|
entity_id2 = "sensor.blu"
|
2016-10-08 18:26:14 +00:00
|
|
|
pointA = dt_util.utcnow()
|
|
|
|
pointB = pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
event_alexa = ha.Event(
|
|
|
|
EVENT_ALEXA_SMART_HOME,
|
|
|
|
{"request": {"namespace": "Alexa.Discovery", "name": "Discover"}},
|
|
|
|
)
|
2018-11-29 19:16:39 +00:00
|
|
|
|
2016-10-08 18:26:14 +00:00
|
|
|
eventA = self.create_state_changed_event(pointA, entity_id, 10)
|
|
|
|
eventB = self.create_state_changed_event(pointB, entity_id2, 20)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
config = logbook.CONFIG_SCHEMA(
|
|
|
|
{
|
|
|
|
ha.DOMAIN: {},
|
|
|
|
logbook.DOMAIN: {
|
2020-04-21 00:48:09 +00:00
|
|
|
logbook.CONF_INCLUDE: {logbook.CONF_DOMAINS: ["sensor", "alexa"]}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
entities_filter = logbook._generate_filter_from_config(config[logbook.DOMAIN])
|
|
|
|
events = [
|
|
|
|
e
|
2020-04-21 00:48:09 +00:00
|
|
|
for e in (ha.Event(EVENT_HOMEASSISTANT_START), event_alexa, eventA, eventB,)
|
2020-03-05 19:55:50 +00:00
|
|
|
if logbook._keep_event(self.hass, e, entities_filter)
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
2018-10-01 14:12:25 +00:00
|
|
|
entries = list(logbook.humanify(self.hass, events))
|
2016-10-08 18:26:14 +00:00
|
|
|
|
2020-04-21 00:48:09 +00:00
|
|
|
assert len(entries) == 3
|
2019-07-31 19:25:30 +00:00
|
|
|
self.assert_entry(
|
|
|
|
entries[0], name="Home Assistant", message="started", domain=ha.DOMAIN
|
|
|
|
)
|
|
|
|
self.assert_entry(entries[1], name="Amazon Alexa", domain="alexa")
|
|
|
|
self.assert_entry(
|
2020-04-21 00:48:09 +00:00
|
|
|
entries[2], pointB, "blu", domain="sensor", entity_id=entity_id2
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-10-08 18:26:14 +00:00
|
|
|
|
|
|
|
def test_include_exclude_events(self):
|
|
|
|
"""Test if events are filtered if include and exclude is configured."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id = "switch.bla"
|
|
|
|
entity_id2 = "sensor.blu"
|
|
|
|
entity_id3 = "sensor.bli"
|
2016-10-08 18:26:14 +00:00
|
|
|
pointA = dt_util.utcnow()
|
|
|
|
pointB = pointA + timedelta(minutes=logbook.GROUP_BY_MINUTES)
|
|
|
|
|
|
|
|
eventA1 = self.create_state_changed_event(pointA, entity_id, 10)
|
|
|
|
eventA2 = self.create_state_changed_event(pointA, entity_id2, 10)
|
|
|
|
eventA3 = self.create_state_changed_event(pointA, entity_id3, 10)
|
|
|
|
eventB1 = self.create_state_changed_event(pointB, entity_id, 20)
|
|
|
|
eventB2 = self.create_state_changed_event(pointB, entity_id2, 20)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
config = logbook.CONFIG_SCHEMA(
|
|
|
|
{
|
|
|
|
ha.DOMAIN: {},
|
|
|
|
logbook.DOMAIN: {
|
|
|
|
logbook.CONF_INCLUDE: {
|
|
|
|
logbook.CONF_DOMAINS: ["sensor"],
|
|
|
|
logbook.CONF_ENTITIES: ["switch.bla"],
|
|
|
|
},
|
|
|
|
logbook.CONF_EXCLUDE: {
|
|
|
|
logbook.CONF_DOMAINS: ["switch"],
|
|
|
|
logbook.CONF_ENTITIES: ["sensor.bli"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
entities_filter = logbook._generate_filter_from_config(config[logbook.DOMAIN])
|
|
|
|
events = [
|
|
|
|
e
|
|
|
|
for e in (
|
|
|
|
ha.Event(EVENT_HOMEASSISTANT_START),
|
|
|
|
eventA1,
|
|
|
|
eventA2,
|
|
|
|
eventA3,
|
|
|
|
eventB1,
|
|
|
|
eventB2,
|
|
|
|
)
|
2020-03-05 19:55:50 +00:00
|
|
|
if logbook._keep_event(self.hass, e, entities_filter)
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
2018-10-01 14:12:25 +00:00
|
|
|
entries = list(logbook.humanify(self.hass, events))
|
2016-10-08 18:26:14 +00:00
|
|
|
|
2020-04-06 10:51:48 +00:00
|
|
|
assert len(entries) == 5
|
2019-07-31 19:25:30 +00:00
|
|
|
self.assert_entry(
|
|
|
|
entries[0], name="Home Assistant", message="started", domain=ha.DOMAIN
|
|
|
|
)
|
|
|
|
self.assert_entry(
|
|
|
|
entries[1], pointA, "bla", domain="switch", entity_id=entity_id
|
|
|
|
)
|
|
|
|
self.assert_entry(
|
|
|
|
entries[2], pointA, "blu", domain="sensor", entity_id=entity_id2
|
|
|
|
)
|
|
|
|
self.assert_entry(
|
|
|
|
entries[3], pointB, "bla", domain="switch", entity_id=entity_id
|
|
|
|
)
|
|
|
|
self.assert_entry(
|
|
|
|
entries[4], pointB, "blu", domain="sensor", entity_id=entity_id2
|
|
|
|
)
|
2016-10-08 18:26:14 +00:00
|
|
|
|
2016-09-21 03:07:26 +00:00
|
|
|
def test_exclude_auto_groups(self):
|
|
|
|
"""Test if events of automatically generated groups are filtered."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id = "switch.bla"
|
|
|
|
entity_id2 = "group.switches"
|
2016-09-21 03:07:26 +00:00
|
|
|
pointA = dt_util.utcnow()
|
|
|
|
|
|
|
|
eventA = self.create_state_changed_event(pointA, entity_id, 10)
|
2019-07-31 19:25:30 +00:00
|
|
|
eventB = self.create_state_changed_event(pointA, entity_id2, 20, {"auto": True})
|
2016-09-21 03:07:26 +00:00
|
|
|
|
2019-03-02 07:23:45 +00:00
|
|
|
entities_filter = logbook._generate_filter_from_config({})
|
2019-07-31 19:25:30 +00:00
|
|
|
events = [
|
2020-03-05 19:55:50 +00:00
|
|
|
e
|
|
|
|
for e in (eventA, eventB)
|
|
|
|
if logbook._keep_event(self.hass, e, entities_filter)
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
2018-10-01 14:12:25 +00:00
|
|
|
entries = list(logbook.humanify(self.hass, events))
|
2016-09-21 03:07:26 +00:00
|
|
|
|
2020-04-06 10:51:48 +00:00
|
|
|
assert len(entries) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
self.assert_entry(
|
|
|
|
entries[0], pointA, "bla", domain="switch", entity_id=entity_id
|
|
|
|
)
|
2016-09-21 03:07:26 +00:00
|
|
|
|
|
|
|
def test_exclude_attribute_changes(self):
|
|
|
|
"""Test if events of attribute changes are filtered."""
|
|
|
|
pointA = dt_util.utcnow()
|
|
|
|
pointB = pointA + timedelta(minutes=1)
|
2020-03-08 04:27:51 +00:00
|
|
|
pointC = pointB + timedelta(minutes=1)
|
2016-09-21 03:07:26 +00:00
|
|
|
|
2020-03-08 04:27:51 +00:00
|
|
|
state_off = ha.State("light.kitchen", "off", {}, pointA, pointA).as_dict()
|
|
|
|
state_100 = ha.State(
|
|
|
|
"light.kitchen", "on", {"brightness": 100}, pointB, pointB
|
|
|
|
).as_dict()
|
|
|
|
state_200 = ha.State(
|
|
|
|
"light.kitchen", "on", {"brightness": 200}, pointB, pointC
|
|
|
|
).as_dict()
|
|
|
|
|
|
|
|
eventA = ha.Event(
|
|
|
|
EVENT_STATE_CHANGED,
|
|
|
|
{
|
|
|
|
"entity_id": "light.kitchen",
|
|
|
|
"old_state": state_off,
|
|
|
|
"new_state": state_100,
|
|
|
|
},
|
|
|
|
time_fired=pointB,
|
|
|
|
)
|
|
|
|
eventB = ha.Event(
|
|
|
|
EVENT_STATE_CHANGED,
|
|
|
|
{
|
|
|
|
"entity_id": "light.kitchen",
|
|
|
|
"old_state": state_100,
|
|
|
|
"new_state": state_200,
|
|
|
|
},
|
|
|
|
time_fired=pointC,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-09-21 03:07:26 +00:00
|
|
|
|
2019-03-02 07:23:45 +00:00
|
|
|
entities_filter = logbook._generate_filter_from_config({})
|
2019-07-31 19:25:30 +00:00
|
|
|
events = [
|
2020-03-05 19:55:50 +00:00
|
|
|
e
|
|
|
|
for e in (eventA, eventB)
|
|
|
|
if logbook._keep_event(self.hass, e, entities_filter)
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
2018-10-01 14:12:25 +00:00
|
|
|
entries = list(logbook.humanify(self.hass, events))
|
2016-09-21 03:07:26 +00:00
|
|
|
|
2020-04-06 10:51:48 +00:00
|
|
|
assert len(entries) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
self.assert_entry(
|
2020-03-08 04:27:51 +00:00
|
|
|
entries[0], pointB, "kitchen", domain="light", entity_id="light.kitchen"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-09-21 03:07:26 +00:00
|
|
|
|
2015-05-01 04:47:20 +00:00
|
|
|
def test_home_assistant_start_stop_grouped(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if HA start and stop events are grouped.
|
|
|
|
|
2017-09-23 15:15:46 +00:00
|
|
|
Events that are occurring in the same minute.
|
2016-03-09 09:25:50 +00:00
|
|
|
"""
|
2019-07-31 19:25:30 +00:00
|
|
|
entries = list(
|
|
|
|
logbook.humanify(
|
|
|
|
self.hass,
|
|
|
|
(
|
|
|
|
ha.Event(EVENT_HOMEASSISTANT_STOP),
|
|
|
|
ha.Event(EVENT_HOMEASSISTANT_START),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
2015-05-01 04:47:20 +00:00
|
|
|
|
2020-04-06 10:51:48 +00:00
|
|
|
assert len(entries) == 1
|
2015-05-01 04:47:20 +00:00
|
|
|
self.assert_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
entries[0], name="Home Assistant", message="restarted", domain=ha.DOMAIN
|
|
|
|
)
|
2015-05-01 04:47:20 +00:00
|
|
|
|
2016-09-21 03:07:26 +00:00
|
|
|
def test_home_assistant_start(self):
|
|
|
|
"""Test if HA start is not filtered or converted into a restart."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id = "switch.bla"
|
2016-09-21 03:07:26 +00:00
|
|
|
pointA = dt_util.utcnow()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
entries = list(
|
|
|
|
logbook.humanify(
|
|
|
|
self.hass,
|
|
|
|
(
|
|
|
|
ha.Event(EVENT_HOMEASSISTANT_START),
|
|
|
|
self.create_state_changed_event(pointA, entity_id, 10),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
2016-09-21 03:07:26 +00:00
|
|
|
|
2020-04-06 10:51:48 +00:00
|
|
|
assert len(entries) == 2
|
2016-09-21 03:07:26 +00:00
|
|
|
self.assert_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
entries[0], name="Home Assistant", message="started", domain=ha.DOMAIN
|
|
|
|
)
|
|
|
|
self.assert_entry(
|
|
|
|
entries[1], pointA, "bla", domain="switch", entity_id=entity_id
|
|
|
|
)
|
2016-09-21 03:07:26 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_device(self):
|
|
|
|
"""Test if logbook message is correctly created for switches.
|
|
|
|
|
|
|
|
Especially test if the special handling for turn on/off events is done.
|
|
|
|
"""
|
|
|
|
pointA = dt_util.utcnow()
|
|
|
|
|
|
|
|
# message for a device state change
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(pointA, "switch.bla", 10)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2016-09-21 03:07:26 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "changed to 10"
|
2016-09-21 03:07:26 +00:00
|
|
|
|
|
|
|
# message for a switch turned on
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(pointA, "switch.bla", STATE_ON)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2016-09-21 03:07:26 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "turned on"
|
2016-09-21 03:07:26 +00:00
|
|
|
|
|
|
|
# message for a switch turned off
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(pointA, "switch.bla", STATE_OFF)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2016-09-21 03:07:26 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "turned off"
|
2016-09-21 03:07:26 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_device_tracker(self):
|
|
|
|
"""Test if logbook message is correctly created for device tracker."""
|
|
|
|
pointA = dt_util.utcnow()
|
|
|
|
|
|
|
|
# message for a device tracker "not home" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "device_tracker.john", STATE_NOT_HOME
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2016-09-21 03:07:26 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is away"
|
2016-09-21 03:07:26 +00:00
|
|
|
|
|
|
|
# message for a device tracker "home" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(pointA, "device_tracker.john", "work")
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2016-09-21 03:07:26 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is at work"
|
2016-09-21 03:07:26 +00:00
|
|
|
|
2019-07-10 22:56:41 +00:00
|
|
|
def test_entry_message_from_state_person(self):
|
|
|
|
"""Test if logbook message is correctly created for a person."""
|
|
|
|
pointA = dt_util.utcnow()
|
|
|
|
|
|
|
|
# message for a device tracker "not home" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(pointA, "person.john", STATE_NOT_HOME)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is away"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a device tracker "home" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(pointA, "person.john", "work")
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is at work"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
2016-09-21 03:07:26 +00:00
|
|
|
def test_entry_message_from_state_sun(self):
|
|
|
|
"""Test if logbook message is correctly created for sun."""
|
|
|
|
pointA = dt_util.utcnow()
|
|
|
|
|
|
|
|
# message for a sun rise
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "sun.sun", sun.STATE_ABOVE_HORIZON
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2016-09-21 03:07:26 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "has risen"
|
2016-09-21 03:07:26 +00:00
|
|
|
|
|
|
|
# message for a sun set
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "sun.sun", sun.STATE_BELOW_HORIZON
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2016-09-21 03:07:26 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "has set"
|
2016-09-21 03:07:26 +00:00
|
|
|
|
2019-07-10 22:56:41 +00:00
|
|
|
def test_entry_message_from_state_binary_sensor_battery(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "battery"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor battery "low" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.battery", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is low"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor battery "normal" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.battery", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is normal"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_connectivity(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "connectivity"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor connectivity "connected" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.connectivity", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is connected"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor connectivity "disconnected" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.connectivity", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is disconnected"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_door(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "door"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor door "open" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.door", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is opened"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor door "closed" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.door", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is closed"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_garage_door(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "garage_door"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor garage_door "open" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.garage_door", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is opened"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor garage_door "closed" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.garage_door", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is closed"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_opening(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "opening"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor opening "open" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.opening", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is opened"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor opening "closed" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.opening", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is closed"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_window(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "window"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor window "open" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.window", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is opened"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor window "closed" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.window", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is closed"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_lock(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "lock"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor lock "unlocked" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.lock", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is unlocked"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor lock "locked" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.lock", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is locked"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_plug(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "plug"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor plug "unpluged" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.plug", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is plugged in"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor plug "pluged" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.plug", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is unplugged"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_presence(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "presence"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor presence "home" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.presence", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is at home"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor presence "away" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.presence", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is away"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_safety(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "safety"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor safety "unsafe" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.safety", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is unsafe"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor safety "safe" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.safety", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "is safe"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_cold(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "cold"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor cold "detected" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.cold", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "detected cold"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensori cold "cleared" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.cold", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "cleared (no cold detected)"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_gas(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "gas"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor gas "detected" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.gas", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "detected gas"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensori gas "cleared" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.gas", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "cleared (no gas detected)"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_heat(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "heat"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor heat "detected" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.heat", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "detected heat"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensori heat "cleared" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.heat", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "cleared (no heat detected)"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_light(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "light"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor light "detected" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.light", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "detected light"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensori light "cleared" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.light", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "cleared (no light detected)"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_moisture(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "moisture"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor moisture "detected" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.moisture", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "detected moisture"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensori moisture "cleared" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.moisture", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "cleared (no moisture detected)"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_motion(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "motion"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor motion "detected" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.motion", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "detected motion"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensori motion "cleared" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.motion", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "cleared (no motion detected)"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_occupancy(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "occupancy"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor occupancy "detected" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.occupancy", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "detected occupancy"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensori occupancy "cleared" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.occupancy", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "cleared (no occupancy detected)"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_power(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "power"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor power "detected" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.power", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "detected power"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensori power "cleared" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.power", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "cleared (no power detected)"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_problem(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "problem"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor problem "detected" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.problem", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "detected problem"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensori problem "cleared" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.problem", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "cleared (no problem detected)"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_smoke(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "smoke"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor smoke "detected" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.smoke", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "detected smoke"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensori smoke "cleared" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.smoke", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "cleared (no smoke detected)"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_sound(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "sound"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor sound "detected" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.sound", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "detected sound"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensori sound "cleared" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.sound", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "cleared (no sound detected)"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
def test_entry_message_from_state_binary_sensor_vibration(self):
|
|
|
|
"""Test if logbook message is correctly created for a binary_sensor."""
|
|
|
|
pointA = dt_util.utcnow()
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes = {"device_class": "vibration"}
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensor vibration "detected" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.vibration", STATE_ON, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "detected vibration"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
|
|
|
# message for a binary_sensori vibration "cleared" state
|
2019-07-31 19:25:30 +00:00
|
|
|
eventA = self.create_state_changed_event(
|
|
|
|
pointA, "binary_sensor.vibration", STATE_OFF, attributes
|
|
|
|
)
|
|
|
|
to_state = ha.State.from_dict(eventA.data.get("new_state"))
|
2019-07-10 22:56:41 +00:00
|
|
|
message = logbook._entry_message_from_state(to_state.domain, to_state)
|
2020-04-06 10:51:48 +00:00
|
|
|
assert message == "cleared (no vibration detected)"
|
2019-07-10 22:56:41 +00:00
|
|
|
|
2015-09-14 01:30:44 +00:00
|
|
|
def test_process_custom_logbook_entries(self):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Test if custom log book entries get added as an entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
name = "Nice name"
|
|
|
|
message = "has a custom entry"
|
|
|
|
entity_id = "sun.sun"
|
|
|
|
|
|
|
|
entries = list(
|
|
|
|
logbook.humanify(
|
|
|
|
self.hass,
|
|
|
|
(
|
|
|
|
ha.Event(
|
|
|
|
logbook.EVENT_LOGBOOK_ENTRY,
|
|
|
|
{
|
|
|
|
logbook.ATTR_NAME: name,
|
|
|
|
logbook.ATTR_MESSAGE: message,
|
|
|
|
logbook.ATTR_ENTITY_ID: entity_id,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
)
|
2015-09-14 01:30:44 +00:00
|
|
|
|
2020-04-06 10:51:48 +00:00
|
|
|
assert len(entries) == 1
|
2015-09-14 01:30:44 +00:00
|
|
|
self.assert_entry(
|
2019-07-31 19:25:30 +00:00
|
|
|
entries[0], name=name, message=message, domain="sun", entity_id=entity_id
|
|
|
|
)
|
2015-09-14 01:30:44 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def assert_entry(
|
|
|
|
self, entry, when=None, name=None, message=None, domain=None, entity_id=None
|
|
|
|
):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Assert an entry is what is expected."""
|
2015-05-01 04:47:20 +00:00
|
|
|
if when:
|
2019-07-31 19:25:30 +00:00
|
|
|
assert when == entry["when"]
|
2015-05-01 04:47:20 +00:00
|
|
|
|
|
|
|
if name:
|
2019-07-31 19:25:30 +00:00
|
|
|
assert name == entry["name"]
|
2015-05-01 04:47:20 +00:00
|
|
|
|
|
|
|
if message:
|
2019-07-31 19:25:30 +00:00
|
|
|
assert message == entry["message"]
|
2015-05-01 04:47:20 +00:00
|
|
|
|
|
|
|
if domain:
|
2019-07-31 19:25:30 +00:00
|
|
|
assert domain == entry["domain"]
|
2015-05-01 04:47:20 +00:00
|
|
|
|
|
|
|
if entity_id:
|
2019-07-31 19:25:30 +00:00
|
|
|
assert entity_id == entry["entity_id"]
|
|
|
|
|
|
|
|
def create_state_changed_event(
|
|
|
|
self,
|
|
|
|
event_time_fired,
|
|
|
|
entity_id,
|
|
|
|
state,
|
|
|
|
attributes=None,
|
|
|
|
last_changed=None,
|
|
|
|
last_updated=None,
|
|
|
|
):
|
2016-03-09 09:25:50 +00:00
|
|
|
"""Create state changed event."""
|
2020-03-08 04:27:51 +00:00
|
|
|
old_state = ha.State(
|
|
|
|
entity_id, "old", attributes, last_changed, last_updated
|
|
|
|
).as_dict()
|
|
|
|
new_state = ha.State(
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id, state, attributes, last_changed, last_updated
|
|
|
|
).as_dict()
|
2015-05-01 04:47:20 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
return ha.Event(
|
|
|
|
EVENT_STATE_CHANGED,
|
2020-03-08 04:27:51 +00:00
|
|
|
{"entity_id": entity_id, "old_state": old_state, "new_state": new_state},
|
2019-07-31 19:25:30 +00:00
|
|
|
time_fired=event_time_fired,
|
|
|
|
)
|
2018-03-15 17:54:22 +00:00
|
|
|
|
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
async def test_logbook_view(hass, hass_client):
|
2018-03-15 17:54:22 +00:00
|
|
|
"""Test the logbook view."""
|
2020-03-23 18:27:45 +00:00
|
|
|
await hass.async_add_executor_job(init_recorder_component, hass)
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_setup_component(hass, "logbook", {})
|
2018-03-15 17:54:22 +00:00
|
|
|
await hass.async_add_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
|
2018-12-02 15:32:53 +00:00
|
|
|
client = await hass_client()
|
2020-04-04 22:26:08 +00:00
|
|
|
response = await client.get(f"/api/logbook/{dt_util.utcnow().isoformat()}")
|
2018-03-15 17:54:22 +00:00
|
|
|
assert response.status == 200
|
2018-10-01 14:12:25 +00:00
|
|
|
|
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
async def test_logbook_view_period_entity(hass, hass_client):
|
2018-10-11 12:15:04 +00:00
|
|
|
"""Test the logbook view with period and entity."""
|
2020-03-23 18:27:45 +00:00
|
|
|
await hass.async_add_executor_job(init_recorder_component, hass)
|
2019-07-31 19:25:30 +00:00
|
|
|
await async_setup_component(hass, "logbook", {})
|
2018-10-11 12:15:04 +00:00
|
|
|
await hass.async_add_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id_test = "switch.test"
|
2018-10-11 12:15:04 +00:00
|
|
|
hass.states.async_set(entity_id_test, STATE_OFF)
|
|
|
|
hass.states.async_set(entity_id_test, STATE_ON)
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_id_second = "switch.second"
|
2018-10-11 12:15:04 +00:00
|
|
|
hass.states.async_set(entity_id_second, STATE_OFF)
|
|
|
|
hass.states.async_set(entity_id_second, STATE_ON)
|
2020-03-10 00:43:26 +00:00
|
|
|
await hass.async_add_job(partial(trigger_db_commit, hass))
|
2018-10-11 12:15:04 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
await hass.async_add_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
|
|
|
|
|
2018-12-02 15:32:53 +00:00
|
|
|
client = await hass_client()
|
2018-10-11 12:15:04 +00:00
|
|
|
|
|
|
|
# Today time 00:00:00
|
|
|
|
start = dt_util.utcnow().date()
|
|
|
|
start_date = datetime(start.year, start.month, start.day)
|
|
|
|
|
|
|
|
# Test today entries without filters
|
2020-04-04 22:26:08 +00:00
|
|
|
response = await client.get(f"/api/logbook/{start_date.isoformat()}")
|
2018-10-11 12:15:04 +00:00
|
|
|
assert response.status == 200
|
|
|
|
json = await response.json()
|
|
|
|
assert len(json) == 2
|
2019-07-31 19:25:30 +00:00
|
|
|
assert json[0]["entity_id"] == entity_id_test
|
|
|
|
assert json[1]["entity_id"] == entity_id_second
|
2018-10-11 12:15:04 +00:00
|
|
|
|
|
|
|
# Test today entries with filter by period
|
2020-04-04 22:26:08 +00:00
|
|
|
response = await client.get(f"/api/logbook/{start_date.isoformat()}?period=1")
|
2018-10-11 12:15:04 +00:00
|
|
|
assert response.status == 200
|
|
|
|
json = await response.json()
|
|
|
|
assert len(json) == 2
|
2019-07-31 19:25:30 +00:00
|
|
|
assert json[0]["entity_id"] == entity_id_test
|
|
|
|
assert json[1]["entity_id"] == entity_id_second
|
2018-10-11 12:15:04 +00:00
|
|
|
|
|
|
|
# Test today entries with filter by entity_id
|
|
|
|
response = await client.get(
|
2020-04-04 22:26:08 +00:00
|
|
|
f"/api/logbook/{start_date.isoformat()}?entity=switch.test"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-10-11 12:15:04 +00:00
|
|
|
assert response.status == 200
|
|
|
|
json = await response.json()
|
|
|
|
assert len(json) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert json[0]["entity_id"] == entity_id_test
|
2018-10-11 12:15:04 +00:00
|
|
|
|
|
|
|
# Test entries for 3 days with filter by entity_id
|
|
|
|
response = await client.get(
|
2020-04-04 22:26:08 +00:00
|
|
|
f"/api/logbook/{start_date.isoformat()}?period=3&entity=switch.test"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-10-11 12:15:04 +00:00
|
|
|
assert response.status == 200
|
|
|
|
json = await response.json()
|
|
|
|
assert len(json) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert json[0]["entity_id"] == entity_id_test
|
2018-10-11 12:15:04 +00:00
|
|
|
|
|
|
|
# Tomorrow time 00:00:00
|
|
|
|
start = (dt_util.utcnow() + timedelta(days=1)).date()
|
|
|
|
start_date = datetime(start.year, start.month, start.day)
|
|
|
|
|
|
|
|
# Test tomorrow entries without filters
|
2020-04-04 22:26:08 +00:00
|
|
|
response = await client.get(f"/api/logbook/{start_date.isoformat()}")
|
2018-10-11 12:15:04 +00:00
|
|
|
assert response.status == 200
|
|
|
|
json = await response.json()
|
|
|
|
assert len(json) == 0
|
|
|
|
|
|
|
|
# Test tomorrow entries with filter by entity_id
|
|
|
|
response = await client.get(
|
2020-04-04 22:26:08 +00:00
|
|
|
f"/api/logbook/{start_date.isoformat()}?entity=switch.test"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-10-11 12:15:04 +00:00
|
|
|
assert response.status == 200
|
|
|
|
json = await response.json()
|
|
|
|
assert len(json) == 0
|
|
|
|
|
|
|
|
# Test entries from tomorrow to 3 days ago with filter by entity_id
|
|
|
|
response = await client.get(
|
2020-04-04 22:26:08 +00:00
|
|
|
f"/api/logbook/{start_date.isoformat()}?period=3&entity=switch.test"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-10-11 12:15:04 +00:00
|
|
|
assert response.status == 200
|
|
|
|
json = await response.json()
|
|
|
|
assert len(json) == 1
|
2019-07-31 19:25:30 +00:00
|
|
|
assert json[0]["entity_id"] == entity_id_test
|
2018-10-11 12:15:04 +00:00
|
|
|
|
|
|
|
|
2020-03-05 19:55:50 +00:00
|
|
|
async def test_logbook_describe_event(hass, hass_client):
|
|
|
|
"""Test teaching logbook about a new event."""
|
|
|
|
await hass.async_add_executor_job(init_recorder_component, hass)
|
|
|
|
assert await async_setup_component(hass, "logbook", {})
|
|
|
|
with patch(
|
|
|
|
"homeassistant.util.dt.utcnow",
|
|
|
|
return_value=dt_util.utcnow() - timedelta(seconds=5),
|
|
|
|
):
|
|
|
|
hass.bus.async_fire("some_event")
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
await hass.async_add_executor_job(
|
|
|
|
hass.data[recorder.DATA_INSTANCE].block_till_done
|
|
|
|
)
|
|
|
|
|
|
|
|
def _describe(event):
|
|
|
|
"""Describe an event."""
|
|
|
|
return {"name": "Test Name", "message": "tested a message"}
|
|
|
|
|
|
|
|
hass.components.logbook.async_describe_event("test_domain", "some_event", _describe)
|
|
|
|
|
|
|
|
client = await hass_client()
|
|
|
|
response = await client.get("/api/logbook")
|
|
|
|
results = await response.json()
|
|
|
|
assert len(results) == 1
|
|
|
|
event = results[0]
|
|
|
|
assert event["name"] == "Test Name"
|
|
|
|
assert event["message"] == "tested a message"
|
|
|
|
assert event["domain"] == "test_domain"
|
2020-06-03 19:07:56 +00:00
|
|
|
|
|
|
|
|
2020-06-12 18:45:17 +00:00
|
|
|
async def test_exclude_described_event(hass, hass_client):
|
|
|
|
"""Test exclusions of events that are described by another integration."""
|
|
|
|
name = "My Automation Rule"
|
|
|
|
entity_id = "automation.excluded_rule"
|
|
|
|
entity_id2 = "automation.included_rule"
|
|
|
|
entity_id3 = "sensor.excluded_domain"
|
|
|
|
|
|
|
|
await hass.async_add_executor_job(init_recorder_component, hass)
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
logbook.DOMAIN,
|
|
|
|
{
|
|
|
|
logbook.DOMAIN: {
|
|
|
|
logbook.CONF_EXCLUDE: {
|
|
|
|
logbook.CONF_DOMAINS: ["sensor"],
|
|
|
|
logbook.CONF_ENTITIES: [entity_id],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.util.dt.utcnow",
|
|
|
|
return_value=dt_util.utcnow() - timedelta(seconds=5),
|
|
|
|
):
|
|
|
|
hass.bus.async_fire(
|
|
|
|
"some_automation_event",
|
|
|
|
{logbook.ATTR_NAME: name, logbook.ATTR_ENTITY_ID: entity_id},
|
|
|
|
)
|
|
|
|
hass.bus.async_fire(
|
|
|
|
"some_automation_event",
|
|
|
|
{logbook.ATTR_NAME: name, logbook.ATTR_ENTITY_ID: entity_id2},
|
|
|
|
)
|
|
|
|
hass.bus.async_fire(
|
|
|
|
"some_event", {logbook.ATTR_NAME: name, logbook.ATTR_ENTITY_ID: entity_id3}
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
await hass.async_add_executor_job(
|
|
|
|
hass.data[recorder.DATA_INSTANCE].block_till_done
|
|
|
|
)
|
|
|
|
|
|
|
|
def _describe(event):
|
|
|
|
"""Describe an event."""
|
|
|
|
return {
|
|
|
|
"name": "Test Name",
|
|
|
|
"message": "tested a message",
|
|
|
|
"entity_id": event.data.get(ATTR_ENTITY_ID),
|
|
|
|
}
|
|
|
|
|
|
|
|
hass.components.logbook.async_describe_event(
|
|
|
|
"automation", "some_automation_event", _describe
|
|
|
|
)
|
|
|
|
hass.components.logbook.async_describe_event("sensor", "some_event", _describe)
|
|
|
|
|
|
|
|
client = await hass_client()
|
|
|
|
response = await client.get("/api/logbook")
|
|
|
|
results = await response.json()
|
|
|
|
assert len(results) == 1
|
|
|
|
event = results[0]
|
|
|
|
assert event["name"] == "Test Name"
|
|
|
|
assert event["message"] == "tested a message"
|
|
|
|
assert event["domain"] == "automation"
|
|
|
|
assert event["entity_id"] == "automation.included_rule"
|
|
|
|
|
|
|
|
|
2020-06-03 19:07:56 +00:00
|
|
|
async def test_logbook_view_end_time_entity(hass, hass_client):
|
|
|
|
"""Test the logbook view with end_time and entity."""
|
|
|
|
await hass.async_add_executor_job(init_recorder_component, hass)
|
|
|
|
await async_setup_component(hass, "logbook", {})
|
|
|
|
await hass.async_add_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
|
|
|
|
|
|
|
|
entity_id_test = "switch.test"
|
|
|
|
hass.states.async_set(entity_id_test, STATE_OFF)
|
|
|
|
hass.states.async_set(entity_id_test, STATE_ON)
|
|
|
|
entity_id_second = "switch.second"
|
|
|
|
hass.states.async_set(entity_id_second, STATE_OFF)
|
|
|
|
hass.states.async_set(entity_id_second, STATE_ON)
|
|
|
|
await hass.async_add_job(partial(trigger_db_commit, hass))
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
await hass.async_add_job(hass.data[recorder.DATA_INSTANCE].block_till_done)
|
|
|
|
|
|
|
|
client = await hass_client()
|
|
|
|
|
|
|
|
# Today time 00:00:00
|
|
|
|
start = dt_util.utcnow().date()
|
|
|
|
start_date = datetime(start.year, start.month, start.day)
|
|
|
|
|
|
|
|
# Test today entries with filter by end_time
|
|
|
|
end_time = start + timedelta(hours=24)
|
|
|
|
response = await client.get(
|
|
|
|
f"/api/logbook/{start_date.isoformat()}?end_time={end_time}"
|
|
|
|
)
|
|
|
|
assert response.status == 200
|
|
|
|
json = await response.json()
|
|
|
|
assert len(json) == 2
|
|
|
|
assert json[0]["entity_id"] == entity_id_test
|
|
|
|
assert json[1]["entity_id"] == entity_id_second
|
|
|
|
|
|
|
|
# Test entries for 3 days with filter by entity_id
|
|
|
|
end_time = start + timedelta(hours=72)
|
|
|
|
response = await client.get(
|
|
|
|
f"/api/logbook/{start_date.isoformat()}?end_time={end_time}&entity=switch.test"
|
|
|
|
)
|
|
|
|
assert response.status == 200
|
|
|
|
json = await response.json()
|
|
|
|
assert len(json) == 1
|
|
|
|
assert json[0]["entity_id"] == entity_id_test
|
|
|
|
|
|
|
|
# Tomorrow time 00:00:00
|
|
|
|
start = dt_util.utcnow()
|
|
|
|
start_date = datetime(start.year, start.month, start.day)
|
|
|
|
|
|
|
|
# Test entries from today to 3 days with filter by entity_id
|
|
|
|
end_time = start_date + timedelta(hours=72)
|
|
|
|
response = await client.get(
|
|
|
|
f"/api/logbook/{start_date.isoformat()}?end_time={end_time}&entity=switch.test"
|
|
|
|
)
|
|
|
|
assert response.status == 200
|
|
|
|
json = await response.json()
|
|
|
|
assert len(json) == 1
|
|
|
|
assert json[0]["entity_id"] == entity_id_test
|