From 0a57968fdc39603f0c1791e02850c5a26d208d8c Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Thu, 9 Nov 2023 19:01:07 +0100 Subject: [PATCH] Update nut sensor tests to use parametrize (#103707) * Update nut sensor tests to use parametrize * Add missing model * Check on unique_id instead of config_entry_id * Separate tests --- tests/components/nut/test_sensor.py | 220 +++++----------------------- 1 file changed, 37 insertions(+), 183 deletions(-) diff --git a/tests/components/nut/test_sensor.py b/tests/components/nut/test_sensor.py index 4ec1e3c47ca..014e683b30c 100644 --- a/tests/components/nut/test_sensor.py +++ b/tests/components/nut/test_sensor.py @@ -1,6 +1,8 @@ """The sensor tests for the nut platform.""" from unittest.mock import patch +import pytest + from homeassistant.components.nut.const import DOMAIN from homeassistant.const import ( CONF_HOST, @@ -17,37 +19,25 @@ from .util import _get_mock_pynutclient, async_init_integration from tests.common import MockConfigEntry -async def test_pr3000rt2u(hass: HomeAssistant) -> None: - """Test creation of PR3000RT2U sensors.""" +@pytest.mark.parametrize( + "model", + [ + "CP1350C", + "5E650I", + "5E850I", + "CP1500PFCLCD", + "DL650ELCD", + "EATON5P1550", + "blazer_usb", + ], +) +async def test_devices( + hass: HomeAssistant, entity_registry: er.EntityRegistry, model: str +) -> None: + """Test creation of device sensors.""" - await async_init_integration(hass, "PR3000RT2U") - registry = er.async_get(hass) - entry = registry.async_get("sensor.ups1_battery_charge") - assert entry - assert entry.unique_id == "CPS_PR3000RT2U_PYVJO2000034_battery.charge" - - state = hass.states.get("sensor.ups1_battery_charge") - assert state.state == "100" - - expected_attributes = { - "device_class": "battery", - "friendly_name": "Ups1 Battery charge", - "unit_of_measurement": PERCENTAGE, - } - # Only test for a subset of attributes in case - # HA changes the implementation and a new one appears - assert all( - state.attributes[key] == attr for key, attr in expected_attributes.items() - ) - - -async def test_cp1350c(hass: HomeAssistant) -> None: - """Test creation of CP1350C sensors.""" - - config_entry = await async_init_integration(hass, "CP1350C") - - registry = er.async_get(hass) - entry = registry.async_get("sensor.ups1_battery_charge") + config_entry = await async_init_integration(hass, model) + entry = entity_registry.async_get("sensor.ups1_battery_charge") assert entry assert entry.unique_id == f"{config_entry.entry_id}_battery.charge" @@ -66,161 +56,25 @@ async def test_cp1350c(hass: HomeAssistant) -> None: ) -async def test_5e850i(hass: HomeAssistant) -> None: - """Test creation of 5E850I sensors.""" +@pytest.mark.parametrize( + ("model", "unique_id"), + [ + ("PR3000RT2U", "CPS_PR3000RT2U_PYVJO2000034_battery.charge"), + ( + "BACKUPSES600M1", + "American Power Conversion_Back-UPS ES 600M1_4B1713P32195 _battery.charge", + ), + ], +) +async def test_devices_with_unique_ids( + hass: HomeAssistant, entity_registry: er.EntityRegistry, model: str, unique_id: str +) -> None: + """Test creation of device sensors with unique ids.""" - config_entry = await async_init_integration(hass, "5E850I") - registry = er.async_get(hass) - entry = registry.async_get("sensor.ups1_battery_charge") + await async_init_integration(hass, model) + entry = entity_registry.async_get("sensor.ups1_battery_charge") assert entry - assert entry.unique_id == f"{config_entry.entry_id}_battery.charge" - - state = hass.states.get("sensor.ups1_battery_charge") - assert state.state == "100" - - expected_attributes = { - "device_class": "battery", - "friendly_name": "Ups1 Battery charge", - "unit_of_measurement": PERCENTAGE, - } - # Only test for a subset of attributes in case - # HA changes the implementation and a new one appears - assert all( - state.attributes[key] == attr for key, attr in expected_attributes.items() - ) - - -async def test_5e650i(hass: HomeAssistant) -> None: - """Test creation of 5E650I sensors.""" - - config_entry = await async_init_integration(hass, "5E650I") - registry = er.async_get(hass) - entry = registry.async_get("sensor.ups1_battery_charge") - assert entry - assert entry.unique_id == f"{config_entry.entry_id}_battery.charge" - - state = hass.states.get("sensor.ups1_battery_charge") - assert state.state == "100" - - expected_attributes = { - "device_class": "battery", - "friendly_name": "Ups1 Battery charge", - "unit_of_measurement": PERCENTAGE, - } - # Only test for a subset of attributes in case - # HA changes the implementation and a new one appears - assert all( - state.attributes[key] == attr for key, attr in expected_attributes.items() - ) - - -async def test_backupsses600m1(hass: HomeAssistant) -> None: - """Test creation of BACKUPSES600M1 sensors.""" - - await async_init_integration(hass, "BACKUPSES600M1") - registry = er.async_get(hass) - entry = registry.async_get("sensor.ups1_battery_charge") - assert entry - assert ( - entry.unique_id - == "American Power Conversion_Back-UPS ES 600M1_4B1713P32195 _battery.charge" - ) - - state = hass.states.get("sensor.ups1_battery_charge") - assert state.state == "100" - - expected_attributes = { - "device_class": "battery", - "friendly_name": "Ups1 Battery charge", - "unit_of_measurement": PERCENTAGE, - } - # Only test for a subset of attributes in case - # HA changes the implementation and a new one appears - assert all( - state.attributes[key] == attr for key, attr in expected_attributes.items() - ) - - -async def test_cp1500pfclcd(hass: HomeAssistant) -> None: - """Test creation of CP1500PFCLCD sensors.""" - - config_entry = await async_init_integration(hass, "CP1500PFCLCD") - registry = er.async_get(hass) - entry = registry.async_get("sensor.ups1_battery_charge") - assert entry - assert entry.unique_id == f"{config_entry.entry_id}_battery.charge" - - state = hass.states.get("sensor.ups1_battery_charge") - assert state.state == "100" - - expected_attributes = { - "device_class": "battery", - "friendly_name": "Ups1 Battery charge", - "unit_of_measurement": PERCENTAGE, - } - # Only test for a subset of attributes in case - # HA changes the implementation and a new one appears - assert all( - state.attributes[key] == attr for key, attr in expected_attributes.items() - ) - - -async def test_dl650elcd(hass: HomeAssistant) -> None: - """Test creation of DL650ELCD sensors.""" - - config_entry = await async_init_integration(hass, "DL650ELCD") - registry = er.async_get(hass) - entry = registry.async_get("sensor.ups1_battery_charge") - assert entry - assert entry.unique_id == f"{config_entry.entry_id}_battery.charge" - - state = hass.states.get("sensor.ups1_battery_charge") - assert state.state == "100" - - expected_attributes = { - "device_class": "battery", - "friendly_name": "Ups1 Battery charge", - "unit_of_measurement": PERCENTAGE, - } - # Only test for a subset of attributes in case - # HA changes the implementation and a new one appears - assert all( - state.attributes[key] == attr for key, attr in expected_attributes.items() - ) - - -async def test_eaton5p1550(hass: HomeAssistant) -> None: - """Test creation of EATON5P1550 sensors.""" - - config_entry = await async_init_integration(hass, "EATON5P1550") - registry = er.async_get(hass) - entry = registry.async_get("sensor.ups1_battery_charge") - assert entry - assert entry.unique_id == f"{config_entry.entry_id}_battery.charge" - - state = hass.states.get("sensor.ups1_battery_charge") - assert state.state == "100" - - expected_attributes = { - "device_class": "battery", - "friendly_name": "Ups1 Battery charge", - "unit_of_measurement": PERCENTAGE, - } - # Only test for a subset of attributes in case - # HA changes the implementation and a new one appears - assert all( - state.attributes[key] == attr for key, attr in expected_attributes.items() - ) - - -async def test_blazer_usb(hass: HomeAssistant) -> None: - """Test creation of blazer_usb sensors.""" - - config_entry = await async_init_integration(hass, "blazer_usb") - registry = er.async_get(hass) - entry = registry.async_get("sensor.ups1_battery_charge") - assert entry - assert entry.unique_id == f"{config_entry.entry_id}_battery.charge" + assert entry.unique_id == unique_id state = hass.states.get("sensor.ups1_battery_charge") assert state.state == "100"