Add input_boolean for water heater preparation logic

Refreshed logic to allow users to toggle hot water preparation. Introduced new `input_boolean.boiler_prepare_hot_water` and updated config, services, and tests to ensure water readiness only when required.
This commit is contained in:
Marius
2026-03-16 11:42:49 +02:00
parent 5cc0ab29a6
commit 53eb53539a
13 changed files with 434 additions and 39 deletions
@@ -0,0 +1,130 @@
---
name: Water Heater Preparation Decision
overview: Add a new input_boolean so the user can decide whether to prepare hot water. When ON, the system ensures water is ready by the deadline using cheap intervals. When OFF, preparation is skipped and the boiler is turned off if running.
todos:
- id: add-ha-entity
content: Add input_boolean.boiler_prepare_hot_water to Home Assistant (user config) and regenerate entities
- id: extend-boiler-service
content: Add prepareHotWater parameter to IBoilerControlService and BoilerControlService
- id: update-nordpool-init
content: Read new input_boolean in NordPoolInit and pass to BoilerControlService
- id: update-entity-context
content: Add prepareHotWater to EntityUpdateContext and NordPoolEntityService
- id: unit-tests
content: Add BoilerControlService tests for prepareHotWater false/true
isProject: false
---
# Water Heater Preparation Decision
## Goal
Add a new `input_boolean.boiler_prepare_hot_water` so the user can decide whether to prepare hot water. When **ON**: ensure water is ready by the deadline (current behavior). When **OFF**: skip preparation entirely and turn off the boiler if it is running.
## Current Flow
```mermaid
flowchart LR
subgraph config [Config]
DailyHours[BoilerDailyHours]
PriceCap[BoilerPriceCap]
ReadyBy[BoilerReadyBy]
end
subgraph logic [Logic]
Cheapest[CheapestIntervalsFinder]
BoilerControl[BoilerControlService]
end
subgraph decision [Decision]
isCheap[isNowCheap]
waterReady[waterReady]
end
subgraph action [Action]
TurnOn[Turn On]
TurnOff[Turn Off]
end
DailyHours --> Cheapest
PriceCap --> Cheapest
ReadyBy --> Cheapest
Cheapest --> isCheap
isCheap --> BoilerControl
waterReady --> BoilerControl
BoilerControl --> TurnOn
BoilerControl --> TurnOff
```
## Proposed Changes
### 1. New Home Assistant Entity
**User action:** Add to `configuration.yaml` or via UI:
```yaml
input_boolean:
boiler_prepare_hot_water:
name: "Prepare hot water"
icon: mdi:water-thermometer
```
Then run `dotnet tool run nd-codegen` to regenerate [HomeAssistant.Entities/EntityGeneration/HomeAssistantGenerated.cs](HomeAssistant.Entities/EntityGeneration/HomeAssistantGenerated.cs) so the new entity is available in code.
### 2. Add Prepare Hot Water to NordPoolInit
**File:** [NordPool.Manager/NordPoolInit.cs](NordPool.Manager/NordPoolInit.cs)
- Inject `InputBooleanEntity BoilerPrepareHotWaterEntity` from `entities.InputBoolean.BoilerPrepareHotWater` (after codegen)
- Read `BoilerPrepareHotWaterEntity.State` in `UpdateEntitiesAsync` before calling `BoilerControlService`
- Parse as boolean: `"on"` = true, `"off"` or null = false. For backward compatibility when entity does not exist yet, treat null/unknown as true
- Pass `prepareHotWater` to `DetermineAction`
### 3. Extend BoilerControlService and IBoilerControlService
**Files:** [NordPool.Manager/Interface/IBoilerControlService.cs](NordPool.Manager/Interface/IBoilerControlService.cs), [NordPool.Manager/BoilerControlService.cs](NordPool.Manager/BoilerControlService.cs)
- Add `bool prepareHotWater` as first parameter to `DetermineAction`
- When `prepareHotWater == false`: return `BoilerAction.TurnOff` if boiler is on, else `NoChange`
- When `prepareHotWater == true`: keep current logic (cheap + not water ready)
**Interface change:**
```csharp
BoilerAction DetermineAction(bool prepareHotWater, bool isNowCheap, bool waterReady, string? currentSwitchState);
```
### 4. Update EntityUpdateContext
**File:** [NordPool.Manager/Models/EntityUpdateContext.cs](NordPool.Manager/Models/EntityUpdateContext.cs)
- Add `bool PrepareHotWater { get; init; }` for UI/attributes
### 5. Update NordPoolEntityService
**File:** [NordPool.Manager/NordPoolEntityService.cs](NordPool.Manager/NordPoolEntityService.cs)
- Add `prepare_hot_water` to `sensor.nordpool_cheapest_intervals` attributes so the user can see the decision state in HA
### 6. Unit Tests
**File:** [NordPool.Manager.Tests/BoilerControlServiceTests.cs](NordPool.Manager.Tests/BoilerControlServiceTests.cs)
- Add tests for `prepareHotWater == false`: always TurnOff when on, NoChange when off
- Add tests for `prepareHotWater == true`: existing behavior unchanged
## Logic Summary
| Prepare Hot Water | isNowCheap | waterReady | Switch State | Action |
|------------------|------------|------------|--------------|--------|
| OFF | any | any | on | TurnOff |
| OFF | any | any | off | NoChange |
| ON | true | false | off | TurnOn |
| ON | true | false | on | NoChange |
| ON | false | any | on | TurnOff |
| ON | any | true | on | TurnOff |
## Backward Compatibility
- If `BoilerPrepareHotWaterEntity` does not exist yet (before HA config): use a fallback or skip the entity read
- If state is null or unknown: treat as `true` (prepare hot water) to preserve current behavior until the user adds the entity