diff --git a/homeassistant/components/bayesian/__init__.py b/homeassistant/components/bayesian/__init__.py index 971ff8427ac..485592dc5e4 100644 --- a/homeassistant/components/bayesian/__init__.py +++ b/homeassistant/components/bayesian/__init__.py @@ -1 +1,4 @@ """The bayesian component.""" + +DOMAIN = "bayesian" +PLATFORMS = ["binary_sensor"] diff --git a/homeassistant/components/bayesian/binary_sensor.py b/homeassistant/components/bayesian/binary_sensor.py index 8d4dab62263..90540e456c5 100644 --- a/homeassistant/components/bayesian/binary_sensor.py +++ b/homeassistant/components/bayesian/binary_sensor.py @@ -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] diff --git a/homeassistant/components/bayesian/services.yaml b/homeassistant/components/bayesian/services.yaml new file mode 100644 index 00000000000..ec7313a8630 --- /dev/null +++ b/homeassistant/components/bayesian/services.yaml @@ -0,0 +1,2 @@ +reload: + description: Reload all bayesian entities. diff --git a/tests/components/bayesian/test_binary_sensor.py b/tests/components/bayesian/test_binary_sensor.py index 9e4983ab4d5..57c7f404c7f 100644 --- a/tests/components/bayesian/test_binary_sensor.py +++ b/tests/components/bayesian/test_binary_sensor.py @@ -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__))) diff --git a/tests/fixtures/bayesian/configuration.yaml b/tests/fixtures/bayesian/configuration.yaml new file mode 100644 index 00000000000..56a490d4aec --- /dev/null +++ b/tests/fixtures/bayesian/configuration.yaml @@ -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