pyastroweatherio 0.24.0.dev12__tar.gz → 0.24.0.dev13__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (18) hide show
  1. {pyastroweatherio-0.24.0.dev12 → pyastroweatherio-0.24.0.dev13}/PKG-INFO +1 -1
  2. {pyastroweatherio-0.24.0.dev12 → pyastroweatherio-0.24.0.dev13}/pyastroweatherio/client.py +4 -4
  3. pyastroweatherio-0.24.0.dev13/pyastroweatherio/helper_functions.py +401 -0
  4. {pyastroweatherio-0.24.0.dev12 → pyastroweatherio-0.24.0.dev13}/pyastroweatherio.egg-info/PKG-INFO +1 -1
  5. pyastroweatherio-0.24.0.dev13/pyastroweatherio.egg-info/requires.txt +2 -0
  6. {pyastroweatherio-0.24.0.dev12 → pyastroweatherio-0.24.0.dev13}/setup.py +2 -2
  7. pyastroweatherio-0.24.0.dev12/pyastroweatherio/helper_functions.py +0 -353
  8. pyastroweatherio-0.24.0.dev12/pyastroweatherio.egg-info/requires.txt +0 -3
  9. {pyastroweatherio-0.24.0.dev12 → pyastroweatherio-0.24.0.dev13}/LICENSE +0 -0
  10. {pyastroweatherio-0.24.0.dev12 → pyastroweatherio-0.24.0.dev13}/README.md +0 -0
  11. {pyastroweatherio-0.24.0.dev12 → pyastroweatherio-0.24.0.dev13}/pyastroweatherio/__init__.py +0 -0
  12. {pyastroweatherio-0.24.0.dev12 → pyastroweatherio-0.24.0.dev13}/pyastroweatherio/const.py +0 -0
  13. {pyastroweatherio-0.24.0.dev12 → pyastroweatherio-0.24.0.dev13}/pyastroweatherio/dataclasses.py +0 -0
  14. {pyastroweatherio-0.24.0.dev12 → pyastroweatherio-0.24.0.dev13}/pyastroweatherio/errors.py +0 -0
  15. {pyastroweatherio-0.24.0.dev12 → pyastroweatherio-0.24.0.dev13}/pyastroweatherio.egg-info/SOURCES.txt +0 -0
  16. {pyastroweatherio-0.24.0.dev12 → pyastroweatherio-0.24.0.dev13}/pyastroweatherio.egg-info/dependency_links.txt +0 -0
  17. {pyastroweatherio-0.24.0.dev12 → pyastroweatherio-0.24.0.dev13}/pyastroweatherio.egg-info/top_level.txt +0 -0
  18. {pyastroweatherio-0.24.0.dev12 → pyastroweatherio-0.24.0.dev13}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pyastroweatherio
3
- Version: 0.24.0.dev12
3
+ Version: 0.24.0.dev13
4
4
  Summary: Python Wrapper for 7Timer and Met.no REST API
5
5
  Home-page: https://github.com/mawinkler/pyastroweatherio
6
6
  Author: Markus Winkler
@@ -81,8 +81,8 @@ class AstroWeather:
81
81
  )
82
82
 
83
83
  # Testing
84
- self.test_mode = False
85
- self.dump_json = False
84
+ self.test_mode = True
85
+ self.dump_json = True
86
86
 
87
87
  # Public functions
88
88
  async def get_location_data(
@@ -259,7 +259,7 @@ class AstroWeather:
259
259
  # )
260
260
  await self._astro_routines.need_update()
261
261
 
262
- utc_to_local_diff = await self._astro_routines.utc_to_local_diff()
262
+ utc_to_local_diff = self._astro_routines.utc_to_local_diff()
263
263
  _LOGGER.debug("UTC to local diff: %s", str(utc_to_local_diff))
264
264
  _LOGGER.debug("Forecast length: %s", str(len(self._weather_data_seventimer)))
265
265
 
@@ -381,7 +381,7 @@ class AstroWeather:
381
381
  # )
382
382
  await self._astro_routines.need_update()
383
383
 
384
- utc_to_local_diff = await self._astro_routines.utc_to_local_diff()
384
+ utc_to_local_diff = self._astro_routines.utc_to_local_diff()
385
385
 
386
386
  # Create forecast
387
387
  forecast_dayname = ""
@@ -0,0 +1,401 @@
1
+ """ Contains Helper functions for AstroWeather."""
2
+ from datetime import datetime, timedelta
3
+ import logging
4
+ import ephem
5
+ from ephem import degree
6
+ from math import degrees as deg
7
+ import pytz
8
+
9
+ # The introduction of the module [timezonefinder](https://github.com/jannikmi/timezonefinder)
10
+ # with it's nested dependency to [py-h3](https://github.com/uber/h3-py) failed while compiling
11
+ # the `c`-module h3 on some home assistant deployment variants (e.g. Home Assistant
12
+ # Operating System on RPi).
13
+ # from timezonefinder import TimezoneFinder
14
+ from pyastroweatherio.const import (
15
+ DEFAULT_ELEVATION,
16
+ DEFAULT_TIMEZONE,
17
+ HOME_LATITUDE,
18
+ HOME_LONGITUDE,
19
+ CIVIL_TWILIGHT,
20
+ CIVIL_DUSK_DAWN,
21
+ NAUTICAL_TWILIGHT,
22
+ NAUTICAL_DUSK_DAWN,
23
+ ASTRONOMICAL_TWILIGHT,
24
+ ASTRONOMICAL_DUSK_DAWN,
25
+ )
26
+
27
+ _LOGGER = logging.getLogger(__name__)
28
+
29
+
30
+ class ConversionFunctions:
31
+ """Convert between different Weather Units."""
32
+
33
+ async def epoch_to_datetime(self, value) -> str:
34
+ """Converts EPOC time to Date Time String."""
35
+ return datetime.datetime.fromtimestamp(int(value)).strftime("%Y-%m-%d %H:%M:%S")
36
+
37
+ async def anchor_timestamp(self, value) -> datetime:
38
+ """Converts the datetime string from 7Timer to DateTime."""
39
+ return datetime.strptime(value, "%Y%m%d%H")
40
+
41
+
42
+ class AstronomicalRoutines:
43
+ """Calculate different astronomical objects"""
44
+
45
+ def __init__(
46
+ self,
47
+ latitude=HOME_LATITUDE,
48
+ longitude=HOME_LONGITUDE,
49
+ elevation=DEFAULT_ELEVATION,
50
+ timezone_info=DEFAULT_TIMEZONE,
51
+ forecast_time=datetime.utcnow(),
52
+ ):
53
+ self._latitude = latitude
54
+ self._longitude = longitude
55
+ self._elevation = elevation
56
+ self._timezone_info = timezone_info
57
+ # tz_find = TimezoneFinder()
58
+ # self._timezone_info = tz_find.timezone_at(lng=longitude, lat=latitude)
59
+ _LOGGER.debug("Timezone: %s", self._timezone_info)
60
+ self._forecast_time = forecast_time
61
+
62
+ self._sun_observer = None
63
+ self._sun_observer_nautical = None
64
+ self._sun_observer_astro = None
65
+ self._moon_observer = None
66
+ self._sun_next_rising_civil = None
67
+ self._sun_next_setting_civil = None
68
+ self._sun_next_rising_nautical = None
69
+ self._sun_next_setting_nautical = None
70
+ self._sun_next_rising_astro = None
71
+ self._sun_next_setting_astro = None
72
+ self._sun_altitude = None
73
+ self._sun_azimuth = None
74
+ self._moon_next_rising = None
75
+ self._moon_next_setting = None
76
+ self._moon_altitude = None
77
+ self._moon_azimuth = None
78
+ self._sun = None
79
+ self._moon = None
80
+
81
+ # self.calculate_sun()
82
+ # self.calculate_moon()
83
+
84
+ def utc_to_local(self, utc_dt):
85
+ """Localizes the datetime"""
86
+ local_tz = pytz.timezone(self._timezone_info)
87
+ local_dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(local_tz)
88
+ return local_tz.normalize(local_dt)
89
+
90
+ def utc_to_local_diff(self):
91
+ """returns the UTC Offset"""
92
+ local = self.utc_to_local(datetime.now())
93
+ return local.utcoffset().seconds // 3600
94
+
95
+ async def need_update(self):
96
+ localtime = self.utc_to_local(datetime.utcnow())
97
+ if (
98
+ self._sun_next_setting_civil is None
99
+ or self._sun_next_setting_nautical is None
100
+ or self._sun_next_setting_astro is None
101
+ or self._sun_next_rising_astro is None
102
+ or self._sun_next_rising_nautical is None
103
+ or self._sun_next_rising_civil is None
104
+ or self._moon_next_rising is None
105
+ or self._moon_next_setting is None
106
+ or localtime > self._sun_next_setting_civil
107
+ or localtime > self._sun_next_setting_nautical
108
+ or localtime > self._sun_next_setting_astro
109
+ or localtime > self._sun_next_rising_astro
110
+ or localtime > self._sun_next_rising_nautical
111
+ or localtime > self._sun_next_rising_civil
112
+ or localtime > self._moon_next_rising
113
+ or localtime > self._moon_next_setting
114
+ ):
115
+ self.calculate_sun()
116
+ self.calculate_moon()
117
+ else:
118
+ _LOGGER.debug("Astronomical calculations are up to date")
119
+
120
+ def get_sun_observer(self, below_horizon=ASTRONOMICAL_DUSK_DAWN) -> ephem.Observer:
121
+ """Retrieves the ephem sun observer for the current location"""
122
+ observer = ephem.Observer()
123
+ observer.lon = self._longitude * degree
124
+ observer.lat = self._latitude * degree
125
+ observer.elevation = self._elevation
126
+ observer.horizon = below_horizon * degree
127
+ observer.pressure = 0
128
+ observer.epoch = datetime.now().strftime("%Y/%m/%d")
129
+ return observer
130
+
131
+ def get_moon_observer(self) -> ephem.Observer:
132
+ """Retrieves the ephem mon observer for the current location"""
133
+ observer = ephem.Observer()
134
+ observer.lon = self._longitude * degree
135
+ observer.lat = self._latitude * degree
136
+ observer.elevation = self._elevation
137
+ observer.horizon = 0
138
+ observer.pressure = 0
139
+ observer.epoch = datetime.now().strftime("%Y/%m/%d")
140
+ return observer
141
+
142
+ def calculate_sun(self):
143
+ """Calculates sun risings and settings"""
144
+ if self._sun_observer is None:
145
+ self._sun_observer = self.get_sun_observer(CIVIL_DUSK_DAWN)
146
+ if self._sun_observer_nautical is None:
147
+ self._sun_observer_nautical = self.get_sun_observer(NAUTICAL_DUSK_DAWN)
148
+ if self._sun_observer_astro is None:
149
+ self._sun_observer_astro = self.get_sun_observer(ASTRONOMICAL_DUSK_DAWN)
150
+ if self._sun is None:
151
+ self._sun = ephem.Sun()
152
+
153
+ # Alt / Az
154
+ self._sun_observer.date = self.utc_to_local(self._forecast_time)
155
+ self._sun.compute(self._sun_observer)
156
+
157
+ self._sun_altitude = deg(float(self._sun.alt))
158
+ self._sun_azimuth = deg(float(self._sun.az))
159
+
160
+ try:
161
+ self._sun_next_rising_civil = self.utc_to_local(
162
+ self._sun_observer.next_rising(ephem.Sun(), use_center=True).datetime()
163
+ )
164
+ except (ephem.AlwaysUpError, ephem.NeverUpError):
165
+ # Search for the next rising
166
+ start = self._sun_observer.date.datetime()
167
+ end = self._sun_observer.date.datetime() + timedelta(days=365)
168
+ timestamp = start
169
+ while timestamp < end:
170
+ timestamp += timedelta(minutes=1440)
171
+ self._sun_observer.date = timestamp
172
+ try:
173
+ self._sun_next_rising_astro = self.utc_to_local(
174
+ self._sun_observer.next_rising(ephem.Sun(), use_center=True).datetime()
175
+ )
176
+ except (ephem.AlwaysUpError, ephem.NeverUpError):
177
+ continue
178
+ break
179
+
180
+ try:
181
+ self._sun_next_setting_civil = self.utc_to_local(
182
+ self._sun_observer.next_setting(ephem.Sun(), use_center=True).datetime()
183
+ )
184
+ except (ephem.AlwaysUpError, ephem.NeverUpError):
185
+ # Search for the next setting
186
+ start = self._sun_observer.date.datetime()
187
+ end = self._sun_observer.date.datetime() + timedelta(days=365)
188
+ timestamp = start
189
+ while timestamp < end:
190
+ timestamp += timedelta(minutes=1440)
191
+ self._sun_observer.date = timestamp
192
+ try:
193
+ self._sun_next_setting_astro = self.utc_to_local(
194
+ self._sun_observer.next_setting(ephem.Sun(), use_center=True).datetime()
195
+ )
196
+ except (ephem.AlwaysUpError, ephem.NeverUpError):
197
+ continue
198
+ break
199
+
200
+ # Rise and Setting (Nautical)
201
+ self._sun_observer_nautical.date = self.utc_to_local(self._forecast_time)
202
+ self._sun.compute(self._sun_observer_nautical)
203
+
204
+ try:
205
+ self._sun_next_rising_nautical = self.utc_to_local(
206
+ self._sun_observer_nautical.next_rising(ephem.Sun(), use_center=True).datetime()
207
+ )
208
+ except (ephem.AlwaysUpError, ephem.NeverUpError):
209
+ # Search for the next astronomical rising
210
+ start = self._sun_observer_nautical.date.datetime()
211
+ end = self._sun_observer_nautical.date.datetime() + timedelta(days=365)
212
+ timestamp = start
213
+ while timestamp < end:
214
+ timestamp += timedelta(minutes=1440)
215
+ self._sun_observer_nautical.date = timestamp
216
+ try:
217
+ self._sun_next_rising_nautical = self.utc_to_local(
218
+ self._sun_observer_nautical.next_rising(ephem.Sun(), use_center=True).datetime()
219
+ )
220
+ except (ephem.AlwaysUpError, ephem.NeverUpError):
221
+ continue
222
+ break
223
+
224
+ try:
225
+ self._sun_next_setting_nautical = self.utc_to_local(
226
+ self._sun_observer_nautical.next_setting(ephem.Sun(), use_center=True).datetime()
227
+ )
228
+ except (ephem.AlwaysUpError, ephem.NeverUpError):
229
+ # Search for the next astronomical setting
230
+ start = self._sun_observer_nautical.date.datetime()
231
+ end = self._sun_observer_nautical.date.datetime() + timedelta(days=365)
232
+ timestamp = start
233
+ while timestamp < end:
234
+ timestamp += timedelta(minutes=1440)
235
+ self._sun_observer_nautical.date = timestamp
236
+ try:
237
+ self._sun_next_setting_nautical = self.utc_to_local(
238
+ self._sun_observer_nautical.next_setting(ephem.Sun(), use_center=True).datetime()
239
+ )
240
+ except (ephem.AlwaysUpError, ephem.NeverUpError):
241
+ continue
242
+ break
243
+
244
+ # Rise and Setting (Astronomical)
245
+ self._sun_observer_astro.date = self.utc_to_local(self._forecast_time)
246
+ self._sun.compute(self._sun_observer_astro)
247
+
248
+ try:
249
+ self._sun_next_rising_astro = self.utc_to_local(
250
+ self._sun_observer_astro.next_rising(ephem.Sun(), use_center=True).datetime()
251
+ )
252
+ except (ephem.AlwaysUpError, ephem.NeverUpError):
253
+ # Search for the next astronomical rising
254
+ start = self._sun_observer_astro.date.datetime()
255
+ end = self._sun_observer_astro.date.datetime() + timedelta(days=365)
256
+ timestamp = start
257
+ while timestamp < end:
258
+ timestamp += timedelta(minutes=1440)
259
+ self._sun_observer_astro.date = timestamp
260
+ try:
261
+ self._sun_next_rising_astro = self.utc_to_local(
262
+ self._sun_observer_astro.next_rising(ephem.Sun(), use_center=True).datetime()
263
+ )
264
+ except (ephem.AlwaysUpError, ephem.NeverUpError):
265
+ continue
266
+ break
267
+
268
+ try:
269
+ self._sun_next_setting_astro = self.utc_to_local(
270
+ self._sun_observer_astro.next_setting(ephem.Sun(), use_center=True).datetime()
271
+ )
272
+ except (ephem.AlwaysUpError, ephem.NeverUpError):
273
+ # Search for the next astronomical setting
274
+ start = self._sun_observer_astro.date.datetime()
275
+ end = self._sun_observer_astro.date.datetime() + timedelta(days=365)
276
+ timestamp = start
277
+ while timestamp < end:
278
+ timestamp += timedelta(minutes=1440)
279
+ self._sun_observer_astro.date = timestamp
280
+ try:
281
+ self._sun_next_setting_astro = self.utc_to_local(
282
+ self._sun_observer_astro.next_setting(ephem.Sun(), use_center=True).datetime()
283
+ )
284
+ except (ephem.AlwaysUpError, ephem.NeverUpError):
285
+ continue
286
+ break
287
+
288
+ def calculate_moon(self):
289
+ """Calculates moon rising and setting"""
290
+ if self._moon_observer is None:
291
+ self._moon_observer = self.get_moon_observer()
292
+ if self._moon is None:
293
+ self._moon = ephem.Moon()
294
+
295
+ # Alt / Az
296
+ self._moon_observer.date = self.utc_to_local(self._forecast_time)
297
+ self._moon.compute(self._moon_observer)
298
+
299
+ self._moon_altitude = deg(float(self._moon.alt))
300
+ self._moon_azimuth = deg(float(self._moon.az))
301
+
302
+ # Rise and Setting
303
+ self._moon_observer.date = self._forecast_time
304
+ self._moon.compute(self._moon_observer)
305
+
306
+ try:
307
+ self._moon_next_rising = self.utc_to_local(
308
+ self._moon_observer.next_rising(ephem.Moon(), use_center=True).datetime()
309
+ )
310
+ except (ephem.AlwaysUpError, ephem.NeverUpError):
311
+ pass
312
+
313
+ try:
314
+ self._moon_next_setting = self.utc_to_local(
315
+ self._moon_observer.next_setting(ephem.Moon(), use_center=True).datetime()
316
+ )
317
+ except (ephem.AlwaysUpError, ephem.NeverUpError):
318
+ pass
319
+
320
+
321
+ async def sun_next_rising(self) -> datetime:
322
+ """Returns sun next rising"""
323
+ if self._sun_next_rising_astro is not None:
324
+ return self._sun_next_rising_astro
325
+ if self._sun_next_rising_nautical is not None:
326
+ return self._sun_next_rising_nautical
327
+ if self._sun_next_rising_civil is not None:
328
+ return self._sun_next_rising_civil
329
+
330
+ async def sun_next_rising_civil(self) -> datetime:
331
+ """Returns sun next rising"""
332
+ if self._sun_next_rising_civil is not None:
333
+ return self._sun_next_rising_civil
334
+
335
+ async def sun_next_rising_nautical(self) -> datetime:
336
+ """Returns sun next nautical rising"""
337
+ if self._sun_next_rising_nautical is not None:
338
+ return self._sun_next_rising_nautical
339
+
340
+ async def sun_next_rising_astro(self) -> datetime:
341
+ """Returns sun next astronomical rising"""
342
+ if self._sun_next_rising_astro is not None:
343
+ return self._sun_next_rising_astro
344
+
345
+ async def sun_next_setting(self) -> datetime:
346
+ """Returns sun next setting"""
347
+ if self._sun_next_setting_astro is not None:
348
+ return self._sun_next_setting_astro
349
+ if self._sun_next_setting_nautical is not None:
350
+ return self._sun_next_setting_nautical
351
+ if self._sun_next_setting_civil is not None:
352
+ return self._sun_next_setting_civil
353
+
354
+ async def sun_next_setting_civil(self) -> datetime:
355
+ """Returns sun next setting"""
356
+ if self._sun_next_setting_civil is not None:
357
+ return self._sun_next_setting_civil
358
+
359
+ async def sun_next_setting_nautical(self) -> datetime:
360
+ """Returns sun next nautical setting"""
361
+ if self._sun_next_setting_nautical is not None:
362
+ return self._sun_next_setting_nautical
363
+
364
+ async def sun_next_setting_astro(self) -> datetime:
365
+ """Returns sun next astronomical setting"""
366
+ if self._sun_next_setting_astro is not None:
367
+ return self._sun_next_setting_astro
368
+
369
+ async def sun_altitude(self) -> float:
370
+ """Returns the sun altitude"""
371
+ if self._sun_altitude is not None:
372
+ return self._sun_altitude
373
+
374
+ async def sun_azimuth(self) -> float:
375
+ """Returns the sun azimuth"""
376
+ if self._sun_azimuth is not None:
377
+ return self._sun_azimuth
378
+
379
+ async def moon_next_rising(self) -> datetime:
380
+ """Returns moon next rising"""
381
+ if self._moon_next_rising is not None:
382
+ return self._moon_next_rising
383
+
384
+ async def moon_next_setting(self) -> datetime:
385
+ """Returns moon next setting"""
386
+ if self._moon_next_setting is not None:
387
+ return self._moon_next_setting
388
+
389
+ async def moon_phase(self) -> float:
390
+ """Returns the moon phase"""
391
+ return self._moon.phase
392
+
393
+ async def moon_altitude(self) -> float:
394
+ """Returns the moon altitude"""
395
+ if self._moon_altitude is not None:
396
+ return self._moon_altitude
397
+
398
+ async def moon_azimuth(self) -> float:
399
+ """Returns the moon azimuth"""
400
+ if self._moon_azimuth is not None:
401
+ return self._moon_azimuth
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: pyastroweatherio
3
- Version: 0.24.0.dev12
3
+ Version: 0.24.0.dev13
4
4
  Summary: Python Wrapper for 7Timer and Met.no REST API
5
5
  Home-page: https://github.com/mawinkler/pyastroweatherio
6
6
  Author: Markus Winkler
@@ -3,7 +3,7 @@ from setuptools import setup
3
3
  setup(
4
4
  name="pyastroweatherio",
5
5
  packages=["pyastroweatherio"],
6
- version="0.24.0.dev12",
6
+ version="0.24.0.dev13",
7
7
  license="MIT",
8
8
  description="Python Wrapper for 7Timer and Met.no REST API",
9
9
  long_description=" ".join(
@@ -16,7 +16,7 @@ setup(
16
16
  author_email="winkler.info@icloud.com",
17
17
  url="https://github.com/mawinkler/pyastroweatherio",
18
18
  keywords=["AstroWeather", "7Timer", "Met.no", "Python"],
19
- install_requires=["aiohttp", "astroplan", "astropy"],
19
+ install_requires=["aiohttp", "pyephem"],
20
20
  classifiers=[
21
21
  # Chose either "3 - Alpha", "4 - Beta" or "5 - Production/Stable" as the current state of your package
22
22
  "Development Status :: 4 - Beta",
@@ -1,353 +0,0 @@
1
- """ Contains Helper functions for AstroWeather."""
2
- from datetime import datetime
3
- # , timedelta
4
- import logging
5
- # import ephem
6
- # from ephem import degree
7
- # from math import degrees as deg
8
- import pytz
9
-
10
- # Astroplan
11
- import warnings
12
- from pytz import timezone
13
- from astropy import units as u
14
- from astropy.coordinates import EarthLocation
15
- from astropy.time import Time
16
- from astroplan import Observer
17
- from astroplan.exceptions import TargetAlwaysUpWarning, TargetNeverUpWarning
18
-
19
- # The introduction of the module [timezonefinder](https://github.com/jannikmi/timezonefinder)
20
- # with it's nested dependency to [py-h3](https://github.com/uber/h3-py) failed while compiling
21
- # the `c`-module h3 on some home assistant deployment variants (e.g. Home Assistant
22
- # Operating System on RPi).
23
- # from timezonefinder import TimezoneFinder
24
- from pyastroweatherio.const import (
25
- DEFAULT_ELEVATION,
26
- DEFAULT_TIMEZONE,
27
- HOME_LATITUDE,
28
- HOME_LONGITUDE,
29
- CIVIL_TWILIGHT,
30
- CIVIL_DUSK_DAWN,
31
- NAUTICAL_TWILIGHT,
32
- NAUTICAL_DUSK_DAWN,
33
- ASTRONOMICAL_TWILIGHT,
34
- ASTRONOMICAL_DUSK_DAWN,
35
- )
36
-
37
- _LOGGER = logging.getLogger(__name__)
38
-
39
-
40
- class ConversionFunctions:
41
- """Convert between different Weather Units."""
42
-
43
- async def epoch_to_datetime(self, value) -> str:
44
- """Converts EPOC time to Date Time String."""
45
- return datetime.datetime.fromtimestamp(int(value)).strftime("%Y-%m-%d %H:%M:%S")
46
-
47
- async def anchor_timestamp(self, value) -> datetime:
48
- """Converts the datetime string from 7Timer to DateTime."""
49
- return datetime.strptime(value, "%Y%m%d%H")
50
-
51
-
52
- class AstronomicalRoutines:
53
- """Calculate different astronomical objects"""
54
-
55
- def __init__(
56
- self,
57
- latitude=HOME_LATITUDE,
58
- longitude=HOME_LONGITUDE,
59
- elevation=DEFAULT_ELEVATION,
60
- timezone_info=DEFAULT_TIMEZONE,
61
- forecast_time=datetime.utcnow(),
62
- ):
63
- self._longitude = longitude
64
- self._latitude = latitude
65
- self._elevation = elevation
66
- self._timezone_info = timezone_info
67
- # tz_find = TimezoneFinder()
68
- # self._timezone_info = tz_find.timezone_at(lng=longitude, lat=latitude)
69
- _LOGGER.debug("Timezone: %s", self._timezone_info)
70
- self._forecast_time = forecast_time
71
-
72
- self._sun_observer = None
73
- self._sun_observer_nautical = None
74
- self._sun_observer_astro = None
75
- self._moon_observer = None
76
- self._sun_next_rising_civil = None
77
- self._sun_next_setting_civil = None
78
- self._sun_next_rising_nautical = None
79
- self._sun_next_setting_nautical = None
80
- self._sun_next_rising_astro = None
81
- self._sun_next_setting_astro = None
82
- self._sun_altitude = None
83
- self._sun_azimuth = None
84
- self._moon_next_rising = None
85
- self._moon_next_setting = None
86
- self._moon_altitude = None
87
- self._moon_azimuth = None
88
- self._moon_illumination = None
89
- self._sun = None
90
- self._moon = None
91
-
92
- async def calc(
93
- self,
94
- longitude,
95
- latitude,
96
- elevation,
97
- tz,
98
- pressure=0,
99
- relative_humidity=0,
100
- temperature=0,
101
- observation_date=None,
102
- ):
103
- # Our observer
104
- location = EarthLocation.from_geodetic(longitude, latitude, elevation * u.m)
105
-
106
- observer = Observer(
107
- name="Observer",
108
- location=location,
109
- pressure=pressure * u.bar,
110
- relative_humidity=relative_humidity,
111
- temperature=temperature * u.deg_C,
112
- timezone=timezone(tz),
113
- )
114
-
115
- # Calculate tonights night unless a date is given
116
- if observation_date is None:
117
- # time = (
118
- # Time(
119
- # datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0),
120
- # scale="utc",
121
- # location=observer.location,
122
- # )
123
- # + 12 * u.hour
124
- # )
125
- time = Time(
126
- datetime.utcnow(),
127
- scale="utc",
128
- location=observer.location,
129
- )
130
- else:
131
- time = (
132
- Time(
133
- datetime.strptime(observation_date, "%m/%d/%y").replace(hour=0, minute=0, second=0, microsecond=0),
134
- scale="utc",
135
- location=observer.location,
136
- )
137
- + 12 * u.hour
138
- )
139
- _LOGGER.debug("Astronomical calculations for: {0} UTC".format(time.strftime("%m/%d/%Y %H:%M:%S")))
140
- _LOGGER.debug("EarthLocation: {0} {1} {2}".format(longitude, latitude, elevation * u.m))
141
-
142
-
143
- darkness = ""
144
-
145
- with warnings.catch_warnings(record=True) as w:
146
- observer.sun_set_time(time, which="next", horizon=-6 * u.deg)
147
- if len(w):
148
- if issubclass(w[-1].category, TargetAlwaysUpWarning):
149
- _LOGGER.debug("Sun is not setting civically")
150
- self._sun_next_setting_civil = await self.utc_to_local((time + 1 * u.day).to_datetime())
151
- self._sun_next_rising_civil = await self.utc_to_local(time.to_datetime())
152
- w.clear()
153
- else:
154
- darkness = "civil"
155
- sun_next_setting_civil = observer.sun_set_time(time=time, which='next', horizon=-6 * u.deg)
156
- sun_next_rising_civil = observer.sun_rise_time(time=time, which='next', horizon=-6 * u.deg)
157
-
158
- # Compatibility
159
- self._sun_next_setting_civil = await self.utc_to_local(sun_next_setting_civil.to_datetime())
160
- self._sun_next_rising_civil = await self.utc_to_local(sun_next_rising_civil.to_datetime())
161
-
162
- with warnings.catch_warnings(record=True) as w:
163
- observer.sun_set_time(time, which="next", horizon=-12 * u.deg)
164
- if len(w):
165
- if issubclass(w[-1].category, TargetAlwaysUpWarning):
166
- _LOGGER.debug("Sun is not setting nautically")
167
- w.clear()
168
- else:
169
- darkness = "nautical"
170
- sun_next_setting_nautical = observer.sun_set_time(time=time, which='next', horizon=-12 * u.deg)
171
- sun_next_rising_nautical = observer.sun_rise_time(time=time, which='next', horizon=-12 * u.deg)
172
-
173
- # Compatibility
174
- self._sun_next_setting_nautical = await self.utc_to_local(sun_next_setting_nautical.to_datetime())
175
- self._sun_next_rising_nautical = await self.utc_to_local(sun_next_rising_nautical.to_datetime())
176
-
177
- with warnings.catch_warnings(record=True) as w:
178
- observer.sun_set_time(time, which="next", horizon=-18 * u.deg)
179
- if len(w):
180
- if issubclass(w[-1].category, TargetAlwaysUpWarning):
181
- _LOGGER.debug("Sun is not setting astronomically")
182
- w.clear()
183
- else:
184
- darkness = "astronomical"
185
- sun_next_setting_astronomical = observer.sun_set_time(
186
- time=time, which='next', horizon=-18 * u.deg
187
- )
188
- sun_next_rising_astronomical = observer.sun_rise_time(
189
- time=time, which='next', horizon=-18 * u.deg
190
- )
191
-
192
- # Compatibility
193
- self._sun_next_setting_astro = await self.utc_to_local(sun_next_setting_astronomical.to_datetime())
194
- self._sun_next_rising_astro = await self.utc_to_local(sun_next_rising_astronomical.to_datetime())
195
-
196
- with warnings.catch_warnings(record=True) as w:
197
- observer.moon_set_time(time, which="next", horizon=0 * u.deg)
198
- if len(w):
199
- if issubclass(w[-1].category, TargetAlwaysUpWarning):
200
- _LOGGER.debug("Moon is not setting civically")
201
- w.clear()
202
- else:
203
- # moon_next_setting_civil, moon_next_rising_civil = observer.tonight(
204
- # time=time, horizon=0 * u.deg
205
- # )
206
- moon_next_setting_civil = observer.moon_set_time(time=time, which='next', horizon=0 * u.deg)
207
- moon_next_rising_civil = observer.moon_rise_time(time=time, which='next', horizon=0 * u.deg)
208
-
209
- # Compatibility
210
- self._moon_next_setting = await self.utc_to_local(moon_next_setting_civil.to_datetime())
211
- self._moon_next_rising = await self.utc_to_local(moon_next_rising_civil.to_datetime())
212
-
213
- _LOGGER.debug("Darkness: {0}".format(darkness))
214
- _LOGGER.debug("Sun set local {0}: {1}".format("astronomical", self._sun_next_setting_astro))
215
- _LOGGER.debug("Sun rise local {0}: {1}".format("astronomical", self._sun_next_rising_astro))
216
- _LOGGER.debug("Sun set local {0}: {1}".format("nautical", self._sun_next_setting_nautical))
217
- _LOGGER.debug("Sun rise local {0}: {1}".format("nautical", self._sun_next_rising_nautical))
218
- _LOGGER.debug("Sun set local {0}: {1}".format("civil", self._sun_next_setting_civil))
219
- _LOGGER.debug("Sun rise local {0}: {1}".format("civil", self._sun_next_rising_civil))
220
- _LOGGER.debug("Moon set local {0}: {1}".format("civil", self._moon_next_setting))
221
- _LOGGER.debug("Moon rise local {0}: {1}".format("civil", self._moon_next_rising))
222
-
223
- altaz_sun = observer.sun_altaz(Time.now())
224
- # Compatibility
225
- self._sun_altitude = altaz_sun.alt.degree
226
- self._sun_azimuth = altaz_sun.az.degree
227
- # _LOGGER.debug("Sun alt: {0.alt}, az: {0.az}".format(altaz_sun))
228
-
229
- altaz_moon = observer.moon_altaz(Time.now())
230
- # Compatibility
231
- self._moon_altitude = altaz_moon.alt.degree
232
- self._moon_azimuth = altaz_moon.az.degree
233
- # _LOGGER.debug("Moon alt: {0.alt}, az: {0.az}".format(altaz_moon))
234
-
235
- self._moon_illumination = observer.moon_illumination(self._sun_next_setting_civil) * 100
236
- _LOGGER.debug("Moon illumination: {:.0f}%".format(self._moon_illumination))
237
-
238
- async def utc_to_local(self, utc_dt):
239
- """Localizes the datetime"""
240
- local_tz = pytz.timezone(self._timezone_info)
241
- local_dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(local_tz)
242
- return local_tz.normalize(local_dt)
243
-
244
- async def utc_to_local_diff(self):
245
- """returns the UTC Offset"""
246
- local = await self.utc_to_local(datetime.now())
247
- return local.utcoffset().seconds // 3600
248
-
249
- async def need_update(self):
250
- localtime = await self.utc_to_local(datetime.utcnow())
251
- if (
252
- self._sun_next_setting_civil is None
253
- or self._sun_next_setting_nautical is None
254
- or self._sun_next_setting_astro is None
255
- or self._sun_next_rising_astro is None
256
- or self._sun_next_rising_nautical is None
257
- or self._sun_next_rising_civil is None
258
- or self._moon_next_rising is None
259
- or self._moon_next_setting is None
260
- or localtime > self._sun_next_setting_civil
261
- or localtime > self._sun_next_setting_nautical
262
- or localtime > self._sun_next_setting_astro
263
- or localtime > self._sun_next_rising_astro
264
- or localtime > self._sun_next_rising_nautical
265
- or localtime > self._sun_next_rising_civil
266
- or localtime > self._moon_next_rising
267
- or localtime > self._moon_next_setting
268
- ):
269
- await self.calc(self._longitude, self._latitude, self._elevation, self._timezone_info)
270
- else:
271
- _LOGGER.debug("Astronomical calculations are up to date")
272
-
273
- async def sun_next_rising(self) -> datetime:
274
- """Returns sun next rising"""
275
- if self._sun_next_rising_astro is not None:
276
- return self._sun_next_rising_astro
277
- if self._sun_next_rising_nautical is not None:
278
- return self._sun_next_rising_nautical
279
- if self._sun_next_rising_civil is not None:
280
- return self._sun_next_rising_civil
281
-
282
- async def sun_next_rising_civil(self) -> datetime:
283
- """Returns sun next civil rising"""
284
- if self._sun_next_rising_civil is not None:
285
- return self._sun_next_rising_civil
286
-
287
- async def sun_next_rising_nautical(self) -> datetime:
288
- """Returns sun next nautical rising"""
289
- if self._sun_next_rising_nautical is not None:
290
- return self._sun_next_rising_nautical
291
-
292
- async def sun_next_rising_astro(self) -> datetime:
293
- """Returns sun next astronomical rising"""
294
- if self._sun_next_rising_astro is not None:
295
- return self._sun_next_rising_astro
296
-
297
- async def sun_next_setting(self) -> datetime:
298
- """Returns sun next setting"""
299
- if self._sun_next_setting_astro is not None:
300
- return self._sun_next_setting_astro
301
- if self._sun_next_setting_nautical is not None:
302
- return self._sun_next_setting_nautical
303
- if self._sun_next_setting_civil is not None:
304
- return self._sun_next_setting_civil
305
-
306
- async def sun_next_setting_civil(self) -> datetime:
307
- """Returns sun next civil setting"""
308
- if self._sun_next_setting_civil is not None:
309
- return self._sun_next_setting_civil
310
-
311
- async def sun_next_setting_nautical(self) -> datetime:
312
- """Returns sun next nautical setting"""
313
- if self._sun_next_setting_nautical is not None:
314
- return self._sun_next_setting_nautical
315
-
316
- async def sun_next_setting_astro(self) -> datetime:
317
- """Returns sun next astronomical setting"""
318
- if self._sun_next_setting_astro is not None:
319
- return self._sun_next_setting_astro
320
-
321
- async def sun_altitude(self) -> float:
322
- """Returns the sun altitude"""
323
- if self._sun_altitude is not None:
324
- return self._sun_altitude
325
-
326
- async def sun_azimuth(self) -> float:
327
- """Returns the sun azimuth"""
328
- if self._sun_azimuth is not None:
329
- return self._sun_azimuth
330
-
331
- async def moon_next_rising(self) -> datetime:
332
- """Returns moon next rising"""
333
- if self._moon_next_rising is not None:
334
- return self._moon_next_rising
335
-
336
- async def moon_next_setting(self) -> datetime:
337
- """Returns moon next setting"""
338
- if self._moon_next_setting is not None:
339
- return self._moon_next_setting
340
-
341
- async def moon_phase(self) -> float:
342
- """Returns the moon phase"""
343
- return self._moon_illumination
344
-
345
- async def moon_altitude(self) -> float:
346
- """Returns the moon altitude"""
347
- if self._moon_altitude is not None:
348
- return self._moon_altitude
349
-
350
- async def moon_azimuth(self) -> float:
351
- """Returns the moon azimuth"""
352
- if self._moon_azimuth is not None:
353
- return self._moon_azimuth
@@ -1,3 +0,0 @@
1
- aiohttp
2
- astroplan
3
- astropy