Skip to content
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

Add the device of the source entity in the helper entities for Riemann sum integral #94727

Merged
merged 3 commits into from Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 30 additions & 1 deletion homeassistant/components/integration/sensor.py
Expand Up @@ -28,7 +28,12 @@
UnitOfTime,
)
from homeassistant.core import Event, HomeAssistant, State, callback
from homeassistant.helpers import config_validation as cv, entity_registry as er
from homeassistant.helpers import (
config_validation as cv,
device_registry as dr,
entity_registry as er,
)
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_track_state_change_event
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
Expand Down Expand Up @@ -140,6 +145,27 @@ async def async_setup_entry(
registry, config_entry.options[CONF_SOURCE_SENSOR]
)

source_entity = er.EntityRegistry.async_get(registry, source_entity_id)
dev_reg = dr.async_get(hass)
# Resolve source entity device
if (
(source_entity is not None)
and (source_entity.device_id is not None)
and (
(
device := dev_reg.async_get(
device_id=source_entity.device_id,
)
)
is not None
)
):
device_info = DeviceInfo(
identifiers=device.identifiers,
)
else:
device_info = None

unit_prefix = config_entry.options[CONF_UNIT_PREFIX]
if unit_prefix == "none":
unit_prefix = None
Expand All @@ -152,6 +178,7 @@ async def async_setup_entry(
unique_id=config_entry.entry_id,
unit_prefix=unit_prefix,
unit_time=config_entry.options[CONF_UNIT_TIME],
device_info=device_info,
)

async_add_entities([integral])
Expand Down Expand Up @@ -194,6 +221,7 @@ def __init__(
unique_id: str | None,
unit_prefix: str | None,
unit_time: UnitOfTime,
device_info: DeviceInfo | None = None,
) -> None:
"""Initialize the integration sensor."""
self._attr_unique_id = unique_id
Expand All @@ -211,6 +239,7 @@ def __init__(
self._attr_icon = "mdi:chart-histogram"
self._source_entity: str = source_entity
self._last_valid_state: Decimal | None = None
self._attr_device_info = device_info

def _unit(self, source_unit: str) -> str:
"""Derive unit from the source sensor, SI prefix and time unit."""
Expand Down
52 changes: 51 additions & 1 deletion tests/components/integration/test_sensor.py
Expand Up @@ -4,6 +4,7 @@
from freezegun import freeze_time
import pytest

from homeassistant.components.integration.const import DOMAIN
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT,
Expand All @@ -16,10 +17,15 @@
UnitOfTime,
)
from homeassistant.core import HomeAssistant, State
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util

from tests.common import mock_restore_cache, mock_restore_cache_with_extra_data
from tests.common import (
MockConfigEntry,
mock_restore_cache,
mock_restore_cache_with_extra_data,
)


@pytest.mark.parametrize("method", ["trapezoidal", "left", "right"])
Expand Down Expand Up @@ -671,3 +677,47 @@ async def test_calc_errors(hass: HomeAssistant, method) -> None:
state = hass.states.get("sensor.integration")
assert state is not None
assert round(float(state.state)) == 0 if method != "right" else 1


async def test_device_id(hass: HomeAssistant) -> None:
"""Test for source entity device for Riemann sum integral."""
device_registry = dr.async_get(hass)
entity_registry = er.async_get(hass)

source_config_entry = MockConfigEntry()
source_device_entry = device_registry.async_get_or_create(
config_entry_id=source_config_entry.entry_id,
identifiers={("sensor", "identifier_test")},
)
source_entity = 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 entity_registry.async_get("sensor.test_source") is not None

integration_config_entry = MockConfigEntry(
data={},
domain=DOMAIN,
options={
"method": "trapezoidal",
"name": "integration",
"round": 1.0,
"source": "sensor.test_source",
"unit_prefix": "k",
"unit_time": "min",
},
title="Integration",
)

integration_config_entry.add_to_hass(hass)

assert await hass.config_entries.async_setup(integration_config_entry.entry_id)
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 == source_entity.device_id