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
This commit is contained in:
Jan-Philipp Benecke 2023-11-09 19:01:07 +01:00 committed by GitHub
parent 81909f7ddf
commit 0a57968fdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,8 @@
"""The sensor tests for the nut platform.""" """The sensor tests for the nut platform."""
from unittest.mock import patch from unittest.mock import patch
import pytest
from homeassistant.components.nut.const import DOMAIN from homeassistant.components.nut.const import DOMAIN
from homeassistant.const import ( from homeassistant.const import (
CONF_HOST, CONF_HOST,
@ -17,37 +19,25 @@ from .util import _get_mock_pynutclient, async_init_integration
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
async def test_pr3000rt2u(hass: HomeAssistant) -> None: @pytest.mark.parametrize(
"""Test creation of PR3000RT2U sensors.""" "model",
[
await async_init_integration(hass, "PR3000RT2U") "CP1350C",
registry = er.async_get(hass) "5E650I",
entry = registry.async_get("sensor.ups1_battery_charge") "5E850I",
assert entry "CP1500PFCLCD",
assert entry.unique_id == "CPS_PR3000RT2U_PYVJO2000034_battery.charge" "DL650ELCD",
"EATON5P1550",
state = hass.states.get("sensor.ups1_battery_charge") "blazer_usb",
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_devices(
hass: HomeAssistant, entity_registry: er.EntityRegistry, model: str
) -> None:
"""Test creation of device sensors."""
config_entry = await async_init_integration(hass, model)
async def test_cp1350c(hass: HomeAssistant) -> None: entry = entity_registry.async_get("sensor.ups1_battery_charge")
"""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")
assert entry assert entry
assert entry.unique_id == f"{config_entry.entry_id}_battery.charge" 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: @pytest.mark.parametrize(
"""Test creation of 5E850I sensors.""" ("model", "unique_id"),
[
config_entry = await async_init_integration(hass, "5E850I") ("PR3000RT2U", "CPS_PR3000RT2U_PYVJO2000034_battery.charge"),
registry = er.async_get(hass) (
entry = registry.async_get("sensor.ups1_battery_charge") "BACKUPSES600M1",
assert entry "American Power Conversion_Back-UPS ES 600M1_4B1713P32195 _battery.charge",
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_devices_with_unique_ids(
hass: HomeAssistant, entity_registry: er.EntityRegistry, model: str, unique_id: str
) -> None:
"""Test creation of device sensors with unique ids."""
await async_init_integration(hass, model)
async def test_5e650i(hass: HomeAssistant) -> None: entry = entity_registry.async_get("sensor.ups1_battery_charge")
"""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
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"
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"
state = hass.states.get("sensor.ups1_battery_charge") state = hass.states.get("sensor.ups1_battery_charge")
assert state.state == "100" assert state.state == "100"