Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update nut sensor tests to use parametrize #103707

Merged
merged 4 commits into from
Nov 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
220 changes: 37 additions & 183 deletions tests/components/nut/test_sensor.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -17,184 +19,25 @@
from tests.common import MockConfigEntry


async def test_pr3000rt2u(hass: HomeAssistant) -> None:
"""Test creation of PR3000RT2U 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")
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_5e850i(hass: HomeAssistant) -> None:
"""Test creation of 5E850I sensors."""

config_entry = await async_init_integration(hass, "5E850I")
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_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."""
@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."""

config_entry = await async_init_integration(hass, "EATON5P1550")
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"

Expand All @@ -213,14 +56,25 @@ async def test_eaton5p1550(hass: HomeAssistant) -> None:
)


async def test_blazer_usb(hass: HomeAssistant) -> None:
"""Test creation of blazer_usb 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, "blazer_usb")
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"
assert entry.unique_id == unique_id

state = hass.states.get("sensor.ups1_battery_charge")
assert state.state == "100"
Expand Down