From c8549032a212baf2307567ea3b8df0483818708f Mon Sep 17 00:00:00 2001 From: Patrick Pichon Date: Sun, 5 Nov 2023 18:54:32 +0100 Subject: [PATCH] wip --- Classes/ZigpyTransport/AppBellows.py | 23 ++++-- Classes/ZigpyTransport/AppDeconz.py | 13 ++++ Classes/ZigpyTransport/AppGeneric.py | 110 +++++++++++++++++++++------ Classes/ZigpyTransport/AppZnp.py | 18 ++++- plugin.py | 2 +- 5 files changed, 134 insertions(+), 32 deletions(-) diff --git a/Classes/ZigpyTransport/AppBellows.py b/Classes/ZigpyTransport/AppBellows.py index c0d6c5b7f..70fc55b93 100644 --- a/Classes/ZigpyTransport/AppBellows.py +++ b/Classes/ZigpyTransport/AppBellows.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python3btt. # coding: utf-8 -*- # # Author: deufo, badz & pipiche38 @@ -7,8 +7,8 @@ import logging import bellows.config as bellows_conf -import bellows.types as bt -import zigpy.types as t +import bellows.types as t +import zigpy.types as zigpy_t import bellows.zigbee.application import zigpy.config as zigpy_conf import zigpy.device @@ -137,7 +137,7 @@ async def register_endpoints(self, endpoint=1): def get_device(self, ieee=None, nwk=None): return Classes.ZigpyTransport.AppGeneric.get_device(self, ieee, nwk) - def handle_join(self, nwk: bt.EmberNodeId, ieee: bt.EmberEUI64, parent_nwk: bt.EmberNodeId, *, handle_rejoin: bool = True,) -> None: + def handle_join(self, nwk: t.EmberNodeId, ieee: t.EmberEUI64, parent_nwk: t.EmberNodeId, *, handle_rejoin: bool = True,) -> None: return Classes.ZigpyTransport.AppGeneric.handle_join(self, nwk, ieee, parent_nwk) def get_device_ieee(self, nwk): @@ -149,8 +149,21 @@ def handle_leave(self, nwk, ieee): def get_zigpy_version(self): return Classes.ZigpyTransport.AppGeneric.get_zigpy_version(self) - def packet_received(self, packet: t.ZigbeePacket) -> None: + def packet_received(self, packet: zigpy_t.ZigbeePacket) -> None: return Classes.ZigpyTransport.AppGeneric.packet_received(self,packet) + + def handle_message( + self, + sender: zigpy.device.Device, + profile: int, + cluster: int, + src_ep: int, + dst_ep: int, + message: bytes, + * , + dst_addressing=None, + )->None: + return Classes.ZigpyTransport.AppGeneric.handle_message(self,sender,profile,cluster,src_ep,dst_ep,message, dst_addressing=dst_addressing) async def set_zigpy_tx_power(self, power): # EmberConfigTxPowerMode - EZSP_CONFIG_TX_POWER_MODE in EzspConfigId diff --git a/Classes/ZigpyTransport/AppDeconz.py b/Classes/ZigpyTransport/AppDeconz.py index e7ace4f73..1f4c6a39e 100644 --- a/Classes/ZigpyTransport/AppDeconz.py +++ b/Classes/ZigpyTransport/AppDeconz.py @@ -145,6 +145,19 @@ def handle_leave(self, nwk, ieee): def packet_received(self, packet: t.ZigbeePacket) -> None: return Classes.ZigpyTransport.AppGeneric.packet_received(self,packet) + def handle_message( + self, + sender: zigpy.device.Device, + profile: int, + cluster: int, + src_ep: int, + dst_ep: int, + message: bytes, + * , + dst_addressing=None, + ) -> None: + return Classes.ZigpyTransport.AppGeneric.handle_message(self,sender,profile,cluster,src_ep,dst_ep,message,dst_addressing=dst_addressing) + async def set_zigpy_tx_power(self, power): pass #await self._api.set_tx_power(power) diff --git a/Classes/ZigpyTransport/AppGeneric.py b/Classes/ZigpyTransport/AppGeneric.py index a1d1911ba..63fe39194 100644 --- a/Classes/ZigpyTransport/AppGeneric.py +++ b/Classes/ZigpyTransport/AppGeneric.py @@ -172,8 +172,14 @@ def get_zigpy_version(self): return self.version -def packet_received(self, packet: t.ZigbeePacket) -> None: +def packet_received( + self, + packet: t.ZigbeePacket + ) -> None: + """Notify zigpy of a received Zigbee packet.""" + self.log.logging("TransportZigpy", "Debug", "packet_received %s" %(packet)) + try: sender = self.get_device_with_address(packet.src) self.log.logging("TransportZigpy", "Debug", "identified device - %s (%s)" %(str(sender), type(sender)) ) @@ -188,57 +194,115 @@ def packet_received(self, packet: t.ZigbeePacket) -> None: message = packet.data.serialize() hex_message = binascii.hexlify(message).decode("utf-8") dst_addressing = packet.dst.addr_mode if packet.dst else None - - write_capture_rx_frames( self, sender, profile, cluster, src_ep, dst_ep, message, hex_message, dst_addressing) - self.log.logging("TransportZigpy", "Debug", "packet_received - %s %s %s %s %s %s %s %s" %( + self.log.logging("TransportZigpy", "Debug", "packet_received - %s %s %s %s %s %s %s %s" %( sender, profile, cluster, src_ep, dst_ep, message, hex_message, dst_addressing)) + + _handle_message( self, sender, profile, cluster, src_ep, dst_ep, message, dst_addressing, packet) + return + +def handle_message( + self, + sender: zigpy.device.Device, + profile: int, + cluster: int, + src_ep: int, + dst_ep: int, + message: bytes, + dst_addressing=None, + ) -> None: + """Notify zigpy of a received handle_message.""" + _handle_message( self, sender, profile, cluster, src_ep, dst_ep, message, dst_addressing) + + +def _handle_message( + self, + sender: zigpy.device.Device, + profile: int, + cluster: int, + src_ep: int, + dst_ep: int, + message: bytes, + dst_addressing=None, + Packet=None + ) -> None: + + hex_message = binascii.hexlify(message).decode("utf-8") + write_capture_rx_frames( self, sender, profile, cluster, src_ep, dst_ep, message, hex_message, dst_addressing) + if sender.nwk == 0x0000: - # When coming from coordinator we have to send it back to zigpy - self.log.logging("TransportZigpy", "Debug", "packet_received from Controller Sender: %s Profile: %04x Cluster: %04x srcEp: %02x dstEp: %02x message: %s" %( + self.log.logging("TransportZigpy", "Debug", "handle_message from Controller Sender: %s Profile: %04x Cluster: %04x srcEp: %02x dstEp: %02x message: %s" %( str(sender.nwk), profile, cluster, src_ep, dst_ep, hex_message)) - super(type(self),self).packet_received(packet) + #self.super().handle_message(sender, profile, cluster, src_ep, dst_ep, message) + if Packet: + super(type(self),self).packet_received(Packet) + else: + super(type(self),self).handle_message(sender, profile, cluster, src_ep, dst_ep, message) if cluster == 0x8036: # This has been handle via on_zdo_mgmt_permitjoin_rsp() - self.log.logging("TransportZigpy", "Debug", "packet_received 0x8036: %s Profile: %04x Cluster: %04x srcEp: %02x dstEp: %02x message: %s" %( + self.log.logging("TransportZigpy", "Debug", "handle_message 0x8036: %s Profile: %04x Cluster: %04x srcEp: %02x dstEp: %02x message: %s" %( str(sender.nwk), profile, cluster, src_ep, dst_ep, hex_message)) self.callBackFunction( build_plugin_8014_frame_content(self, str(sender), hex_message ) ) - super(type(self),self).packet_received(packet) + if Packet: + super(type(self),self).packet_received(Packet) + else: + super(type(self),self).handle_message(sender, profile, cluster, src_ep, dst_ep, message) return if cluster == 0x8034: # This has been handle via on_zdo_mgmt_leave_rsp() - self.log.logging("TransportZigpy", "Debug", "packet_received 0x8036: %s Profile: %04x Cluster: %04x srcEp: %02x dstEp: %02x message: %s" %( + self.log.logging("TransportZigpy", "Debug", "handle_message 0x8036: %s Profile: %04x Cluster: %04x srcEp: %02x dstEp: %02x message: %s" %( str(sender.nwk), profile, cluster, src_ep, dst_ep, hex_message)) self.callBackFunction( build_plugin_8047_frame_content(self, str(sender), hex_message) ) return addr = None if sender.nwk is not None: + addr_mode = 0x02 addr = sender.nwk.serialize()[::-1].hex() + if profile and cluster: + self.log.logging( + "TransportZigpy", + "Debug", + "handle_message device 1: %s Profile: %04x Cluster: %04x sEP: %s dEp: %s message: %s lqi: %s" % ( + str(sender), profile, cluster, src_ep, dst_ep, hex_message, sender.lqi)), + elif sender.ieee is not None: addr = "%016x" % t.uint64_t.deserialize(sender.ieee.serialize())[0] - - addr_mode = 0x02 if sender.nwk is not None else 0x03 - sender.lqi = sender.lqi or 0x00 + addr_mode = 0x03 + if profile and cluster: + self.log.logging( + "TransportZigpy", + "Debug", + "handle_message device 1: %s Profile: %04x Cluster: %04x sEP: %s dEp: %s message: %s lqi: %s" % ( + str(sender), profile, cluster, src_ep, dst_ep, hex_message, sender.lqi)), + + if sender.lqi is None: + sender.lqi = 0x00 - # Let's force profile to ZDP if eps == 0x00 if src_ep == dst_ep == 0x00: profile = 0x0000 - if profile is not None and cluster is not None: - self.log.logging( "TransportZigpy", "Debug", "packet_received device: %s Profile: %04x Cluster: %04x sEP: %s dEp: %s message: %s lqi: %s" % ( - str(addr), profile, cluster, src_ep, dst_ep, hex_message, sender.lqi), ) + if profile and cluster: + self.log.logging( + "TransportZigpy", + "Debug", + "handle_message device 2: %s Profile: %04x Cluster: %04x sEP: %s dEp: %s message: %s lqi: %s" % ( + str(addr), profile, cluster, src_ep, dst_ep, hex_message, sender.lqi), + ) - if addr is not None: + if addr: plugin_frame = build_plugin_8002_frame_content(self, addr, profile, cluster, src_ep, dst_ep, message, sender.lqi, src_addrmode=addr_mode) - self.log.logging("TransportZigpy", "Debug", "packet_received Sender: %s frame for plugin: %s" % (addr, plugin_frame)) - return self.callBackFunction(plugin_frame) - - self.log.logging( "TransportZigpy", "Error", "packet_received - Issue with sender is %s %s" % ( - sender.nwk, sender.ieee), ) + self.log.logging("TransportZigpy", "Debug", "handle_message Sender: %s frame for plugin: %s" % (addr, plugin_frame)) + self.callBackFunction(plugin_frame) + else: + self.log.logging( + "TransportZigpy", + "Error", + "handle_message - Issue with sender is %s %s" % (sender.nwk, sender.ieee), + ) return diff --git a/Classes/ZigpyTransport/AppZnp.py b/Classes/ZigpyTransport/AppZnp.py index b6de747e8..15cead8ec 100644 --- a/Classes/ZigpyTransport/AppZnp.py +++ b/Classes/ZigpyTransport/AppZnp.py @@ -13,8 +13,8 @@ import zigpy.zdo.types as zdo_types import zigpy_znp.commands.util import zigpy_znp.config as znp_conf -#import zigpy_znp.types as znp_t -import zigpy.types as t +import zigpy_znp.types as t +import zigpy.types as zigpy_t import zigpy_znp.zigbee.application from Classes.ZigpyTransport.firmwareversionHelper import \ znp_extract_versioning_for_plugin @@ -117,8 +117,20 @@ def handle_leave(self, nwk, ieee): def get_zigpy_version(self): return Classes.ZigpyTransport.AppGeneric.get_zigpy_version(self) - def packet_received(self, packet: t.ZigbeePacket) -> None: + def packet_received(self, packet: zigpy_t.ZigbeePacket) -> None: return Classes.ZigpyTransport.AppGeneric.packet_received(self,packet) + + def handle_message( + self, + sender: zigpy.device.Device, + profile: int, + cluster: int, + src_ep: int, + dst_ep: int, + message: bytes, + dst_addressing=None, + ) -> None: + return Classes.ZigpyTransport.AppGeneric.handle_message(self,sender,profile,cluster,src_ep,dst_ep,message, dst_addressing=dst_addressing) async def set_zigpy_tx_power(self, power): self.log.logging("TransportZigpy", "Debug", "set_tx_power %s" %power) diff --git a/plugin.py b/plugin.py index 8ce8404a3..58c09d6b1 100644 --- a/plugin.py +++ b/plugin.py @@ -1505,7 +1505,7 @@ def check_python_modules_version( self ): "zigpy": "0.59.0", "zigpy_znp": "0.11.6", "zigpy_deconz": "0.21.1", - "bellows": "0.35.8", + "bellows": "0.36.8", } flag = True