Home Assistant Unofficial Reference 2024.12.1
deprecate.py
Go to the documentation of this file.
1 """Helper class for deprecating entities."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Sequence
6 from dataclasses import dataclass
7 from typing import TYPE_CHECKING
8 
9 from homeassistant.components.automation import automations_with_entity
10 from homeassistant.components.script import scripts_with_entity
11 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers import entity_registry as er
13 from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
14 
15 from .const import DOMAIN
16 
17 if TYPE_CHECKING:
18  from .entity import CoordinatedTPLinkFeatureEntity, TPLinkFeatureEntityDescription
19 
20 
21 @dataclass(slots=True)
23  """Class to define deprecation info for deprecated entities."""
24 
25  platform: str
26  new_platform: str
27  breaks_in_ha_version: str
28 
29 
31  hass: HomeAssistant,
32  unique_id: str,
33  entity_description: TPLinkFeatureEntityDescription,
34 ) -> bool:
35  """Return true if the entity should be created based on the deprecated_info.
36 
37  If deprecated_info is not defined will return true.
38  If entity not yet created will return false.
39  If entity disabled will return false.
40  """
41  if not entity_description.deprecated_info:
42  return True
43 
44  deprecated_info = entity_description.deprecated_info
45  platform = deprecated_info.platform
46 
47  ent_reg = er.async_get(hass)
48  entity_id = ent_reg.async_get_entity_id(
49  platform,
50  DOMAIN,
51  unique_id,
52  )
53  if not entity_id:
54  return False
55 
56  entity_entry = ent_reg.async_get(entity_id)
57  assert entity_entry
58  return not entity_entry.disabled
59 
60 
62  hass: HomeAssistant,
63  platform: str,
64  entry_id: str,
65  entities: Sequence[CoordinatedTPLinkFeatureEntity],
66 ) -> None:
67  """Remove disabled deprecated entities or create issues if necessary."""
68  ent_reg = er.async_get(hass)
69  for entity in entities:
70  if not (deprecated_info := entity.entity_description.deprecated_info):
71  continue
72 
73  assert entity.unique_id
74  entity_id = ent_reg.async_get_entity_id(
75  platform,
76  DOMAIN,
77  entity.unique_id,
78  )
79  assert entity_id
80  # Check for issues that need to be created
81  entity_automations = automations_with_entity(hass, entity_id)
82  entity_scripts = scripts_with_entity(hass, entity_id)
83 
84  for item in entity_automations + entity_scripts:
86  hass,
87  DOMAIN,
88  f"deprecated_entity_{entity_id}_{item}",
89  breaks_in_ha_version=deprecated_info.breaks_in_ha_version,
90  is_fixable=False,
91  is_persistent=False,
92  severity=IssueSeverity.WARNING,
93  translation_key="deprecated_entity",
94  translation_placeholders={
95  "entity": entity_id,
96  "info": item,
97  "platform": platform,
98  "new_platform": deprecated_info.new_platform,
99  },
100  )
101 
102  # Remove entities that are no longer provided and have been disabled.
103  unique_ids = {entity.unique_id for entity in entities}
104  for entity_entry in er.async_entries_for_config_entry(ent_reg, entry_id):
105  if (
106  entity_entry.domain == platform
107  and entity_entry.disabled
108  and entity_entry.unique_id not in unique_ids
109  ):
110  ent_reg.async_remove(entity_entry.entity_id)
111  continue
list[str] automations_with_entity(HomeAssistant hass, str entity_id)
Definition: __init__.py:191
None async_create_issue(HomeAssistant hass, str entry_id)
Definition: repairs.py:69
list[str] scripts_with_entity(HomeAssistant hass, str entity_id)
Definition: __init__.py:126