HABApp 0.30

Good news everyone!

I just released HABApp 0.30.0!

This is a really huge release which brings many improvements (and probably new bugs :wink: ).
Most notably is the new scheduler with a new countdown option which simplifies many rules!

Example of a rule that will turn the light on and off based on two movement sensors:

import HABApp
from HABApp.core.items import Item
from HABApp.core.events import ValueUpdateEvent

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

        self.countdown = self.run.countdown(30, self.switch_off)
        self.device = Item.get_item('my_device')

        self.movement1 = Item.get_item('movement_sensor1')
        self.movement1.listen_event(self.movement, ValueUpdateEvent)

        self.movement2 = Item.get_item('movement_sensor2')
        self.movement2.listen_event(self.movement, ValueUpdateEvent)

    def movement(self, event: ValueUpdateEvent):
        if self.device != 'ON':
            self.device.post_value('ON')

        self.countdown.reset()

    def switch_off(self):
        self.device.post_value('OFF')

MyCountdownRule()

There is no need to cancel and reschedule - just call the reset() function and the countdown will start again.


:exclamation:Attention :exclamation:

  • 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 rules 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
  • Search for self.run_on_sun('sunrise' and replace with self.run.on_sunrise(
  • Search for self.run_on_sun('sunset' and replace with self.run.on_sunset(
2 Likes

Excited to try, but experiencing difficulty upgrading per the instructions, apparently related to pendulum. Had tried a system update through openhabian-configā€¦ trying to figure out how to determine installed version nowā€¦in case it actually worked (I donā€™t think it did).

(habapp) openhabian@pi-openhab3:/opt/habapp $ sudo python3 -m pip install --upgrade habapp
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting habapp
  Using cached https://files.pythonhosted.org/packages/1b/ea/3b08de44fc6a9e66dfa753b64dde66f7a1efae68de781854d2ffe6178171/HABApp-0.30.0-py3-none-any.whl
Collecting stackprinter==0.2.5 (from habapp)
  Using cached https://files.pythonhosted.org/packages/f3/f5/ff972ffa3b54afce3f48d36bf749e3048ee22473afa4489dd6c5c1be402a/stackprinter-0.2.5-py3-none-any.whl
Collecting pendulum==2.1.2 (from habapp)
  Using cached https://files.pythonhosted.org/packages/db/15/6e89ae7cde7907118769ed3d2481566d05b5fd362724025198bb95faf599/pendulum-2.1.2.tar.gz
  Installing build dependencies ... done
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/usr/lib/python3.7/tokenize.py", line 447, in open
        buffer = _builtin_open(filename, 'rb')
    FileNotFoundError: [Errno 2] No such file or directory: '/tmp/pip-install-t4wzuhgr/pendulum/setup.py'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-t4wzuhgr/pendulum/

Can you upgrade pip python -m pip install --upgrade pip and see if a new version of pip fixes your issue?

Neither this command nor python3 -m pip install --upgrade pip caused any changes

Have you executed it in your venv? I got a similar error but after I upgraded pip it was gone.
Or maybe it was because I installed an older version of pendulum first. Could you try that?

Yes, I ran all of these commands in the venv, per the instructionsā€¦

I just ran them outside of the venv and it completed successfully, but I donā€™t think that means it will work.

Should I reset the venv and then reinstall?

Yes - maybe recreating the venv solves the issue.

other than deleting the habapp folder in /opt, what else would I need to do to allow for a successful reinstallation from your docs or openhabian-config?

I just checked on a Pi3 with openhabian.

cd /opt/habapp/bin
sudo chmod +x . -R
./pip install --upgrade habapp

This raises the error message.
The following commands then install it successfully.

sudo ./python -m pip  install --upgrade pip
./pip install --upgrade habapp

Great, thanks for the fix above. I might try to clear everything out again and try again, because of the below situation, but I donā€™t think it will fix it?

I know this is not really a HABapp question, but Iā€™m finding the service runs different than the command line in the venv. Running the below command from the command line I see all my logging settings in frontail, but not if I run the service, also shown below:

(habapp) openhabian@pi-openhab3:/opt/habapp $ /opt/habapp/bin/habapp -c /etc/openhab/habapp
[Unit]
Description=HABApp
Documentation=https://habapp.readthedocs.io
After=openhab.service

[Service]
Type=simple
User=openhab
Group=openhabian
UMask=002
ExecStart=/opt/habapp/bin/habapp -c /etc/openhab/habapp

[Install]
WantedBy=multi-user.target

I clearly do not understand venv well enoughā€¦

Apologies for hijacking the thread. I really want to get this running consistently before I start really migrating rules.

the service status has this log entry that I think is related to the issue:

May 03 08:59:17 pi-openhab3 habapp[6739]: Error loading logging config: Unable to configure handler 'EventFile'

and that section of the logging.yaml file looks like this:

  EventFile:
    class: HABApp.core.lib.handler.MidnightRotatingFileHandler
    filename: '/var/log/openhab/HABApp_events.log'
    maxBytes: 1_048_576
    backupCount: 3
    formatter: HABApp_format
    level: DEBUG

Happy to buy some coffee or beer to support all this hard work.

This is definately a permission issue.
Can you try and see if ā€œFix Permissionsā€ (option 14 in openhabian-config) will fix your problems?

You should definitely treat yourself to a cold one after you have everything set up an running! :wink:

1 Like

Thatā€™s the ticket!

Permissions on the log files looked consistent, but I should have thought to apply that option. It should be step 1 in many troubleshooting situationsā€¦

Thanks! Ready to get rolling!

Hi Sebastian,

Thereā€™s either a bug/limitation importing lib modules or Iā€™m making a silly mistake.

Iā€™ve a file habapp_utils.py in my lib folder which I import into my rules with ā€œimport habapp_utilsā€ and ā€œfrom habapp_utils import *ā€. This works fine, however any changes I make into habapp_utils.py arenā€™t reflected when I run my rules unless I first restart habapp.

Is it intentional, a bug, or am I doing something dumb?

thanks!

Dan

Oh i think i know it, me, me, me me please

As far as i understand. HabApp reconises your change, but the already loaded rules do not. There is a specific dependency option. With this option you could say reload my rule if file xyz changes.

See the documentation on this:File-Properties

Thomas

@Dibbler42 While file properties are really nice they donā€™t apply here

@dan12345
Itā€™s a library folder that gets imported on startup.
Currently there is no proper method in python to dynamically reload libraries, thatā€™s why changes to the libraries donā€™t get picked up during runtime.

And to be fair most of the time this is not necessary:

  • HABApp can build rules dynamically based on parameter files and reload the rule files on parameter change (see the link from Thomas).
  • You also can run functions from rules from different files.
  • As a third option you can create HABApp internal items and use these to transfer data between rules/files.
  • Overlaying states and a device driver/logic model can be created with the MultiModeIItem

With these mechanisms creating libraries in the original sense of library is not necessary any more.
And when it makes sense itā€™s so static that restarts donā€™t matter :wink:

Ohhhh so much to learn ā€¦

Itā€™s interesting how different people have different approaches. I like building a big library with all the utility functions, and then having short simple rules files. But restarting when the library changes isnā€™t a hardship!

For me thatā€™s HABApp :wink:

I used to try that too, but I changed that to create a rule class that does exactly one thing and Iā€™m rather creating multiple instances of the rule than try working with a big library.
For example I now have six rules per rollershutter and they are combined through the multimode item.

If you like you can create a thread and Iā€™ll have a look at your things and give you an example how I would have done it. If not and youā€™re happy restarting HABApp thatā€™s fine with me, too! :slight_smile:
Whatever suits you the best!

Just found an alternative:

import importlib
importlib.reload(habapp_utils)

Forces a reload of the library module. Cool!