From da06e35362573d5852da4f1c9bee13f188db55f0 Mon Sep 17 00:00:00 2001 From: Tony Stork Date: Thu, 13 Jan 2022 18:12:21 -0500 Subject: [PATCH] Add date_countdown.py --- python_scripts/date_countdown.py | 112 +++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 python_scripts/date_countdown.py diff --git a/python_scripts/date_countdown.py b/python_scripts/date_countdown.py new file mode 100644 index 0000000..970ee12 --- /dev/null +++ b/python_scripts/date_countdown.py @@ -0,0 +1,112 @@ +########## +# +# Date Countdown +# +# A python_script for homeassistant by mf_social +# +########## + + +# Get the basic information to do the calculations +today = datetime.datetime.now().date() +name = data.get('name') +eventType = data.get('type') +countup = data.get('reverse' , False) +appendYear = data.get('append_year' , False) +defaultFriendlyName = '' +friendlyName = '' +numberOfDays = 0 +defaultIcon = "mdi:calendar-star" + +# Convert the date we got +dateStr = data.get('date') +dateSplit = dateStr.split("/") + +dateDay = int(dateSplit[0]) +dateMonth = int(dateSplit[1]) +dateYear = int(dateSplit[2]) +date = datetime.date(dateYear , dateMonth , dateDay) + + +# Calculate the next occurrence +nextOccurYear = int(today.year) +nextOccur = datetime.date(nextOccurYear , dateMonth , dateDay) + +# If countup (reverse == true) +if countup: + defaultIcon = "mdi:calendar-arrow-right" + years = today.year - date.year + + if nextOccur < today: + #if event has passed this year, get days between then and today + numberOfDays = (today - nextOccur).days + + else: + # Count days from last year + lastYearDate = datetime.date(today.year - 1 , dateMonth, dateDay) + numberOfDays = (today - lastYearDate).days + + +# Regular countdown +else: + if nextOccur < date: + # date must be the first occurrence + nextOccur = date + + if nextOccur < today: + # if event has passed this year, nextOccur is next year + nextOccurYear = nextOccurYear + 1 + nextOccur = datetime.date(nextOccurYear, dateMonth, dateDay) + + years = nextOccurYear - dateYear + + if years < 0: + # if years is negative, then date is more than 365 days away + # nextOccur will be the first occurrence + years = 0 + + numberOfDays = (nextOccur - today).days + + +# Set the default friendly name +if eventType.lower() == 'birthday': + # add an apostophe for birthdays + defaultFriendlyName = "{}'s {}".format(name , eventType) +else: + defaultFriendlyName = "{} {}".format(name , eventType) + + +# Sanitise the entity_id to meet the criteria by +# replacing Scandanavian characters and spaces +rawName = "{}_{}".format(eventType , name) +rawName1 = rawName.replace("Æ" , "AE") +rawName2 = rawName1.replace("Ø" , "O") +rawName3 = rawName2.replace("Å" , "AA") +rawName4 = rawName3.replace("æ" , "ae") +rawName5 = rawName4.replace("ø" , "o") +rawName6 = rawName5.replace("å" , "aa") +safeName = rawName6.replace(" " , "_") +sensorName = "sensor.{}".format(safeName) + + +# Set friendly_name +rawFriendlyName = data.get('friendly_name', defaultFriendlyName) + +if appendYear: + #add Years to the end of friendly_name + friendlyName = "{} ({})".format(rawFriendlyName , years) + +else: + friendlyName = "{}".format(rawFriendlyName) + + +# Send the sensor to homeassistant +hass.states.set(sensorName , numberOfDays , + { + "icon" : data.get("icon", defaultIcon), + "unit_of_measurement" : "days" , + "friendly_name" : "{}".format(friendlyName), + "nextoccur" : "{}/{}/{}".format(nextOccur.day , nextOccur.month , nextOccur.year) , + "years" : years + } +)