Help needed with syntax

Hi,

i’m trying to learn python and AppDeamon but i have hit a wall.
For learning purposes i removed all the fancy bits from the motion light exemple and got this:

import appdaemon.plugins.hass.hassapi as hass

class GarageRorelsevakt(hass.Hass):

def initialize(self):

 self.handle = None
 self.listen_state(self.motion,"switch.garage_rorelsevakt")

def motion(self, entity, attribute, old, new, kwargs):
if new == "on":
  self.turn_on("switch.garage_belysning")
  delay = "120"
  self.cancel_timer(self.handle)
  self.handle = self.run_in(self.light_off, delay)

def light_off(self, kwargs):
self.turn_off("switch.garage_belysning")

def cancel(self):
self.cancel_timer(self.handle)

I get what it does but not how.

For this to work:

  self.cancel_timer(self.handle)

Shouldn’t

self.listen_state(self.motion,"switch.garage_rorelsevakt")

Be

handle = self.listen_state(self.motion,"switch.garage_rorelsevakt")

And to be honest i don’t really get this either :confused:

self.handle = self.run_in(self.light_off, delay)

Any help is mostly appreciated.

I think this is the source of your confusion. self.run_in starts a timer, and returns a handle stored in a variable self.handle that is used to cancel it.

    self.cancel_timer(self.handle)

cancels the timer using the handle returned by self.run_in

1 Like