diff --git a/homeassistant/components/shelly/sensor.py b/homeassistant/components/shelly/sensor.py index 7dea45c0c1f47..743c7c7ff01e3 100644 --- a/homeassistant/components/shelly/sensor.py +++ b/homeassistant/components/shelly/sensor.py @@ -989,6 +989,24 @@ class RestSensorDescription(RestEntityDescription, SensorEntityDescription): or status[key]["counts"].get("xtotal") is None ), ), + "counter_frequency": RpcSensorDescription( + key="input", + sub_key="counts", + name="Pulse counter frequency", + native_unit_of_measurement=UnitOfFrequency.HERTZ, + state_class=SensorStateClass.MEASUREMENT, + value=lambda status, _: status["freq"], + removal_condition=lambda config, status, key: (config[key]["enable"] is False), + ), + "counter_frequency_value": RpcSensorDescription( + key="input", + sub_key="counts", + name="Pulse counter frequency value", + value=lambda status, _: status["xfreq"], + removal_condition=lambda config, status, key: ( + config[key]["enable"] is False or status[key]["counts"].get("xfreq") is None + ), + ), } diff --git a/tests/components/shelly/conftest.py b/tests/components/shelly/conftest.py index 8e41cbe060fdc..a16cc62fbaee9 100644 --- a/tests/components/shelly/conftest.py +++ b/tests/components/shelly/conftest.py @@ -226,7 +226,10 @@ def mock_white_light_set_state( "switch:0": {"output": True}, "input:0": {"id": 0, "state": None}, "input:1": {"id": 1, "percent": 89, "xpercent": 8.9}, - "input:2": {"id": 2, "counts": {"total": 56174, "xtotal": 561.74}}, + "input:2": { + "id": 2, + "counts": {"total": 56174, "xtotal": 561.74, "freq": 208.00, "xfreq": 6.11}, + }, "light:0": {"output": True, "brightness": 53.0}, "light:1": {"output": True, "brightness": 53.0}, "light:2": {"output": True, "brightness": 53.0}, diff --git a/tests/components/shelly/test_sensor.py b/tests/components/shelly/test_sensor.py index 036a5e0d70ee7..513bcd875e2a1 100644 --- a/tests/components/shelly/test_sensor.py +++ b/tests/components/shelly/test_sensor.py @@ -25,6 +25,7 @@ STATE_UNAVAILABLE, STATE_UNKNOWN, UnitOfEnergy, + UnitOfFrequency, ) from homeassistant.core import HomeAssistant, State from homeassistant.helpers.device_registry import DeviceRegistry @@ -801,3 +802,29 @@ async def test_rpc_disabled_xtotal_counter( entity_id = f"{SENSOR_DOMAIN}.gas_counter_value" assert hass.states.get(entity_id) is None + + +async def test_rpc_pulse_counter_frequency_sensors( + hass: HomeAssistant, + mock_rpc_device: Mock, + entity_registry: EntityRegistry, +) -> None: + """Test RPC counter sensor.""" + await init_integration(hass, 2) + + entity_id = f"{SENSOR_DOMAIN}.gas_pulse_counter_frequency" + state = hass.states.get(entity_id) + assert state.state == "208.0" + assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfFrequency.HERTZ + assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT + + entry = entity_registry.async_get(entity_id) + assert entry + assert entry.unique_id == "123456789ABC-input:2-counter_frequency" + + entity_id = f"{SENSOR_DOMAIN}.gas_pulse_counter_frequency_value" + assert hass.states.get(entity_id).state == "6.11" + + entry = entity_registry.async_get(entity_id) + assert entry + assert entry.unique_id == "123456789ABC-input:2-counter_frequency_value"