mariusz-ostoja-swierczynski / tech-controllers

TECH Controllers integration for Home Assistant
MIT License
96 stars 67 forks source link

[Bug]: Eventual consistency on thermostat #70

Closed daroga0002 closed 6 months ago

daroga0002 commented 7 months ago

The problem

Integration have problem with consistency under thermostat. So to replicate:

  1. Setup some integration with thermostat
  2. Set temperature to 20 degree and close thermostat
  3. Reopen thermostat and it show temperature from time before setting temp from point 2
  4. After cache refresh time sometimes temperature is coming into real setup from point 2 or you need to wait for next refresh

What version of Tech Controllers integration has the issue?

v2.0.1

What version of Home Assistant Core has the issue?

doesnt matter

What was the last working version of Tech Controller integration?

No response

Diagnostics information

Log:

homeassistant  | 2024-04-02 19:23:49.021 DEBUG (MainThread) [custom_components.tech] Finished fetching tech data in 0.301 seconds (success: True)
homeassistant  | 2024-04-02 19:24:04.321 DEBUG (MainThread) [custom_components.tech.climate] Biuro: Setting temperature to 19.2
homeassistant  | 2024-04-02 19:24:04.321 DEBUG (MainThread) [custom_components.tech.tech] Setting zone constant temperature...
homeassistant  | 2024-04-02 19:24:04.321 DEBUG (MainThread) [custom_components.tech.tech] {'mode': {'id': 9038, 'parentId': 9036, 'mode': 'constantTemp', 'constTempTime': 60, 'setTemperature': 192, 'scheduleIndex': 0}}
homeassistant  | 2024-04-02 19:24:04.321 DEBUG (MainThread) [custom_components.tech.tech] Sending POST request: https://emodul.eu/api/v1/users/61726*****/modules/*****f8****************/zones
homeassistant  | 2024-04-02 19:24:04.568 DEBUG (MainThread) [custom_components.tech.tech] {'message': 'Data has been sent correctly'}

this is caused because tech API is asynchronous and during zone temp change is setting: duringChange: true

Until this value is true setTemperature configuration is not changed to temperature which was set on thermostat.

Solution which can be potencially usefull here is to after sending POST request to tech API also update local cache with requested value, additionally during receiving new update on GET requests with values check does duringChange value is true or false. If true then just dont update it in cache and hold until it will become false

Anything in the logs that might be useful for us?

No response

Additional information

No response

anarion80 commented 7 months ago

Ok, so this is difficult.

I tested this while having two windows open: one with HA dashboard with thermostat, and emodul.pl site. In such view, after changing set temperature from HA frontend thermostat, it remained as set. emodul.pl updated itself after a couple seconds. So no user-perceived issue here.

But indeed, when I opened the thermostat entity in a popup, it had the old value, and for that I needed to wait for SCAN_INTERVAL (which is 60s now) to get updated.

So clearly HA frontend itself just caches the set value for what it is displaying currently. For the other scenario, we need to wait at most 2xSCAN_INTERVAL for everything to be correct.

The problem is that we don't do any caching in the integration or API (no local cache). Everything is left to HA coordinator to just fetch data every SCAN_INTERVAL. With that, I don't think it is worth the effort to implement that local cache for this case, although I will gladly accept a PR.

daroga0002 commented 7 months ago

yup, I just noticed that during refactor local cache was removed and moved to HA coordinator 💯

The logic which I see is to avoid updating values in coordinator when duringChange: true. I will try to play with it in some free time.