Skip to content

Commit

Permalink
fix optimistic state updates (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
zlangbert authored Jan 22, 2024
1 parent b96b99e commit 3718cc4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
26 changes: 11 additions & 15 deletions custom_components/daikinone/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,13 @@ async def async_update(self, no_throttle: bool = False) -> None:
await self._data.update(no_throttle=no_throttle)
self._thermostat = self._data.daikin.get_thermostat(self._thermostat.id)

# update entity data
#
self.update_entity_attributes()

def update_entity_attributes(self) -> None:
self._attr_available = self._thermostat.online
self._attr_current_temperature = self._thermostat.indoor_temperature.celsius
self._attr_current_humidity = self._thermostat.indoor_humidity

# hvac mode
#
match self._thermostat.mode:
case DaikinThermostatMode.AUTO:
self._attr_hvac_mode = HVACMode.HEAT_COOL
Expand All @@ -239,8 +238,7 @@ async def async_update(self, no_throttle: bool = False) -> None:
case DaikinThermostatMode.OFF:
self._attr_hvac_mode = HVACMode.OFF

# hvac action
#
# hvac current action
match self._thermostat.status:
case DaikinThermostatStatus.HEATING:
self._attr_hvac_action = HVACAction.HEATING
Expand All @@ -254,27 +252,24 @@ async def async_update(self, no_throttle: bool = False) -> None:
self._attr_hvac_action = HVACAction.IDLE

# target temperature
#

# reset target temperature attributes first, single target temp takes precedence and can conflict with range
self._attr_target_temperature = None
self._attr_target_temperature_low = None
self._attr_target_temperature_high = None

match self._thermostat.mode:
case DaikinThermostatMode.HEAT | DaikinThermostatMode.AUX_HEAT:
self._attr_target_temperature = self._thermostat.set_point_heat.celsius
case DaikinThermostatMode.COOL:
self._attr_target_temperature = self._thermostat.set_point_cool.celsius
case _:
pass

# target temperature range
#
match self._thermostat.mode:
case DaikinThermostatMode.AUTO:
self._attr_target_temperature_low = self._thermostat.set_point_heat.celsius
self._attr_target_temperature_high = self._thermostat.set_point_cool.celsius
case _:
pass

# temperature bounds
#

# these should be the same but just in case, take the larger of the two for the min
self._attr_min_temp = max(
self._thermostat.set_point_heat_min.celsius,
Expand All @@ -299,6 +294,7 @@ async def update_state_optimistically(

# execute state update optimistically
update(self._thermostat)
self.update_entity_attributes()
self.async_write_ha_state()

# wait for remote state to be updated
Expand Down
11 changes: 6 additions & 5 deletions custom_components/daikinone/daikinone.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import json
import logging
from datetime import timedelta
from enum import Enum, auto
Expand Down Expand Up @@ -209,7 +210,7 @@ async def set_thermostat_home_set_points(
if cool:
payload["cspHome"] = cool.celsius
if override_schedule:
payload["schedOverride"] = True
payload["schedOverride"] = 1

await self.__req(
url=f"{DAIKIN_API_URL_DEVICE_DATA}/{thermostat_id}",
Expand Down Expand Up @@ -468,7 +469,7 @@ async def __req(
await self.__refresh_token()
return await self.__req(url, method, body, retry=False)

raise DaikinServiceException(
f"Failed to send request to Daikin API: method={method} url={url} body={body}, response={response}",
status=response.status,
)
raise DaikinServiceException(
f"Failed to send request to Daikin API: method={method} url={url} body={json.dumps(body)}, response_code={response.status} response_body={await response.text()}",
status=response.status,
)

0 comments on commit 3718cc4

Please sign in to comment.