Files
core/tests/components/plugwise/test_sensor.py
T
2026-11-15 10:21:30 +01:00

236 lines
8.6 KiB
Python

"""Tests for the Plugwise Sensor integration."""
import pytest
from pytest_homeassistant.common import MockConfigEntry, snapshot_platform
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.plugwise.const import DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
@pytest.mark.usefixtures("mock_smile_adam_heat_cool")
@pytest.mark.parametrize("chosen_env", ["m_adam_heating"], indirect=True)
@pytest.mark.parametrize("cooling_present", [False], indirect=True)
@pytest.mark.parametrize("platforms", [(SENSOR_DOMAIN,)])
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_adam_sensor_snapshot(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
setup_platform: MockConfigEntry,
) -> None:
"""Test Adam sensor snapshot."""
await snapshot_platform(hass, entity_registry, snapshot, setup_platform.entry_id)
@pytest.mark.usefixtures("mock_smile_adam_jip")
@pytest.mark.parametrize("platforms", [(SENSOR_DOMAIN,)])
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_adam_jip_heater_sensor_snapshot(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
setup_platform: MockConfigEntry,
) -> None:
"""Test Adam Jip OpenTherm heater sensor snapshot."""
# Only the heater: zones and thermostats share names, so their entity_ids
# depend on the order in which the devices are added.
device = device_registry.async_get_device_by_identifier(
(DOMAIN, "e4684553153b44afbef2200885f379dc"), setup_platform.entry_id
)
assert device
entries = er.async_entries_for_device(entity_registry, device.id)
assert entries
for entry in sorted(entries, key=lambda entry: entry.entity_id):
assert entry == snapshot(name=f"{entry.entity_id}-entry")
assert hass.states.get(entry.entity_id) == snapshot(
name=f"{entry.entity_id}-state"
)
@pytest.mark.usefixtures("mock_smile_adam_jip")
async def test_adam_climate_sensor_humidity(
hass: HomeAssistant,
init_integration: MockConfigEntry,
) -> None:
"""Test creation of climate related humidity sensor entity."""
state = hass.states.get("sensor.woonkamer_humidity")
assert state
assert float(state.state) == 56.2
@pytest.mark.usefixtures("mock_smile_adam_jip")
async def test_unique_id_migration_humidity(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test unique ID migration of -relative_humidity to -humidity."""
mock_config_entry.add_to_hass(hass)
# Entry to migrate
entity_registry.async_get_or_create(
SENSOR_DOMAIN,
DOMAIN,
"f61f1a2535f54f52ad006a3d18e459ca-relative_humidity",
config_entry=mock_config_entry,
suggested_object_id="woonkamer_humidity",
disabled_by=None,
)
# Entry not needing migration
entity_registry.async_get_or_create(
SENSOR_DOMAIN,
DOMAIN,
"f61f1a2535f54f52ad006a3d18e459ca-battery",
config_entry=mock_config_entry,
suggested_object_id="woonkamer_battery",
disabled_by=None,
)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert hass.states.get("sensor.woonkamer_humidity") is not None
assert hass.states.get("sensor.woonkamer_battery") is not None
entity_entry = entity_registry.async_get("sensor.woonkamer_humidity")
assert entity_entry
assert entity_entry.unique_id == "f61f1a2535f54f52ad006a3d18e459ca-humidity"
entity_entry = entity_registry.async_get("sensor.woonkamer_battery")
assert entity_entry
assert entity_entry.unique_id == "f61f1a2535f54f52ad006a3d18e459ca-battery"
@pytest.mark.usefixtures("mock_smile_anna")
@pytest.mark.parametrize("chosen_env", ["anna_heatpump_heating"], indirect=True)
@pytest.mark.parametrize("cooling_present", [True], indirect=True)
@pytest.mark.parametrize("platforms", [(SENSOR_DOMAIN,)])
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_anna_sensor_snapshot(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
setup_platform: MockConfigEntry,
) -> None:
"""Test Anna sensor snapshot."""
await snapshot_platform(hass, entity_registry, snapshot, setup_platform.entry_id)
@pytest.mark.usefixtures("mock_smile_anna")
@pytest.mark.parametrize("chosen_env", ["anna_heatpump_heating"], indirect=True)
@pytest.mark.parametrize("cooling_present", [True], indirect=True)
@pytest.mark.parametrize("platforms", [(SENSOR_DOMAIN,)])
@pytest.mark.parametrize(
"key",
[
"dhw_flow",
"oem_fault_code",
"fault_flags",
],
)
async def test_heater_sensors_disabled_by_default(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
setup_platform: MockConfigEntry,
key: str,
) -> None:
"""Test the optional heater sensors are created but disabled by default."""
entity_id = entity_registry.async_get_entity_id(
SENSOR_DOMAIN, DOMAIN, f"1cbf783bb11e4a7c8a6843dee3a86927-{key}"
)
assert entity_id
entry = entity_registry.async_get(entity_id)
assert entry
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
assert hass.states.get(entity_id) is None
@pytest.mark.usefixtures("mock_smile_anna_p1")
@pytest.mark.parametrize("platforms", [(SENSOR_DOMAIN,)])
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_anna_p1_sensor_snapshot(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
setup_platform: MockConfigEntry,
) -> None:
"""Test Anna P1 sensor snapshot."""
await snapshot_platform(hass, entity_registry, snapshot, setup_platform.entry_id)
@pytest.mark.usefixtures("mock_smile_p1")
@pytest.mark.parametrize("chosen_env", ["p1v4_442_single"], indirect=True)
@pytest.mark.parametrize(
"gateway_id", ["a455b61e52394b2db5081ce025a430f3"], indirect=True
)
@pytest.mark.parametrize("platforms", [(SENSOR_DOMAIN,)])
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_p1_dsmr_sensor_snapshot(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
setup_platform: MockConfigEntry,
) -> None:
"""Test P1 1-phase sensor snapshot."""
await snapshot_platform(hass, entity_registry, snapshot, setup_platform.entry_id)
@pytest.mark.usefixtures("mock_smile_p1")
@pytest.mark.parametrize("chosen_env", ["p1v4_442_triple"], indirect=True)
@pytest.mark.parametrize(
"gateway_id", ["03e65b16e4b247a29ae0d75a78cb492e"], indirect=True
)
@pytest.mark.parametrize("platforms", [(SENSOR_DOMAIN,)])
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_p1_3ph_dsmr_sensor_snapshot(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
setup_platform: MockConfigEntry,
) -> None:
"""Test P1 3-phase sensor snapshot."""
await snapshot_platform(hass, entity_registry, snapshot, setup_platform.entry_id)
@pytest.mark.usefixtures("mock_smile_p1")
@pytest.mark.parametrize("chosen_env", ["p1v4_442_triple"], indirect=True)
@pytest.mark.parametrize(
"gateway_id", ["03e65b16e4b247a29ae0d75a78cb492e"], indirect=True
)
async def test_p1_3ph_dsmr_sensor_disabled_entities(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
init_integration: MockConfigEntry,
) -> None:
"""Test disabled power related sensor entities intent."""
entity_id = "sensor.p1_voltage_phase_one"
state = hass.states.get(entity_id)
assert not state
entity_registry.async_update_entity(entity_id=entity_id, disabled_by=None)
await hass.async_block_till_done()
await hass.config_entries.async_reload(init_integration.entry_id)
await hass.async_block_till_done()
state = hass.states.get("sensor.p1_voltage_phase_one")
assert state
assert float(state.state) == 233.2
@pytest.mark.usefixtures("mock_stretch")
@pytest.mark.parametrize("platforms", [(SENSOR_DOMAIN,)])
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_stretch_sensor_snapshot(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
setup_platform: MockConfigEntry,
) -> None:
"""Test Stretch sensor snapshot."""
await snapshot_platform(hass, entity_registry, snapshot, setup_platform.entry_id)