Skip to main content

Easy automation with openHAB and/or MQTT. Create home automation rules in python.

Project description

HABApp

Easy automation with openHAB and/or MQTT Build Status

HABApp is a asyncio/multithread application that connects to an openhab instance and/or a MQTT broker. It is possible to create rules that listen to events from these instances and then react accordingly.

Goals

The goal of this application is to provide a simple way to create home automation rules in python. With full syntax highlighting and descriptive names it should almost never be required to look something up in the documentation

Installation

The installation is very easy. This module can be installed through pip (or pip3 on linux):

pip install HABApp

However it is recommended to create a virtual environment, first. Once the virtual environment is activated the habapp-command can be used to run it.

Usage

First start

It is recommended to specify a folder before first start.

python -m HABApp -c /Path/to/Config/Folder/
habapp -c /Path/to/Config/Folder/              (only in activated virtual environment)

This will also create some dummy configuration files:

  • config.yml: Used to configure the behaviour of HABApp
  • logging.yml : Used to setup logging

For more information check the HABApp wiki!

Creating rules

Create a python file in the rules directory (see config.yml) This rule will automatically be reloaded when the file changes.

There are several working examples in the conf folder

A simple rule could look like this

import HABApp
from HABApp.core.events import ValueChangeEvent, ValueUpdateEvent

class MyRule(HABApp.Rule):
    def __init__(self):
        super().__init__()

        # Subscribe to ItemStateEvent for item TestSwitchTOGGLE
        self.listen_event( 'TestSwitchTOGGLE', self.cb, ValueUpdateEvent)

    def cb(self, event):
        assert isinstance(event, ValueUpdateEvent)  # this will provide syntax highlighting for event
        self.post_update('MyOtherItem', 'test')

MyRule()

# MQTT example
class MyMQTTRule(HABApp.Rule):
    def __init__(self):
        super().__init__()

        # Subscribe to topic but only process changes
        self.listen_event( 'test/topic1', self.cb, ValueChangeEvent)

    def cb(self, event):
        assert isinstance(event, ValueChangeEvent)
        self.mqtt_publish( 'test/topic2', event.value + 1)

MyMQTTRule()

More functionality is available through class functions. It is recommended to use an editor with syntax highlighting. Examples:

# Listen for events
self.listen_event(name, callback)               # Callback on any event
self.listen_event(name, callback, even_type)    # Callback if event is instance of event_type


# Watch if items do not change a specific ammount of time and then generate an event
self.item_watch(item_name, seconds_constant, watch_only_changes = True)
# This also registers an event listener with a corresponding event
self.item_watch_and_listen(item_name, seconds_constant, callback, watch_only_changes = True)


# HAPApp item cache
# Lookups are almost instant so this can be used to get states inside of rules
self.get_item_state(item_name)
self.get_item(item_name)
self.item_exists(item_name)
self.set_item_state(item_name, value)   # If the item does not exist it will be created


# Post an event to the HABApp event bus.
# Can be used to post and react to custom events together with self.listen_event
# Also creates an item in the item cache
self.post_event(name, event)


# Interact with openhab (if configured)
self.post_update(item_name, value)
self.send_command(item_name, value)
self.create_openhab_item(item_type, item_name)
self.remove_openhab_item(item_name)


# MQTT (if configured)
# Node: subscribing is possible through the config,
#       changes to mqtt config entries get picked up without a restart
self.mqtt_publish(self, topic, payload, qos=None, retain=None)


# Time intervalls
self.run_every( date_time, interval, callback)
self.run_on_day_of_week(time, ['Mon,Tue'], callback)
self.run_on_workdays(time, callback)
self.run_on_weekends(time, callback)
self.run_daily(callback)
self.run_hourly(callback)
self.run_minutely(callback)
self.run_at(date_time, callback)
self.run_in(seconds, callback)
self.run_soon(callback)


# get another rule by name
self.get_rule(rule_name)

Project details


Release history Release notifications | RSS feed

This version

0.3.9

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

HABApp-0.3.9.tar.gz (29.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

HABApp-0.3.9-py3-none-any.whl (45.2 kB view details)

Uploaded Python 3

File details

Details for the file HABApp-0.3.9.tar.gz.

File metadata

  • Download URL: HABApp-0.3.9.tar.gz
  • Upload date:
  • Size: 29.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.16.5 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.2

File hashes

Hashes for HABApp-0.3.9.tar.gz
Algorithm Hash digest
SHA256 30ba648e4a5769d95766ed4f652f2008b4c6905cb93826521967cc63903721db
MD5 0ad2643b4227ae165735e0c7414e7727
BLAKE2b-256 f42c62c95201730f02efdb11b73bace708b3c115677a1c84154c861be70e2945

See more details on using hashes here.

File details

Details for the file HABApp-0.3.9-py3-none-any.whl.

File metadata

  • Download URL: HABApp-0.3.9-py3-none-any.whl
  • Upload date:
  • Size: 45.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.16.5 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.2

File hashes

Hashes for HABApp-0.3.9-py3-none-any.whl
Algorithm Hash digest
SHA256 7d8e89d238e41b751a095979aac95cb6e2ffce8d5a3f2769888c8d0c8c748094
MD5 1229ca37a9bf15b06ef100febd3fd258
BLAKE2b-256 38dbdbcf30260cd876bdf288b74a743a6297ef3a0fe992e8b5bfaa5adbd81a35

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page