Rewrite tod unittest tests to pytest style test functions (#41603)

This commit is contained in:
CurrentThread 2020-10-16 12:10:38 +02:00 committed by GitHub
parent a28f347b2b
commit 904ce66a72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,19 +1,17 @@
"""Test Times of the Day Binary Sensor."""
from datetime import datetime, timedelta
import unittest
import pytest
import pytz
from homeassistant import setup
from homeassistant.const import STATE_OFF, STATE_ON
import homeassistant.core as ha
from homeassistant.helpers.sun import get_astral_event_date, get_astral_event_next
from homeassistant.setup import setup_component
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
from tests.async_mock import patch
from tests.common import assert_setup_component, get_test_home_assistant
from tests.common import assert_setup_component
@pytest.fixture(autouse=True)
@ -22,23 +20,14 @@ def mock_legacy_time(legacy_patchable_time):
yield
class TestBinarySensorTod(unittest.TestCase):
"""Test for Binary sensor tod platform."""
hass = None
# pylint: disable=invalid-name
def setup_method(self, method):
@pytest.fixture(autouse=True)
def setup_fixture(hass):
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.hass.config.latitude = 50.27583
self.hass.config.longitude = 18.98583
hass.config.latitude = 50.27583
hass.config.longitude = 18.98583
def teardown_method(self, method):
"""Stop everything that was started."""
self.hass.stop()
def test_setup(self):
async def test_setup(hass):
"""Test the setup."""
config = {
"binary_sensor": [
@ -59,18 +48,20 @@ class TestBinarySensorTod(unittest.TestCase):
]
}
with assert_setup_component(2):
assert setup.setup_component(self.hass, "binary_sensor", config)
assert await async_setup_component(hass, "binary_sensor", config)
def test_setup_no_sensors(self):
async def test_setup_no_sensors(hass):
"""Test setup with no sensors."""
with assert_setup_component(0):
assert setup.setup_component(
self.hass, "binary_sensor", {"binary_sensor": {"platform": "tod"}}
assert await async_setup_component(
hass, "binary_sensor", {"binary_sensor": {"platform": "tod"}}
)
def test_in_period_on_start(self):
async def test_in_period_on_start(hass):
"""Test simple setting."""
test_time = datetime(2019, 1, 10, 18, 43, 0, tzinfo=self.hass.config.time_zone)
test_time = datetime(2019, 1, 10, 18, 43, 0, tzinfo=hass.config.time_zone)
config = {
"binary_sensor": [
{
@ -85,15 +76,16 @@ class TestBinarySensorTod(unittest.TestCase):
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
return_value=test_time,
):
setup_component(self.hass, "binary_sensor", config)
self.hass.block_till_done()
await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.evening")
state = hass.states.get("binary_sensor.evening")
assert state.state == STATE_ON
def test_midnight_turnover_before_midnight_inside_period(self):
async def test_midnight_turnover_before_midnight_inside_period(hass):
"""Test midnight turnover setting before midnight inside period ."""
test_time = datetime(2019, 1, 10, 22, 30, 0, tzinfo=self.hass.config.time_zone)
test_time = datetime(2019, 1, 10, 22, 30, 0, tzinfo=hass.config.time_zone)
config = {
"binary_sensor": [
{"platform": "tod", "name": "Night", "after": "22:00", "before": "5:00"}
@ -103,15 +95,16 @@ class TestBinarySensorTod(unittest.TestCase):
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
return_value=test_time,
):
setup_component(self.hass, "binary_sensor", config)
self.hass.block_till_done()
await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.night")
state = hass.states.get("binary_sensor.night")
assert state.state == STATE_ON
def test_midnight_turnover_after_midnight_inside_period(self):
async def test_midnight_turnover_after_midnight_inside_period(hass):
"""Test midnight turnover setting before midnight inside period ."""
test_time = self.hass.config.time_zone.localize(
test_time = hass.config.time_zone.localize(
datetime(2019, 1, 10, 21, 0, 0)
).astimezone(pytz.UTC)
config = {
@ -123,30 +116,31 @@ class TestBinarySensorTod(unittest.TestCase):
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
return_value=test_time,
):
setup_component(self.hass, "binary_sensor", config)
self.hass.block_till_done()
await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.night")
state = hass.states.get("binary_sensor.night")
assert state.state == STATE_OFF
self.hass.block_till_done()
await hass.async_block_till_done()
with patch(
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
return_value=test_time + timedelta(hours=1),
):
self.hass.bus.fire(
hass.bus.async_fire(
ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: test_time + timedelta(hours=1)}
)
self.hass.block_till_done()
state = self.hass.states.get("binary_sensor.night")
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.night")
assert state.state == STATE_ON
def test_midnight_turnover_before_midnight_outside_period(self):
async def test_midnight_turnover_before_midnight_outside_period(hass):
"""Test midnight turnover setting before midnight outside period."""
test_time = self.hass.config.time_zone.localize(
test_time = hass.config.time_zone.localize(
datetime(2019, 1, 10, 20, 30, 0)
).astimezone(pytz.UTC)
config = {
@ -158,15 +152,16 @@ class TestBinarySensorTod(unittest.TestCase):
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
return_value=test_time,
):
setup_component(self.hass, "binary_sensor", config)
self.hass.block_till_done()
await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.night")
state = hass.states.get("binary_sensor.night")
assert state.state == STATE_OFF
def test_midnight_turnover_after_midnight_outside_period(self):
async def test_midnight_turnover_after_midnight_outside_period(hass):
"""Test midnight turnover setting before midnight inside period ."""
test_time = self.hass.config.time_zone.localize(
test_time = hass.config.time_zone.localize(
datetime(2019, 1, 10, 20, 0, 0)
).astimezone(pytz.UTC)
@ -179,13 +174,13 @@ class TestBinarySensorTod(unittest.TestCase):
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
return_value=test_time,
):
setup_component(self.hass, "binary_sensor", config)
self.hass.block_till_done()
await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
state = self.hass.states.get("binary_sensor.night")
state = hass.states.get("binary_sensor.night")
assert state.state == STATE_OFF
switchover_time = self.hass.config.time_zone.localize(
switchover_time = hass.config.time_zone.localize(
datetime(2019, 1, 11, 4, 59, 0)
).astimezone(pytz.UTC)
with patch(
@ -193,9 +188,9 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=switchover_time,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: switchover_time})
self.hass.block_till_done()
state = self.hass.states.get("binary_sensor.night")
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: switchover_time})
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.night")
assert state.state == STATE_ON
with patch(
@ -203,25 +198,26 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=switchover_time + timedelta(minutes=1, seconds=1),
):
self.hass.bus.fire(
hass.bus.async_fire(
ha.EVENT_TIME_CHANGED,
{ha.ATTR_NOW: switchover_time + timedelta(minutes=1, seconds=1)},
)
self.hass.block_till_done()
state = self.hass.states.get("binary_sensor.night")
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.night")
assert state.state == STATE_OFF
def test_from_sunrise_to_sunset(self):
async def test_from_sunrise_to_sunset(hass):
"""Test period from sunrise to sunset."""
test_time = self.hass.config.time_zone.localize(
datetime(2019, 1, 12)
).astimezone(pytz.UTC)
test_time = hass.config.time_zone.localize(datetime(2019, 1, 12)).astimezone(
pytz.UTC
)
sunrise = dt_util.as_local(
get_astral_event_date(self.hass, "sunrise", dt_util.as_utc(test_time))
get_astral_event_date(hass, "sunrise", dt_util.as_utc(test_time))
)
sunset = dt_util.as_local(
get_astral_event_date(self.hass, "sunset", dt_util.as_utc(test_time))
get_astral_event_date(hass, "sunset", dt_util.as_utc(test_time))
)
config = {
"binary_sensor": [
@ -239,11 +235,11 @@ class TestBinarySensorTod(unittest.TestCase):
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
return_value=testtime,
):
setup_component(self.hass, "binary_sensor", config)
self.hass.block_till_done()
await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
self.hass.block_till_done()
state = self.hass.states.get(entity_id)
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
testtime = sunrise
@ -252,10 +248,10 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_ON
testtime = sunrise + timedelta(seconds=1)
@ -264,13 +260,13 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_ON
self.hass.block_till_done()
await hass.async_block_till_done()
testtime = sunset + timedelta(seconds=-1)
with patch(
@ -278,13 +274,13 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_ON
self.hass.block_till_done()
await hass.async_block_till_done()
testtime = sunset
with patch(
@ -292,13 +288,13 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
self.hass.block_till_done()
await hass.async_block_till_done()
testtime = sunset + timedelta(seconds=1)
with patch(
@ -306,19 +302,20 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
def test_from_sunset_to_sunrise(self):
async def test_from_sunset_to_sunrise(hass):
"""Test period from sunset to sunrise."""
test_time = self.hass.config.time_zone.localize(
datetime(2019, 1, 12)
).astimezone(pytz.UTC)
sunset = dt_util.as_local(get_astral_event_date(self.hass, "sunset", test_time))
sunrise = dt_util.as_local(get_astral_event_next(self.hass, "sunrise", sunset))
test_time = hass.config.time_zone.localize(datetime(2019, 1, 12)).astimezone(
pytz.UTC
)
sunset = dt_util.as_local(get_astral_event_date(hass, "sunset", test_time))
sunrise = dt_util.as_local(get_astral_event_next(hass, "sunrise", sunset))
# assert sunset == sunrise
config = {
"binary_sensor": [
@ -336,11 +333,11 @@ class TestBinarySensorTod(unittest.TestCase):
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
return_value=testtime,
):
setup_component(self.hass, "binary_sensor", config)
self.hass.block_till_done()
await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
self.hass.block_till_done()
state = self.hass.states.get(entity_id)
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
testtime = sunset
@ -349,10 +346,10 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_ON
testtime = sunset + timedelta(minutes=1)
@ -361,10 +358,10 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_ON
testtime = sunrise + timedelta(minutes=-1)
@ -373,10 +370,10 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_ON
testtime = sunrise
@ -385,11 +382,11 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
self.hass.block_till_done()
state = hass.states.get(entity_id)
await hass.async_block_till_done()
# assert state == "dupa"
assert state.state == STATE_OFF
@ -399,21 +396,22 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
def test_offset(self):
"""Test offset."""
after = self.hass.config.time_zone.localize(
datetime(2019, 1, 10, 18, 0, 0)
).astimezone(pytz.UTC) + timedelta(hours=1, minutes=34)
before = self.hass.config.time_zone.localize(
datetime(2019, 1, 10, 22, 0, 0)
).astimezone(pytz.UTC) + timedelta(hours=1, minutes=45)
async def test_offset(hass):
"""Test offset."""
after = hass.config.time_zone.localize(datetime(2019, 1, 10, 18, 0, 0)).astimezone(
pytz.UTC
) + timedelta(hours=1, minutes=34)
before = hass.config.time_zone.localize(datetime(2019, 1, 10, 22, 0, 0)).astimezone(
pytz.UTC
) + timedelta(hours=1, minutes=45)
entity_id = "binary_sensor.evening"
config = {
@ -433,10 +431,10 @@ class TestBinarySensorTod(unittest.TestCase):
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
return_value=testtime,
):
setup_component(self.hass, "binary_sensor", config)
self.hass.block_till_done()
await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
testtime = after
@ -444,10 +442,10 @@ class TestBinarySensorTod(unittest.TestCase):
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_ON
testtime = before + timedelta(seconds=-1)
@ -455,10 +453,10 @@ class TestBinarySensorTod(unittest.TestCase):
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_ON
testtime = before
@ -466,10 +464,10 @@ class TestBinarySensorTod(unittest.TestCase):
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
testtime = before + timedelta(seconds=1)
@ -477,17 +475,18 @@ class TestBinarySensorTod(unittest.TestCase):
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
def test_offset_overnight(self):
async def test_offset_overnight(hass):
"""Test offset overnight."""
after = self.hass.config.time_zone.localize(
datetime(2019, 1, 10, 18, 0, 0)
).astimezone(pytz.UTC) + timedelta(hours=1, minutes=34)
after = hass.config.time_zone.localize(datetime(2019, 1, 10, 18, 0, 0)).astimezone(
pytz.UTC
) + timedelta(hours=1, minutes=34)
entity_id = "binary_sensor.evening"
config = {
"binary_sensor": [
@ -506,10 +505,10 @@ class TestBinarySensorTod(unittest.TestCase):
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
return_value=testtime,
):
setup_component(self.hass, "binary_sensor", config)
self.hass.block_till_done()
await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
testtime = after
@ -517,25 +516,26 @@ class TestBinarySensorTod(unittest.TestCase):
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_ON
def test_norwegian_case_winter(self):
"""Test location in Norway where the sun doesn't set in summer."""
self.hass.config.latitude = 69.6
self.hass.config.longitude = 18.8
test_time = self.hass.config.time_zone.localize(
datetime(2010, 1, 1)
).astimezone(pytz.UTC)
async def test_norwegian_case_winter(hass):
"""Test location in Norway where the sun doesn't set in summer."""
hass.config.latitude = 69.6
hass.config.longitude = 18.8
test_time = hass.config.time_zone.localize(datetime(2010, 1, 1)).astimezone(
pytz.UTC
)
sunrise = dt_util.as_local(
get_astral_event_next(self.hass, "sunrise", dt_util.as_utc(test_time))
get_astral_event_next(hass, "sunrise", dt_util.as_utc(test_time))
)
sunset = dt_util.as_local(
get_astral_event_next(self.hass, "sunset", dt_util.as_utc(test_time))
get_astral_event_next(hass, "sunset", dt_util.as_utc(test_time))
)
config = {
"binary_sensor": [
@ -553,11 +553,11 @@ class TestBinarySensorTod(unittest.TestCase):
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
return_value=testtime,
):
setup_component(self.hass, "binary_sensor", config)
self.hass.block_till_done()
await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
self.hass.block_till_done()
state = self.hass.states.get(entity_id)
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
testtime = sunrise + timedelta(seconds=-1)
@ -566,10 +566,10 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
testtime = sunrise
@ -578,10 +578,10 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_ON
testtime = sunrise + timedelta(seconds=1)
@ -590,13 +590,13 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_ON
self.hass.block_till_done()
await hass.async_block_till_done()
testtime = sunset + timedelta(seconds=-1)
with patch(
@ -604,13 +604,13 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_ON
self.hass.block_till_done()
await hass.async_block_till_done()
testtime = sunset
with patch(
@ -618,13 +618,13 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
self.hass.block_till_done()
await hass.async_block_till_done()
testtime = sunset + timedelta(seconds=1)
with patch(
@ -632,26 +632,27 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
def test_norwegian_case_summer(self):
"""Test location in Norway where the sun doesn't set in summer."""
self.hass.config.latitude = 69.6
self.hass.config.longitude = 18.8
test_time = self.hass.config.time_zone.localize(
datetime(2010, 6, 1)
).astimezone(pytz.UTC)
async def test_norwegian_case_summer(hass):
"""Test location in Norway where the sun doesn't set in summer."""
hass.config.latitude = 69.6
hass.config.longitude = 18.8
test_time = hass.config.time_zone.localize(datetime(2010, 6, 1)).astimezone(
pytz.UTC
)
sunrise = dt_util.as_local(
get_astral_event_next(self.hass, "sunrise", dt_util.as_utc(test_time))
get_astral_event_next(hass, "sunrise", dt_util.as_utc(test_time))
)
sunset = dt_util.as_local(
get_astral_event_next(self.hass, "sunset", dt_util.as_utc(test_time))
get_astral_event_next(hass, "sunset", dt_util.as_utc(test_time))
)
config = {
"binary_sensor": [
@ -669,11 +670,11 @@ class TestBinarySensorTod(unittest.TestCase):
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
return_value=testtime,
):
setup_component(self.hass, "binary_sensor", config)
self.hass.block_till_done()
await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
self.hass.block_till_done()
state = self.hass.states.get(entity_id)
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
testtime = sunrise + timedelta(seconds=-1)
@ -682,10 +683,10 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
testtime = sunrise
@ -694,10 +695,10 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_ON
testtime = sunrise + timedelta(seconds=1)
@ -706,13 +707,13 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_ON
self.hass.block_till_done()
await hass.async_block_till_done()
testtime = sunset + timedelta(seconds=-1)
with patch(
@ -720,13 +721,13 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_ON
self.hass.block_till_done()
await hass.async_block_till_done()
testtime = sunset
with patch(
@ -734,13 +735,13 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
self.hass.block_till_done()
await hass.async_block_till_done()
testtime = sunset + timedelta(seconds=1)
with patch(
@ -748,23 +749,24 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
def test_sun_offset(self):
async def test_sun_offset(hass):
"""Test sun event with offset."""
test_time = self.hass.config.time_zone.localize(
datetime(2019, 1, 12)
).astimezone(pytz.UTC)
test_time = hass.config.time_zone.localize(datetime(2019, 1, 12)).astimezone(
pytz.UTC
)
sunrise = dt_util.as_local(
get_astral_event_date(self.hass, "sunrise", dt_util.as_utc(test_time))
get_astral_event_date(hass, "sunrise", dt_util.as_utc(test_time))
+ timedelta(hours=-1, minutes=-30)
)
sunset = dt_util.as_local(
get_astral_event_date(self.hass, "sunset", dt_util.as_utc(test_time))
get_astral_event_date(hass, "sunset", dt_util.as_utc(test_time))
+ timedelta(hours=1, minutes=30)
)
config = {
@ -785,11 +787,11 @@ class TestBinarySensorTod(unittest.TestCase):
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
return_value=testtime,
):
setup_component(self.hass, "binary_sensor", config)
self.hass.block_till_done()
await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
self.hass.block_till_done()
state = self.hass.states.get(entity_id)
await hass.async_block_till_done()
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
testtime = sunrise
@ -798,10 +800,10 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_ON
testtime = sunrise + timedelta(seconds=1)
@ -810,13 +812,13 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_ON
self.hass.block_till_done()
await hass.async_block_till_done()
testtime = sunset + timedelta(seconds=-1)
with patch(
@ -824,13 +826,13 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_ON
self.hass.block_till_done()
await hass.async_block_till_done()
testtime = sunset
with patch(
@ -838,13 +840,13 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
self.hass.block_till_done()
await hass.async_block_till_done()
testtime = sunset + timedelta(seconds=1)
with patch(
@ -852,15 +854,15 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_OFF
test_time = test_time + timedelta(days=1)
sunrise = dt_util.as_local(
get_astral_event_date(self.hass, "sunrise", dt_util.as_utc(test_time))
get_astral_event_date(hass, "sunrise", dt_util.as_utc(test_time))
+ timedelta(hours=-1, minutes=-30)
)
testtime = sunrise
@ -869,16 +871,17 @@ class TestBinarySensorTod(unittest.TestCase):
return_value=testtime,
):
self.hass.bus.fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
self.hass.block_till_done()
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: testtime})
await hass.async_block_till_done()
state = self.hass.states.get(entity_id)
state = hass.states.get(entity_id)
assert state.state == STATE_ON
def test_dst(self):
async def test_dst(hass):
"""Test sun event with offset."""
self.hass.config.time_zone = pytz.timezone("CET")
test_time = self.hass.config.time_zone.localize(
hass.config.time_zone = pytz.timezone("CET")
test_time = hass.config.time_zone.localize(
datetime(2019, 3, 30, 3, 0, 0)
).astimezone(pytz.UTC)
config = {
@ -895,11 +898,11 @@ class TestBinarySensorTod(unittest.TestCase):
"homeassistant.components.tod.binary_sensor.dt_util.utcnow",
return_value=testtime,
):
setup_component(self.hass, "binary_sensor", config)
self.hass.block_till_done()
await async_setup_component(hass, "binary_sensor", config)
await hass.async_block_till_done()
self.hass.block_till_done()
state = self.hass.states.get(entity_id)
await hass.async_block_till_done()
state = hass.states.get(entity_id)
state.attributes["after"] == "2019-03-31T03:30:00+02:00"
state.attributes["before"] == "2019-03-31T03:40:00+02:00"
state.attributes["next_update"] == "2019-03-31T03:30:00+02:00"