Thanks @Matze0211 - I did check out the API to make sure it was exposed and possible, and know I could write some code to do it, but just wondered if there was a more graceful way to do in HABApp before doing so…
I already have a thing monitoring rule using HABApp so it seems like I should be able to change the state against the already referenced Thing. Agreed, not a big deal to do it with ‘requests’ as part of the code, but want to keep as simple as possible, especially as it is not that annoying of an issue.
Working with HABApp is inspiring me to write cleaner, more re-usable code, where I am typically just a ‘make it work’ coder (it is not my profession). Maybe some day I’ll be able to contribute some code to such a project…
@alfista2600
You can always contribute to the documentation!
It’s the most important part of a project but it’s often overlooked because it’s cumbersome to create and hard keeping it up to date.
This is great, and earlier in the week I decided I’d try to document the feature but you beat me to it.
I’ve never been a technical writer but if there are areas I can flesh out, I’ll do my best.
Meanwhile, I’m happy to offer this sample code for review and possible inclusion as an example. I’m sure you can whittle it down to 7 or 8 lines…lol
I just finished it and can see dozens of improvement opportunities, but it solves a nice little problem for me as is (I think). Also never sure of which code fences to use…
# Some other stuff
#
# HABApp:
# reloads on:
# - params/disableable_things.yml
import HABApp
from HABApp.openhab.items import Thing
from HABApp.core.events import ValueChangeEventFilter
import logging
class ThingDisabler(HABApp.Rule):
def __init__(self, item, parameters):
super().__init__()
self.item = item
self.parameters = parameters
self.things = self.parameters['definition']['things']
self.listen_event(self.item, self.item_toggle, ValueChangeEventFilter())
self.light_timer = self.run.countdown(parameters['definition']['startup_time'], self.things_online)
def item_toggle(self, event: ValueChangeEventFilter):
if event.value == 'ON':
# give device time to start up before enabling thing
log.warning('Setting Startup Timer for Things Controlled By ' + self.item)
self.light_timer.reset()
else:
for thing in self.things:
Thing.get_item(thing).set_enabled(enable=False)
log.warning('Disabling: ' + thing)
def things_online(self):
for thing in self.things:
Thing.get_item(thing).set_enabled(enable=True)
log.warning('Enabling: ' + thing)
log = logging.getLogger('MyRules')
cfg = HABApp.DictParameter('disableable_things') # this will get the file content
for d, p in cfg.items():
enabled = p['definition'].get('enabled', False)
if enabled:
log.warning('Creating listener to enable/disable things based on ' + d)
ThingDisabler(d, p)
Why would you do this? This would return the json representation of the value as a string.
The check in the next line is always true since you are checking the bool value of the string …
The json part has to removed.
my bad - as I was cleaning it up, that area had a json.loads and json.dumps that worked but was a remnant of me being a bad coder (still am)… I started trying to simplify but forgot to finish/test. Editing original post now…