diff --git a/homeassistant/components/shelly/sensor.py b/homeassistant/components/shelly/sensor.py index dd0ace9a6b91d61d276d8001cffea6c9a80cf54e..03ce080db8e83fab7568fa75061e106dbe3860ff 100644 --- a/homeassistant/components/shelly/sensor.py +++ b/homeassistant/components/shelly/sensor.py @@ -1116,6 +1116,15 @@ RPC_SENSORS: Final = { state_class=SensorStateClass.MEASUREMENT, available=lambda status: status is not None, ), + "voltmeter_value": RpcSensorDescription( + key="voltmeter", + sub_key="xvoltage", + name="Voltmeter value", + removal_condition=lambda _config, status, key: ( + status[key].get("xvoltage") is None + ), + unit=lambda config: config["xvoltage"]["unit"] or None, + ), "analoginput": RpcSensorDescription( key="input", sub_key="percent", diff --git a/tests/components/shelly/conftest.py b/tests/components/shelly/conftest.py index d453d25698c01e9d8d456ed60363746e1cc52a49..b2550c2b9d43552727061cc3c67e8c86aab92196 100644 --- a/tests/components/shelly/conftest.py +++ b/tests/components/shelly/conftest.py @@ -199,6 +199,7 @@ MOCK_CONFIG = { }, "wifi": {"sta": {"enable": True}, "sta1": {"enable": False}}, "ws": {"enable": False, "server": None}, + "voltmeter:100": {"xvoltage": {"unit": "ppm"}}, } MOCK_SHELLY_COAP = { @@ -280,7 +281,7 @@ MOCK_STATUS_RPC = { }, "relay_in_thermostat": True, }, - "voltmeter": {"voltage": 4.321}, + "voltmeter:100": {"voltage": 4.321, "xvoltage": 12.34}, "wifi": {"rssi": -63}, } diff --git a/tests/components/shelly/test_sensor.py b/tests/components/shelly/test_sensor.py index 18c3d874c559da795853b89ec3a5e3cea10ef290..c62f21d9c8f9883b1ca297fe89c86f6e3dfb0e82 100644 --- a/tests/components/shelly/test_sensor.py +++ b/tests/components/shelly/test_sensor.py @@ -428,14 +428,16 @@ async def test_rpc_sensor_error( assert hass.states.get(entity_id).state == "4.321" - mutate_rpc_device_status(monkeypatch, mock_rpc_device, "voltmeter", "voltage", None) + mutate_rpc_device_status( + monkeypatch, mock_rpc_device, "voltmeter:100", "voltage", None + ) mock_rpc_device.mock_update() assert hass.states.get(entity_id).state == STATE_UNAVAILABLE entry = entity_registry.async_get(entity_id) assert entry - assert entry.unique_id == "123456789ABC-voltmeter-voltmeter" + assert entry.unique_id == "123456789ABC-voltmeter:100-voltmeter" async def test_rpc_polling_sensor( @@ -1383,3 +1385,23 @@ async def test_rpc_device_sensor_goes_unavailable_on_disconnect( await hass.async_block_till_done() temp_sensor_state = hass.states.get("sensor.test_name_temperature") assert temp_sensor_state.state != STATE_UNAVAILABLE + + +async def test_rpc_voltmeter_value( + hass: HomeAssistant, + mock_rpc_device: Mock, + entity_registry: EntityRegistry, +) -> None: + """Test RPC voltmeter value sensor.""" + entity_id = f"{SENSOR_DOMAIN}.test_name_voltmeter_value" + + await init_integration(hass, 2) + + state = hass.states.get(entity_id) + + assert state.state == "12.34" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "ppm" + + entry = entity_registry.async_get(entity_id) + assert entry + assert entry.unique_id == "123456789ABC-voltmeter:100-voltmeter_value"