forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sun.py
208 lines (165 loc) · 7.54 KB
/
test_sun.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"""The tests for the Sun helpers."""
from datetime import datetime, timedelta
from unittest.mock import patch
import pytest
from homeassistant.const import SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET
from homeassistant.core import HomeAssistant
import homeassistant.helpers.sun as sun
import homeassistant.util.dt as dt_util
def test_next_events(hass: HomeAssistant) -> None:
"""Test retrieving next sun events."""
utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC)
from astral import LocationInfo
import astral.sun
utc_today = utc_now.date()
location = LocationInfo(
latitude=hass.config.latitude, longitude=hass.config.longitude
)
mod = -1
while True:
next_dawn = astral.sun.dawn(
location.observer, date=utc_today + timedelta(days=mod)
)
if next_dawn > utc_now:
break
mod += 1
mod = -1
while True:
next_dusk = astral.sun.dusk(
location.observer, date=utc_today + timedelta(days=mod)
)
if next_dusk > utc_now:
break
mod += 1
mod = -1
while True:
next_midnight = astral.sun.midnight(
location.observer, date=utc_today + timedelta(days=mod)
)
if next_midnight > utc_now:
break
mod += 1
mod = -1
while True:
next_noon = astral.sun.noon(
location.observer, date=utc_today + timedelta(days=mod)
)
if next_noon > utc_now:
break
mod += 1
mod = -1
while True:
next_rising = astral.sun.sunrise(
location.observer, date=utc_today + timedelta(days=mod)
)
if next_rising > utc_now:
break
mod += 1
mod = -1
while True:
next_setting = astral.sun.sunset(
location.observer, utc_today + timedelta(days=mod)
)
if next_setting > utc_now:
break
mod += 1
with patch("homeassistant.helpers.condition.dt_util.utcnow", return_value=utc_now):
assert next_dawn == sun.get_astral_event_next(hass, "dawn")
assert next_dusk == sun.get_astral_event_next(hass, "dusk")
assert next_midnight == sun.get_astral_event_next(hass, "midnight")
assert next_noon == sun.get_astral_event_next(hass, "noon")
assert next_rising == sun.get_astral_event_next(hass, SUN_EVENT_SUNRISE)
assert next_setting == sun.get_astral_event_next(hass, SUN_EVENT_SUNSET)
def test_date_events(hass: HomeAssistant) -> None:
"""Test retrieving next sun events."""
utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC)
from astral import LocationInfo
import astral.sun
utc_today = utc_now.date()
location = LocationInfo(
latitude=hass.config.latitude, longitude=hass.config.longitude
)
dawn = astral.sun.dawn(location.observer, utc_today)
dusk = astral.sun.dusk(location.observer, utc_today)
midnight = astral.sun.midnight(location.observer, utc_today)
noon = astral.sun.noon(location.observer, utc_today)
sunrise = astral.sun.sunrise(location.observer, utc_today)
sunset = astral.sun.sunset(location.observer, utc_today)
assert dawn == sun.get_astral_event_date(hass, "dawn", utc_today)
assert dusk == sun.get_astral_event_date(hass, "dusk", utc_today)
assert midnight == sun.get_astral_event_date(hass, "midnight", utc_today)
assert noon == sun.get_astral_event_date(hass, "noon", utc_today)
assert sunrise == sun.get_astral_event_date(hass, SUN_EVENT_SUNRISE, utc_today)
assert sunset == sun.get_astral_event_date(hass, SUN_EVENT_SUNSET, utc_today)
def test_date_events_default_date(hass: HomeAssistant) -> None:
"""Test retrieving next sun events."""
utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC)
from astral import LocationInfo
import astral.sun
utc_today = utc_now.date()
location = LocationInfo(
latitude=hass.config.latitude, longitude=hass.config.longitude
)
dawn = astral.sun.dawn(location.observer, date=utc_today)
dusk = astral.sun.dusk(location.observer, date=utc_today)
midnight = astral.sun.midnight(location.observer, date=utc_today)
noon = astral.sun.noon(location.observer, date=utc_today)
sunrise = astral.sun.sunrise(location.observer, date=utc_today)
sunset = astral.sun.sunset(location.observer, date=utc_today)
with patch("homeassistant.util.dt.now", return_value=utc_now):
assert dawn == sun.get_astral_event_date(hass, "dawn", utc_today)
assert dusk == sun.get_astral_event_date(hass, "dusk", utc_today)
assert midnight == sun.get_astral_event_date(hass, "midnight", utc_today)
assert noon == sun.get_astral_event_date(hass, "noon", utc_today)
assert sunrise == sun.get_astral_event_date(hass, SUN_EVENT_SUNRISE, utc_today)
assert sunset == sun.get_astral_event_date(hass, SUN_EVENT_SUNSET, utc_today)
def test_date_events_accepts_datetime(hass: HomeAssistant) -> None:
"""Test retrieving next sun events."""
utc_now = datetime(2016, 11, 1, 8, 0, 0, tzinfo=dt_util.UTC)
from astral import LocationInfo
import astral.sun
utc_today = utc_now.date()
location = LocationInfo(
latitude=hass.config.latitude, longitude=hass.config.longitude
)
dawn = astral.sun.dawn(location.observer, date=utc_today)
dusk = astral.sun.dusk(location.observer, date=utc_today)
midnight = astral.sun.midnight(location.observer, date=utc_today)
noon = astral.sun.noon(location.observer, date=utc_today)
sunrise = astral.sun.sunrise(location.observer, date=utc_today)
sunset = astral.sun.sunset(location.observer, date=utc_today)
assert dawn == sun.get_astral_event_date(hass, "dawn", utc_now)
assert dusk == sun.get_astral_event_date(hass, "dusk", utc_now)
assert midnight == sun.get_astral_event_date(hass, "midnight", utc_now)
assert noon == sun.get_astral_event_date(hass, "noon", utc_now)
assert sunrise == sun.get_astral_event_date(hass, SUN_EVENT_SUNRISE, utc_now)
assert sunset == sun.get_astral_event_date(hass, SUN_EVENT_SUNSET, utc_now)
def test_is_up(hass: HomeAssistant) -> None:
"""Test retrieving next sun events."""
utc_now = datetime(2016, 11, 1, 12, 0, 0, tzinfo=dt_util.UTC)
with patch("homeassistant.helpers.condition.dt_util.utcnow", return_value=utc_now):
assert not sun.is_up(hass)
utc_now = datetime(2016, 11, 1, 18, 0, 0, tzinfo=dt_util.UTC)
with patch("homeassistant.helpers.condition.dt_util.utcnow", return_value=utc_now):
assert sun.is_up(hass)
def test_norway_in_june(hass: HomeAssistant) -> None:
"""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)
assert sun.get_astral_event_next(hass, SUN_EVENT_SUNRISE, june) == datetime(
2016, 7, 24, 22, 59, 45, 689645, tzinfo=dt_util.UTC
)
assert sun.get_astral_event_next(hass, SUN_EVENT_SUNSET, june) == datetime(
2016, 7, 25, 22, 17, 13, 503932, tzinfo=dt_util.UTC
)
assert sun.get_astral_event_date(hass, SUN_EVENT_SUNRISE, june) is None
assert sun.get_astral_event_date(hass, SUN_EVENT_SUNSET, june) is None
def test_impossible_elevation(hass: HomeAssistant) -> None:
"""Test altitude where the sun can't set."""
hass.config.latitude = 69.6
hass.config.longitude = 18.8
hass.config.elevation = 10000000
june = datetime(2016, 6, 1, tzinfo=dt_util.UTC)
with pytest.raises(ValueError):
sun.get_astral_event_next(hass, SUN_EVENT_SUNRISE, june)