Skip to content

Commit 65559b3

Browse files
Add entity to a device
1 parent ebc7c4b commit 65559b3

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

custom_components/periodic_min_max/sensor.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from datetime import datetime
66
from typing import Any
77

8+
import voluptuous as vol
89
from homeassistant.components.sensor import (
910
SensorDeviceClass,
1011
SensorEntity,
@@ -20,6 +21,7 @@
2021
STATE_UNKNOWN,
2122
)
2223
from homeassistant.core import Event, EventStateChangedData, HomeAssistant, callback
24+
from homeassistant.helpers import device_registry as dr
2325
from homeassistant.helpers import entity_platform
2426
from homeassistant.helpers import entity_registry as er
2527
from homeassistant.helpers.device import async_device_info_to_link_from_entity
@@ -49,18 +51,50 @@
4951
SENSOR_TYPE_TO_ATTR = {v: k for k, v in SENSOR_TYPES.items()}
5052

5153

54+
@callback
55+
def async_add_to_device(
56+
hass: HomeAssistant, entry: ConfigEntry, entity_id: str
57+
) -> str | None:
58+
"""Add our config entry to the tracked entity's device."""
59+
registry = er.async_get(hass)
60+
device_registry = dr.async_get(hass)
61+
device_id = None
62+
63+
if (
64+
not (source_device := registry.async_get(entity_id))
65+
or not (device_id := source_device.device_id)
66+
or not (device_registry.async_get(device_id))
67+
):
68+
return device_id
69+
70+
device_registry.async_update_device(device_id, add_config_entry_id=entry.entry_id)
71+
72+
return device_id
73+
74+
5275
async def async_setup_entry(
5376
hass: HomeAssistant,
5477
config_entry: ConfigEntry,
5578
async_add_entities: AddConfigEntryEntitiesCallback,
5679
) -> None:
5780
"""Initialize min/max/mean config entry."""
5881
registry = er.async_get(hass)
59-
entity_id = er.async_validate_entity_id(
60-
registry, config_entry.options[CONF_ENTITY_ID]
61-
)
82+
try:
83+
entity_id = er.async_validate_entity_id(
84+
registry, config_entry.options[CONF_ENTITY_ID]
85+
)
86+
except vol.Invalid:
87+
# The entity is identified by an unknown entity registry ID
88+
LOGGER.error(
89+
"Failed to setup periodic_min_max for unknown entity %s",
90+
config_entry.options[CONF_ENTITY_ID],
91+
)
92+
return False
93+
6294
sensor_type = config_entry.options[CONF_TYPE]
6395

96+
async_add_to_device(hass, config_entry, entity_id)
97+
6498
async_add_entities(
6599
[
66100
PeriodicMinMaxSensor(

scripts/develop

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,10 @@ if [[ ! -d "${PWD}/config" ]]; then
1111
fi
1212

1313
# Set the path to custom_components
14-
## This let's us have the structure we want <root>/custom_components/periodic_min_max
14+
## This let's us have the structure we want <root>/custom_components/integration_battery_types
1515
## while at the same time have Home Assistant configuration inside <root>/config
1616
## without resulting to symlinks.
1717
export PYTHONPATH="${PYTHONPATH}:${PWD}/custom_components"
1818

1919
# Start Home Assistant
20-
# debugpy logging, if required: --log-to debugpy
21-
python3 -Xfrozen_modules=off \
22-
-m debugpy --listen localhost:5678 \
23-
-m homeassistant --config "${PWD}/config"
20+
hass --config "${PWD}/config" --debug

0 commit comments

Comments
 (0)