From 8618cd89b5d810f709326fc4a67869c9f6044f23 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Thu, 24 Sep 2020 23:47:22 +0200 Subject: [PATCH] create validate function --- custom_components/adaptive_lighting/switch.py | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 7c1c2f3a..594b69c2 100755 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -109,6 +109,20 @@ def replace_none(value): return value if value != FAKE_NONE else None +def validate(config_entry): + """Gets the options and data from the config_entry and adds defaults.""" + defaults = {key: default for key, default, _ in VALIDATION_TUPLES} + data = deepcopy(defaults) + data.update(config_entry.options) # come from options flow + data.update(config_entry.data) # all yaml settings come from data + data = {key: replace_none(value) for key, value in data.items()} + for key, (validate, _) in EXTRA_VALIDATION.items(): + value = data.get(key) + if value is not None: + data[key] = validate(value) # Fix the types of the inputs + return data + + class AdaptiveSwitch(SwitchEntity, RestoreEntity): """Representation of a Adaptive Lighting switch.""" @@ -119,17 +133,7 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): self._entity_id = f"switch.{DOMAIN}_{slugify(name)}" self._icon = ICON - defaults = {key: default for key, default, _ in VALIDATION_TUPLES} - data = deepcopy(defaults) - data.update(config_entry.options) # come from options flow - data.update(config_entry.data) # all yaml settings come from data - data = {key: replace_none(value) for key, value in data.items()} - for key, (validate, coerce) in EXTRA_VALIDATION.items(): - # Fix the types of the inputs - value = data.get(key) - if value is not None: - data[key] = validate(value) - + data = validate(config_entry) self._lights = data[CONF_LIGHTS] self._disable_brightness_adjust = data[CONF_DISABLE_BRIGHTNESS_ADJUST] self._disable_entity = data[CONF_DISABLE_ENTITY]