From 6d6e0dd8bf6dcf2d1cea937b04e9eef286936c75 Mon Sep 17 00:00:00 2001 From: Thomas Dietrich Date: Sat, 4 Dec 2021 09:50:47 +0100 Subject: [PATCH] Add unique_id to the statistics component (#59205) * Implement optional manually defined uniqueid * Fix test case via mocked environment --- homeassistant/components/statistics/sensor.py | 10 ++++++++ tests/components/statistics/test_sensor.py | 24 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/homeassistant/components/statistics/sensor.py b/homeassistant/components/statistics/sensor.py index a931a4cf806..44548c7d1a7 100644 --- a/homeassistant/components/statistics/sensor.py +++ b/homeassistant/components/statistics/sensor.py @@ -17,6 +17,7 @@ from homeassistant.const import ( ATTR_UNIT_OF_MEASUREMENT, CONF_ENTITY_ID, CONF_NAME, + CONF_UNIQUE_ID, STATE_UNAVAILABLE, STATE_UNKNOWN, ) @@ -115,6 +116,7 @@ _PLATFORM_SCHEMA_BASE = PLATFORM_SCHEMA.extend( { vol.Required(CONF_ENTITY_ID): cv.entity_id, vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, + vol.Optional(CONF_UNIQUE_ID): cv.string, vol.Optional(CONF_STATE_CHARACTERISTIC, default=STAT_DEFAULT): vol.In( [ STAT_AVERAGE_LINEAR, @@ -170,6 +172,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= StatisticsSensor( source_entity_id=config.get(CONF_ENTITY_ID), name=config.get(CONF_NAME), + unique_id=config.get(CONF_UNIQUE_ID), state_characteristic=config.get(CONF_STATE_CHARACTERISTIC), samples_max_buffer_size=config.get(CONF_SAMPLES_MAX_BUFFER_SIZE), samples_max_age=config.get(CONF_MAX_AGE), @@ -190,6 +193,7 @@ class StatisticsSensor(SensorEntity): self, source_entity_id, name, + unique_id, state_characteristic, samples_max_buffer_size, samples_max_age, @@ -201,6 +205,7 @@ class StatisticsSensor(SensorEntity): self._source_entity_id = source_entity_id self.is_binary = self._source_entity_id.split(".")[0] == "binary_sensor" self._name = name + self._unique_id = unique_id self._state_characteristic = state_characteristic if self._state_characteristic == STAT_DEFAULT: self._state_characteristic = STAT_COUNT if self.is_binary else STAT_MEAN @@ -335,6 +340,11 @@ class StatisticsSensor(SensorEntity): """Return the name of the sensor.""" return self._name + @property + def unique_id(self): + """Return the unique id of the sensor.""" + return self._unique_id + @property def state_class(self): """Return the state class of this entity.""" diff --git a/tests/components/statistics/test_sensor.py b/tests/components/statistics/test_sensor.py index 658d6a089e7..50f6993e44c 100644 --- a/tests/components/statistics/test_sensor.py +++ b/tests/components/statistics/test_sensor.py @@ -17,6 +17,7 @@ from homeassistant.const import ( STATE_UNKNOWN, TEMP_CELSIUS, ) +from homeassistant.helpers import entity_registry as er from homeassistant.setup import async_setup_component, setup_component from homeassistant.util import dt as dt_util @@ -35,6 +36,29 @@ def mock_legacy_time(legacy_patchable_time): yield +async def test_unique_id(hass): + """Test configuration defined unique_id.""" + assert await async_setup_component( + hass, + "sensor", + { + "sensor": [ + { + "platform": "statistics", + "name": "test", + "entity_id": "sensor.test_monitored", + "unique_id": "uniqueid_sensor_test", + }, + ] + }, + ) + await hass.async_block_till_done() + + entity_reg = er.async_get(hass) + entity_id = entity_reg.async_get_entity_id("sensor", DOMAIN, "uniqueid_sensor_test") + assert entity_id == "sensor.test" + + class TestStatisticsSensor(unittest.TestCase): """Test the Statistics sensor."""