mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Take elevation into consideration
This commit is contained in:
parent
35f0270688
commit
1fda362ca3
@ -32,6 +32,8 @@ REQUIREMENTS = ['astral>=0.8.1']
|
|||||||
DOMAIN = "sun"
|
DOMAIN = "sun"
|
||||||
ENTITY_ID = "sun.sun"
|
ENTITY_ID = "sun.sun"
|
||||||
|
|
||||||
|
CONF_ELEVATION = 'elevation'
|
||||||
|
|
||||||
STATE_ABOVE_HORIZON = "above_horizon"
|
STATE_ABOVE_HORIZON = "above_horizon"
|
||||||
STATE_BELOW_HORIZON = "below_horizon"
|
STATE_BELOW_HORIZON = "below_horizon"
|
||||||
|
|
||||||
@ -116,7 +118,21 @@ def setup(hass, config):
|
|||||||
_LOGGER.error('Invalid configuration received: %s', ", ".join(errors))
|
_LOGGER.error('Invalid configuration received: %s', ", ".join(errors))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
sun = Sun(hass, latitude, longitude)
|
platform_config = config.get(DOMAIN, {})
|
||||||
|
|
||||||
|
elevation = platform_config.get(CONF_ELEVATION)
|
||||||
|
|
||||||
|
from astral import Location, GoogleGeocoder
|
||||||
|
|
||||||
|
location = Location(('', '', latitude, longitude, hass.config.time_zone,
|
||||||
|
elevation or 0))
|
||||||
|
|
||||||
|
if elevation is None:
|
||||||
|
google = GoogleGeocoder()
|
||||||
|
google._get_elevation(location) # pylint: disable=protected-access
|
||||||
|
_LOGGER.info('Retrieved elevation from Google: %s', location.elevation)
|
||||||
|
|
||||||
|
sun = Sun(hass, location)
|
||||||
sun.point_in_time_listener(dt_util.utcnow())
|
sun.point_in_time_listener(dt_util.utcnow())
|
||||||
|
|
||||||
return True
|
return True
|
||||||
@ -127,13 +143,9 @@ class Sun(Entity):
|
|||||||
|
|
||||||
entity_id = ENTITY_ID
|
entity_id = ENTITY_ID
|
||||||
|
|
||||||
def __init__(self, hass, latitude, longitude):
|
def __init__(self, hass, location):
|
||||||
from astral import Astral
|
|
||||||
|
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self.latitude = latitude
|
self.location = location
|
||||||
self.longitude = longitude
|
|
||||||
self.astral = Astral()
|
|
||||||
self._state = self.next_rising = self.next_setting = None
|
self._state = self.next_rising = self.next_setting = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -168,18 +180,16 @@ class Sun(Entity):
|
|||||||
""" Calculate sun state at a point in UTC time. """
|
""" Calculate sun state at a point in UTC time. """
|
||||||
mod = -1
|
mod = -1
|
||||||
while True:
|
while True:
|
||||||
next_rising_dt = self.astral.sunrise_utc(
|
next_rising_dt = self.location.sunrise(
|
||||||
utc_point_in_time +
|
utc_point_in_time + timedelta(days=mod), local=False)
|
||||||
timedelta(days=mod), self.latitude, self.longitude)
|
|
||||||
if next_rising_dt > utc_point_in_time:
|
if next_rising_dt > utc_point_in_time:
|
||||||
break
|
break
|
||||||
mod += 1
|
mod += 1
|
||||||
|
|
||||||
mod = -1
|
mod = -1
|
||||||
while True:
|
while True:
|
||||||
next_setting_dt = (self.astral.sunset_utc(
|
next_setting_dt = (self.location.sunset(
|
||||||
utc_point_in_time +
|
utc_point_in_time + timedelta(days=mod), local=False))
|
||||||
timedelta(days=mod), self.latitude, self.longitude))
|
|
||||||
if next_setting_dt > utc_point_in_time:
|
if next_setting_dt > utc_point_in_time:
|
||||||
break
|
break
|
||||||
mod += 1
|
mod += 1
|
||||||
|
@ -67,7 +67,7 @@ class TestDeviceSunLightTrigger(unittest.TestCase):
|
|||||||
light.DOMAIN: {CONF_PLATFORM: 'test'}
|
light.DOMAIN: {CONF_PLATFORM: 'test'}
|
||||||
})
|
})
|
||||||
|
|
||||||
sun.setup(self.hass, {})
|
sun.setup(self.hass, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}})
|
||||||
|
|
||||||
def tearDown(self): # pylint: disable=invalid-name
|
def tearDown(self): # pylint: disable=invalid-name
|
||||||
""" Stop down stuff we started. """
|
""" Stop down stuff we started. """
|
||||||
|
@ -40,7 +40,7 @@ class TestSun(unittest.TestCase):
|
|||||||
# Compare it with the real data
|
# Compare it with the real data
|
||||||
self.hass.config.latitude = latitude
|
self.hass.config.latitude = latitude
|
||||||
self.hass.config.longitude = longitude
|
self.hass.config.longitude = longitude
|
||||||
sun.setup(self.hass, None)
|
sun.setup(self.hass, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}})
|
||||||
|
|
||||||
astral = Astral()
|
astral = Astral()
|
||||||
utc_now = dt_util.utcnow()
|
utc_now = dt_util.utcnow()
|
||||||
@ -77,7 +77,7 @@ class TestSun(unittest.TestCase):
|
|||||||
""" Test if the state changes at next setting/rising. """
|
""" Test if the state changes at next setting/rising. """
|
||||||
self.hass.config.latitude = '32.87336'
|
self.hass.config.latitude = '32.87336'
|
||||||
self.hass.config.longitude = '117.22743'
|
self.hass.config.longitude = '117.22743'
|
||||||
sun.setup(self.hass, None)
|
sun.setup(self.hass, {})
|
||||||
|
|
||||||
if sun.is_on(self.hass):
|
if sun.is_on(self.hass):
|
||||||
test_state = sun.STATE_BELOW_HORIZON
|
test_state = sun.STATE_BELOW_HORIZON
|
||||||
|
Loading…
x
Reference in New Issue
Block a user