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