Bayesian - support unique_id:
(#79879)
* support unique_id * adds test for unique_ids
This commit is contained in:
parent
3b6f0d2870
commit
fe7402375d
2 changed files with 21 additions and 1 deletions
|
@ -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),
|
||||
|
|
|
@ -17,6 +17,7 @@ from homeassistant.const import (
|
|||
STATE_UNKNOWN,
|
||||
)
|
||||
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.issue_registry import async_get
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
@ -31,6 +32,8 @@ async def test_load_values_when_added_to_hass(hass):
|
|||
"binary_sensor": {
|
||||
"name": "Test_Binary",
|
||||
"platform": "bayesian",
|
||||
"unique_id": "3b4c9563-5e84-4167-8fe7-8f507e796d72",
|
||||
"device_class": "connectivity",
|
||||
"observations": [
|
||||
{
|
||||
"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)
|
||||
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")
|
||||
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_false"] == 0.4
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue