Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flow volume sensor entity for the FlowMeasurement cluster #187

Merged
merged 5 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ async def async_test_humidity(
assert_state(entity, 10.0, "%")


async def async_test_flow(
zha_gateway: Gateway, cluster: Cluster, entity: PlatformEntity
) -> None:
"""Test flow sensor."""
await send_attributes_report(zha_gateway, cluster, {1: 1, 0: 40})
assert_state(entity, 4.0, "m³/h")

await send_attributes_report(zha_gateway, cluster, {1: 1, 0: 0xFFFF})
assert_state(entity, None, "m³/h")


async def async_test_temperature(
zha_gateway: Gateway, cluster: Cluster, entity: PlatformEntity
) -> None:
Expand Down Expand Up @@ -396,6 +407,13 @@ async def async_test_pi_heating_demand(
None,
None,
),
(
measurement.FlowMeasurement.cluster_id,
sensor.Flow,
async_test_flow,
None,
None,
),
(
measurement.TemperatureMeasurement.cluster_id,
sensor.Temperature,
Expand Down
18 changes: 18 additions & 0 deletions zha/application/platforms/sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
CLUSTER_HANDLER_DEVICE_TEMPERATURE,
CLUSTER_HANDLER_DIAGNOSTIC,
CLUSTER_HANDLER_ELECTRICAL_MEASUREMENT,
CLUSTER_HANDLER_FLOW,
CLUSTER_HANDLER_HUMIDITY,
CLUSTER_HANDLER_ILLUMINANCE,
CLUSTER_HANDLER_LEAF_WETNESS,
Expand Down Expand Up @@ -1116,6 +1117,23 @@ class Pressure(Sensor):
_attr_native_unit_of_measurement = UnitOfPressure.HPA


@MULTI_MATCH(cluster_handler_names=CLUSTER_HANDLER_FLOW)
class Flow(Sensor):
"""Flow Measurement sensor."""

_attribute_name = "measured_value"
_attr_device_class: SensorDeviceClass = SensorDeviceClass.VOLUME_FLOW_RATE
_attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT
_divisor = 10
_attr_native_unit_of_measurement = UnitOfVolumeFlowRate.CUBIC_METERS_PER_HOUR

def formatter(self, value: int) -> int | float | str | None:
"""Handle unknown value state."""
if value == 0xFFFF:
return None
return super().formatter(value)


@MULTI_MATCH(cluster_handler_names=CLUSTER_HANDLER_TEMPERATURE)
class Temperature(Sensor):
"""Temperature Sensor."""
Expand Down
1 change: 1 addition & 0 deletions zha/zigbee/cluster_handlers/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
CLUSTER_HANDLER_ELECTRICAL_MEASUREMENT: Final[str] = "electrical_measurement"
CLUSTER_HANDLER_EVENT_RELAY: Final[str] = "event_relay"
CLUSTER_HANDLER_FAN: Final[str] = "fan"
CLUSTER_HANDLER_FLOW: Final[str] = "flow"
CLUSTER_HANDLER_HUMIDITY: Final[str] = "humidity"
CLUSTER_HANDLER_HUE_OCCUPANCY: Final[str] = "philips_occupancy"
CLUSTER_HANDLER_SOIL_MOISTURE: Final[str] = "soil_moisture"
Expand Down
Loading