From cf516837b0c00e506f31e926664c2d99b98de0bf Mon Sep 17 00:00:00 2001 From: Marc Forth Date: Tue, 26 May 2020 13:09:52 +0100 Subject: [PATCH] Add append option (#16) * Update date_countdown.py * Update README.md * Update info.md --- README.md | 6 +++++- info.md | 6 +++++- python_scripts/date_countdown.py | 17 +++++++++++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b8e2462..1474aef 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ key | required | type | description `type:` | True | string | Type of date (eg. Birthday) `date:` | True | string | Date, in format DD/MM/YYYY `friendly_name:` | False | string | Display name of the sensor +`append_year:` | False | boolean | Appends the number of years to the friendly name `icon:` | False | string | Icon of the sensor, defaults to 'mdi:calendar-star' `reverse:` | False | boolean | Reverses the sensor to count up instead of down. (Defaults to False) @@ -39,6 +40,7 @@ You can also customize the sensor icon and friendly names : ``` icon: "mdi:ICON_OF_DATE" friendly_name: FRIENDLY_NAME_OF_DATE +append_year: True ``` And you can reverse the sensor so it counts up from a date: @@ -62,6 +64,7 @@ name: Our wedding type: anniversary date: 14/02/1994 icon: "mdi:ring" +append_year: True ``` or @@ -94,7 +97,7 @@ nextoccur: 17/08/YYYY (either this year or next year as appropriate) years: However old John will be on his next birthday sensor.anniversary_our_wedding -friendly_name: Our wedding anniversary +friendly_name: Our wedding anniversary (20) state: However many days to 14th February nextoccur: 14/02/YYYY (either this year or next year as appropriate) years: How many years you will have been married on that day @@ -131,6 +134,7 @@ automation: type: anniversary date: 14/02/1994 icon: "mdi:ring" + append_year: True - service: python_script.date_countdown data: name: Quit smoking diff --git a/info.md b/info.md index 014d69c..96338dd 100644 --- a/info.md +++ b/info.md @@ -15,6 +15,7 @@ key | required | type | description `type:` | True | string | Type of date (eg. Birthday) `date:` | True | string | Date, in format DD/MM/YYYY `friendly_name:` | False | string | Display name of the sensor +`append_year:` | False | boolean | Appends the number of years to the friendly name `icon:` | False | string | Icon of the sensor, defaults to 'mdi:calendar-star' `reverse:` | False | boolean | Reverses the sensor to count up instead of down. (Defaults to False) @@ -34,6 +35,7 @@ You can also customize the sensor icon and friendly names : ``` icon: "mdi:ICON_OF_DATE" friendly_name: FRIENDLY_NAME_OF_DATE +append_year: True ``` And you can reverse the sensor so it counts up from a date: @@ -57,6 +59,7 @@ name: Our wedding type: anniversary date: 14/02/1994 icon: "mdi:ring" +append_year: True ``` or @@ -89,7 +92,7 @@ nextoccur: 17/08/YYYY (either this year or next year as appropriate) years: However old John will be on his next birthday sensor.anniversary_our_wedding -friendly_name: Our wedding anniversary +friendly_name: Our wedding anniversary (20) state: However many days to 14th February nextoccur: 14/02/YYYY (either this year or next year as appropriate) years: How many years you will have been married on that day @@ -126,6 +129,7 @@ automation: type: anniversary date: 14/02/1994 icon: "mdi:ring" + append_year: True - service: python_script.date_countdown data: name: Quit smoking diff --git a/python_scripts/date_countdown.py b/python_scripts/date_countdown.py index 81e6f47..970ee12 100644 --- a/python_scripts/date_countdown.py +++ b/python_scripts/date_countdown.py @@ -12,7 +12,9 @@ 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" @@ -87,13 +89,24 @@ 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" : data.get('friendly_name', defaultFriendlyName), + "friendly_name" : "{}".format(friendlyName), "nextoccur" : "{}/{}/{}".format(nextOccur.day , nextOccur.month , nextOccur.year) , "years" : years } -) \ No newline at end of file +)