Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve profile validation #1768

Merged
merged 2 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Version 2024.10.7 (2024-10-12)

- Improve profile validation

# Version 2024.10.6 (2024-10-11)

- Export SIMPLE_PROFILE_DICT, SIMPLE_WEEKDAY_LIST
-

# Version 2024.10.5 (2024-10-11)

Expand Down
21 changes: 13 additions & 8 deletions hahomematic/platforms/custom/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
_PARTY_INIT_DATE: Final = "2000_01_01 00:00"
_RAW_SCHEDULE_DICT = dict[str, float | int]
_TEMP_CELSIUS: Final = "°C"
SCHEDULE_SLOT_RANGE: Final = range(1, 14)
SCHEDULE_SLOT_RANGE: Final = range(1, 13)
SCHEDULE_SLOT_IN_RANGE: Final = range(1, 14)
SCHEDULE_TIME_RANGE: Final = range(1441)
HM_PRESET_MODE_PREFIX: Final = "week_program_"

Expand Down Expand Up @@ -456,7 +457,7 @@ def _validate_and_convert_simple_to_profile_weekday(
"""Convert simple weekday list to weekday dict."""
if not self.min_temp <= base_temperature <= self.max_temp:
raise ValidationException(
f"VALIDATE_SIMPLE_PROFILE: Base temperature {base_temperature} not in valid range (min: {self.min_temp}, "
f"VALIDATE_PROFILE: Base temperature {base_temperature} not in valid range (min: {self.min_temp}, "
f"max: {self.max_temp})"
)

Expand All @@ -468,22 +469,22 @@ def _validate_and_convert_simple_to_profile_weekday(
slot_no = 1
for slot in sorted_simple_weekday_list:
if (starttime := slot.get(ScheduleSlotType.STARTTIME)) is None:
raise ValidationException("VALIDATE_SIMPLE_PROFILE: STARTTIME is missing.")
raise ValidationException("VALIDATE_PROFILE: STARTTIME is missing.")
if (endtime := slot.get(ScheduleSlotType.ENDTIME)) is None:
raise ValidationException("VALIDATE_SIMPLE_PROFILE: ENDTIME is missing.")
raise ValidationException("VALIDATE_PROFILE: ENDTIME is missing.")
if (temperature := slot.get(ScheduleSlotType.TEMPERATURE)) is None:
raise ValidationException("VALIDATE_SIMPLE_PROFILE: TEMPERATURE is missing.")
raise ValidationException("VALIDATE_PROFILE: TEMPERATURE is missing.")

if _convert_time_str_to_minutes(str(starttime)) < _convert_time_str_to_minutes(
previous_endtime
):
raise ValidationException(
f"VALIDATE_SIMPLE_PROFILE: Timespans are overlapping with a previous slot for starttime: {starttime} / endtime: {endtime}"
f"VALIDATE_PROFILE: Timespans are overlapping with a previous slot for starttime: {starttime} / endtime: {endtime}"
)

if not self.min_temp <= float(temperature) <= self.max_temp:
raise ValidationException(
f"VALIDATE_SIMPLE_PROFILE: Temperature {temperature} not in valid range (min: {self.min_temp}, "
f"VALIDATE_PROFILE: Temperature {temperature} not in valid range (min: {self.min_temp}, "
f"max: {self.max_temp}) for starttime: {starttime} / endtime: {endtime}"
)

Expand Down Expand Up @@ -520,6 +521,10 @@ def _validate_profile_weekday(
) -> None:
"""Validate the profile weekday."""
previous_endtime = 0
if len(weekday_data) != 13:
raise ValidationException(
f"VALIDATE_PROFILE: {"Too many" if len(weekday_data) > 13 else "Too few"} slots in profile: {profile} / weekday: {weekday}"
)
for no in SCHEDULE_SLOT_RANGE:
if no not in weekday_data:
raise ValidationException(
Expand Down Expand Up @@ -942,7 +947,7 @@ def _sort_simple_weekday_list(simple_weekday_list: SIMPLE_WEEKDAY_LIST) -> SIMPL

def _fillup_weekday_data(base_temperature: float, weekday_data: WEEKDAY_DICT) -> WEEKDAY_DICT:
"""Fillup weekday data."""
for slot_no in SCHEDULE_SLOT_RANGE:
for slot_no in SCHEDULE_SLOT_IN_RANGE:
if slot_no not in weekday_data:
weekday_data[slot_no] = {
ScheduleSlotType.ENDTIME: "24:00",
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "hahomematic"
version = "2024.10.6"
version = "2024.10.7"
license = {text = "MIT License"}
description = "Homematic interface for Home Assistant running on Python 3."
readme = "README.md"
Expand Down
98 changes: 91 additions & 7 deletions tests/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,13 +675,16 @@ async def test_climate_ip_with_pydevccu(central_unit_mini) -> None:
profile="P1",
base_temperature=16.0,
simple_profile_data={
"MONDAY": [],
"TUESDAY": [],
"WEDNESDAY": [],
"THURSDAY": [],
"FRIDAY": [],
"SATURDAY": [],
"SUNDAY": [],
"MONDAY": [
{"TEMPERATURE": 17.0, "STARTTIME": "05:00", "ENDTIME": "06:00"},
{"TEMPERATURE": 22.0, "STARTTIME": "19:00", "ENDTIME": "22:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "09:00", "ENDTIME": "15:00"},
],
"TUESDAY": [
{"TEMPERATURE": 17.0, "STARTTIME": "05:00", "ENDTIME": "06:00"},
{"TEMPERATURE": 22.0, "STARTTIME": "19:00", "ENDTIME": "22:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "09:00", "ENDTIME": "15:00"},
],
},
)

Expand All @@ -692,3 +695,84 @@ async def test_climate_ip_with_pydevccu(central_unit_mini) -> None:
"MONDAY": [],
},
)

manual_simple_weekday_list3 = [
{"TEMPERATURE": 17.0, "STARTTIME": "05:00", "ENDTIME": "06:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "06:00", "ENDTIME": "07:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "07:00", "ENDTIME": "08:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "08:00", "ENDTIME": "09:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "09:00", "ENDTIME": "10:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "10:00", "ENDTIME": "11:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "11:00", "ENDTIME": "12:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "12:00", "ENDTIME": "13:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "13:00", "ENDTIME": "14:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "14:00", "ENDTIME": "15:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "15:00", "ENDTIME": "16:00"},
]
weekday_data3 = climate_bwth._validate_and_convert_simple_to_profile_weekday(
base_temperature=16.0, simple_weekday_list=manual_simple_weekday_list3
)
assert weekday_data3 == {
1: {"ENDTIME": "05:00", "TEMPERATURE": 16.0},
2: {"ENDTIME": "06:00", "TEMPERATURE": 17.0},
3: {"ENDTIME": "07:00", "TEMPERATURE": 17.0},
4: {"ENDTIME": "08:00", "TEMPERATURE": 17.0},
5: {"ENDTIME": "09:00", "TEMPERATURE": 17.0},
6: {"ENDTIME": "10:00", "TEMPERATURE": 17.0},
7: {"ENDTIME": "11:00", "TEMPERATURE": 17.0},
8: {"ENDTIME": "12:00", "TEMPERATURE": 17.0},
9: {"ENDTIME": "13:00", "TEMPERATURE": 17.0},
10: {"ENDTIME": "14:00", "TEMPERATURE": 17.0},
11: {"ENDTIME": "15:00", "TEMPERATURE": 17.0},
12: {"ENDTIME": "16:00", "TEMPERATURE": 17.0},
13: {"ENDTIME": "24:00", "TEMPERATURE": 16.0},
}
await climate_bwth.set_simple_profile_weekday(
profile="P1",
weekday="MONDAY",
base_temperature=16.0,
simple_weekday_list=manual_simple_weekday_list3,
)

await climate_bwth.set_simple_profile_weekday(
profile="P1",
weekday="MONDAY",
base_temperature=16.0,
simple_weekday_list=[
{"TEMPERATURE": 17.0, "STARTTIME": "05:00", "ENDTIME": "06:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "06:00", "ENDTIME": "07:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "13:00", "ENDTIME": "14:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "14:00", "ENDTIME": "15:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "15:00", "ENDTIME": "16:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "12:00", "ENDTIME": "13:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "07:00", "ENDTIME": "08:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "08:00", "ENDTIME": "09:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "10:00", "ENDTIME": "11:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "11:00", "ENDTIME": "12:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "09:00", "ENDTIME": "10:00"},
],
)

# 14 entries
with pytest.raises(ValidationException):
await climate_bwth.set_simple_profile_weekday(
profile="P1",
weekday="MONDAY",
base_temperature=16.0,
simple_weekday_list=[
{"TEMPERATURE": 17.0, "STARTTIME": "05:00", "ENDTIME": "06:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "06:00", "ENDTIME": "07:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "07:00", "ENDTIME": "08:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "08:00", "ENDTIME": "09:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "09:00", "ENDTIME": "10:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "10:00", "ENDTIME": "11:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "11:00", "ENDTIME": "12:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "12:00", "ENDTIME": "13:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "13:00", "ENDTIME": "14:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "14:00", "ENDTIME": "15:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "15:00", "ENDTIME": "16:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "16:00", "ENDTIME": "17:00"},
{"TEMPERATURE": 22.0, "STARTTIME": "17:00", "ENDTIME": "18:00"},
{"TEMPERATURE": 17.0, "STARTTIME": "18:00", "ENDTIME": "19:00"},
],
)