From ae73818b3b0786ebcf72b16a6f27428e516686e6 Mon Sep 17 00:00:00 2001 From: bruxy70 Date: Fri, 9 Dec 2022 12:43:50 +0100 Subject: [PATCH] Fix Garbage collection schedule ignores first & last month #442 --- custom_components/garbage_collection/__init__.py | 9 ++++++--- custom_components/garbage_collection/sensor.py | 14 ++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/custom_components/garbage_collection/__init__.py b/custom_components/garbage_collection/__init__.py index 239054a..32423fc 100644 --- a/custom_components/garbage_collection/__init__.py +++ b/custom_components/garbage_collection/__init__.py @@ -22,9 +22,12 @@ _LOGGER = logging.getLogger(__name__) +months = [m["value"] for m in const.MONTH_OPTIONS] +frequencies = [f["value"] for f in const.FREQUENCY_OPTIONS] + SENSOR_SCHEMA = vol.Schema( { - vol.Required(const.CONF_FREQUENCY): vol.In(const.FREQUENCY_OPTIONS), + vol.Required(const.CONF_FREQUENCY): vol.In(frequencies), vol.Optional(const.CONF_ICON_NORMAL): cv.icon, vol.Optional(const.CONF_ICON_TODAY): cv.icon, vol.Optional(const.CONF_ICON_TOMORROW): cv.icon, @@ -37,8 +40,8 @@ vol.Optional(const.CONF_COLLECTION_DAYS): vol.All( cv.ensure_list, [vol.In(WEEKDAYS)] ), - vol.Optional(const.CONF_FIRST_MONTH): vol.In(const.MONTH_OPTIONS), - vol.Optional(const.CONF_LAST_MONTH): vol.In(const.MONTH_OPTIONS), + vol.Optional(const.CONF_FIRST_MONTH): vol.In(months), + vol.Optional(const.CONF_LAST_MONTH): vol.In(months), vol.Optional(const.CONF_WEEKDAY_ORDER_NUMBER): vol.All( cv.ensure_list, [vol.All(vol.Coerce(int), vol.Range(min=1, max=5))] ), diff --git a/custom_components/garbage_collection/sensor.py b/custom_components/garbage_collection/sensor.py index 4e88699..8bba63a 100644 --- a/custom_components/garbage_collection/sensor.py +++ b/custom_components/garbage_collection/sensor.py @@ -96,16 +96,13 @@ def __init__(self, config_entry: ConfigEntry) -> None: self._hidden = config.get(ATTR_HIDDEN, False) self._manual = config.get(const.CONF_MANUAL) first_month = config.get(const.CONF_FIRST_MONTH, const.DEFAULT_FIRST_MONTH) + months = [m["value"] for m in const.MONTH_OPTIONS] self._first_month: int = ( - const.MONTH_OPTIONS.index(first_month) + 1 - if first_month in const.MONTH_OPTIONS - else 1 + months.index(first_month) + 1 if first_month in months else 1 ) last_month = config.get(const.CONF_LAST_MONTH, const.DEFAULT_LAST_MONTH) self._last_month: int = ( - const.MONTH_OPTIONS.index(last_month) + 1 - if last_month in const.MONTH_OPTIONS - else 12 + months.index(last_month) + 1 if last_month in months else 12 ) self._verbose_state = config.get(const.CONF_VERBOSE_STATE) self._icon_normal = config.get(const.CONF_ICON_NORMAL) @@ -322,19 +319,20 @@ def move_to_range(self, day: date) -> date: if not self.date_inside(day): year = day.year month = day.month + months = [m["label"] for m in const.MONTH_OPTIONS] if self._first_month <= self._last_month < month: _LOGGER.debug( "(%s) %s outside the range, lookig from %s next year", self._attr_name, day, - const.MONTH_OPTIONS[self._first_month - 1], + months[self._first_month - 1], ) return date(year + 1, self._first_month, 1) _LOGGER.debug( "(%s) %s outside the range, searching from %s", self._attr_name, day, - const.MONTH_OPTIONS[self._first_month - 1], + months[self._first_month - 1], ) return date(year, self._first_month, 1) return day