Skip to content

Commit

Permalink
Matter Zigbee support for sensors: Temperature, Humidity, Pressure (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
s-hadinger authored and hawa-lc4 committed Sep 16, 2024
1 parent d4f06d3 commit 1c9ed26
Show file tree
Hide file tree
Showing 21 changed files with 7,465 additions and 6,176 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
- Command ``SetOption69 1`` to enable Serial Bridge inverted Receive (#22000)
- Zigbee Koenkk firmware 20240710 for Sonoff Zigbee ZBPro
- Berry Zigbee improvements to prepare Matter
- Matter Zigbee support for sensors: Temperature, Humidity, Pressure

### Breaking Changed
- Berry make `energy` modules changes from #21887 backwards compatible
Expand Down
2 changes: 2 additions & 0 deletions lib/libesp32/berry/default/be_modtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ be_extern_native_module(TFL);
be_extern_native_module(mdns);
#ifdef USE_ZIGBEE
be_extern_native_module(zigbee);
be_extern_native_module(matter_zigbee);
#endif // USE_ZIGBEE
#ifdef USE_BERRY_CAM
be_extern_native_module(cam);
Expand Down Expand Up @@ -171,6 +172,7 @@ BERRY_LOCAL const bntvmodule_t* const be_module_table[] = {
#endif // USE_WEBSERVER
#ifdef USE_ZIGBEE
&be_native_module(zigbee),
&be_native_module(matter_zigbee),
#endif // USE_ZIGBEE
&be_native_module(flash),
&be_native_module(partition_core),
Expand Down
4 changes: 4 additions & 0 deletions lib/libesp32/berry_matter/src/be_matter_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ extern const bclass be_class_Matter_TLV; // need to declare it upfront because
#include "solidify/solidified_Matter_Plugin_0.h"
#include "solidify/solidified_Matter_z_Commissioning.h"
#include "solidify/solidified_Matter_z_Autoconf.h"
#include "solidify/solidified_Matter_z_Zigbee.h"
#include "solidify/solidified_Matter_Base38.h"
#include "solidify/solidified_Matter_UI.h"
#include "solidify/solidified_Matter_Profiler.h"
Expand Down Expand Up @@ -295,6 +296,9 @@ extern const bclass be_class_Matter_TLV; // need to declare it upfront because
#include "solidify/solidified_Matter_Plugin_8_Bridge_Sensor_Air_Quality.h"
#include "solidify/solidified_Matter_Plugin_8_Bridge_Sensor_Rain.h"
#include "solidify/solidified_Matter_Plugin_8_Bridge_Sensor_Waterleak.h"
#include "solidify/solidified_Matter_Plugin_9_Zigbee_Temperature.h"
#include "solidify/solidified_Matter_Plugin_9_Zigbee_Pressure.h"
#include "solidify/solidified_Matter_Plugin_9_Zigbee_Humidity.h"
#include "solidify/solidified_Matter_Plugin_z_All.h"
#include "solidify/solidified_Matter_zz_Device.h"

Expand Down
1 change: 1 addition & 0 deletions lib/libesp32/berry_matter/src/embedded/Matter_Plugin_0.be
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Matter_Plugin
static var UPDATE_TIME = 5000 # default is every 5 seconds
static var VIRTUAL = false # set to true only for virtual devices
static var BRIDGE = false # set to true only for bridged devices (ESP8266 or OpenBK)
static var ZIGBEE = false # set to true only when mapped to a zigbee device
var update_next # next timestamp for update
# Configuration of the plugin: clusters and type
static var CLUSTERS = matter.consolidate_clusters(_class, {
Expand Down
17 changes: 17 additions & 0 deletions lib/libesp32/berry_matter/src/embedded/Matter_Plugin_1_Device.be
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ class Matter_Plugin_Device : Matter_Plugin
#############################################################
# Constructor
def init(device, endpoint, arguments)
# Zigbee code, activated only when `ZIGBEE` is true
# attribute `zigbee_mapper` needs to be defined for classes with `ZIGBEE` true
if self.ZIGBEE
self.zigbee_mapper = device.create_zb_mapper(self) # needs to exist before `parse_configuration()` is called
end

super(self).init(device, endpoint, arguments)

if self.BRIDGE
Expand All @@ -64,6 +70,17 @@ class Matter_Plugin_Device : Matter_Plugin
end
end

#############################################################
# parse_configuration
#
# Parse configuration map, handling case of Zigbee configuration
def parse_configuration(config)
# super(self).parse_configuration(config) # not necessary because the superclass does nothing
if self.ZIGBEE && self.zigbee_mapper
self.zigbee_mapper.parse_configuration(config)
end
end

#############################################################
# read an attribute
#
Expand Down
27 changes: 27 additions & 0 deletions lib/libesp32/berry_matter/src/embedded/Matter_Plugin_2_Sensor.be
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Matter_Plugin_Sensor : Matter_Plugin_Device
#
# Parse configuration map
def parse_configuration(config)
super(self).parse_configuration(config)
self.tasmota_sensor_filter = config.find(self.ARG#-'filter'-#)
if self.tasmota_sensor_filter
self.tasmota_sensor_matcher = tasmota.Rule_Matcher.parse(self.tasmota_sensor_filter)
Expand Down Expand Up @@ -167,5 +168,31 @@ class Matter_Plugin_Sensor : Matter_Plugin_Device
#############################################################
#############################################################

#############################################################
# For Zigbee devices
#############################################################
#############################################################
# attributes_refined
#
# Filtered to only events for this endpoint
#
# Can be called only if `self.ZIGBEE` is true
def zigbee_received(frame, attr_list)
import math
log(f"MTR: zigbee_received Ox{self.zigbee_mapper.shortaddr:04X} {attr_list=} {type(attr_list)=}", 3)
var idx = 0
while (idx < size(attr_list))
var entry = attr_list[idx]
if (entry.key == self.ZIGBEE_NAME)
var val = self.pre_value(entry.val)
var update_list = { self.JSON_NAME : val } # Matter temperature is 1/100th of degrees
self.update_virtual(update_list)
log(f"MTR: [{self.endpoint:02X}] {self.JSON_NAME} updated {update_list}", 3)
return nil
end
idx += 1
end
end

end
matter.Plugin_Sensor = Matter_Plugin_Sensor
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#
# Matter_Plugin_9_Zigbee_Humidity.be - implements the behavior for a Zigbee Humidity 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_Zigbee_Humidity,weak

class Matter_Plugin_Zigbee_Humidity : Matter_Plugin_Sensor_Humidity
static var ZIGBEE = true
static var TYPE = "z_humidity" # name of the plug-in in json
static var DISPLAY_NAME = "Zig Humidity" # display name of the plug-in
static var ZIGBEE_NAME = "Humidity" # name of zigbee attribute with sensor reported
static var ARG = "zigbee_device" # zigbee device
static var ARG_TYPE = / x -> str(x) # function to convert argument to the right type
static var ARG_HINT = "Device" # Hint for entering the Argument (inside 'placeholder')
static var VIRTUAL = true # virtual device, necessary for Zigbee mapping
var zigbee_mapper # required for zigbee device

end
matter.Plugin_Zigbee_Humidity = Matter_Plugin_Zigbee_Humidity
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#
# Matter_Plugin_9_Zigbee_Pressure.be - implements the behavior for a Zigbee Pressure 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_Zigbee_Pressure,weak

class Matter_Plugin_Zigbee_Pressure : Matter_Plugin_Sensor_Pressure
static var ZIGBEE = true
static var TYPE = "z_pressure" # name of the plug-in in json
static var DISPLAY_NAME = "Zig Pressure" # display name of the plug-in
static var ZIGBEE_NAME = "Pressure" # name of zigbee attribute with sensor reported
static var ARG = "zigbee_device" # zigbee device
static var ARG_TYPE = / x -> str(x) # function to convert argument to the right type
static var ARG_HINT = "Device" # Hint for entering the Argument (inside 'placeholder')
static var VIRTUAL = true # virtual device, necessary for Zigbee mapping
var zigbee_mapper # required for zigbee device

end
matter.Plugin_Zigbee_Pressure = Matter_Plugin_Zigbee_Pressure
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#
# Matter_Plugin_9_Zigbee_Temperature.be - implements the behavior for a Zigbee Temperature 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_Zigbee_Temperature,weak

class Matter_Plugin_Zigbee_Temperature : Matter_Plugin_Sensor_Temp
static var ZIGBEE = true
static var TYPE = "z_temp" # name of the plug-in in json
static var DISPLAY_NAME = "Zig Temperature" # display name of the plug-in
static var ZIGBEE_NAME = "Temperature" # name of zigbee attribute with sensor reported
static var ARG = "zigbee_device" # zigbee device
static var ARG_TYPE = / x -> str(x) # function to convert argument to the right type
static var ARG_HINT = "Device" # Hint for entering the Argument (inside 'placeholder')
static var VIRTUAL = true # virtual device, necessary for Zigbee mapping
var zigbee_mapper # required for zigbee device

end
matter.Plugin_Zigbee_Temperature = Matter_Plugin_Zigbee_Temperature
22 changes: 17 additions & 5 deletions lib/libesp32/berry_matter/src/embedded/Matter_UI.be
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ import matter
# WebUI for the partition manager
#################################################################################
class Matter_UI
static var _CLASSES_TYPES = "|relay|light0|light1|light2|light3|shutter|shutter+tilt"
static var _CLASSES_TYPES_STD =
"|relay|light0|light1|light2|light3|shutter|shutter+tilt"
"|gensw_btn"
"|temperature|pressure|illuminance|humidity|occupancy|onoff|contact|flow|rain|waterleak"
"|airquality"
"|-virtual|v_relay|v_light0|v_light1|v_light2|v_light3"
static var _CLASSES_TYPES_VIRTUAL =
"-virtual|v_relay|v_light0|v_light1|v_light2|v_light3"
"|v_fan"
"|v_temp|v_pressure|v_illuminance|v_humidity|v_occupancy|v_contact|v_flow|v_rain|v_waterleak"
"|v_airquality"
Expand Down Expand Up @@ -438,7 +440,11 @@ class Matter_UI


# Add new endpoint section
self.show_plugins_hints_js(self._CLASSES_TYPES)
if self.device.zigbee
self.show_plugins_hints_js(self._CLASSES_TYPES_STD, self.device.zigbee._CLASSES_TYPES, self._CLASSES_TYPES_VIRTUAL)
else
self.show_plugins_hints_js(self._CLASSES_TYPES_STD, self._CLASSES_TYPES_VIRTUAL)
end

webserver.content_send("<p></p><fieldset><legend><b>&nbsp;Add to Configuration&nbsp;</b></legend><p></p>"
"<p><b>Add local sensor or device</b></p>"
Expand All @@ -453,9 +459,13 @@ class Matter_UI
"<tr>"
"<td style='font-size:smaller;'><input type='text' name='nam' size='1' value='' placeholder='(optional)' title=''></td>"
"<td style='font-size:smaller;'><select id='pi' name='pi' onchange='otm(\"arg\",this.value)'>")
self.plugin_option('', self._CLASSES_TYPES)
if self.device.zigbee
self.plugin_option('', self._CLASSES_TYPES_STD, self.device.zigbee._CLASSES_TYPES, self._CLASSES_TYPES_VIRTUAL)
else
self.plugin_option('', self._CLASSES_TYPES_STD, self._CLASSES_TYPES_VIRTUAL)
end
webserver.content_send("</select></td>"
"<td style='font-size:smaller;'><input type='text' id='arg' name='arg' size='1' value=''></td>"
"<td style='font-size:smaller;'><input type='text' id='arg' name='arg' size='1' value=''></td>"
"</tr></table>"

"<div style='display: block;'></div>"
Expand Down Expand Up @@ -509,6 +519,8 @@ class Matter_UI
webserver.content_send("<option value=''></option>")
elif typ == '-virtual'
webserver.content_send("<option value='' disabled>--- Virtual Devices ---</option>")
elif typ == '-zigbee'
webserver.content_send("<option value='' disabled>--- Zigbee Devices ---</option>")
else
var nam = self.device.get_plugin_class_displayname(typ)
webserver.content_send(format("<option value='%s'%s>%s</option>", typ, (typ == cur) ? " selected" : "", nam))
Expand Down
Loading

0 comments on commit 1c9ed26

Please sign in to comment.