AppDaemon / appdaemon

:page_facing_up: Python Apps for Home Automation
Other
851 stars 418 forks source link

Make state callbacks filter out value #674

Closed sriramsv closed 5 years ago

sriramsv commented 5 years ago

@acockburn @ReneTode Currently, the listen_state is defined as

self.listen_state(callback, entity) 

Want the method to take in a value parameter, where a callback can be directly called when the value is the specified value. Something like

self.listen_state(callback,entity,target_value)

This will make code more loosely coupled than existing ways as currently the old way to that is

import appdaemon.plugins.hass.hassapi as hass

class Test(hass.Hass):

    def initialize(self):
        self.sensor = self.args.get("sensor")
        self.listen_state(self.state_change, self.sensor)

    def state_change(self, entity, attribute, old, new, kwargs):
        if new == 'slide':
            self.slide()
        if new == "flip":
            self.flip()

    def slide(self):
        self.log("slided")

    def flip(self):
        self.log("flip")

It will be much simpler if we could have something like

import appdaemon.plugins.hass.hassapi as hass

class Test(hass.Hass):

    def initialize(self):
        self.sensor = self.args.get("sensor")
        self.listen_state(self.slide, self.sensor,"slide")
        self.listen_state(self.flip,self.sensor,"flip")

    def slide(self,kwargs):
        self.log("slided")

    def flip(self,kwargs):
        self.log("flip")
ReneTode commented 5 years ago

mabe you should read the docs a bit more.

import appdaemon.plugins.hass.hassapi as hass

class Test(hass.Hass):

    def initialize(self):
        self.sensor = self.args.get("sensor")
        self.listen_state(self.slide, self.sensor,new="slide")
        self.listen_state(self.flip,self.sensor,new="flip")

    def slide(self,kwargs):
        self.log("slided")

    def flip(self,kwargs):
        self.log("flip")

does exactly what you want ;)

sriramsv commented 5 years ago

Wow!, I have gone through the doc so many times, didn't catch my eye. :+1: Thanks a lot @ReneTode

ReneTode commented 5 years ago

@sriramsv check out all different options on listen_state: https://appdaemon.readthedocs.io/en/latest/AD_API_REFERENCE.html#listen-state attribute, new, old, duration, immediate and oneshot.

you can do a lot with them.