Skip to content

Commit

Permalink
Fix updating
Browse files Browse the repository at this point in the history
Increase responsiveness of most changes and prevent integration from going unavailable after token refresh
  • Loading branch information
apetrycki committed Apr 17, 2024
1 parent 023cf91 commit 76593bb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
3 changes: 2 additions & 1 deletion custom_components/daikinskyport/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
"""Update listener."""
_LOGGER.debug("Update listener: %s", str(entry))
await hass.config_entries.async_reload(entry.entry_id)
# await hass.config_entries.async_reload(entry.entry_id)


class DaikinSkyportData:
Expand Down Expand Up @@ -164,6 +164,7 @@ async def _async_update_data(self):
"""Update data via library."""
try:
current = await self.hass.async_add_executor_job(self.daikinskyport.update)
_LOGGER.debug("Daikin Skyport _async_update_data")
except ExpiredTokenError:
_LOGGER.debug("Daikin Skyport tokens expired")
await self.async_refresh()
Expand Down
21 changes: 20 additions & 1 deletion custom_components/daikinskyport/daikinskyport.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def __init__(self, config_filename=None, user_email=None, user_password=None, co
self.thermostats = list()
self.thermostatlist = list()
self.authenticated = False
self.skip_next = False

if config is None:
self.file_based_config = True
Expand Down Expand Up @@ -269,11 +270,15 @@ def write_tokens_to_file(self):

def update(self):
''' Get new thermostat data from daikin skyport '''
sleep(3)
if self.skip_next:
logger.debug("Skipping update due to setting change")
self.skip_next = False
return
result = self.get_thermostats()
return result

def make_request(self, index, body, log_msg_action, *, retry_count=0):
self.skip_next = True
deviceID = self.thermostats[index]['id']
url = 'https://api.daikinskyport.com/deviceData/' + deviceID
header = {'Content-Type': 'application/json;charset=UTF-8',
Expand Down Expand Up @@ -307,6 +312,7 @@ def set_hvac_mode(self, index, hvac_mode):
''' possible modes are DAIKIN_HVAC_MODE_{OFF,HEAT,COOL,AUTO,AUXHEAT} '''
body = {"mode": hvac_mode}
log_msg_action = "set HVAC mode"
self.thermostats[index]["mode"] = hvac_mode
return self.make_request(index, body, log_msg_action)

def set_thermostat_schedule(self, index, prefix, start, enable, label, heating, cooling):
Expand All @@ -331,12 +337,14 @@ def set_fan_mode(self, index, fan_mode):
''' Set fan mode. Values: auto (0), schedule (2), on (1) '''
body = {"fanCirculate": fan_mode}
log_msg_action = "set fan mode"
self.thermostats[index]["fanCirculate"] = fan_mode
return self.make_request(index, body, log_msg_action)

def set_fan_speed(self, index, fan_speed):
''' Set fan speed. Values: low (0), medium (1), high (2) '''
body = {"fanCirculateSpeed": fan_speed}
log_msg_action = "set fan speed"
self.thermostats[index]["fanCirculateSpeed"] = fan_speed
return self.make_request(index, body, log_msg_action)

def set_fan_clean(self, index, active):
Expand Down Expand Up @@ -368,6 +376,10 @@ def set_temp_hold(self, index, cool_temp=None, heat_temp=None,
"schedOverrideDuration": hold_duration
}
log_msg_action = "set hold temp"
self.thermostats[index]["hspHome"] = round(heat_temp, 1)
self.thermostats[index]["cspHome"] = round(cool_temp, 1)
self.thermostats[index]["schedOverride"] = 1
self.thermostats[index]["schedOverrideDuration"] = hold_duration
return self.make_request(index, body, log_msg_action)

def set_permanent_hold(self, index, cool_temp=None, heat_temp=None):
Expand All @@ -384,6 +396,10 @@ def set_permanent_hold(self, index, cool_temp=None, heat_temp=None):
"schedEnabled": False
}
log_msg_action = "set permanent hold"
self.thermostats[index]["hspHome"] = round(heat_temp, 1)
self.thermostats[index]["cspHome"] = round(cool_temp, 1)
self.thermostats[index]["schedOverride"] = 0
self.thermostats[index]["schedEnabled"] = False
return self.make_request(index, body, log_msg_action)

def set_away(self, index, mode, heat_temp=None, cool_temp=None):
Expand All @@ -398,6 +414,9 @@ def set_away(self, index, mode, heat_temp=None, cool_temp=None):
}

log_msg_action = "set away mode"
self.thermostats[index]["geofencingAway"] = mode
self.thermostats[index]["hspAway"] = heat_temp
self.thermostats[index]["cspAway"] = cool_temp
return self.make_request(index, body, log_msg_action)

def resume_program(self, index):
Expand Down
5 changes: 3 additions & 2 deletions custom_components/daikinskyport/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def turn_on(self, **kwargs: Any) -> None:
send_command = self.data.daikinskyport.set_hvac_mode(self._index, DAIKIN_HVAC_MODE_AUXHEAT)
if send_command:
self.aux_on = True
# self.async_write_ha_state()
self.async_write_ha_state()
else:
raise HomeAssistantError(f"Error {send_command}: Failed to turn on {self._name}")

Expand All @@ -67,10 +67,11 @@ def turn_off(self, **kwargs: Any) -> None:
if self.data.daikinskyport.get_thermostat(self._index)['mode'] == DAIKIN_HVAC_MODE_AUXHEAT:
self.data.daikinskyport.set_hvac_mode(self._index, DAIKIN_HVAC_MODE_HEAT)
self.aux_on = False
# self.async_write_ha_state()
self.async_write_ha_state()

async def async_update(self) -> None:
"""Get the latest state of the switch."""
_LOGGER.debug("Updating switch entity")
await self.data._async_update_data()
thermostat = self.data.daikinskyport.get_thermostat(self._index)
if thermostat['mode'] == DAIKIN_HVAC_MODE_AUXHEAT:
Expand Down

0 comments on commit 76593bb

Please sign in to comment.