Skip to content

Commit 6ea2ed8

Browse files
committed
Refactor control example and display cooling info
Make get_attribute and the print helpers synchronous (they no longer await), reduce repetition, and show cooling details: mode changeover, heating/cooling setpoints in device state and separate heating/cooling bounds in static state.
1 parent 2cb3f97 commit 6ea2ed8

1 file changed

Lines changed: 77 additions & 95 deletions

File tree

examples/control.py

Lines changed: 77 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from discovery import get_bsblan_host, get_config_from_env
3636

3737

38-
async def get_attribute(
38+
def get_attribute(
3939
attribute: Any, attr_type: str = "value", default: str = "N/A"
4040
) -> str:
4141
"""Safely retrieve the desired property ('value' or 'desc') of an attribute.
@@ -103,16 +103,16 @@ def get_hvac_action_name(status_code: int) -> str:
103103
return category.name.lower()
104104

105105

106-
async def print_state(state: State) -> None:
106+
def print_state(state: State) -> None:
107107
"""Print the current state of the BSBLan device.
108108
109109
Args:
110110
state (State): The current state of the BSBLan device.
111111
112112
"""
113113
# Get the HVAC action - both the raw value and mapped action name
114-
hvac_action_desc = await get_attribute(state.hvac_action, "desc", "Unknown Action")
115-
hvac_action_value = await get_attribute(state.hvac_action, "value", "N/A")
114+
hvac_action_desc = get_attribute(state.hvac_action, "desc", "Unknown Action")
115+
hvac_action_value = get_attribute(state.hvac_action, "value", "N/A")
116116

117117
# Map the raw status code to a simplified action name using the new enum approach
118118
hvac_action_mapped = "N/A"
@@ -133,63 +133,56 @@ async def print_state(state: State) -> None:
133133
"HVAC Action (device desc)": hvac_action_desc,
134134
"HVAC Action (status name)": status_name,
135135
"HVAC Action (category)": hvac_action_mapped,
136-
"HVAC Mode": await get_attribute(state.hvac_mode, "desc", "Unknown Mode"),
137-
"Current Temperature": await get_attribute(
138-
state.current_temperature, "value", "N/A"
139-
),
136+
"HVAC Mode": get_attribute(state.hvac_mode, "desc", "Unknown Mode"),
137+
"Mode Changeover": get_attribute(state.hvac_mode_changeover, "desc"),
138+
"Target Temperature (heating)": get_attribute(state.target_temperature),
139+
"Cooling Setpoint (target high)": get_attribute(state.target_temperature_high),
140+
"Current Temperature": get_attribute(state.current_temperature),
140141
}
141142
print_attributes("Device State", attributes)
142143

143144

144-
async def print_sensor(sensor: Sensor) -> None:
145+
def print_sensor(sensor: Sensor) -> None:
145146
"""Print sensor information from the BSBLan device.
146147
147148
Args:
148149
sensor (Sensor): The sensor information from the BSBLan device.
149150
150151
"""
151152
attributes = {
152-
"Outside Temperature": await get_attribute(
153-
sensor.outside_temperature, "value", "N/A"
154-
),
155-
"Current Temperature": await get_attribute(
156-
sensor.current_temperature, "value", "N/A"
157-
),
153+
"Outside Temperature": get_attribute(sensor.outside_temperature),
154+
"Current Temperature": get_attribute(sensor.current_temperature),
158155
}
159156
print_attributes("Sensor Information", attributes)
160157

161158

162-
async def print_device_time(device_time: DeviceTime) -> None:
159+
def print_device_time(device_time: DeviceTime) -> None:
163160
"""Print device time information.
164161
165162
Args:
166163
device_time (DeviceTime): The device time information from the BSBLan device.
167164
168165
"""
169166
attributes = {
170-
"Current Time": await get_attribute(device_time.time, "value", "N/A"),
171-
"Time Unit": await get_attribute(device_time.time, "unit", "N/A"),
172-
"Time Description": await get_attribute(device_time.time, "desc", "N/A"),
167+
"Current Time": get_attribute(device_time.time, "value"),
168+
"Time Unit": get_attribute(device_time.time, "unit"),
169+
"Time Description": get_attribute(device_time.time, "desc"),
173170
}
174171
print_attributes("Device Time", attributes)
175172

176173

177-
async def print_device_info(device: Device, info: Info) -> None:
174+
def print_device_info(device: Device, info: Info) -> None:
178175
"""Print device and general information.
179176
180177
Args:
181178
device (Device): The device information from the BSBLan device.
182179
info (Info): The general information from the BSBLan device.
183180
184181
"""
185-
device_identification = await get_attribute(
186-
info.device_identification, "value", "N/A"
187-
)
188-
189182
attributes = {
190183
"Device Name": device.name or "N/A",
191184
"Version": device.version or "N/A",
192-
"Device Identification": device_identification,
185+
"Device Identification": get_attribute(info.device_identification),
193186
"Bus Type": format_optional(device.bus),
194187
"Bus Writable Flag": format_optional(device.buswritable),
195188
"Bus Address": format_optional(device.busaddr),
@@ -199,26 +192,31 @@ async def print_device_info(device: Device, info: Info) -> None:
199192
print_attributes("Device Information", attributes)
200193

201194

202-
async def print_static_state(static_state: StaticState) -> None:
203-
"""Print static state information.
195+
def print_static_state(static_state: StaticState) -> None:
196+
"""Print static state information, including heating and cooling bounds.
204197
205198
Args:
206199
static_state (StaticState): The static state information from the BSBLan device.
207200
208201
"""
209-
min_temp = await get_attribute(static_state.min_temp, "value", "N/A")
210-
max_temp = await get_attribute(static_state.max_temp, "value", "N/A")
211-
min_temp_unit = await get_attribute(static_state.min_temp, "unit", "N/A")
212-
213202
attributes = {
214-
"Min Temperature": min_temp,
215-
"Max Temperature": max_temp,
216-
"Min Temperature Unit": min_temp_unit,
203+
"Min Temperature (heating)": get_attribute(static_state.min_temp),
204+
"Max Temperature (heating)": get_attribute(static_state.max_temp),
205+
"Heating Protective Setpoint": get_attribute(
206+
static_state.heating_protective_setpoint
207+
),
208+
"Cooling Setpoint Min": get_attribute(
209+
static_state.cooling_comfort_setpoint_min
210+
),
211+
"Cooling Setpoint Max (reduced)": get_attribute(
212+
static_state.cooling_reduced_setpoint
213+
),
214+
"Temperature Unit": get_attribute(static_state.min_temp, "unit"),
217215
}
218216
print_attributes("Static State", attributes)
219217

220218

221-
async def print_hot_water_state(hot_water_state: HotWaterState) -> None:
219+
def print_hot_water_state(hot_water_state: HotWaterState) -> None:
222220
"""Print essential hot water state information.
223221
224222
Args:
@@ -227,24 +225,20 @@ async def print_hot_water_state(hot_water_state: HotWaterState) -> None:
227225
228226
"""
229227
attributes = {
230-
"Operating Mode": await get_attribute(
228+
"Operating Mode": get_attribute(
231229
hot_water_state.operating_mode, "desc", "Unknown Mode"
232230
),
233-
"Nominal Setpoint": await get_attribute(
234-
hot_water_state.nominal_setpoint, "value", "N/A"
235-
),
236-
"Release": await get_attribute(hot_water_state.release, "desc", "N/A"),
237-
"Current Temperature": await get_attribute(
238-
hot_water_state.dhw_actual_value_top_temperature, "value", "N/A"
239-
),
240-
"DHW Pump State": await get_attribute(
241-
hot_water_state.state_dhw_pump, "desc", "N/A"
231+
"Nominal Setpoint": get_attribute(hot_water_state.nominal_setpoint),
232+
"Release": get_attribute(hot_water_state.release, "desc"),
233+
"Current Temperature": get_attribute(
234+
hot_water_state.dhw_actual_value_top_temperature
242235
),
236+
"DHW Pump State": get_attribute(hot_water_state.state_dhw_pump, "desc"),
243237
}
244238
print_attributes("Hot Water State (Essential)", attributes)
245239

246240

247-
async def print_hot_water_config(hot_water_config: HotWaterConfig) -> None:
241+
def print_hot_water_config(hot_water_config: HotWaterConfig) -> None:
248242
"""Print hot water configuration information.
249243
250244
Args:
@@ -253,65 +247,53 @@ async def print_hot_water_config(hot_water_config: HotWaterConfig) -> None:
253247
254248
"""
255249
attributes = {
256-
"Nominal Setpoint Max": await get_attribute(
257-
hot_water_config.nominal_setpoint_max, "value", "N/A"
258-
),
259-
"Reduced Setpoint": await get_attribute(
260-
hot_water_config.reduced_setpoint, "value", "N/A"
250+
"Nominal Setpoint Max": get_attribute(hot_water_config.nominal_setpoint_max),
251+
"Reduced Setpoint": get_attribute(hot_water_config.reduced_setpoint),
252+
"Legionella Function": get_attribute(
253+
hot_water_config.legionella_function, "desc"
261254
),
262-
"Legionella Function": await get_attribute(
263-
hot_water_config.legionella_function, "desc", "N/A"
255+
"Legionella Setpoint": get_attribute(
256+
hot_water_config.legionella_function_setpoint
264257
),
265-
"Legionella Setpoint": await get_attribute(
266-
hot_water_config.legionella_function_setpoint, "value", "N/A"
258+
"Legionella Periodicity": get_attribute(
259+
hot_water_config.legionella_function_periodicity
267260
),
268-
"Legionella Periodicity": await get_attribute(
269-
hot_water_config.legionella_function_periodicity, "value", "N/A"
261+
"Circulation Pump Release": get_attribute(
262+
hot_water_config.dhw_circulation_pump_release, "desc"
270263
),
271-
"Circulation Pump Release": await get_attribute(
272-
hot_water_config.dhw_circulation_pump_release, "desc", "N/A"
273-
),
274-
"Circulation Setpoint": await get_attribute(
275-
hot_water_config.dhw_circulation_setpoint, "value", "N/A"
264+
"Circulation Setpoint": get_attribute(
265+
hot_water_config.dhw_circulation_setpoint
276266
),
277267
}
278268
print_attributes("Hot Water Configuration", attributes)
279269

280270

281-
async def print_hot_water_schedule(hot_water_schedule: HotWaterSchedule) -> None:
271+
def print_hot_water_schedule(hot_water_schedule: HotWaterSchedule) -> None:
282272
"""Print hot water schedule information.
283273
284274
Args:
285275
hot_water_schedule (HotWaterSchedule): The hot water schedule information
286276
from the BSBLan device (time programs).
287277
288278
"""
279+
days = (
280+
"monday",
281+
"tuesday",
282+
"wednesday",
283+
"thursday",
284+
"friday",
285+
"saturday",
286+
"sunday",
287+
)
289288
attributes = {
290-
"Monday": await get_attribute(
291-
hot_water_schedule.dhw_time_program_monday, "value", "N/A"
292-
),
293-
"Tuesday": await get_attribute(
294-
hot_water_schedule.dhw_time_program_tuesday, "value", "N/A"
295-
),
296-
"Wednesday": await get_attribute(
297-
hot_water_schedule.dhw_time_program_wednesday, "value", "N/A"
298-
),
299-
"Thursday": await get_attribute(
300-
hot_water_schedule.dhw_time_program_thursday, "value", "N/A"
301-
),
302-
"Friday": await get_attribute(
303-
hot_water_schedule.dhw_time_program_friday, "value", "N/A"
304-
),
305-
"Saturday": await get_attribute(
306-
hot_water_schedule.dhw_time_program_saturday, "value", "N/A"
307-
),
308-
"Sunday": await get_attribute(
309-
hot_water_schedule.dhw_time_program_sunday, "value", "N/A"
310-
),
311-
"Standard Values": await get_attribute(
312-
hot_water_schedule.dhw_time_program_standard_values, "value", "N/A"
313-
),
289+
day.capitalize(): get_attribute(
290+
getattr(hot_water_schedule, f"dhw_time_program_{day}")
291+
)
292+
for day in days
314293
}
294+
attributes["Standard Values"] = get_attribute(
295+
hot_water_schedule.dhw_time_program_standard_values
296+
)
315297
print_attributes("Hot Water Schedule", attributes)
316298

317299

@@ -337,11 +319,11 @@ async def main() -> None:
337319
# Get and print device and general info, including bus metadata
338320
device: Device = bsblan.device_info or await bsblan.device()
339321
info: Info = await bsblan.info()
340-
await print_device_info(device, info)
322+
print_device_info(device, info)
341323

342324
# Get and print state
343325
state: State = await bsblan.state()
344-
await print_state(state)
326+
print_state(state)
345327

346328
# Set thermostat temperature
347329
print("\nSetting temperature to 18°C")
@@ -353,34 +335,34 @@ async def main() -> None:
353335

354336
# Get and print sensor information
355337
sensor: Sensor = await bsblan.sensor()
356-
await print_sensor(sensor)
338+
print_sensor(sensor)
357339

358340
# Get and print device time
359341
if bsblan.supports_time_sync:
360342
device_time: DeviceTime = await bsblan.time()
361-
await print_device_time(device_time)
343+
print_device_time(device_time)
362344
else:
363345
print("\nDevice time is not available for this bus type")
364346

365347
# Get and print static state
366348
static_state: StaticState = await bsblan.static_values()
367-
await print_static_state(static_state)
349+
print_static_state(static_state)
368350

369351
# Get hot water state (essential parameters for frequent polling)
370352
hot_water_state: HotWaterState = await bsblan.hot_water_state()
371-
await print_hot_water_state(hot_water_state)
353+
print_hot_water_state(hot_water_state)
372354

373355
# Get hot water configuration (checked less frequently)
374356
try:
375357
hot_water_config: HotWaterConfig = await bsblan.hot_water_config()
376-
await print_hot_water_config(hot_water_config)
358+
print_hot_water_config(hot_water_config)
377359
except Exception as e: # noqa: BLE001 - Broad exception for demo purposes
378360
print(f"Hot water configuration not available: {e}")
379361

380362
# Get hot water schedule (time programs)
381363
try:
382364
hot_water_schedule: HotWaterSchedule = await bsblan.hot_water_schedule()
383-
await print_hot_water_schedule(hot_water_schedule)
365+
print_hot_water_schedule(hot_water_schedule)
384366
except Exception as e: # noqa: BLE001 - Broad exception for demo purposes
385367
print(f"Hot water schedule not available: {e}")
386368

0 commit comments

Comments
 (0)