as the listen_event does not accept a parameter like “event name” like “SELECT”, “OFF”, “ON”, “PREV”, NEXT", which I could evaluate in the callback function.
of, if i do not add the event=... i handle it in the callback:
OpenhabItem.get_item('iGarage_Ro_Kt').listen_event(self.garage_door_cnt,
ItemStateChangedEventFilter())
...
def garage_door_cnt(self, event):
value = event.value
if value == 'ON':
pass
elif value == 'OFF':
pass
In my living room, I have on the wall sets of switches that should switch lights on or dim etc.
The case is also that the same light might be switched by different switches positioned across the living room.
First of all don’t use importlib.reload since it’ll create problems and very hard to track down issues.
It’s a super dirty hack that sometimes works but sometimes doesn’t.
Also there is no need to use it because you can just pass the values as an argument into your rule.
Inheritance is the wrong tool here.
Your examples of what you are trying to achieve are very inconsistent:
Sometimes you want to trigger on different channels, sometimes it’s the same channel with a different value and it’s all mixed.
Your last example is just a super complicated way to write
Thank you for your feedback!
The work you’ve done for HABApp is truly admirable!
During the development of a local library, this is the only (as I know) way to get the changes of the library get updated.
The hint of inheritance I do not get, related to importlib.reload?
a) What I want to achieve is to define at one place in the code what sort of triggers (channels-triggers, item state, or update changes) are connected to what desired actions.
b) When the rule triggers I would like to know what action needs to be done, regardless of which trigger ( (channels-triggers, item state, or update changes) initiated it.
How would you write the sample, but which would be scalable to 100+ trigger definitions?
And also open the possibility to define these 100+ triggers either by a generic JSON, or YAML definition.
I have seen only one possibility: one generic trigger-receiving function and then branching out to actions.
As I said - this might sometimes work but is not the proper way.
Put your whole definition in the rule file and work there.
Once you are done you can move the definition to another file.
You are using inheritance for something that is basically a shared function.
It might work but there are easier ways to achieve the same.
value_to_text = {
'1002': 'SELECT',
...
}
class MyChannelRule(Rule):
def __init__(self, channel: str):
super().__init__()
self.listen_event(channel, self.my_func, EventFilter(ChannelTriggeredEvent))
def my_func(self, event: ChannelTriggeredEvent)
text = value_to_text.get(event.event)
if text is None:
return None
print(event, text)
# this can also be done depending on a HABApp Parameter file
MyChannelRule('deconz:switch:00212E00C488:04cd15fffe6f9d60011000:buttonevent')
You can make the class instantiation depending on the HABApp Parameters.
This works out of the box and you can automtically reload the rule file when the parameter file changes.
See the docs for an example.
If you have some spare time have a look at the link above I posted about my light control.
There is one big JSON definition for the light system behavior (toggle, dim, timeout,day-night time, movement presence detection, sunset, …
There is one rule handling all these different triggers (100+ lights, 100+ pushbutton switches)
There is one callback function for all these triggers.
I already ported the system from DSL rules to Jython.
I seems like it. Inheritance is not the right way to do it non the less.
This should work as expected with one big definition
class MyChannelRule(Rule):
def __init__(self, channel: str, value_to_str: dict[str, str]):
super().__init__()
self.value_to_str = value_to_str
self.listen_event(channel, self.my_func, EventFilter(ChannelTriggeredEvent))
def my_func(self, event: ChannelTriggeredEvent):
text = self.value_to_str.get(event.event)
if text is None:
return None
print(event, text)
my_def = {
'deconz:switch:00212E00C488:04cd15fffe6f9d60011000:buttonevent': {
'1002': 'SELECT'
...
}
}
for channel, value_to_str in my_def.items():
# this can also be done depending on a HABApp Parameter file
MyChannelRule(channel, value_to_str)