Bayesian - support unique_id: (#79879)

* support unique_id

* adds test for unique_ids
This commit is contained in:
HarvsG
2022-10-25 11:42:59 +01:00
committed by GitHub
parent 3b6f0d2870
commit fe7402375d
2 changed files with 21 additions and 1 deletions

View File

@@ -22,6 +22,7 @@ from homeassistant.const import (
CONF_NAME,
CONF_PLATFORM,
CONF_STATE,
CONF_UNIQUE_ID,
CONF_VALUE_TEMPLATE,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
@@ -100,6 +101,7 @@ TEMPLATE_SCHEMA = vol.Schema(
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_UNIQUE_ID): cv.string,
vol.Optional(CONF_DEVICE_CLASS): cv.string,
vol.Required(CONF_OBSERVATIONS): vol.Schema(
vol.All(
@@ -134,6 +136,7 @@ async def async_setup_platform(
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
name: str = config[CONF_NAME]
unique_id: str | None = config.get(CONF_UNIQUE_ID)
observations: list[ConfigType] = config[CONF_OBSERVATIONS]
prior: float = config[CONF_PRIOR]
probability_threshold: float = config[CONF_PROBABILITY_THRESHOLD]
@@ -152,7 +155,12 @@ async def async_setup_platform(
async_add_entities(
[
BayesianBinarySensor(
name, prior, observations, probability_threshold, device_class
name,
unique_id,
prior,
observations,
probability_threshold,
device_class,
)
]
)
@@ -166,6 +174,7 @@ class BayesianBinarySensor(BinarySensorEntity):
def __init__(
self,
name: str,
unique_id: str | None,
prior: float,
observations: list[ConfigType],
probability_threshold: float,
@@ -173,6 +182,7 @@ class BayesianBinarySensor(BinarySensorEntity):
) -> None:
"""Initialize the Bayesian sensor."""
self._attr_name = name
self._attr_unique_id = unique_id and f"bayesian-{unique_id}"
self._observations = [
Observation(
entity_id=observation.get(CONF_ENTITY_ID),