Do not set a device on YAML integration entities (#177596)

This commit is contained in:
Artur Pragacz
2026-07-30 08:49:23 +02:00
committed by GitHub
parent 6c38a774cb
commit afad195652
2 changed files with 48 additions and 7 deletions
@@ -38,6 +38,7 @@ from homeassistant.core import (
)
from homeassistant.helpers import config_validation as cv, entity_registry as er
from homeassistant.helpers.device import async_entity_id_to_device
from homeassistant.helpers.device_registry import DeviceEntry
from homeassistant.helpers.entity_platform import (
AddConfigEntryEntitiesCallback,
AddEntitiesCallback,
@@ -266,7 +267,6 @@ async def async_setup_entry(
round_digits = int(round_digits)
integral = IntegrationSensor(
hass,
integration_method=config_entry.options[CONF_METHOD],
name=config_entry.title,
round_digits=round_digits,
@@ -275,6 +275,7 @@ async def async_setup_entry(
unit_prefix=unit_prefix,
unit_time=config_entry.options[CONF_UNIT_TIME],
max_sub_interval=max_sub_interval,
device=async_entity_id_to_device(hass, source_entity_id),
)
async_add_entities([integral])
@@ -288,7 +289,6 @@ async def async_setup_platform(
) -> None:
"""Set up the integration sensor."""
integral = IntegrationSensor(
hass,
integration_method=config[CONF_METHOD],
name=config.get(CONF_NAME),
round_digits=config.get(CONF_ROUND_DIGITS),
@@ -310,7 +310,6 @@ class IntegrationSensor(RestoreSensor):
def __init__(
self,
hass: HomeAssistant,
*,
integration_method: str,
name: str | None,
@@ -320,6 +319,7 @@ class IntegrationSensor(RestoreSensor):
unit_prefix: str | None,
unit_time: UnitOfTime,
max_sub_interval: timedelta | None,
device: DeviceEntry | None = None,
) -> None:
"""Initialize the integration sensor."""
self._attr_unique_id = unique_id
@@ -337,10 +337,7 @@ class IntegrationSensor(RestoreSensor):
self._attr_icon = "mdi:chart-histogram"
self._source_entity: str = source_entity
self._last_valid_state: Decimal | None = None
self.device_entry = async_entity_id_to_device(
hass,
source_entity,
)
self.device_entry = device
self._max_sub_interval: timedelta | None = (
None # disable time based integration
if max_sub_interval is None or max_sub_interval.total_seconds() == 0
@@ -918,6 +918,50 @@ async def test_device_id(
assert integration_entity.device_id == source_entity.device_id
async def test_device_id_yaml(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test no device is set for a YAML-configured Riemann sum integral."""
source_config_entry = MockConfigEntry()
source_config_entry.add_to_hass(hass)
source_device_entry = device_registry.async_get_or_create(
config_entry_id=source_config_entry.entry_id,
identifiers={("sensor", "identifier_test")},
connections={("mac", "30:31:32:33:34:35")},
)
entity_registry.async_get_or_create(
"sensor",
"test",
"source",
config_entry=source_config_entry,
device_id=source_device_entry.id,
)
await hass.async_block_till_done()
assert await async_setup_component(
hass,
"sensor",
{
"sensor": {
"platform": "integration",
"name": "integration",
"source": "sensor.test_source",
"method": "right",
"unique_id": "integration_yaml",
}
},
)
await hass.async_block_till_done()
integration_entity = entity_registry.async_get("sensor.integration")
assert integration_entity is not None
assert integration_entity.device_id is None
assert "attempts to attach a device to an entity" not in caplog.text
def _integral_sensor_config(max_sub_interval: dict[str, int] | None) -> dict[str, Any]:
sensor = {
"platform": "integration",