@bastler , @Spaceman_Spiff Thank you for you answers
Yes,
self.listen_event('iPhoneIVTPingOK', self.on_all, EventFilter(ItemStateChangedEvent))
behaves as expected and as I need for evaluation of state change.
True
I did not know how to use these filters and asked GPT4:
give me an example of how to use these habapp classes:
# Todo: Drop this when we go OH4.0 only
class ItemStateEventFilter(TypeBoundEventFilter):
def __init__(self, value: Any = MISSING):
super().__init__(ItemStateEvent, value=value)
class ItemStateUpdatedEventFilter(TypeBoundEventFilter):
def __init__(self, value: Any = MISSING):
super().__init__(ItemStateUpdatedEvent, value=value)
class ItemStateChangedEventFilter(TypeBoundEventFilter):
def __init__(self, value: Any = MISSING, old_value: Any = MISSING):
super().__init__(ItemStateChangedEvent, value=value, old_value=old_value)
class ItemCommandEventFilter(TypeBoundEventFilter):
def __init__(self, value: Any = MISSING):
super().__init__(ItemCommandEvent, value=value)
And got this answer:
Okay, understanding that these classes are from HABApp (which facilitates working with OpenHAB in a Pythonic way), the usage would be tied to how HABApp handles events and rules.
HABApp creates a straightforward way to listen to OpenHAB events and react accordingly using Python. The event filters you provided allow you to fine-tune which events you want to listen for.
Here’s a basic example of how you might set up a rule using these event filters in HABApp:
import HABApp
from HABApp.core.events import ItemStateEvent, ItemStateUpdatedEvent, ItemStateChangedEvent, ItemCommandEvent
from HABApp.core.items import Item
from .your_event_filters_module import ItemStateEventFilter, ItemStateUpdatedEventFilter, ItemStateChangedEventFilter, ItemCommandEventFilter
class MyRule(HABApp.Rule):
def __init__(self):
super().__init__()
# Create filters
self.state_filter = ItemStateEventFilter(value="ON")
self.update_filter = ItemStateUpdatedEventFilter(value="OFF")
self.change_filter = ItemStateChangedEventFilter(value="OFF", old_value="ON")
self.command_filter = ItemCommandEventFilter(value="TOGGLE")
# Listen for events
self.listen_event('SomeOpenHABItem', self.item_turned_on, self.state_filter)
self.listen_event('AnotherOpenHABItem', self.item_updated_off, self.update_filter)
self.listen_event('YetAnotherOpenHABItem', self.item_changed_from_on_to_off, self.change_filter)
self.listen_event('FinalOpenHABItem', self.item_received_toggle_command, self.command_filter)
def item_turned_on(self, event: ItemStateEvent):
self.log.info(f"Item {event.item_name} is now ON")
def item_updated_off(self, event: ItemStateUpdatedEvent):
self.log.info(f"Item {event.item_name} was updated to OFF")
def item_changed_from_on_to_off(self, event: ItemStateChangedEvent):
self.log.info(f"Item {event.item_name} changed from ON to OFF")
def item_received_toggle_command(self, event: ItemCommandEvent):
self.log.info(f"Item {event.item_name} received a TOGGLE command")
# Register the rule to make it run
MyRule()
This example assumes that the event filters are defined in a separate module, which I’ve named .your_event_filters_module
for the sake of the example. The way these filters work is they listen for events on specific OpenHAB items and then trigger specified functions in response.
Again, this is a basic demonstration to give you an idea. You may need to adjust based on your specific requirements and the latest capabilities of HABApp.
This is pretty amazing!