Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions custom_components/pyscript/trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from croniter import croniter

from homeassistant.const import SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET
from homeassistant.core import Context
from homeassistant.helpers import sun
from homeassistant.util import dt as dt_util
Expand Down Expand Up @@ -672,28 +673,15 @@ async def parse_date_time(cls, date_time_str, day_offset, now, startup_time):
else:
hour, mins, sec = int(match0[1]), int(match0[2]), 0
dt_str = dt_str[len(match0.group(0)) :]
elif dt_str.startswith("sunrise") or dt_str.startswith("sunset"):
location = sun.get_astral_location(cls.hass)
if isinstance(location, tuple):
# HA core-2021.5.0 included this breaking change: https://github.com/home-assistant/core/pull/48573.
# As part of the upgrade to astral 2.2, sun.get_astral_location() now returns a tuple including the
# elevation. We just want the astral.location.Location object.
location = location[0]
try:
if dt_str.startswith("sunrise"):
time_sun = await cls.hass.async_add_executor_job(
location.sunrise, dt.date(year, month, day)
)
dt_str = dt_str[7:]
else:
time_sun = await cls.hass.async_add_executor_job(
location.sunset, dt.date(year, month, day)
)
dt_str = dt_str[6:]
except Exception:
elif dt_str.startswith(SUN_EVENT_SUNRISE) or dt_str.startswith(SUN_EVENT_SUNSET):
event = SUN_EVENT_SUNRISE if dt_str.startswith(SUN_EVENT_SUNRISE) else SUN_EVENT_SUNSET
time_sun = sun.get_astral_event_date(cls.hass, event, dt.date(year, month, day))
if time_sun is None:
_LOGGER.warning("'%s' not defined at this latitude", dt_str)
# return something in the past so it is ignored
return now - dt.timedelta(days=100), fixed_date
time_sun = dt_util.as_local(time_sun)
dt_str = dt_str[len(event) :]
now += time_sun.date() - now.date()
hour, mins, sec = time_sun.hour, time_sun.minute, time_sun.second
elif dt_str.startswith("noon"):
Expand Down
15 changes: 7 additions & 8 deletions docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2035,21 +2035,20 @@ You can use ``hass`` to compute sunrise and sunset times using the same method H
.. code:: python

import homeassistant.helpers.sun as sun
import homeassistant.util.dt as dt_util
import datetime

location = sun.get_astral_location(hass)
sunrise = location[0].sunrise(datetime.datetime.today()).replace(tzinfo=None)
sunset = location[0].sunset(datetime.datetime.today()).replace(tzinfo=None)
date = datetime.date.today()
sunrise = dt_util.as_local(sun.get_astral_event_date(hass, "sunrise", date)).replace(tzinfo=None)
sunset = dt_util.as_local(sun.get_astral_event_date(hass, "sunset", date)).replace(tzinfo=None)
print(f"today sunrise = {sunrise}, sunset = {sunset}")

(Note that the ``sun.sun`` attributes already provide times for the next sunrise and sunset, so
this example is a bit contrived. Also note this only applies to HA 2021.5 and later; prior
to that, ``sun.get_astral_location()`` doesn't return the elevation, so replace ``location[0]``
with ``location`` in the two expressions if you have an older version of HA.)
this example is a bit contrived. The ``sun.get_astral_event_date()`` helper returns the time in
UTC, so ``dt_util.as_local()`` converts it to local time.)

Here's another method that uses the installed version of ``astral`` directly, rather than the HASS
helper function. HA 2021.5 upgraded to ``astral 2.2``, and here's one way of getting sunrise
using this version (prior to this HA used a very old version of ``astral``).
helper function (note this does not account for your configured elevation):

.. code:: python

Expand Down
Loading