Skip to content

Commit

Permalink
Add append option (#16)
Browse files Browse the repository at this point in the history
* Update date_countdown.py

* Update README.md

* Update info.md
  • Loading branch information
Marc Forth committed May 26, 2020
1 parent 8fc440e commit cf51683
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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:
Expand All @@ -62,6 +64,7 @@ name: Our wedding
type: anniversary
date: 14/02/1994
icon: "mdi:ring"
append_year: True
```

or
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion info.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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:
Expand All @@ -57,6 +59,7 @@ name: Our wedding
type: anniversary
date: 14/02/1994
icon: "mdi:ring"
append_year: True
```

or
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions python_scripts/date_countdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
}
)
)

0 comments on commit cf51683

Please sign in to comment.