This release brings lots of improvements the most notably full support for tags and groups and an easy option to search for items.
Item search
# This seaches for items that are in group 'my_group' and have the tags set
for item in self.get_items(groups='my_group', tags=['tag1', 'tag2']):
print(item.tags) # All tags are now on the OpenhabItem
print(item.groups) # All defined groups are accessable, too
# This returns all switch items
for item in self.get_items(type=SwitchItem):
print(item.is_on())
# This returns all items who's name end with _battery
for item in self.get_items(name='_battery'):
...
Groups and Tags
group = GroupItem.get_item('My_Group')
# It's possible to get the defined children of a group directly
for item in group.members:
item.value
# Items have the definition as a property
item.tags
item.groups
Yes and sensor is the corresponding item, not the name of the item.
So HABApp does the item lookup for you.
As always - I recommend using a good naming scheme over groups because it’s much more flexible (e.g. Plant_Name_Sensor) and it allows changes in logic without having to edit the item definitions.
Another even better option is to pass the item names directly to the rule class
It’s also possible to search for items by metadata
class MyRule(HABApp.Rule):
def __init__(self):
super().__init__()
# This returns all homekit items
for homekit_item in self.get_items(metadata='homekit'):
print(homekit_item)
# This returns all homekit items which are a valve
for homekit_valve in self.get_items(metadata='homekit', metadata_value='Valve'):
print(homekit_valve)
EventListenerGroup
EventListenerGroup is a helper class which allows to subscribe to multiple items at once.
All subscriptions can be canceled together, too.
This is useful if e.g. something has to be done once after a sensor reports a value.
This is a rule which will turn on the lights once (!) in a room on the first movement in the morning.
The lights will only turn on after 4 and before 8 and two movement sensors are used to pick up movement.
from datetime import time
from HABApp import Rule
from HABApp.core.events import ValueChangeEvent
from HABApp.openhab.items import SwitchItem, NumberItem
from HABApp.util import EventListenerGroup
class EventListenerGroupExample(Rule):
def __init__(self):
super().__init__()
self.lights = SwitchItem.get_item('RoomLights')
self.sensor_move_1 = NumberItem.get_item('MovementSensor1')
self.sensor_move_1 = NumberItem.get_item('MovementSensor2')
# use the defaults so we don't have to pass the callback and event filter in add_listener
self.group = EventListenerGroup(default_callback=self.sensor_changed, default_event_filter=ValueChangeEvent).\
add_listener(self.sensor_move_1).add_listener(self.sensor_move_1)
self.run.on_every_day(time(4), self.listen_sensors)
self.run.on_every_day(time(8), self.sensors_cancel)
def listen_sensors(self):
self.group .listen()
def sensors_cancel(self):
self.group .cancel()
def sensor_changed(self, event):
self.group .cancel()
self.lights.on()
EventListenerGroupExample()