Home Assistant Unofficial Reference 2024.12.1
device.py
Go to the documentation of this file.
1 """Provides useful helpers for handling devices."""
2 
3 from homeassistant.core import HomeAssistant, callback
4 
5 from . import device_registry as dr, entity_registry as er
6 
7 
8 @callback
10  hass: HomeAssistant,
11  entity_id_or_uuid: str,
12 ) -> str | None:
13  """Resolve the device id to the entity id or entity uuid."""
14 
15  ent_reg = er.async_get(hass)
16 
17  entity_id = er.async_validate_entity_id(ent_reg, entity_id_or_uuid)
18  if (entity := ent_reg.async_get(entity_id)) is None:
19  return None
20 
21  return entity.device_id
22 
23 
24 @callback
26  hass: HomeAssistant,
27  entity_id_or_uuid: str,
28 ) -> dr.DeviceInfo | None:
29  """DeviceInfo with information to link a device from an entity.
30 
31  DeviceInfo will only return information to categorize as a link.
32  """
33 
35  hass,
36  async_entity_id_to_device_id(hass, entity_id_or_uuid),
37  )
38 
39 
40 @callback
42  hass: HomeAssistant,
43  device_id: str | None,
44 ) -> dr.DeviceInfo | None:
45  """DeviceInfo with information to link a device from a device id.
46 
47  DeviceInfo will only return information to categorize as a link.
48  """
49 
50  dev_reg = dr.async_get(hass)
51 
52  if device_id is None or (device := dev_reg.async_get(device_id=device_id)) is None:
53  return None
54 
55  return dr.DeviceInfo(
56  identifiers=device.identifiers,
57  connections=device.connections,
58  )
59 
60 
61 @callback
63  hass: HomeAssistant,
64  entry_id: str,
65  source_entity_id_or_uuid: str,
66 ) -> None:
67  """Remove the link between stale devices and a configuration entry.
68 
69  Only the device passed in the source_entity_id_or_uuid parameter
70  linked to the configuration entry will be maintained.
71  """
72 
74  hass=hass,
75  entry_id=entry_id,
76  current_device_id=async_entity_id_to_device_id(hass, source_entity_id_or_uuid),
77  )
78 
79 
80 @callback
82  hass: HomeAssistant,
83  entry_id: str,
84  current_device_id: str | None,
85 ) -> None:
86  """Remove the link between stale devices and a configuration entry.
87 
88  Only the device passed in the current_device_id parameter linked to
89  the configuration entry will be maintained.
90  """
91 
92  dev_reg = dr.async_get(hass)
93  # Removes all devices from the config entry that are not the same as the current device
94  for device in dev_reg.devices.get_devices_for_config_entry_id(entry_id):
95  if device.id == current_device_id:
96  continue
97  dev_reg.async_update_device(device.id, remove_config_entry_id=entry_id)
dr.DeviceInfo|None async_device_info_to_link_from_entity(HomeAssistant hass, str entity_id_or_uuid)
Definition: device.py:28
None async_remove_stale_devices_links_keep_current_device(HomeAssistant hass, str entry_id, str|None current_device_id)
Definition: device.py:85
None async_remove_stale_devices_links_keep_entity_device(HomeAssistant hass, str entry_id, str source_entity_id_or_uuid)
Definition: device.py:66
dr.DeviceInfo|None async_device_info_to_link_from_device_id(HomeAssistant hass, str|None device_id)
Definition: device.py:44
str|None async_entity_id_to_device_id(HomeAssistant hass, str entity_id_or_uuid)
Definition: device.py:12