Refactor drop tests for binary_sensor (#107090)

This commit is contained in:
Jan Bouwhuis 2024-01-04 13:12:11 +01:00 committed by GitHub
parent 5ae8b6bc02
commit 10f5ce2dc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,9 +1,7 @@
"""Test DROP binary sensor entities.""" """Test DROP binary sensor entities."""
from homeassistant.components.drop_connect.const import DOMAIN from homeassistant.const import STATE_OFF, STATE_ON
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNKNOWN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from .common import ( from .common import (
TEST_DATA_HUB, TEST_DATA_HUB,
@ -27,6 +25,13 @@ from .common import (
TEST_DATA_SOFTENER, TEST_DATA_SOFTENER,
TEST_DATA_SOFTENER_RESET, TEST_DATA_SOFTENER_RESET,
TEST_DATA_SOFTENER_TOPIC, TEST_DATA_SOFTENER_TOPIC,
config_entry_hub,
config_entry_leak,
config_entry_protection_valve,
config_entry_pump_controller,
config_entry_ro_filter,
config_entry_salt,
config_entry_softener,
) )
from tests.common import async_fire_mqtt_message from tests.common import async_fire_mqtt_message
@ -34,159 +39,158 @@ from tests.typing import MqttMockHAClient
async def test_binary_sensors_hub( async def test_binary_sensors_hub(
hass: HomeAssistant, config_entry_hub, mqtt_mock: MqttMockHAClient hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None: ) -> None:
"""Test DROP binary sensors for hubs.""" """Test DROP binary sensors for hubs."""
config_entry_hub.add_to_hass(hass) entry = config_entry_hub()
assert await async_setup_component(hass, DOMAIN, {}) entry.add_to_hass(hass)
await hass.async_block_till_done() assert await hass.config_entries.async_setup(entry.entry_id)
pending_notifications_sensor_name = ( pending_notifications_sensor_name = (
"binary_sensor.hub_drop_1_c0ffee_notification_unread" "binary_sensor.hub_drop_1_c0ffee_notification_unread"
) )
hass.states.async_set(pending_notifications_sensor_name, STATE_UNKNOWN) assert hass.states.get(pending_notifications_sensor_name).state == STATE_OFF
leak_sensor_name = "binary_sensor.hub_drop_1_c0ffee_leak_detected" leak_sensor_name = "binary_sensor.hub_drop_1_c0ffee_leak_detected"
hass.states.async_set(leak_sensor_name, STATE_UNKNOWN) assert hass.states.get(leak_sensor_name).state == STATE_OFF
async_fire_mqtt_message(hass, TEST_DATA_HUB_TOPIC, TEST_DATA_HUB_RESET) async_fire_mqtt_message(hass, TEST_DATA_HUB_TOPIC, TEST_DATA_HUB_RESET)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(pending_notifications_sensor_name).state == STATE_OFF
assert hass.states.get(leak_sensor_name).state == STATE_OFF
async_fire_mqtt_message(hass, TEST_DATA_HUB_TOPIC, TEST_DATA_HUB) async_fire_mqtt_message(hass, TEST_DATA_HUB_TOPIC, TEST_DATA_HUB)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(pending_notifications_sensor_name).state == STATE_ON
pending_notifications = hass.states.get(pending_notifications_sensor_name) assert hass.states.get(leak_sensor_name).state == STATE_OFF
assert pending_notifications.state == STATE_ON
leak = hass.states.get(leak_sensor_name)
assert leak.state == STATE_OFF
async def test_binary_sensors_salt( async def test_binary_sensors_salt(
hass: HomeAssistant, config_entry_salt, mqtt_mock: MqttMockHAClient hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None: ) -> None:
"""Test DROP binary sensors for salt sensors.""" """Test DROP binary sensors for salt sensors."""
config_entry_salt.add_to_hass(hass) entry = config_entry_salt()
assert await async_setup_component(hass, DOMAIN, {}) entry.add_to_hass(hass)
await hass.async_block_till_done() assert await hass.config_entries.async_setup(entry.entry_id)
salt_sensor_name = "binary_sensor.salt_sensor_salt_low" salt_sensor_name = "binary_sensor.salt_sensor_salt_low"
hass.states.async_set(salt_sensor_name, STATE_UNKNOWN) assert hass.states.get(salt_sensor_name).state == STATE_OFF
async_fire_mqtt_message(hass, TEST_DATA_SALT_TOPIC, TEST_DATA_SALT_RESET) async_fire_mqtt_message(hass, TEST_DATA_SALT_TOPIC, TEST_DATA_SALT_RESET)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(salt_sensor_name).state == STATE_OFF
async_fire_mqtt_message(hass, TEST_DATA_SALT_TOPIC, TEST_DATA_SALT) async_fire_mqtt_message(hass, TEST_DATA_SALT_TOPIC, TEST_DATA_SALT)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(salt_sensor_name).state == STATE_ON
salt = hass.states.get(salt_sensor_name)
assert salt.state == STATE_ON
async def test_binary_sensors_leak( async def test_binary_sensors_leak(
hass: HomeAssistant, config_entry_leak, mqtt_mock: MqttMockHAClient hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None: ) -> None:
"""Test DROP binary sensors for leak detectors.""" """Test DROP binary sensors for leak detectors."""
config_entry_leak.add_to_hass(hass) entry = config_entry_leak()
assert await async_setup_component(hass, DOMAIN, {}) entry.add_to_hass(hass)
await hass.async_block_till_done() assert await hass.config_entries.async_setup(entry.entry_id)
leak_sensor_name = "binary_sensor.leak_detector_leak_detected" leak_sensor_name = "binary_sensor.leak_detector_leak_detected"
hass.states.async_set(leak_sensor_name, STATE_UNKNOWN) assert hass.states.get(leak_sensor_name).state == STATE_OFF
async_fire_mqtt_message(hass, TEST_DATA_LEAK_TOPIC, TEST_DATA_LEAK_RESET) async_fire_mqtt_message(hass, TEST_DATA_LEAK_TOPIC, TEST_DATA_LEAK_RESET)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(leak_sensor_name).state == STATE_OFF
async_fire_mqtt_message(hass, TEST_DATA_LEAK_TOPIC, TEST_DATA_LEAK) async_fire_mqtt_message(hass, TEST_DATA_LEAK_TOPIC, TEST_DATA_LEAK)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(leak_sensor_name).state == STATE_ON
leak = hass.states.get(leak_sensor_name)
assert leak.state == STATE_ON
async def test_binary_sensors_softener( async def test_binary_sensors_softener(
hass: HomeAssistant, config_entry_softener, mqtt_mock: MqttMockHAClient hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None: ) -> None:
"""Test DROP binary sensors for softeners.""" """Test DROP binary sensors for softeners."""
config_entry_softener.add_to_hass(hass) entry = config_entry_softener()
assert await async_setup_component(hass, DOMAIN, {}) entry.add_to_hass(hass)
await hass.async_block_till_done() assert await hass.config_entries.async_setup(entry.entry_id)
reserve_in_use_sensor_name = "binary_sensor.softener_reserve_capacity_in_use" reserve_in_use_sensor_name = "binary_sensor.softener_reserve_capacity_in_use"
hass.states.async_set(reserve_in_use_sensor_name, STATE_UNKNOWN) assert hass.states.get(reserve_in_use_sensor_name).state == STATE_OFF
async_fire_mqtt_message(hass, TEST_DATA_SOFTENER_TOPIC, TEST_DATA_SOFTENER_RESET) async_fire_mqtt_message(hass, TEST_DATA_SOFTENER_TOPIC, TEST_DATA_SOFTENER_RESET)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(reserve_in_use_sensor_name).state == STATE_OFF
async_fire_mqtt_message(hass, TEST_DATA_SOFTENER_TOPIC, TEST_DATA_SOFTENER) async_fire_mqtt_message(hass, TEST_DATA_SOFTENER_TOPIC, TEST_DATA_SOFTENER)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(reserve_in_use_sensor_name).state == STATE_ON
reserve_in_use = hass.states.get(reserve_in_use_sensor_name)
assert reserve_in_use.state == STATE_ON
async def test_binary_sensors_protection_valve( async def test_binary_sensors_protection_valve(
hass: HomeAssistant, config_entry_protection_valve, mqtt_mock: MqttMockHAClient hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None: ) -> None:
"""Test DROP binary sensors for protection valves.""" """Test DROP binary sensors for protection valves."""
config_entry_protection_valve.add_to_hass(hass) entry = config_entry_protection_valve()
assert await async_setup_component(hass, DOMAIN, {}) entry.add_to_hass(hass)
await hass.async_block_till_done() assert await hass.config_entries.async_setup(entry.entry_id)
leak_sensor_name = "binary_sensor.protection_valve_leak_detected" leak_sensor_name = "binary_sensor.protection_valve_leak_detected"
hass.states.async_set(leak_sensor_name, STATE_UNKNOWN) assert hass.states.get(leak_sensor_name).state == STATE_OFF
async_fire_mqtt_message( async_fire_mqtt_message(
hass, TEST_DATA_PROTECTION_VALVE_TOPIC, TEST_DATA_PROTECTION_VALVE_RESET hass, TEST_DATA_PROTECTION_VALVE_TOPIC, TEST_DATA_PROTECTION_VALVE_RESET
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(leak_sensor_name).state == STATE_OFF
async_fire_mqtt_message( async_fire_mqtt_message(
hass, TEST_DATA_PROTECTION_VALVE_TOPIC, TEST_DATA_PROTECTION_VALVE hass, TEST_DATA_PROTECTION_VALVE_TOPIC, TEST_DATA_PROTECTION_VALVE
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(leak_sensor_name).state == STATE_ON
leak = hass.states.get(leak_sensor_name)
assert leak.state == STATE_ON
async def test_binary_sensors_pump_controller( async def test_binary_sensors_pump_controller(
hass: HomeAssistant, config_entry_pump_controller, mqtt_mock: MqttMockHAClient hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None: ) -> None:
"""Test DROP binary sensors for pump controllers.""" """Test DROP binary sensors for pump controllers."""
config_entry_pump_controller.add_to_hass(hass) entry = config_entry_pump_controller()
assert await async_setup_component(hass, DOMAIN, {}) entry.add_to_hass(hass)
await hass.async_block_till_done() assert await hass.config_entries.async_setup(entry.entry_id)
leak_sensor_name = "binary_sensor.pump_controller_leak_detected" leak_sensor_name = "binary_sensor.pump_controller_leak_detected"
hass.states.async_set(leak_sensor_name, STATE_UNKNOWN) assert hass.states.get(leak_sensor_name).state == STATE_OFF
pump_sensor_name = "binary_sensor.pump_controller_pump_status" pump_sensor_name = "binary_sensor.pump_controller_pump_status"
hass.states.async_set(pump_sensor_name, STATE_UNKNOWN) assert hass.states.get(pump_sensor_name).state == STATE_OFF
async_fire_mqtt_message( async_fire_mqtt_message(
hass, TEST_DATA_PUMP_CONTROLLER_TOPIC, TEST_DATA_PUMP_CONTROLLER_RESET hass, TEST_DATA_PUMP_CONTROLLER_TOPIC, TEST_DATA_PUMP_CONTROLLER_RESET
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(leak_sensor_name).state == STATE_OFF
assert hass.states.get(pump_sensor_name).state == STATE_OFF
async_fire_mqtt_message( async_fire_mqtt_message(
hass, TEST_DATA_PUMP_CONTROLLER_TOPIC, TEST_DATA_PUMP_CONTROLLER hass, TEST_DATA_PUMP_CONTROLLER_TOPIC, TEST_DATA_PUMP_CONTROLLER
) )
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(leak_sensor_name).state == STATE_ON
leak = hass.states.get(leak_sensor_name) assert hass.states.get(pump_sensor_name).state == STATE_ON
assert leak.state == STATE_ON
pump = hass.states.get(pump_sensor_name)
assert pump.state == STATE_ON
async def test_binary_sensors_ro_filter( async def test_binary_sensors_ro_filter(
hass: HomeAssistant, config_entry_ro_filter, mqtt_mock: MqttMockHAClient hass: HomeAssistant, mqtt_mock: MqttMockHAClient
) -> None: ) -> None:
"""Test DROP binary sensors for RO filters.""" """Test DROP binary sensors for RO filters."""
config_entry_ro_filter.add_to_hass(hass) entry = config_entry_ro_filter()
assert await async_setup_component(hass, DOMAIN, {}) entry.add_to_hass(hass)
await hass.async_block_till_done() assert await hass.config_entries.async_setup(entry.entry_id)
leak_sensor_name = "binary_sensor.ro_filter_leak_detected" leak_sensor_name = "binary_sensor.ro_filter_leak_detected"
hass.states.async_set(leak_sensor_name, STATE_UNKNOWN) assert hass.states.get(leak_sensor_name).state == STATE_OFF
async_fire_mqtt_message(hass, TEST_DATA_RO_FILTER_TOPIC, TEST_DATA_RO_FILTER_RESET) async_fire_mqtt_message(hass, TEST_DATA_RO_FILTER_TOPIC, TEST_DATA_RO_FILTER_RESET)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(leak_sensor_name).state == STATE_OFF
async_fire_mqtt_message(hass, TEST_DATA_RO_FILTER_TOPIC, TEST_DATA_RO_FILTER) async_fire_mqtt_message(hass, TEST_DATA_RO_FILTER_TOPIC, TEST_DATA_RO_FILTER)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(leak_sensor_name).state == STATE_ON
leak = hass.states.get(leak_sensor_name)
assert leak.state == STATE_ON