Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Add optional location based region to dwd_weather_warnings #96027
Add optional location based region to dwd_weather_warnings #96027
Changes from 10 commits
bb7019f
051edc7
327680c
f4e72b6
3135078
ee0f181
b3c5908
13e1651
8edddbe
345ee77
df914c2
f7f1ce3
848cfc5
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
What's the meaning passing
None
here?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 initializes the API with an invalid state. I chose this instead of
None
to avoid having to deal with a potentialNone
object in thesensor.py
where the data is primarily usedThere 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 fully follow. The normal pattern for coordinator based integrations is that entities only access the coordinator after a successful refresh.
An option could be to not initialize
self._api
in__init__
, instead just add a type annotation for it on class level:If there's some bug causing sensors to access the coordinator before the first successful data update, an attribute error will be raised.
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.
Updated the initialization of
self._api
in df914c2 as suggestedThere 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 think we should avoid creating a new object if the location has not changed.
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.
Do you mean the region that the API would determine using the coordinates or the coordinates directly? I'd have to create the API object to test the former.
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 meant that if the location of the device tracker has not changed, we should not create a new API object since it seems to be somewhat expensive? Or is the cost of just updating an existing API object similar to creating an API object, including the initial update?
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 initialization includes 5 API calls to get the required information based on what the object was initialized with, i.e. region ID, region name or GPS coordinates. So it's definitely more costly as the updates only do a singular API call because the required information is already there.
Is there some convenient method to verify if the location has changed instead of having to store the previous position?
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.
Just store the previous position. Maybe check if the position has changed more than 100m (or whatever is reasonable) to avoid recreating the API because of GPS inaccuracy. We have some helpers to help calculate the absolute distance between two points.
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.
Implemented in 848cfc5
After a bit of googling I chose 50m as the threshold. I've also added a normal API update in case the object doesn't get recreated as the data wouldn't update otherwise
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 this happen?
self._api
is unconditionally assigned in__init__
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.
It might not be possible anymore. I think ruff marked this line at one point but I can't check for around 3 weeks as I'm on vacation as of today.
Will check after that of course
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.
OK. I've marked the PR as draft, please mark it ready for review when you've had time to check 👍
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.
Removed it in df914c2 :)
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 the location is changed, the coordinator creates a new api object, I don't think it's valid to store a reference here, don't we need to access the api object via the coordinator instead?
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.
A reference of a complex object shares the memory with the original as far as I know so this should be fine. But using
self.coordinator._api
directly instead of a reference is probably cleaner..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.
That's absolutely not the case, as evident from this snippet:
Don't the added tests cover creating new API object when location 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.
The tests cover creating the API object initially but not a location change that would result in the API having a different city set.
Also noticed that using
self.coordinator._api
doesn't sit well with Pylint. Is renaming the_api
variable toapi
the way to go here?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.
Yeah, name it
api
if it's to be used from other classes.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.
Updated in 848cfc5
Check warning on line 34 in homeassistant/components/dwd_weather_warnings/util.py
Codecov / codecov/patch
homeassistant/components/dwd_weather_warnings/util.py#L34