Add unique_id support to history_stats sensor (#92972)
parent
24284fe379
commit
02b5d643e7
|
@ -17,6 +17,7 @@ from homeassistant.const import (
|
|||
CONF_NAME,
|
||||
CONF_STATE,
|
||||
CONF_TYPE,
|
||||
CONF_UNIQUE_ID,
|
||||
PERCENTAGE,
|
||||
UnitOfTime,
|
||||
)
|
||||
|
@ -72,6 +73,7 @@ PLATFORM_SCHEMA = vol.All(
|
|||
vol.Optional(CONF_DURATION): cv.time_period,
|
||||
vol.Optional(CONF_TYPE, default=CONF_TYPE_TIME): vol.In(CONF_TYPE_KEYS),
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
||||
}
|
||||
),
|
||||
exactly_two_period_keys,
|
||||
|
@ -95,6 +97,7 @@ async def async_setup_platform(
|
|||
duration: datetime.timedelta | None = config.get(CONF_DURATION)
|
||||
sensor_type: str = config[CONF_TYPE]
|
||||
name: str = config[CONF_NAME]
|
||||
unique_id: str | None = config.get(CONF_UNIQUE_ID)
|
||||
|
||||
for template in (start, end):
|
||||
if template is not None:
|
||||
|
@ -105,7 +108,7 @@ async def async_setup_platform(
|
|||
await coordinator.async_refresh()
|
||||
if not coordinator.last_update_success:
|
||||
raise PlatformNotReady from coordinator.last_exception
|
||||
async_add_entities([HistoryStatsSensor(coordinator, sensor_type, name)])
|
||||
async_add_entities([HistoryStatsSensor(coordinator, sensor_type, name, unique_id)])
|
||||
|
||||
|
||||
class HistoryStatsSensorBase(
|
||||
|
@ -150,11 +153,13 @@ class HistoryStatsSensor(HistoryStatsSensorBase):
|
|||
coordinator: HistoryStatsUpdateCoordinator,
|
||||
sensor_type: str,
|
||||
name: str,
|
||||
unique_id: str | None,
|
||||
) -> None:
|
||||
"""Initialize the HistoryStats sensor."""
|
||||
super().__init__(coordinator, name)
|
||||
self._attr_native_unit_of_measurement = UNITS[sensor_type]
|
||||
self._type = sensor_type
|
||||
self._attr_unique_id = unique_id
|
||||
self._process_update()
|
||||
if self._type == CONF_TYPE_TIME:
|
||||
self._attr_device_class = SensorDeviceClass.DURATION
|
||||
|
|
|
@ -15,6 +15,7 @@ from homeassistant.components.recorder import Recorder
|
|||
from homeassistant.const import ATTR_DEVICE_CLASS, SERVICE_RELOAD, STATE_UNKNOWN
|
||||
import homeassistant.core as ha
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers.entity_component import async_update_entity
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
@ -1589,3 +1590,25 @@ async def test_history_stats_handles_floored_timestamps(
|
|||
await hass.async_block_till_done()
|
||||
|
||||
assert last_times == (start_time, start_time + timedelta(hours=2))
|
||||
|
||||
|
||||
async def test_unique_id(recorder_mock: Recorder, hass: HomeAssistant) -> None:
|
||||
"""Test unique_id property."""
|
||||
|
||||
config = {
|
||||
"sensor": {
|
||||
"platform": "history_stats",
|
||||
"entity_id": "binary_sensor.test_id",
|
||||
"state": "on",
|
||||
"start": "{{ utcnow() }}",
|
||||
"duration": "01:00",
|
||||
"name": "Test",
|
||||
"unique_id": "some_history_stats_unique_id",
|
||||
},
|
||||
}
|
||||
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
registry = er.async_get(hass)
|
||||
assert registry.async_get("sensor.test").unique_id == "some_history_stats_unique_id"
|
||||
|
|
Loading…
Reference in New Issue