Reorganize trigger code (#38655)
This commit is contained in:
1 parent
fca071742d
commit
ca9dd0c833
51 files changed
+306
-194
No files matched your search
@@ -1,168 +0,0 @@
|
||||
"""The tests for the Event automation."""
|
||||
import pytest
|
||||
|
||||
import homeassistant.components.automation as automation
|
||||
from homeassistant.core import Context
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import async_mock_service, mock_component
|
||||
from tests.components.automation import common
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def calls(hass):
|
||||
"""Track calls to a mock service."""
|
||||
return async_mock_service(hass, "test", "automation")
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_comp(hass):
|
||||
"""Initialize components."""
|
||||
mock_component(hass, "group")
|
||||
|
||||
|
||||
async def test_if_fires_on_event(hass, calls):
|
||||
"""Test the firing of events."""
|
||||
context = Context()
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.bus.async_fire("test_event", context=context)
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
assert calls[0].context.parent_id == context.id
|
||||
|
||||
await common.async_turn_off(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_on_event_extra_data(hass, calls):
|
||||
"""Test the firing of events still matches with event data."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.bus.async_fire("test_event", {"extra_key": "extra_data"})
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
await common.async_turn_off(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_on_event_with_data(hass, calls):
|
||||
"""Test the firing of events with data."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "event",
|
||||
"event_type": "test_event",
|
||||
"event_data": {"some_attr": "some_value"},
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.bus.async_fire("test_event", {"some_attr": "some_value", "another": "value"})
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_on_event_with_empty_data_config(hass, calls):
|
||||
"""Test the firing of events with empty data config.
|
||||
|
||||
The frontend automation editor can produce configurations with an
|
||||
empty dict for event_data instead of no key.
|
||||
"""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "event",
|
||||
"event_type": "test_event",
|
||||
"event_data": {},
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.bus.async_fire("test_event", {"some_attr": "some_value", "another": "value"})
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_on_event_with_nested_data(hass, calls):
|
||||
"""Test the firing of events with nested data."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "event",
|
||||
"event_type": "test_event",
|
||||
"event_data": {"parent_attr": {"some_attr": "some_value"}},
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.bus.async_fire(
|
||||
"test_event", {"parent_attr": {"some_attr": "some_value", "another": "value"}}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_not_fires_if_event_data_not_matches(hass, calls):
|
||||
"""Test firing of event if no match."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "event",
|
||||
"event_type": "test_event",
|
||||
"event_data": {"some_attr": "some_value"},
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.bus.async_fire("test_event", {"some_attr": "some_other_value"})
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
@@ -1,314 +0,0 @@
|
||||
"""The tests for the geolocation trigger."""
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import automation, zone
|
||||
from homeassistant.core import Context
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import async_mock_service, mock_component
|
||||
from tests.components.automation import common
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def calls(hass):
|
||||
"""Track calls to a mock service."""
|
||||
return async_mock_service(hass, "test", "automation")
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_comp(hass):
|
||||
"""Initialize components."""
|
||||
mock_component(hass, "group")
|
||||
hass.loop.run_until_complete(
|
||||
async_setup_component(
|
||||
hass,
|
||||
zone.DOMAIN,
|
||||
{
|
||||
"zone": {
|
||||
"name": "test",
|
||||
"latitude": 32.880837,
|
||||
"longitude": -117.237561,
|
||||
"radius": 250,
|
||||
}
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
async def test_if_fires_on_zone_enter(hass, calls):
|
||||
"""Test for firing on zone enter."""
|
||||
context = Context()
|
||||
hass.states.async_set(
|
||||
"geo_location.entity",
|
||||
"hello",
|
||||
{"latitude": 32.881011, "longitude": -117.234758, "source": "test_source"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "geo_location",
|
||||
"source": "test_source",
|
||||
"zone": "zone.test",
|
||||
"event": "enter",
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "{{ trigger.%s }}"
|
||||
% "}} - {{ trigger.".join(
|
||||
(
|
||||
"platform",
|
||||
"entity_id",
|
||||
"from_state.state",
|
||||
"to_state.state",
|
||||
"zone.name",
|
||||
)
|
||||
)
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set(
|
||||
"geo_location.entity",
|
||||
"hello",
|
||||
{"latitude": 32.880586, "longitude": -117.237564},
|
||||
context=context,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
assert calls[0].context.parent_id == context.id
|
||||
assert (
|
||||
calls[0].data["some"]
|
||||
== "geo_location - geo_location.entity - hello - hello - test"
|
||||
)
|
||||
|
||||
# Set out of zone again so we can trigger call
|
||||
hass.states.async_set(
|
||||
"geo_location.entity",
|
||||
"hello",
|
||||
{"latitude": 32.881011, "longitude": -117.234758},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await common.async_turn_off(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.states.async_set(
|
||||
"geo_location.entity",
|
||||
"hello",
|
||||
{"latitude": 32.880586, "longitude": -117.237564},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_not_fires_for_enter_on_zone_leave(hass, calls):
|
||||
"""Test for not firing on zone leave."""
|
||||
hass.states.async_set(
|
||||
"geo_location.entity",
|
||||
"hello",
|
||||
{"latitude": 32.880586, "longitude": -117.237564, "source": "test_source"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "geo_location",
|
||||
"source": "test_source",
|
||||
"zone": "zone.test",
|
||||
"event": "enter",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set(
|
||||
"geo_location.entity",
|
||||
"hello",
|
||||
{"latitude": 32.881011, "longitude": -117.234758},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_if_fires_on_zone_leave(hass, calls):
|
||||
"""Test for firing on zone leave."""
|
||||
hass.states.async_set(
|
||||
"geo_location.entity",
|
||||
"hello",
|
||||
{"latitude": 32.880586, "longitude": -117.237564, "source": "test_source"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "geo_location",
|
||||
"source": "test_source",
|
||||
"zone": "zone.test",
|
||||
"event": "leave",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set(
|
||||
"geo_location.entity",
|
||||
"hello",
|
||||
{"latitude": 32.881011, "longitude": -117.234758, "source": "test_source"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_not_fires_for_leave_on_zone_enter(hass, calls):
|
||||
"""Test for not firing on zone enter."""
|
||||
hass.states.async_set(
|
||||
"geo_location.entity",
|
||||
"hello",
|
||||
{"latitude": 32.881011, "longitude": -117.234758, "source": "test_source"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "geo_location",
|
||||
"source": "test_source",
|
||||
"zone": "zone.test",
|
||||
"event": "leave",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set(
|
||||
"geo_location.entity",
|
||||
"hello",
|
||||
{"latitude": 32.880586, "longitude": -117.237564},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_if_fires_on_zone_appear(hass, calls):
|
||||
"""Test for firing if entity appears in zone."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "geo_location",
|
||||
"source": "test_source",
|
||||
"zone": "zone.test",
|
||||
"event": "enter",
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "{{ trigger.%s }}"
|
||||
% "}} - {{ trigger.".join(
|
||||
(
|
||||
"platform",
|
||||
"entity_id",
|
||||
"from_state.state",
|
||||
"to_state.state",
|
||||
"zone.name",
|
||||
)
|
||||
)
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# Entity appears in zone without previously existing outside the zone.
|
||||
context = Context()
|
||||
hass.states.async_set(
|
||||
"geo_location.entity",
|
||||
"hello",
|
||||
{"latitude": 32.880586, "longitude": -117.237564, "source": "test_source"},
|
||||
context=context,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
assert calls[0].context.parent_id == context.id
|
||||
assert (
|
||||
calls[0].data["some"] == "geo_location - geo_location.entity - - hello - test"
|
||||
)
|
||||
|
||||
|
||||
async def test_if_fires_on_zone_disappear(hass, calls):
|
||||
"""Test for firing if entity disappears from zone."""
|
||||
hass.states.async_set(
|
||||
"geo_location.entity",
|
||||
"hello",
|
||||
{"latitude": 32.880586, "longitude": -117.237564, "source": "test_source"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "geo_location",
|
||||
"source": "test_source",
|
||||
"zone": "zone.test",
|
||||
"event": "leave",
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "{{ trigger.%s }}"
|
||||
% "}} - {{ trigger.".join(
|
||||
(
|
||||
"platform",
|
||||
"entity_id",
|
||||
"from_state.state",
|
||||
"to_state.state",
|
||||
"zone.name",
|
||||
)
|
||||
)
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# Entity disappears from zone without new coordinates outside the zone.
|
||||
hass.states.async_remove("geo_location.entity")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
assert (
|
||||
calls[0].data["some"] == "geo_location - geo_location.entity - hello - - test"
|
||||
)
|
||||
@@ -1,68 +0,0 @@
|
||||
"""The tests for the Event automation."""
|
||||
import homeassistant.components.automation as automation
|
||||
from homeassistant.core import CoreState
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.async_mock import AsyncMock, patch
|
||||
from tests.common import async_mock_service
|
||||
|
||||
|
||||
async def test_if_fires_on_hass_start(hass):
|
||||
"""Test the firing when Home Assistant starts."""
|
||||
calls = async_mock_service(hass, "test", "automation")
|
||||
hass.state = CoreState.not_running
|
||||
config = {
|
||||
automation.DOMAIN: {
|
||||
"alias": "hello",
|
||||
"trigger": {"platform": "homeassistant", "event": "start"},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
}
|
||||
|
||||
assert await async_setup_component(hass, automation.DOMAIN, config)
|
||||
assert automation.is_on(hass, "automation.hello")
|
||||
assert len(calls) == 0
|
||||
|
||||
await hass.async_start()
|
||||
await hass.async_block_till_done()
|
||||
assert automation.is_on(hass, "automation.hello")
|
||||
assert len(calls) == 1
|
||||
|
||||
with patch(
|
||||
"homeassistant.config.async_hass_config_yaml", AsyncMock(return_value=config),
|
||||
):
|
||||
await hass.services.async_call(
|
||||
automation.DOMAIN, automation.SERVICE_RELOAD, blocking=True
|
||||
)
|
||||
|
||||
assert automation.is_on(hass, "automation.hello")
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_on_hass_shutdown(hass):
|
||||
"""Test the firing when Home Assistant shuts down."""
|
||||
calls = async_mock_service(hass, "test", "automation")
|
||||
hass.state = CoreState.not_running
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"alias": "hello",
|
||||
"trigger": {"platform": "homeassistant", "event": "shutdown"},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
assert automation.is_on(hass, "automation.hello")
|
||||
assert len(calls) == 0
|
||||
|
||||
await hass.async_start()
|
||||
assert automation.is_on(hass, "automation.hello")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
with patch.object(hass.loop, "stop"):
|
||||
await hass.async_stop()
|
||||
assert len(calls) == 1
|
||||
@@ -861,6 +861,22 @@ async def test_automation_not_trigger_on_bootstrap(hass):
|
||||
assert ["hello.world"] == calls[0].data.get(ATTR_ENTITY_ID)
|
||||
|
||||
|
||||
async def test_automation_bad_trigger(hass, caplog):
|
||||
"""Test bad trigger configuration."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"alias": "hello",
|
||||
"trigger": {"platform": "automation"},
|
||||
"action": [],
|
||||
}
|
||||
},
|
||||
)
|
||||
assert "Integration 'automation' does not provide trigger support." in caplog.text
|
||||
|
||||
|
||||
async def test_automation_with_error_in_script(hass, caplog):
|
||||
"""Test automation with an error in script."""
|
||||
assert await async_setup_component(
|
||||
|
||||
@@ -1,267 +0,0 @@
|
||||
"""The tests for the litejet component."""
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant import setup
|
||||
from homeassistant.components import litejet
|
||||
import homeassistant.components.automation as automation
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import async_fire_time_changed, async_mock_service
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ENTITY_SWITCH = "switch.mock_switch_1"
|
||||
ENTITY_SWITCH_NUMBER = 1
|
||||
ENTITY_OTHER_SWITCH = "switch.mock_switch_2"
|
||||
ENTITY_OTHER_SWITCH_NUMBER = 2
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def calls(hass):
|
||||
"""Track calls to a mock service."""
|
||||
return async_mock_service(hass, "test", "automation")
|
||||
|
||||
|
||||
def get_switch_name(number):
|
||||
"""Get a mock switch name."""
|
||||
return f"Mock Switch #{number}"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_lj(hass):
|
||||
"""Initialize components."""
|
||||
with mock.patch("homeassistant.components.litejet.LiteJet") as mock_pylitejet:
|
||||
mock_lj = mock_pylitejet.return_value
|
||||
|
||||
mock_lj.switch_pressed_callbacks = {}
|
||||
mock_lj.switch_released_callbacks = {}
|
||||
|
||||
def on_switch_pressed(number, callback):
|
||||
mock_lj.switch_pressed_callbacks[number] = callback
|
||||
|
||||
def on_switch_released(number, callback):
|
||||
mock_lj.switch_released_callbacks[number] = callback
|
||||
|
||||
mock_lj.loads.return_value = range(0)
|
||||
mock_lj.button_switches.return_value = range(1, 3)
|
||||
mock_lj.all_switches.return_value = range(1, 6)
|
||||
mock_lj.scenes.return_value = range(0)
|
||||
mock_lj.get_switch_name.side_effect = get_switch_name
|
||||
mock_lj.on_switch_pressed.side_effect = on_switch_pressed
|
||||
mock_lj.on_switch_released.side_effect = on_switch_released
|
||||
|
||||
config = {"litejet": {"port": "/dev/serial/by-id/mock-litejet"}}
|
||||
assert hass.loop.run_until_complete(
|
||||
setup.async_setup_component(hass, litejet.DOMAIN, config)
|
||||
)
|
||||
|
||||
mock_lj.start_time = dt_util.utcnow()
|
||||
mock_lj.last_delta = timedelta(0)
|
||||
return mock_lj
|
||||
|
||||
|
||||
async def simulate_press(hass, mock_lj, number):
|
||||
"""Test to simulate a press."""
|
||||
_LOGGER.info("*** simulate press of %d", number)
|
||||
callback = mock_lj.switch_pressed_callbacks.get(number)
|
||||
with mock.patch(
|
||||
"homeassistant.helpers.condition.dt_util.utcnow",
|
||||
return_value=mock_lj.start_time + mock_lj.last_delta,
|
||||
):
|
||||
if callback is not None:
|
||||
await hass.async_add_job(callback)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def simulate_release(hass, mock_lj, number):
|
||||
"""Test to simulate releasing."""
|
||||
_LOGGER.info("*** simulate release of %d", number)
|
||||
callback = mock_lj.switch_released_callbacks.get(number)
|
||||
with mock.patch(
|
||||
"homeassistant.helpers.condition.dt_util.utcnow",
|
||||
return_value=mock_lj.start_time + mock_lj.last_delta,
|
||||
):
|
||||
if callback is not None:
|
||||
await hass.async_add_job(callback)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def simulate_time(hass, mock_lj, delta):
|
||||
"""Test to simulate time."""
|
||||
_LOGGER.info(
|
||||
"*** simulate time change by %s: %s", delta, mock_lj.start_time + delta
|
||||
)
|
||||
mock_lj.last_delta = delta
|
||||
with mock.patch(
|
||||
"homeassistant.helpers.condition.dt_util.utcnow",
|
||||
return_value=mock_lj.start_time + delta,
|
||||
):
|
||||
_LOGGER.info("now=%s", dt_util.utcnow())
|
||||
async_fire_time_changed(hass, mock_lj.start_time + delta)
|
||||
await hass.async_block_till_done()
|
||||
_LOGGER.info("done with now=%s", dt_util.utcnow())
|
||||
|
||||
|
||||
async def setup_automation(hass, trigger):
|
||||
"""Test setting up the automation."""
|
||||
assert await setup.async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: [
|
||||
{
|
||||
"alias": "My Test",
|
||||
"trigger": trigger,
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
]
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_simple(hass, calls, mock_lj):
|
||||
"""Test the simplest form of a LiteJet trigger."""
|
||||
await setup_automation(
|
||||
hass, {"platform": "litejet", "number": ENTITY_OTHER_SWITCH_NUMBER}
|
||||
)
|
||||
|
||||
await simulate_press(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
await simulate_release(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_held_more_than_short(hass, calls, mock_lj):
|
||||
"""Test a too short hold."""
|
||||
await setup_automation(
|
||||
hass,
|
||||
{
|
||||
"platform": "litejet",
|
||||
"number": ENTITY_OTHER_SWITCH_NUMBER,
|
||||
"held_more_than": {"milliseconds": "200"},
|
||||
},
|
||||
)
|
||||
|
||||
await simulate_press(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
await simulate_time(hass, mock_lj, timedelta(seconds=0.1))
|
||||
await simulate_release(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_held_more_than_long(hass, calls, mock_lj):
|
||||
"""Test a hold that is long enough."""
|
||||
await setup_automation(
|
||||
hass,
|
||||
{
|
||||
"platform": "litejet",
|
||||
"number": ENTITY_OTHER_SWITCH_NUMBER,
|
||||
"held_more_than": {"milliseconds": "200"},
|
||||
},
|
||||
)
|
||||
|
||||
await simulate_press(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
assert len(calls) == 0
|
||||
await simulate_time(hass, mock_lj, timedelta(seconds=0.3))
|
||||
assert len(calls) == 1
|
||||
await simulate_release(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_held_less_than_short(hass, calls, mock_lj):
|
||||
"""Test a hold that is short enough."""
|
||||
await setup_automation(
|
||||
hass,
|
||||
{
|
||||
"platform": "litejet",
|
||||
"number": ENTITY_OTHER_SWITCH_NUMBER,
|
||||
"held_less_than": {"milliseconds": "200"},
|
||||
},
|
||||
)
|
||||
|
||||
await simulate_press(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
await simulate_time(hass, mock_lj, timedelta(seconds=0.1))
|
||||
assert len(calls) == 0
|
||||
await simulate_release(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_held_less_than_long(hass, calls, mock_lj):
|
||||
"""Test a hold that is too long."""
|
||||
await setup_automation(
|
||||
hass,
|
||||
{
|
||||
"platform": "litejet",
|
||||
"number": ENTITY_OTHER_SWITCH_NUMBER,
|
||||
"held_less_than": {"milliseconds": "200"},
|
||||
},
|
||||
)
|
||||
|
||||
await simulate_press(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
assert len(calls) == 0
|
||||
await simulate_time(hass, mock_lj, timedelta(seconds=0.3))
|
||||
assert len(calls) == 0
|
||||
await simulate_release(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_held_in_range_short(hass, calls, mock_lj):
|
||||
"""Test an in-range trigger with a too short hold."""
|
||||
await setup_automation(
|
||||
hass,
|
||||
{
|
||||
"platform": "litejet",
|
||||
"number": ENTITY_OTHER_SWITCH_NUMBER,
|
||||
"held_more_than": {"milliseconds": "100"},
|
||||
"held_less_than": {"milliseconds": "300"},
|
||||
},
|
||||
)
|
||||
|
||||
await simulate_press(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
await simulate_time(hass, mock_lj, timedelta(seconds=0.05))
|
||||
await simulate_release(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_held_in_range_just_right(hass, calls, mock_lj):
|
||||
"""Test an in-range trigger with a just right hold."""
|
||||
await setup_automation(
|
||||
hass,
|
||||
{
|
||||
"platform": "litejet",
|
||||
"number": ENTITY_OTHER_SWITCH_NUMBER,
|
||||
"held_more_than": {"milliseconds": "100"},
|
||||
"held_less_than": {"milliseconds": "300"},
|
||||
},
|
||||
)
|
||||
|
||||
await simulate_press(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
assert len(calls) == 0
|
||||
await simulate_time(hass, mock_lj, timedelta(seconds=0.2))
|
||||
assert len(calls) == 0
|
||||
await simulate_release(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_held_in_range_long(hass, calls, mock_lj):
|
||||
"""Test an in-range trigger with a too long hold."""
|
||||
await setup_automation(
|
||||
hass,
|
||||
{
|
||||
"platform": "litejet",
|
||||
"number": ENTITY_OTHER_SWITCH_NUMBER,
|
||||
"held_more_than": {"milliseconds": "100"},
|
||||
"held_less_than": {"milliseconds": "300"},
|
||||
},
|
||||
)
|
||||
|
||||
await simulate_press(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
assert len(calls) == 0
|
||||
await simulate_time(hass, mock_lj, timedelta(seconds=0.4))
|
||||
assert len(calls) == 0
|
||||
await simulate_release(hass, mock_lj, ENTITY_OTHER_SWITCH_NUMBER)
|
||||
assert len(calls) == 0
|
||||
@@ -1,132 +0,0 @@
|
||||
"""The tests for the MQTT automation."""
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
import homeassistant.components.automation as automation
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import async_fire_mqtt_message, async_mock_service, mock_component
|
||||
from tests.components.automation import common
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def calls(hass):
|
||||
"""Track calls to a mock service."""
|
||||
return async_mock_service(hass, "test", "automation")
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_comp(hass, mqtt_mock):
|
||||
"""Initialize components."""
|
||||
mock_component(hass, "group")
|
||||
|
||||
|
||||
async def test_if_fires_on_topic_match(hass, calls):
|
||||
"""Test if message is fired on topic match."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "mqtt", "topic": "test-topic"},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "{{ trigger.platform }} - {{ trigger.topic }}"
|
||||
" - {{ trigger.payload }} - "
|
||||
"{{ trigger.payload_json.hello }}"
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
async_fire_mqtt_message(hass, "test-topic", '{ "hello": "world" }')
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
assert 'mqtt - test-topic - { "hello": "world" } - world' == calls[0].data["some"]
|
||||
|
||||
await common.async_turn_off(hass)
|
||||
await hass.async_block_till_done()
|
||||
async_fire_mqtt_message(hass, "test-topic", "test_payload")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_on_topic_and_payload_match(hass, calls):
|
||||
"""Test if message is fired on topic and payload match."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "mqtt",
|
||||
"topic": "test-topic",
|
||||
"payload": "hello",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
async_fire_mqtt_message(hass, "test-topic", "hello")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_not_fires_on_topic_but_no_payload_match(hass, calls):
|
||||
"""Test if message is not fired on topic but no payload."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "mqtt",
|
||||
"topic": "test-topic",
|
||||
"payload": "hello",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
async_fire_mqtt_message(hass, "test-topic", "no-hello")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_encoding_default(hass, calls, mqtt_mock):
|
||||
"""Test default encoding."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "mqtt", "topic": "test-topic"},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
mqtt_mock.async_subscribe.assert_called_once_with(
|
||||
"test-topic", mock.ANY, 0, "utf-8"
|
||||
)
|
||||
|
||||
|
||||
async def test_encoding_custom(hass, calls, mqtt_mock):
|
||||
"""Test default encoding."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "mqtt", "topic": "test-topic", "encoding": ""},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
mqtt_mock.async_subscribe.assert_called_once_with("test-topic", mock.ANY, 0, None)
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -1,843 +0,0 @@
|
||||
"""The tests for the sun automation."""
|
||||
from datetime import datetime
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import sun
|
||||
import homeassistant.components.automation as automation
|
||||
from homeassistant.const import SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import async_fire_time_changed, async_mock_service, mock_component
|
||||
from tests.components.automation import common
|
||||
|
||||
ORIG_TIME_ZONE = dt_util.DEFAULT_TIME_ZONE
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def calls(hass):
|
||||
"""Track calls to a mock service."""
|
||||
return async_mock_service(hass, "test", "automation")
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_comp(hass):
|
||||
"""Initialize components."""
|
||||
mock_component(hass, "group")
|
||||
dt_util.set_default_time_zone(hass.config.time_zone)
|
||||
hass.loop.run_until_complete(
|
||||
async_setup_component(hass, sun.DOMAIN, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}})
|
||||
)
|
||||
|
||||
|
||||
def teardown():
|
||||
"""Restore."""
|
||||
dt_util.set_default_time_zone(ORIG_TIME_ZONE)
|
||||
|
||||
|
||||
async def test_sunset_trigger(hass, calls, legacy_patchable_time):
|
||||
"""Test the sunset trigger."""
|
||||
now = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC)
|
||||
trigger_time = datetime(2015, 9, 16, 2, tzinfo=dt_util.UTC)
|
||||
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "sun", "event": SUN_EVENT_SUNSET},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
await common.async_turn_off(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
async_fire_time_changed(hass, trigger_time)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
await common.async_turn_on(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
async_fire_time_changed(hass, trigger_time)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_sunrise_trigger(hass, calls, legacy_patchable_time):
|
||||
"""Test the sunrise trigger."""
|
||||
now = datetime(2015, 9, 13, 23, tzinfo=dt_util.UTC)
|
||||
trigger_time = datetime(2015, 9, 16, 14, tzinfo=dt_util.UTC)
|
||||
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "sun", "event": SUN_EVENT_SUNRISE},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
async_fire_time_changed(hass, trigger_time)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_sunset_trigger_with_offset(hass, calls, legacy_patchable_time):
|
||||
"""Test the sunset trigger with offset."""
|
||||
now = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC)
|
||||
trigger_time = datetime(2015, 9, 16, 2, 30, tzinfo=dt_util.UTC)
|
||||
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "sun",
|
||||
"event": SUN_EVENT_SUNSET,
|
||||
"offset": "0:30:00",
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "{{ trigger.%s }}"
|
||||
% "}} - {{ trigger.".join(("platform", "event", "offset"))
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
async_fire_time_changed(hass, trigger_time)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
assert calls[0].data["some"] == "sun - sunset - 0:30:00"
|
||||
|
||||
|
||||
async def test_sunrise_trigger_with_offset(hass, calls, legacy_patchable_time):
|
||||
"""Test the sunrise trigger with offset."""
|
||||
now = datetime(2015, 9, 13, 23, tzinfo=dt_util.UTC)
|
||||
trigger_time = datetime(2015, 9, 16, 13, 30, tzinfo=dt_util.UTC)
|
||||
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "sun",
|
||||
"event": SUN_EVENT_SUNRISE,
|
||||
"offset": "-0:30:00",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
async_fire_time_changed(hass, trigger_time)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_action_before_sunrise_no_offset(hass, calls):
|
||||
"""
|
||||
Test if action was before sunrise.
|
||||
|
||||
Before sunrise is true from midnight until sunset, local time.
|
||||
"""
|
||||
await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"condition": {"condition": "sun", "before": SUN_EVENT_SUNRISE},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# sunrise: 2015-09-16 06:32:43 local, sunset: 2015-09-16 18:55:24 local
|
||||
# sunrise: 2015-09-16 13:32:43 UTC, sunset: 2015-09-17 01:55:24 UTC
|
||||
# now = sunrise + 1s -> 'before sunrise' not true
|
||||
now = datetime(2015, 9, 16, 13, 32, 44, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
# now = sunrise -> 'before sunrise' true
|
||||
now = datetime(2015, 9, 16, 13, 32, 43, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = local midnight -> 'before sunrise' true
|
||||
now = datetime(2015, 9, 16, 7, 0, 0, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
|
||||
# now = local midnight - 1s -> 'before sunrise' not true
|
||||
now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
|
||||
|
||||
async def test_if_action_after_sunrise_no_offset(hass, calls):
|
||||
"""
|
||||
Test if action was after sunrise.
|
||||
|
||||
After sunrise is true from sunrise until midnight, local time.
|
||||
"""
|
||||
await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"condition": {"condition": "sun", "after": SUN_EVENT_SUNRISE},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# sunrise: 2015-09-16 06:32:43 local, sunset: 2015-09-16 18:55:24 local
|
||||
# sunrise: 2015-09-16 13:32:43 UTC, sunset: 2015-09-17 01:55:24 UTC
|
||||
# now = sunrise - 1s -> 'after sunrise' not true
|
||||
now = datetime(2015, 9, 16, 13, 32, 42, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
# now = sunrise + 1s -> 'after sunrise' true
|
||||
now = datetime(2015, 9, 16, 13, 32, 43, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = local midnight -> 'after sunrise' not true
|
||||
now = datetime(2015, 9, 16, 7, 0, 0, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = local midnight - 1s -> 'after sunrise' true
|
||||
now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
|
||||
|
||||
async def test_if_action_before_sunrise_with_offset(hass, calls):
|
||||
"""
|
||||
Test if action was before sunrise with offset.
|
||||
|
||||
Before sunrise is true from midnight until sunset, local time.
|
||||
"""
|
||||
await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"condition": {
|
||||
"condition": "sun",
|
||||
"before": SUN_EVENT_SUNRISE,
|
||||
"before_offset": "+1:00:00",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# sunrise: 2015-09-16 06:32:43 local, sunset: 2015-09-16 18:55:24 local
|
||||
# sunrise: 2015-09-16 13:32:43 UTC, sunset: 2015-09-17 01:55:24 UTC
|
||||
# now = sunrise + 1s + 1h -> 'before sunrise' with offset +1h not true
|
||||
now = datetime(2015, 9, 16, 14, 32, 44, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
# now = sunrise + 1h -> 'before sunrise' with offset +1h true
|
||||
now = datetime(2015, 9, 16, 14, 32, 43, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = UTC midnight -> 'before sunrise' with offset +1h not true
|
||||
now = datetime(2015, 9, 17, 0, 0, 0, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = UTC midnight - 1s -> 'before sunrise' with offset +1h not true
|
||||
now = datetime(2015, 9, 16, 23, 59, 59, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = local midnight -> 'before sunrise' with offset +1h true
|
||||
now = datetime(2015, 9, 16, 7, 0, 0, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
|
||||
# now = local midnight - 1s -> 'before sunrise' with offset +1h not true
|
||||
now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
|
||||
# now = sunset -> 'before sunrise' with offset +1h not true
|
||||
now = datetime(2015, 9, 17, 1, 56, 48, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
|
||||
# now = sunset -1s -> 'before sunrise' with offset +1h not true
|
||||
now = datetime(2015, 9, 17, 1, 56, 45, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
|
||||
|
||||
async def test_if_action_before_sunset_with_offset(hass, calls):
|
||||
"""
|
||||
Test if action was before sunset with offset.
|
||||
|
||||
Before sunset is true from midnight until sunset, local time.
|
||||
"""
|
||||
await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"condition": {
|
||||
"condition": "sun",
|
||||
"before": "sunset",
|
||||
"before_offset": "+1:00:00",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# sunrise: 2015-09-16 06:32:43 local, sunset: 2015-09-16 18:55:24 local
|
||||
# sunrise: 2015-09-16 13:32:43 UTC, sunset: 2015-09-17 01:55:24 UTC
|
||||
# now = local midnight -> 'before sunset' with offset +1h true
|
||||
now = datetime(2015, 9, 16, 7, 0, 0, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = sunset + 1s + 1h -> 'before sunset' with offset +1h not true
|
||||
now = datetime(2015, 9, 17, 2, 55, 25, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = sunset + 1h -> 'before sunset' with offset +1h true
|
||||
now = datetime(2015, 9, 17, 2, 55, 24, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
|
||||
# now = UTC midnight -> 'before sunset' with offset +1h true
|
||||
now = datetime(2015, 9, 17, 0, 0, 0, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 3
|
||||
|
||||
# now = UTC midnight - 1s -> 'before sunset' with offset +1h true
|
||||
now = datetime(2015, 9, 16, 23, 59, 59, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 4
|
||||
|
||||
# now = sunrise -> 'before sunset' with offset +1h true
|
||||
now = datetime(2015, 9, 16, 13, 32, 43, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 5
|
||||
|
||||
# now = sunrise -1s -> 'before sunset' with offset +1h true
|
||||
now = datetime(2015, 9, 16, 13, 32, 42, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 6
|
||||
|
||||
# now = local midnight-1s -> 'after sunrise' with offset +1h not true
|
||||
now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 6
|
||||
|
||||
|
||||
async def test_if_action_after_sunrise_with_offset(hass, calls):
|
||||
"""
|
||||
Test if action was after sunrise with offset.
|
||||
|
||||
After sunrise is true from sunrise until midnight, local time.
|
||||
"""
|
||||
await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"condition": {
|
||||
"condition": "sun",
|
||||
"after": SUN_EVENT_SUNRISE,
|
||||
"after_offset": "+1:00:00",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# sunrise: 2015-09-16 06:32:43 local, sunset: 2015-09-16 18:55:24 local
|
||||
# sunrise: 2015-09-16 13:32:43 UTC, sunset: 2015-09-17 01:55:24 UTC
|
||||
# now = sunrise - 1s + 1h -> 'after sunrise' with offset +1h not true
|
||||
now = datetime(2015, 9, 16, 14, 32, 42, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
# now = sunrise + 1h -> 'after sunrise' with offset +1h true
|
||||
now = datetime(2015, 9, 16, 14, 32, 43, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = UTC noon -> 'after sunrise' with offset +1h not true
|
||||
now = datetime(2015, 9, 16, 12, 0, 0, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = UTC noon - 1s -> 'after sunrise' with offset +1h not true
|
||||
now = datetime(2015, 9, 16, 11, 59, 59, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = local noon -> 'after sunrise' with offset +1h true
|
||||
now = datetime(2015, 9, 16, 19, 1, 0, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
|
||||
# now = local noon - 1s -> 'after sunrise' with offset +1h true
|
||||
now = datetime(2015, 9, 16, 18, 59, 59, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 3
|
||||
|
||||
# now = sunset -> 'after sunrise' with offset +1h true
|
||||
now = datetime(2015, 9, 17, 1, 55, 24, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 4
|
||||
|
||||
# now = sunset + 1s -> 'after sunrise' with offset +1h true
|
||||
now = datetime(2015, 9, 17, 1, 55, 25, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 5
|
||||
|
||||
# now = local midnight-1s -> 'after sunrise' with offset +1h true
|
||||
now = datetime(2015, 9, 17, 6, 59, 59, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 6
|
||||
|
||||
# now = local midnight -> 'after sunrise' with offset +1h not true
|
||||
now = datetime(2015, 9, 17, 7, 0, 0, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 6
|
||||
|
||||
|
||||
async def test_if_action_after_sunset_with_offset(hass, calls):
|
||||
"""
|
||||
Test if action was after sunset with offset.
|
||||
|
||||
After sunset is true from sunset until midnight, local time.
|
||||
"""
|
||||
await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"condition": {
|
||||
"condition": "sun",
|
||||
"after": "sunset",
|
||||
"after_offset": "+1:00:00",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# sunrise: 2015-09-15 06:32:05 local, sunset: 2015-09-15 18:56:46 local
|
||||
# sunrise: 2015-09-15 13:32:05 UTC, sunset: 2015-09-16 01:56:46 UTC
|
||||
# now = sunset - 1s + 1h -> 'after sunset' with offset +1h not true
|
||||
now = datetime(2015, 9, 16, 2, 56, 45, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
# now = sunset + 1h -> 'after sunset' with offset +1h true
|
||||
now = datetime(2015, 9, 16, 2, 56, 46, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = midnight-1s -> 'after sunset' with offset +1h true
|
||||
now = datetime(2015, 9, 16, 6, 59, 59, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
|
||||
# now = midnight -> 'after sunset' with offset +1h not true
|
||||
now = datetime(2015, 9, 16, 7, 0, 0, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
|
||||
|
||||
async def test_if_action_before_and_after_during(hass, calls):
|
||||
"""
|
||||
Test if action was after sunset and before sunrise.
|
||||
|
||||
This is true from sunrise until sunset.
|
||||
"""
|
||||
await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"condition": {
|
||||
"condition": "sun",
|
||||
"after": SUN_EVENT_SUNRISE,
|
||||
"before": SUN_EVENT_SUNSET,
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# sunrise: 2015-09-16 06:32:43 local, sunset: 2015-09-16 18:55:24 local
|
||||
# sunrise: 2015-09-16 13:32:43 UTC, sunset: 2015-09-17 01:55:24 UTC
|
||||
# now = sunrise - 1s -> 'after sunrise' + 'before sunset' not true
|
||||
now = datetime(2015, 9, 16, 13, 32, 42, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
# now = sunset + 1s -> 'after sunrise' + 'before sunset' not true
|
||||
now = datetime(2015, 9, 17, 1, 55, 25, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
# now = sunrise -> 'after sunrise' + 'before sunset' true
|
||||
now = datetime(2015, 9, 16, 13, 32, 43, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = sunset -> 'after sunrise' + 'before sunset' true
|
||||
now = datetime(2015, 9, 17, 1, 55, 24, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
|
||||
# now = 9AM local -> 'after sunrise' + 'before sunset' true
|
||||
now = datetime(2015, 9, 16, 16, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 3
|
||||
|
||||
|
||||
async def test_if_action_before_sunrise_no_offset_kotzebue(hass, calls):
|
||||
"""
|
||||
Test if action was before sunrise.
|
||||
|
||||
Local timezone: Alaska time
|
||||
Location: Kotzebue, which has a very skewed local timezone with sunrise
|
||||
at 7 AM and sunset at 3AM during summer
|
||||
After sunrise is true from sunrise until midnight, local time.
|
||||
"""
|
||||
tz = dt_util.get_time_zone("America/Anchorage")
|
||||
dt_util.set_default_time_zone(tz)
|
||||
hass.config.latitude = 66.5
|
||||
hass.config.longitude = 162.4
|
||||
await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"condition": {"condition": "sun", "before": SUN_EVENT_SUNRISE},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# sunrise: 2015-07-24 07:17:24 local, sunset: 2015-07-25 03:16:27 local
|
||||
# sunrise: 2015-07-24 15:17:24 UTC, sunset: 2015-07-25 11:16:27 UTC
|
||||
# now = sunrise + 1s -> 'before sunrise' not true
|
||||
now = datetime(2015, 7, 24, 15, 17, 25, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
# now = sunrise -> 'before sunrise' true
|
||||
now = datetime(2015, 7, 24, 15, 17, 24, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = local midnight -> 'before sunrise' true
|
||||
now = datetime(2015, 7, 24, 8, 0, 0, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
|
||||
# now = local midnight - 1s -> 'before sunrise' not true
|
||||
now = datetime(2015, 7, 24, 7, 59, 59, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
|
||||
|
||||
async def test_if_action_after_sunrise_no_offset_kotzebue(hass, calls):
|
||||
"""
|
||||
Test if action was after sunrise.
|
||||
|
||||
Local timezone: Alaska time
|
||||
Location: Kotzebue, which has a very skewed local timezone with sunrise
|
||||
at 7 AM and sunset at 3AM during summer
|
||||
Before sunrise is true from midnight until sunrise, local time.
|
||||
"""
|
||||
tz = dt_util.get_time_zone("America/Anchorage")
|
||||
dt_util.set_default_time_zone(tz)
|
||||
hass.config.latitude = 66.5
|
||||
hass.config.longitude = 162.4
|
||||
await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"condition": {"condition": "sun", "after": SUN_EVENT_SUNRISE},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# sunrise: 2015-07-24 07:17:24 local, sunset: 2015-07-25 03:16:27 local
|
||||
# sunrise: 2015-07-24 15:17:24 UTC, sunset: 2015-07-25 11:16:27 UTC
|
||||
# now = sunrise -> 'after sunrise' true
|
||||
now = datetime(2015, 7, 24, 15, 17, 24, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = sunrise - 1s -> 'after sunrise' not true
|
||||
now = datetime(2015, 7, 24, 15, 17, 23, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = local midnight -> 'after sunrise' not true
|
||||
now = datetime(2015, 7, 24, 8, 0, 1, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = local midnight - 1s -> 'after sunrise' true
|
||||
now = datetime(2015, 7, 24, 7, 59, 59, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
|
||||
|
||||
async def test_if_action_before_sunset_no_offset_kotzebue(hass, calls):
|
||||
"""
|
||||
Test if action was before sunrise.
|
||||
|
||||
Local timezone: Alaska time
|
||||
Location: Kotzebue, which has a very skewed local timezone with sunrise
|
||||
at 7 AM and sunset at 3AM during summer
|
||||
Before sunset is true from midnight until sunset, local time.
|
||||
"""
|
||||
tz = dt_util.get_time_zone("America/Anchorage")
|
||||
dt_util.set_default_time_zone(tz)
|
||||
hass.config.latitude = 66.5
|
||||
hass.config.longitude = 162.4
|
||||
await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"condition": {"condition": "sun", "before": SUN_EVENT_SUNSET},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# sunrise: 2015-07-24 07:17:24 local, sunset: 2015-07-25 03:16:27 local
|
||||
# sunrise: 2015-07-24 15:17:24 UTC, sunset: 2015-07-25 11:16:27 UTC
|
||||
# now = sunrise + 1s -> 'before sunrise' not true
|
||||
now = datetime(2015, 7, 25, 11, 16, 28, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
# now = sunrise -> 'before sunrise' true
|
||||
now = datetime(2015, 7, 25, 11, 16, 27, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = local midnight -> 'before sunrise' true
|
||||
now = datetime(2015, 7, 24, 8, 0, 0, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
|
||||
# now = local midnight - 1s -> 'before sunrise' not true
|
||||
now = datetime(2015, 7, 24, 7, 59, 59, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
|
||||
|
||||
async def test_if_action_after_sunset_no_offset_kotzebue(hass, calls):
|
||||
"""
|
||||
Test if action was after sunrise.
|
||||
|
||||
Local timezone: Alaska time
|
||||
Location: Kotzebue, which has a very skewed local timezone with sunrise
|
||||
at 7 AM and sunset at 3AM during summer
|
||||
After sunset is true from sunset until midnight, local time.
|
||||
"""
|
||||
tz = dt_util.get_time_zone("America/Anchorage")
|
||||
dt_util.set_default_time_zone(tz)
|
||||
hass.config.latitude = 66.5
|
||||
hass.config.longitude = 162.4
|
||||
await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"condition": {"condition": "sun", "after": SUN_EVENT_SUNSET},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# sunrise: 2015-07-24 07:17:24 local, sunset: 2015-07-25 03:16:27 local
|
||||
# sunrise: 2015-07-24 15:17:24 UTC, sunset: 2015-07-25 11:16:27 UTC
|
||||
# now = sunset -> 'after sunset' true
|
||||
now = datetime(2015, 7, 25, 11, 16, 27, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = sunset - 1s -> 'after sunset' not true
|
||||
now = datetime(2015, 7, 25, 11, 16, 26, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = local midnight -> 'after sunset' not true
|
||||
now = datetime(2015, 7, 24, 8, 0, 1, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# now = local midnight - 1s -> 'after sunset' true
|
||||
now = datetime(2015, 7, 24, 7, 59, 59, tzinfo=dt_util.UTC)
|
||||
with patch("homeassistant.util.dt.utcnow", return_value=now):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
@@ -1,788 +0,0 @@
|
||||
"""The tests for the Template automation."""
|
||||
from datetime import timedelta
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
import homeassistant.components.automation as automation
|
||||
from homeassistant.core import Context, callback
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import (
|
||||
assert_setup_component,
|
||||
async_fire_time_changed,
|
||||
async_mock_service,
|
||||
mock_component,
|
||||
)
|
||||
from tests.components.automation import common
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def calls(hass):
|
||||
"""Track calls to a mock service."""
|
||||
return async_mock_service(hass, "test", "automation")
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_comp(hass):
|
||||
"""Initialize components."""
|
||||
mock_component(hass, "group")
|
||||
hass.states.async_set("test.entity", "hello")
|
||||
|
||||
|
||||
async def test_if_fires_on_change_bool(hass, calls):
|
||||
"""Test for firing on boolean change."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "template", "value_template": "{{ true }}"},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
await common.async_turn_off(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.states.async_set("test.entity", "planet")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_on_change_str(hass, calls):
|
||||
"""Test for firing on change."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "template", "value_template": '{{ "true" }}'},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_on_change_str_crazy(hass, calls):
|
||||
"""Test for firing on change."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "template", "value_template": '{{ "TrUE" }}'},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_not_fires_on_change_bool(hass, calls):
|
||||
"""Test for not firing on boolean change."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "template", "value_template": "{{ false }}"},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_if_not_fires_on_change_str(hass, calls):
|
||||
"""Test for not firing on string change."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "template", "value_template": "true"},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_if_not_fires_on_change_str_crazy(hass, calls):
|
||||
"""Test for not firing on string change."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": '{{ "Anything other than true is false." }}',
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_if_fires_on_no_change(hass, calls):
|
||||
"""Test for firing on no change."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "template", "value_template": "{{ true }}"},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
cur_len = len(calls)
|
||||
|
||||
hass.states.async_set("test.entity", "hello")
|
||||
await hass.async_block_till_done()
|
||||
assert cur_len == len(calls)
|
||||
|
||||
|
||||
async def test_if_fires_on_two_change(hass, calls):
|
||||
"""Test for firing on two changes."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "template", "value_template": "{{ true }}"},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# Trigger once
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
# Trigger again
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_template(hass, calls):
|
||||
"""Test for firing on change with template."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": '{{ is_state("test.entity", "world") }}',
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_not_fires_on_change_with_template(hass, calls):
|
||||
"""Test for not firing on change with template."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": '{{ is_state("test.entity", "hello") }}',
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_template_advanced(hass, calls):
|
||||
"""Test for firing on change with template advanced."""
|
||||
context = Context()
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": '{{ is_state("test.entity", "world") }}',
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "{{ trigger.%s }}"
|
||||
% "}} - {{ trigger.".join(
|
||||
(
|
||||
"platform",
|
||||
"entity_id",
|
||||
"from_state.state",
|
||||
"to_state.state",
|
||||
"for",
|
||||
)
|
||||
)
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.states.async_set("test.entity", "world", context=context)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
assert calls[0].context.parent_id == context.id
|
||||
assert "template - test.entity - hello - world - None" == calls[0].data["some"]
|
||||
|
||||
|
||||
async def test_if_fires_on_no_change_with_template_advanced(hass, calls):
|
||||
"""Test for firing on no change with template advanced."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": """{%- if is_state("test.entity", "world") -%}
|
||||
true
|
||||
{%- else -%}
|
||||
false
|
||||
{%- endif -%}""",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# Different state
|
||||
hass.states.async_set("test.entity", "worldz")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
# Different state
|
||||
hass.states.async_set("test.entity", "hello")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_template_2(hass, calls):
|
||||
"""Test for firing on change with template."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": '{{ not is_state("test.entity", "world") }}',
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
hass.states.async_set("test.entity", "home")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
hass.states.async_set("test.entity", "work")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
hass.states.async_set("test.entity", "not_home")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
hass.states.async_set("test.entity", "home")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
|
||||
|
||||
async def test_if_action(hass, calls):
|
||||
"""Test for firing if action."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"condition": [
|
||||
{
|
||||
"condition": "template",
|
||||
"value_template": '{{ is_state("test.entity", "world") }}',
|
||||
}
|
||||
],
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# Condition is not true yet
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
# Change condition to true, but it shouldn't be triggered yet
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
# Condition is true and event is triggered
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_bad_template(hass, calls):
|
||||
"""Test for firing on change with bad template."""
|
||||
with assert_setup_component(0, automation.DOMAIN):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "template", "value_template": "{{ "},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_bad_template_2(hass, calls):
|
||||
"""Test for firing on change with bad template."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": "{{ xyz | round(0) }}",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_wait_template_with_trigger(hass, calls):
|
||||
"""Test using wait template with 'trigger.entity_id'."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": "{{ states.test.entity.state == 'world' }}",
|
||||
},
|
||||
"action": [
|
||||
{"event": "test_event"},
|
||||
{"wait_template": "{{ is_state(trigger.entity_id, 'hello') }}"},
|
||||
{
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "{{ trigger.%s }}"
|
||||
% "}} - {{ trigger.".join(
|
||||
(
|
||||
"platform",
|
||||
"entity_id",
|
||||
"from_state.state",
|
||||
"to_state.state",
|
||||
"for",
|
||||
)
|
||||
)
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@callback
|
||||
def event_handler(event):
|
||||
hass.states.async_set("test.entity", "hello")
|
||||
|
||||
hass.bus.async_listen_once("test_event", event_handler)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
assert calls[0].data["some"] == "template - test.entity - hello - world - None"
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_for(hass, calls):
|
||||
"""Test for firing on change with for."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": "{{ is_state('test.entity', 'world') }}",
|
||||
"for": {"seconds": 5},
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_for_advanced(hass, calls):
|
||||
"""Test for firing on change with for advanced."""
|
||||
context = Context()
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": '{{ is_state("test.entity", "world") }}',
|
||||
"for": {"seconds": 5},
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "{{ trigger.%s }}"
|
||||
% "}} - {{ trigger.".join(
|
||||
(
|
||||
"platform",
|
||||
"entity_id",
|
||||
"from_state.state",
|
||||
"to_state.state",
|
||||
"for",
|
||||
)
|
||||
)
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.states.async_set("test.entity", "world", context=context)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
assert calls[0].context.parent_id == context.id
|
||||
assert "template - test.entity - hello - world - 0:00:05" == calls[0].data["some"]
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_for_0(hass, calls):
|
||||
"""Test for firing on change with for: 0."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": "{{ is_state('test.entity', 'world') }}",
|
||||
"for": {"seconds": 0},
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_for_0_advanced(hass, calls):
|
||||
"""Test for firing on change with for: 0 advanced."""
|
||||
context = Context()
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": '{{ is_state("test.entity", "world") }}',
|
||||
"for": {"seconds": 0},
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "{{ trigger.%s }}"
|
||||
% "}} - {{ trigger.".join(
|
||||
(
|
||||
"platform",
|
||||
"entity_id",
|
||||
"from_state.state",
|
||||
"to_state.state",
|
||||
"for",
|
||||
)
|
||||
)
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.states.async_set("test.entity", "world", context=context)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
assert calls[0].context.parent_id == context.id
|
||||
assert calls[0].data["some"] == "template - test.entity - hello - world - 0:00:00"
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_for_2(hass, calls):
|
||||
"""Test for firing on change with for."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": "{{ is_state('test.entity', 'world') }}",
|
||||
"for": 5,
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_not_fires_on_change_with_for(hass, calls):
|
||||
"""Test for firing on change with for."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": "{{ is_state('test.entity', 'world') }}",
|
||||
"for": {"seconds": 5},
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=4))
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
hass.states.async_set("test.entity", "hello")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=6))
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_if_not_fires_when_turned_off_with_for(hass, calls):
|
||||
"""Test for firing on change with for."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": "{{ is_state('test.entity', 'world') }}",
|
||||
"for": {"seconds": 5},
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=4))
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
await common.async_turn_off(hass)
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=6))
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_for_template_1(hass, calls):
|
||||
"""Test for firing on change with for template."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": "{{ is_state('test.entity', 'world') }}",
|
||||
"for": {"seconds": "{{ 5 }}"},
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_for_template_2(hass, calls):
|
||||
"""Test for firing on change with for template."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": "{{ is_state('test.entity', 'world') }}",
|
||||
"for": "{{ 5 }}",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_on_change_with_for_template_3(hass, calls):
|
||||
"""Test for firing on change with for template."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": "{{ is_state('test.entity', 'world') }}",
|
||||
"for": "00:00:{{ 5 }}",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=10))
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_invalid_for_template_1(hass, calls):
|
||||
"""Test for invalid for template."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "template",
|
||||
"value_template": "{{ is_state('test.entity', 'world') }}",
|
||||
"for": {"seconds": "{{ five }}"},
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
with mock.patch.object(automation.template, "_LOGGER") as mock_logger:
|
||||
hass.states.async_set("test.entity", "world")
|
||||
await hass.async_block_till_done()
|
||||
assert mock_logger.error.called
|
||||
@@ -1,389 +0,0 @@
|
||||
"""The tests for the time automation."""
|
||||
from datetime import timedelta
|
||||
|
||||
import pytest
|
||||
|
||||
import homeassistant.components.automation as automation
|
||||
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.async_mock import Mock, patch
|
||||
from tests.common import (
|
||||
assert_setup_component,
|
||||
async_fire_time_changed,
|
||||
async_mock_service,
|
||||
mock_component,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def calls(hass):
|
||||
"""Track calls to a mock service."""
|
||||
return async_mock_service(hass, "test", "automation")
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_comp(hass):
|
||||
"""Initialize components."""
|
||||
mock_component(hass, "group")
|
||||
|
||||
|
||||
async def test_if_fires_using_at(hass, calls):
|
||||
"""Test for firing at."""
|
||||
now = dt_util.utcnow()
|
||||
|
||||
time_that_will_not_match_right_away = now.replace(
|
||||
hour=4, minute=59, second=0
|
||||
) + timedelta(2)
|
||||
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "time", "at": "5:00:00"},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "{{ trigger.platform }} - {{ trigger.now.hour }}"
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
now = dt_util.utcnow()
|
||||
|
||||
async_fire_time_changed(
|
||||
hass, now.replace(hour=5, minute=0, second=0) + timedelta(2)
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
assert calls[0].data["some"] == "time - 5"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"has_date,has_time", [(True, True), (True, False), (False, True)]
|
||||
)
|
||||
async def test_if_fires_using_at_input_datetime(hass, calls, has_date, has_time):
|
||||
"""Test for firing at input_datetime."""
|
||||
await async_setup_component(
|
||||
hass,
|
||||
"input_datetime",
|
||||
{"input_datetime": {"trigger": {"has_date": has_date, "has_time": has_time}}},
|
||||
)
|
||||
|
||||
now = dt_util.now()
|
||||
|
||||
trigger_dt = now.replace(
|
||||
hour=5 if has_time else 0, minute=0, second=0, microsecond=0
|
||||
) + timedelta(2)
|
||||
|
||||
await hass.services.async_call(
|
||||
"input_datetime",
|
||||
"set_datetime",
|
||||
{
|
||||
ATTR_ENTITY_ID: "input_datetime.trigger",
|
||||
"datetime": str(trigger_dt.replace(tzinfo=None)),
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
time_that_will_not_match_right_away = trigger_dt - timedelta(minutes=1)
|
||||
|
||||
some_data = "{{ trigger.platform }}-{{ trigger.now.day }}-{{ trigger.now.hour }}"
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow",
|
||||
return_value=dt_util.as_utc(time_that_will_not_match_right_away),
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "time", "at": "input_datetime.trigger"},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {"some": some_data},
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
async_fire_time_changed(hass, trigger_dt)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
assert calls[0].data["some"] == f"time-{trigger_dt.day}-{trigger_dt.hour}"
|
||||
|
||||
if has_date:
|
||||
trigger_dt += timedelta(days=1)
|
||||
if has_time:
|
||||
trigger_dt += timedelta(hours=1)
|
||||
|
||||
await hass.services.async_call(
|
||||
"input_datetime",
|
||||
"set_datetime",
|
||||
{
|
||||
ATTR_ENTITY_ID: "input_datetime.trigger",
|
||||
"datetime": str(trigger_dt.replace(tzinfo=None)),
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
async_fire_time_changed(hass, trigger_dt)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 2
|
||||
assert calls[1].data["some"] == f"time-{trigger_dt.day}-{trigger_dt.hour}"
|
||||
|
||||
|
||||
async def test_if_fires_using_multiple_at(hass, calls):
|
||||
"""Test for firing at."""
|
||||
|
||||
now = dt_util.utcnow()
|
||||
|
||||
time_that_will_not_match_right_away = now.replace(
|
||||
hour=4, minute=59, second=0
|
||||
) + timedelta(2)
|
||||
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "time", "at": ["5:00:00", "6:00:00"]},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "{{ trigger.platform }} - {{ trigger.now.hour }}"
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
now = dt_util.utcnow()
|
||||
|
||||
async_fire_time_changed(
|
||||
hass, now.replace(hour=5, minute=0, second=0) + timedelta(2)
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
assert calls[0].data["some"] == "time - 5"
|
||||
|
||||
async_fire_time_changed(
|
||||
hass, now.replace(hour=6, minute=0, second=0) + timedelta(2)
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
assert calls[1].data["some"] == "time - 6"
|
||||
|
||||
|
||||
async def test_if_not_fires_using_wrong_at(hass, calls):
|
||||
"""YAML translates time values to total seconds.
|
||||
|
||||
This should break the before rule.
|
||||
"""
|
||||
now = dt_util.utcnow()
|
||||
|
||||
time_that_will_not_match_right_away = now.replace(
|
||||
year=now.year + 1, hour=1, minute=0, second=0
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
|
||||
):
|
||||
with assert_setup_component(0, automation.DOMAIN):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "time",
|
||||
"at": 3605,
|
||||
# Total seconds. Hour = 3600 second
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
async_fire_time_changed(
|
||||
hass, now.replace(year=now.year + 1, hour=1, minute=0, second=5)
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_if_action_before(hass, calls):
|
||||
"""Test for if action before."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"condition": {"condition": "time", "before": "10:00"},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
before_10 = dt_util.now().replace(hour=8)
|
||||
after_10 = dt_util.now().replace(hour=14)
|
||||
|
||||
with patch("homeassistant.helpers.condition.dt_util.now", return_value=before_10):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
|
||||
with patch("homeassistant.helpers.condition.dt_util.now", return_value=after_10):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_action_after(hass, calls):
|
||||
"""Test for if action after."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"condition": {"condition": "time", "after": "10:00"},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
before_10 = dt_util.now().replace(hour=8)
|
||||
after_10 = dt_util.now().replace(hour=14)
|
||||
|
||||
with patch("homeassistant.helpers.condition.dt_util.now", return_value=before_10):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 0
|
||||
|
||||
with patch("homeassistant.helpers.condition.dt_util.now", return_value=after_10):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_action_one_weekday(hass, calls):
|
||||
"""Test for if action with one weekday."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"condition": {"condition": "time", "weekday": "mon"},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
days_past_monday = dt_util.now().weekday()
|
||||
monday = dt_util.now() - timedelta(days=days_past_monday)
|
||||
tuesday = monday + timedelta(days=1)
|
||||
|
||||
with patch("homeassistant.helpers.condition.dt_util.now", return_value=monday):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
|
||||
with patch("homeassistant.helpers.condition.dt_util.now", return_value=tuesday):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_action_list_weekday(hass, calls):
|
||||
"""Test for action with a list of weekdays."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"condition": {"condition": "time", "weekday": ["mon", "tue"]},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
days_past_monday = dt_util.now().weekday()
|
||||
monday = dt_util.now() - timedelta(days=days_past_monday)
|
||||
tuesday = monday + timedelta(days=1)
|
||||
wednesday = tuesday + timedelta(days=1)
|
||||
|
||||
with patch("homeassistant.helpers.condition.dt_util.now", return_value=monday):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
|
||||
with patch("homeassistant.helpers.condition.dt_util.now", return_value=tuesday):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 2
|
||||
|
||||
with patch("homeassistant.helpers.condition.dt_util.now", return_value=wednesday):
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 2
|
||||
|
||||
|
||||
async def test_untrack_time_change(hass):
|
||||
"""Test for removing tracked time changes."""
|
||||
mock_track_time_change = Mock()
|
||||
with patch(
|
||||
"homeassistant.components.automation.time.async_track_time_change",
|
||||
return_value=mock_track_time_change,
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"alias": "test",
|
||||
"trigger": {
|
||||
"platform": "time",
|
||||
"at": ["5:00:00", "6:00:00", "7:00:00"],
|
||||
},
|
||||
"action": {"service": "test.automation", "data": {"test": "test"}},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
await hass.services.async_call(
|
||||
automation.DOMAIN,
|
||||
SERVICE_TURN_OFF,
|
||||
{ATTR_ENTITY_ID: "automation.test"},
|
||||
blocking=True,
|
||||
)
|
||||
|
||||
assert len(mock_track_time_change.mock_calls) == 3
|
||||
@@ -1,296 +0,0 @@
|
||||
"""The tests for the time_pattern automation."""
|
||||
from asynctest.mock import patch
|
||||
import pytest
|
||||
|
||||
import homeassistant.components.automation as automation
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import async_fire_time_changed, async_mock_service, mock_component
|
||||
from tests.components.automation import common
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def calls(hass):
|
||||
"""Track calls to a mock service."""
|
||||
return async_mock_service(hass, "test", "automation")
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_comp(hass):
|
||||
"""Initialize components."""
|
||||
mock_component(hass, "group")
|
||||
|
||||
|
||||
async def test_if_fires_when_hour_matches(hass, calls):
|
||||
"""Test for firing if hour is matching."""
|
||||
now = dt_util.utcnow()
|
||||
time_that_will_not_match_right_away = dt_util.utcnow().replace(
|
||||
year=now.year + 1, hour=3
|
||||
)
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "time_pattern",
|
||||
"hours": 0,
|
||||
"minutes": "*",
|
||||
"seconds": "*",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
async_fire_time_changed(hass, now.replace(year=now.year + 2, hour=0))
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
await common.async_turn_off(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
async_fire_time_changed(hass, now.replace(year=now.year + 1, hour=0))
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_when_minute_matches(hass, calls):
|
||||
"""Test for firing if minutes are matching."""
|
||||
now = dt_util.utcnow()
|
||||
time_that_will_not_match_right_away = dt_util.utcnow().replace(
|
||||
year=now.year + 1, minute=30
|
||||
)
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "time_pattern",
|
||||
"hours": "*",
|
||||
"minutes": 0,
|
||||
"seconds": "*",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
async_fire_time_changed(hass, now.replace(year=now.year + 2, minute=0))
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_when_second_matches(hass, calls):
|
||||
"""Test for firing if seconds are matching."""
|
||||
now = dt_util.utcnow()
|
||||
time_that_will_not_match_right_away = dt_util.utcnow().replace(
|
||||
year=now.year + 1, second=30
|
||||
)
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "time_pattern",
|
||||
"hours": "*",
|
||||
"minutes": "*",
|
||||
"seconds": 0,
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
async_fire_time_changed(hass, now.replace(year=now.year + 2, second=0))
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_when_all_matches(hass, calls):
|
||||
"""Test for firing if everything matches."""
|
||||
now = dt_util.utcnow()
|
||||
time_that_will_not_match_right_away = dt_util.utcnow().replace(
|
||||
year=now.year + 1, hour=4
|
||||
)
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "time_pattern",
|
||||
"hours": 1,
|
||||
"minutes": 2,
|
||||
"seconds": 3,
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
async_fire_time_changed(
|
||||
hass, now.replace(year=now.year + 2, hour=1, minute=2, second=3)
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_periodic_seconds(hass, calls):
|
||||
"""Test for firing periodically every second."""
|
||||
now = dt_util.utcnow()
|
||||
time_that_will_not_match_right_away = dt_util.utcnow().replace(
|
||||
year=now.year + 1, second=1
|
||||
)
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "time_pattern",
|
||||
"hours": "*",
|
||||
"minutes": "*",
|
||||
"seconds": "/10",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
async_fire_time_changed(
|
||||
hass, now.replace(year=now.year + 2, hour=0, minute=0, second=10)
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) >= 1
|
||||
|
||||
|
||||
async def test_if_fires_periodic_minutes(hass, calls):
|
||||
"""Test for firing periodically every minute."""
|
||||
|
||||
now = dt_util.utcnow()
|
||||
time_that_will_not_match_right_away = dt_util.utcnow().replace(
|
||||
year=now.year + 1, minute=1
|
||||
)
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "time_pattern",
|
||||
"hours": "*",
|
||||
"minutes": "/2",
|
||||
"seconds": "*",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
async_fire_time_changed(
|
||||
hass, now.replace(year=now.year + 2, hour=0, minute=2, second=0)
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_fires_periodic_hours(hass, calls):
|
||||
"""Test for firing periodically every hour."""
|
||||
now = dt_util.utcnow()
|
||||
time_that_will_not_match_right_away = dt_util.utcnow().replace(
|
||||
year=now.year + 1, hour=1
|
||||
)
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "time_pattern",
|
||||
"hours": "/2",
|
||||
"minutes": "*",
|
||||
"seconds": "*",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
async_fire_time_changed(
|
||||
hass, now.replace(year=now.year + 2, hour=2, minute=0, second=0)
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_default_values(hass, calls):
|
||||
"""Test for firing at 2 minutes every hour."""
|
||||
now = dt_util.utcnow()
|
||||
time_that_will_not_match_right_away = dt_util.utcnow().replace(
|
||||
year=now.year + 1, minute=1
|
||||
)
|
||||
with patch(
|
||||
"homeassistant.util.dt.utcnow", return_value=time_that_will_not_match_right_away
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "time_pattern", "minutes": "2"},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
async_fire_time_changed(
|
||||
hass, now.replace(year=now.year + 2, hour=1, minute=2, second=0)
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
async_fire_time_changed(
|
||||
hass, now.replace(year=now.year + 2, hour=1, minute=2, second=1)
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
|
||||
async_fire_time_changed(
|
||||
hass, now.replace(year=now.year + 2, hour=2, minute=2, second=0)
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 2
|
||||
@@ -1,111 +0,0 @@
|
||||
"""The tests for the webhook automation trigger."""
|
||||
import pytest
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
async def setup_http(hass):
|
||||
"""Set up http."""
|
||||
assert await async_setup_component(hass, "http", {})
|
||||
assert await async_setup_component(hass, "webhook", {})
|
||||
|
||||
|
||||
async def test_webhook_json(hass, aiohttp_client):
|
||||
"""Test triggering with a JSON webhook."""
|
||||
events = []
|
||||
|
||||
@callback
|
||||
def store_event(event):
|
||||
"""Helepr to store events."""
|
||||
events.append(event)
|
||||
|
||||
hass.bus.async_listen("test_success", store_event)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"automation",
|
||||
{
|
||||
"automation": {
|
||||
"trigger": {"platform": "webhook", "webhook_id": "json_webhook"},
|
||||
"action": {
|
||||
"event": "test_success",
|
||||
"event_data_template": {"hello": "yo {{ trigger.json.hello }}"},
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
client = await aiohttp_client(hass.http.app)
|
||||
|
||||
await client.post("/api/webhook/json_webhook", json={"hello": "world"})
|
||||
|
||||
assert len(events) == 1
|
||||
assert events[0].data["hello"] == "yo world"
|
||||
|
||||
|
||||
async def test_webhook_post(hass, aiohttp_client):
|
||||
"""Test triggering with a POST webhook."""
|
||||
events = []
|
||||
|
||||
@callback
|
||||
def store_event(event):
|
||||
"""Helepr to store events."""
|
||||
events.append(event)
|
||||
|
||||
hass.bus.async_listen("test_success", store_event)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"automation",
|
||||
{
|
||||
"automation": {
|
||||
"trigger": {"platform": "webhook", "webhook_id": "post_webhook"},
|
||||
"action": {
|
||||
"event": "test_success",
|
||||
"event_data_template": {"hello": "yo {{ trigger.data.hello }}"},
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
client = await aiohttp_client(hass.http.app)
|
||||
|
||||
await client.post("/api/webhook/post_webhook", data={"hello": "world"})
|
||||
|
||||
assert len(events) == 1
|
||||
assert events[0].data["hello"] == "yo world"
|
||||
|
||||
|
||||
async def test_webhook_query(hass, aiohttp_client):
|
||||
"""Test triggering with a query POST webhook."""
|
||||
events = []
|
||||
|
||||
@callback
|
||||
def store_event(event):
|
||||
"""Helepr to store events."""
|
||||
events.append(event)
|
||||
|
||||
hass.bus.async_listen("test_success", store_event)
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"automation",
|
||||
{
|
||||
"automation": {
|
||||
"trigger": {"platform": "webhook", "webhook_id": "query_webhook"},
|
||||
"action": {
|
||||
"event": "test_success",
|
||||
"event_data_template": {"hello": "yo {{ trigger.query.hello }}"},
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
client = await aiohttp_client(hass.http.app)
|
||||
|
||||
await client.post("/api/webhook/query_webhook?hello=world")
|
||||
|
||||
assert len(events) == 1
|
||||
assert events[0].data["hello"] == "yo world"
|
||||
@@ -1,223 +0,0 @@
|
||||
"""The tests for the location automation."""
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import automation, zone
|
||||
from homeassistant.core import Context
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import async_mock_service, mock_component
|
||||
from tests.components.automation import common
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def calls(hass):
|
||||
"""Track calls to a mock service."""
|
||||
return async_mock_service(hass, "test", "automation")
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_comp(hass):
|
||||
"""Initialize components."""
|
||||
mock_component(hass, "group")
|
||||
hass.loop.run_until_complete(
|
||||
async_setup_component(
|
||||
hass,
|
||||
zone.DOMAIN,
|
||||
{
|
||||
"zone": {
|
||||
"name": "test",
|
||||
"latitude": 32.880837,
|
||||
"longitude": -117.237561,
|
||||
"radius": 250,
|
||||
}
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
async def test_if_fires_on_zone_enter(hass, calls):
|
||||
"""Test for firing on zone enter."""
|
||||
context = Context()
|
||||
hass.states.async_set(
|
||||
"test.entity", "hello", {"latitude": 32.881011, "longitude": -117.234758}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "zone",
|
||||
"entity_id": "test.entity",
|
||||
"zone": "zone.test",
|
||||
"event": "enter",
|
||||
},
|
||||
"action": {
|
||||
"service": "test.automation",
|
||||
"data_template": {
|
||||
"some": "{{ trigger.%s }}"
|
||||
% "}} - {{ trigger.".join(
|
||||
(
|
||||
"platform",
|
||||
"entity_id",
|
||||
"from_state.state",
|
||||
"to_state.state",
|
||||
"zone.name",
|
||||
)
|
||||
)
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set(
|
||||
"test.entity",
|
||||
"hello",
|
||||
{"latitude": 32.880586, "longitude": -117.237564},
|
||||
context=context,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
assert calls[0].context.parent_id == context.id
|
||||
assert "zone - test.entity - hello - hello - test" == calls[0].data["some"]
|
||||
|
||||
# Set out of zone again so we can trigger call
|
||||
hass.states.async_set(
|
||||
"test.entity", "hello", {"latitude": 32.881011, "longitude": -117.234758}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
await common.async_turn_off(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.states.async_set(
|
||||
"test.entity", "hello", {"latitude": 32.880586, "longitude": -117.237564}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_not_fires_for_enter_on_zone_leave(hass, calls):
|
||||
"""Test for not firing on zone leave."""
|
||||
hass.states.async_set(
|
||||
"test.entity", "hello", {"latitude": 32.880586, "longitude": -117.237564}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "zone",
|
||||
"entity_id": "test.entity",
|
||||
"zone": "zone.test",
|
||||
"event": "enter",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set(
|
||||
"test.entity", "hello", {"latitude": 32.881011, "longitude": -117.234758}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_if_fires_on_zone_leave(hass, calls):
|
||||
"""Test for firing on zone leave."""
|
||||
hass.states.async_set(
|
||||
"test.entity", "hello", {"latitude": 32.880586, "longitude": -117.237564}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "zone",
|
||||
"entity_id": "test.entity",
|
||||
"zone": "zone.test",
|
||||
"event": "leave",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set(
|
||||
"test.entity", "hello", {"latitude": 32.881011, "longitude": -117.234758}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 1
|
||||
|
||||
|
||||
async def test_if_not_fires_for_leave_on_zone_enter(hass, calls):
|
||||
"""Test for not firing on zone enter."""
|
||||
hass.states.async_set(
|
||||
"test.entity", "hello", {"latitude": 32.881011, "longitude": -117.234758}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {
|
||||
"platform": "zone",
|
||||
"entity_id": "test.entity",
|
||||
"zone": "zone.test",
|
||||
"event": "leave",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.states.async_set(
|
||||
"test.entity", "hello", {"latitude": 32.880586, "longitude": -117.237564}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 0
|
||||
|
||||
|
||||
async def test_zone_condition(hass, calls):
|
||||
"""Test for zone condition."""
|
||||
hass.states.async_set(
|
||||
"test.entity", "hello", {"latitude": 32.880586, "longitude": -117.237564}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
automation.DOMAIN,
|
||||
{
|
||||
automation.DOMAIN: {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"condition": {
|
||||
"condition": "zone",
|
||||
"entity_id": "test.entity",
|
||||
"zone": "zone.test",
|
||||
},
|
||||
"action": {"service": "test.automation"},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
hass.bus.async_fire("test_event")
|
||||
await hass.async_block_till_done()
|
||||
assert len(calls) == 1
|
||||
Reference in new issue
Block a user