Skip to content

Commit

Permalink
Support for Matter 1.3 Water leak detectors (#21456)
Browse files Browse the repository at this point in the history
* Create Matter_Plugin_2_Sensor_Waterleak.be

* Update Matter_Plugin_2_Sensor_Waterleak.be

* Create Matter_Plugin_3_Bridge_Sensor_Waterleak.be

* Create Matter_Plugin_9_Virt_Sensor_Waterleak.be

* Update Matter_UI.be

* Update be_matter_module.c

* Update Matter_UI.be

* Solidify Matter

* Solidified new Matter files

* Solidified new Matter files
  • Loading branch information
lboue committed May 21, 2024
1 parent 2eccc96 commit dc107a1
Show file tree
Hide file tree
Showing 10 changed files with 1,223 additions and 42 deletions.
3 changes: 3 additions & 0 deletions lib/libesp32/berry_matter/src/be_matter_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,10 @@ extern const bclass be_class_Matter_TLV; // need to declare it upfront because
#include "solidify/solidified_Matter_Plugin_2_Sensor_Occupancy.h"
#include "solidify/solidified_Matter_Plugin_2_Sensor_OnOff.h"
#include "solidify/solidified_Matter_Plugin_2_Sensor_Contact.h"
#include "solidify/solidified_Matter_Plugin_2_Sensor_Waterleak.h"
#include "solidify/solidified_Matter_Plugin_9_Virt_Sensor_Contact.h"
#include "solidify/solidified_Matter_Plugin_9_Virt_Sensor_Occupancy.h"
#include "solidify/solidified_Matter_Plugin_9_Virt_Sensor_Waterleak.h"
#include "solidify/solidified_Matter_Plugin_2_Bridge_HTTP.h"
#include "solidify/solidified_Matter_Plugin_4_Bridge_OnOff.h"
#include "solidify/solidified_Matter_Plugin_3_Bridge_Light0.h"
Expand All @@ -269,6 +271,7 @@ extern const bclass be_class_Matter_TLV; // need to declare it upfront because
#include "solidify/solidified_Matter_Plugin_3_Bridge_Sensor_Occupancy.h"
#include "solidify/solidified_Matter_Plugin_3_Bridge_Sensor_Contact.h"
#include "solidify/solidified_Matter_Plugin_4_Bridge_Sensor_Flow.h"
#include "solidify/solidified_Matter_Plugin_3_Bridge_Sensor_Waterleak.h"
#include "solidify/solidified_Matter_Plugin_z_All.h"
#include "solidify/solidified_Matter_zz_Device.h"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#
# Matter_Plugin_2_Sensor_Waterleak.be - implements the behavior for a Water leak Sensor
#
# Copyright (C) 2024 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_Waterleak,weak

class Matter_Plugin_Sensor_Waterleak : Matter_Plugin_Device
static var TYPE = "waterleak" # name of the plug-in in json
static var DISPLAY_NAME = "Waterleak" # display name of the plug-in
static var ARG = "switch" # additional argument name (or empty if none)
static var ARG_HINT = "Switch<x> number"
static var ARG_TYPE = / x -> int(x) # function to convert argument to the right type
static var UPDATE_TIME = 750 # update every 750ms
static var UPDATE_COMMANDS = matter.UC_LIST(_class, "Waterleak")
static var CLUSTERS = matter.consolidate_clusters(_class, {
0x0045: [0], # Boolean State p.70 - no writable
})
# MATTER_WATER_LEAK_DETECTOR_DEVICE_TYPE_ID 0x0043
static var TYPES = { 0x0043: 1 } # Waterleak Sensor, rev 1

var tasmota_switch_index # Switch number in Tasmota (one based)
var shadow_leak

#############################################################
# Constructor
def init(device, endpoint, config)
super(self).init(device, endpoint, config)
self.shadow_leak = false
end

#############################################################
# parse_configuration
#
# Parse configuration map
def parse_configuration(config)
self.tasmota_switch_index = int(config.find(self.ARG #-'switch'-#, 1))
if self.tasmota_switch_index <= 0 self.tasmota_switch_index = 1 end
end

#############################################################
# Update shadow
#
def update_shadow()
super(self).update_shadow()
if !self.VIRTUAL
var switch_str = "Switch" + str(self.tasmota_switch_index)

var j = tasmota.cmd("Status 8", true)
if j != nil j = j.find("StatusSNS") end
if j != nil && j.contains(switch_str)
var state = (j.find(switch_str) == "ON")

if (self.shadow_leak != state)
self.attribute_updated(0x0045, 0x0000)
end
self.shadow_leak = state
end
end
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 == 0x0045 # ========== Boolean State ==========
if attribute == 0x0000 # ---------- StateValue / bool ----------
if self.shadow_leak != nil
return tlv_solo.set(TLV.BOOL, self.shadow_leak)
else
return tlv_solo.set(TLV.NULL, nil)
end
end

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

#############################################################
# update_virtual
#
# Update internal state for virtual devices
def update_virtual(payload_json)
var val_onoff = payload_json.find("Waterleak")
if val_onoff != nil
val_onoff = bool(val_onoff)
if self.shadow_leak != val_onoff
self.attribute_updated(0x0045, 0x0000)
self.shadow_leak = val_onoff
end
end
super(self).update_virtual(payload_json)
end

end
matter.Plugin_Sensor_Waterleak = Matter_Plugin_Sensor_Waterleak
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#
# Matter_Plugin_Bridge_Sensor_Waterleak.be - implements Waterleak Sensor via HTTP to Tasmota
#
# Copyright (C) 2024 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_Waterleak,weak

class Matter_Plugin_Bridge_Sensor_Waterleak : Matter_Plugin_Bridge_HTTP
static var TYPE = "http_waterleak" # name of the plug-in in json
static var DISPLAY_NAME = "Waterleak" # display name of the plug-in
static var ARG = "switch" # additional argument name (or empty if none)
static var ARG_HINT = "Switch<x> number"
static var ARG_TYPE = / x -> int(x) # function to convert argument to the right type
static var UPDATE_TIME = 5000 # update every 5s
static var UPDATE_CMD = "Status 8" # command to send for updates

static var CLUSTERS = matter.consolidate_clusters(_class, {
0x0045: [0], # Boolean State p.70 - no writable
})
static var TYPES = { 0x0043: 1 } # Waterleak Sensor, rev 1

var tasmota_switch_index # Switch number in Tasmota (one based)
var shadow_Waterleak

#############################################################
# Constructor
def init(device, endpoint, arguments)
super(self).init(device, endpoint, arguments)
self.tasmota_switch_index = int(arguments.find(self.ARG #-'switch'-#, 1))
if self.tasmota_switch_index <= 0 self.tasmota_switch_index = 1 end
end

#############################################################
# Stub for updating shadow values (local copies of what we published to the Matter gateway)
#
# This call is synnchronous and blocking.
def parse_update(data, index)
if index == 8 # Status 8
var state = false

state = (data.find("Switch" + str(self.tasmota_switch_index)) == "ON")

if self.shadow_Waterleak != nil && self.shadow_Waterleak != bool(state)
self.attribute_updated(0x0045, 0x0000)
end
self.shadow_Waterleak = state
end
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 == 0x0045 # ========== Boolean State ==========
if attribute == 0x0000 # ---------- StateValue / bool ----------
if self.shadow_Waterleak != nil
return tlv_solo.set(TLV.BOOL, self.shadow_Waterleak)
else
return tlv_solo.set(TLV.NULL, nil)
end
end

end
return super(self).read_attribute(session, ctx, tlv_solo)
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("Waterleak%i %s", self.tasmota_switch_index, self.web_value_onoff(self.shadow_Waterleak)))
end

# Show prefix before web value
def web_values_prefix()
import webserver
var name = self.get_name()
if !name
name = "Switch" + str(self.tasmota_switch_index)
end
webserver.content_send(format(self.PREFIX, name ? webserver.html_escape(name) : ""))
end
end
matter.Plugin_Bridge_Sensor_Waterleak = Matter_Plugin_Bridge_Sensor_Waterleak
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

#
# Matter_Plugin_9_Virt_Sensor_Waterleak.be - implements the behavior for a Virtual Waterleak Sensor
#
# Copyright (C) 2024 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_Waterleak ,weak

class Matter_Plugin_Virt_Sensor_Waterleak : Matter_Plugin_Virt_Sensor_Waterleak
static var TYPE = "v_waterleak" # name of the plug-in in json
static var DISPLAY_NAME = "v.Waterleak" # 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_Waterleak = Matter_Plugin_Virt_Sensor_Waterleak
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,12 +33,12 @@ import matter
#################################################################################
class Matter_UI
static var _CLASSES_TYPES = "|relay|light0|light1|light2|light3|shutter|shutter+tilt"
"|temperature|pressure|illuminance|humidity|occupancy|onoff|contact|flow"
"|temperature|pressure|illuminance|humidity|occupancy|onoff|contact|flow|waterleak"
"|-virtual|v_relay|v_light0|v_light1|v_light2|v_light3"
"|v_temp|v_pressure|v_illuminance|v_humidity|v_occupancy|v_contact|v_flow"
"|v_temp|v_pressure|v_illuminance|v_humidity|v_occupancy|v_contact|v_flow|v_waterleak"
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_flow"
"|http_occupancy|http_contact|http_flow|http_waterleak"
var device

# ####################################################################################################
Expand Down

0 comments on commit dc107a1

Please sign in to comment.