Skip to content

Commit

Permalink
Fix Garbage collection schedule ignores first & last month #442
Browse files Browse the repository at this point in the history
  • Loading branch information
bruxy70 committed Dec 9, 2022
1 parent 383b648 commit ae73818
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
9 changes: 6 additions & 3 deletions custom_components/garbage_collection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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))]
),
Expand Down
14 changes: 6 additions & 8 deletions custom_components/garbage_collection/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ae73818

Please sign in to comment.