Skip to content

Commit

Permalink
Add min_elevation (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
pnbruckner authored Jun 12, 2020
1 parent 407154a commit af7963f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type | description
type | description
-|-
`elevation` | The sun's elevation (degrees).
`min_elevation` | The sun's elevation at solar midnight (degrees).
`max_elevation` | The sun's elevation at solar noon (degrees).

## Binary Sensors
Expand Down Expand Up @@ -117,6 +118,7 @@ sensor:
- nautical_night
- astronomical_night
- elevation
- min_elevation
- max_elevation
binary_sensor:
- platform: sun2
Expand Down
32 changes: 27 additions & 5 deletions custom_components/sun2/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,24 +193,45 @@ def _update(self):
self._state = round(self._state, 3)


class Sun2MaxElevationSensor(Sun2Sensor):
"""Sun2 Max Elevation Sensor."""
class Sun2MinMaxElevationSensor(Sun2Sensor):
"""Sun2 Min/Max Elevation Sensor."""

def __init__(self, hass, sensor_type, icon, is_min):
"""Initialize sensor."""
super().__init__(hass, sensor_type, icon)
self._event = 'solar_midnight' if is_min else 'solar_noon'

@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return '°'

def _get_data(self, date_or_dt):
solar_noon = self._get_astral_event('solar_noon', date_or_dt)
return self._get_astral_event('solar_elevation', solar_noon)
event_time = self._get_astral_event(self._event, date_or_dt)
return self._get_astral_event('solar_elevation', event_time)

def _update(self):
super()._update()
if self._state is not None:
self._state = round(self._state, 3)


class Sun2MinElevationSensor(Sun2MinMaxElevationSensor):
"""Sun2 Min Elevation Sensor."""

def __init__(self, hass, sensor_type, icon):
"""Initialize sensor."""
super().__init__(hass, sensor_type, icon, is_min=True)


class Sun2MaxElevationSensor(Sun2MinMaxElevationSensor):
"""Sun2 Max Elevation Sensor."""

def __init__(self, hass, sensor_type, icon):
"""Initialize sensor."""
super().__init__(hass, sensor_type, icon, is_min=False)


def _nearest_multiple(value, multiple):
return int(round(value / multiple)) * multiple

Expand Down Expand Up @@ -376,7 +397,8 @@ def async_update(now):
'civil_night': (Sun2PeriodOfTimeSensor, 'mdi:weather-night'),
'nautical_night': (Sun2PeriodOfTimeSensor, 'mdi:weather-night'),
'astronomical_night': (Sun2PeriodOfTimeSensor, 'mdi:weather-night'),
# Max elevation
# Min/Max elevation
'min_elevation': (Sun2MinElevationSensor, 'mdi:weather-night'),
'max_elevation': (Sun2MaxElevationSensor, 'mdi:weather-sunny'),
# Elevation
'elevation': (Sun2ElevationSensor, 'mdi:weather-sunny'),
Expand Down

0 comments on commit af7963f

Please sign in to comment.