daily pastebin goal
62%
SHARE
TWEET

Untitled

a guest Mar 24th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import appdaemon.plugins.hass.hassapi as hass
  2. import time
  3.  
  4. class Clicks(hass.Hass):
  5.     def initialize(self):
  6.         self.entities = type("entities", (object,), self.args["entities"])
  7.         self.config = type("config", (object,), self.args["config"])
  8.        
  9.         # Lights to flash with the doorbell or other notifications
  10.         self.notification_lights = {
  11.             self.entities.dale_light,
  12.             self.entities.kumara_light,
  13.             self.entities.back_light,
  14.             self.entities.hall_light
  15.         }
  16.        
  17.         # When these sensors turn On, call the associated handler
  18.         self.state_off_to_on_actions = {
  19.             self.entities.lounge_motion: self.living_light_on_motion,
  20.             self.entities.back_motion: self.back_light_on_motion,
  21.             self.entities.front_motion: self.front_light_on_motion
  22.         }
  23.         # When these sensors turn Off, call the associated handler
  24.         self.state_on_to_off_actions = {
  25.         }
  26.  
  27.         # When these sensors update an attribute while turned off, call the associated handler
  28.         self.state_off_to_off_actions = {
  29.             self.entities.lounge_motion: self.living_light_off_motion,
  30.             self.entities.back_motion: self.back_light_off_motion,
  31.             self.entities.front_motion: self.front_light_off_motion
  32.         }
  33.        
  34.         # When these sensors send temperature updates, call the associated handler
  35.         self.temperature_actions = {}
  36.         self.temperature_actions[self.entities.dale_temperature] = self.dale_light_update_colortemp
  37.        
  38.         # When these xiaomi_aqara buttons raise a single click event, call the associated handler
  39.         self.click_actions = {
  40.             self.entities.dale_desk: self.dale_light_toggle,
  41.             self.entities.dale_door: self.dale_light_toggle,
  42.             self.entities.hall_switch_left: self.living_light_toggle,
  43.             self.entities.hall_switch_right: self.hall_light_toggle,
  44.             self.entities.kitchen_switch_left: self.kitchen_light_toggle,
  45.             self.entities.kitchen_switch_right: self.lounge_light_toggle,
  46.             self.entities.front_switch_left: self.living_light_toggle,
  47.             self.entities.front_switch_right: self.front_light_toggle,
  48.             self.entities.back_switch: self.back_light_toggle,
  49.             self.entities.kumara_switch1: self.kumara_light_toggle,
  50.             self.entities.kumara_switch2: self.kumara_light_toggle,
  51.             self.entities.doorbell: self.doorbell_ring
  52.         }
  53.         # When these xiaomi_aqara buttons raise a double click event, call the associated handler
  54.         self.doubleclick_actions = {
  55.             self.entities.dale_desk: self.dale_light_on_dim,
  56.             self.entities.dale_door: self.dale_light_on_dim,
  57.             self.entities.kumara_switch1: self.kumara_light_on_dim,
  58.             self.entities.kumara_switch2: self.kumara_light_on_dim
  59.         }
  60.         # When these xiaomi_aqara buttons raise a long click event, call the associated handler
  61.         self.longclick_actions = {
  62.             self.entities.dale_desk: self.dale_light_on,
  63.             self.entities.dale_door: self.dale_light_on,
  64.             self.entities.kumara_switch1: self.kumara_light_on,
  65.             self.entities.kumara_switch2: self.kumara_light_on
  66.         }
  67.         # When these dual xiaomi_aqara buttons raise a both click event, call the associated handler
  68.         self.bothclick_actions = {
  69.             self.entities.hall_switch_both: [self.hall_light_toggle, self.living_light_toggle],
  70.             self.entities.kitchen_switch_both: [self.living_light_toggle],
  71.             self.entities.front_switch_both: [self.living_light_toggle, self.front_light_toggle]
  72.         }
  73.        
  74.         # Track event handlers for cleanup on shutdown
  75.         self.listen_events = []
  76.         self.listen_states = []
  77.         self.listen_events.append(self.listen_event(self.event_detected))
  78.         self.listen_states.append(self.listen_state(self.state_change))
  79.  
  80.         # Flags to indicate a light has been triggered by a soft action (motion sense) rather than manually turned on
  81.         self.back_light_turned_on_by_motion = 0
  82.         self.front_light_turned_on_by_motion = 0
  83.         self.living_light_turned_on_by_motion = 0
  84.        
  85.         self.call_service("light/turn_on", entity_id=self.entities.gateway_light, brightness=20, rgb_color=[255,220,255])
  86.  
  87.     # Play doorbell sound and flash all notification_lights
  88.     def doorbell_ring(self):
  89.         prev_brightness = {}
  90.         prev_state = {}
  91.         for light in self.notification_lights:
  92.             state = self.get_state(light, attribute="all")
  93.             prev_state[light] = state["state"]
  94.             if(prev_state[light] == "off"):
  95.                 self.call_service("light/turn_on", entity_id=light, transition=0)
  96.                 time.sleep(0.1)
  97.                 state = self.get_state(light, attribute="all")
  98.                 prev_brightness[light] = state["attributes"]["brightness"]
  99.             else:
  100.                 prev_brightness[light] = state["attributes"]["brightness"]
  101.             self.call_service("light/turn_on", entity_id=light, brightness=255, transition=0)
  102.        
  103.         for x in [1,2]:
  104.             self.call_service("light/toggle", entity_id="light.notification_lights", transition=0)
  105.  
  106.         for light in self.notification_lights:
  107.             if(prev_state[light] == "off"):
  108.                 self.call_service("light/turn_on", entity_id=light, brightness=prev_brightness[light], transition=0)
  109.                 self.call_service("light/turn_off", entity_id=light)
  110.             else:
  111.                 self.call_service("light/turn_on", entity_id=light, brightness=prev_brightness[light], transition=0)
  112.    
  113.     def lounge_light_on(self):
  114.         self.living_light_turned_on_by_motion = 0
  115.         self.call_service("light/turn_on", entity_id=self.entities.lounge_light, brightness=255)
  116.     def lounge_light_off(self):
  117.         self.call_service("light/turn_off", entity_id=self.entities.lounge_light)
  118.     def lounge_light_toggle(self):
  119.         self.call_service("light/toggle", entity_id=self.entities.lounge_light)
  120.    
  121.     def kitchen_light_on(self):
  122.         self.living_light_turned_on_by_motion = 0
  123.         self.call_service("light/turn_on", entity_id=self.entities.kitchen_light, brightness=255)
  124.     def kitchen_light_off(self):
  125.         self.call_service("light/turn_off", entity_id=self.entities.kitchen_light)
  126.     def kitchen_light_toggle(self):
  127.         self.call_service("light/toggle", entity_id=self.entities.kitchen_light)
  128.        
  129.     def hall_light_on(self):
  130.         self.call_service("light/turn_on", entity_id=self.entities.hall_light, brightness=255)
  131.     def hall_light_off(self):
  132.         self.call_service("light/turn_off", entity_id=self.entities.hall_light)
  133.     def hall_light_toggle(self):
  134.         self.call_service("light/toggle", entity_id=self.entities.hall_light)
  135.  
  136.     def living_light_on(self):
  137.         self.living_light_turned_on_by_motion = 0
  138.         self.call_service("light/turn_on", entity_id=self.entities.lounge_light, brightness=255)
  139.         self.call_service("light/turn_on", entity_id=self.entities.kitchen_light, brightness=255)
  140.     def living_light_on_motion(self):
  141.         #if(float(self.get_state(self.entities.lounge_illumination)) < 300):
  142.         if(self.get_state("sun.sun") == "below_horizon"):
  143.             if(self.get_state(self.entities.kitchen_light) == "off"):
  144.                 self.living_light_turned_on_by_motion = 1
  145.             self.call_service("light/turn_on", entity_id=self.entities.lounge_light, brightness=255)
  146.             self.call_service("light/turn_on", entity_id=self.entities.kitchen_light, brightness=255)
  147.     def living_light_off(self):
  148.         self.call_service("light/turn_off", entity_id=self.entities.lounge_light)
  149.         self.call_service("light/turn_off", entity_id=self.entities.kitchen_light)
  150.         self.call_service("light/turn_off", entity_id=self.entities.front_light)
  151.         self.call_service("light/turn_off", entity_id=self.entities.back_light)
  152.     def living_light_off_motion(self):
  153.         if(self.living_light_turned_on_by_motion):
  154.             self.living_light_turned_on_by_motion = 0
  155.             if(int(self.get_state(self.entities.lounge_motion, attribute="all")["attributes"]["No motion since"]) >= self.config.lounge_motion_on_time * 60):
  156.                 self.call_service("light/turn_off", entity_id=self.entities.lounge_light)
  157.                 self.call_service("light/turn_off", entity_id=self.entities.kitchen_light)
  158.     def living_light_toggle(self):
  159.         if(self.get_state(self.entities.kitchen_light) == "on"):
  160.             self.living_light_off()
  161.         else:
  162.             self.living_light_on()
  163.  
  164.     def back_light_on(self):
  165.         self.back_light_turned_on_by_motion = 0
  166.         self.call_service("light/turn_on", entity_id=self.entities.back_light, brightness=255)
  167.     def back_light_on_motion(self):
  168.         if(self.get_state("sun.sun") == "below_horizon"):
  169.             if(self.get_state(self.entities.back_light) == "off"):
  170.                 self.back_light_turned_on_by_motion = 1
  171.                 self.call_service("light/turn_on", entity_id=self.entities.back_light, brightness=255)
  172.     def back_light_off(self):
  173.         self.call_service("light/turn_off", entity_id=self.entities.back_light)
  174.     def back_light_off_motion(self):
  175.         if(self.back_light_turned_on_by_motion):
  176.             self.back_light_turned_on_by_motion = 0
  177.             if(int(self.get_state(self.entities.back_motion, attribute="all")["attributes"]["No motion since"]) >= self.config.back_motion_on_time * 60):
  178.                 self.call_service("light/turn_off", entity_id=self.entities.back_light)
  179.     def back_light_toggle(self):
  180.         if(self.get_state(self.entities.back_light) == "on"):
  181.             self.back_light_off()
  182.         else:
  183.             self.back_light_on()
  184.  
  185.     def front_light_on(self):
  186.         self.front_light_turned_on_by_motion = 0
  187.         self.call_service("light/turn_on", entity_id=self.entities.front_light, brightness=255)
  188.     def front_light_on_motion(self):
  189.         if(self.get_state("sun.sun") == "below_horizon"):
  190.             if(self.get_state(self.entities.front_light) == "off"):
  191.                 self.front_light_turned_on_by_motion = 1
  192.                 self.call_service("light/turn_on", entity_id=self.entities.front_light, brightness=255)
  193.     def front_light_off(self):
  194.         self.call_service("light/turn_off", entity_id=self.entities.front_light)
  195.     def front_light_off_motion(self):
  196.         if(self.front_light_turned_on_by_motion):
  197.             self.front_light_turned_on_by_motion = 0
  198.             if(int(self.get_state(self.entities.front_motion, attribute="all")["attributes"]["No motion since"]) >= self.config.front_motion_on_time * 60):
  199.                 self.call_service("light/turn_off", entity_id=self.entities.front_light)
  200.     def front_light_toggle(self):
  201.         if(self.get_state(self.entities.front_light) == "on"):
  202.             self.front_light_off()
  203.         else:
  204.             self.front_light_on()
  205.  
  206.     def kumara_light_toggle(self):
  207.         self.call_service("light/toggle", entity_id=self.entities.kumara_light)
  208.     def kumara_light_on_dim(self):
  209.         self.call_service("light/turn_on", entity_id=self.entities.kumara_light, brightness=1)
  210.     def kumara_light_on(self):
  211.         self.call_service("light/turn_on", entity_id=self.entities.kumara_light, brightness=255)
  212.     def kumara_light_off(self):
  213.         self.call_service("light/turn_off", entity_id=self.entities.kumara_light)
  214.  
  215.     def dale_light_toggle(self):
  216.         self.call_service("light/toggle", entity_id=self.entities.dale_light)
  217.     def dale_light_on_dim(self):
  218.         self.call_service("light/turn_on", entity_id=self.entities.dale_light, brightness=1)
  219.     def dale_light_on(self):
  220.         sense = self.get_state(self.entities.lounge_motion, attribute="all")
  221.         self.log(str(sense))
  222.         self.call_service("light/turn_on", entity_id=self.entities.dale_light, brightness=255)
  223.     def dale_light_off(self):
  224.         self.call_service("light/turn_off", entity_id=self.entities.dale_light)
  225.  
  226.     def dale_light_update_colortemp(self, temp):
  227.         if(self.get_state(self.entities.dale_light) == "on"):
  228.             kelv = int(6500 - ((float(temp) - 10)*100))
  229.             self.call_service("light/turn_on", entity_id=self.entities.dale_light, kelvin=kelv)
  230.  
  231.     def day_night_change(self):
  232.         if(self.get_state("sun.sun") == "above_horizon"):
  233.             self.back_light_forced_on = 0
  234.             self.front_light_forced_on = 0
  235.             if(self.get_state(self.entities.gateway_light) == "on"):
  236.                 self.call_service("light/turn_off", entity_id=self.entities.gateway_light)
  237.         else:
  238.             if(self.get_state(self.entities.gateway_light) == "off"):
  239.                 self.call_service("light/turn_on", entity_id=self.entities.gateway_light, brightness=30, rgb_color=[255,230,205])            
  240.  
  241.     def state_change(self, entity, attribute, old, new, kwargs):
  242.         if(attribute == "state" and old == "off" and new == "on"):
  243.             func = self.state_off_to_on_actions.get(entity, lambda: "none")
  244.             func()
  245.  
  246.         if(attribute == "state" and old == "on" and new == "off"):
  247.             func = self.state_on_to_off_actions.get(entity, lambda: "none")
  248.             func()
  249.            
  250.         if(attribute == "state" and old == "off" and new == "off"):
  251.             func = self.state_off_to_off_actions.get(entity, lambda: "none")
  252.             func()
  253.        
  254.         if(attribute == "state" and entity == "sun.sun"):
  255.             self.day_night_change()
  256.        
  257.         func = self.temperature_actions.get(entity, lambda temp: "none")
  258.         func(new)
  259.    
  260.     def event_detected(self, event_name, data, kwargs):
  261.         if(event_name == "xiaomi_aqara.click"):
  262.             if(data["click_type"] == "click" or data["click_type"] == "single"):
  263.                 func = self.click_actions.get(data["entity_id"], lambda: "none")
  264.                 func()
  265.             if(data["click_type"] == "both"):
  266.                 func = self.bothclick_actions.get(data["entity_id"], lambda: "none")
  267.                 for f in func:
  268.                     f()
  269.             if(data["click_type"] == "double"):
  270.                 func = self.doubleclick_actions.get(data["entity_id"], lambda: "none")
  271.                 func()
  272.             if(data["click_type"] == "long_click_press"):
  273.                 func = self.longclick_actions.get(data["entity_id"], lambda: "none")
  274.                 func()
  275.        
  276.     def terminate(self):
  277.         for handle in self.listen_events:
  278.             self.cancel_listen_event(handle)
  279.         for handle in self.listen_states:
  280.             self.cancel_listen_state(handle)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top