@@ -1800,32 +1800,27 @@ You can use ``hass`` to compute sunrise and sunset times using the same method H
18001800 import datetime
18011801
18021802 location = sun.get_astral_location(hass)
1803- sunrise = location.sunrise(datetime.datetime.today()).replace(tzinfo = None )
1804- sunset = location.sunset(datetime.datetime.today()).replace(tzinfo = None )
1803+ sunrise = location[ 0 ] .sunrise(datetime.datetime.today()).replace(tzinfo = None )
1804+ sunset = location[ 0 ] .sunset(datetime.datetime.today()).replace(tzinfo = None )
18051805 print (f " today sunrise = { sunrise} , sunset = { sunset} " )
18061806
18071807 (Note that the ``sun.sun `` attributes already provide times for the next sunrise and sunset, so
1808- this example is a bit contrived.)
1808+ this example is a bit contrived. Also note this only applies to HA 2021.5 and later; prior
1809+ to that, ``sun.get_astral_location() `` doesn't return the elevation, so replace ``location[0] ``
1810+ with ``location `` in the two expressions if you have an older version of HA.)
18091811
18101812Here's another method that uses the installed version of ``astral `` directly, rather than the HASS
1811- helper function. It's a bit more cryptic since it 's a very old version of `` astral ``, but you can
1812- see how the HASS configuration values are used:
1813+ helper function. HA 2021.5 upgraded to `` astral 2.2 ``, and here 's one way of getting sunrise
1814+ using this version (prior to this HA used a very old version of `` astral ``).
18131815
18141816.. code :: python
18151817
18161818 import astral
1819+ import astral.location
18171820 import datetime
18181821
1819- here = astral.Location(
1820- (
1821- " " ,
1822- " " ,
1823- hass.config.latitude,
1824- hass.config.longitude,
1825- str (hass.config.time_zone),
1826- hass.config.elevation,
1827- )
1828- )
1822+ here = astral.location.Location(astral.LocationInfo(" City" , " Country" , str (hass.config.time_zone),
1823+ hass.config.latitude, hass.config.longitude))
18291824 sunrise = here.sunrise(datetime.datetime.today()).replace(tzinfo = None )
18301825 sunset = here.sunset(datetime.datetime.today()).replace(tzinfo = None )
18311826 print (f " today sunrise = { sunrise} , sunset = { sunset} " )
0 commit comments