Skip to main content

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

Project description

HABApp

Tests Status Documentation Status Updates PyPI - Python Version

PyPI Downloads Docker Image Version (latest by date) Docker Pulls

Easy automation with MQTT and/or openHAB

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

Documentation

The documentation can be found at here

Examples

MQTT Rule example

import datetime
import random

import HABApp
from HABApp.core.events import ValueUpdateEvent


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

        self.run.every(
            time=datetime.timedelta(seconds=60),
            interval=datetime.timedelta(seconds=30),
            callback=self.publish_rand_value
        )

        self.listen_event('test/test', self.topic_updated, ValueUpdateEvent)

    def publish_rand_value(self):
        print('test mqtt_publish')
        self.mqtt.publish('test/test', str(random.randint(0, 1000)))

    def topic_updated(self, event):
        assert isinstance(event, ValueUpdateEvent), type(event)
        print( f'mqtt topic "test/test" updated to {event.value}')


ExampleMqttTestRule()

Openhab rule example

import HABApp
from HABApp.core.events import ValueUpdateEvent, ValueChangeEvent
from HABApp.openhab.events import ItemStateEvent, ItemCommandEvent, ItemStateChangedEvent

class MyOpenhabRule(HABApp.Rule):

    def __init__(self):
        super().__init__()

        # Trigger on item updates
        self.listen_event( 'TestContact', self.item_state_update, ItemStateEvent)
        self.listen_event( 'TestDateTime', self.item_state_update, ValueUpdateEvent)

        # Trigger on item changes
        self.listen_event( 'TestDateTime', self.item_state_change, ItemStateChangedEvent)
        self.listen_event( 'TestSwitch', self.item_state_change, ValueChangeEvent)

        # Trigger on item commands
        self.listen_event( 'TestSwitch', self.item_command, ItemCommandEvent)

    def item_state_update(self, event):
        assert isinstance(event, ValueUpdateEvent)
        print( f'{event}')

    def item_state_change(self, event):
        assert isinstance(event, ValueChangeEvent)
        print( f'{event}')

        # interaction is available through self.openhab or self.oh
        self.openhab.send_command('TestItemCommand', 'ON')

    def item_command(self, event):
        assert isinstance(event, ItemCommandEvent)
        print( f'{event}')

        # interaction is available through self.openhab or self.oh
        self.oh.post_update('TestItemUpdate', 123)

MyOpenhabRule()

Changelog

0.31.0 (08.10.2021)

  • added self.get_items to easily search for items in a rule
  • added full support for tags and groups on OpenhabItem
  • Application should now properly shut down when there is a PermissionError
  • Added DatetimeItem to docs
  • Label in commandOption is optional
  • Added message when file is removed
  • Examples in the docs get checked with a newly created sphinx extension
  • Reworked the openhab tests

0.30.3 (17.06.2021)

  • add support for custom ca cert for MQTT
  • Scheduler runs only when the rule file has been loaded properly
  • Sync openhab calls raise an error when called from an async context
  • Replaced thread check for asyncio with a contextvar (internal)

0.30.3 (01.06.2021)

  • Scheduler runs only when the rule file has been loaded properly
  • Replaced thread check for asyncio with a contextvar
  • Sync openhab calls raise an error when called from an async context

0.30.2 (26.05.2021)

  • Item and Thing loading from openHAB is more robust and disconnects now properly if openHAB is only partly ready
  • Renamed command line argument "-s" to "-wos" or "--wait_os_uptime"
  • Updated dependencies

0.30.1 (07.05.2021)

  • latitude is now set correctly for sunrise/sunset calculation (closes #217)
  • Added missing " for tags in textual thing configuration
  • Updated scheduler which fixes an overflow error(#216)
  • States of openHAB groups are now unpacked correctly

0.30.0 (02.05.2021)

Attention:

  • No more support for python 3.6!
  • Migration of rules is needed!

Changelog

  • Switched to Apache2.0 License
  • Fix DateTime string parsing for OH 3.1 (#214)
  • State of Groupitem gets set correctly
  • About ~50% performance increase for async calls in rules
  • Significantly less CPU usage when no functions are running
  • Completely reworked the file handling (loading and dependency resolution)
  • Completely reworked the Scheduler!
    • Has now subsecond accuracity (finally!)
    • Has a new .coundown() job which can simplify many rules. It is made for functions that do something after a certain period of time (e.g. switch a light off after movement)
  • Added hsb_to_rgb, rgb_to_hsb functions which can be used in rules
  • Better error message if configured foldes overlap with HABApp folders
  • Renamed HABAppError to HABAppException
  • Some Doc improvements

Migration of rules:

  • Search for self.run_ and replace with self.run.
  • Search for self.run.in and replace with self.run.at
  • Search for .get_next_call() and replace with .get_next_run() (But make sure it's a scheduled job)
  • Search for HABAppError and replace with HABAppException

0.20.2 (07.04.2021)

  • Added HABApp.util.functions with min/max
  • Reworked small parts of the file watcher
  • Doc improvements
  • Dependency updates

Project details


Release history Release notifications | RSS feed

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.31.0.tar.gz (100.2 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.31.0-py3-none-any.whl (150.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: HABApp-0.31.0.tar.gz
  • Upload date:
  • Size: 100.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for HABApp-0.31.0.tar.gz
Algorithm Hash digest
SHA256 8fb1210e9642f8aebef3d83950164500d8cc00c1f8ce24b30cafaf9bd4c80f84
MD5 3e5520ebf833ce3f53ca0943b34cf10b
BLAKE2b-256 e4a3f03baf6fa1a64c46b18ab93141a3a2a2c16120399a7317c62c26d16c19e5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: HABApp-0.31.0-py3-none-any.whl
  • Upload date:
  • Size: 150.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for HABApp-0.31.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0d501663e253db3a26bdca95a7e0b6aac9b4bec978a5f6f5f5c8258b0bfdb535
MD5 ffa3aa73f37b568ed2f070a8c7bc8107
BLAKE2b-256 cf702f72081a2f2301bda6d443d4580ab47eb2b01f5192dfbc39e0a27890ccef

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