mirror of
https://github.com/home-assistant/core.git
synced 2025-07-03 19:37:10 +00:00
Add the ability to reload bayesian platforms from yaml (#39771)
This commit is contained in:
parent
2a879afc7a
commit
c9ec533aa5
@ -1 +1,4 @@
|
||||
"""The bayesian component."""
|
||||
|
||||
DOMAIN = "bayesian"
|
||||
PLATFORMS = ["binary_sensor"]
|
||||
|
@ -25,8 +25,11 @@ from homeassistant.helpers.event import (
|
||||
async_track_state_change_event,
|
||||
async_track_template_result,
|
||||
)
|
||||
from homeassistant.helpers.reload import async_setup_reload_service
|
||||
from homeassistant.helpers.template import result_as_boolean
|
||||
|
||||
from . import DOMAIN, PLATFORMS
|
||||
|
||||
ATTR_OBSERVATIONS = "observations"
|
||||
ATTR_OCCURRED_OBSERVATION_ENTITIES = "occurred_observation_entities"
|
||||
ATTR_PROBABILITY = "probability"
|
||||
@ -106,6 +109,8 @@ def update_probability(prior, prob_given_true, prob_given_false):
|
||||
|
||||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
||||
"""Set up the Bayesian Binary sensor."""
|
||||
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
|
||||
|
||||
name = config[CONF_NAME]
|
||||
observations = config[CONF_OBSERVATIONS]
|
||||
prior = config[CONF_PRIOR]
|
||||
|
2
homeassistant/components/bayesian/services.yaml
Normal file
2
homeassistant/components/bayesian/services.yaml
Normal file
@ -0,0 +1,2 @@
|
||||
reload:
|
||||
description: Reload all bayesian entities.
|
@ -1,15 +1,18 @@
|
||||
"""The test for the bayesian sensor platform."""
|
||||
import json
|
||||
from os import path
|
||||
import unittest
|
||||
|
||||
from homeassistant.components.bayesian import binary_sensor as bayesian
|
||||
from homeassistant import config as hass_config
|
||||
from homeassistant.components.bayesian import DOMAIN, binary_sensor as bayesian
|
||||
from homeassistant.components.homeassistant import (
|
||||
DOMAIN as HA_DOMAIN,
|
||||
SERVICE_UPDATE_ENTITY,
|
||||
)
|
||||
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN
|
||||
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_RELOAD, STATE_UNKNOWN
|
||||
from homeassistant.setup import async_setup_component, setup_component
|
||||
|
||||
from tests.async_mock import patch
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
|
||||
@ -631,3 +634,55 @@ async def test_monitored_sensor_goes_away(hass):
|
||||
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get("binary_sensor.test_binary").state == "on"
|
||||
|
||||
|
||||
async def test_reload(hass):
|
||||
"""Verify we can reload bayesian sensors."""
|
||||
|
||||
config = {
|
||||
"binary_sensor": {
|
||||
"name": "test",
|
||||
"platform": "bayesian",
|
||||
"observations": [
|
||||
{
|
||||
"platform": "state",
|
||||
"entity_id": "sensor.test_monitored",
|
||||
"to_state": "on",
|
||||
"prob_given_true": 0.9,
|
||||
"prob_given_false": 0.4,
|
||||
},
|
||||
],
|
||||
"prior": 0.2,
|
||||
"probability_threshold": 0.32,
|
||||
}
|
||||
}
|
||||
|
||||
await async_setup_component(hass, "binary_sensor", config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_all()) == 1
|
||||
|
||||
assert hass.states.get("binary_sensor.test")
|
||||
|
||||
yaml_path = path.join(
|
||||
_get_fixtures_base_path(),
|
||||
"fixtures",
|
||||
"bayesian/configuration.yaml",
|
||||
)
|
||||
with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path):
|
||||
await hass.services.async_call(
|
||||
DOMAIN,
|
||||
SERVICE_RELOAD,
|
||||
{},
|
||||
blocking=True,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.states.async_all()) == 1
|
||||
|
||||
assert hass.states.get("binary_sensor.test") is None
|
||||
assert hass.states.get("binary_sensor.test2")
|
||||
|
||||
|
||||
def _get_fixtures_base_path():
|
||||
return path.dirname(path.dirname(path.dirname(__file__)))
|
||||
|
10
tests/fixtures/bayesian/configuration.yaml
vendored
Normal file
10
tests/fixtures/bayesian/configuration.yaml
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
binary_sensor:
|
||||
- platform: bayesian
|
||||
prior: 0.1
|
||||
observations:
|
||||
- entity_id: 'switch.kitchen_lights'
|
||||
prob_given_true: 0.6
|
||||
prob_given_false: 0.2
|
||||
platform: 'state'
|
||||
to_state: 'on'
|
||||
name: test2
|
Loading…
x
Reference in New Issue
Block a user