Skip to content

Commit

Permalink
Sun rise/setting
Browse files Browse the repository at this point in the history
Added next sunrise and setting from sun.sun in HH:MM format and a TTS attribute.
  • Loading branch information
J-Lindvig authored Jan 15, 2022
1 parent 7dd385a commit 627bd21
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
3 changes: 3 additions & 0 deletions custom_components/time_date_dk/const.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
ATTRIBUTION = 'Created by J-Lindvig'
DATE_FORMAT = '%d-%m-%Y'
DOMAIN = 'time_date_dk'
TIME_FORMAT = '%H:%M'
2 changes: 1 addition & 1 deletion custom_components/time_date_dk/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"codeowners": ["J-Lindvig"],
"requirements": [],
"iot_class": "local_push",
"version": "0.1.0"
"version": "1.0"
}
62 changes: 46 additions & 16 deletions custom_components/time_date_dk/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
import datetime as DT
from datetime import timedelta

from homeassistant.const import ATTR_ATTRIBUTION
from .const import (
ATTRIBUTION,
DATE_FORMAT,
TIME_FORMAT,
)

_LOGGER: logging.Logger = logging.getLogger(__package__)
_LOGGER = logging.getLogger(__name__)

Expand All @@ -21,10 +28,9 @@ async def async_setup_platform(
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
async_add_entities([ExampleSensor(hass)])
async_add_entities([TimeDateSensor(hass)])

class ExampleSensor(SensorEntity):
"""Representation of a sensor."""
class TimeDateSensor(SensorEntity):

def __init__(self, hass) -> None:
"""Initialize the sensor."""
Expand Down Expand Up @@ -79,14 +85,23 @@ def _getDay_TTS(self, day = 0):
day_TTS += ordinalNumbers[day[0] + "x"]
return day_TTS

def _getTime(self, format = '%H:%M:%S'):
def _getTime(self, format = None):
if format is None:
format = TIME_FORMAT
return self._dateObj.strftime(format)

def _getTime_TTS(self):
def _getTime_TTS(self, time = None):
timeNames = {0: "natten", 6: "morgenen", 9: "formiddagen", 12: "middagen", 14: "eftermiddagen", 18: "aftenen" }
H24 = self._getTime('%-H')
H12 = int(self._getTime('%-I'))
M = int(self._getTime('%-M'))

if time is None:
H24 = self._getTime('%-H')
H12 = int(self._getTime('%-I'))
M = int(self._getTime('%-M'))
else:
timeList = time.split(':')
H24 = int(timeList[0]) if int(timeList[0][0]) > 0 else int(timeList[0][1])
H12 = H24 - 12 if H24 > 12 else H24
M = int(timeList[1]) if int(timeList[1][0]) > 0 else int(timeList[1][1])

timeName = timeNames[min(timeNames, key=lambda x:abs(x-int(H24)))]
if M == 0:
Expand All @@ -106,14 +121,13 @@ def _getTime_TTS(self):

def _getAdventsDates(self):
adventDates = []
format = '%d-%m-%Y'
XmasDateObj = DT.datetime.strptime(str(self._year) + '-12-24 00:00:00', '%Y-%m-%d %H:%M:%S')
XmasDelta = 22 + XmasDateObj.weekday()

adventDates.append((XmasDateObj - timedelta(days = XmasDelta )).strftime(format))
adventDates.append((XmasDateObj - timedelta(days = XmasDelta - 7 )).strftime(format))
adventDates.append((XmasDateObj - timedelta(days = XmasDelta - 14 )).strftime(format))
adventDates.append((XmasDateObj - timedelta(days = XmasDelta - 21 )).strftime(format))
adventDates.append((XmasDateObj - timedelta(days = XmasDelta )).strftime(DATE_FORMAT))
adventDates.append((XmasDateObj - timedelta(days = XmasDelta - 7 )).strftime(DATE_FORMAT))
adventDates.append((XmasDateObj - timedelta(days = XmasDelta - 14 )).strftime(DATE_FORMAT))
adventDates.append((XmasDateObj - timedelta(days = XmasDelta - 21 )).strftime(DATE_FORMAT))

return adventDates

Expand All @@ -137,6 +151,8 @@ def extra_state_attributes(self):
# Prepare a dictionary with attributes
attr = {}

attr[ATTR_ATTRIBUTION] = ATTRIBUTION

attr['ts'] = self._ts
attr['day'] = self._day
attr['day_tts'] = self._day_TTS
Expand All @@ -154,11 +170,15 @@ def extra_state_attributes(self):
attr['weekday_name'] = self._weekdayName

attr['time'] = self._time
attr['time_hhmm'] = self._time_HHMM
attr['time_tts'] = self._time_TTS

attr['advents_dates'] = self._adventsDates

attr['sun_next_rising'] = self._sun_next_rising
attr['sun_next_rising_tts'] = self._sun_next_rising_tts
attr['sun_next_setting'] = self._sun_next_setting
attr['sun_next_setting_tts'] = self._sun_next_setting_tts

return attr

async def async_added_to_hass(self) -> None:
Expand Down Expand Up @@ -190,9 +210,20 @@ def _update_internal_state(self):
self._ts = self._dateObj.timestamp()

self._day = self._dateObj.day

self._day_TTS = self._getDay_TTS()

sunTs = dt_util.as_timestamp(self.hass.states.get('sun.sun').attributes['next_rising'])
sunObj = dt_util.utc_from_timestamp(sunTs)
sunObj = dt_util.as_local(sunObj)
self._sun_next_rising = sunObj.strftime(TIME_FORMAT)
self._sun_next_rising_tts = self._getTime_TTS(self._sun_next_rising)

sunTs = dt_util.as_timestamp(self.hass.states.get('sun.sun').attributes['next_setting'])
sunObj = dt_util.utc_from_timestamp(sunTs)
sunObj = dt_util.as_local(sunObj)
self._sun_next_setting = sunObj.strftime(TIME_FORMAT)
self._sun_next_setting_tts = self._getTime_TTS(self._sun_next_setting)

self._monthNames = ['Januar', 'Februar', 'Marts', 'April', 'Maj', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'December']
self._month = self._dateObj.month
self._monthName = self._monthNames[self._month - 1]
Expand All @@ -206,7 +237,6 @@ def _update_internal_state(self):
self._weekdayName = self._weekdayNameShort + 'dag'

self._time = self._getTime()
self._time_HHMM = self._getTime('%H:%M')
self._time_TTS = self._getTime_TTS()

self._adventsDates = self._getAdventsDates()
Expand Down

0 comments on commit 627bd21

Please sign in to comment.