-
-
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
Deprecate tplink alarm button entities #126349
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f1095a5
Deprecate tplink alarm button entities
sdb9696 29a8be9
Add test for deprecated not previously existing
sdb9696 9070062
Move side effects out of list comprehension
sdb9696 2ee59c2
Address review comments
sdb9696 c7f0754
Fix coverage by adding deprecated clean up to sensor
sdb9696 1e7a73b
Merge branch 'dev' into tplink/deprecate_buttons
bdraco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
"""Helper class for deprecating entities.""" | ||
|
||
from __future__ import annotations | ||
|
||
from collections.abc import Sequence | ||
from dataclasses import dataclass | ||
from typing import TYPE_CHECKING | ||
|
||
from homeassistant.components.automation import automations_with_entity | ||
from homeassistant.components.script import scripts_with_entity | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers import entity_registry as er | ||
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue | ||
|
||
from .const import DOMAIN | ||
|
||
if TYPE_CHECKING: | ||
from .entity import CoordinatedTPLinkFeatureEntity, TPLinkFeatureEntityDescription | ||
|
||
|
||
@dataclass(slots=True) | ||
class DeprecatedInfo: | ||
"""Class to define deprecation info for deprecated entities.""" | ||
|
||
platform: str | ||
new_platform: str | ||
breaks_in_ha_version: str | ||
|
||
|
||
def async_check_create_deprecated( | ||
hass: HomeAssistant, | ||
unique_id: str, | ||
entity_description: TPLinkFeatureEntityDescription, | ||
) -> bool: | ||
"""Return true if the entity should be created based on the deprecated_info. | ||
|
||
If deprecated_info is not defined will return true. | ||
If entity not yet created will return false. | ||
If entity disabled will return false. | ||
""" | ||
if not entity_description.deprecated_info: | ||
return True | ||
|
||
deprecated_info = entity_description.deprecated_info | ||
platform = deprecated_info.platform | ||
|
||
ent_reg = er.async_get(hass) | ||
entity_id = ent_reg.async_get_entity_id( | ||
platform, | ||
DOMAIN, | ||
unique_id, | ||
) | ||
if not entity_id: | ||
return False | ||
sdb9696 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
entity_entry = ent_reg.async_get(entity_id) | ||
assert entity_entry | ||
return not entity_entry.disabled | ||
|
||
|
||
def async_cleanup_deprecated( | ||
hass: HomeAssistant, | ||
platform: str, | ||
entry_id: str, | ||
entities: Sequence[CoordinatedTPLinkFeatureEntity], | ||
) -> None: | ||
"""Remove disabled deprecated entities or create issues if necessary.""" | ||
ent_reg = er.async_get(hass) | ||
for entity in entities: | ||
if not (deprecated_info := entity.entity_description.deprecated_info): | ||
continue | ||
bdraco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
assert entity.unique_id | ||
entity_id = ent_reg.async_get_entity_id( | ||
platform, | ||
DOMAIN, | ||
entity.unique_id, | ||
) | ||
assert entity_id | ||
# Check for issues that need to be created | ||
entity_automations = automations_with_entity(hass, entity_id) | ||
entity_scripts = scripts_with_entity(hass, entity_id) | ||
|
||
for item in entity_automations + entity_scripts: | ||
async_create_issue( | ||
hass, | ||
DOMAIN, | ||
f"deprecated_entity_{entity_id}_{item}", | ||
breaks_in_ha_version=deprecated_info.breaks_in_ha_version, | ||
is_fixable=False, | ||
is_persistent=False, | ||
severity=IssueSeverity.WARNING, | ||
translation_key="deprecated_entity", | ||
translation_placeholders={ | ||
"entity": entity_id, | ||
"info": item, | ||
"platform": platform, | ||
"new_platform": deprecated_info.new_platform, | ||
}, | ||
) | ||
|
||
# Remove entities that are no longer provided and have been disabled. | ||
unique_ids = {entity.unique_id for entity in entities} | ||
for entity_entry in er.async_entries_for_config_entry(ent_reg, entry_id): | ||
if ( | ||
entity_entry.domain == platform | ||
and entity_entry.disabled | ||
and entity_entry.unique_id not in unique_ids | ||
): | ||
ent_reg.async_remove(entity_entry.entity_id) | ||
continue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
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 saw that you made the PR #125594 but for me it just seems overly complicated?
Should/Could
async_check_create_deprecated
not be a simple helper function which you can run the entities through to deprecate them instead of moving it into the entity description and thus making it more complex (which I don't think is needed)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 created home-assistant/architecture#1133 because I think this is a common pattern that could/should be provided by core.
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 know. My question is still why is this not just a simple helper instead of building this complex thing?
As that discussion is not close to be finalized I think we still need to address it here as this PR is ongoing.
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.
Moved reply to comment below
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 have strong feelings on where the
DeprecatedInfo
gets stored. That should be worked out to a solution that everyone is ok with in home-assistant/architecture#1133For the
tplink
integration, its fine to move forward with this design and adapt to the final outcome of the arch discussion later. I don't want to hold this PR up as the new entity has already been merged in a previous PR and we need to give users proper notice that the other entity is going away.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'm fine with leaving that for the arch discussion to not block this PR 👍