Skip to content

Commit

Permalink
Allow setting 0 for day of month and date config options, and None fo…
Browse files Browse the repository at this point in the history
…r Weekday Order and Chore Day, in order to reset them to blank
  • Loading branch information
bmcclure committed Apr 16, 2023
1 parent ca1a107 commit 3c21261
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
25 changes: 20 additions & 5 deletions custom_components/chore_helper/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,26 @@ async def _validate_config(
_: SchemaConfigFlowHandler | SchemaOptionsFlowHandler, data: Any
) -> Any:
"""Validate config."""
if const.CONF_DAY_OF_MONTH in data and data[const.CONF_DAY_OF_MONTH] < 1:
data[const.CONF_DAY_OF_MONTH] = None

if const.CONF_DATE in data:
try:
helpers.month_day_text(data[const.CONF_DATE])
except vol.Invalid as exc:
raise SchemaFlowError("month_day") from exc
if data[const.CONF_DATE] == "0" or data[const.CONF_DATE] == "0/0":
data[const.CONF_DATE] = ""
else:
try:
helpers.month_day_text(data[const.CONF_DATE])
except vol.Invalid as exc:
raise SchemaFlowError("month_day") from exc

if (
const.CONF_WEEKDAY_ORDER_NUMBER in data
and int(data[const.CONF_WEEKDAY_ORDER_NUMBER]) < 1
):
data[const.CONF_WEEKDAY_ORDER_NUMBER] = None

if const.CONF_CHORE_DAY in data and data[const.CONF_CHORE_DAY] == "0":
data[const.CONF_CHORE_DAY] = None
return data


Expand Down Expand Up @@ -158,7 +173,7 @@ async def detail_config_schema(
optional(const.CONF_DAY_OF_MONTH, handler.options)
] = selector.NumberSelector(
selector.NumberSelectorConfig(
min=1,
min=0,
max=31,
mode=selector.NumberSelectorMode.BOX,
)
Expand Down
2 changes: 2 additions & 0 deletions custom_components/chore_helper/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
BLANK_FREQUENCY = ["blank"]

WEEKDAY_OPTIONS = [
selector.SelectOptionDict(value="0", label="None"),
selector.SelectOptionDict(value="mon", label="Monday"),
selector.SelectOptionDict(value="tue", label="Tuesday"),
selector.SelectOptionDict(value="wed", label="Wednesday"),
Expand All @@ -111,6 +112,7 @@
]

ORDER_OPTIONS = [
selector.SelectOptionDict(value="0", label="None"),
selector.SelectOptionDict(value="1", label="1st"),
selector.SelectOptionDict(value="2", label="2nd"),
selector.SelectOptionDict(value="3", label="3rd"),
Expand Down
7 changes: 4 additions & 3 deletions custom_components/chore_helper/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ def __init__(self, config_entry: ConfigEntry) -> None:
config = config_entry.options
day_of_month = config.get(const.CONF_DAY_OF_MONTH)
self._day_of_month: int | None = (
int(day_of_month) if day_of_month is not None else None
int(day_of_month) if day_of_month is not None and day_of_month > 0 else None
)
self._chore_day = config.get(const.CONF_CHORE_DAY, None)
self._monthly_force_week_numbers = config.get(
Expand Down Expand Up @@ -827,8 +827,9 @@ def __init__(self, config_entry: ConfigEntry) -> None:
"""Read parameters specific for Yearly Chore Frequency."""
super().__init__(config_entry)
config = config_entry.options
self._period = config.get(const.CONF_PERIOD)
self._date = config.get(const.CONF_DATE)
self._period = config.get(const.CONF_PERIOD, 1)
due_date = config.get(const.CONF_DATE, None)
self._date = due_date if due_date is not None and due_date != "0" else None

def _find_candidate_date(self, day1: date) -> date | None:
"""Calculate possible date, for yearly frequency."""
Expand Down

0 comments on commit 3c21261

Please sign in to comment.