Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import appdaemon.plugins.hass.hassapi as hass
- import time
- class Clicks(hass.Hass):
- def initialize(self):
- self.entities = type("entities", (object,), self.args["entities"])
- self.config = type("config", (object,), self.args["config"])
- # Lights to flash with the doorbell or other notifications
- self.notification_lights = {
- self.entities.dale_light,
- self.entities.kumara_light,
- self.entities.back_light,
- self.entities.hall_light
- }
- # When these sensors turn On, call the associated handler
- self.state_off_to_on_actions = {
- self.entities.lounge_motion: self.living_light_on_motion,
- self.entities.back_motion: self.back_light_on_motion,
- self.entities.front_motion: self.front_light_on_motion
- }
- # When these sensors turn Off, call the associated handler
- self.state_on_to_off_actions = {
- }
- # When these sensors update an attribute while turned off, call the associated handler
- self.state_off_to_off_actions = {
- self.entities.lounge_motion: self.living_light_off_motion,
- self.entities.back_motion: self.back_light_off_motion,
- self.entities.front_motion: self.front_light_off_motion
- }
- # When these sensors send temperature updates, call the associated handler
- self.temperature_actions = {}
- self.temperature_actions[self.entities.dale_temperature] = self.dale_light_update_colortemp
- # When these xiaomi_aqara buttons raise a single click event, call the associated handler
- self.click_actions = {
- self.entities.dale_desk: self.dale_light_toggle,
- self.entities.dale_door: self.dale_light_toggle,
- self.entities.hall_switch_left: self.living_light_toggle,
- self.entities.hall_switch_right: self.hall_light_toggle,
- self.entities.kitchen_switch_left: self.kitchen_light_toggle,
- self.entities.kitchen_switch_right: self.lounge_light_toggle,
- self.entities.front_switch_left: self.living_light_toggle,
- self.entities.front_switch_right: self.front_light_toggle,
- self.entities.back_switch: self.back_light_toggle,
- self.entities.kumara_switch1: self.kumara_light_toggle,
- self.entities.kumara_switch2: self.kumara_light_toggle,
- self.entities.doorbell: self.doorbell_ring
- }
- # When these xiaomi_aqara buttons raise a double click event, call the associated handler
- self.doubleclick_actions = {
- self.entities.dale_desk: self.dale_light_on_dim,
- self.entities.dale_door: self.dale_light_on_dim,
- self.entities.kumara_switch1: self.kumara_light_on_dim,
- self.entities.kumara_switch2: self.kumara_light_on_dim
- }
- # When these xiaomi_aqara buttons raise a long click event, call the associated handler
- self.longclick_actions = {
- self.entities.dale_desk: self.dale_light_on,
- self.entities.dale_door: self.dale_light_on,
- self.entities.kumara_switch1: self.kumara_light_on,
- self.entities.kumara_switch2: self.kumara_light_on
- }
- # When these dual xiaomi_aqara buttons raise a both click event, call the associated handler
- self.bothclick_actions = {
- self.entities.hall_switch_both: [self.hall_light_toggle, self.living_light_toggle],
- self.entities.kitchen_switch_both: [self.living_light_toggle],
- self.entities.front_switch_both: [self.living_light_toggle, self.front_light_toggle]
- }
- # Track event handlers for cleanup on shutdown
- self.listen_events = []
- self.listen_states = []
- self.listen_events.append(self.listen_event(self.event_detected))
- self.listen_states.append(self.listen_state(self.state_change))
- # Flags to indicate a light has been triggered by a soft action (motion sense) rather than manually turned on
- self.back_light_turned_on_by_motion = 0
- self.front_light_turned_on_by_motion = 0
- self.living_light_turned_on_by_motion = 0
- self.call_service("light/turn_on", entity_id=self.entities.gateway_light, brightness=20, rgb_color=[255,220,255])
- # Play doorbell sound and flash all notification_lights
- def doorbell_ring(self):
- prev_brightness = {}
- prev_state = {}
- for light in self.notification_lights:
- state = self.get_state(light, attribute="all")
- prev_state[light] = state["state"]
- if(prev_state[light] == "off"):
- self.call_service("light/turn_on", entity_id=light, transition=0)
- time.sleep(0.1)
- state = self.get_state(light, attribute="all")
- prev_brightness[light] = state["attributes"]["brightness"]
- else:
- prev_brightness[light] = state["attributes"]["brightness"]
- self.call_service("light/turn_on", entity_id=light, brightness=255, transition=0)
- for x in [1,2]:
- self.call_service("light/toggle", entity_id="light.notification_lights", transition=0)
- for light in self.notification_lights:
- if(prev_state[light] == "off"):
- self.call_service("light/turn_on", entity_id=light, brightness=prev_brightness[light], transition=0)
- self.call_service("light/turn_off", entity_id=light)
- else:
- self.call_service("light/turn_on", entity_id=light, brightness=prev_brightness[light], transition=0)
- def lounge_light_on(self):
- self.living_light_turned_on_by_motion = 0
- self.call_service("light/turn_on", entity_id=self.entities.lounge_light, brightness=255)
- def lounge_light_off(self):
- self.call_service("light/turn_off", entity_id=self.entities.lounge_light)
- def lounge_light_toggle(self):
- self.call_service("light/toggle", entity_id=self.entities.lounge_light)
- def kitchen_light_on(self):
- self.living_light_turned_on_by_motion = 0
- self.call_service("light/turn_on", entity_id=self.entities.kitchen_light, brightness=255)
- def kitchen_light_off(self):
- self.call_service("light/turn_off", entity_id=self.entities.kitchen_light)
- def kitchen_light_toggle(self):
- self.call_service("light/toggle", entity_id=self.entities.kitchen_light)
- def hall_light_on(self):
- self.call_service("light/turn_on", entity_id=self.entities.hall_light, brightness=255)
- def hall_light_off(self):
- self.call_service("light/turn_off", entity_id=self.entities.hall_light)
- def hall_light_toggle(self):
- self.call_service("light/toggle", entity_id=self.entities.hall_light)
- def living_light_on(self):
- self.living_light_turned_on_by_motion = 0
- self.call_service("light/turn_on", entity_id=self.entities.lounge_light, brightness=255)
- self.call_service("light/turn_on", entity_id=self.entities.kitchen_light, brightness=255)
- def living_light_on_motion(self):
- #if(float(self.get_state(self.entities.lounge_illumination)) < 300):
- if(self.get_state("sun.sun") == "below_horizon"):
- if(self.get_state(self.entities.kitchen_light) == "off"):
- self.living_light_turned_on_by_motion = 1
- self.call_service("light/turn_on", entity_id=self.entities.lounge_light, brightness=255)
- self.call_service("light/turn_on", entity_id=self.entities.kitchen_light, brightness=255)
- def living_light_off(self):
- self.call_service("light/turn_off", entity_id=self.entities.lounge_light)
- self.call_service("light/turn_off", entity_id=self.entities.kitchen_light)
- self.call_service("light/turn_off", entity_id=self.entities.front_light)
- self.call_service("light/turn_off", entity_id=self.entities.back_light)
- def living_light_off_motion(self):
- if(self.living_light_turned_on_by_motion):
- self.living_light_turned_on_by_motion = 0
- if(int(self.get_state(self.entities.lounge_motion, attribute="all")["attributes"]["No motion since"]) >= self.config.lounge_motion_on_time * 60):
- self.call_service("light/turn_off", entity_id=self.entities.lounge_light)
- self.call_service("light/turn_off", entity_id=self.entities.kitchen_light)
- def living_light_toggle(self):
- if(self.get_state(self.entities.kitchen_light) == "on"):
- self.living_light_off()
- else:
- self.living_light_on()
- def back_light_on(self):
- self.back_light_turned_on_by_motion = 0
- self.call_service("light/turn_on", entity_id=self.entities.back_light, brightness=255)
- def back_light_on_motion(self):
- if(self.get_state("sun.sun") == "below_horizon"):
- if(self.get_state(self.entities.back_light) == "off"):
- self.back_light_turned_on_by_motion = 1
- self.call_service("light/turn_on", entity_id=self.entities.back_light, brightness=255)
- def back_light_off(self):
- self.call_service("light/turn_off", entity_id=self.entities.back_light)
- def back_light_off_motion(self):
- if(self.back_light_turned_on_by_motion):
- self.back_light_turned_on_by_motion = 0
- if(int(self.get_state(self.entities.back_motion, attribute="all")["attributes"]["No motion since"]) >= self.config.back_motion_on_time * 60):
- self.call_service("light/turn_off", entity_id=self.entities.back_light)
- def back_light_toggle(self):
- if(self.get_state(self.entities.back_light) == "on"):
- self.back_light_off()
- else:
- self.back_light_on()
- def front_light_on(self):
- self.front_light_turned_on_by_motion = 0
- self.call_service("light/turn_on", entity_id=self.entities.front_light, brightness=255)
- def front_light_on_motion(self):
- if(self.get_state("sun.sun") == "below_horizon"):
- if(self.get_state(self.entities.front_light) == "off"):
- self.front_light_turned_on_by_motion = 1
- self.call_service("light/turn_on", entity_id=self.entities.front_light, brightness=255)
- def front_light_off(self):
- self.call_service("light/turn_off", entity_id=self.entities.front_light)
- def front_light_off_motion(self):
- if(self.front_light_turned_on_by_motion):
- self.front_light_turned_on_by_motion = 0
- if(int(self.get_state(self.entities.front_motion, attribute="all")["attributes"]["No motion since"]) >= self.config.front_motion_on_time * 60):
- self.call_service("light/turn_off", entity_id=self.entities.front_light)
- def front_light_toggle(self):
- if(self.get_state(self.entities.front_light) == "on"):
- self.front_light_off()
- else:
- self.front_light_on()
- def kumara_light_toggle(self):
- self.call_service("light/toggle", entity_id=self.entities.kumara_light)
- def kumara_light_on_dim(self):
- self.call_service("light/turn_on", entity_id=self.entities.kumara_light, brightness=1)
- def kumara_light_on(self):
- self.call_service("light/turn_on", entity_id=self.entities.kumara_light, brightness=255)
- def kumara_light_off(self):
- self.call_service("light/turn_off", entity_id=self.entities.kumara_light)
- def dale_light_toggle(self):
- self.call_service("light/toggle", entity_id=self.entities.dale_light)
- def dale_light_on_dim(self):
- self.call_service("light/turn_on", entity_id=self.entities.dale_light, brightness=1)
- def dale_light_on(self):
- sense = self.get_state(self.entities.lounge_motion, attribute="all")
- self.log(str(sense))
- self.call_service("light/turn_on", entity_id=self.entities.dale_light, brightness=255)
- def dale_light_off(self):
- self.call_service("light/turn_off", entity_id=self.entities.dale_light)
- def dale_light_update_colortemp(self, temp):
- if(self.get_state(self.entities.dale_light) == "on"):
- kelv = int(6500 - ((float(temp) - 10)*100))
- self.call_service("light/turn_on", entity_id=self.entities.dale_light, kelvin=kelv)
- def day_night_change(self):
- if(self.get_state("sun.sun") == "above_horizon"):
- self.back_light_forced_on = 0
- self.front_light_forced_on = 0
- if(self.get_state(self.entities.gateway_light) == "on"):
- self.call_service("light/turn_off", entity_id=self.entities.gateway_light)
- else:
- if(self.get_state(self.entities.gateway_light) == "off"):
- self.call_service("light/turn_on", entity_id=self.entities.gateway_light, brightness=30, rgb_color=[255,230,205])
- def state_change(self, entity, attribute, old, new, kwargs):
- if(attribute == "state" and old == "off" and new == "on"):
- func = self.state_off_to_on_actions.get(entity, lambda: "none")
- func()
- if(attribute == "state" and old == "on" and new == "off"):
- func = self.state_on_to_off_actions.get(entity, lambda: "none")
- func()
- if(attribute == "state" and old == "off" and new == "off"):
- func = self.state_off_to_off_actions.get(entity, lambda: "none")
- func()
- if(attribute == "state" and entity == "sun.sun"):
- self.day_night_change()
- func = self.temperature_actions.get(entity, lambda temp: "none")
- func(new)
- def event_detected(self, event_name, data, kwargs):
- if(event_name == "xiaomi_aqara.click"):
- if(data["click_type"] == "click" or data["click_type"] == "single"):
- func = self.click_actions.get(data["entity_id"], lambda: "none")
- func()
- if(data["click_type"] == "both"):
- func = self.bothclick_actions.get(data["entity_id"], lambda: "none")
- for f in func:
- f()
- if(data["click_type"] == "double"):
- func = self.doubleclick_actions.get(data["entity_id"], lambda: "none")
- func()
- if(data["click_type"] == "long_click_press"):
- func = self.longclick_actions.get(data["entity_id"], lambda: "none")
- func()
- def terminate(self):
- for handle in self.listen_events:
- self.cancel_listen_event(handle)
- for handle in self.listen_states:
- self.cancel_listen_state(handle)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement