|
| 1 | +# |
| 2 | +# Matter_Plugin_Sensor_Boolean.be - implements the behavior for an abstract boolean sensor - to be inherited |
| 3 | +# |
| 4 | +# Copyright (C) 2023 Stephan Hadinger & Theo Arends |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +# |
| 19 | + |
| 20 | +import matter |
| 21 | + |
| 22 | +# Matter plug-in for core behavior |
| 23 | + |
| 24 | +#@ solidify:Matter_Plugin_Sensor_Boolean,weak |
| 25 | + |
| 26 | +class Matter_Plugin_Sensor_Boolean : Matter_Plugin_Device |
| 27 | + # static var TYPE = "" # name of the plug-in in json |
| 28 | + # static var DISPLAY_NAME = "" # display name of the plug-in |
| 29 | + static var ARG = "switch" # additional argument name (or empty if none) |
| 30 | + static var ARG_HINT = "Switch<x> number" |
| 31 | + static var ARG_TYPE = / x -> int(x) # function to convert argument to the right type |
| 32 | + static var UPDATE_TIME = 750 # update every 750ms |
| 33 | + |
| 34 | + var tasmota_switch_index # Switch number in Tasmota (one based) |
| 35 | + var shadow_bool_value |
| 36 | + |
| 37 | + ############################################################# |
| 38 | + # Constructor |
| 39 | + def init(device, endpoint, config) |
| 40 | + super(self).init(device, endpoint, config) |
| 41 | + self.shadow_bool_value = false |
| 42 | + end |
| 43 | + |
| 44 | + ############################################################# |
| 45 | + # parse_configuration |
| 46 | + # |
| 47 | + # Parse configuration map |
| 48 | + def parse_configuration(config) |
| 49 | + self.tasmota_switch_index = int(config.find(self.ARG #-'switch'-#, 1)) |
| 50 | + if self.tasmota_switch_index <= 0 self.tasmota_switch_index = 1 end |
| 51 | + end |
| 52 | + |
| 53 | + ############################################################# |
| 54 | + # Update shadow |
| 55 | + # |
| 56 | + def update_shadow() |
| 57 | + super(self).update_shadow() |
| 58 | + if !self.VIRTUAL |
| 59 | + var switch_str = "Switch" + str(self.tasmota_switch_index) |
| 60 | + |
| 61 | + var j = tasmota.cmd("Status 10", true) |
| 62 | + if j != nil j = j.find("StatusSNS") end |
| 63 | + if j != nil && j.contains(switch_str) |
| 64 | + var state = (j.find(switch_str) == "ON") |
| 65 | + |
| 66 | + if (self.shadow_bool_value != state) |
| 67 | + self.value_updated() |
| 68 | + end |
| 69 | + self.shadow_bool_value = state |
| 70 | + end |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + ############################################################# |
| 75 | + # value_updated |
| 76 | + # |
| 77 | + # This is triggered when a new value is changed, for subscription |
| 78 | + # This method is meant to be overloaded and maximize shared code |
| 79 | + def value_updated() |
| 80 | + end |
| 81 | + |
| 82 | + ############################################################# |
| 83 | + # For Bridge devices |
| 84 | + ############################################################# |
| 85 | + ############################################################# |
| 86 | + # Stub for updating shadow values (local copies of what we published to the Matter gateway) |
| 87 | + # |
| 88 | + # This call is synnchronous and blocking. |
| 89 | + def parse_status(data, index) |
| 90 | + if index == 10 # Status 10 |
| 91 | + var state = false |
| 92 | + |
| 93 | + state = (data.find("Switch" + str(self.tasmota_switch_index)) == "ON") |
| 94 | + |
| 95 | + if self.shadow_bool_value != nil && self.shadow_bool_value != bool(state) |
| 96 | + self.value_updated() |
| 97 | + end |
| 98 | + self.shadow_bool_value = state |
| 99 | + end |
| 100 | + end |
| 101 | + ############################################################# |
| 102 | + ############################################################# |
| 103 | +end |
| 104 | +matter.Plugin_Sensor_Boolean = Matter_Plugin_Sensor_Boolean |
0 commit comments