fix(climate): write mode-correct setpoints and stop REST reverting live state
Validate / HACS validation (pull_request) Skipped
Validate / Hassfest validation (pull_request) Skipped

Two independent bugs made a second change (e.g. adjusting the target
temperature right after switching cool -> heat) fail to take, and made
Home Assistant settle back on a previous state while DKN Cloud NA showed
the correct one.

Wrong setpoint key on write:

_writable_temperature_property_for_mode() ignored its hvac_mode argument
and resolved the key from real_mode/mode in the cached device payload.
That payload still describes the pre-change state, so a heat setpoint was
emitted as setpoint_air_cool: the unit switched to heat but the heat
setpoint never moved. The same defect broke AUTO in steady state, where
real_mode=cool made the write target setpoint_air_cool while the read
used setpoint_air_auto, so the value could never appear to change.

target_temperature() and writable_target_temperature_key() now accept the
mode the caller intends, and that mode takes priority over anything in the
device payload. Device-reported modes remain a fallback only for payloads
that do not expose the requested mode's setpoint.

async_set_temperature() no longer re-sends power+mode; async_set_hvac_mode()
already sent them, and the stale-cache condition guarding the resend was
always true. It now waits (bounded) for the cloud to echo the mode before
sending the setpoint, matching the ensure_mode ordering the smoke test uses.

REST snapshot reverting live state:

The coordinator merged {**existing, **device}, letting the lagging REST
/installations snapshot win over live Socket.IO pushes. Once the socket
confirmed a write, _reconcile_optimistic dropped the overlay, leaving
nothing to stop the next poll from reverting the state.

Socket-pushed values are now tracked per device and applied after the REST
payload. They are discarded when the socket session changes, since updates
may have been missed while it was down and REST becomes authoritative again.

Also report hvac_action and target_temperature against the effective
(optimistic-aware) mode so the action cannot contradict the reported mode
while a change propagates.

Co-authored-by: anthropic/claude-opus-5
This commit is contained in:
2026-09-19 09:48:10 -03:00
co-authored by anthropic/claude-opus-5
parent 41a05a1a69
commit 3929546ea5
7 changed files with 145 additions and 42 deletions
+35 -29
View File
@@ -133,10 +133,16 @@ class DknClimateEntity(DknEntity, ClimateEntity):
@property
def hvac_action(self) -> HVACAction | None:
if self.hvac_mode == HVACMode.OFF:
mode = self.hvac_mode
if mode == HVACMode.OFF:
return HVACAction.OFF
action = inferred_hvac_action(self._device_data)
action = inferred_hvac_action(
self._device_data,
mode=_HVAC_TO_MODE.get(mode),
target=self.target_temperature,
power=True,
)
if action == "heating":
return HVACAction.HEATING
if action == "cooling":
@@ -158,7 +164,12 @@ class DknClimateEntity(DknEntity, ClimateEntity):
mode = self.hvac_mode
if mode in _NO_TARGET_TEMP_MODES:
return None
fallback = model_target_temperature(self._device_data)
# Read the setpoint for the effective mode so the value shown matches
# the key a write would target, even while a mode change is still
# propagating through the cloud.
fallback = model_target_temperature(self._device_data, _HVAC_TO_MODE.get(mode))
if fallback is None:
fallback = model_target_temperature(self._device_data)
if fallback is None:
return None
return self._optimistic_get("target_temp", fallback)
@@ -239,31 +250,22 @@ class DknClimateEntity(DknEntity, ClimateEntity):
)
installation_id = self._installation_id
property_name = self._writable_temperature_property_for_mode(target_mode)
device_temp = self._to_device_temperature(float(temperature))
requested_mode_code = _HVAC_TO_MODE.get(target_mode)
async with self._get_device_lock():
# A mode change made moments ago (either above, or by a separate
# set_hvac_mode call from the UI) may not have landed in the cloud
# yet. Give it a bounded window to be echoed back before writing the
# setpoint, so the setpoint is not applied against the old mode.
if (
requested_mode_code is not None
and self._device_data.get("mode") != requested_mode_code
):
await self._async_wait_for_device_value("mode", requested_mode_code)
property_name = self._writable_temperature_property_for_mode(target_mode)
device_temp = self._to_device_temperature(float(temperature))
try:
requested_mode_code = _HVAC_TO_MODE.get(target_mode)
if (
requested_mode_code is not None
and self._device_data.get("mode") != requested_mode_code
):
await self.coordinator.client.async_send_machine_event(
installation_id, self._command_mac, "power", True
)
await self.coordinator.client.async_send_machine_event(
installation_id, self._command_mac, "mode", requested_mode_code
)
self._optimistic_set(
"power", True, device_key="power", device_value=True
)
self._optimistic_set(
"hvac_mode",
target_mode,
device_key="mode",
device_value=requested_mode_code,
)
await self.coordinator.client.async_send_machine_event(
installation_id, self._command_mac, property_name, device_temp
)
@@ -354,10 +356,14 @@ class DknClimateEntity(DknEntity, ClimateEntity):
return key
def _writable_temperature_property_for_mode(self, hvac_mode: HVACMode) -> str:
preferred = writable_target_temperature_key(self._device_data)
if preferred is not None:
return preferred
return self._temperature_property_for_mode(hvac_mode)
# The key must follow the mode being requested, not whatever mode the
# cached device payload still reports; that payload lags behind a mode
# change and would send e.g. a heat setpoint to setpoint_air_cool.
requested = self._temperature_property_for_mode(hvac_mode)
preferred = writable_target_temperature_key(
self._device_data, _HVAC_TO_MODE.get(hvac_mode)
)
return preferred or requested
def _to_device_temperature(self, temperature_c: float) -> float | int:
return to_device_temperature(temperature_c, self._device_data.get("units"))