You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So I tried to be clever and structure my code into classes. It works, kind of.
My use case is I have a few ZigBee devices of the same type in different rooms for which I wait for events and then do stuff.
I created a base class for the zha event, then classes for different devices that inherit from the base class, and then I created instanses of the classes in several script files (pyscript/scripts/).
The base class creates a list of triggers that is attached to self, so I expected those triggers to be destroyed when the reference to the class instance is lost, ie when reloading the script that creates the class instance and has the reference to it.
But that is not happening, instead every time I reload the script it just adds more triggers without destroying the previous ones. I have to reload the file that contains the definition of the base class for the triggers to be destroyed.
But if I don't have a reference to the instance (remove ws_hallway below) then nothing happens when I push the switch which I assume is because the triggers have been destroyed when the instance was not referenced anymore.
What am I missing here?
This is basically how I implemented it:
pyscript/modules/zha.py:
classZhaEventBase:
def__init__(self):
# Cannot use `super().__init__()` so use `zha_event_base()` insteadpassdefzha_event_base(self,
device_id:str,
command_callbacks:dict):
""" `command_callbacks` is a flat dictonary with `"command": callback`. """self.triggers= []
forcommand, callbackincommand_callbacks.items():
ifcallbackisnotNone:
log.debug(f"Registering command '{command}'")
@event_trigger("zha_event", f"device_id == '{device_id}' and command == '{command}'")defzha_event_callback(device_id=None, command=None, args=None, **kwargs):
callback(device_id, command, args)
self.triggers.append(zha_event_callback)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
So I tried to be clever and structure my code into classes. It works, kind of.
My use case is I have a few ZigBee devices of the same type in different rooms for which I wait for events and then do stuff.
I created a base class for the zha event, then classes for different devices that inherit from the base class, and then I created instanses of the classes in several script files (pyscript/scripts/).
The base class creates a list of triggers that is attached to
self
, so I expected those triggers to be destroyed when the reference to the class instance is lost, ie when reloading the script that creates the class instance and has the reference to it.But that is not happening, instead every time I reload the script it just adds more triggers without destroying the previous ones. I have to reload the file that contains the definition of the base class for the triggers to be destroyed.
But if I don't have a reference to the instance (remove
ws_hallway
below) then nothing happens when I push the switch which I assume is because the triggers have been destroyed when the instance was not referenced anymore.What am I missing here?
This is basically how I implemented it:
pyscript/modules/zha.py:
pyscript/modules/philips_hue.py:
pyscript/scripts/hallway.py:
Beta Was this translation helpful? Give feedback.
All reactions