Skip to content

Commit

Permalink
Make data more precise (#2)
Browse files Browse the repository at this point in the history
Make attributes raw datetimes or un-rounded floats to support more precise calculations.

Add today attribute which is raw version of state.

Round float state values to 3 decimal places so that history graphs can show some daily change.
  • Loading branch information
pnbruckner authored Jun 20, 2019
1 parent d103776 commit fa41975
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions custom_components/sun2/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __init__(self, event):
self._location = None
self._state = None
self._yesterday = None
self._today = None
self._tomorrow = None

@property
Expand All @@ -76,6 +77,7 @@ def device_state_attributes(self):
"""Return device specific state attributes."""
return {
'yesterday': self._yesterday,
'today': self._today,
'tomorrow': self._tomorrow,
}

Expand Down Expand Up @@ -117,15 +119,19 @@ def async_update_at_midnight(now):
def _get_astral_event(self, date):
try:
if self._event in _DT_TYPES:
return getattr(self._location, self._event)(date).isoformat()
return getattr(self._location, self._event)(date)
start, end = getattr(self._location, self._event)(date)
return round((end - start).total_seconds()/3600, 2)
return (end - start).total_seconds()/3600
except AstralError:
return 'none'

async def async_update(self):
"""Update state."""
today = dt_util.now().date()
self._state = self._get_astral_event(today)
self._yesterday = self._get_astral_event(today-timedelta(days=1))
self._today = self._get_astral_event(today)
self._tomorrow = self._get_astral_event(today+timedelta(days=1))
if self._event in _DT_TYPES:
self._state = self._today.isoformat()
else:
self._state = round(self._today, 3)

0 comments on commit fa41975

Please sign in to comment.