Skip to content

Commit 8de200d

Browse files
HarvsGemontnemery
authored andcommitted
Fix Bayesian ConfigFlow templates in 2025.10 (home-assistant#153289)
Co-authored-by: Erik Montnemery <[email protected]>
1 parent f242e29 commit 8de200d

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

homeassistant/components/bayesian/binary_sensor.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,13 @@ async def async_setup_entry(
272272
observations: list[ConfigType] = [
273273
dict(subentry.data) for subentry in config_entry.subentries.values()
274274
]
275+
276+
for observation in observations:
277+
if observation[CONF_PLATFORM] == CONF_TEMPLATE:
278+
observation[CONF_VALUE_TEMPLATE] = Template(
279+
observation[CONF_VALUE_TEMPLATE], hass
280+
)
281+
275282
prior: float = config[CONF_PRIOR]
276283
probability_threshold: float = config[CONF_PROBABILITY_THRESHOLD]
277284
device_class: BinarySensorDeviceClass | None = config.get(CONF_DEVICE_CLASS)

tests/components/bayesian/test_binary_sensor.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
DOMAIN as HA_DOMAIN,
1414
SERVICE_UPDATE_ENTITY,
1515
)
16+
from homeassistant.config_entries import ConfigSubentryData
1617
from homeassistant.const import (
1718
ATTR_ENTITY_ID,
1819
SERVICE_RELOAD,
@@ -26,7 +27,7 @@
2627
from homeassistant.helpers.event import async_track_state_change_event
2728
from homeassistant.setup import async_setup_component
2829

29-
from tests.common import get_fixture_path
30+
from tests.common import MockConfigEntry, get_fixture_path
3031

3132

3233
async def test_load_values_when_added_to_hass(hass: HomeAssistant) -> None:
@@ -295,6 +296,44 @@ async def test_sensor_value_template(hass: HomeAssistant) -> None:
295296
assert await async_setup_component(hass, "binary_sensor", config)
296297
await hass.async_block_till_done()
297298

299+
await _test_sensor_value_template(hass)
300+
301+
302+
async def test_sensor_value_template_config_entry(hass: HomeAssistant) -> None:
303+
"""Test sensor on template platform observations."""
304+
template_config_entry = MockConfigEntry(
305+
data={},
306+
domain=DOMAIN,
307+
options={
308+
"name": "Test_Binary",
309+
"prior": 0.2,
310+
"probability_threshold": 0.32,
311+
},
312+
subentries_data=[
313+
ConfigSubentryData(
314+
data={
315+
"platform": "template",
316+
"value_template": "{{states('sensor.test_monitored') == 'off'}}",
317+
"prob_given_true": 0.8,
318+
"prob_given_false": 0.4,
319+
"name": "observation_1",
320+
},
321+
subentry_type="observation",
322+
title="observation_1",
323+
unique_id=None,
324+
)
325+
],
326+
title="Test_Binary",
327+
)
328+
template_config_entry.add_to_hass(hass)
329+
330+
assert await hass.config_entries.async_setup(template_config_entry.entry_id)
331+
await hass.async_block_till_done()
332+
333+
await _test_sensor_value_template(hass)
334+
335+
336+
async def _test_sensor_value_template(hass: HomeAssistant) -> None:
298337
hass.states.async_set("sensor.test_monitored", "on")
299338

300339
state = hass.states.get("binary_sensor.test_binary")

0 commit comments

Comments
 (0)