Appearance
Features
Four services automate devices on the controller: the scheduler, tethering, themes and freeze protection. Each one reads its rules from the API and drives devices by publishing the same controller/<device>/state commands a user would. None of them talk to hardware directly.
Precedence
When more than one feature wants the same device, the higher one wins.
| Rank | Owner | How it holds the device |
|---|---|---|
| 1 | Freeze protection | Every other feature checks the frozen set and leaves those devices alone |
| 2 | A running theme | The scheduler skips any device a running theme drives |
| 3 | The user | The scheduler only commands at the start and end of a window, so a manual change inside a window sticks |
| 4 | A schedule | Runs when nothing above it holds the device |
Tethers sit alongside this. A tether reacts to its parent changing and never drives a device freeze protection holds. The scheduler also holds off a transition a tether would undo, and retries it on its next check.
Freeze protection and themes record themselves on their own rows, not on the devices they drive. cfp_shared.freeze.frozen_device_uuids() and cfp_shared.themes.themed_device_uuids() look up the children, and every feature calls them before sending a command.
tests/integration/ exercises this table end to end against the real API. Any change to how a feature claims or releases a device needs a test there. See Testing.
Schedules
scheduler/, rules in /schedules.
A schedule is one device, a start and end time, and the settings to apply: the state, plus a mode, temperature, light speed and brightness, or salt production where the device has them.
- The scheduler checks every 15 seconds, in the Pi's timezone from
/etc/timezone. Under the emulator it usesAmerica/New_York. - It is edge triggered. It sends one command when a window opens and one when it closes, and nothing in between.
- A window that has not opened yet does nothing. A window that closes turns the device off.
- A device held by freeze protection or a running theme is skipped. When the holder lets go, the schedule applies again on its next check.
Tethering
tether/, rules in /tethering/parents and /tethering/children.
A tether makes child devices follow a parent. The parent row names the device, and optionally a mode it has to be in. Each child row says what the child does when the parent reaches a given state.
| Child field | Meaning |
|---|---|
parent_device_setting | The parent state this row applies to, such as active |
state_setting | What the child is set to |
mode_setting, temperature_setting, and the rest | Settings sent with it |
Parent on and parent off are separate child rows. A parent state with no matching row leaves the child alone.
The subscribers publish each device change to controller/tether, with the device uuid, its state and, for a pump, the mode it is heading for. The tether service matches that against the rules and, for each matching child, starts a loop that resends the child's command every 5 seconds until the rule changes. Because the command is resent, a tether holds its child.
Themes
themer/, rules in /themes/parents and /themes/children.
A theme is a named set of devices with settings, run for a duration. Start one by publishing { "uuid": "<theme uuid>" } to controller/themes/start, and stop it with controller/themes/stop.
- Starting a theme stops any theme already running.
- The start time is saved with
PUT /themes/parents/{uuid}/started, so a running theme picks back up after the themer restarts. - When a theme ends, its children turn off and any schedule for them applies again.
- A child held by freeze protection is left alone.
- The running theme is published on
controller/themes/status.
Freeze protection
freeze-protection/, rules in /freeze-protection/parents and /freeze-protection/children.
A freeze protection rule is a thermistor, a temperature threshold, and the devices to run while the reading is below it.
- Every 10 seconds the service reads each rule's thermistor. Below
freeze_protect_temp, the thermistor is markedfreeze, and above it,nominal. - When any rule is freezing, every child of every freezing rule is turned on with its settings.
- Every 3 minutes it rereads the rules, starting children that were added or changed and stopping ones that were removed. A child with
pool_cycle,spa_cycleorspillover_cycleset moves its valves to the next mode in that cycle, so water runs through every line. - While protection runs, the thermistors are checked every 60 seconds. Once every one reads above its threshold, all children turn off. Nothing is resumed.
Status is published on controller/freeze-protection/status.
A rule can be paused while it is freezing by publishing { "uuid": "<freeze parent uuid>" } to controller/freeze/pause. Its children turn off and the thermistor is marked freeze#paused. controller/freeze/resume starts them again. A pause only lasts while that thermistor stays below its threshold, so it never carries over to the next freeze.
Associated devices
Some devices act on others directly, outside the four services.
| Device | Behaviour |
|---|---|
| Pump with valves | Pool, spa and spillover modes move the pump's suction and pressure valves |
Valve with mirror_turn_off | Closes when its associated pump turns off |
| Heater | Holds its associated pump at a minimum speed while it cools down |
| Salt cell | Starts its associated pump if it is stopped, and turns it off once when the cell turns off, only if the cell started it |
Commands a device sends to another carry a sender, such as pump, so the receiver can tell them from a user's command. See MQTT API.
Adding a feature
- Read and write state only through the API, and command devices only through their
statetopics. - Before commanding a device, check
frozen_device_uuids(), andthemed_device_uuids()if the feature ranks below themes. - Record what the feature holds on its own rows, not on the devices, as freeze protection and themes do.
- Add the service to
docker-compose.yaml,docker-compose.dev.yaml,config.prod.json,config.dev.jsonanddocker-bake.hcl, and add it toCOMMAND_SOURCESindocker-updater.pyso updates stop it first and start it last. - Add integration tests for how it ranks against every existing feature.