mirror of https://github.com/home-assistant/core
Bayesian - support `unique_id:` (#79879)
* support unique_id * adds test for unique_ids
This commit is contained in:
parent
3b6f0d2870
commit
fe7402375d
|
@ -22,6 +22,7 @@ from homeassistant.const import (
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
CONF_PLATFORM,
|
CONF_PLATFORM,
|
||||||
CONF_STATE,
|
CONF_STATE,
|
||||||
|
CONF_UNIQUE_ID,
|
||||||
CONF_VALUE_TEMPLATE,
|
CONF_VALUE_TEMPLATE,
|
||||||
STATE_UNAVAILABLE,
|
STATE_UNAVAILABLE,
|
||||||
STATE_UNKNOWN,
|
STATE_UNKNOWN,
|
||||||
|
@ -100,6 +101,7 @@ TEMPLATE_SCHEMA = vol.Schema(
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
|
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
||||||
vol.Optional(CONF_DEVICE_CLASS): cv.string,
|
vol.Optional(CONF_DEVICE_CLASS): cv.string,
|
||||||
vol.Required(CONF_OBSERVATIONS): vol.Schema(
|
vol.Required(CONF_OBSERVATIONS): vol.Schema(
|
||||||
vol.All(
|
vol.All(
|
||||||
|
@ -134,6 +136,7 @@ async def async_setup_platform(
|
||||||
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
|
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
|
||||||
|
|
||||||
name: str = config[CONF_NAME]
|
name: str = config[CONF_NAME]
|
||||||
|
unique_id: str | None = config.get(CONF_UNIQUE_ID)
|
||||||
observations: list[ConfigType] = config[CONF_OBSERVATIONS]
|
observations: list[ConfigType] = config[CONF_OBSERVATIONS]
|
||||||
prior: float = config[CONF_PRIOR]
|
prior: float = config[CONF_PRIOR]
|
||||||
probability_threshold: float = config[CONF_PROBABILITY_THRESHOLD]
|
probability_threshold: float = config[CONF_PROBABILITY_THRESHOLD]
|
||||||
|
@ -152,7 +155,12 @@ async def async_setup_platform(
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
BayesianBinarySensor(
|
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__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
name: str,
|
name: str,
|
||||||
|
unique_id: str | None,
|
||||||
prior: float,
|
prior: float,
|
||||||
observations: list[ConfigType],
|
observations: list[ConfigType],
|
||||||
probability_threshold: float,
|
probability_threshold: float,
|
||||||
|
@ -173,6 +182,7 @@ class BayesianBinarySensor(BinarySensorEntity):
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the Bayesian sensor."""
|
"""Initialize the Bayesian sensor."""
|
||||||
self._attr_name = name
|
self._attr_name = name
|
||||||
|
self._attr_unique_id = unique_id and f"bayesian-{unique_id}"
|
||||||
self._observations = [
|
self._observations = [
|
||||||
Observation(
|
Observation(
|
||||||
entity_id=observation.get(CONF_ENTITY_ID),
|
entity_id=observation.get(CONF_ENTITY_ID),
|
||||||
|
|
|
@ -17,6 +17,7 @@ from homeassistant.const import (
|
||||||
STATE_UNKNOWN,
|
STATE_UNKNOWN,
|
||||||
)
|
)
|
||||||
from homeassistant.core import Context, callback
|
from homeassistant.core import Context, callback
|
||||||
|
from homeassistant.helpers.entity_registry import async_get as async_get_entities
|
||||||
from homeassistant.helpers.event import async_track_state_change_event
|
from homeassistant.helpers.event import async_track_state_change_event
|
||||||
from homeassistant.helpers.issue_registry import async_get
|
from homeassistant.helpers.issue_registry import async_get
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
@ -31,6 +32,8 @@ async def test_load_values_when_added_to_hass(hass):
|
||||||
"binary_sensor": {
|
"binary_sensor": {
|
||||||
"name": "Test_Binary",
|
"name": "Test_Binary",
|
||||||
"platform": "bayesian",
|
"platform": "bayesian",
|
||||||
|
"unique_id": "3b4c9563-5e84-4167-8fe7-8f507e796d72",
|
||||||
|
"device_class": "connectivity",
|
||||||
"observations": [
|
"observations": [
|
||||||
{
|
{
|
||||||
"platform": "state",
|
"platform": "state",
|
||||||
|
@ -51,7 +54,14 @@ async def test_load_values_when_added_to_hass(hass):
|
||||||
assert await async_setup_component(hass, "binary_sensor", config)
|
assert await async_setup_component(hass, "binary_sensor", config)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
entity_registry = async_get_entities(hass)
|
||||||
|
assert (
|
||||||
|
entity_registry.entities["binary_sensor.test_binary"].unique_id
|
||||||
|
== "bayesian-3b4c9563-5e84-4167-8fe7-8f507e796d72"
|
||||||
|
)
|
||||||
|
|
||||||
state = hass.states.get("binary_sensor.test_binary")
|
state = hass.states.get("binary_sensor.test_binary")
|
||||||
|
assert state.attributes.get("device_class") == "connectivity"
|
||||||
assert state.attributes.get("observations")[0]["prob_given_true"] == 0.8
|
assert state.attributes.get("observations")[0]["prob_given_true"] == 0.8
|
||||||
assert state.attributes.get("observations")[0]["prob_given_false"] == 0.4
|
assert state.attributes.get("observations")[0]["prob_given_false"] == 0.4
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue