diff --git a/tests/components/greeneye_monitor/common.py b/tests/components/greeneye_monitor/common.py index 952992ee500..0a19b79795f 100644 --- a/tests/components/greeneye_monitor/common.py +++ b/tests/components/greeneye_monitor/common.py @@ -128,6 +128,35 @@ SINGLE_MONITOR_CONFIG_VOLTAGE_SENSORS = make_single_monitor_config_with_sensors( } ) +MULTI_MONITOR_CONFIG = { + DOMAIN: { + CONF_PORT: 7513, + CONF_MONITORS: [ + { + CONF_SERIAL_NUMBER: "00000001", + CONF_TEMPERATURE_SENSORS: { + CONF_TEMPERATURE_UNIT: "C", + CONF_SENSORS: [{CONF_NUMBER: 1, CONF_NAME: "unit_1_temp_1"}], + }, + }, + { + CONF_SERIAL_NUMBER: "00000002", + CONF_TEMPERATURE_SENSORS: { + CONF_TEMPERATURE_UNIT: "F", + CONF_SENSORS: [{CONF_NUMBER: 1, CONF_NAME: "unit_2_temp_1"}], + }, + }, + { + CONF_SERIAL_NUMBER: "00000003", + CONF_TEMPERATURE_SENSORS: { + CONF_TEMPERATURE_UNIT: "C", + CONF_SENSORS: [{CONF_NUMBER: 1, CONF_NAME: "unit_3_temp_1"}], + }, + }, + ], + } +} + async def setup_greeneye_monitor_component_with_config( hass: HomeAssistant, config: ConfigType diff --git a/tests/components/greeneye_monitor/test_init.py b/tests/components/greeneye_monitor/test_init.py index 5bffe2eb4a4..c8e13714939 100644 --- a/tests/components/greeneye_monitor/test_init.py +++ b/tests/components/greeneye_monitor/test_init.py @@ -6,23 +6,12 @@ from unittest.mock import AsyncMock import pytest -from homeassistant.components.greeneye_monitor import ( - CONF_MONITORS, - CONF_NUMBER, - CONF_SERIAL_NUMBER, - CONF_TEMPERATURE_SENSORS, - DOMAIN, -) -from homeassistant.const import ( - CONF_NAME, - CONF_PORT, - CONF_SENSORS, - CONF_TEMPERATURE_UNIT, -) +from homeassistant.components.greeneye_monitor import DOMAIN from homeassistant.core import HomeAssistant from homeassistant.setup import async_setup_component from .common import ( + MULTI_MONITOR_CONFIG, SINGLE_MONITOR_CONFIG_NO_SENSORS, SINGLE_MONITOR_CONFIG_POWER_SENSORS, SINGLE_MONITOR_CONFIG_PULSE_COUNTERS, @@ -155,35 +144,12 @@ async def test_multi_monitor_config(hass: HomeAssistant, monitors: AsyncMock) -> """Test that component setup registers entities from multiple monitors correctly.""" assert await setup_greeneye_monitor_component_with_config( hass, - { - DOMAIN: { - CONF_PORT: 7513, - CONF_MONITORS: [ - { - CONF_SERIAL_NUMBER: "00000001", - CONF_TEMPERATURE_SENSORS: { - CONF_TEMPERATURE_UNIT: "C", - CONF_SENSORS: [ - {CONF_NUMBER: 1, CONF_NAME: "unit_1_temp_1"} - ], - }, - }, - { - CONF_SERIAL_NUMBER: "00000002", - CONF_TEMPERATURE_SENSORS: { - CONF_TEMPERATURE_UNIT: "F", - CONF_SENSORS: [ - {CONF_NUMBER: 1, CONF_NAME: "unit_2_temp_1"} - ], - }, - }, - ], - } - }, + MULTI_MONITOR_CONFIG, ) assert_temperature_sensor_registered(hass, 1, 1, "unit_1_temp_1") assert_temperature_sensor_registered(hass, 2, 1, "unit_2_temp_1") + assert_temperature_sensor_registered(hass, 3, 1, "unit_3_temp_1") async def test_setup_and_shutdown(hass: HomeAssistant, monitors: AsyncMock) -> None: diff --git a/tests/components/greeneye_monitor/test_sensor.py b/tests/components/greeneye_monitor/test_sensor.py index 9cd6f97fc38..ac1fe92873a 100644 --- a/tests/components/greeneye_monitor/test_sensor.py +++ b/tests/components/greeneye_monitor/test_sensor.py @@ -13,6 +13,7 @@ from homeassistant.helpers.entity_registry import ( ) from .common import ( + MULTI_MONITOR_CONFIG, SINGLE_MONITOR_CONFIG_POWER_SENSORS, SINGLE_MONITOR_CONFIG_PULSE_COUNTERS, SINGLE_MONITOR_CONFIG_TEMPERATURE_SENSORS, @@ -154,6 +155,17 @@ async def test_voltage_sensor(hass: HomeAssistant, monitors: AsyncMock) -> None: assert_sensor_state(hass, "sensor.voltage_1", "120.0") +async def test_multi_monitor_sensors(hass: HomeAssistant, monitors: AsyncMock) -> None: + """Test that sensors still work when multiple monitors are registered.""" + await setup_greeneye_monitor_component_with_config(hass, MULTI_MONITOR_CONFIG) + connect_monitor(monitors, 1) + connect_monitor(monitors, 2) + connect_monitor(monitors, 3) + assert_sensor_state(hass, "sensor.unit_1_temp_1", "32.0") + assert_sensor_state(hass, "sensor.unit_2_temp_1", "0.0") + assert_sensor_state(hass, "sensor.unit_3_temp_1", "32.0") + + def connect_monitor(monitors: AsyncMock, serial_number: int) -> MagicMock: """Simulate a monitor connecting to Home Assistant. Returns the mock monitor API object.""" monitor = mock_monitor(serial_number)