Several optimizations to automations (#35007)

This commit is contained in:
Franck Nijhof 2020-05-01 16:37:25 +02:00 committed by GitHub
parent f3d79104a7
commit 8c65062271
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 330 additions and 325 deletions

View file

@ -59,7 +59,7 @@ async def test_sunset_trigger(hass, calls):
async_fire_time_changed(hass, trigger_time)
await hass.async_block_till_done()
assert 0 == len(calls)
assert len(calls) == 0
with patch("homeassistant.util.dt.utcnow", return_value=now):
await common.async_turn_on(hass)
@ -67,7 +67,7 @@ async def test_sunset_trigger(hass, calls):
async_fire_time_changed(hass, trigger_time)
await hass.async_block_till_done()
assert 1 == len(calls)
assert len(calls) == 1
async def test_sunrise_trigger(hass, calls):
@ -89,7 +89,7 @@ async def test_sunrise_trigger(hass, calls):
async_fire_time_changed(hass, trigger_time)
await hass.async_block_till_done()
assert 1 == len(calls)
assert len(calls) == 1
async def test_sunset_trigger_with_offset(hass, calls):
@ -121,8 +121,8 @@ async def test_sunset_trigger_with_offset(hass, calls):
async_fire_time_changed(hass, trigger_time)
await hass.async_block_till_done()
assert 1 == len(calls)
assert "sun - sunset - 0:30:00" == calls[0].data["some"]
assert len(calls) == 1
assert calls[0].data["some"] == "sun - sunset - 0:30:00"
async def test_sunrise_trigger_with_offset(hass, calls):
@ -148,7 +148,7 @@ async def test_sunrise_trigger_with_offset(hass, calls):
async_fire_time_changed(hass, trigger_time)
await hass.async_block_till_done()
assert 1 == len(calls)
assert len(calls) == 1
async def test_if_action_before_sunrise_no_offset(hass, calls):
@ -176,28 +176,28 @@ async def test_if_action_before_sunrise_no_offset(hass, calls):
with patch("homeassistant.util.dt.utcnow", return_value=now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert 0 == len(calls)
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 1 == len(calls)
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 2 == len(calls)
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 2 == len(calls)
assert len(calls) == 2
async def test_if_action_after_sunrise_no_offset(hass, calls):
@ -225,28 +225,28 @@ async def test_if_action_after_sunrise_no_offset(hass, calls):
with patch("homeassistant.util.dt.utcnow", return_value=now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert 0 == len(calls)
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 1 == len(calls)
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 1 == len(calls)
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 2 == len(calls)
assert len(calls) == 2
async def test_if_action_before_sunrise_with_offset(hass, calls):
@ -278,56 +278,56 @@ async def test_if_action_before_sunrise_with_offset(hass, calls):
with patch("homeassistant.util.dt.utcnow", return_value=now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert 0 == len(calls)
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 1 == len(calls)
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 1 == len(calls)
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 1 == len(calls)
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 2 == len(calls)
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 2 == len(calls)
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 2 == len(calls)
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 2 == len(calls)
assert len(calls) == 2
async def test_if_action_before_sunset_with_offset(hass, calls):
@ -359,56 +359,56 @@ async def test_if_action_before_sunset_with_offset(hass, calls):
with patch("homeassistant.util.dt.utcnow", return_value=now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert 1 == len(calls)
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 1 == len(calls)
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 2 == len(calls)
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 3 == len(calls)
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 4 == len(calls)
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 5 == len(calls)
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 6 == len(calls)
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 6 == len(calls)
assert len(calls) == 6
async def test_if_action_after_sunrise_with_offset(hass, calls):
@ -440,70 +440,70 @@ async def test_if_action_after_sunrise_with_offset(hass, calls):
with patch("homeassistant.util.dt.utcnow", return_value=now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert 0 == len(calls)
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 1 == len(calls)
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 1 == len(calls)
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 1 == len(calls)
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 2 == len(calls)
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 3 == len(calls)
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 4 == len(calls)
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 5 == len(calls)
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 6 == len(calls)
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 6 == len(calls)
assert len(calls) == 6
async def test_if_action_after_sunset_with_offset(hass, calls):
@ -535,28 +535,28 @@ async def test_if_action_after_sunset_with_offset(hass, calls):
with patch("homeassistant.util.dt.utcnow", return_value=now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert 0 == len(calls)
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 1 == len(calls)
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 2 == len(calls)
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 2 == len(calls)
assert len(calls) == 2
async def test_if_action_before_and_after_during(hass, calls):
@ -588,35 +588,35 @@ async def test_if_action_before_and_after_during(hass, calls):
with patch("homeassistant.util.dt.utcnow", return_value=now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert 0 == len(calls)
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 0 == len(calls)
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 1 == len(calls)
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 2 == len(calls)
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 3 == len(calls)
assert len(calls) == 3
async def test_if_action_before_sunrise_no_offset_kotzebue(hass, calls):
@ -651,28 +651,28 @@ async def test_if_action_before_sunrise_no_offset_kotzebue(hass, calls):
with patch("homeassistant.util.dt.utcnow", return_value=now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert 0 == len(calls)
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 1 == len(calls)
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 2 == len(calls)
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 2 == len(calls)
assert len(calls) == 2
async def test_if_action_after_sunrise_no_offset_kotzebue(hass, calls):
@ -707,28 +707,28 @@ async def test_if_action_after_sunrise_no_offset_kotzebue(hass, calls):
with patch("homeassistant.util.dt.utcnow", return_value=now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert 1 == len(calls)
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 1 == len(calls)
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 1 == len(calls)
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 2 == len(calls)
assert len(calls) == 2
async def test_if_action_before_sunset_no_offset_kotzebue(hass, calls):
@ -763,28 +763,28 @@ async def test_if_action_before_sunset_no_offset_kotzebue(hass, calls):
with patch("homeassistant.util.dt.utcnow", return_value=now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert 0 == len(calls)
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 1 == len(calls)
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 2 == len(calls)
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 2 == len(calls)
assert len(calls) == 2
async def test_if_action_after_sunset_no_offset_kotzebue(hass, calls):
@ -819,25 +819,25 @@ async def test_if_action_after_sunset_no_offset_kotzebue(hass, calls):
with patch("homeassistant.util.dt.utcnow", return_value=now):
hass.bus.async_fire("test_event")
await hass.async_block_till_done()
assert 1 == len(calls)
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 1 == len(calls)
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 1 == len(calls)
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 2 == len(calls)
assert len(calls) == 2