Keep models of rooms removed in Options
Saving wrote only the currently configured rooms, so removing a room in Configure (even by accident) discarded its learned model at the next save, and adding it back started from scratch. Keep validated models of removed rooms (at most 24, oldest dropped first) in an optional retired_learners payload key and restore them when the room is added back. Payload schema_version stays 2: the key is additive and older 0.2.x readers ignore it. Replacing the relay still starts a fresh model, as before. Status lists retired_room_models. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
955c679f5e
commit
c0e4de832b
3 files changed
+72
-7
No files matched your search
@@ -182,3 +182,26 @@ async def test_relay_listener_integrates_changes_between_polls(hass, entry, inpu
|
||||
hass.states.async_set("switch.boiler", "on")
|
||||
await hass.async_block_till_done()
|
||||
assert not coordinator._relay_events
|
||||
|
||||
|
||||
async def test_removed_room_model_survives_options_round_trip(hass, entry, inputs, hass_storage, freezer):
|
||||
from custom_components.heating_mpc.physics import Learner
|
||||
key = f"{DOMAIN}.{entry.entry_id}"
|
||||
hass_storage[key] = {"version": 1, "data": {
|
||||
"schema_version": 2, "heater_entity": "switch.boiler",
|
||||
"learners": {"sensor.room": Learner(count=250, on_count=125, off_count=125).as_dict()}}}
|
||||
hass.states.async_set("sensor.new", "21", {"unit_of_measurement": "°C"})
|
||||
await setup(hass, entry)
|
||||
hass.config_entries.async_update_entry(entry, options={"room_entities": ["sensor.new"]})
|
||||
await hass.async_block_till_done()
|
||||
future = dt_util.utcnow() + timedelta(minutes=5, seconds=1)
|
||||
freezer.move_to(future)
|
||||
async_fire_time_changed(hass, future)
|
||||
await hass.async_block_till_done()
|
||||
saved = hass_storage[key]["data"]
|
||||
assert set(saved["learners"]) == {"sensor.new"}
|
||||
assert saved["retired_learners"]["sensor.room"]["count"] == 250
|
||||
hass.config_entries.async_update_entry(
|
||||
entry, options={"room_entities": ["sensor.room", "sensor.new"]})
|
||||
await hass.async_block_till_done()
|
||||
assert hass.data[DOMAIN][entry.entry_id].learners["sensor.room"].count == 250
|
||||
Reference in new issue
Block a user