Add pulse counter frequency sensors to Shelly (#119898)

* Add pulse counter frequency sensors

* Cleaning

---------

Co-authored-by: Maciej Bieniek <478555+bieniu@users.noreply.github.com>
This commit is contained in:
Maciej Bieniek 2024-06-18 23:08:30 +02:00 committed by GitHub
parent 3d45ced02e
commit 54f8b5afdf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 49 additions and 1 deletions

View file

@ -989,6 +989,24 @@ RPC_SENSORS: Final = {
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
),
),
}

View file

@ -226,7 +226,10 @@ MOCK_STATUS_RPC = {
"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},

View file

@ -25,6 +25,7 @@ from homeassistant.const import (
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"