Skip to content

Commit

Permalink
Don't overwrite request_remaining with "-1" if we have a previously k…
Browse files Browse the repository at this point in the history
…nown state.
  • Loading branch information
milkboy committed Mar 15, 2022
1 parent 15d27a2 commit 62068da
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions custom_components/volkswagencarnet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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)):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 62068da

Please sign in to comment.