From cb170afa78de0940c17b333b76708ef5e9c834e8 Mon Sep 17 00:00:00 2001 From: BottlecapDave Date: Sat, 18 May 2024 06:15:52 +0100 Subject: [PATCH] fix: Fixed issue where start, end and offset could not be unset for target rate config (15 minutes) --- .../octopus_energy/config/target_rates.py | 10 +++--- .../octopus_energy/config_flow.py | 21 ++++-------- .../test_validate_target_rate_config.py | 32 +++++++++++++++++++ 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/custom_components/octopus_energy/config/target_rates.py b/custom_components/octopus_energy/config/target_rates.py index 5ea1cce0..47dbc54d 100644 --- a/custom_components/octopus_energy/config/target_rates.py +++ b/custom_components/octopus_energy/config/target_rates.py @@ -151,17 +151,17 @@ def validate_target_rate_config(data, account_info, now): if data[CONFIG_TARGET_HOURS] < 0.5: errors[CONFIG_TARGET_HOURS] = "invalid_target_hours" - if CONFIG_TARGET_START_TIME in data: + if CONFIG_TARGET_START_TIME in data and data[CONFIG_TARGET_START_TIME] is not None: matches = re.search(REGEX_TIME, data[CONFIG_TARGET_START_TIME]) if matches is None: errors[CONFIG_TARGET_START_TIME] = "invalid_target_time" - if CONFIG_TARGET_END_TIME in data: + if CONFIG_TARGET_END_TIME in data and data[CONFIG_TARGET_END_TIME] is not None: matches = re.search(REGEX_TIME, data[CONFIG_TARGET_END_TIME]) if matches is None: errors[CONFIG_TARGET_END_TIME] = "invalid_target_time" - if CONFIG_TARGET_OFFSET in data: + if CONFIG_TARGET_OFFSET in data and data[CONFIG_TARGET_OFFSET] is not None: matches = re.search(REGEX_OFFSET_PARTS, data[CONFIG_TARGET_OFFSET]) if matches is None: errors[CONFIG_TARGET_OFFSET] = "invalid_offset" @@ -197,8 +197,8 @@ def validate_target_rate_config(data, account_info, now): if data[CONFIG_TARGET_TYPE] != CONFIG_TARGET_TYPE_CONTINUOUS: errors[CONFIG_TARGET_WEIGHTING] = "weighting_not_supported" - start_time = data[CONFIG_TARGET_START_TIME] if CONFIG_TARGET_START_TIME in data else "00:00" - end_time = data[CONFIG_TARGET_END_TIME] if CONFIG_TARGET_END_TIME in data else "00:00" + start_time = data[CONFIG_TARGET_START_TIME] if CONFIG_TARGET_START_TIME in data and data[CONFIG_TARGET_START_TIME] is not None else "00:00" + end_time = data[CONFIG_TARGET_END_TIME] if CONFIG_TARGET_END_TIME in data and data[CONFIG_TARGET_END_TIME] is not None else "00:00" is_time_valid = CONFIG_TARGET_START_TIME not in errors and CONFIG_TARGET_END_TIME not in errors diff --git a/custom_components/octopus_energy/config_flow.py b/custom_components/octopus_energy/config_flow.py index ef327bbc..fb98b32f 100644 --- a/custom_components/octopus_energy/config_flow.py +++ b/custom_components/octopus_energy/config_flow.py @@ -356,18 +356,6 @@ async def __async_setup_target_rate_schema__(self, config, errors): if (CONFIG_TARGET_MPAN not in config): config[CONFIG_TARGET_MPAN] = meters[0] - start_time_key = vol.Optional(CONFIG_TARGET_START_TIME) - if (CONFIG_TARGET_START_TIME in config): - start_time_key = vol.Optional(CONFIG_TARGET_START_TIME, default=config[CONFIG_TARGET_START_TIME]) - - end_time_key = vol.Optional(CONFIG_TARGET_END_TIME) - if (CONFIG_TARGET_END_TIME in config): - end_time_key = vol.Optional(CONFIG_TARGET_END_TIME, default=config[CONFIG_TARGET_END_TIME]) - - offset_key = vol.Optional(CONFIG_TARGET_OFFSET) - if (CONFIG_TARGET_OFFSET in config): - offset_key = vol.Optional(CONFIG_TARGET_OFFSET, default=config[CONFIG_TARGET_OFFSET]) - # True by default for backwards compatibility is_rolling_target = True if (CONFIG_TARGET_ROLLING_TARGET in config): @@ -402,9 +390,9 @@ async def __async_setup_target_rate_schema__(self, config, errors): mode=selector.SelectSelectorMode.DROPDOWN, ) ), - start_time_key: str, - end_time_key: str, - offset_key: str, + vol.Optional(CONFIG_TARGET_START_TIME): str, + vol.Optional(CONFIG_TARGET_END_TIME): str, + vol.Optional(CONFIG_TARGET_OFFSET): str, vol.Optional(CONFIG_TARGET_ROLLING_TARGET): bool, vol.Optional(CONFIG_TARGET_LAST_RATES): bool, vol.Optional(CONFIG_TARGET_INVERT_TARGET_RATES): bool, @@ -417,6 +405,9 @@ async def __async_setup_target_rate_schema__(self, config, errors): CONFIG_TARGET_HOURS: f'{config[CONFIG_TARGET_HOURS]}', CONFIG_TARGET_TYPE: config[CONFIG_TARGET_TYPE], CONFIG_TARGET_MPAN: config[CONFIG_TARGET_MPAN], + CONFIG_TARGET_START_TIME: config[CONFIG_TARGET_START_TIME] if CONFIG_TARGET_START_TIME in config else None, + CONFIG_TARGET_END_TIME: config[CONFIG_TARGET_END_TIME] if CONFIG_TARGET_END_TIME in config else None, + CONFIG_TARGET_OFFSET: config[CONFIG_TARGET_OFFSET] if CONFIG_TARGET_OFFSET in config else None, CONFIG_TARGET_ROLLING_TARGET: is_rolling_target, CONFIG_TARGET_LAST_RATES: find_last_rates, CONFIG_TARGET_INVERT_TARGET_RATES: invert_target_rates, diff --git a/tests/unit/config/test_validate_target_rate_config.py b/tests/unit/config/test_validate_target_rate_config.py index 7d050125..bbde0c90 100644 --- a/tests/unit/config/test_validate_target_rate_config.py +++ b/tests/unit/config/test_validate_target_rate_config.py @@ -59,6 +59,38 @@ async def test_when_config_is_valid_no_errors_returned(): assert CONFIG_TARGET_MAX_RATE not in errors assert CONFIG_TARGET_WEIGHTING not in errors +@pytest.mark.asyncio +async def test_when_optional_config_is_valid_no_errors_returned(): + # Arrange + data = { + CONFIG_TARGET_TYPE: CONFIG_TARGET_TYPE_CONTINUOUS, + CONFIG_TARGET_NAME: "test", + CONFIG_TARGET_MPAN: mpan, + CONFIG_TARGET_HOURS: "1.5", + CONFIG_TARGET_START_TIME: None, + CONFIG_TARGET_END_TIME: None, + CONFIG_TARGET_OFFSET: None, + CONFIG_TARGET_MIN_RATE: None, + CONFIG_TARGET_MAX_RATE: None, + CONFIG_TARGET_WEIGHTING: None + } + + account_info = get_account_info() + + # Act + errors = validate_target_rate_config(data, account_info, now) + + # Assert + assert CONFIG_TARGET_NAME not in errors + assert CONFIG_TARGET_MPAN not in errors + assert CONFIG_TARGET_HOURS not in errors + assert CONFIG_TARGET_START_TIME not in errors + assert CONFIG_TARGET_END_TIME not in errors + assert CONFIG_TARGET_OFFSET not in errors + assert CONFIG_TARGET_MIN_RATE not in errors + assert CONFIG_TARGET_MAX_RATE not in errors + assert CONFIG_TARGET_WEIGHTING not in errors + @pytest.mark.asyncio @pytest.mark.parametrize("name,tariff",[ ("", non_agile_tariff),