How to use test_binary method in gabbi

Best Python code snippet using gabbi_python

test_binary_sensor.py

Source:test_binary_sensor.py Github

copy

Full Screen

1"""The test for the bayesian sensor platform."""2import json3from unittest.mock import patch4from homeassistant import config as hass_config5from homeassistant.components.bayesian import DOMAIN, binary_sensor as bayesian6from homeassistant.components.homeassistant import (7 DOMAIN as HA_DOMAIN,8 SERVICE_UPDATE_ENTITY,9)10from homeassistant.const import (11 ATTR_ENTITY_ID,12 SERVICE_RELOAD,13 STATE_OFF,14 STATE_ON,15 STATE_UNKNOWN,16)17from homeassistant.core import Context, callback18from homeassistant.setup import async_setup_component19from tests.common import get_fixture_path20async def test_load_values_when_added_to_hass(hass):21 """Test that sensor initializes with observations of relevant entities."""22 config = {23 "binary_sensor": {24 "name": "Test_Binary",25 "platform": "bayesian",26 "observations": [27 {28 "platform": "state",29 "entity_id": "sensor.test_monitored",30 "to_state": "off",31 "prob_given_true": 0.8,32 "prob_given_false": 0.4,33 }34 ],35 "prior": 0.2,36 "probability_threshold": 0.32,37 }38 }39 hass.states.async_set("sensor.test_monitored", "off")40 await hass.async_block_till_done()41 assert await async_setup_component(hass, "binary_sensor", config)42 await hass.async_block_till_done()43 state = hass.states.get("binary_sensor.test_binary")44 assert state.attributes.get("observations")[0]["prob_given_true"] == 0.845 assert state.attributes.get("observations")[0]["prob_given_false"] == 0.446async def test_unknown_state_does_not_influence_probability(hass):47 """Test that an unknown state does not change the output probability."""48 config = {49 "binary_sensor": {50 "name": "Test_Binary",51 "platform": "bayesian",52 "observations": [53 {54 "platform": "state",55 "entity_id": "sensor.test_monitored",56 "to_state": "off",57 "prob_given_true": 0.8,58 "prob_given_false": 0.4,59 }60 ],61 "prior": 0.2,62 "probability_threshold": 0.32,63 }64 }65 hass.states.async_set("sensor.test_monitored", STATE_UNKNOWN)66 await hass.async_block_till_done()67 assert await async_setup_component(hass, "binary_sensor", config)68 await hass.async_block_till_done()69 state = hass.states.get("binary_sensor.test_binary")70 assert state.attributes.get("observations") == []71async def test_sensor_numeric_state(hass):72 """Test sensor on numeric state platform observations."""73 config = {74 "binary_sensor": {75 "platform": "bayesian",76 "name": "Test_Binary",77 "observations": [78 {79 "platform": "numeric_state",80 "entity_id": "sensor.test_monitored",81 "below": 10,82 "above": 5,83 "prob_given_true": 0.6,84 },85 {86 "platform": "numeric_state",87 "entity_id": "sensor.test_monitored1",88 "below": 7,89 "above": 5,90 "prob_given_true": 0.9,91 "prob_given_false": 0.1,92 },93 ],94 "prior": 0.2,95 }96 }97 assert await async_setup_component(hass, "binary_sensor", config)98 await hass.async_block_till_done()99 hass.states.async_set("sensor.test_monitored", 4)100 await hass.async_block_till_done()101 state = hass.states.get("binary_sensor.test_binary")102 assert [] == state.attributes.get("observations")103 assert state.attributes.get("probability") == 0.2104 assert state.state == "off"105 hass.states.async_set("sensor.test_monitored", 6)106 await hass.async_block_till_done()107 hass.states.async_set("sensor.test_monitored", 4)108 await hass.async_block_till_done()109 hass.states.async_set("sensor.test_monitored", 6)110 hass.states.async_set("sensor.test_monitored1", 6)111 await hass.async_block_till_done()112 state = hass.states.get("binary_sensor.test_binary")113 assert state.attributes.get("observations")[0]["prob_given_true"] == 0.6114 assert state.attributes.get("observations")[1]["prob_given_true"] == 0.9115 assert state.attributes.get("observations")[1]["prob_given_false"] == 0.1116 assert round(abs(0.77 - state.attributes.get("probability")), 7) == 0117 assert state.state == "on"118 hass.states.async_set("sensor.test_monitored", 6)119 hass.states.async_set("sensor.test_monitored1", 0)120 await hass.async_block_till_done()121 hass.states.async_set("sensor.test_monitored", 4)122 await hass.async_block_till_done()123 state = hass.states.get("binary_sensor.test_binary")124 assert state.attributes.get("probability") == 0.2125 assert state.state == "off"126 hass.states.async_set("sensor.test_monitored", 15)127 await hass.async_block_till_done()128 state = hass.states.get("binary_sensor.test_binary")129 assert state.state == "off"130async def test_sensor_state(hass):131 """Test sensor on state platform observations."""132 config = {133 "binary_sensor": {134 "name": "Test_Binary",135 "platform": "bayesian",136 "observations": [137 {138 "platform": "state",139 "entity_id": "sensor.test_monitored",140 "to_state": "off",141 "prob_given_true": 0.8,142 "prob_given_false": 0.4,143 }144 ],145 "prior": 0.2,146 "probability_threshold": 0.32,147 }148 }149 assert await async_setup_component(hass, "binary_sensor", config)150 await hass.async_block_till_done()151 hass.states.async_set("sensor.test_monitored", "on")152 state = hass.states.get("binary_sensor.test_binary")153 assert [] == state.attributes.get("observations")154 assert state.attributes.get("probability") == 0.2155 assert state.state == "off"156 hass.states.async_set("sensor.test_monitored", "off")157 await hass.async_block_till_done()158 hass.states.async_set("sensor.test_monitored", "on")159 await hass.async_block_till_done()160 hass.states.async_set("sensor.test_monitored", "off")161 await hass.async_block_till_done()162 state = hass.states.get("binary_sensor.test_binary")163 assert state.attributes.get("observations")[0]["prob_given_true"] == 0.8164 assert state.attributes.get("observations")[0]["prob_given_false"] == 0.4165 assert round(abs(0.33 - state.attributes.get("probability")), 7) == 0166 assert state.state == "on"167 hass.states.async_set("sensor.test_monitored", "off")168 await hass.async_block_till_done()169 hass.states.async_set("sensor.test_monitored", "on")170 await hass.async_block_till_done()171 state = hass.states.get("binary_sensor.test_binary")172 assert round(abs(0.2 - state.attributes.get("probability")), 7) == 0173 assert state.state == "off"174async def test_sensor_value_template(hass):175 """Test sensor on template platform observations."""176 config = {177 "binary_sensor": {178 "name": "Test_Binary",179 "platform": "bayesian",180 "observations": [181 {182 "platform": "template",183 "value_template": "{{states('sensor.test_monitored') == 'off'}}",184 "prob_given_true": 0.8,185 "prob_given_false": 0.4,186 }187 ],188 "prior": 0.2,189 "probability_threshold": 0.32,190 }191 }192 assert await async_setup_component(hass, "binary_sensor", config)193 await hass.async_block_till_done()194 hass.states.async_set("sensor.test_monitored", "on")195 state = hass.states.get("binary_sensor.test_binary")196 assert [] == state.attributes.get("observations")197 assert state.attributes.get("probability") == 0.2198 assert state.state == "off"199 hass.states.async_set("sensor.test_monitored", "off")200 await hass.async_block_till_done()201 hass.states.async_set("sensor.test_monitored", "on")202 await hass.async_block_till_done()203 hass.states.async_set("sensor.test_monitored", "off")204 await hass.async_block_till_done()205 state = hass.states.get("binary_sensor.test_binary")206 assert state.attributes.get("observations")[0]["prob_given_true"] == 0.8207 assert state.attributes.get("observations")[0]["prob_given_false"] == 0.4208 assert round(abs(0.33 - state.attributes.get("probability")), 7) == 0209 assert state.state == "on"210 hass.states.async_set("sensor.test_monitored", "off")211 await hass.async_block_till_done()212 hass.states.async_set("sensor.test_monitored", "on")213 await hass.async_block_till_done()214 state = hass.states.get("binary_sensor.test_binary")215 assert round(abs(0.2 - state.attributes.get("probability")), 7) == 0216 assert state.state == "off"217async def test_threshold(hass):218 """Test sensor on probability threshold limits."""219 config = {220 "binary_sensor": {221 "name": "Test_Binary",222 "platform": "bayesian",223 "observations": [224 {225 "platform": "state",226 "entity_id": "sensor.test_monitored",227 "to_state": "on",228 "prob_given_true": 1.0,229 }230 ],231 "prior": 0.5,232 "probability_threshold": 1.0,233 }234 }235 assert await async_setup_component(hass, "binary_sensor", config)236 await hass.async_block_till_done()237 hass.states.async_set("sensor.test_monitored", "on")238 await hass.async_block_till_done()239 state = hass.states.get("binary_sensor.test_binary")240 assert round(abs(1.0 - state.attributes.get("probability")), 7) == 0241 assert state.state == "on"242async def test_multiple_observations(hass):243 """Test sensor with multiple observations of same entity."""244 config = {245 "binary_sensor": {246 "name": "Test_Binary",247 "platform": "bayesian",248 "observations": [249 {250 "platform": "state",251 "entity_id": "sensor.test_monitored",252 "to_state": "blue",253 "prob_given_true": 0.8,254 "prob_given_false": 0.4,255 },256 {257 "platform": "state",258 "entity_id": "sensor.test_monitored",259 "to_state": "red",260 "prob_given_true": 0.2,261 "prob_given_false": 0.4,262 },263 ],264 "prior": 0.2,265 "probability_threshold": 0.32,266 }267 }268 assert await async_setup_component(hass, "binary_sensor", config)269 await hass.async_block_till_done()270 hass.states.async_set("sensor.test_monitored", "off")271 state = hass.states.get("binary_sensor.test_binary")272 for key, attrs in state.attributes.items():273 json.dumps(attrs)274 assert [] == state.attributes.get("observations")275 assert state.attributes.get("probability") == 0.2276 assert state.state == "off"277 hass.states.async_set("sensor.test_monitored", "blue")278 await hass.async_block_till_done()279 hass.states.async_set("sensor.test_monitored", "off")280 await hass.async_block_till_done()281 hass.states.async_set("sensor.test_monitored", "blue")282 await hass.async_block_till_done()283 state = hass.states.get("binary_sensor.test_binary")284 assert state.attributes.get("observations")[0]["prob_given_true"] == 0.8285 assert state.attributes.get("observations")[0]["prob_given_false"] == 0.4286 assert round(abs(0.33 - state.attributes.get("probability")), 7) == 0287 assert state.state == "on"288 hass.states.async_set("sensor.test_monitored", "blue")289 await hass.async_block_till_done()290 hass.states.async_set("sensor.test_monitored", "red")291 await hass.async_block_till_done()292 state = hass.states.get("binary_sensor.test_binary")293 assert round(abs(0.11 - state.attributes.get("probability")), 7) == 0294 assert state.state == "off"295async def test_probability_updates(hass):296 """Test probability update function."""297 prob_given_true = [0.3, 0.6, 0.8]298 prob_given_false = [0.7, 0.4, 0.2]299 prior = 0.5300 for pt, pf in zip(prob_given_true, prob_given_false):301 prior = bayesian.update_probability(prior, pt, pf)302 assert round(abs(0.720000 - prior), 7) == 0303 prob_given_true = [0.8, 0.3, 0.9]304 prob_given_false = [0.6, 0.4, 0.2]305 prior = 0.7306 for pt, pf in zip(prob_given_true, prob_given_false):307 prior = bayesian.update_probability(prior, pt, pf)308 assert round(abs(0.9130434782608695 - prior), 7) == 0309async def test_observed_entities(hass):310 """Test sensor on observed entities."""311 config = {312 "binary_sensor": {313 "name": "Test_Binary",314 "platform": "bayesian",315 "observations": [316 {317 "platform": "state",318 "entity_id": "sensor.test_monitored",319 "to_state": "off",320 "prob_given_true": 0.9,321 "prob_given_false": 0.4,322 },323 {324 "platform": "template",325 "value_template": "{{is_state('sensor.test_monitored1','on') and is_state('sensor.test_monitored','off')}}",326 "prob_given_true": 0.9,327 },328 ],329 "prior": 0.2,330 "probability_threshold": 0.32,331 }332 }333 assert await async_setup_component(hass, "binary_sensor", config)334 await hass.async_block_till_done()335 hass.states.async_set("sensor.test_monitored", "on")336 await hass.async_block_till_done()337 hass.states.async_set("sensor.test_monitored1", "off")338 await hass.async_block_till_done()339 state = hass.states.get("binary_sensor.test_binary")340 assert [] == state.attributes.get("occurred_observation_entities")341 hass.states.async_set("sensor.test_monitored", "off")342 await hass.async_block_till_done()343 state = hass.states.get("binary_sensor.test_binary")344 assert ["sensor.test_monitored"] == state.attributes.get(345 "occurred_observation_entities"346 )347 hass.states.async_set("sensor.test_monitored1", "on")348 await hass.async_block_till_done()349 state = hass.states.get("binary_sensor.test_binary")350 assert ["sensor.test_monitored", "sensor.test_monitored1"] == sorted(351 state.attributes.get("occurred_observation_entities")352 )353async def test_state_attributes_are_serializable(hass):354 """Test sensor on observed entities."""355 config = {356 "binary_sensor": {357 "name": "Test_Binary",358 "platform": "bayesian",359 "observations": [360 {361 "platform": "state",362 "entity_id": "sensor.test_monitored",363 "to_state": "off",364 "prob_given_true": 0.9,365 "prob_given_false": 0.4,366 },367 {368 "platform": "template",369 "value_template": "{{is_state('sensor.test_monitored1','on') and is_state('sensor.test_monitored','off')}}",370 "prob_given_true": 0.9,371 },372 ],373 "prior": 0.2,374 "probability_threshold": 0.32,375 }376 }377 assert await async_setup_component(hass, "binary_sensor", config)378 await hass.async_block_till_done()379 hass.states.async_set("sensor.test_monitored", "on")380 await hass.async_block_till_done()381 hass.states.async_set("sensor.test_monitored1", "off")382 await hass.async_block_till_done()383 state = hass.states.get("binary_sensor.test_binary")384 assert [] == state.attributes.get("occurred_observation_entities")385 hass.states.async_set("sensor.test_monitored", "off")386 await hass.async_block_till_done()387 state = hass.states.get("binary_sensor.test_binary")388 assert ["sensor.test_monitored"] == state.attributes.get(389 "occurred_observation_entities"390 )391 hass.states.async_set("sensor.test_monitored1", "on")392 await hass.async_block_till_done()393 state = hass.states.get("binary_sensor.test_binary")394 assert ["sensor.test_monitored", "sensor.test_monitored1"] == sorted(395 state.attributes.get("occurred_observation_entities")396 )397 for key, attrs in state.attributes.items():398 json.dumps(attrs)399async def test_template_error(hass, caplog):400 """Test sensor with template error."""401 config = {402 "binary_sensor": {403 "name": "Test_Binary",404 "platform": "bayesian",405 "observations": [406 {407 "platform": "template",408 "value_template": "{{ xyz + 1 }}",409 "prob_given_true": 0.9,410 },411 ],412 "prior": 0.2,413 "probability_threshold": 0.32,414 }415 }416 await async_setup_component(hass, "binary_sensor", config)417 await hass.async_block_till_done()418 assert hass.states.get("binary_sensor.test_binary").state == "off"419 assert "TemplateError" in caplog.text420 assert "xyz" in caplog.text421async def test_update_request_with_template(hass):422 """Test sensor on template platform observations that gets an update request."""423 config = {424 "binary_sensor": {425 "name": "Test_Binary",426 "platform": "bayesian",427 "observations": [428 {429 "platform": "template",430 "value_template": "{{states('sensor.test_monitored') == 'off'}}",431 "prob_given_true": 0.8,432 "prob_given_false": 0.4,433 }434 ],435 "prior": 0.2,436 "probability_threshold": 0.32,437 }438 }439 await async_setup_component(hass, "binary_sensor", config)440 await async_setup_component(hass, HA_DOMAIN, {})441 await hass.async_block_till_done()442 assert hass.states.get("binary_sensor.test_binary").state == "off"443 await hass.services.async_call(444 HA_DOMAIN,445 SERVICE_UPDATE_ENTITY,446 {ATTR_ENTITY_ID: "binary_sensor.test_binary"},447 blocking=True,448 )449 await hass.async_block_till_done()450 assert hass.states.get("binary_sensor.test_binary").state == "off"451async def test_update_request_without_template(hass):452 """Test sensor on template platform observations that gets an update request."""453 config = {454 "binary_sensor": {455 "name": "Test_Binary",456 "platform": "bayesian",457 "observations": [458 {459 "platform": "state",460 "entity_id": "sensor.test_monitored",461 "to_state": "off",462 "prob_given_true": 0.9,463 "prob_given_false": 0.4,464 },465 ],466 "prior": 0.2,467 "probability_threshold": 0.32,468 }469 }470 await async_setup_component(hass, "binary_sensor", config)471 await async_setup_component(hass, HA_DOMAIN, {})472 await hass.async_block_till_done()473 hass.states.async_set("sensor.test_monitored", "on")474 await hass.async_block_till_done()475 assert hass.states.get("binary_sensor.test_binary").state == "off"476 await hass.services.async_call(477 HA_DOMAIN,478 SERVICE_UPDATE_ENTITY,479 {ATTR_ENTITY_ID: "binary_sensor.test_binary"},480 blocking=True,481 )482 await hass.async_block_till_done()483 assert hass.states.get("binary_sensor.test_binary").state == "off"484async def test_monitored_sensor_goes_away(hass):485 """Test sensor on template platform observations that goes away."""486 config = {487 "binary_sensor": {488 "name": "Test_Binary",489 "platform": "bayesian",490 "observations": [491 {492 "platform": "state",493 "entity_id": "sensor.test_monitored",494 "to_state": "on",495 "prob_given_true": 0.9,496 "prob_given_false": 0.4,497 },498 ],499 "prior": 0.2,500 "probability_threshold": 0.32,501 }502 }503 await async_setup_component(hass, "binary_sensor", config)504 await async_setup_component(hass, HA_DOMAIN, {})505 await hass.async_block_till_done()506 hass.states.async_set("sensor.test_monitored", "on")507 await hass.async_block_till_done()508 assert hass.states.get("binary_sensor.test_binary").state == "on"509 hass.states.async_remove("sensor.test_monitored")510 await hass.async_block_till_done()511 assert hass.states.get("binary_sensor.test_binary").state == "on"512async def test_reload(hass):513 """Verify we can reload bayesian sensors."""514 config = {515 "binary_sensor": {516 "name": "test",517 "platform": "bayesian",518 "observations": [519 {520 "platform": "state",521 "entity_id": "sensor.test_monitored",522 "to_state": "on",523 "prob_given_true": 0.9,524 "prob_given_false": 0.4,525 },526 ],527 "prior": 0.2,528 "probability_threshold": 0.32,529 }530 }531 await async_setup_component(hass, "binary_sensor", config)532 await hass.async_block_till_done()533 assert len(hass.states.async_all()) == 1534 assert hass.states.get("binary_sensor.test")535 yaml_path = get_fixture_path("configuration.yaml", "bayesian")536 with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path):537 await hass.services.async_call(538 DOMAIN,539 SERVICE_RELOAD,540 {},541 blocking=True,542 )543 await hass.async_block_till_done()544 assert len(hass.states.async_all()) == 1545 assert hass.states.get("binary_sensor.test") is None546 assert hass.states.get("binary_sensor.test2")547async def test_template_triggers(hass):548 """Test sensor with template triggers."""549 hass.states.async_set("input_boolean.test", STATE_OFF)550 config = {551 "binary_sensor": {552 "name": "Test_Binary",553 "platform": "bayesian",554 "observations": [555 {556 "platform": "template",557 "value_template": "{{ states.input_boolean.test.state }}",558 "prob_given_true": 1999.9,559 },560 ],561 "prior": 0.2,562 "probability_threshold": 0.32,563 }564 }565 await async_setup_component(hass, "binary_sensor", config)566 await hass.async_block_till_done()567 assert hass.states.get("binary_sensor.test_binary").state == STATE_OFF568 events = []569 hass.helpers.event.async_track_state_change_event(570 "binary_sensor.test_binary", callback(lambda event: events.append(event))571 )572 context = Context()573 hass.states.async_set("input_boolean.test", STATE_ON, context=context)574 await hass.async_block_till_done()575 await hass.async_block_till_done()576 assert events[0].context == context577async def test_state_triggers(hass):578 """Test sensor with state triggers."""579 hass.states.async_set("sensor.test_monitored", STATE_OFF)580 config = {581 "binary_sensor": {582 "name": "Test_Binary",583 "platform": "bayesian",584 "observations": [585 {586 "platform": "state",587 "entity_id": "sensor.test_monitored",588 "to_state": "off",589 "prob_given_true": 999.9,590 "prob_given_false": 999.4,591 },592 ],593 "prior": 0.2,594 "probability_threshold": 0.32,595 }596 }597 await async_setup_component(hass, "binary_sensor", config)598 await hass.async_block_till_done()599 assert hass.states.get("binary_sensor.test_binary").state == STATE_OFF600 events = []601 hass.helpers.event.async_track_state_change_event(602 "binary_sensor.test_binary", callback(lambda event: events.append(event))603 )604 context = Context()605 hass.states.async_set("sensor.test_monitored", STATE_ON, context=context)606 await hass.async_block_till_done()607 await hass.async_block_till_done()...

Full Screen

Full Screen

test_bayesian.py

Source:test_bayesian.py Github

copy

Full Screen

1"""The test for the bayesian sensor platform."""2import unittest3from homeassistant.setup import setup_component4from homeassistant.components.binary_sensor import bayesian5from tests.common import get_test_home_assistant6class TestBayesianBinarySensor(unittest.TestCase):7 """Test the threshold sensor."""8 def setup_method(self, method):9 """Set up things to be run when tests are started."""10 self.hass = get_test_home_assistant()11 def teardown_method(self, method):12 """Stop everything that was started."""13 self.hass.stop()14 def test_sensor_numeric_state(self):15 """Test sensor on numeric state platform observations."""16 config = {17 'binary_sensor': {18 'platform':19 'bayesian',20 'name':21 'Test_Binary',22 'observations': [{23 'platform': 'numeric_state',24 'entity_id': 'sensor.test_monitored',25 'below': 10,26 'above': 5,27 'prob_given_true': 0.628 }, {29 'platform': 'numeric_state',30 'entity_id': 'sensor.test_monitored1',31 'below': 7,32 'above': 5,33 'prob_given_true': 0.9,34 'prob_given_false': 0.135 }],36 'prior':37 0.2,38 }39 }40 assert setup_component(self.hass, 'binary_sensor', config)41 self.hass.states.set('sensor.test_monitored', 4)42 self.hass.block_till_done()43 state = self.hass.states.get('binary_sensor.test_binary')44 self.assertEqual([], state.attributes.get('observations'))45 self.assertEqual(0.2, state.attributes.get('probability'))46 assert state.state == 'off'47 self.hass.states.set('sensor.test_monitored', 6)48 self.hass.block_till_done()49 self.hass.states.set('sensor.test_monitored', 4)50 self.hass.block_till_done()51 self.hass.states.set('sensor.test_monitored', 6)52 self.hass.states.set('sensor.test_monitored1', 6)53 self.hass.block_till_done()54 state = self.hass.states.get('binary_sensor.test_binary')55 self.assertEqual([{56 'prob_false': 0.4,57 'prob_true': 0.658 }, {59 'prob_false': 0.1,60 'prob_true': 0.961 }], state.attributes.get('observations'))62 self.assertAlmostEqual(0.77, state.attributes.get('probability'))63 assert state.state == 'on'64 self.hass.states.set('sensor.test_monitored', 6)65 self.hass.states.set('sensor.test_monitored1', 0)66 self.hass.block_till_done()67 self.hass.states.set('sensor.test_monitored', 4)68 self.hass.block_till_done()69 state = self.hass.states.get('binary_sensor.test_binary')70 self.assertEqual(0.2, state.attributes.get('probability'))71 assert state.state == 'off'72 self.hass.states.set('sensor.test_monitored', 15)73 self.hass.block_till_done()74 state = self.hass.states.get('binary_sensor.test_binary')75 assert state.state == 'off'76 def test_sensor_state(self):77 """Test sensor on state platform observations."""78 config = {79 'binary_sensor': {80 'name':81 'Test_Binary',82 'platform':83 'bayesian',84 'observations': [{85 'platform': 'state',86 'entity_id': 'sensor.test_monitored',87 'to_state': 'off',88 'prob_given_true': 0.8,89 'prob_given_false': 0.490 }],91 'prior':92 0.2,93 'probability_threshold':94 0.32,95 }96 }97 assert setup_component(self.hass, 'binary_sensor', config)98 self.hass.states.set('sensor.test_monitored', 'on')99 state = self.hass.states.get('binary_sensor.test_binary')100 self.assertEqual([], state.attributes.get('observations'))101 self.assertEqual(0.2, state.attributes.get('probability'))102 assert state.state == 'off'103 self.hass.states.set('sensor.test_monitored', 'off')104 self.hass.block_till_done()105 self.hass.states.set('sensor.test_monitored', 'on')106 self.hass.block_till_done()107 self.hass.states.set('sensor.test_monitored', 'off')108 self.hass.block_till_done()109 state = self.hass.states.get('binary_sensor.test_binary')110 self.assertEqual([{111 'prob_true': 0.8,112 'prob_false': 0.4113 }], state.attributes.get('observations'))114 self.assertAlmostEqual(0.33, state.attributes.get('probability'))115 assert state.state == 'on'116 self.hass.states.set('sensor.test_monitored', 'off')117 self.hass.block_till_done()118 self.hass.states.set('sensor.test_monitored', 'on')119 self.hass.block_till_done()120 state = self.hass.states.get('binary_sensor.test_binary')121 self.assertAlmostEqual(0.2, state.attributes.get('probability'))122 assert state.state == 'off'123 def test_threshold(self):124 """Test sensor on probabilty threshold limits."""125 config = {126 'binary_sensor': {127 'name':128 'Test_Binary',129 'platform':130 'bayesian',131 'observations': [{132 'platform': 'state',133 'entity_id': 'sensor.test_monitored',134 'to_state': 'on',135 'prob_given_true': 1.0,136 }],137 'prior':138 0.5,139 'probability_threshold':140 1.0,141 }142 }143 assert setup_component(self.hass, 'binary_sensor', config)144 self.hass.states.set('sensor.test_monitored', 'on')145 self.hass.block_till_done()146 state = self.hass.states.get('binary_sensor.test_binary')147 self.assertAlmostEqual(1.0, state.attributes.get('probability'))148 assert state.state == 'on'149 def test_multiple_observations(self):150 """Test sensor with multiple observations of same entity."""151 config = {152 'binary_sensor': {153 'name':154 'Test_Binary',155 'platform':156 'bayesian',157 'observations': [{158 'platform': 'state',159 'entity_id': 'sensor.test_monitored',160 'to_state': 'blue',161 'prob_given_true': 0.8,162 'prob_given_false': 0.4163 }, {164 'platform': 'state',165 'entity_id': 'sensor.test_monitored',166 'to_state': 'red',167 'prob_given_true': 0.2,168 'prob_given_false': 0.4169 }],170 'prior':171 0.2,172 'probability_threshold':173 0.32,174 }175 }176 assert setup_component(self.hass, 'binary_sensor', config)177 self.hass.states.set('sensor.test_monitored', 'off')178 state = self.hass.states.get('binary_sensor.test_binary')179 self.assertEqual([], state.attributes.get('observations'))180 self.assertEqual(0.2, state.attributes.get('probability'))181 assert state.state == 'off'182 self.hass.states.set('sensor.test_monitored', 'blue')183 self.hass.block_till_done()184 self.hass.states.set('sensor.test_monitored', 'off')185 self.hass.block_till_done()186 self.hass.states.set('sensor.test_monitored', 'blue')187 self.hass.block_till_done()188 state = self.hass.states.get('binary_sensor.test_binary')189 self.assertEqual([{190 'prob_true': 0.8,191 'prob_false': 0.4192 }], state.attributes.get('observations'))193 self.assertAlmostEqual(0.33, state.attributes.get('probability'))194 assert state.state == 'on'195 self.hass.states.set('sensor.test_monitored', 'blue')196 self.hass.block_till_done()197 self.hass.states.set('sensor.test_monitored', 'red')198 self.hass.block_till_done()199 state = self.hass.states.get('binary_sensor.test_binary')200 self.assertAlmostEqual(0.11, state.attributes.get('probability'))201 assert state.state == 'off'202 def test_probability_updates(self):203 """Test probability update function."""204 prob_true = [0.3, 0.6, 0.8]205 prob_false = [0.7, 0.4, 0.2]206 prior = 0.5207 for pt, pf in zip(prob_true, prob_false):208 prior = bayesian.update_probability(prior, pt, pf)209 self.assertAlmostEqual(0.720000, prior)210 prob_true = [0.8, 0.3, 0.9]211 prob_false = [0.6, 0.4, 0.2]212 prior = 0.7213 for pt, pf in zip(prob_true, prob_false):214 prior = bayesian.update_probability(prior, pt, pf)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run gabbi automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful