Skip to content

Commit

Permalink
Adding Matter Flow sensor support (#19852)
Browse files Browse the repository at this point in the history
* Create Matter_Plugin_3_Sensor_Flow.be

* Create Matter_Plugin_Bridge_Sensor_Flow.be

* Create Matter_Plugin_9_Virt_Sensor_Flow.be

* Update Matter_Plugin_3_Sensor_Flow.be

0x0404 Flow Measurement cluster

* Rename Matter_Plugin_Bridge_Sensor_Flow.be to Matter_Plugin_4_Bridge_Sensor_Flow.be

* Update Matter_UI.be

* Update be_matter_module.c

* Update be_matter_module.c

* Fixes

* Update be_matter_module.c

* Update Matter_Plugin_9_Virt_Sensor_Flow.be

* Update Matter_Plugin_3_Sensor_Flow.be

* Update be_matter_module.c

* Update solidified_Matter_Plugin_9_Virt_Sensor_Flow.h
  • Loading branch information
lboue authored Nov 3, 2023
1 parent 2d05bd2 commit 8c40d77
Show file tree
Hide file tree
Showing 9 changed files with 853 additions and 5 deletions.
6 changes: 6 additions & 0 deletions lib/libesp32/berry_matter/src/be_matter_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,9 @@ extern const bclass be_class_Matter_TLV; // need to declare it upfront because
#include "solidify/solidified_Matter_Plugin_3_ShutterTilt.h"
#include "solidify/solidified_Matter_Plugin_2_Sensor.h"
#include "solidify/solidified_Matter_Plugin_3_Sensor_Pressure.h"
#include "solidify/solidified_Matter_Plugin_3_Sensor_Flow.h"
#include "solidify/solidified_Matter_Plugin_9_Virt_Sensor_Pressure.h"
#include "solidify/solidified_Matter_Plugin_9_Virt_Sensor_Flow.h"
#include "solidify/solidified_Matter_Plugin_3_Sensor_Temp.h"
#include "solidify/solidified_Matter_Plugin_9_Virt_Sensor_Temp.h"
#include "solidify/solidified_Matter_Plugin_3_Sensor_Illuminance.h"
Expand All @@ -251,6 +253,7 @@ extern const bclass be_class_Matter_TLV; // need to declare it upfront because
#include "solidify/solidified_Matter_Plugin_4_Bridge_Sensor_Humidity.h"
#include "solidify/solidified_Matter_Plugin_4_Bridge_Sensor_Occupancy.h"
#include "solidify/solidified_Matter_Plugin_4_Bridge_Sensor_Contact.h"
#include "solidify/solidified_Matter_Plugin_4_Bridge_Sensor_Flow.h"

/*********************************************************************************************\
* Get a bytes() object of the certificate DAC/PAI_Cert
Expand Down Expand Up @@ -448,7 +451,9 @@ module matter (scope: global, strings: weak) {
Plugin_ShutterTilt, class(be_class_Matter_Plugin_ShutterTilt) // Shutter + Tilt
Plugin_Sensor, class(be_class_Matter_Plugin_Sensor) // Generic Sensor
Plugin_Sensor_Pressure, class(be_class_Matter_Plugin_Sensor_Pressure) // Pressure Sensor
Plugin_Sensor_Flow, class(be_class_Matter_Plugin_Sensor_Flow) // Flow Sensor
Plugin_Sensor_Virt_Pressure, class(be_class_Matter_Plugin_Virt_Sensor_Pressure) // Pressure Virtual Sensor
Plugin_Sensor_Virt_Flow, class(be_class_Matter_Plugin_Virt_Sensor_Flow) // Flow Virtual Sensor
Plugin_Sensor_Temp, class(be_class_Matter_Plugin_Sensor_Temp) // Temperature Sensor
Plugin_Virt_Sensor_Temp, class(be_class_Matter_Plugin_Virt_Sensor_Temp) // Temperature Sensor
Plugin_Sensor_Illuminance, class(be_class_Matter_Plugin_Sensor_Illuminance) // Illuminance Sensor
Expand All @@ -468,6 +473,7 @@ module matter (scope: global, strings: weak) {
Plugin_Bridge_Light3, class(be_class_Matter_Plugin_Bridge_Light3) // HTTP RGB Light
Plugin_Bridge_Sensor, class(be_class_Matter_Plugin_Bridge_Sensor) // HTTP generic sensor
Plugin_Bridge_Sensor_Pressure, class(be_class_Matter_Plugin_Bridge_Sensor_Pressure) // HTTP Pressure sensor
Plugin_Bridge_Sensor_Flow, class(be_class_Matter_Plugin_Bridge_Sensor_Flow) // HTTP Flow sensor
Plugin_Bridge_Sensor_Temp, class(be_class_Matter_Plugin_Bridge_Sensor_Temp) // HTTP Temperature sensor
Plugin_Bridge_Sensor_Illuminance, class(be_class_Matter_Plugin_Bridge_Sensor_Illuminance) // HTTP Illuminance sensor
Plugin_Bridge_Sensor_Humidity, class(be_class_Matter_Plugin_Bridge_Sensor_Humidity) // HTTP Humidity sensor
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#
# Matter_Plugin_Sensor_Flow.be - implements the behavior for a Flow Sensor
#
# Copyright (C) 2023 Stephan Hadinger & Theo Arends
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

import matter

# Matter plug-in for core behavior

#@ solidify:Matter_Plugin_Sensor_Flow,weak

class Matter_Plugin_Sensor_Flow : Matter_Plugin_Sensor
static var TYPE = "flow" # name of the plug-in in json
static var DISPLAY_NAME = "Flow" # display name of the plug-in
static var JSON_NAME = "Flow" # Name of the sensor attribute in JSON payloads
static var UPDATE_COMMANDS = matter.UC_LIST(_class, "Flow")
static var CLUSTERS = matter.consolidate_clusters(_class, {
0x0404: [0,1,2,0xFFFC,0xFFFD], # Flow Measurement
})
static var TYPES = { 0x0306: 2 } # Flow Sensor, rev 2

#############################################################
# Pre-process value
#
# This must be overriden.
# This allows to convert the raw sensor value to the target one, typically int
def pre_value(val)
return val != nil ? int(val) : nil
end

#############################################################
# Called when the value changed compared to shadow value
#
# This must be overriden.
# This is where you call `self.attribute_updated(<cluster>, <attribute>)`
def value_changed()
self.attribute_updated(0x0404, 0x0000)
end

#############################################################
# read an attribute
#
def read_attribute(session, ctx, tlv_solo)
var TLV = matter.TLV
var cluster = ctx.cluster
var attribute = ctx.attribute

# ====================================================================================================
if cluster == 0x0404 # ========== Flow Measurement 2.4 p.98 ==========
if attribute == 0x0000 # ---------- MeasuredValue / i16 ----------
if self.shadow_value != nil
return tlv_solo.set(TLV.I2, int(self.shadow_value)) # MeasuredValue represents the flow in m3/h
else
return tlv_solo.set(TLV.NULL, nil)
end
elif attribute == 0x0001 # ---------- MinMeasuredValue / i16 ----------
return tlv_solo.set(TLV.I2, 0) # 500 m3/h
elif attribute == 0x0002 # ---------- MaxMeasuredValue / i16 ----------
return tlv_solo.set(TLV.I2, 65534) # 65534 m3/h
elif attribute == 0xFFFC # ---------- FeatureMap / map32 ----------
return tlv_solo.set(TLV.U4, 0) # 0 = no Extended Range
elif attribute == 0xFFFD # ---------- ClusterRevision / u2 ----------
return tlv_solo.set(TLV.U4, 3) # 3 = New data model format and notation
end

else
return super(self).read_attribute(session, ctx, tlv_solo)
end
end

end
matter.Plugin_Sensor_Flow = Matter_Plugin_Sensor_Flow
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#
# Matter_Plugin_Bridge_Sensor_Flow.be - implements base class for a Flow Sensor via HTTP to Tasmota
#
# Copyright (C) 2023 Stephan Hadinger & Theo Arends
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

import matter

# Matter plug-in for core behavior

#@ solidify:Matter_Plugin_Bridge_Sensor_Flow,weak

class Matter_Plugin_Bridge_Sensor_Flow : Matter_Plugin_Bridge_Sensor
static var TYPE = "http_flow" # name of the plug-in in json
static var DISPLAY_NAME = "Flow" # display name of the plug-in

static var CLUSTERS = matter.consolidate_clusters(_class, {
0x0404: [0,1,2,0xFFFC,0xFFFD], # Flow Measurement
})
static var TYPES = { 0x0306: 2 } # Flow Sensor, rev 2

#############################################################
# Called when the value changed compared to shadow value
#
# This must be overriden.
# This is where you call `self.attribute_updated(<cluster>, <attribute>)`
def value_changed()
self.attribute_updated(0x0404, 0x0000)
end

#############################################################
# Pre-process value
#
# This must be overriden.
# This allows to convert the raw sensor value to the target one, typically int
def pre_value(val)
return val != nil ? int(val) : nil
end

#############################################################
# read an attribute
#
def read_attribute(session, ctx, tlv_solo)
var TLV = matter.TLV
var cluster = ctx.cluster
var attribute = ctx.attribute

# ====================================================================================================
if cluster == 0x0404 # ========== Flow Measurement 2.1.2 p.127 ==========
if attribute == 0x0000 # ---------- MeasuredValue / i16 ----------
if self.shadow_value != nil
return tlv_solo.set(TLV.I2, int(self.shadow_value)) # MeasuredValue represents the flow in m3/h
else
return tlv_solo.set(TLV.NULL, nil)
end
elif attribute == 0x0001 # ---------- MinMeasuredValue / i16 ----------
return tlv_solo.set(TLV.I2, 0) # 500 m3/h
elif attribute == 0x0002 # ---------- MaxMeasuredValue / i16 ----------
return tlv_solo.set(TLV.I2, 65534) # 65534 m3/h
elif attribute == 0xFFFC # ---------- FeatureMap / map32 ----------
return tlv_solo.set(TLV.U4, 0) # 0 = no Extended Range
elif attribute == 0xFFFD # ---------- ClusterRevision / u2 ----------
return tlv_solo.set(TLV.U4, 3) # 3 = New data model format and notation
end

else
return super(self).read_attribute(session, ctx, tlv_solo)
end
end

#############################################################
# web_values
#
# Show values of the remote device as HTML
def web_values()
import webserver
self.web_values_prefix() # display '| ' and name if present
webserver.content_send(format("&#x26C5; %i m3/h",
int(self.shadow_value)))
end

end
matter.Plugin_Bridge_Sensor_Flow = Matter_Plugin_Bridge_Sensor_Flow
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#
# Matter_Plugin_9_Virt_Sensor_Flow.be - implements the behavior for a Flow Sensor
#
# Copyright (C) 2023 Stephan Hadinger & Theo Arends
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

import matter

# Matter plug-in for core behavior

#@ solidify:Matter_Plugin_Virt_Sensor_Flow,weak

class Matter_Plugin_Virt_Sensor_Flow : Matter_Plugin_Sensor_Flow
static var TYPE = "v_flow" # name of the plug-in in json
static var DISPLAY_NAME = "v.Flow" # display name of the plug-in
static var ARG = "" # no arg for virtual device
static var ARG_HINT = "_Not used_" # Hint for entering the Argument (inside 'placeholder')
static var VIRTUAL = true # virtual device

end
matter.Plugin_Virt_Sensor_Flow = Matter_Plugin_Virt_Sensor_Flow
6 changes: 3 additions & 3 deletions lib/libesp32/berry_matter/src/embedded/Matter_UI.be
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ import matter
#################################################################################
class Matter_UI
static var _CLASSES_TYPES = "|relay|light0|light1|light2|light3|shutter|shutter+tilt"
"|temperature|pressure|illuminance|humidity|occupancy|onoff|contact"
"|temperature|pressure|illuminance|humidity|occupancy|onoff|contact|flow"
"|-virtual|v_relay|v_light0|v_light1|v_light2|v_light3"
"|v_temp|v_pressure|v_illuminance|v_humidity|v_occupancy|v_contact"
"|v_temp|v_pressure|v_illuminance|v_humidity|v_occupancy|v_contact|v_flow"
# static var _CLASSES_HTTP = "-http"
static var _CLASSES_TYPES2= "|http_relay|http_light0|http_light1|http_light2|http_light3"
"|http_temperature|http_pressure|http_illuminance|http_humidity"
"|http_occupancy|http_contact"
"|http_occupancy|http_contact|http_flow"
var device

# ####################################################################################################
Expand Down
Loading

0 comments on commit 8c40d77

Please sign in to comment.