-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
Waze travel time last updated fix #146803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Waze travel time last updated fix #146803
Conversation
When the Waze Travel Time sensor fails to fetch new data, it currently reverts to 'unknown'. This change retains the last known value instead, as it still provides a coherent estimation when combined with the sensor's last_updated attribute.
Hey there @eifinger, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
Reverted commit 15a3a9d This change prevented the coordinator from retrying to fetch data on error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this big investment and huge amount of work. There is currently a lot going on and its easier to review and approve when we split this up. Here is my suggestion for a list of PRs:
- Make
async_get_travel_times
return[]
instead of None - Remove
_LOGGER.warning
statements insideasync_get_travel_times
and catch the error conditions where the function is used - Introduce a coordinator
|
||
SCAN_INTERVAL = timedelta(minutes=5) | ||
|
||
PARALLEL_UPDATES = 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
destination_coordinates = find_coordinates(self.hass, self._destination) | ||
|
||
if ( | ||
self.config_entry is None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How could it happen that config_entry
is None?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't remember why I've put that there, but you're right, it seems pointless. I'll remove it.
route=route.name, | ||
) | ||
|
||
await asyncio.sleep(SECONDS_BETWEEN_API_CALLS) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has to happen before releasing the semaphore on line 134
SECONDS_BETWEEN_API_CALLS = 0.5 | ||
|
||
|
||
class WazeTravelTimeData(TypedDict): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why a TypedDict instead of a dataclass?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took HERE as inspiration (which was using TypedDicts), but I'll try to restore the dataclass in the new PRs to avoid unnecessary changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember again why a TypedDict is better. This way you can use the key
attribute in SensorDescrptions and you don't have to write custom code for native_value
etc:
core/homeassistant/components/here_travel_time/sensor.py
Lines 53 to 61 in c7b2f23
SensorEntityDescription( | |
translation_key="duration", | |
icon=ICONS.get(travel_mode, ICON_CAR), | |
key=ATTR_DURATION, | |
state_class=SensorStateClass.MEASUREMENT, | |
device_class=SensorDeviceClass.DURATION, | |
native_unit_of_measurement=UnitOfTime.SECONDS, | |
suggested_unit_of_measurement=UnitOfTime.MINUTES, | |
), |
@callback | ||
def _handle_coordinator_update(self) -> None: | ||
"""Handle updated data from the coordinator.""" | ||
if self.coordinator.data["duration"] is not None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if self.coordinator.data["duration"] is not None: | |
if self.coordinator.data is not None: |
The coordinator automatically handles failed updates. We do not need to set a custom object with none values. Or is this the only way to achieve your desired behavior? If yes I have to take a closer look
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had put this as I was returning a WazeTravelTimeData object filled with None at line 89 of the coordinator, but since you suggested to raise UpdateFailed instead, we can remove that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can self.coordinator.data even be None here ?
The coordinator calls this function only after successfully fetching data.
Here is more info
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The BaseCoordinatorEntity doesn't check this at all:
core/homeassistant/helpers/update_coordinator.py
Lines 558 to 561 in c7b2f23
@callback | |
def _handle_coordinator_update(self) -> None: | |
"""Handle updated data from the coordinator.""" | |
self.async_write_ha_state() |
Used in combination with
core/homeassistant/helpers/update_coordinator.py
Lines 590 to 592 in c7b2f23
def available(self) -> bool: | |
"""Return if entity is available.""" | |
return self.coordinator.last_update_success |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will however not do what you want to do, as in instead of becoming "unavailable" continue to show the latest update/state.
When you open your PR we should ask the core team if your wanted behavior would be accepted from a UX point of view.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The BaseCoordinatorEntity doesn't check this at all:
Thanks for the explanation, it's clearer for me now 👍
When you open your PR we should ask the core team if your wanted behavior would be accepted from a UX point of view.
Of course ! But in my opinion this is the behavior that makes the most sense for a travel time integration, so it shouldn't be too hard to convince them.
Thanks for all your suggestions! |
I think I'll make the following PRs :
I will start tomorrow |
I am missing the changes to |
Proposed change
Previously, when Waze Travel Time sensor was failing to update (often because of a 503 error), its state was left untouched, but its last_updated value was refreshed.
However, to calculate an ETA in an automation, the sensor value and the time at which it was updated needs to be summed up.
So, with the previous behavior, the ETA was slowly getting off at each failed update.
Now, with the added coordinator, the sensor is left untouched when an update fails, to avoid propagating a wrong last_updated timestamp.
#146798
Type of change
Additional information
Checklist
ruff format homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
.To help with the load of incoming pull requests: