Skip to content

Commit a2c6438

Browse files
committed
⬆️ Refactor hot water parameters: replace hardcoded values with constants for improved maintainability
1 parent 8f2c990 commit a2c6438

2 files changed

Lines changed: 163 additions & 151 deletions

File tree

src/bsblan/bsblan.py

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
API_VERSION_ERROR_MSG,
2424
API_VERSIONS,
2525
FIRMWARE_VERSION_ERROR_MSG,
26+
HOT_WATER_CONFIG_PARAMS,
27+
HOT_WATER_ESSENTIAL_PARAMS,
28+
HOT_WATER_SCHEDULE_PARAMS,
2629
HVAC_MODE_DICT,
2730
HVAC_MODE_DICT_REVERSE,
2831
MAX_VALID_YEAR,
@@ -720,16 +723,6 @@ async def hot_water_state(self) -> HotWaterState:
720723
HotWaterState: Essential hot water state information.
721724
722725
"""
723-
# Define essential parameters for frequent monitoring
724-
essential_param_ids = [
725-
"1600", # operating_mode
726-
"1610", # nominal_setpoint
727-
"1612", # reduced_setpoint
728-
"1620", # release
729-
"8830", # dhw_actual_value_top_temperature
730-
"8820", # state_dhw_pump
731-
]
732-
733726
# Use cached parameters or fall back to API validator
734727
hotwater_params = (
735728
self._hot_water_param_cache
@@ -738,7 +731,7 @@ async def hot_water_state(self) -> HotWaterState:
738731
essential_params = {
739732
param_id: param_name
740733
for param_id, param_name in hotwater_params.items()
741-
if param_id in essential_param_ids
734+
if param_id in HOT_WATER_ESSENTIAL_PARAMS
742735
}
743736

744737
if not essential_params:
@@ -760,25 +753,6 @@ async def hot_water_config(self) -> HotWaterConfig:
760753
HotWaterConfig: Hot water configuration information.
761754
762755
"""
763-
# Define configuration parameters
764-
config_param_ids = [
765-
"1601", # eco_mode_selection
766-
"1611", # nominal_setpoint_max
767-
"1640", # dhw_charging_priority
768-
"1602", # operating_mode_changeover
769-
"1642", # legionella_function
770-
"1644", # legionella_setpoint
771-
"1645", # legionella_periodicity
772-
"1646", # legionella_function_day
773-
"1647", # legionella_function_time
774-
"1648", # legionella_dwelling_time
775-
"1649", # legionella_circulation_pump
776-
"1650", # legionella_circulation_temp_diff
777-
"1660", # dhw_circulation_pump_release
778-
"1661", # dhw_circulation_pump_cycling
779-
"1663", # dhw_circulation_setpoint
780-
]
781-
782756
# Use cached parameters or fall back to API validator
783757
hotwater_params = (
784758
self._hot_water_param_cache
@@ -787,7 +761,7 @@ async def hot_water_config(self) -> HotWaterConfig:
787761
config_params = {
788762
param_id: param_name
789763
for param_id, param_name in hotwater_params.items()
790-
if param_id in config_param_ids
764+
if param_id in HOT_WATER_CONFIG_PARAMS
791765
}
792766

793767
if not config_params:
@@ -809,18 +783,6 @@ async def hot_water_schedule(self) -> HotWaterSchedule:
809783
HotWaterSchedule: Hot water schedule information.
810784
811785
"""
812-
# Define schedule parameters
813-
schedule_param_ids = [
814-
"561", # dhw_time_program_monday
815-
"562", # dhw_time_program_tuesday
816-
"563", # dhw_time_program_wednesday
817-
"564", # dhw_time_program_thursday
818-
"565", # dhw_time_program_friday
819-
"566", # dhw_time_program_saturday
820-
"567", # dhw_time_program_sunday
821-
"576", # dhw_time_program_standard_values
822-
]
823-
824786
# Use cached parameters or fall back to API validator
825787
hotwater_params = (
826788
self._hot_water_param_cache
@@ -829,7 +791,7 @@ async def hot_water_schedule(self) -> HotWaterSchedule:
829791
schedule_params = {
830792
param_id: param_name
831793
for param_id, param_name in hotwater_params.items()
832-
if param_id in schedule_param_ids
794+
if param_id in HOT_WATER_SCHEDULE_PARAMS
833795
}
834796

835797
if not schedule_params:

src/bsblan/constants.py

Lines changed: 157 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -27,117 +27,110 @@ class APIConfig(TypedDict):
2727
hot_water: dict[str, str]
2828

2929

30-
API_V1: Final[APIConfig] = {
31-
"heating": {
32-
"700": "hvac_mode",
33-
"710": "target_temperature",
34-
"900": "hvac_mode2",
35-
"8000": "hvac_action",
36-
"8740": "current_temperature",
37-
"8749": "room1_thermostat_mode",
38-
},
39-
"staticValues": {
40-
"714": "min_temp",
41-
"730": "max_temp",
42-
},
43-
"device": {
44-
"6224": "device_identification",
45-
"6225": "controller_family",
46-
"6226": "controller_variant",
47-
},
48-
"sensor": {
49-
"8700": "outside_temperature",
50-
"8740": "current_temperature",
51-
},
52-
"hot_water": {
53-
"1600": "operating_mode",
54-
"1601": "eco_mode_selection",
55-
"1610": "nominal_setpoint",
56-
"1614": "nominal_setpoint_max",
57-
"1612": "reduced_setpoint",
58-
"1620": "release",
59-
"1630": "dhw_charging_priority",
60-
"1640": "legionella_function",
61-
"1645": "legionella_setpoint",
62-
"1641": "legionella_periodicity",
63-
"1642": "legionella_function_day",
64-
"1643": "legionella_function_time",
65-
"1646": "legionella_dwelling_time",
66-
"1647": "legionella_circulation_pump",
67-
"1648": "legionella_circulation_temp_diff",
68-
"1660": "dhw_circulation_pump_release",
69-
"1661": "dhw_circulation_pump_cycling",
70-
"1663": "dhw_circulation_setpoint",
71-
"1680": "operating_mode_changeover",
72-
"8830": "dhw_actual_value_top_temperature",
73-
"8820": "state_dhw_pump",
74-
"561": "dhw_time_program_monday",
75-
"562": "dhw_time_program_tuesday",
76-
"563": "dhw_time_program_wednesday",
77-
"564": "dhw_time_program_thursday",
78-
"565": "dhw_time_program_friday",
79-
"566": "dhw_time_program_saturday",
80-
"567": "dhw_time_program_sunday",
81-
"576": "dhw_time_program_standard_values",
82-
},
30+
# Base parameters that exist in all API versions
31+
BASE_HEATING_PARAMS: Final[dict[str, str]] = {
32+
"700": "hvac_mode",
33+
"710": "target_temperature",
34+
"900": "hvac_mode2",
35+
"8000": "hvac_action",
36+
"8740": "current_temperature",
37+
"8749": "room1_thermostat_mode",
8338
}
8439

85-
API_V3: Final[APIConfig] = {
86-
"heating": {
87-
"700": "hvac_mode",
88-
"710": "target_temperature",
89-
"900": "hvac_mode2",
90-
"8000": "hvac_action",
91-
"8740": "current_temperature",
92-
"8749": "room1_thermostat_mode",
93-
"770": "room1_temp_setpoint_boost",
94-
},
95-
"staticValues": {
96-
"714": "min_temp",
97-
"716": "max_temp",
98-
},
99-
"device": {
100-
"6224": "device_identification",
101-
"6225": "controller_family",
102-
"6226": "controller_variant",
103-
},
104-
"sensor": {
105-
"8700": "outside_temperature",
106-
"8740": "current_temperature",
107-
},
108-
"hot_water": {
109-
"1600": "operating_mode",
110-
"1601": "eco_mode_selection",
111-
"1610": "nominal_setpoint",
112-
"1614": "nominal_setpoint_max",
113-
"1612": "reduced_setpoint",
114-
"1620": "release",
115-
"1630": "dhw_charging_priority",
116-
"1640": "legionella_function",
117-
"1645": "legionella_setpoint",
118-
"1641": "legionella_periodicity",
119-
"1642": "legionella_function_day",
120-
"1644": "legionella_function_time",
121-
"1646": "legionella_dwelling_time",
122-
"1647": "legionella_circulation_pump",
123-
"1648": "legionella_circulation_temp_diff",
124-
"1660": "dhw_circulation_pump_release",
125-
"1661": "dhw_circulation_pump_cycling",
126-
"1663": "dhw_circulation_setpoint",
127-
"1680": "operating_mode_changeover",
128-
"8830": "dhw_actual_value_top_temperature",
129-
"8820": "state_dhw_pump",
130-
"561": "dhw_time_program_monday",
131-
"562": "dhw_time_program_tuesday",
132-
"563": "dhw_time_program_wednesday",
133-
"564": "dhw_time_program_thursday",
134-
"565": "dhw_time_program_friday",
135-
"566": "dhw_time_program_saturday",
136-
"567": "dhw_time_program_sunday",
137-
"576": "dhw_time_program_standard_values",
138-
},
40+
BASE_STATIC_VALUES_PARAMS: Final[dict[str, str]] = {
41+
"714": "min_temp",
13942
}
14043

44+
BASE_DEVICE_PARAMS: Final[dict[str, str]] = {
45+
"6224": "device_identification",
46+
"6225": "controller_family",
47+
"6226": "controller_variant",
48+
}
49+
50+
BASE_SENSOR_PARAMS: Final[dict[str, str]] = {
51+
"8700": "outside_temperature",
52+
"8740": "current_temperature",
53+
}
54+
55+
BASE_HOT_WATER_PARAMS: Final[dict[str, str]] = {
56+
"1600": "operating_mode",
57+
"1601": "eco_mode_selection",
58+
"1610": "nominal_setpoint",
59+
"1614": "nominal_setpoint_max",
60+
"1612": "reduced_setpoint",
61+
"1620": "release",
62+
"1630": "dhw_charging_priority",
63+
"1640": "legionella_function",
64+
"1645": "legionella_setpoint",
65+
"1641": "legionella_periodicity",
66+
"1642": "legionella_function_day",
67+
"1644": "legionella_function_time",
68+
"1646": "legionella_dwelling_time",
69+
"1647": "legionella_circulation_pump",
70+
"1648": "legionella_circulation_temp_diff",
71+
"1660": "dhw_circulation_pump_release",
72+
"1661": "dhw_circulation_pump_cycling",
73+
"1663": "dhw_circulation_setpoint",
74+
"1680": "operating_mode_changeover",
75+
"8830": "dhw_actual_value_top_temperature",
76+
"8820": "state_dhw_pump",
77+
"561": "dhw_time_program_monday",
78+
"562": "dhw_time_program_tuesday",
79+
"563": "dhw_time_program_wednesday",
80+
"564": "dhw_time_program_thursday",
81+
"565": "dhw_time_program_friday",
82+
"566": "dhw_time_program_saturday",
83+
"567": "dhw_time_program_sunday",
84+
"576": "dhw_time_program_standard_values",
85+
}
86+
87+
# V1-specific parameters
88+
V1_STATIC_VALUES_EXTENSIONS: Final[dict[str, str]] = {
89+
"730": "max_temp", # V1 uses 730 for max_temp
90+
}
91+
92+
# V3-specific additional parameters
93+
V3_HEATING_EXTENSIONS: Final[dict[str, str]] = {
94+
"770": "room1_temp_setpoint_boost",
95+
# Future V3 extensions like 701, 701.1, 701.2 can be added here
96+
}
97+
98+
V3_STATIC_VALUES_EXTENSIONS: Final[dict[str, str]] = {
99+
"716": "max_temp", # V3 uses 716 for max_temp
100+
}
101+
102+
103+
def build_api_config(version: str) -> APIConfig:
104+
"""Build API configuration dynamically based on version.
105+
106+
Args:
107+
version: The API version ("v1" or "v3")
108+
109+
Returns:
110+
APIConfig: The complete API configuration for the specified version
111+
112+
"""
113+
config: APIConfig = {
114+
"heating": BASE_HEATING_PARAMS.copy(),
115+
"staticValues": BASE_STATIC_VALUES_PARAMS.copy(),
116+
"device": BASE_DEVICE_PARAMS.copy(),
117+
"sensor": BASE_SENSOR_PARAMS.copy(),
118+
"hot_water": BASE_HOT_WATER_PARAMS.copy(),
119+
}
120+
121+
if version == "v1":
122+
config["staticValues"].update(V1_STATIC_VALUES_EXTENSIONS)
123+
elif version == "v3":
124+
config["heating"].update(V3_HEATING_EXTENSIONS)
125+
config["staticValues"].update(V3_STATIC_VALUES_EXTENSIONS)
126+
127+
return config
128+
129+
130+
# Pre-built API configurations
131+
API_V1: Final[APIConfig] = build_api_config("v1")
132+
API_V3: Final[APIConfig] = build_api_config("v3")
133+
141134
API_VERSIONS: Final[dict[str, APIConfig]] = {
142135
"v1": API_V1,
143136
"v3": API_V3,
@@ -189,3 +182,60 @@ class APIConfig(TypedDict):
189182

190183
# Handle both ASCII and Unicode degree symbols
191184
TEMPERATURE_UNITS = {"°C", "°F", "°C", "°F", "°C", "°F"}
185+
186+
# Hot Water Parameter Groups
187+
# Essential parameters for frequent monitoring
188+
HOT_WATER_ESSENTIAL_PARAMS: Final[set[str]] = {
189+
param_id
190+
for param_id, name in BASE_HOT_WATER_PARAMS.items()
191+
if name
192+
in {
193+
"operating_mode",
194+
"nominal_setpoint",
195+
"reduced_setpoint",
196+
"release",
197+
"dhw_actual_value_top_temperature",
198+
"state_dhw_pump",
199+
}
200+
}
201+
202+
# Configuration parameters checked less frequently
203+
HOT_WATER_CONFIG_PARAMS: Final[set[str]] = {
204+
param_id
205+
for param_id, name in BASE_HOT_WATER_PARAMS.items()
206+
if name
207+
in {
208+
"eco_mode_selection",
209+
"nominal_setpoint_max",
210+
"dhw_charging_priority",
211+
"operating_mode_changeover",
212+
"legionella_function",
213+
"legionella_setpoint",
214+
"legionella_periodicity",
215+
"legionella_function_day",
216+
"legionella_function_time",
217+
"legionella_dwelling_time",
218+
"legionella_circulation_pump",
219+
"legionella_circulation_temp_diff",
220+
"dhw_circulation_pump_release",
221+
"dhw_circulation_pump_cycling",
222+
"dhw_circulation_setpoint",
223+
}
224+
}
225+
226+
# Schedule parameters (time programs)
227+
HOT_WATER_SCHEDULE_PARAMS: Final[set[str]] = {
228+
param_id
229+
for param_id, name in BASE_HOT_WATER_PARAMS.items()
230+
if name
231+
in {
232+
"dhw_time_program_monday",
233+
"dhw_time_program_tuesday",
234+
"dhw_time_program_wednesday",
235+
"dhw_time_program_thursday",
236+
"dhw_time_program_friday",
237+
"dhw_time_program_saturday",
238+
"dhw_time_program_sunday",
239+
"dhw_time_program_standard_values",
240+
}
241+
}

0 commit comments

Comments
 (0)