Skip to content

Commit

Permalink
Bettter handling of defaults in 0.110.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bruxy70 committed May 20, 2020
1 parent a7f85e4 commit 584716a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
27 changes: 17 additions & 10 deletions custom_components/garbage_collection/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,24 @@
_LOGGER = logging.getLogger(__name__)


def clean_optional(dict, key):
"""Remove optional keys before update"""
if key in dict:
del dict[key]


class garbage_collection_options:
def __init__(self, unique_id):
self._data = {}
self._data["unique_id"] = unique_id
self.errors = {}
self.data_schema = {}

def update_data(self, user_input, step):
self._data.update(user_input)
# Remove empty fields
items = {
key: value
for (key, value) in CONFIGURATION.options.items()
if ("step" in value and value["step"] == step)
}
for key, value in items.items():
if key in self._data and (key not in user_input or user_input[key] == ""):
del self._data[key]

def step1_user_init(self, user_input, defaults=None):
"""
Expand Down Expand Up @@ -54,9 +60,10 @@ def step1_user_init(self, user_input, defaults=None):
CONFIGURATION.set_defaults(1, user_input)
if self.errors == {}:
# Valid input - go to the next step!
self._data.update(user_input)
self.update_data(user_input, 1)
return True
elif defaults is not None:
CONFIGURATION.reset_defaults()
CONFIGURATION.set_defaults(1, defaults)
self.data_schema = CONFIGURATION.compile_config_flow(step=1)
return False
Expand Down Expand Up @@ -90,7 +97,7 @@ def step2_annual_group(self, user_input, defaults=None):
# Remember step2 values
if self._data[CONF_FREQUENCY] in GROUP_FREQUENCY:
updates[CONF_ENTITIES] = string_to_list(user_input[CONF_ENTITIES])
self._data.update(updates)
self.update_data(updates, 2)
return True
elif defaults is not None:
CONFIGURATION.set_defaults(2, defaults)
Expand Down Expand Up @@ -135,7 +142,7 @@ def step3_detail(self, user_input, defaults=None):
self.errors["base"] = "days"
if self.errors == {}:
# Remember values
self._data.update(updates)
self.update_data(updates, 3)
return True
elif defaults is not None:
CONFIGURATION.set_defaults(3, defaults)
Expand Down Expand Up @@ -205,7 +212,7 @@ def step4_final(self, user_input, defaults=None):
if len(updates[CONF_WEEKDAY_ORDER_NUMBER]) == 0:
self.errors["base"] = CONF_WEEKDAY_ORDER_NUMBER
if self.errors == {}:
self._data.update(updates)
self.update_data(updates, 4)
if CONF_FORCE_WEEK_NUMBERS in self._data:
if self._data[CONF_FORCE_WEEK_NUMBERS]:
if CONF_WEEKDAY_ORDER_NUMBER in self._data:
Expand Down
19 changes: 8 additions & 11 deletions custom_components/garbage_collection/config_singularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,12 @@ def compile_config_flow(self, step, valid_for=None):
}
for key, value in items.items():
if key in self.__defaults:
# result[
# value["method"](
# key,
# description={"suggested_value": self.__defaults[key]}
# )
# ] = value["type"]
result[value["method"](key, default=self.__defaults[key])] = value[
"type"
]
result[
value["method"](
key,
description={"suggested_value": self.__defaults[key]}
)
] = value["type"]
else:
result[value["method"](key)] = value["type"]
return result
Expand All @@ -97,8 +94,8 @@ def compile_schema(self, step=None, valid_for=None):
for key, value in items.items():
# use the validator if exists, otherwise the type
t = value["validator"] if "validator" in value else value["type"]
if key in self.__defaults:
result[value["method"](key, default=self.__defaults[key])] = t
if "default" in value:
result[value["method"](key, default=value["default"])] = t
else:
result[value["method"](key)] = t
return result
Expand Down
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"zip_release": true,
"filename": "garbage_collection.zip",
"domains": ["garbage_collection", "sensor"],
"homeassistant": "0.109.0",
"homeassistant": "0.110.0",
"iot_class": "Local Push"
}

0 comments on commit 584716a

Please sign in to comment.