From ab069de4aabb2c972ff062a7f203585ebaef6e16 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Fri, 25 Sep 2020 10:49:58 +0200 Subject: [PATCH] simplify --- custom_components/adaptive_lighting/const.py | 7 ++++--- custom_components/adaptive_lighting/switch.py | 17 +++-------------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/custom_components/adaptive_lighting/const.py b/custom_components/adaptive_lighting/const.py index a963efae..56cb1079 100644 --- a/custom_components/adaptive_lighting/const.py +++ b/custom_components/adaptive_lighting/const.py @@ -97,8 +97,9 @@ def maybe_coerce(key, validation): return validation -def replace_none(x): - return x if x != NONE_STR else vol.UNDEFINED +def replace_none(value, replace_with=None): + """Replaces "None" -> replace_with.""" + return value if value != NONE_STR else replace_with validation_tuples = [ @@ -108,7 +109,7 @@ validation_tuples = [ _DOMAIN_SCHEMA = vol.Schema( { - vol.Optional(key, default=replace_none(default)): validation + vol.Optional(key, default=replace_none(default, vol.UNDEFINED)): validation for key, default, validation in validation_tuples } ) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index c47d6fb8..00b95565 100755 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -5,7 +5,6 @@ import bisect import logging from copy import deepcopy from datetime import timedelta -from functools import partial import voluptuous as vol @@ -74,11 +73,11 @@ from .const import ( DOMAIN, EXTRA_VALIDATION, ICON, - NONE_STR, SERVICE_APPLY, SUN_EVENT_MIDNIGHT, SUN_EVENT_NOON, VALIDATION_TUPLES, + replace_none, ) _SUPPORT_OPTS = { @@ -88,13 +87,8 @@ _SUPPORT_OPTS = { "transition": SUPPORT_TRANSITION, } -_ALLOWED_ORDERS = { - (SUN_EVENT_SUNRISE, SUN_EVENT_NOON, SUN_EVENT_SUNSET, SUN_EVENT_MIDNIGHT), - (SUN_EVENT_SUNSET, SUN_EVENT_MIDNIGHT, SUN_EVENT_SUNRISE, SUN_EVENT_NOON), - (SUN_EVENT_MIDNIGHT, SUN_EVENT_SUNRISE, SUN_EVENT_NOON, SUN_EVENT_SUNSET), - (SUN_EVENT_NOON, SUN_EVENT_SUNSET, SUN_EVENT_MIDNIGHT, SUN_EVENT_SUNRISE), -} - +_ORDER = (SUN_EVENT_SUNRISE, SUN_EVENT_NOON, SUN_EVENT_SUNSET, SUN_EVENT_MIDNIGHT) +_ALLOWED_ORDERS = {_ORDER[i:] + _ORDER[:i] for i in range(len(_ORDER))} _LOGGER = logging.getLogger(__name__) @@ -133,11 +127,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities): async_add_entities([switch], update_before_add=True) -def replace_none(value): - """Replaces "None" -> None.""" - return value if value != NONE_STR 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}