2016-03-09 09:25:50 +00:00
|
|
|
"""The tests for the Sun component."""
|
2019-05-15 07:02:29 +00:00
|
|
|
from datetime import datetime, timedelta
|
2014-11-25 07:15:14 +00:00
|
|
|
|
2019-05-15 07:02:29 +00:00
|
|
|
from pytest import mark
|
|
|
|
|
|
|
|
import homeassistant.components.sun as sun
|
|
|
|
from homeassistant.const import EVENT_STATE_CHANGED
|
2019-12-09 13:38:01 +00:00
|
|
|
import homeassistant.core as ha
|
2019-05-15 07:02:29 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2019-12-09 13:38:01 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2019-05-15 07:02:29 +00:00
|
|
|
|
2020-05-03 18:27:19 +00:00
|
|
|
from tests.async_mock import patch
|
|
|
|
|
2019-05-15 07:02:29 +00:00
|
|
|
|
2020-06-29 16:39:24 +00:00
|
|
|
async def test_setting_rising(hass, legacy_patchable_time):
|
2019-05-15 07:02:29 +00:00
|
|
|
"""Test retrieving sun setting and rising."""
|
|
|
|
utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC)
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.helpers.condition.dt_util.utcnow", return_value=utc_now):
|
|
|
|
await async_setup_component(
|
|
|
|
hass, sun.DOMAIN, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}}
|
|
|
|
)
|
2019-05-15 07:02:29 +00:00
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
state = hass.states.get(sun.ENTITY_ID)
|
|
|
|
|
|
|
|
from astral import Astral
|
|
|
|
|
|
|
|
astral = Astral()
|
|
|
|
utc_today = utc_now.date()
|
|
|
|
|
|
|
|
latitude = hass.config.latitude
|
|
|
|
longitude = hass.config.longitude
|
|
|
|
|
|
|
|
mod = -1
|
|
|
|
while True:
|
2019-07-31 19:25:30 +00:00
|
|
|
next_dawn = astral.dawn_utc(
|
|
|
|
utc_today + timedelta(days=mod), latitude, longitude
|
|
|
|
)
|
2019-05-15 07:02:29 +00:00
|
|
|
if next_dawn > utc_now:
|
|
|
|
break
|
|
|
|
mod += 1
|
|
|
|
|
|
|
|
mod = -1
|
|
|
|
while True:
|
2019-07-31 19:25:30 +00:00
|
|
|
next_dusk = astral.dusk_utc(
|
|
|
|
utc_today + timedelta(days=mod), latitude, longitude
|
|
|
|
)
|
2019-05-15 07:02:29 +00:00
|
|
|
if next_dusk > utc_now:
|
|
|
|
break
|
|
|
|
mod += 1
|
|
|
|
|
|
|
|
mod = -1
|
|
|
|
while True:
|
2019-07-31 19:25:30 +00:00
|
|
|
next_midnight = astral.solar_midnight_utc(
|
|
|
|
utc_today + timedelta(days=mod), longitude
|
|
|
|
)
|
2019-05-15 07:02:29 +00:00
|
|
|
if next_midnight > utc_now:
|
|
|
|
break
|
|
|
|
mod += 1
|
|
|
|
|
|
|
|
mod = -1
|
|
|
|
while True:
|
2019-07-31 19:25:30 +00:00
|
|
|
next_noon = astral.solar_noon_utc(utc_today + timedelta(days=mod), longitude)
|
2019-05-15 07:02:29 +00:00
|
|
|
if next_noon > utc_now:
|
|
|
|
break
|
|
|
|
mod += 1
|
|
|
|
|
|
|
|
mod = -1
|
|
|
|
while True:
|
2019-07-31 19:25:30 +00:00
|
|
|
next_rising = astral.sunrise_utc(
|
|
|
|
utc_today + timedelta(days=mod), latitude, longitude
|
|
|
|
)
|
2019-05-15 07:02:29 +00:00
|
|
|
if next_rising > utc_now:
|
|
|
|
break
|
|
|
|
mod += 1
|
|
|
|
|
|
|
|
mod = -1
|
|
|
|
while True:
|
2019-07-31 19:25:30 +00:00
|
|
|
next_setting = astral.sunset_utc(
|
|
|
|
utc_today + timedelta(days=mod), latitude, longitude
|
|
|
|
)
|
2019-05-15 07:02:29 +00:00
|
|
|
if next_setting > utc_now:
|
|
|
|
break
|
|
|
|
mod += 1
|
|
|
|
|
|
|
|
assert next_dawn == dt_util.parse_datetime(
|
2019-07-31 19:25:30 +00:00
|
|
|
state.attributes[sun.STATE_ATTR_NEXT_DAWN]
|
|
|
|
)
|
2019-05-15 07:02:29 +00:00
|
|
|
assert next_dusk == dt_util.parse_datetime(
|
2019-07-31 19:25:30 +00:00
|
|
|
state.attributes[sun.STATE_ATTR_NEXT_DUSK]
|
|
|
|
)
|
2019-05-15 07:02:29 +00:00
|
|
|
assert next_midnight == dt_util.parse_datetime(
|
2019-07-31 19:25:30 +00:00
|
|
|
state.attributes[sun.STATE_ATTR_NEXT_MIDNIGHT]
|
|
|
|
)
|
2019-05-15 07:02:29 +00:00
|
|
|
assert next_noon == dt_util.parse_datetime(
|
2019-07-31 19:25:30 +00:00
|
|
|
state.attributes[sun.STATE_ATTR_NEXT_NOON]
|
|
|
|
)
|
2019-05-15 07:02:29 +00:00
|
|
|
assert next_rising == dt_util.parse_datetime(
|
2019-07-31 19:25:30 +00:00
|
|
|
state.attributes[sun.STATE_ATTR_NEXT_RISING]
|
|
|
|
)
|
2019-05-15 07:02:29 +00:00
|
|
|
assert next_setting == dt_util.parse_datetime(
|
2019-07-31 19:25:30 +00:00
|
|
|
state.attributes[sun.STATE_ATTR_NEXT_SETTING]
|
|
|
|
)
|
2019-05-15 07:02:29 +00:00
|
|
|
|
|
|
|
|
2020-06-29 16:39:24 +00:00
|
|
|
async def test_state_change(hass, legacy_patchable_time):
|
2019-05-15 07:02:29 +00:00
|
|
|
"""Test if the state changes at next setting/rising."""
|
|
|
|
now = datetime(2016, 6, 1, 8, 0, 0, tzinfo=dt_util.UTC)
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.helpers.condition.dt_util.utcnow", return_value=now):
|
|
|
|
await async_setup_component(
|
|
|
|
hass, sun.DOMAIN, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}}
|
|
|
|
)
|
2019-05-15 07:02:29 +00:00
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
test_time = dt_util.parse_datetime(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.states.get(sun.ENTITY_ID).attributes[sun.STATE_ATTR_NEXT_RISING]
|
|
|
|
)
|
2019-05-15 07:02:29 +00:00
|
|
|
assert test_time is not None
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert sun.STATE_BELOW_HORIZON == hass.states.get(sun.ENTITY_ID).state
|
2019-05-15 07:02:29 +00:00
|
|
|
|
2020-08-29 03:06:06 +00:00
|
|
|
patched_time = test_time + timedelta(seconds=5)
|
|
|
|
with patch(
|
|
|
|
"homeassistant.helpers.condition.dt_util.utcnow", return_value=patched_time
|
|
|
|
):
|
|
|
|
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: patched_time})
|
|
|
|
await hass.async_block_till_done()
|
2019-05-15 07:02:29 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert sun.STATE_ABOVE_HORIZON == hass.states.get(sun.ENTITY_ID).state
|
2019-05-15 07:02:29 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.helpers.condition.dt_util.utcnow", return_value=now):
|
|
|
|
await hass.config.async_update(longitude=hass.config.longitude + 90)
|
2019-06-08 06:21:41 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert sun.STATE_ABOVE_HORIZON == hass.states.get(sun.ENTITY_ID).state
|
2019-06-08 06:21:41 +00:00
|
|
|
|
2019-05-15 07:02:29 +00:00
|
|
|
|
|
|
|
async def test_norway_in_june(hass):
|
|
|
|
"""Test location in Norway where the sun doesn't set in summer."""
|
|
|
|
hass.config.latitude = 69.6
|
|
|
|
hass.config.longitude = 18.8
|
|
|
|
|
|
|
|
june = datetime(2016, 6, 1, tzinfo=dt_util.UTC)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.helpers.condition.dt_util.utcnow", return_value=june):
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass, sun.DOMAIN, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}}
|
|
|
|
)
|
2019-05-15 07:02:29 +00:00
|
|
|
|
|
|
|
state = hass.states.get(sun.ENTITY_ID)
|
|
|
|
assert state is not None
|
|
|
|
|
|
|
|
assert dt_util.parse_datetime(
|
2019-07-31 19:25:30 +00:00
|
|
|
state.attributes[sun.STATE_ATTR_NEXT_RISING]
|
|
|
|
) == datetime(2016, 7, 25, 23, 23, 39, tzinfo=dt_util.UTC)
|
2019-05-15 07:02:29 +00:00
|
|
|
assert dt_util.parse_datetime(
|
2019-07-31 19:25:30 +00:00
|
|
|
state.attributes[sun.STATE_ATTR_NEXT_SETTING]
|
|
|
|
) == datetime(2016, 7, 26, 22, 19, 1, tzinfo=dt_util.UTC)
|
2019-05-15 07:02:29 +00:00
|
|
|
|
2019-06-08 06:21:41 +00:00
|
|
|
assert state.state == sun.STATE_ABOVE_HORIZON
|
|
|
|
|
2019-05-15 07:02:29 +00:00
|
|
|
|
|
|
|
@mark.skip
|
|
|
|
async def test_state_change_count(hass):
|
|
|
|
"""Count the number of state change events in a location."""
|
|
|
|
# Skipped because it's a bit slow. Has been validated with
|
|
|
|
# multiple lattitudes and dates
|
|
|
|
hass.config.latitude = 10
|
|
|
|
hass.config.longitude = 0
|
|
|
|
|
|
|
|
now = datetime(2016, 6, 1, tzinfo=dt_util.UTC)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("homeassistant.helpers.condition.dt_util.utcnow", return_value=now):
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass, sun.DOMAIN, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}}
|
|
|
|
)
|
2019-05-15 07:02:29 +00:00
|
|
|
|
|
|
|
events = []
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-05-15 07:02:29 +00:00
|
|
|
@ha.callback
|
|
|
|
def state_change_listener(event):
|
2019-07-31 19:25:30 +00:00
|
|
|
if event.data.get("entity_id") == "sun.sun":
|
2019-05-15 07:02:29 +00:00
|
|
|
events.append(event)
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-05-15 07:02:29 +00:00
|
|
|
hass.bus.async_listen(EVENT_STATE_CHANGED, state_change_listener)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
for _ in range(24 * 60 * 60):
|
2019-05-15 07:02:29 +00:00
|
|
|
now += timedelta(seconds=1)
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: now})
|
2019-05-15 07:02:29 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert len(events) < 721
|