Closed pdcastro closed 4 months ago
The proposed solution has a corner-case limitation involving bump charge. During the 5-minute API polling interval, or while Home Assistant is down for any reason (upgrade, server maintenance...), it is in theory possible for the planned dispatch source
attribute to change e.g. from smart-charge
to bump-charge
to None
again, if the user initiates a bump charge during that period (or the opposite change, if the user ends a bump charge during that period). In this case, the integration could assume the wrong value for source
, and in principle it could take several hours until the Octopus API produced a non-None source
value again. During those hours, issues #35 and #36 would remain unsolved. This is a fundamental limitation of the approach of assuming the last seen non-None source
value while the Octopus API provides a None
value.
I believe that most users will never come across that corner case, even if they regularly use bump charge (which I suspect is also rare). This PR thus offers an effective workaround until Octopus fixes their API to ensure that the source
attribute is never None
.
This PR proposes a fix for issues #35 and #36. As @matt7aylor had pointed out, sometimes the
source
attribute in the Octopus API planned dispatch list gets changed from"source": "smart-charge"
to"source": None
. The proposed solution / workaround is to remember the last seen non-None source and use it whensource
is None. For example:Initially,
'source': 'smart-charge'
:Then it goes rogue,
'source': None
:The proposed solution replaces
'source': None
with'source': 'smart-charge'
(in this particular example) before passing it on to the integration sensors etc.The proposed solution also introduces a
PersistentData
class that uses the Home Assistanthelpers.storage.Store
class to persist the last seen non-Nonesource
to disk when Home Assistant is stopped / restarted, and restores it when the integration is loaded again.It is worth noting that initially I tried something else. I looked at the Octopus GraphQL schema for plannedDispatches and noticed that they document two
source
attributes, one in themeta
inner object and one in the outer scope:Octopus API query documentation:
Octopus API response documentation:
So I tried modifying the query in
graphql_client.py
__async_get_combined_state() to include bothsource
attributes, in the hope that when one is None, the other isn’t. Alas, I observed that the newly added outersource
attribute was alwaysNone
, regardless of the whether themeta.source
attribute wasNone
or not. Example: