From 62068da7770a39b8e44c0471f3b3c06dacaf64eb Mon Sep 17 00:00:00 2001 From: Michael Wikberg Date: Tue, 15 Mar 2022 20:15:04 +0200 Subject: [PATCH] Don't overwrite request_remaining with "-1" if we have a previously known state. --- .../volkswagencarnet/__init__.py | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/custom_components/volkswagencarnet/__init__.py b/custom_components/volkswagencarnet/__init__.py index e71e66e1..e9a8ad16 100755 --- a/custom_components/volkswagencarnet/__init__.py +++ b/custom_components/volkswagencarnet/__init__.py @@ -13,6 +13,7 @@ CONF_USERNAME, EVENT_HOMEASSISTANT_STOP, STATE_UNKNOWN, + STATE_UNAVAILABLE, ) from homeassistant.core import HomeAssistant, Event, callback, State from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -318,6 +319,17 @@ def async_write_ha_state(self) -> None: # Get the previous state from the state machine if found, restored data otherwise prev: Optional[State] = self.hass.states.get(self.entity_id) or self.restored_state + # This is not the best place to handle this, but... :shrug:.. + if ( + self.attribute == "requests_remaining" + and self.state == -1 + and prev is not None + and prev.state not in [STATE_UNKNOWN, STATE_UNAVAILABLE] + and int(prev.state) >= 0 + ): + _LOGGER.debug(f"Not changing requests remaining to '-1', as we have previous value '{prev.state}'") + return + # need to persist state if: # - there is no previous state # - there is no information about when the last backend update was done @@ -330,7 +342,7 @@ def async_write_ha_state(self) -> None: ): super().async_write_ha_state() else: - _LOGGER.debug(f"{self.name}: state not changed ('{prev.state}' == {self.state}), skipping update.") + _LOGGER.debug(f"{self.name}: state not changed ('{prev.state}' == '{self.state}'), skipping update.") @callback def _handle_coordinator_update(self) -> None: @@ -471,10 +483,8 @@ async def _async_update_data(self) -> list[Instrument]: if vehicle is None: raise UpdateFailed( - ( - "Failed to update WeConnect. Need to accept EULA? ", - "Try logging in to the portal: https://www.portal.volkswagen-we.com/", - ) + "Failed to update WeConnect. Need to accept EULA? " + "Try logging in to the portal: https://www.portal.volkswagen-we.com/" ) if self.entry.options.get(CONF_REPORT_REQUEST, self.entry.data.get(CONF_REPORT_REQUEST, False)): @@ -510,11 +520,9 @@ async def async_login(self) -> bool: await self.connection.doLogin(3) if not self.connection.logged_in: _LOGGER.warning( - ( - "Could not login to volkswagen WeConnect, ", - "please check your credentials and verify that ", - "the service is working", - ) + "Could not login to volkswagen WeConnect, " + "please check your credentials and verify that " + "the service is working" ) return False @@ -553,11 +561,9 @@ async def report_request(self, vehicle: Vehicle) -> None: await self.connection.doLogin() if not self.connection.logged_in: _LOGGER.warning( - ( - "Could not login to volkswagen WeConnect, please ", - "check your credentials and verify that the service ", - "is working", - ) + "Could not login to volkswagen WeConnect, please " + "check your credentials and verify that the service " + "is working" ) return