-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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 work area switch for Husqvarna Automower #126376
Add work area switch for Husqvarna Automower #126376
Conversation
class WorkAreaControlEntity(AutomowerControlEntity): | ||
"""Replies available when the mower is connected.""" | ||
|
||
def __init__( | ||
self, | ||
mower_id: str, | ||
coordinator: AutomowerDataUpdateCoordinator, | ||
work_area_id: int, | ||
) -> None: | ||
"""Initialize AutomowerEntity.""" | ||
super().__init__(mower_id, coordinator) | ||
self.work_area_id = work_area_id | ||
|
||
@property | ||
def work_area_attributes(self) -> WorkArea: | ||
"""Get the mower attributes of the current mower.""" | ||
if TYPE_CHECKING: | ||
assert self.mower_attributes.work_areas is not None | ||
return self.mower_attributes.work_areas[self.work_area_id] |
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.
Also wondering, should we check if self.work_area_id
is in self.mower_attributes.work_areas
in the available property? so if its not there we render the entity unavailable?
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.
Not sure, if I understood that right. Do you mean, a case were one of the work areas is deleted by the user in the mower and HA ist not restarted, yet?
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.
Yes, in that case, this would raise an error I think, so should we add a
@property
def available(self) -> bool:
return super().available and self.work_area_id in self.mower_attributes.work_areas
And maybe we can move the automatic work area creation and deletion to runtime, which is awesome (but can be daunting, so its fine to move to a followup)
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 have an good example for 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.
I use this logic in withings
sensor platform (but it has added complexity).
In essence what is needed is:
- Add listener to the coordinator in
async_setup_entry
of the relevant platforms - Keep a list (or a set for easy adding and subtracting of identifiers) of identifiers of (in this case) work areas you have added
- In the listener you compare the new data with the old data, and if you find old ones you call the entity registry to delete the entities (
airgradient
has this in the mutable entities likenumber
) and if you find new ones, you do a call toasync_add_entities
tedee
also is a nice example, but they have the logic added in the coordinator, which might not fit well
@property | ||
def translation_key(self) -> str: | ||
"""Return the translation key of the work area.""" | ||
return self.entity_description.translation_key_fn(self.work_area_id) | ||
return self.entity_description.translation_key_fn( | ||
self.work_area_id, self.entity_description.key | ||
) |
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.
Is this dynamic? Or should we set this to _attr_translation_key
in the constructor?
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's dynamic. There is one work area which is always there. I called it my_lawn
. This is translated with My lawn
. All other work areas are named by the user and are not translated.
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.
With dynamic I mean, can it change on runtime? Or will it be set for the full runtime of the entity?
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.
And for the work areas which are not translated and set by the user, the entity name would change if the users changes the work area name in the mower.
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.
But then we should set translation key in the constructor and the name in the property
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.
With dynamic I mean, can it change on runtime? Or will it be set for the full runtime of the entity?
Maybe it would be better, if it only changes on reload. Because if a work area name is changed, the user would see it, but if a work area was added, the user wouldn't see the change. I think that could be confusing?
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.
There are ways you can add those entities on runtime, I think its a good user experience if HA follows the app within reasonable time
tests/components/husqvarna_automower/test_work_area_deletion.py
Outdated
Show resolved
Hide resolved
tests/components/husqvarna_automower/test_work_area_deletion.py
Outdated
Show resolved
Hide resolved
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
Co-authored-by: Joost Lekkerkerker <[email protected]>
Breaking change
Proposed change
Add a switch for each work area to enable and disable it.
As this ist the second work area control entity a generic
WorkAreaControlEntity
is introduced.Also the deletion of not needed work area entitites is moved from
number.py
tosensor.py
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: