Add missing sensors for Shelly Plus RGBW PM (#123589)

* Add missing sensors for Shelly Plus RGBW PM

* Add tests
This commit is contained in:
Maciej Bieniek
2024-08-18 12:58:51 +03:00
committed by GitHub
parent 59d72bb4ed
commit ec0012209d
2 changed files with 196 additions and 0 deletions
+90
View File
@@ -25,8 +25,12 @@ from homeassistant.const import (
PERCENTAGE,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
UnitOfElectricCurrent,
UnitOfElectricPotential,
UnitOfEnergy,
UnitOfFrequency,
UnitOfPower,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant, State
from homeassistant.helpers.device_registry import DeviceRegistry
@@ -1189,3 +1193,89 @@ async def test_rpc_remove_enum_virtual_sensor_when_orphaned(
entry = entity_registry.async_get(entity_id)
assert not entry
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
@pytest.mark.parametrize("light_type", ["rgb", "rgbw"])
async def test_rpc_rgbw_sensors(
hass: HomeAssistant,
entity_registry: EntityRegistry,
mock_rpc_device: Mock,
monkeypatch: pytest.MonkeyPatch,
light_type: str,
) -> None:
"""Test sensors for RGB/RGBW light."""
config = deepcopy(mock_rpc_device.config)
config[f"{light_type}:0"] = {"id": 0}
monkeypatch.setattr(mock_rpc_device, "config", config)
status = deepcopy(mock_rpc_device.status)
status[f"{light_type}:0"] = {
"temperature": {"tC": 54.3, "tF": 129.7},
"aenergy": {"total": 45.141},
"apower": 12.2,
"current": 0.23,
"voltage": 12.4,
}
monkeypatch.setattr(mock_rpc_device, "status", status)
await init_integration(hass, 2)
entity_id = "sensor.test_name_power"
state = hass.states.get(entity_id)
assert state
assert state.state == "12.2"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT
entry = entity_registry.async_get(entity_id)
assert entry
assert entry.unique_id == f"123456789ABC-{light_type}:0-power_{light_type}"
entity_id = "sensor.test_name_energy"
state = hass.states.get(entity_id)
assert state
assert state.state == "0.045141"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR
entry = entity_registry.async_get(entity_id)
assert entry
assert entry.unique_id == f"123456789ABC-{light_type}:0-energy_{light_type}"
entity_id = "sensor.test_name_current"
state = hass.states.get(entity_id)
assert state
assert state.state == "0.23"
assert (
state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfElectricCurrent.AMPERE
)
entry = entity_registry.async_get(entity_id)
assert entry
assert entry.unique_id == f"123456789ABC-{light_type}:0-current_{light_type}"
entity_id = "sensor.test_name_voltage"
state = hass.states.get(entity_id)
assert state
assert state.state == "12.4"
assert (
state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfElectricPotential.VOLT
)
entry = entity_registry.async_get(entity_id)
assert entry
assert entry.unique_id == f"123456789ABC-{light_type}:0-voltage_{light_type}"
entity_id = "sensor.test_name_device_temperature"
state = hass.states.get(entity_id)
assert state
assert state.state == "54.3"
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfTemperature.CELSIUS
entry = entity_registry.async_get(entity_id)
assert entry
assert entry.unique_id == f"123456789ABC-{light_type}:0-temperature_{light_type}"