mdeweerd / zha-toolkit

🧰 Zigbee Home Assistant Toolkit - service for "rare" Zigbee operations using ZHA on Home Assistant
GNU General Public License v3.0
171 stars 17 forks source link

Fallback on on actual attribute read if `use_cache` is `True` but attribute not in cache for `attr_read`/`attr_write #194

Closed pdecat closed 10 months ago

pdecat commented 11 months ago

When attr_read/attr_write are invoked with use_cache=True, if the attribute is not in the cache, shouldn't an actual read happen to fill the cache?

Currently, the actual read only happens if use_cache is False: https://github.com/mdeweerd/zha-toolkit/blob/v0.9.7/custom_components/zha_toolkit/zcl_attr.py#L367

I'd replace the else: by an if not use_cache or not success:.

WDYT?

mdeweerd commented 11 months ago

I do not disagree as such, but one has to consider other use cases as well.

For instance, I just wait for the device to report some values as they are sleepy and actually reading the value from the device would need a lot of repetitions. Not falling back to a read ensures a fast return.

So "use_cache" could be updated to accept false, true, 0, 1, and 2 where "2" would indicate that it is ok to fallback to a normal read.

We need to be careful not to apply "tries" to the read from cache.

I am currently on vacation, so I will not be implementing this in the next few weeks, but I am open to a PR.

mdeweerd commented 10 months ago

See v0.9.8!

pdecat commented 10 months ago

Awesome, thanks @mdeweerd! :rocket: