Add unique_id to the filter component (#65010)
* Optional manually defined uniqueid * move to _attrpull/66997/head
parent
2456d8a401
commit
9ed4bcf965
|
@ -29,6 +29,7 @@ from homeassistant.const import (
|
|||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
CONF_ENTITY_ID,
|
||||
CONF_NAME,
|
||||
CONF_UNIQUE_ID,
|
||||
STATE_UNAVAILABLE,
|
||||
STATE_UNKNOWN,
|
||||
)
|
||||
|
@ -150,6 +151,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||
cv.entity_domain(INPUT_NUMBER_DOMAIN),
|
||||
),
|
||||
vol.Optional(CONF_NAME): cv.string,
|
||||
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
||||
vol.Required(CONF_FILTERS): vol.All(
|
||||
cv.ensure_list,
|
||||
[
|
||||
|
@ -178,6 +180,7 @@ async def async_setup_platform(
|
|||
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
|
||||
|
||||
name = config.get(CONF_NAME)
|
||||
unique_id = config.get(CONF_UNIQUE_ID)
|
||||
entity_id = config.get(CONF_ENTITY_ID)
|
||||
|
||||
filters = [
|
||||
|
@ -185,15 +188,16 @@ async def async_setup_platform(
|
|||
for _filter in config[CONF_FILTERS]
|
||||
]
|
||||
|
||||
async_add_entities([SensorFilter(name, entity_id, filters)])
|
||||
async_add_entities([SensorFilter(name, unique_id, entity_id, filters)])
|
||||
|
||||
|
||||
class SensorFilter(SensorEntity):
|
||||
"""Representation of a Filter Sensor."""
|
||||
|
||||
def __init__(self, name, entity_id, filters):
|
||||
def __init__(self, name, unique_id, entity_id, filters):
|
||||
"""Initialize the sensor."""
|
||||
self._name = name
|
||||
self._attr_unique_id = unique_id
|
||||
self._entity = entity_id
|
||||
self._unit_of_measurement = None
|
||||
self._state = None
|
||||
|
|
|
@ -26,6 +26,7 @@ from homeassistant.const import (
|
|||
STATE_UNKNOWN,
|
||||
)
|
||||
import homeassistant.core as ha
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.setup import async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
|
@ -256,6 +257,7 @@ async def test_setup(hass):
|
|||
"sensor": {
|
||||
"platform": "filter",
|
||||
"name": "test",
|
||||
"unique_id": "uniqueid_sensor_test",
|
||||
"entity_id": "sensor.test_monitored",
|
||||
"filters": [
|
||||
{"filter": "outlier", "window_size": 10, "radius": 4.0},
|
||||
|
@ -285,6 +287,12 @@ async def test_setup(hass):
|
|||
assert state.attributes[ATTR_STATE_CLASS] is SensorStateClass.TOTAL_INCREASING
|
||||
assert state.state == "1.0"
|
||||
|
||||
entity_reg = er.async_get(hass)
|
||||
entity_id = entity_reg.async_get_entity_id(
|
||||
"sensor", DOMAIN, "uniqueid_sensor_test"
|
||||
)
|
||||
assert entity_id == "sensor.test"
|
||||
|
||||
|
||||
async def test_invalid_state(hass):
|
||||
"""Test if filter attributes are inherited."""
|
||||
|
|
Loading…
Reference in New Issue