Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matter support for Virtual Devices controllable via Rules or Berry #19520

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ All notable changes to this project will be documented in this file.
### Added
- Support for HDMI CEC protocol (#19434)
- Support different baudrates on BL0942
- Matter support for Virtual Devices controllable via Rules or Berry

### Changed
- Berry fast_loop is now called every 5ms whatever the Sleep value (#19436)
Expand Down
43 changes: 43 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 @@ -172,5 +172,48 @@ class Matter_Plugin_Device : Matter_Plugin
end
end

#############################################################
# append_state_json
#
# Output the current state in JSON.
# The JSON is build via introspection to see what attributes
# exist and need to be output
# New values need to be appended with `,"key":value` (including prefix comma)
def append_state_json()
import introspect
var ret = ""

# ret: string
# attribute: attrbute name
# key: in json
def _stats_json_inner(attribute, key)
import introspect
import json
var val
if (val := introspect.get(self, attribute)) != nil
if type(val) == 'boot' val = int(val) end # transform bool into 1/0
ret += f',"{key}":{json.dump(val)}'
end
end

# lights
# print(f'{self=} {type(self)} {introspect.members(self)=}')
_stats_json_inner("shadow_onoff", "Power")
_stats_json_inner("shadow_bri", "Bri")
_stats_json_inner("shadow_ct", "CT")
_stats_json_inner("shadow_hue", "Hue")
_stats_json_inner("shadow_sat", "Sat")
# shutters
_stats_json_inner("shadow_shutter_pos", "ShutterPos")
_stats_json_inner("shadow_shutter_target", "ShutterTarget")
_stats_json_inner("shadow_shutter_tilt", "ShutterTilt")

# sensors
_stats_json_inner("shadow_contact", "Contact")
_stats_json_inner("shadow_occupancy", "Occupancy")
# print(ret)
return ret
end

end
matter.Plugin_Device = Matter_Plugin_Device
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,6 @@ class Matter_Plugin_Light0 : Matter_Plugin_Device
return m.find(key_i)
end

#############################################################
# append_state_json
#
# Output the current state in JSON
# New values need to be appended with `,"key":value` (including prefix comma)
def append_state_json()
return f',"Power":{int(self.shadow_onoff)}'
end

#############################################################
# update_virtual
#
Expand Down
10 changes: 0 additions & 10 deletions lib/libesp32/berry_matter/src/embedded/Matter_Plugin_2_OnOff.be
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,6 @@ class Matter_Plugin_OnOff : Matter_Plugin_Device

end

#############################################################
# append_state_json
#
# Output the current state in JSON
# Takes the JSON string prefix
# New values need to be appended with `,"key":value` (including prefix comma)
def append_state_json()
return f',"Power":{int(self.shadow_onoff)}'
end

#############################################################
# update_virtual
#
Expand Down
10 changes: 0 additions & 10 deletions lib/libesp32/berry_matter/src/embedded/Matter_Plugin_2_Shutter.be
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class Matter_Plugin_Shutter : Matter_Plugin_Device
var tasmota_shutter_index # Shutter number in Tasmota (zero based)
var shadow_shutter_pos
var shadow_shutter_target
var shadow_shutter_tilt
var shadow_shutter_direction # 1=opening -1=closing 0=not moving TODO
var shadow_shutter_inverted # 1=same as matter 0=matter must invert

Expand Down Expand Up @@ -225,14 +224,5 @@ class Matter_Plugin_Shutter : Matter_Plugin_Device
end
end

#############################################################
# append_state_json
#
# Output the current state in JSON
# New values need to be appended with `,"key":value` (including prefix comma)
def append_state_json(payload_str)
return f',"ShutterPos":{self.shadow_shutter_pos},"ShutterTarget":{self.shadow_shutter_target}'
end

end
matter.Plugin_Shutter = Matter_Plugin_Shutter
10 changes: 0 additions & 10 deletions lib/libesp32/berry_matter/src/embedded/Matter_Plugin_3_Light1.be
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,6 @@ class Matter_Plugin_Light1 : Matter_Plugin_Light0
end
end

#############################################################
# append_state_json
#
# Output the current state in JSON
# Takes the JSON string prefix
# New values need to be appended with `,"key":value` (including prefix comma)
def append_state_json()
return f',"Power":{int(self.shadow_onoff)},"Bri":{self.shadow_bri}'
end

#############################################################
# update_virtual
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,5 @@ class Matter_Plugin_Sensor_Contact : Matter_Plugin_Device
end
end

#############################################################
# append_state_json
#
# Output the current state in JSON
# New values need to be appended with `,"key":value` (including prefix comma)
def append_state_json()
return f',"Contact":{int(self.shadow_contact)}'
end

end
matter.Plugin_Sensor_Contact = Matter_Plugin_Sensor_Contact
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,5 @@ class Matter_Plugin_Sensor_Occupancy : Matter_Plugin_Device
end
end

#############################################################
# append_state_json
#
# Output the current state in JSON
# New values need to be appended with `,"key":value` (including prefix comma)
def append_state_json()
return f',"Occupancy":{int(self.shadow_occupancy)}'
end

end
matter.Plugin_Sensor_Occupancy = Matter_Plugin_Sensor_Occupancy
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Matter_Plugin_ShutterTilt : Matter_Plugin_Shutter
# var shadow_shutter_target
# var shadow_shutter_tilt
# var shadow_shutter_direction # 1=opening -1=closing 0=not moving TODO
var shadow_shutter_tilt
var tilt_min, tilt_max

#############################################################
Expand Down Expand Up @@ -164,14 +165,5 @@ class Matter_Plugin_ShutterTilt : Matter_Plugin_Shutter

end

#############################################################
# append_state_json
#
# Output the current state in JSON
# New values need to be appended with `,"key":value` (including prefix comma)
def append_state_json(payload_str)
return f',"ShutterPos":{self.shadow_shutter_pos},"ShutterTarget":{self.shadow_shutter_target},"ShutterTilt":{self.shadow_shutter_tilt}'
end

end
matter.Plugin_ShutterTilt = Matter_Plugin_ShutterTilt
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,6 @@ class Matter_Plugin_Light2 : Matter_Plugin_Light1

end

#############################################################
# append_state_json
#
# Output the current state in JSON
# New values need to be appended with `,"key":value` (including prefix comma)
def append_state_json(payload_str)
return f',"Power":{int(self.shadow_onoff)},"Bri":{self.shadow_bri},"CT":{self.shadow_ct}'
end

#############################################################
# update_virtual
#
Expand Down
10 changes: 0 additions & 10 deletions lib/libesp32/berry_matter/src/embedded/Matter_Plugin_4_Light3.be
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,6 @@ class Matter_Plugin_Light3 : Matter_Plugin_Light1

end

#############################################################
# append_state_json
#
# Output the current state in JSON
# Takes the JSON string prefix
# New values need to be appended with `,"key":value` (including prefix comma)
def append_state_json(payload_str)
return f',"Power":{int(self.shadow_onoff)},"Bri":{self.shadow_bri},"Hue":{self.shadow_hue},"Sat":{self.shadow_sat}'
end

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