Add unique_id to the statistics component (#59205)
* Implement optional manually defined uniqueid * Fix test case via mocked environment
This commit is contained in:
parent
a553054fde
commit
6d6e0dd8bf
2 changed files with 34 additions and 0 deletions
|
@ -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."""
|
||||
|
|
|
@ -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."""
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue