-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Matter support for Occupancy via Switch (experimental) (#18742)
- Loading branch information
1 parent
3f094c9
commit 83e47fa
Showing
22 changed files
with
1,614 additions
and
717 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
lib/libesp32/berry_matter/src/embedded/Matter_Plugin_Bridge_Sensor_Occupancy.be
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# | ||
# Matter_Plugin_Bridge_Sensor_Occupancy.be - implements base class for a Occupancy 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/>. | ||
# | ||
|
||
# Matter plug-in for core behavior | ||
|
||
# dummy declaration for solidification | ||
class Matter_Plugin_Bridge_HTTP end | ||
|
||
#@ solidify:Matter_Plugin_Bridge_Sensor_Occupancy,weak | ||
|
||
class Matter_Plugin_Bridge_Sensor_Occupancy : Matter_Plugin_Bridge_HTTP | ||
static var TYPE = "http_occupancy" # name of the plug-in in json | ||
static var NAME = "🔗 Occupancy" # display name of the plug-in | ||
static var ARG = "switch" # additional argument name (or empty if none) | ||
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 = { | ||
0x0406: [0,1,2,0xFFFC,0xFFFD], # Occupancy Sensing p.105 - no writable | ||
} | ||
static var TYPES = { 0x0107: 2, 0x0013: 1 } # Occupancy Sensor, rev 2 | ||
|
||
var tasmota_switch_index # Switch number in Tasmota (one based) | ||
var shadow_occupancy | ||
|
||
############################################################# | ||
# Constructor | ||
def init(device, endpoint, arguments) | ||
super(self).init(device, endpoint, arguments) | ||
self.tasmota_switch_index = int(arguments.find(self.ARG #-'relay'-#, 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_occupancy != nil && self.shadow_occupancy != bool(state) | ||
self.attribute_updated(0x0406, 0x0000) | ||
end | ||
self.shadow_occupancy = state | ||
end | ||
end | ||
|
||
############################################################# | ||
# read an attribute | ||
# | ||
def read_attribute(session, ctx) | ||
import string | ||
var TLV = matter.TLV | ||
var cluster = ctx.cluster | ||
var attribute = ctx.attribute | ||
|
||
# ==================================================================================================== | ||
if cluster == 0x0406 # ========== Occupancy Sensing ========== | ||
if attribute == 0x0000 # ---------- Occupancy / U8 ---------- | ||
if self.shadow_occupancy != nil | ||
return TLV.create_TLV(TLV.U1, self.shadow_occupancy) | ||
else | ||
return TLV.create_TLV(TLV.NULL, nil) | ||
end | ||
elif attribute == 0x0001 # ---------- OccupancySensorType / enum8 ---------- | ||
return TLV.create_TLV(TLV.U1, 3) # physical contact | ||
elif attribute == 0x0002 # ---------- OccupancySensorTypeBitmap / u8 ---------- | ||
return TLV.create_TLV(TLV.U1, 0) # unknown | ||
elif attribute == 0xFFFC # ---------- FeatureMap / map32 ---------- | ||
return TLV.create_TLV(TLV.U4, 0) | ||
elif attribute == 0xFFFD # ---------- ClusterRevision / u2 ---------- | ||
return TLV.create_TLV(TLV.U4, 3) # 4 = New data model format and notation | ||
end | ||
|
||
else | ||
return super(self).read_attribute(session, ctx) | ||
end | ||
end | ||
|
||
############################################################# | ||
# web_values | ||
# | ||
# Show values of the remote device as HTML | ||
def web_values() | ||
import webserver | ||
import string | ||
webserver.content_send(string.format("| Occupancy%i %s", self.tasmota_switch_index, self.web_value_onoff(self.shadow_occupancy))) | ||
end | ||
|
||
end | ||
matter.Plugin_Bridge_Sensor_Occupancy = Matter_Plugin_Bridge_Sensor_Occupancy |
104 changes: 104 additions & 0 deletions
104
lib/libesp32/berry_matter/src/embedded/Matter_Plugin_Sensor_Occupancy.be
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
# | ||
# Matter_Plugin_Sensor_Occupancy.be - implements the behavior for a Occupany Switch | ||
# | ||
# 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/>. | ||
# | ||
|
||
# Matter plug-in for core behavior | ||
|
||
# dummy declaration for solidification | ||
class Matter_Plugin_Device end | ||
|
||
#@ solidify:Matter_Plugin_Sensor_Occupancy,weak | ||
|
||
class Matter_Plugin_Sensor_Occupancy : Matter_Plugin_Device | ||
static var TYPE = "occupancy" # name of the plug-in in json | ||
static var NAME = "Occupancy" # display name of the plug-in | ||
static var ARG = "switch" # additional argument name (or empty if none) | ||
static var ARG_TYPE = / x -> int(x) # function to convert argument to the right type | ||
static var UPDATE_TIME = 5000 # update every 250ms | ||
static var CLUSTERS = { | ||
0x0406: [0,1,2,0xFFFC,0xFFFD], # Occupancy Sensing p.105 - no writable | ||
} | ||
static var TYPES = { 0x0107: 2 } # Occupancy Sensor, rev 2 | ||
|
||
var tasmota_switch_index # Switch number in Tasmota (one based) | ||
var shadow_occupancy | ||
|
||
############################################################# | ||
# Constructor | ||
def init(device, endpoint, arguments) | ||
super(self).init(device, endpoint, arguments) | ||
self.tasmota_switch_index = int(arguments.find(self.ARG #-'relay'-#, 1)) | ||
if self.tasmota_switch_index <= 0 self.tasmota_switch_index = 1 end | ||
end | ||
|
||
############################################################# | ||
# Update shadow | ||
# | ||
def update_shadow() | ||
super(self).update_shadow() | ||
|
||
import json | ||
var ret = tasmota.cmd("Status 8", true) | ||
if ret != nil | ||
var j = json.load(ret) | ||
if j != nil | ||
var state = false | ||
state = (j.find("Switch" + str(self.tasmota_switch_index)) == "ON") | ||
|
||
if self.shadow_occupancy != nil && self.shadow_occupancy != bool(state) | ||
self.attribute_updated(0x0406, 0x0000) | ||
end | ||
self.shadow_occupancy = state | ||
end | ||
end | ||
end | ||
|
||
############################################################# | ||
# read an attribute | ||
# | ||
def read_attribute(session, ctx) | ||
import string | ||
var TLV = matter.TLV | ||
var cluster = ctx.cluster | ||
var attribute = ctx.attribute | ||
|
||
# ==================================================================================================== | ||
if cluster == 0x0406 # ========== Occupancy Sensing ========== | ||
if attribute == 0x0000 # ---------- Occupancy / U8 ---------- | ||
if self.shadow_occupancy != nil | ||
return TLV.create_TLV(TLV.U1, self.shadow_occupancy) | ||
else | ||
return TLV.create_TLV(TLV.NULL, nil) | ||
end | ||
elif attribute == 0x0001 # ---------- OccupancySensorType / enum8 ---------- | ||
return TLV.create_TLV(TLV.U1, 3) # physical contact | ||
elif attribute == 0x0002 # ---------- OccupancySensorTypeBitmap / u8 ---------- | ||
return TLV.create_TLV(TLV.U1, 0) # unknown | ||
elif attribute == 0xFFFC # ---------- FeatureMap / map32 ---------- | ||
return TLV.create_TLV(TLV.U4, 0) | ||
elif attribute == 0xFFFD # ---------- ClusterRevision / u2 ---------- | ||
return TLV.create_TLV(TLV.U4, 3) # 4 = New data model format and notation | ||
end | ||
|
||
else | ||
return super(self).read_attribute(session, ctx) | ||
end | ||
end | ||
|
||
end | ||
matter.Plugin_Sensor_Occupancy = Matter_Plugin_Sensor_Occupancy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.