Skip to content
Jupiter documentation
Menu

Architecture

The modules of HVAC Horizon, the order of one control tick, and where to add a setting or a published value.

Layout#

The code is in custom_components/hvac_horizon/ and has three layers.

The first two layers hold no Home Assistant state: functions take values and return values. tests/test_layers.py reads the imports of every module in room_model/ and engine/ and fails on one outside its column.

Both packages sit inside the integration folder, because HACS installs that folder only. Python runs the integration's __init__.py before a module below it, so importing room_model on its own needs the package copied out of the folder.

Pure modules#

Coordinator#

HorizonCoordinator in coordinator.py is a DataUpdateCoordinator with a 300 second interval. It holds __init__, the tick and _plan_control. It inherits one mixin per area.

Each mixin lists the instance attributes it reads as class-level annotations. A test that patches a module-level function patches the module that calls it, for example coordinator_learning for fit.

Home Assistant surface#

One tick#

_async_update_data() runs these steps in order.

  1. Weather. _refresh_weather() fetches the daily and hourly forecast, at most every 30 minutes.
  2. Read and store. _read_inputs(now) builds an Entry. The tick appends it and caps the history at MAX_ENTRIES.
  3. Refit. Every 12 readings, _refit() runs learn_room_rates(), fit() and backtest().
  4. Forecast. _forecast(now) sets minutes, energy_kwh, cost, status and the fit attributes.
  5. Context. The tick sets the day type, night plan, occupancy, solar gain, window advice, the economizer phase, the heat source and the motion preset.
  6. Window actuator. _drive_window_actuator(result) opens or closes the actuator.
  7. Targets. _plan_control(now, result) sets the preset, the cool threshold, the heat target and the heat threshold.
  8. Plans. mpc_plan() and mpc_heat_plan() run when a fit and price slots exist.
  9. Heater output. PID or TPI gives the output. _heater_gate_closed(result) sets the gate.
  10. Cover and load shedding. _drive_cover() and _drive_shed().
  11. AC control. _async_control(now, result) runs with control on. It handles off from the thermostat, the lock, the manual-change pause, the stall, free cooling and the mode, then calls decide() and writes the AC. It also drives the fan, humidifier, dehumidifier, boiler and whole-house fan.
  12. Heater switch. _async_heater_step(now, result) runs in a finally block, so it runs when the AC step raises.
  13. Investigation and valve. _publish_investigation() and _drive_valve().
  14. Publish. The result dict becomes coordinator.data. The sensors and the thermostat read it.

The room thermostat calls async_refresh() after each change, so a new mode or target takes effect at once.

Watchdog#

start_watchdog() in coordinator_safety.py starts a 60-second timer at setup. It reads _last_tick_done_s, which the tick sets when it returns without an error. When no tick has finished for 16 minutes, the first check runs one tick itself, unless a tick is running. After a clock jump or a suspend that tick finishes and nothing trips. When it fails or a tick hangs, the next check turns every output off.

Free cooling states#

free_cooling_step() in features.py is the only code that changes the free cooling phase.

While the phase is gave_up the window closes, the fan stops and the AC cools.

Add a setting#

  1. Add the key and its default to const.py.
  2. Add it to _schema() and to one section of SECTIONS in config_flow.py. Add its label and its help text to translations/en.json, under config.step and with the same text under options.step. A test fails when the two differ.
  3. Read it in the coordinator through self.settings, which holds the entry's data and options, and make it change a device command or a published value.
  4. Add a row to SETTINGS in tests/ha/test_settings_effect.py with the level you expect: control or published.
  5. Add it to Settings.

test_every_setting_in_const_has_an_audit_row fails when step 4 is missing. The row's test fails when the setting changes nothing. See Guard tests.

Add a published value#

Write the key to the result dict in the mixin that computes it. Add it to STATUS_KEYS in sensor.py, or to BINARY_KEYS in binary_sensor.py when it is on or off, with its name under entity in translations/en.json. Or return it from extra_state_attributes. tests/test_published_keys.py fails when a key has neither.

Source: belikh/hvac-horizon v0.8.0