mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Convert automation tests to async (#17794)
* Convert automation tests to async * Fix 8 last tests * Lint
This commit is contained in:
@@ -1,47 +1,216 @@
|
||||
"""The tests for the time automation."""
|
||||
from datetime import timedelta
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.setup import setup_component
|
||||
import pytest
|
||||
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
import homeassistant.components.automation as automation
|
||||
|
||||
from tests.common import (
|
||||
fire_time_changed, get_test_home_assistant, assert_setup_component,
|
||||
mock_component)
|
||||
async_fire_time_changed, assert_setup_component, mock_component)
|
||||
from tests.components.automation import common
|
||||
from tests.common import async_mock_service
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
class TestAutomationTime(unittest.TestCase):
|
||||
"""Test the event automation."""
|
||||
@pytest.fixture
|
||||
def calls(hass):
|
||||
"""Track calls to a mock serivce."""
|
||||
return async_mock_service(hass, 'test', 'automation')
|
||||
|
||||
def setUp(self):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
mock_component(self.hass, 'group')
|
||||
self.calls = []
|
||||
|
||||
@callback
|
||||
def record_call(service):
|
||||
"""Record calls."""
|
||||
self.calls.append(service)
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_comp(hass):
|
||||
"""Initialize components."""
|
||||
mock_component(hass, 'group')
|
||||
|
||||
self.hass.services.register('test', 'automation', record_call)
|
||||
|
||||
def tearDown(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
async def test_if_fires_when_hour_matches(hass, calls):
|
||||
"""Test for firing if hour is matching."""
|
||||
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'time',
|
||||
'hours': 0,
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
def test_if_fires_when_hour_matches(self):
|
||||
"""Test for firing if hour is matching."""
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
async_fire_time_changed(hass, dt_util.utcnow().replace(hour=0))
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
|
||||
await common.async_turn_off(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
async_fire_time_changed(hass, dt_util.utcnow().replace(hour=0))
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
|
||||
|
||||
async def test_if_fires_when_minute_matches(hass, calls):
|
||||
"""Test for firing if minutes are matching."""
|
||||
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'time',
|
||||
'minutes': 0,
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
async_fire_time_changed(hass, dt_util.utcnow().replace(minute=0))
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
|
||||
|
||||
async def test_if_fires_when_second_matches(hass, calls):
|
||||
"""Test for firing if seconds are matching."""
|
||||
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'time',
|
||||
'seconds': 0,
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
async_fire_time_changed(hass, dt_util.utcnow().replace(second=0))
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
|
||||
|
||||
async def test_if_fires_when_all_matches(hass, calls):
|
||||
"""Test for firing if everything matches."""
|
||||
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'time',
|
||||
'hours': 1,
|
||||
'minutes': 2,
|
||||
'seconds': 3,
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
async_fire_time_changed(hass, dt_util.utcnow().replace(
|
||||
hour=1, minute=2, second=3))
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
|
||||
|
||||
async def test_if_fires_periodic_seconds(hass, calls):
|
||||
"""Test for firing periodically every second."""
|
||||
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'time',
|
||||
'seconds': "/2",
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
async_fire_time_changed(hass, dt_util.utcnow().replace(
|
||||
hour=0, minute=0, second=2))
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
|
||||
|
||||
async def test_if_fires_periodic_minutes(hass, calls):
|
||||
"""Test for firing periodically every minute."""
|
||||
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'time',
|
||||
'minutes': "/2",
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
async_fire_time_changed(hass, dt_util.utcnow().replace(
|
||||
hour=0, minute=2, second=0))
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
|
||||
|
||||
async def test_if_fires_periodic_hours(hass, calls):
|
||||
"""Test for firing periodically every hour."""
|
||||
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'time',
|
||||
'hours': "/2",
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
async_fire_time_changed(hass, dt_util.utcnow().replace(
|
||||
hour=2, minute=0, second=0))
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
|
||||
|
||||
async def test_if_fires_using_at(hass, calls):
|
||||
"""Test for firing at."""
|
||||
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 }}'
|
||||
},
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
async_fire_time_changed(hass, dt_util.utcnow().replace(
|
||||
hour=5, minute=0, second=0))
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert 1 == len(calls)
|
||||
assert 'time - 5' == calls[0].data['some']
|
||||
|
||||
|
||||
async def test_if_not_working_if_no_values_in_conf_provided(hass, calls):
|
||||
"""Test for failure if no configuration."""
|
||||
with assert_setup_component(0):
|
||||
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'time',
|
||||
'hours': 0,
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
@@ -49,24 +218,25 @@ class TestAutomationTime(unittest.TestCase):
|
||||
}
|
||||
})
|
||||
|
||||
fire_time_changed(self.hass, dt_util.utcnow().replace(hour=0))
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(self.calls)
|
||||
async_fire_time_changed(hass, dt_util.utcnow().replace(
|
||||
hour=5, minute=0, second=0))
|
||||
|
||||
common.turn_off(self.hass)
|
||||
self.hass.block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
assert 0 == len(calls)
|
||||
|
||||
fire_time_changed(self.hass, dt_util.utcnow().replace(hour=0))
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(self.calls)
|
||||
|
||||
def test_if_fires_when_minute_matches(self):
|
||||
"""Test for firing if minutes are matching."""
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
async def test_if_not_fires_using_wrong_at(hass, calls):
|
||||
"""YAML translates time values to total seconds.
|
||||
|
||||
This should break the before rule.
|
||||
"""
|
||||
with assert_setup_component(0):
|
||||
assert await async_setup_component(hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'time',
|
||||
'minutes': 0,
|
||||
'at': 3605,
|
||||
# Total seconds. Hour = 3600 second
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
@@ -74,328 +244,162 @@ class TestAutomationTime(unittest.TestCase):
|
||||
}
|
||||
})
|
||||
|
||||
fire_time_changed(self.hass, dt_util.utcnow().replace(minute=0))
|
||||
async_fire_time_changed(hass, dt_util.utcnow().replace(
|
||||
hour=1, minute=0, second=5))
|
||||
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(self.calls)
|
||||
await hass.async_block_till_done()
|
||||
assert 0 == len(calls)
|
||||
|
||||
def test_if_fires_when_second_matches(self):
|
||||
"""Test for firing if seconds are matching."""
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'time',
|
||||
'seconds': 0,
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
}
|
||||
|
||||
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'
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
fire_time_changed(self.hass, dt_util.utcnow().replace(second=0))
|
||||
before_10 = dt_util.now().replace(hour=8)
|
||||
after_10 = dt_util.now().replace(hour=14)
|
||||
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(self.calls)
|
||||
with patch('homeassistant.helpers.condition.dt_util.now',
|
||||
return_value=before_10):
|
||||
hass.bus.async_fire('test_event')
|
||||
await hass.async_block_till_done()
|
||||
|
||||
def test_if_fires_when_all_matches(self):
|
||||
"""Test for firing if everything matches."""
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'time',
|
||||
'hours': 1,
|
||||
'minutes': 2,
|
||||
'seconds': 3,
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
}
|
||||
assert 1 == len(calls)
|
||||
|
||||
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 1 == len(calls)
|
||||
|
||||
|
||||
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'
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
fire_time_changed(self.hass, dt_util.utcnow().replace(
|
||||
hour=1, minute=2, second=3))
|
||||
before_10 = dt_util.now().replace(hour=8)
|
||||
after_10 = dt_util.now().replace(hour=14)
|
||||
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(self.calls)
|
||||
with patch('homeassistant.helpers.condition.dt_util.now',
|
||||
return_value=before_10):
|
||||
hass.bus.async_fire('test_event')
|
||||
await hass.async_block_till_done()
|
||||
|
||||
def test_if_fires_periodic_seconds(self):
|
||||
"""Test for firing periodically every second."""
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'time',
|
||||
'seconds': "/2",
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
}
|
||||
assert 0 == len(calls)
|
||||
|
||||
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 1 == len(calls)
|
||||
|
||||
|
||||
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'
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
fire_time_changed(self.hass, dt_util.utcnow().replace(
|
||||
hour=0, minute=0, second=2))
|
||||
days_past_monday = dt_util.now().weekday()
|
||||
monday = dt_util.now() - timedelta(days=days_past_monday)
|
||||
tuesday = monday + timedelta(days=1)
|
||||
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(self.calls)
|
||||
with patch('homeassistant.helpers.condition.dt_util.now',
|
||||
return_value=monday):
|
||||
hass.bus.async_fire('test_event')
|
||||
await hass.async_block_till_done()
|
||||
|
||||
def test_if_fires_periodic_minutes(self):
|
||||
"""Test for firing periodically every minute."""
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'time',
|
||||
'minutes': "/2",
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
}
|
||||
assert 1 == len(calls)
|
||||
|
||||
with patch('homeassistant.helpers.condition.dt_util.now',
|
||||
return_value=tuesday):
|
||||
hass.bus.async_fire('test_event')
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert 1 == len(calls)
|
||||
|
||||
|
||||
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'
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
fire_time_changed(self.hass, dt_util.utcnow().replace(
|
||||
hour=0, minute=2, second=0))
|
||||
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)
|
||||
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(self.calls)
|
||||
with patch('homeassistant.helpers.condition.dt_util.now',
|
||||
return_value=monday):
|
||||
hass.bus.async_fire('test_event')
|
||||
await hass.async_block_till_done()
|
||||
|
||||
def test_if_fires_periodic_hours(self):
|
||||
"""Test for firing periodically every hour."""
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'time',
|
||||
'hours': "/2",
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
}
|
||||
}
|
||||
})
|
||||
assert 1 == len(calls)
|
||||
|
||||
fire_time_changed(self.hass, dt_util.utcnow().replace(
|
||||
hour=2, minute=0, second=0))
|
||||
with patch('homeassistant.helpers.condition.dt_util.now',
|
||||
return_value=tuesday):
|
||||
hass.bus.async_fire('test_event')
|
||||
await hass.async_block_till_done()
|
||||
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(self.calls)
|
||||
assert 2 == len(calls)
|
||||
|
||||
def test_if_fires_using_at(self):
|
||||
"""Test for firing at."""
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'time',
|
||||
'at': '5:00:00',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation',
|
||||
'data_template': {
|
||||
'some': '{{ trigger.platform }} - '
|
||||
'{{ trigger.now.hour }}'
|
||||
},
|
||||
}
|
||||
}
|
||||
})
|
||||
with patch('homeassistant.helpers.condition.dt_util.now',
|
||||
return_value=wednesday):
|
||||
hass.bus.async_fire('test_event')
|
||||
await hass.async_block_till_done()
|
||||
|
||||
fire_time_changed(self.hass, dt_util.utcnow().replace(
|
||||
hour=5, minute=0, second=0))
|
||||
|
||||
self.hass.block_till_done()
|
||||
assert 1 == len(self.calls)
|
||||
assert 'time - 5' == self.calls[0].data['some']
|
||||
|
||||
def test_if_not_working_if_no_values_in_conf_provided(self):
|
||||
"""Test for failure if no configuration."""
|
||||
with assert_setup_component(0):
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'time',
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
fire_time_changed(self.hass, dt_util.utcnow().replace(
|
||||
hour=5, minute=0, second=0))
|
||||
|
||||
self.hass.block_till_done()
|
||||
assert 0 == len(self.calls)
|
||||
|
||||
def test_if_not_fires_using_wrong_at(self):
|
||||
"""YAML translates time values to total seconds.
|
||||
|
||||
This should break the before rule.
|
||||
"""
|
||||
with assert_setup_component(0):
|
||||
assert setup_component(self.hass, automation.DOMAIN, {
|
||||
automation.DOMAIN: {
|
||||
'trigger': {
|
||||
'platform': 'time',
|
||||
'at': 3605,
|
||||
# Total seconds. Hour = 3600 second
|
||||
},
|
||||
'action': {
|
||||
'service': 'test.automation'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
fire_time_changed(self.hass, dt_util.utcnow().replace(
|
||||
hour=1, minute=0, second=5))
|
||||
|
||||
self.hass.block_till_done()
|
||||
assert 0 == len(self.calls)
|
||||
|
||||
def test_if_action_before(self):
|
||||
"""Test for if action before."""
|
||||
assert setup_component(self.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):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert 1 == len(self.calls)
|
||||
|
||||
with patch('homeassistant.helpers.condition.dt_util.now',
|
||||
return_value=after_10):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert 1 == len(self.calls)
|
||||
|
||||
def test_if_action_after(self):
|
||||
"""Test for if action after."""
|
||||
assert setup_component(self.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):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert 0 == len(self.calls)
|
||||
|
||||
with patch('homeassistant.helpers.condition.dt_util.now',
|
||||
return_value=after_10):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert 1 == len(self.calls)
|
||||
|
||||
def test_if_action_one_weekday(self):
|
||||
"""Test for if action with one weekday."""
|
||||
assert setup_component(self.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):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert 1 == len(self.calls)
|
||||
|
||||
with patch('homeassistant.helpers.condition.dt_util.now',
|
||||
return_value=tuesday):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert 1 == len(self.calls)
|
||||
|
||||
def test_if_action_list_weekday(self):
|
||||
"""Test for action with a list of weekdays."""
|
||||
assert setup_component(self.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):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert 1 == len(self.calls)
|
||||
|
||||
with patch('homeassistant.helpers.condition.dt_util.now',
|
||||
return_value=tuesday):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert 2 == len(self.calls)
|
||||
|
||||
with patch('homeassistant.helpers.condition.dt_util.now',
|
||||
return_value=wednesday):
|
||||
self.hass.bus.fire('test_event')
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert 2 == len(self.calls)
|
||||
assert 2 == len(calls)
|
||||
|
||||
Reference in New Issue
Block a user