Items are like variables. They have a name and a value (which can be anything). Items from openHAB use the item name from openHAB and get created when HABApp successfully connects to openHAB or when the openHAB configuration changes. Items from MQTT use the topic as item name and get created as soon as a message gets processed.
Some item types provide convenience functions, so it is advised to always set the correct item type.
The preferred way to get and create items is through the class factories ~HABApp.core.items.Item.get_item
and ~HABApp.core.items.Item.get_create_item
since this ensures the proper item class and provides type hints when using an IDE!
If an item value gets set there will be a ~HABApp.core.ValueUpdateEvent
on the event bus. If it changes there will be additionally a ~HABApp.core.ValueChangeEvent
, too.
It is possible to check the item value by comparing it
An overview over the item types can be found on the HABApp item section <HABAPP_ITEM_TYPES>
, the openHAB item section <OPENHAB_ITEM_TYPES>
and the the mqtt item section <MQTT_ITEM_TYPES>
It is possible to listen to events through the ~HABApp.Rule.listen_event
function. The passed function will be called as soon as an event occurs and the event will pe passed as an argument into the function.
There is the possibility to reduce the function calls to a certain event type with an additional event filter (typically ~HABApp.core.ValueUpdateEventFilter
or ~HABApp.core.ValueChangeEventFilter
).
An overview over the events can be found on the HABApp event section <HABAPP_EVENT_TYPES>
, the openHAB event section <OPENHAB_EVENT_TYPES>
and the the MQTT event section <MQTT_EVENT_TYPES>
Additionally there is the possibility to filter not only on the event type but on the event values, too. This can be achieved by passing the value to the event filter. There are convenience Filters (e.g. ~HABApp.core.events.ValueUpdateEventFilter
and ~HABApp.core.events.ValueChangeEventFilter
) for the most used event types that provide type hints.
HABApp.core.events.NoEventFilter
HABApp.core.events.EventFilter
HABApp.core.events.ValueUpdateEventFilter
HABApp.core.events.ValueChangeEventFilter
HABApp.core.events.AndFilterGroup
HABApp.core.events.OrFilterGroup
With the scheduler it is easy to call functions in the future or periodically. Do not use time.sleep but rather self.run.at. Another very useful function is self.run.countdown as it can simplify many rules!
Function | Description |
---|---|
~HABApp.rule.scheduler.HABAppSchedulerView.soon |
Run the callback as soon as possible (typically in the next second). |
~HABApp.rule.scheduler.HABAppSchedulerView.at |
Run the callback in x seconds or at a specified time. |
~HABApp.rule.scheduler.HABAppSchedulerView.countdown |
Run a function after a time has run down |
~HABApp.rule.scheduler.HABAppSchedulerView.every |
Run a function periodically |
~HABApp.rule.scheduler.HABAppSchedulerView.every_minute |
Run a function every minute |
~HABApp.rule.scheduler.HABAppSchedulerView.every_hour |
Run a function every hour |
~HABApp.rule.scheduler.HABAppSchedulerView.on_every_day |
Run a function at a specific time every day |
~HABApp.rule.scheduler.HABAppSchedulerView.on_workdays |
Run a function at a specific time on workdays |
~HABApp.rule.scheduler.HABAppSchedulerView.on_weekends |
Run a function at a specific time on weekends |
~HABApp.rule.scheduler.HABAppSchedulerView.on_day_of_week |
Run a function at a specific time on specific days of the week |
~HABApp.rule.scheduler.HABAppSchedulerView.on_sun_dawn |
Run a function on dawn |
~HABApp.rule.scheduler.HABAppSchedulerView.on_sunrise |
Run a function on sunrise |
~HABApp.rule.scheduler.HABAppSchedulerView.on_sunset |
Run a function on sunset |
~HABApp.rule.scheduler.HABAppSchedulerView.on_sun_dusk |
Run a function on dusk |
All functions return an instance of ScheduledCallbackBase
HABApp.rule.scheduler.HABAppSchedulerView
External tools can be run with the ~HABApp.Rule.execute_subprocess
function. Once the process has finished the callback will be called with an ~HABApp.rule.FinishedProcessInfo
instance as first argument. Example:
import HABApp
class MyExecutionRule(HABApp.Rule):
def __init__(self):
super().__init__()
self.execute_subprocess( self.func_when_finished, 'path_to_program', 'arg1', capture_output=True)
def func_when_finished(self, process_info):
assert isinstance(process_info, HABApp.rule.FinishedProcessInfo)
print(process_info)
MyExecutionRule()
HABApp.rule.FinishedProcessInfo
- var int returncode
Return code of the process (0: IO, -1: Exception while starting process)
- var str stdout
Standard output of the process or None
- var str stderr
Error output of the process or None
This example shows how to properly get a rule during runtime and execute one of its function. With the proper import and type hint this method provides syntax checks and auto complete.
Rule instances can be accessed by their name (typically the class name). In the HABApp.log
you can see the name when the rule is loaded. If you want to assign a custom name, you can change the rule name easily by assigning it to self.rule_name
in __init__
.
Important
Always look up rule every time, never assign to a class member! The rule might get reloaded and then the class member will still point to the old unloaded instance.
rule_a.py:
import HABApp
class ClassA(HABApp.Rule):
...
def function_a(self):
...
ClassA()
rule_b.py:
import HABApp
import typing
if typing.TYPE_CHECKING: # This is only here to allow
from .rule_a import ClassA # type hints for the IDE
class ClassB(HABApp.Rule):
...
def function_b(self):
r = self.get_rule('ClassA') # type: ClassA
# The comment "# type: ClassA" will signal the IDE that the value returned from the
# function is an instance of ClassA and thus provide checks and auto complete.
# this calls the function on the instance
r.function_a()
HABApp.Rule
- var async_http
Async http connections <ref_async_io>
- var mqtt
MQTT interaction <ref_mqtt>
- var openhab
openhab interaction <ref_openhab>
- var oh
short alias for :py
openhab
openhab