1
- -- Copyright 2022 SmartThings
1
+ -- Copyright 2025 SmartThings
2
2
--
3
3
-- Licensed under the Apache License, Version 2.0 (the "License");
4
4
-- you may not use this file except in compliance with the License.
51
51
local TEMP_BOUND_RECEIVED = " __temp_bound_received"
52
52
local TEMP_MIN = " __temp_min"
53
53
local TEMP_MAX = " __temp_max"
54
+ local FLOW_BOUND_RECEIVED = " __flow_bound_received"
55
+ local FLOW_MIN = " __flow_min"
56
+ local FLOW_MAX = " __flow_max"
54
57
55
58
local battery_support = {
56
59
NO_BATTERY = " NO_BATTERY" ,
@@ -148,6 +151,10 @@ local function match_profile(driver, device, battery_supported)
148
151
profile_name = profile_name .. " -leak"
149
152
end
150
153
154
+ if device :supports_capability (capabilities .flowMeasurement ) then
155
+ profile_name = profile_name .. " -flow"
156
+ end
157
+
151
158
if battery_supported == battery_support .BATTERY_PERCENTAGE then
152
159
profile_name = profile_name .. " -battery"
153
160
elseif battery_supported == battery_support .BATTERY_LEVEL then
@@ -356,6 +363,37 @@ local function pressure_attr_handler(driver, device, ib, response)
356
363
end
357
364
end
358
365
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
+
359
397
local matter_driver_template = {
360
398
lifecycle_handlers = {
361
399
init = device_init ,
@@ -395,6 +433,11 @@ local matter_driver_template = {
395
433
},
396
434
[clusters .Thermostat .ID ] = {
397
435
[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 )
398
441
}
399
442
}
400
443
},
@@ -525,6 +568,11 @@ local matter_driver_template = {
525
568
[capabilities .rainSensor .ID ] = {
526
569
clusters .BooleanState .attributes .StateValue ,
527
570
},
571
+ [capabilities .flowMeasurement .ID ] = {
572
+ clusters .FlowMeasurement .attributes .MeasuredValue ,
573
+ clusters .FlowMeasurement .attributes .MinMeasuredValue ,
574
+ clusters .FlowMeasurement .attributes .MaxMeasuredValue
575
+ },
528
576
},
529
577
capability_handlers = {
530
578
},
@@ -540,7 +588,8 @@ local matter_driver_template = {
540
588
capabilities .waterSensor ,
541
589
capabilities .temperatureAlarm ,
542
590
capabilities .rainSensor ,
543
- capabilities .hardwareFault
591
+ capabilities .hardwareFault ,
592
+ capabilities .flowMeasurement
544
593
},
545
594
sub_drivers = {
546
595
require (" air-quality-sensor" ),
0 commit comments