init home-assistant
This commit is contained in:
1
home-assistant/tests/components/threshold/__init__.py
Normal file
1
home-assistant/tests/components/threshold/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests for threshold component."""
|
||||
@@ -0,0 +1,47 @@
|
||||
# serializer version: 1
|
||||
# name: test_config_flow_preview_success[missing_entity_id]
|
||||
dict({
|
||||
'attributes': dict({
|
||||
'friendly_name': '',
|
||||
}),
|
||||
'state': 'unavailable',
|
||||
})
|
||||
# ---
|
||||
# name: test_config_flow_preview_success[missing_upper_lower]
|
||||
dict({
|
||||
'attributes': dict({
|
||||
'friendly_name': 'Test Sensor',
|
||||
}),
|
||||
'state': 'unavailable',
|
||||
})
|
||||
# ---
|
||||
# name: test_config_flow_preview_success[success]
|
||||
dict({
|
||||
'attributes': dict({
|
||||
'entity_id': 'sensor.test_monitored',
|
||||
'friendly_name': 'Test Sensor',
|
||||
'hysteresis': 0.0,
|
||||
'lower': 20.0,
|
||||
'position': 'below',
|
||||
'sensor_value': 16.0,
|
||||
'type': 'lower',
|
||||
'upper': None,
|
||||
}),
|
||||
'state': 'on',
|
||||
})
|
||||
# ---
|
||||
# name: test_options_flow_preview
|
||||
dict({
|
||||
'attributes': dict({
|
||||
'entity_id': 'sensor.test_monitored',
|
||||
'friendly_name': 'Test Sensor',
|
||||
'hysteresis': 0.0,
|
||||
'lower': 20.0,
|
||||
'position': 'below',
|
||||
'sensor_value': 16.0,
|
||||
'type': 'lower',
|
||||
'upper': None,
|
||||
}),
|
||||
'state': 'on',
|
||||
})
|
||||
# ---
|
||||
587
home-assistant/tests/components/threshold/test_binary_sensor.py
Normal file
587
home-assistant/tests/components/threshold/test_binary_sensor.py
Normal file
@@ -0,0 +1,587 @@
|
||||
"""The test for the threshold sensor platform."""
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.threshold.const import (
|
||||
ATTR_HYSTERESIS,
|
||||
ATTR_LOWER,
|
||||
ATTR_POSITION,
|
||||
ATTR_SENSOR_VALUE,
|
||||
ATTR_TYPE,
|
||||
ATTR_UPPER,
|
||||
CONF_HYSTERESIS,
|
||||
CONF_LOWER,
|
||||
CONF_UPPER,
|
||||
DOMAIN,
|
||||
POSITION_ABOVE,
|
||||
POSITION_BELOW,
|
||||
POSITION_IN_RANGE,
|
||||
POSITION_UNKNOWN,
|
||||
TYPE_LOWER,
|
||||
TYPE_RANGE,
|
||||
TYPE_UPPER,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_UNIT_OF_MEASUREMENT,
|
||||
CONF_ENTITY_ID,
|
||||
CONF_NAME,
|
||||
CONF_PLATFORM,
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
STATE_UNAVAILABLE,
|
||||
STATE_UNKNOWN,
|
||||
Platform,
|
||||
UnitOfTemperature,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("vals", "expected_position", "expected_state"),
|
||||
[
|
||||
([15], POSITION_BELOW, STATE_OFF), # at threshold
|
||||
([15, 16], POSITION_ABOVE, STATE_ON),
|
||||
([15, 16, 14], POSITION_BELOW, STATE_OFF),
|
||||
([15, 16, 14, 15], POSITION_BELOW, STATE_OFF), # below -> threshold
|
||||
([15, 16, 14, 15, "cat"], POSITION_UNKNOWN, STATE_UNKNOWN),
|
||||
([15, 16, 14, 15, "cat", 15], POSITION_BELOW, STATE_OFF),
|
||||
([15, None], POSITION_UNKNOWN, STATE_UNKNOWN),
|
||||
],
|
||||
)
|
||||
async def test_sensor_upper(
|
||||
hass: HomeAssistant,
|
||||
vals: list[float | str | None],
|
||||
expected_position: str,
|
||||
expected_state: str,
|
||||
) -> None:
|
||||
"""Test if source is above threshold."""
|
||||
config = {
|
||||
Platform.BINARY_SENSOR: {
|
||||
CONF_PLATFORM: "threshold",
|
||||
CONF_UPPER: "15",
|
||||
CONF_ENTITY_ID: "sensor.test_monitored",
|
||||
}
|
||||
}
|
||||
|
||||
assert await async_setup_component(hass, Platform.BINARY_SENSOR, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes[ATTR_ENTITY_ID] == "sensor.test_monitored"
|
||||
assert state.attributes[ATTR_UPPER] == float(
|
||||
config[Platform.BINARY_SENSOR][CONF_UPPER]
|
||||
)
|
||||
assert state.attributes[ATTR_HYSTERESIS] == 0.0
|
||||
assert state.attributes[ATTR_TYPE] == TYPE_UPPER
|
||||
|
||||
for val in vals:
|
||||
hass.states.async_set("sensor.test_monitored", val)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes[ATTR_POSITION] == expected_position
|
||||
assert state.state == expected_state
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("vals", "expected_position", "expected_state"),
|
||||
[
|
||||
([15], POSITION_ABOVE, STATE_OFF), # at threshold
|
||||
([15, 16], POSITION_ABOVE, STATE_OFF),
|
||||
([15, 16, 14], POSITION_BELOW, STATE_ON),
|
||||
([15, 16, 14, 15], POSITION_BELOW, STATE_ON),
|
||||
([15, 16, 14, 15, "cat"], POSITION_UNKNOWN, STATE_UNKNOWN),
|
||||
([15, 16, 14, 15, "cat", 15], POSITION_ABOVE, STATE_OFF),
|
||||
([15, None], POSITION_UNKNOWN, STATE_UNKNOWN),
|
||||
],
|
||||
)
|
||||
async def test_sensor_lower(
|
||||
hass: HomeAssistant,
|
||||
vals: list[float | str | None],
|
||||
expected_position: str,
|
||||
expected_state: str,
|
||||
) -> None:
|
||||
"""Test if source is below threshold."""
|
||||
config = {
|
||||
Platform.BINARY_SENSOR: {
|
||||
CONF_PLATFORM: "threshold",
|
||||
CONF_LOWER: "15",
|
||||
CONF_ENTITY_ID: "sensor.test_monitored",
|
||||
}
|
||||
}
|
||||
|
||||
assert await async_setup_component(hass, Platform.BINARY_SENSOR, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes[ATTR_ENTITY_ID] == "sensor.test_monitored"
|
||||
assert state.attributes[ATTR_LOWER] == float(
|
||||
config[Platform.BINARY_SENSOR][CONF_LOWER]
|
||||
)
|
||||
assert state.attributes[ATTR_HYSTERESIS] == 0.0
|
||||
assert state.attributes[ATTR_TYPE] == TYPE_LOWER
|
||||
|
||||
for val in vals:
|
||||
hass.states.async_set("sensor.test_monitored", val)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes[ATTR_POSITION] == expected_position
|
||||
assert state.state == expected_state
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("vals", "expected_position", "expected_state"),
|
||||
[
|
||||
([17.5], POSITION_BELOW, STATE_OFF), # threshold + hysteresis
|
||||
([17.5, 12.5], POSITION_BELOW, STATE_OFF), # threshold - hysteresis
|
||||
([17.5, 12.5, 20], POSITION_ABOVE, STATE_ON),
|
||||
([17.5, 12.5, 20, 13], POSITION_ABOVE, STATE_ON),
|
||||
([17.5, 12.5, 20, 13, 12], POSITION_BELOW, STATE_OFF),
|
||||
([17.5, 12.5, 20, 13, 12, 17], POSITION_BELOW, STATE_OFF),
|
||||
([17.5, 12.5, 20, 13, 12, 17, 18], POSITION_ABOVE, STATE_ON),
|
||||
([17.5, 12.5, 20, 13, 12, 17, 18, "cat"], POSITION_UNKNOWN, STATE_UNKNOWN),
|
||||
([17.5, 12.5, 20, 13, 12, 17, 18, "cat", 18], POSITION_ABOVE, STATE_ON),
|
||||
([18, None], POSITION_UNKNOWN, STATE_UNKNOWN),
|
||||
# below within -> above
|
||||
([14, 17.6], POSITION_ABOVE, STATE_ON),
|
||||
# above within -> below
|
||||
([16, 12.4], POSITION_BELOW, STATE_OFF),
|
||||
# below within -> above within
|
||||
([14, 16], POSITION_BELOW, STATE_OFF),
|
||||
# above within -> below within
|
||||
([16, 14], POSITION_BELOW, STATE_OFF),
|
||||
# above -> above within -> below within
|
||||
([20, 16, 14], POSITION_ABOVE, STATE_ON),
|
||||
# below -> below within -> above within
|
||||
([10, 14, 16], POSITION_BELOW, STATE_OFF),
|
||||
],
|
||||
)
|
||||
async def test_sensor_upper_hysteresis(
|
||||
hass: HomeAssistant,
|
||||
vals: list[float | str | None],
|
||||
expected_position: str,
|
||||
expected_state: str,
|
||||
) -> None:
|
||||
"""Test if source is above threshold using hysteresis."""
|
||||
config = {
|
||||
Platform.BINARY_SENSOR: {
|
||||
CONF_PLATFORM: "threshold",
|
||||
CONF_UPPER: "15",
|
||||
CONF_HYSTERESIS: "2.5",
|
||||
CONF_ENTITY_ID: "sensor.test_monitored",
|
||||
}
|
||||
}
|
||||
|
||||
assert await async_setup_component(hass, Platform.BINARY_SENSOR, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes[ATTR_ENTITY_ID] == "sensor.test_monitored"
|
||||
assert state.attributes[ATTR_UPPER] == float(
|
||||
config[Platform.BINARY_SENSOR][CONF_UPPER]
|
||||
)
|
||||
assert state.attributes[ATTR_HYSTERESIS] == 2.5
|
||||
assert state.attributes[ATTR_TYPE] == TYPE_UPPER
|
||||
|
||||
for val in vals:
|
||||
hass.states.async_set("sensor.test_monitored", val)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes[ATTR_POSITION] == expected_position
|
||||
assert state.state == expected_state
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("vals", "expected_position", "expected_state"),
|
||||
[
|
||||
([17.5], POSITION_ABOVE, STATE_OFF), # threshold + hysteresis
|
||||
([17.5, 12.5], POSITION_ABOVE, STATE_OFF), # threshold - hysteresis
|
||||
([17.5, 12.5, 20], POSITION_ABOVE, STATE_OFF),
|
||||
([17.5, 12.5, 20, 13], POSITION_ABOVE, STATE_OFF),
|
||||
([17.5, 12.5, 20, 13, 12], POSITION_BELOW, STATE_ON),
|
||||
([17.5, 12.5, 20, 13, 12, 17], POSITION_BELOW, STATE_ON),
|
||||
([17.5, 12.5, 20, 13, 12, 17, 18], POSITION_ABOVE, STATE_OFF),
|
||||
([17.5, 12.5, 20, 13, 12, 17, 18, "cat"], POSITION_UNKNOWN, STATE_UNKNOWN),
|
||||
([17.5, 12.5, 20, 13, 12, 17, 18, "cat", 18], POSITION_ABOVE, STATE_OFF),
|
||||
([18, None], POSITION_UNKNOWN, STATE_UNKNOWN),
|
||||
# below within -> above
|
||||
([14, 17.6], POSITION_ABOVE, STATE_OFF),
|
||||
# above within -> below
|
||||
([16, 12.4], POSITION_BELOW, STATE_ON),
|
||||
# below within -> above within
|
||||
([14, 16], POSITION_ABOVE, STATE_OFF),
|
||||
# above within -> below within
|
||||
([16, 14], POSITION_ABOVE, STATE_OFF),
|
||||
# above -> above within -> below within
|
||||
([20, 16, 14], POSITION_ABOVE, STATE_OFF),
|
||||
# below -> below within -> above within
|
||||
([10, 14, 16], POSITION_BELOW, STATE_ON),
|
||||
],
|
||||
)
|
||||
async def test_sensor_lower_hysteresis(
|
||||
hass: HomeAssistant,
|
||||
vals: list[float | str | None],
|
||||
expected_position: str,
|
||||
expected_state: str,
|
||||
) -> None:
|
||||
"""Test if source is below threshold using hysteresis."""
|
||||
config = {
|
||||
Platform.BINARY_SENSOR: {
|
||||
CONF_PLATFORM: "threshold",
|
||||
CONF_LOWER: "15",
|
||||
CONF_HYSTERESIS: "2.5",
|
||||
CONF_ENTITY_ID: "sensor.test_monitored",
|
||||
}
|
||||
}
|
||||
|
||||
assert await async_setup_component(hass, Platform.BINARY_SENSOR, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes[ATTR_ENTITY_ID] == "sensor.test_monitored"
|
||||
assert state.attributes[ATTR_LOWER] == float(
|
||||
config[Platform.BINARY_SENSOR][CONF_LOWER]
|
||||
)
|
||||
assert state.attributes[ATTR_HYSTERESIS] == 2.5
|
||||
assert state.attributes[ATTR_TYPE] == TYPE_LOWER
|
||||
|
||||
for val in vals:
|
||||
hass.states.async_set("sensor.test_monitored", val)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes[ATTR_POSITION] == expected_position
|
||||
assert state.state == expected_state
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("vals", "expected_position", "expected_state"),
|
||||
[
|
||||
([10], POSITION_IN_RANGE, STATE_ON), # at lower threshold
|
||||
([10, 20], POSITION_IN_RANGE, STATE_ON), # lower threshold -> upper threshold
|
||||
([10, 20, 16], POSITION_IN_RANGE, STATE_ON),
|
||||
([10, 20, 16, 9], POSITION_BELOW, STATE_OFF),
|
||||
([10, 20, 16, 9, 21], POSITION_ABOVE, STATE_OFF),
|
||||
([10, 20, 16, 9, 21, "cat"], POSITION_UNKNOWN, STATE_UNKNOWN),
|
||||
([10, 20, 16, 9, 21, "cat", 21], POSITION_ABOVE, STATE_OFF),
|
||||
([21, None], POSITION_UNKNOWN, STATE_UNKNOWN),
|
||||
# upper threshold -> lower threshold
|
||||
([20, 10], POSITION_IN_RANGE, STATE_ON),
|
||||
# in-range -> upper threshold
|
||||
([15, 20], POSITION_IN_RANGE, STATE_ON),
|
||||
# in-range -> lower threshold
|
||||
([15, 10], POSITION_IN_RANGE, STATE_ON),
|
||||
# below -> above
|
||||
([5, 25], POSITION_ABOVE, STATE_OFF),
|
||||
# above -> below
|
||||
([25, 5], POSITION_BELOW, STATE_OFF),
|
||||
# in-range -> above
|
||||
([15, 25], POSITION_ABOVE, STATE_OFF),
|
||||
# in-range -> below
|
||||
([15, 5], POSITION_BELOW, STATE_OFF),
|
||||
],
|
||||
)
|
||||
async def test_sensor_in_range_no_hysteresis(
|
||||
hass: HomeAssistant,
|
||||
vals: list[float | str | None],
|
||||
expected_position: str,
|
||||
expected_state: str,
|
||||
) -> None:
|
||||
"""Test if source is within the range."""
|
||||
config = {
|
||||
Platform.BINARY_SENSOR: {
|
||||
CONF_PLATFORM: "threshold",
|
||||
CONF_LOWER: "10",
|
||||
CONF_UPPER: "20",
|
||||
CONF_ENTITY_ID: "sensor.test_monitored",
|
||||
}
|
||||
}
|
||||
|
||||
assert await async_setup_component(hass, Platform.BINARY_SENSOR, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes[ATTR_ENTITY_ID] == "sensor.test_monitored"
|
||||
assert state.attributes[ATTR_LOWER] == float(
|
||||
config[Platform.BINARY_SENSOR][CONF_LOWER]
|
||||
)
|
||||
assert state.attributes[ATTR_UPPER] == float(
|
||||
config[Platform.BINARY_SENSOR][CONF_UPPER]
|
||||
)
|
||||
assert state.attributes[ATTR_HYSTERESIS] == 0.0
|
||||
assert state.attributes[ATTR_TYPE] == TYPE_RANGE
|
||||
|
||||
for val in vals:
|
||||
hass.states.async_set("sensor.test_monitored", val)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes[ATTR_POSITION] == expected_position
|
||||
assert state.state == expected_state
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("vals", "expected_position", "expected_state"),
|
||||
[
|
||||
([12], POSITION_IN_RANGE, STATE_ON), # lower threshold + hysteresis
|
||||
([12, 22], POSITION_IN_RANGE, STATE_ON), # upper threshold + hysteresis
|
||||
([12, 22, 18], POSITION_IN_RANGE, STATE_ON), # upper threshold - hysteresis
|
||||
([12, 22, 18, 16], POSITION_IN_RANGE, STATE_ON),
|
||||
([12, 22, 18, 16, 8], POSITION_IN_RANGE, STATE_ON),
|
||||
([12, 22, 18, 16, 8, 7], POSITION_BELOW, STATE_OFF),
|
||||
([12, 22, 18, 16, 8, 7, 12], POSITION_BELOW, STATE_OFF),
|
||||
([12, 22, 18, 16, 8, 7, 12, 13], POSITION_IN_RANGE, STATE_ON),
|
||||
([12, 22, 18, 16, 8, 7, 12, 13, 22], POSITION_IN_RANGE, STATE_ON),
|
||||
([12, 22, 18, 16, 8, 7, 12, 13, 22, 23], POSITION_ABOVE, STATE_OFF),
|
||||
([12, 22, 18, 16, 8, 7, 12, 13, 22, 23, 18], POSITION_ABOVE, STATE_OFF),
|
||||
([12, 22, 18, 16, 8, 7, 12, 13, 22, 23, 18, 17], POSITION_IN_RANGE, STATE_ON),
|
||||
(
|
||||
[12, 22, 18, 16, 8, 7, 12, 13, 22, 23, 18, 17, "cat"],
|
||||
POSITION_UNKNOWN,
|
||||
STATE_UNKNOWN,
|
||||
),
|
||||
(
|
||||
[12, 22, 18, 16, 8, 7, 12, 13, 22, 23, 18, 17, "cat", 17],
|
||||
POSITION_IN_RANGE,
|
||||
STATE_ON,
|
||||
),
|
||||
([17, None], POSITION_UNKNOWN, STATE_UNKNOWN),
|
||||
# upper threshold -> lower threshold
|
||||
([20, 10], POSITION_IN_RANGE, STATE_ON),
|
||||
# in-range -> upper threshold
|
||||
([15, 20], POSITION_IN_RANGE, STATE_ON),
|
||||
# in-range -> lower threshold
|
||||
([15, 10], POSITION_IN_RANGE, STATE_ON),
|
||||
# below -> above
|
||||
([5, 25], POSITION_ABOVE, STATE_OFF),
|
||||
# above -> below
|
||||
([25, 5], POSITION_BELOW, STATE_OFF),
|
||||
# in-range -> above
|
||||
([15, 25], POSITION_ABOVE, STATE_OFF),
|
||||
# in-range -> below
|
||||
([15, 5], POSITION_BELOW, STATE_OFF),
|
||||
# below -> lower threshold
|
||||
([5, 10], POSITION_BELOW, STATE_OFF),
|
||||
# below -> in-range -> lower threshold
|
||||
([5, 15, 10], POSITION_IN_RANGE, STATE_ON),
|
||||
# above -> upper threshold
|
||||
([25, 20], POSITION_ABOVE, STATE_OFF),
|
||||
# above -> in-range -> upper threshold
|
||||
([25, 15, 20], POSITION_IN_RANGE, STATE_ON),
|
||||
([15, 22.1], POSITION_ABOVE, STATE_OFF), # in-range -> above hysteresis edge
|
||||
([15, 7.9], POSITION_BELOW, STATE_OFF), # in-range -> below hysteresis edge
|
||||
([7, 11.9], POSITION_BELOW, STATE_OFF),
|
||||
([23, 18.1], POSITION_ABOVE, STATE_OFF),
|
||||
],
|
||||
)
|
||||
async def test_sensor_in_range_with_hysteresis(
|
||||
hass: HomeAssistant,
|
||||
vals: list[float | str | None],
|
||||
expected_position: str,
|
||||
expected_state: str,
|
||||
) -> None:
|
||||
"""Test if source is within the range."""
|
||||
config = {
|
||||
Platform.BINARY_SENSOR: {
|
||||
CONF_PLATFORM: "threshold",
|
||||
CONF_LOWER: "10",
|
||||
CONF_UPPER: "20",
|
||||
CONF_HYSTERESIS: "2",
|
||||
CONF_ENTITY_ID: "sensor.test_monitored",
|
||||
}
|
||||
}
|
||||
|
||||
assert await async_setup_component(hass, Platform.BINARY_SENSOR, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes[ATTR_ENTITY_ID] == "sensor.test_monitored"
|
||||
assert state.attributes[ATTR_LOWER] == float(
|
||||
config[Platform.BINARY_SENSOR][CONF_LOWER]
|
||||
)
|
||||
assert state.attributes[ATTR_UPPER] == float(
|
||||
config[Platform.BINARY_SENSOR][CONF_UPPER]
|
||||
)
|
||||
assert state.attributes[ATTR_HYSTERESIS] == 2.0
|
||||
assert state.attributes[ATTR_TYPE] == TYPE_RANGE
|
||||
|
||||
for val in vals:
|
||||
hass.states.async_set("sensor.test_monitored", val)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes[ATTR_POSITION] == expected_position
|
||||
assert state.state == expected_state
|
||||
|
||||
|
||||
async def test_sensor_in_range_unknown_state(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
"""Test if source is within the range."""
|
||||
config = {
|
||||
Platform.BINARY_SENSOR: {
|
||||
CONF_PLATFORM: "threshold",
|
||||
CONF_LOWER: "10",
|
||||
CONF_UPPER: "20",
|
||||
CONF_ENTITY_ID: "sensor.test_monitored",
|
||||
}
|
||||
}
|
||||
|
||||
assert await async_setup_component(hass, Platform.BINARY_SENSOR, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.states.async_set(
|
||||
"sensor.test_monitored",
|
||||
16,
|
||||
{ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.CELSIUS},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
|
||||
assert state.attributes[ATTR_ENTITY_ID] == "sensor.test_monitored"
|
||||
assert state.attributes[ATTR_SENSOR_VALUE] == 16
|
||||
assert state.attributes[ATTR_POSITION] == POSITION_IN_RANGE
|
||||
assert state.attributes[ATTR_LOWER] == float(
|
||||
config[Platform.BINARY_SENSOR][CONF_LOWER]
|
||||
)
|
||||
assert state.attributes[ATTR_UPPER] == float(
|
||||
config[Platform.BINARY_SENSOR][CONF_UPPER]
|
||||
)
|
||||
assert state.attributes[ATTR_HYSTERESIS] == 0.0
|
||||
assert state.attributes[ATTR_TYPE] == TYPE_RANGE
|
||||
assert state.state == STATE_ON
|
||||
|
||||
hass.states.async_set("sensor.test_monitored", STATE_UNKNOWN)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes[ATTR_POSITION] == POSITION_UNKNOWN
|
||||
assert state.state == STATE_UNKNOWN
|
||||
|
||||
hass.states.async_set("sensor.test_monitored", STATE_UNAVAILABLE)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes[ATTR_POSITION] == POSITION_UNKNOWN
|
||||
assert state.state == STATE_UNKNOWN
|
||||
|
||||
assert "State is not numerical" not in caplog.text
|
||||
|
||||
|
||||
async def test_sensor_lower_zero_threshold(hass: HomeAssistant) -> None:
|
||||
"""Test if a lower threshold of zero is set."""
|
||||
config = {
|
||||
Platform.BINARY_SENSOR: {
|
||||
CONF_PLATFORM: "threshold",
|
||||
CONF_LOWER: "0",
|
||||
CONF_ENTITY_ID: "sensor.test_monitored",
|
||||
}
|
||||
}
|
||||
|
||||
assert await async_setup_component(hass, Platform.BINARY_SENSOR, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.states.async_set("sensor.test_monitored", 16)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes[ATTR_TYPE] == TYPE_LOWER
|
||||
assert state.attributes[ATTR_LOWER] == float(
|
||||
config[Platform.BINARY_SENSOR][CONF_LOWER]
|
||||
)
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
hass.states.async_set("sensor.test_monitored", -3)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.state == STATE_ON
|
||||
|
||||
|
||||
async def test_sensor_upper_zero_threshold(hass: HomeAssistant) -> None:
|
||||
"""Test if an upper threshold of zero is set."""
|
||||
config = {
|
||||
Platform.BINARY_SENSOR: {
|
||||
CONF_PLATFORM: "threshold",
|
||||
CONF_UPPER: "0",
|
||||
CONF_ENTITY_ID: "sensor.test_monitored",
|
||||
}
|
||||
}
|
||||
|
||||
assert await async_setup_component(hass, Platform.BINARY_SENSOR, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
hass.states.async_set("sensor.test_monitored", -10)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.attributes[ATTR_TYPE] == TYPE_UPPER
|
||||
assert state.attributes[ATTR_UPPER] == float(
|
||||
config[Platform.BINARY_SENSOR][CONF_UPPER]
|
||||
)
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
hass.states.async_set("sensor.test_monitored", 2)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get("binary_sensor.threshold")
|
||||
assert state.state == STATE_ON
|
||||
|
||||
|
||||
async def test_sensor_no_lower_upper(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
"""Test if no lower or upper has been provided."""
|
||||
config = {
|
||||
Platform.BINARY_SENSOR: {
|
||||
CONF_PLATFORM: "threshold",
|
||||
CONF_ENTITY_ID: "sensor.test_monitored",
|
||||
}
|
||||
}
|
||||
|
||||
await async_setup_component(hass, Platform.BINARY_SENSOR, config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert "Lower or Upper thresholds are not provided" in caplog.text
|
||||
|
||||
|
||||
async def test_device_id(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test for source entity device for Threshold."""
|
||||
source_config_entry = MockConfigEntry()
|
||||
source_config_entry.add_to_hass(hass)
|
||||
source_device_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=source_config_entry.entry_id,
|
||||
identifiers={("sensor", "identifier_test")},
|
||||
connections={("mac", "30:31:32:33:34:35")},
|
||||
)
|
||||
source_entity = entity_registry.async_get_or_create(
|
||||
"sensor",
|
||||
"test",
|
||||
"source",
|
||||
config_entry=source_config_entry,
|
||||
device_id=source_device_entry.id,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert entity_registry.async_get("sensor.test_source") is not None
|
||||
|
||||
utility_meter_config_entry = MockConfigEntry(
|
||||
data={},
|
||||
domain=DOMAIN,
|
||||
options={
|
||||
CONF_ENTITY_ID: "sensor.test_source",
|
||||
CONF_HYSTERESIS: 0.0,
|
||||
CONF_LOWER: -2.0,
|
||||
CONF_NAME: "Threshold",
|
||||
CONF_UPPER: None,
|
||||
},
|
||||
title="Threshold",
|
||||
)
|
||||
|
||||
utility_meter_config_entry.add_to_hass(hass)
|
||||
|
||||
assert await hass.config_entries.async_setup(utility_meter_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
utility_meter_entity = entity_registry.async_get("binary_sensor.threshold")
|
||||
assert utility_meter_entity is not None
|
||||
assert utility_meter_entity.device_id == source_entity.device_id
|
||||
347
home-assistant/tests/components/threshold/test_config_flow.py
Normal file
347
home-assistant/tests/components/threshold/test_config_flow.py
Normal file
@@ -0,0 +1,347 @@
|
||||
"""Test the Threshold config flow."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.threshold.const import DOMAIN
|
||||
from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, UnitOfTemperature
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.typing import WebSocketGenerator
|
||||
|
||||
|
||||
async def test_config_flow(hass: HomeAssistant) -> None:
|
||||
"""Test the config flow."""
|
||||
input_sensor = "sensor.input"
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] is None
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.threshold.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
"entity_id": input_sensor,
|
||||
"lower": -2,
|
||||
"upper": 0.0,
|
||||
"name": "My threshold sensor",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == "My threshold sensor"
|
||||
assert result["data"] == {}
|
||||
assert result["options"] == {
|
||||
"entity_id": input_sensor,
|
||||
"hysteresis": 0.0,
|
||||
"lower": -2.0,
|
||||
"name": "My threshold sensor",
|
||||
"upper": 0.0,
|
||||
}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
config_entry = hass.config_entries.async_entries(DOMAIN)[0]
|
||||
assert config_entry.data == {}
|
||||
assert config_entry.options == {
|
||||
"entity_id": input_sensor,
|
||||
"hysteresis": 0.0,
|
||||
"lower": -2.0,
|
||||
"name": "My threshold sensor",
|
||||
"upper": 0.0,
|
||||
}
|
||||
assert config_entry.title == "My threshold sensor"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("extra_input_data", "error"), [({}, "need_lower_upper")])
|
||||
async def test_fail(hass: HomeAssistant, extra_input_data, error) -> None:
|
||||
"""Test not providing lower or upper limit fails."""
|
||||
input_sensor = "sensor.input"
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] is None
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
"entity_id": input_sensor,
|
||||
"name": "My threshold sensor",
|
||||
**extra_input_data,
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": error}
|
||||
|
||||
|
||||
def get_suggested(schema, key):
|
||||
"""Get suggested value for key in voluptuous schema."""
|
||||
for k in schema:
|
||||
if k == key:
|
||||
if k.description is None or "suggested_value" not in k.description:
|
||||
return None
|
||||
return k.description["suggested_value"]
|
||||
# Wanted key absent from schema
|
||||
raise KeyError("Wanted key absent from schema")
|
||||
|
||||
|
||||
async def test_options(hass: HomeAssistant) -> None:
|
||||
"""Test reconfiguring."""
|
||||
input_sensor = "sensor.input"
|
||||
hass.states.async_set(input_sensor, "10")
|
||||
|
||||
# Setup the config entry
|
||||
config_entry = MockConfigEntry(
|
||||
data={},
|
||||
domain=DOMAIN,
|
||||
options={
|
||||
"entity_id": input_sensor,
|
||||
"hysteresis": 0.0,
|
||||
"lower": -2.0,
|
||||
"name": "My threshold",
|
||||
"upper": None,
|
||||
},
|
||||
title="My threshold",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
schema = result["data_schema"].schema
|
||||
assert get_suggested(schema, "hysteresis") == 0.0
|
||||
assert get_suggested(schema, "lower") == -2.0
|
||||
assert get_suggested(schema, "upper") is None
|
||||
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={
|
||||
"entity_id": input_sensor,
|
||||
"hysteresis": 0.0,
|
||||
"upper": 20.0,
|
||||
},
|
||||
)
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["data"] == {
|
||||
"entity_id": input_sensor,
|
||||
"hysteresis": 0.0,
|
||||
"lower": None,
|
||||
"name": "My threshold",
|
||||
"upper": 20.0,
|
||||
}
|
||||
assert config_entry.data == {}
|
||||
assert config_entry.options == {
|
||||
"entity_id": input_sensor,
|
||||
"hysteresis": 0.0,
|
||||
"lower": None,
|
||||
"name": "My threshold",
|
||||
"upper": 20.0,
|
||||
}
|
||||
assert config_entry.title == "My threshold"
|
||||
|
||||
# Check config entry is reloaded with new options
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Check the entity was updated, no new entity was created
|
||||
assert len(hass.states.async_all()) == 2
|
||||
|
||||
# Check the state of the entity has changed as expected
|
||||
state = hass.states.get("binary_sensor.my_threshold")
|
||||
assert state.state == "off"
|
||||
assert state.attributes["type"] == "upper"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"user_input",
|
||||
[
|
||||
(
|
||||
{
|
||||
"name": "Test Sensor",
|
||||
"entity_id": "sensor.test_monitored",
|
||||
"hysteresis": 0.0,
|
||||
"lower": 20.0,
|
||||
}
|
||||
),
|
||||
(
|
||||
{
|
||||
"name": "Test Sensor",
|
||||
"entity_id": "sensor.test_monitored",
|
||||
"hysteresis": 0.0,
|
||||
}
|
||||
),
|
||||
(
|
||||
{
|
||||
"name": "",
|
||||
"entity_id": "",
|
||||
"hysteresis": 0.0,
|
||||
"lower": 20.0,
|
||||
}
|
||||
),
|
||||
],
|
||||
ids=("success", "missing_upper_lower", "missing_entity_id"),
|
||||
)
|
||||
async def test_config_flow_preview_success(
|
||||
hass: HomeAssistant,
|
||||
hass_ws_client: WebSocketGenerator,
|
||||
user_input: str,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test the config flow preview."""
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
# add state for the tests
|
||||
hass.states.async_set(
|
||||
"sensor.test_monitored",
|
||||
16,
|
||||
{ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.CELSIUS},
|
||||
)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "user"
|
||||
assert result["errors"] is None
|
||||
assert result["preview"] == "threshold"
|
||||
|
||||
await client.send_json_auto_id(
|
||||
{
|
||||
"type": "threshold/start_preview",
|
||||
"flow_id": result["flow_id"],
|
||||
"flow_type": "config_flow",
|
||||
"user_input": user_input,
|
||||
}
|
||||
)
|
||||
msg = await client.receive_json()
|
||||
assert msg["success"]
|
||||
assert msg["result"] is None
|
||||
|
||||
msg = await client.receive_json()
|
||||
assert msg["event"] == snapshot
|
||||
assert len(hass.states.async_all()) == 1
|
||||
|
||||
|
||||
async def test_options_flow_preview(
|
||||
hass: HomeAssistant,
|
||||
hass_ws_client: WebSocketGenerator,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test the options flow preview."""
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
# add state for the tests
|
||||
hass.states.async_set(
|
||||
"sensor.test_monitored",
|
||||
16,
|
||||
{ATTR_UNIT_OF_MEASUREMENT: UnitOfTemperature.CELSIUS},
|
||||
)
|
||||
|
||||
# Setup the config entry
|
||||
config_entry = MockConfigEntry(
|
||||
data={},
|
||||
domain=DOMAIN,
|
||||
options={
|
||||
"entity_id": "sensor.test_monitored",
|
||||
"hysteresis": 0.0,
|
||||
"lower": 20.0,
|
||||
"name": "Test Sensor",
|
||||
"upper": None,
|
||||
},
|
||||
title="Test Sensor",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["errors"] is None
|
||||
assert result["preview"] == "threshold"
|
||||
|
||||
await client.send_json_auto_id(
|
||||
{
|
||||
"type": "threshold/start_preview",
|
||||
"flow_id": result["flow_id"],
|
||||
"flow_type": "options_flow",
|
||||
"user_input": {
|
||||
"name": "Test Sensor",
|
||||
"entity_id": "sensor.test_monitored",
|
||||
"hysteresis": 0.0,
|
||||
"lower": 20.0,
|
||||
},
|
||||
}
|
||||
)
|
||||
msg = await client.receive_json()
|
||||
assert msg["success"]
|
||||
assert msg["result"] is None
|
||||
|
||||
msg = await client.receive_json()
|
||||
assert msg["event"] == snapshot
|
||||
assert len(hass.states.async_all()) == 2
|
||||
|
||||
|
||||
async def test_options_flow_sensor_preview_config_entry_removed(
|
||||
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
|
||||
) -> None:
|
||||
"""Test the option flow preview where the config entry is removed."""
|
||||
client = await hass_ws_client(hass)
|
||||
|
||||
# Setup the config entry
|
||||
config_entry = MockConfigEntry(
|
||||
data={},
|
||||
domain=DOMAIN,
|
||||
options={
|
||||
"entity_id": "sensor.test_monitored",
|
||||
"hysteresis": 0.0,
|
||||
"lower": 20.0,
|
||||
"name": "Test Sensor",
|
||||
"upper": None,
|
||||
},
|
||||
title="Test Sensor",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["errors"] is None
|
||||
assert result["preview"] == "threshold"
|
||||
|
||||
await hass.config_entries.async_remove(config_entry.entry_id)
|
||||
|
||||
await client.send_json_auto_id(
|
||||
{
|
||||
"type": "threshold/start_preview",
|
||||
"flow_id": result["flow_id"],
|
||||
"flow_type": "options_flow",
|
||||
"user_input": {
|
||||
"name": "Test Sensor",
|
||||
"entity_id": "sensor.test_monitored",
|
||||
"hysteresis": 0.0,
|
||||
"lower": 20.0,
|
||||
},
|
||||
}
|
||||
)
|
||||
msg = await client.receive_json()
|
||||
assert not msg["success"]
|
||||
assert msg["error"] == {
|
||||
"code": "home_assistant_error",
|
||||
"message": "Config entry not found",
|
||||
}
|
||||
210
home-assistant/tests/components/threshold/test_init.py
Normal file
210
home-assistant/tests/components/threshold/test_init.py
Normal file
@@ -0,0 +1,210 @@
|
||||
"""Test the Min/Max integration."""
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.threshold.const import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.mark.parametrize("platform", ["binary_sensor"])
|
||||
async def test_setup_and_remove_config_entry(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
platform: str,
|
||||
) -> None:
|
||||
"""Test setting up and removing a config entry."""
|
||||
hass.states.async_set("sensor.input", "-10")
|
||||
|
||||
input_sensor = "sensor.input"
|
||||
|
||||
threshold_entity_id = f"{platform}.input_threshold"
|
||||
|
||||
# Setup the config entry
|
||||
config_entry = MockConfigEntry(
|
||||
data={},
|
||||
domain=DOMAIN,
|
||||
options={
|
||||
"entity_id": input_sensor,
|
||||
"hysteresis": 0.0,
|
||||
"lower": -2.0,
|
||||
"name": "Input threshold",
|
||||
"upper": None,
|
||||
},
|
||||
title="Input threshold",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Check the entity is registered in the entity registry
|
||||
assert entity_registry.async_get(threshold_entity_id) is not None
|
||||
|
||||
# Check the platform is setup correctly
|
||||
state = hass.states.get(threshold_entity_id)
|
||||
assert state
|
||||
assert state.state == "on"
|
||||
assert state.attributes["entity_id"] == input_sensor
|
||||
assert state.attributes["hysteresis"] == 0.0
|
||||
assert state.attributes["lower"] == -2.0
|
||||
assert state.attributes["position"] == "below"
|
||||
assert state.attributes["sensor_value"] == -10.0
|
||||
assert state.attributes["type"] == "lower"
|
||||
assert state.attributes["upper"] is None
|
||||
|
||||
# Remove the config entry
|
||||
assert await hass.config_entries.async_remove(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Check the state and entity registry entry are removed
|
||||
assert hass.states.get(threshold_entity_id) is None
|
||||
assert entity_registry.async_get(threshold_entity_id) is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("platform", ["sensor"])
|
||||
async def test_entry_changed(hass: HomeAssistant, platform) -> None:
|
||||
"""Test reconfiguring."""
|
||||
|
||||
device_registry = dr.async_get(hass)
|
||||
entity_registry = er.async_get(hass)
|
||||
|
||||
def _create_mock_entity(domain: str, name: str) -> er.RegistryEntry:
|
||||
config_entry = MockConfigEntry(
|
||||
data={},
|
||||
domain="test",
|
||||
title=f"{name}",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
device_entry = device_registry.async_get_or_create(
|
||||
identifiers={("test", name)}, config_entry_id=config_entry.entry_id
|
||||
)
|
||||
return entity_registry.async_get_or_create(
|
||||
domain, "test", name, suggested_object_id=name, device_id=device_entry.id
|
||||
)
|
||||
|
||||
def _get_device_config_entries(entry: er.RegistryEntry) -> set[str]:
|
||||
assert entry.device_id
|
||||
device = device_registry.async_get(entry.device_id)
|
||||
assert device
|
||||
return device.config_entries
|
||||
|
||||
# Set up entities, with backing devices and config entries
|
||||
run1_entry = _create_mock_entity("sensor", "initial")
|
||||
run2_entry = _create_mock_entity("sensor", "changed")
|
||||
|
||||
# Setup the config entry
|
||||
config_entry = MockConfigEntry(
|
||||
data={},
|
||||
domain=DOMAIN,
|
||||
options={
|
||||
"entity_id": "sensor.initial",
|
||||
"hysteresis": 0.0,
|
||||
"lower": -2.0,
|
||||
"name": "My threshold",
|
||||
"upper": None,
|
||||
},
|
||||
title="My integration",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.entry_id in _get_device_config_entries(run1_entry)
|
||||
assert config_entry.entry_id not in _get_device_config_entries(run2_entry)
|
||||
|
||||
hass.config_entries.async_update_entry(
|
||||
config_entry, options={**config_entry.options, "entity_id": "sensor.changed"}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Check that the config entry association has updated
|
||||
assert config_entry.entry_id not in _get_device_config_entries(run1_entry)
|
||||
assert config_entry.entry_id in _get_device_config_entries(run2_entry)
|
||||
|
||||
|
||||
async def test_device_cleaning(
|
||||
hass: HomeAssistant,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test for source entity device for Threshold."""
|
||||
|
||||
# Source entity device config entry
|
||||
source_config_entry = MockConfigEntry()
|
||||
source_config_entry.add_to_hass(hass)
|
||||
|
||||
# Device entry of the source entity
|
||||
source_device1_entry = device_registry.async_get_or_create(
|
||||
config_entry_id=source_config_entry.entry_id,
|
||||
identifiers={("sensor", "identifier_test1")},
|
||||
connections={("mac", "30:31:32:33:34:01")},
|
||||
)
|
||||
|
||||
# Source entity registry
|
||||
source_entity = entity_registry.async_get_or_create(
|
||||
"sensor",
|
||||
"test",
|
||||
"source",
|
||||
config_entry=source_config_entry,
|
||||
device_id=source_device1_entry.id,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert entity_registry.async_get("sensor.test_source") is not None
|
||||
|
||||
# Configure the configuration entry for Threshold
|
||||
threshold_config_entry = MockConfigEntry(
|
||||
data={},
|
||||
domain=DOMAIN,
|
||||
options={
|
||||
"entity_id": "sensor.test_source",
|
||||
"hysteresis": 0.0,
|
||||
"lower": -2.0,
|
||||
"name": "Threshold",
|
||||
"upper": None,
|
||||
},
|
||||
title="Threshold",
|
||||
)
|
||||
threshold_config_entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(threshold_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Confirm the link between the source entity device and the threshold sensor
|
||||
threshold_entity = entity_registry.async_get("binary_sensor.threshold")
|
||||
assert threshold_entity is not None
|
||||
assert threshold_entity.device_id == source_entity.device_id
|
||||
|
||||
# Device entry incorrectly linked to Threshold config entry
|
||||
device_registry.async_get_or_create(
|
||||
config_entry_id=threshold_config_entry.entry_id,
|
||||
identifiers={("sensor", "identifier_test2")},
|
||||
connections={("mac", "30:31:32:33:34:02")},
|
||||
)
|
||||
device_registry.async_get_or_create(
|
||||
config_entry_id=threshold_config_entry.entry_id,
|
||||
identifiers={("sensor", "identifier_test3")},
|
||||
connections={("mac", "30:31:32:33:34:03")},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Before reloading the config entry, two devices are expected to be linked
|
||||
devices_before_reload = device_registry.devices.get_devices_for_config_entry_id(
|
||||
threshold_config_entry.entry_id
|
||||
)
|
||||
assert len(devices_before_reload) == 3
|
||||
|
||||
# Config entry reload
|
||||
await hass.config_entries.async_reload(threshold_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Confirm the link between the source entity device and the threshold sensor after reload
|
||||
threshold_entity = entity_registry.async_get("binary_sensor.threshold")
|
||||
assert threshold_entity is not None
|
||||
assert threshold_entity.device_id == source_entity.device_id
|
||||
|
||||
# After reloading the config entry, only one linked device is expected
|
||||
devices_after_reload = device_registry.devices.get_devices_for_config_entry_id(
|
||||
threshold_config_entry.entry_id
|
||||
)
|
||||
assert len(devices_after_reload) == 1
|
||||
Reference in New Issue
Block a user