Introduce thermal zones as config subentries

Each temperature sensor used to be its own "room", so a room with two
sensors counted twice and comfort importance was tied to sensors.

zones.py adds Zone: a name, one or more sensors with sensor weights, a
comfort weight, an optional target offset and adjacent zones (made
symmetric). Sensor weights only combine readings; comfort weights only
steer the MPC; neither touches the learned thermal coefficients.

Zones are config subentries (HA's native "Add zone" / edit / delete on
the integration page), with validation that a sensor belongs to one
zone and is not an outdoor source, and a sensor-weight step for
multi-sensor zones. Initial setup still asks for room sensors and
weights and creates one zone per sensor. Configure no longer asks for
rooms or weights.

Config entry version 3. Migrating from 2 turns each room sensor into a
one-sensor zone with its comfort weight and the sensor's entity ID as
the zone key, so learned models, forecast entities (now linked to their
zone, so deleting a zone removes them) and accuracy statistics carry
over. Older versions cannot load a version 3 entry; rolling back needs
the HA backup.

A zone learns only across polls that used the same sensors (a sensor
dropping out shifts the mean by its offset); the new poll outcome
"sensors_changed" reports this. Status renames: input_quality.zones
(per-sensor detail nested), excluded_zones, retired_zone_models; adds
zone_names. Invalid stored comfort weights now fall back to 1.0, so the
invalid_weights status is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
buzeraandClaude Opus 5.5 committed 2026-10-27 02:32:11 +02:00
1 parent 220ac570d2
commit 46587af8a3
15 files changed
+867 -155

No files matched your search

+15 -4
View File
@@ -15,9 +15,18 @@ async def async_setup_entry(hass, entry, async_add_entities) -> None:
coordinator = hass.data[DOMAIN][entry.entry_id]
entities = [MPCStatus(coordinator, entry.entry_id), MPCWeightedTemp(coordinator, entry.entry_id), MPCOutdoorTemperature(coordinator, entry.entry_id),
MPCLearningProgress(coordinator, entry.entry_id), MPCLastSample(coordinator, entry.entry_id)]
entities += [MPCRoomPrediction(coordinator, entry.entry_id, room) for room in coordinator.rooms]
entities += [MPCForecastError(coordinator, entry.entry_id, horizon) for horizon in HORIZONS]
# Zone forecasts belong to their zone subentry, so deleting a zone removes them.
linked = {}
for zone in coordinator.zones.values():
entity = MPCRoomPrediction(coordinator, entry.entry_id, zone.key)
if zone.subentry_id:
linked.setdefault(zone.subentry_id, []).append(entity)
else:
entities.append(entity)
async_add_entities(entities)
for subentry_id, zone_entities in linked.items():
async_add_entities(zone_entities, config_subentry_id=subentry_id)
class MPCStatus(CoordinatorEntity, SensorEntity):
@@ -69,7 +78,7 @@ class MPCRoomPrediction(CoordinatorEntity, SensorEntity):
super().__init__(coordinator)
self.room = room
self._attr_unique_id = f"{entry_id}_{room}_predicted_60min"
self._attr_name = f"MPC {room} 60min Prediction"
self._attr_name = f"MPC {coordinator.zones[room].name} 60min Prediction"
# Change every poll; the 60-minute value itself is recorded.
_unrecorded_attributes = frozenset({"forecast_15min", "plan_15min"})
@@ -120,7 +129,7 @@ class MPCForecastError(CoordinatorEntity, SensorEntity):
_attr_state_class = SensorStateClass.MEASUREMENT
_attr_suggested_display_precision = 2
_attr_icon = "mdi:target"
_unrecorded_attributes = frozenset({"rooms"})
_unrecorded_attributes = frozenset({"zones"})
def __init__(self, coordinator, entry_id, horizon):
super().__init__(coordinator)
@@ -144,7 +153,9 @@ class MPCForecastError(CoordinatorEntity, SensorEntity):
"bias": accuracy.get("bias"),
"persistence_mae": accuracy.get("persistence_mae"),
"skill": accuracy.get("skill"),
"rooms": accuracy.get("rooms", {}),
# Per zone, by name.
"zones": {self.coordinator.zones[key].name if key in self.coordinator.zones else key: stats
for key, stats in accuracy.get("rooms", {}).items()},
"window_days": WINDOW_DAYS,
"unit_of_attributes": "°C",
"basis": ("thermal model replayed with the actual relay history; outdoor "