Skip to content

Commit

Permalink
Address changes in config flow
Browse files Browse the repository at this point in the history
#441 (reading default values TBD)
  • Loading branch information
bruxy70 committed Dec 7, 2022
1 parent fa3b11e commit 89bd946
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 46 deletions.
118 changes: 74 additions & 44 deletions custom_components/garbage_collection/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
_LOGGER = logging.getLogger(__name__)


def _validate_config(data: Any) -> Any:
async def _validate_config(
handler: SchemaConfigFlowHandler | SchemaOptionsFlowHandler, data: Any
) -> Any:
"""Validate config."""
if const.CONF_DATE in data:
try:
Expand Down Expand Up @@ -61,121 +63,149 @@ def optional(
return vol.Optional(key, description={"suggested_value": suggested_value})


def general_options_schema(
_: SchemaConfigFlowHandler | SchemaOptionsFlowHandler,
options: Dict[str, Any],
async def general_options_schema(
handler: SchemaConfigFlowHandler | SchemaOptionsFlowHandler,
) -> vol.Schema:
"""Generate options schema."""
return vol.Schema(
{
required(const.CONF_FREQUENCY, options, const.DEFAULT_FREQUENCY): vol.In(
const.FREQUENCY_OPTIONS
),
required(
const.CONF_FREQUENCY, handler.options, const.DEFAULT_FREQUENCY
): vol.In(const.FREQUENCY_OPTIONS),
optional(
const.CONF_ICON_NORMAL, options, const.DEFAULT_ICON_NORMAL
const.CONF_ICON_NORMAL, handler.options, const.DEFAULT_ICON_NORMAL
): selector.IconSelector(),
optional(
const.CONF_ICON_TODAY, options, const.DEFAULT_ICON_TODAY
const.CONF_ICON_TODAY, handler.options, const.DEFAULT_ICON_TODAY
): selector.IconSelector(),
optional(
const.CONF_ICON_TOMORROW, options, const.DEFAULT_ICON_TOMORROW
const.CONF_ICON_TOMORROW, handler.options, const.DEFAULT_ICON_TOMORROW
): selector.IconSelector(),
optional(const.CONF_EXPIRE_AFTER, options): selector.TimeSelector(),
optional(const.CONF_EXPIRE_AFTER, handler.options): selector.TimeSelector(),
optional(
const.CONF_VERBOSE_STATE, options, const.DEFAULT_VERBOSE_STATE
const.CONF_VERBOSE_STATE, handler.options, const.DEFAULT_VERBOSE_STATE
): bool,
optional(ATTR_HIDDEN, options, False): bool,
optional(const.CONF_MANUAL, options, False): bool,
optional(ATTR_HIDDEN, handler.options, False): bool,
optional(const.CONF_MANUAL, handler.options, False): bool,
}
)


def general_config_schema(
async def general_config_schema(
handler: SchemaConfigFlowHandler | SchemaOptionsFlowHandler,
options: Dict[str, Any],
) -> vol.Schema:
"""Generate config schema."""
return vol.Schema(
{
optional(CONF_NAME, options): selector.TextSelector(),
optional(CONF_NAME, handler.options): selector.TextSelector(),
required(
const.CONF_FREQUENCY, handler.options, const.DEFAULT_FREQUENCY
): vol.In(const.FREQUENCY_OPTIONS),
optional(
const.CONF_ICON_NORMAL, handler.options, const.DEFAULT_ICON_NORMAL
): selector.IconSelector(),
optional(
const.CONF_ICON_TODAY, handler.options, const.DEFAULT_ICON_TODAY
): selector.IconSelector(),
optional(
const.CONF_ICON_TOMORROW, handler.options, const.DEFAULT_ICON_TOMORROW
): selector.IconSelector(),
optional(const.CONF_EXPIRE_AFTER, handler.options): selector.TimeSelector(),
optional(
const.CONF_VERBOSE_STATE, handler.options, const.DEFAULT_VERBOSE_STATE
): bool,
optional(ATTR_HIDDEN, handler.options, False): bool,
optional(const.CONF_MANUAL, handler.options, False): bool,
}
).extend(general_options_schema(handler, options).schema)
)


def detail_config_schema(
_,
options: Dict[str, Any],
async def detail_config_schema(
handler: SchemaConfigFlowHandler | SchemaOptionsFlowHandler,
) -> vol.Schema:
"""Generate options schema."""
options_schema: Dict[vol.Optional | vol.Required, Any] = {}
if options[const.CONF_FREQUENCY] in const.ANNUAL_FREQUENCY:
if handler.options[const.CONF_FREQUENCY] in const.ANNUAL_FREQUENCY:
# "annual"
options_schema[required(const.CONF_DATE, options)] = str
elif options[const.CONF_FREQUENCY] in const.GROUP_FREQUENCY:
options_schema[required(const.CONF_DATE, handler.options)] = str
elif handler.options[const.CONF_FREQUENCY] in const.GROUP_FREQUENCY:
# "group"
options_schema[required(CONF_ENTITIES, options)] = selector.EntitySelector(
options_schema[
required(CONF_ENTITIES, handler.options)
] = selector.EntitySelector(
selector.EntitySelectorConfig(
domain="sensor", integration=const.DOMAIN, multiple=True
),
)
elif options[const.CONF_FREQUENCY] not in const.BLANK_FREQUENCY:
elif handler.options[const.CONF_FREQUENCY] not in const.BLANK_FREQUENCY:
# everything else except "blank" and every-n-days
if options[const.CONF_FREQUENCY] not in const.DAILY_FREQUENCY:
if handler.options[const.CONF_FREQUENCY] not in const.DAILY_FREQUENCY:
weekdays_dict = {weekday: weekday for weekday in WEEKDAYS}
options_schema[
required(const.CONF_COLLECTION_DAYS, options)
required(const.CONF_COLLECTION_DAYS, handler.options)
] = cv.multi_select(weekdays_dict)
# everything else except "blank"
options_schema[
optional(const.CONF_FIRST_MONTH, options, const.DEFAULT_FIRST_MONTH)
optional(const.CONF_FIRST_MONTH, handler.options, const.DEFAULT_FIRST_MONTH)
] = vol.In(const.MONTH_OPTIONS)
options_schema[
optional(const.CONF_LAST_MONTH, options, const.DEFAULT_LAST_MONTH)
optional(const.CONF_LAST_MONTH, handler.options, const.DEFAULT_LAST_MONTH)
] = vol.In(const.MONTH_OPTIONS)
if options[const.CONF_FREQUENCY] in const.MONTHLY_FREQUENCY:
if handler.options[const.CONF_FREQUENCY] in const.MONTHLY_FREQUENCY:
# "monthly"
options_schema[
optional(const.CONF_WEEKDAY_ORDER_NUMBER, options)
optional(const.CONF_WEEKDAY_ORDER_NUMBER, handler.options)
] = vol.All(
cv.multi_select(
{"1": "1st", "2": "2nd", "3": "3rd", "4": "4th", "5": "5th"}
),
)
options_schema[optional(const.CONF_FORCE_WEEK_NUMBERS, options)] = bool
if options[const.CONF_FREQUENCY] in const.WEEKLY_DAILY_MONTHLY:
options_schema[
optional(const.CONF_FORCE_WEEK_NUMBERS, handler.options)
] = bool
if handler.options[const.CONF_FREQUENCY] in const.WEEKLY_DAILY_MONTHLY:
# "every-n-weeks", "every-n-days", "monthly"
options_schema[required(const.CONF_PERIOD, options)] = vol.All(
options_schema[required(const.CONF_PERIOD, handler.options)] = vol.All(
vol.Coerce(int), vol.Range(min=1, max=1000)
)
if options[const.CONF_FREQUENCY] in const.WEEKLY_FREQUENCY_X:
if handler.options[const.CONF_FREQUENCY] in const.WEEKLY_FREQUENCY_X:
# every-n-weeks
options_schema[
required(const.CONF_FIRST_WEEK, options, const.DEFAULT_FIRST_WEEK)
required(
const.CONF_FIRST_WEEK, handler.options, const.DEFAULT_FIRST_WEEK
)
] = vol.All(vol.Coerce(int), vol.Range(min=1, max=52))
if options[const.CONF_FREQUENCY] in const.DAILY_FREQUENCY:
if handler.options[const.CONF_FREQUENCY] in const.DAILY_FREQUENCY:
# every-n-days
options_schema[
required(const.CONF_FIRST_DATE, options)
required(const.CONF_FIRST_DATE, handler.options)
] = selector.DateSelector()
if options.get(const.CONF_VERBOSE_STATE, False):
if handler.options.get(const.CONF_VERBOSE_STATE, False):
# "verbose_state"
options_schema[
required(const.CONF_VERBOSE_FORMAT, options, const.DEFAULT_VERBOSE_FORMAT)
required(
const.CONF_VERBOSE_FORMAT, handler.options, const.DEFAULT_VERBOSE_FORMAT
)
] = cv.string
options_schema[
required(const.CONF_DATE_FORMAT, options, const.DEFAULT_DATE_FORMAT)
required(const.CONF_DATE_FORMAT, handler.options, const.DEFAULT_DATE_FORMAT)
] = cv.string
return vol.Schema(options_schema)


async def choose_details_step(options: dict[str, Any]) -> str:
"""Return next step_id for options flow."""
return "detail"


CONFIG_FLOW: Dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = {
"user": SchemaFlowFormStep(general_config_schema, next_step=lambda x: "detail"),
"user": SchemaFlowFormStep(general_config_schema, next_step=choose_details_step),
"detail": SchemaFlowFormStep(
detail_config_schema, validate_user_input=_validate_config
),
}
OPTIONS_FLOW: Dict[str, SchemaFlowFormStep | SchemaFlowMenuStep] = {
"init": SchemaFlowFormStep(general_options_schema, next_step=lambda x: "detail"),
"init": SchemaFlowFormStep(general_options_schema, next_step=choose_details_step),
"detail": SchemaFlowFormStep(
detail_config_schema, validate_user_input=_validate_config
),
Expand Down
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"zip_release": true,
"filename": "garbage_collection.zip",
"domains": ["sensor"],
"homeassistant": "2022.5.0"
"homeassistant": "2022.12.0"
}
2 changes: 1 addition & 1 deletion requirements_tests.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
python-dateutil>=2.8.2
types-python-dateutil>=2.8.18
homeassistant>=2022.7.2
homeassistant>=2022.12.0
pytest-homeassistant-custom-component>=0.10.2
aiohttp_cors>=0.7.0

0 comments on commit 89bd946

Please sign in to comment.