mirror of
https://github.com/basnijholt/adaptive-lighting.git
synced 2026-09-11 22:34:04 +02:00
create validate function
This commit is contained in:
parent
59f8f743ab
commit
8618cd89b5
1 changed files with 15 additions and 11 deletions
|
|
@ -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]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue