Skip to content

Commit

Permalink
fix: Fixed issue where start, end and offset could not be unset for t…
Browse files Browse the repository at this point in the history
…arget rate config (15 minutes)
  • Loading branch information
BottlecapDave committed May 18, 2024
1 parent 6aeada6 commit cb170af
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
10 changes: 5 additions & 5 deletions custom_components/octopus_energy/config/target_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down
21 changes: 6 additions & 15 deletions custom_components/octopus_energy/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/config/test_validate_target_rate_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit cb170af

Please sign in to comment.