From e43f72c452a7f0b84392ecc8f0a802661d8a4a2e Mon Sep 17 00:00:00 2001 From: Maciej Bieniek <bieniu@users.noreply.github.com> Date: Fri, 3 Jan 2025 14:27:47 +0000 Subject: [PATCH] Add support for `xvoltage` sensor for Shelly Plus UNI (#134261) * Add support for xvoltage sensor * Cleaning --- homeassistant/components/shelly/sensor.py | 9 ++++++++ tests/components/shelly/conftest.py | 3 ++- tests/components/shelly/test_sensor.py | 26 +++++++++++++++++++++-- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/shelly/sensor.py b/homeassistant/components/shelly/sensor.py index dd0ace9a6b9..03ce080db8e 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 d453d25698c..b2550c2b9d4 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 18c3d874c55..c62f21d9c8f 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" -- GitLab