Skip to content

Commit

Permalink
Add pulse counter frequency sensors to Shelly (#119898)
Browse files Browse the repository at this point in the history
* Add pulse counter frequency sensors

* Cleaning

---------

Co-authored-by: Maciej Bieniek <478555+bieniu@users.noreply.github.com>
  • Loading branch information
bieniu and bieniu committed Jun 18, 2024
1 parent 3d45ced commit 54f8b5a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
18 changes: 18 additions & 0 deletions homeassistant/components/shelly/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
),
}


Expand Down
5 changes: 4 additions & 1 deletion tests/components/shelly/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
27 changes: 27 additions & 0 deletions tests/components/shelly/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
STATE_UNAVAILABLE,
STATE_UNKNOWN,
UnitOfEnergy,
UnitOfFrequency,
)
from homeassistant.core import HomeAssistant, State
from homeassistant.helpers.device_registry import DeviceRegistry
Expand Down Expand Up @@ -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"

0 comments on commit 54f8b5a

Please sign in to comment.