1- -- Copyright 2022 SmartThings
1+ -- Copyright 2025 SmartThings
22--
33-- Licensed under the Apache License, Version 2.0 (the "License");
44-- you may not use this file except in compliance with the License.
5151local TEMP_BOUND_RECEIVED = " __temp_bound_received"
5252local TEMP_MIN = " __temp_min"
5353local TEMP_MAX = " __temp_max"
54+ local FLOW_BOUND_RECEIVED = " __flow_bound_received"
55+ local FLOW_MIN = " __flow_min"
56+ local FLOW_MAX = " __flow_max"
5457
5558local battery_support = {
5659 NO_BATTERY = " NO_BATTERY" ,
@@ -148,6 +151,10 @@ local function match_profile(driver, device, battery_supported)
148151 profile_name = profile_name .. " -leak"
149152 end
150153
154+ if device :supports_capability (capabilities .flowMeasurement ) then
155+ profile_name = profile_name .. " -flow"
156+ end
157+
151158 if battery_supported == battery_support .BATTERY_PERCENTAGE then
152159 profile_name = profile_name .. " -battery"
153160 elseif battery_supported == battery_support .BATTERY_LEVEL then
@@ -356,6 +363,37 @@ local function pressure_attr_handler(driver, device, ib, response)
356363 end
357364end
358365
366+ local function flow_attr_handler (driver , device , ib , response )
367+ local measured_value = ib .data .value
368+ if measured_value ~= nil then
369+ local flow = measured_value / 10.0
370+ local unit = " m^3/h"
371+ device :emit_event_for_endpoint (ib .endpoint_id , capabilities .flowMeasurement .flow ({value = flow , unit = unit }))
372+ end
373+ end
374+
375+ local flow_attr_handler_factory = function (minOrMax )
376+ return function (driver , device , ib , response )
377+ if ib .data .value == nil then
378+ return
379+ end
380+ local flow_bound = ib .data .value / 10.0
381+ local unit = " m^3/h"
382+ set_field_for_endpoint (device , FLOW_BOUND_RECEIVED .. minOrMax , ib .endpoint_id , flow_bound )
383+ local min = get_field_for_endpoint (device , FLOW_BOUND_RECEIVED .. FLOW_MIN , ib .endpoint_id )
384+ local max = get_field_for_endpoint (device , FLOW_BOUND_RECEIVED .. FLOW_MAX , ib .endpoint_id )
385+ if min ~= nil and max ~= nil then
386+ if min < max then
387+ device :emit_event_for_endpoint (ib .endpoint_id , capabilities .flowMeasurement .flowRange ({ value = { minimum = min , maximum = max }, unit = unit }))
388+ set_field_for_endpoint (device , FLOW_BOUND_RECEIVED .. FLOW_MIN , ib .endpoint_id , nil )
389+ set_field_for_endpoint (device , FLOW_BOUND_RECEIVED .. FLOW_MAX , ib .endpoint_id , nil )
390+ else
391+ device .log .warn_with ({hub_logs = true }, string.format (" Device reported a min flow measurement %d that is not lower than the reported max flow measurement %d" , min , max ))
392+ end
393+ end
394+ end
395+ end
396+
359397local matter_driver_template = {
360398 lifecycle_handlers = {
361399 init = device_init ,
@@ -395,6 +433,11 @@ local matter_driver_template = {
395433 },
396434 [clusters .Thermostat .ID ] = {
397435 [clusters .Thermostat .attributes .LocalTemperature .ID ] = temperature_attr_handler
436+ },
437+ [clusters .FlowMeasurement .ID ] = {
438+ [clusters .FlowMeasurement .attributes .MeasuredValue .ID ] = flow_attr_handler ,
439+ [clusters .FlowMeasurement .attributes .MinMeasuredValue .ID ] = flow_attr_handler_factory (FLOW_MIN ),
440+ [clusters .FlowMeasurement .attributes .MaxMeasuredValue .ID ] = flow_attr_handler_factory (FLOW_MAX )
398441 }
399442 }
400443 },
@@ -525,6 +568,11 @@ local matter_driver_template = {
525568 [capabilities .rainSensor .ID ] = {
526569 clusters .BooleanState .attributes .StateValue ,
527570 },
571+ [capabilities .flowMeasurement .ID ] = {
572+ clusters .FlowMeasurement .attributes .MeasuredValue ,
573+ clusters .FlowMeasurement .attributes .MinMeasuredValue ,
574+ clusters .FlowMeasurement .attributes .MaxMeasuredValue
575+ },
528576 },
529577 capability_handlers = {
530578 },
@@ -540,7 +588,8 @@ local matter_driver_template = {
540588 capabilities .waterSensor ,
541589 capabilities .temperatureAlarm ,
542590 capabilities .rainSensor ,
543- capabilities .hardwareFault
591+ capabilities .hardwareFault ,
592+ capabilities .flowMeasurement
544593 },
545594 sub_drivers = {
546595 require (" air-quality-sensor" ),
0 commit comments