Skip to content

Commit

Permalink
make unit_of_measurement configurable
Browse files Browse the repository at this point in the history
closes #10
  • Loading branch information
pinkywafer committed Sep 5, 2020
1 parent cfea1e4 commit d20ae1a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
6 changes: 6 additions & 0 deletions custom_components/calendarific/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
DEFAULT_ICON_TODAY,
DEFAULT_DATE_FORMAT,
DEFAULT_SOON,
DEFAULT_UNIT_OF_MEASUREMENT,
CONF_ICON_NORMAL,
CONF_ICON_TODAY,
CONF_ICON_SOON,
CONF_HOLIDAY,
CONF_DATE_FORMAT,
CONF_SOON,
CONF_UNIT_OF_MEASUREMENT,
)

from . import holiday_list
Expand Down Expand Up @@ -65,6 +67,7 @@ async def _show_user_form(self, user_input):
icon_today = DEFAULT_ICON_TODAY
date_format = DEFAULT_DATE_FORMAT
days_as_soon = DEFAULT_SOON
unit_of_measurement = DEFAULT_UNIT_OF_MEASUREMENT
if user_input is not None:
if CONF_NAME in user_input:
name = user_input[CONF_NAME]
Expand All @@ -78,9 +81,12 @@ async def _show_user_form(self, user_input):
icon_today = user_input[CONF_ICON_TODAY]
if CONF_DATE_FORMAT in user_input:
date_format = user_input[CONF_DATE_FORMAT]
if CONF_UNIT_OF_MEASUREMENT in user_input:
unit_of_measurement = user_input[CONF_UNIT_OF_MEASUREMENT]
data_schema = OrderedDict()
data_schema[vol.Required(CONF_HOLIDAY, default=holiday)] = vol.In(holiday_list)
data_schema[vol.Optional(CONF_NAME, default=name)] = str
data_schema[vol.Required(CONF_UNIT_OF_MEASUREMENT, default=unit_of_measurement)] = str
data_schema[vol.Required(CONF_ICON_NORMAL, default=icon_normal)] = str
data_schema[vol.Required(CONF_ICON_TODAY, default=icon_today)] = str
data_schema[vol.Required(CONF_SOON, default=days_as_soon)] = int
Expand Down
2 changes: 2 additions & 0 deletions custom_components/calendarific/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
CONF_DATE_FORMAT = "date_format"
CONF_SOON = "days_as_soon"
CONF_HOLIDAY = "holiday"
CONF_UNIT_OF_MEASUREMENT = "unit_of_measurement"

# Defaults
DEFAULT_ICON_NORMAL = "mdi:calendar-blank"
DEFAULT_ICON_TODAY = "mdi:calendar-star"
DEFAULT_ICON_SOON = "mdi:calendar"
DEFAULT_DATE_FORMAT = "%Y-%m-%d"
DEFAULT_SOON = 1
DEFAULT_UNIT_OF_MEASUREMENT = "Days"



9 changes: 7 additions & 2 deletions custom_components/calendarific/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
DEFAULT_ICON_NORMAL,
DEFAULT_ICON_TODAY,
DEFAULT_DATE_FORMAT,
DEFAULT_UNIT_OF_MEASUREMENT,
CONF_ICON_NORMAL,
CONF_ICON_TODAY,
CONF_ICON_SOON,
CONF_DATE_FORMAT,
CONF_SOON,
CONF_HOLIDAY,
CONF_UNIT_OF_MEASUREMENT,
DOMAIN,
)

Expand All @@ -38,6 +40,7 @@
vol.Optional(CONF_ICON_TODAY, default=DEFAULT_ICON_TODAY): cv.icon,
vol.Optional(CONF_ICON_SOON, default=DEFAULT_ICON_SOON): cv.icon,
vol.Optional(CONF_DATE_FORMAT, default=DEFAULT_DATE_FORMAT): cv.string,
vol.Optional(CONF_UNIT_OF_MEASUREMENT, default=DEFAULT_UNIT_OF_MEASUREMENT): cv.string,
}
)

Expand Down Expand Up @@ -70,6 +73,9 @@ def __init__(self, config, reader):
self._icon_soon = config.get(CONF_ICON_SOON)
self._soon = config.get(CONF_SOON)
self._date_format = config.get(CONF_DATE_FORMAT)
self._unit_of_measurement = config.get(CONF_UNIT_OF_MEASUREMENT)
if self._unit_of_measurement is None:
self._unit_of_measurement = DEFAULT_UNIT_OF_MEASUREMENT
self._icon = self._icon_normal
self._reader = reader
self._description = self._reader.get_description(self._holiday)
Expand Down Expand Up @@ -107,8 +113,7 @@ def device_state_attributes(self):
@property
def unit_of_measurement(self):
"""Return the unit this state is expressed in."""
if self._date != "-":
return "days"
return self._unit_of_measurement

@property
def icon(self):
Expand Down
1 change: 1 addition & 0 deletions custom_components/calendarific/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"data": {
"name": "Friendly name (Leave blank to use holiday name)",
"holiday": "Holiday",
"unit_of_measurement": "Text for unit_of_measurement",
"icon_normal": "Icon",
"icon_today": "Icon when holiday is today",
"days_as_soon": "Number of Days to consider soon",
Expand Down

0 comments on commit d20ae1a

Please sign in to comment.