Add support for xvoltage sensor for Shelly Plus UNI (#134261)

* Add support for xvoltage sensor

* Cleaning
This commit is contained in:
Maciej Bieniek 2025-01-03 14:27:47 +00:00 committed by GitHub
parent 9320ccfa4f
commit e43f72c452
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 3 deletions

View File

@ -1116,6 +1116,15 @@ RPC_SENSORS: Final = {
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
available=lambda status: status is not None, 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( "analoginput": RpcSensorDescription(
key="input", key="input",
sub_key="percent", sub_key="percent",

View File

@ -199,6 +199,7 @@ MOCK_CONFIG = {
}, },
"wifi": {"sta": {"enable": True}, "sta1": {"enable": False}}, "wifi": {"sta": {"enable": True}, "sta1": {"enable": False}},
"ws": {"enable": False, "server": None}, "ws": {"enable": False, "server": None},
"voltmeter:100": {"xvoltage": {"unit": "ppm"}},
} }
MOCK_SHELLY_COAP = { MOCK_SHELLY_COAP = {
@ -280,7 +281,7 @@ MOCK_STATUS_RPC = {
}, },
"relay_in_thermostat": True, "relay_in_thermostat": True,
}, },
"voltmeter": {"voltage": 4.321}, "voltmeter:100": {"voltage": 4.321, "xvoltage": 12.34},
"wifi": {"rssi": -63}, "wifi": {"rssi": -63},
} }

View File

@ -428,14 +428,16 @@ async def test_rpc_sensor_error(
assert hass.states.get(entity_id).state == "4.321" 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() mock_rpc_device.mock_update()
assert hass.states.get(entity_id).state == STATE_UNAVAILABLE assert hass.states.get(entity_id).state == STATE_UNAVAILABLE
entry = entity_registry.async_get(entity_id) entry = entity_registry.async_get(entity_id)
assert entry assert entry
assert entry.unique_id == "123456789ABC-voltmeter-voltmeter" assert entry.unique_id == "123456789ABC-voltmeter:100-voltmeter"
async def test_rpc_polling_sensor( 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() await hass.async_block_till_done()
temp_sensor_state = hass.states.get("sensor.test_name_temperature") temp_sensor_state = hass.states.get("sensor.test_name_temperature")
assert temp_sensor_state.state != STATE_UNAVAILABLE 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"