This commit is contained in:
monster
2018-12-30 14:13:57 +03:00
parent e311c31b88
commit 2033fe79e4
225 changed files with 6370 additions and 0 deletions
@@ -0,0 +1,18 @@
import appdaemon.plugins.hass.hassapi as hass
import globals
class KitchenLedController(hass.Hass):
listen_event_handle_list = []
def initialize(self):
if not globals.check_args(self,['watch_entity']):
return
self.listen_event_handle_list.append(self.listen_state(self.entity_changed, self.args['watch_entity']))
def entity_changed(self, entity, attribute, old, new, kwargs):
stete = "off"
if new == "off":
stete = "off"
if new == "on":
stete = "on"
self.call_service("mqtt/publish", topic = "home/kitchen-led/kitchen-led/state/set", payload = stete)
@@ -0,0 +1,9 @@
#################################
# LIGHT BUTTONS #
#################################
kitchen_led_controller:
module: kitchen_led_controller
class: KitchenLedController
watch_entity: light.kitchen
global_dependencies:
- globals
@@ -0,0 +1,86 @@
import appdaemon.plugins.hass.hassapi as hass
import globals
class LightButton(hass.Hass):
def initialize(self):
if not globals.check_args(self,['entities', 'sensor']):
return
self.listen_event_handle_list = []
self.listen_event_handle_list.append(self.listen_event(self.event_detected, "xiaomi_aqara.click"))
def event_detected(self, event_name, data, kwargs):
if data["entity_id"] != self.args["sensor"]:
return
if data["click_type"] == "single":
self.log('button_click')
self.make_action_by_mode("off")
if data["click_type"] == "double":
self.log('button_dblclick')
self.make_action_by_mode("on")
def make_action_by_mode(self, state):
states = self.fill_states()
control_entity = self.args.get('control_input', None)
if (control_entity != None):
entity_id = next(iter(states))
state = self.get_state(entity_id)
self.set_state(entity_id, self.invert_state(state))
self.set_state(control_entity, state)
return
if len(self.args["entities"]) > 1:
self.toggle_next_with_state(state, states)
def toggle_next_with_state(self, state, states):
for entity_id in states:
entity_state = states[entity_id]
if (entity_state == state):
self.set_state(entity_id, self.invert_state(state))
self.log('toggling {}'.format(entity_id))
return
for entity_id in states:
self.log('toggling {}'.format(entity_id))
self.toggle(entity_id)
def set_state(self, entity_id, state):
if (state == "off"):
self.log('turn_off {}'.format(entity_id))
self.set_off(entity_id)
if (state == "on"):
self.log('turn_on {}'.format(entity_id))
self.set_on(entity_id)
def set_on(self, entity_id):
if entity_id.startswith("light"):
self.call_service("light/turn_on", entity_id = entity_id, transition = 1, brightness = 255)
else:
self.turn_on(entity_id)
def set_off(self, entity_id):
if entity_id.startswith("light"):
self.call_service("light/turn_off", entity_id = entity_id, transition = 1)
else:
self.turn_off(entity_id)
def invert_state(self, state):
if (state == "off"):
return "on"
return "off"
def fill_states(self):
states = {}
for entity_id in self.split_device_list(self.args["entities"]):
state = self.get_state(entity_id)
if (state == None):
self.log("Warning! Device {} not found (state: {}).".format(entity_id, state))
continue
states[entity_id] = state
return states
def terminate(self):
if self.listen_event_handle_list != None:
for listen_event_handle in self.listen_event_handle_list:
self.cancel_listen_event(listen_event_handle)
@@ -0,0 +1,46 @@
#################################
# LIGHT BUTTONS #
#################################
light_button_main:
module: lightbutton
class: LightButton
sensor: binary_sensor.switch_158d00013f81a9
entities: light.yeelight_rgb_34ce008fe328,light.yeelight_rgb_286c071065c7,light.yeelight_white_34ce0087ff33,light.yeelight_white_f0b4290e5444,light.yeelight_white_f0b429a8f6df
global_dependencies:
- globals
light_button_hall:
module: lightbutton
class: LightButton
sensor: binary_sensor.switch_158d000102f7e6
entities: group.hall_light
control_input: input_boolean.hall_light_control
global_dependencies:
- globals
light_button_bath:
module: lightbutton
class: LightButton
sensor: binary_sensor.switch_158d0001256810
entities: group.bath_light
control_input: input_boolean.bath_light_control
global_dependencies:
- globals
light_button_kitchen:
module: lightbutton
class: LightButton
sensor: binary_sensor.switch_158d00012d5720
entities: group.kitchen_light
control_input: input_boolean.kitchen_light_control
global_dependencies:
- globals
light_button_balcony:
module: lightbutton
class: LightButton
sensor: binary_sensor.wall_switch_158d000183a5fe
entities: group.balcony_light
control_input: input_boolean.balcony_light_control
global_dependencies:
- globals
@@ -0,0 +1,82 @@
import appdaemon.plugins.hass.hassapi as hass
import globals
#
# This app will use motion sensors to turn on and off control_entity for 'timeout' time.
# It work with xiaomi motion sensors.
# main algorithm: when sensor turns on - turn on the light; when sensor is off - set timer for 'timeout';
# if sensor goes on - turn off the timer; when timer was hit - turn off the light.
#
# Args:
#
# sensor = motion sensor to use (will work with other sensor types too)
# timeout = timeout after sensor will turned off (keep in mind that xiaomi motion sensor has own timeout!)
# control_entity = entity, that will be controlled (example: group of light bulbs)
# constraint = you can define constraint (input_boolean), that will turn on and off this app.
# after_sundown (optionally) - whether to only trigger after sundown. example: True
# Release Notes
#
# Version 1.0:
# Initial Version
class LightControl(hass.Hass):
timer = None
def initialize(self):
if not globals.check_args(self, ["sensor", "timeout", "control_entity"]):
return
self.listen_event_handle_list = []
self.listen_event_handle_list.append(self.listen_state(self.sensor_trigger, self.args['sensor']))
def sensor_trigger(self, entity, attribute, old, new, kwargs):
if 'constraint' in self.args and not self.constrain_input_boolean(self.args['constraint']):
return
if 'after_sundown' in self.args and self.args['after_sundown'] and not self.sun_down():
# self.log('Sun is up, and we will turn it only after sundown.')
return
#sensor is off
if new == 'off' and old == 'on':
self.log('Sensor is off - running timer for {}s.'.format(self.args['timeout']))
self.run_timer()
#sensor is on
if new == 'on' and old == 'off':
#turnin on control entity and wait while sensor will be off, than run timer.
self.control_entity_on()
self.stop_timer()
def terminate(self):
if self.listen_event_handle_list != None:
for listen_event_handle in self.listen_event_handle_list:
self.cancel_listen_event(listen_event_handle)
############ TIMER ########################
def run_timer(self):
if self.timer != None:
self.stop_timer()
self.timer = self.run_in(self.control_entity_off, self.args['timeout'])
def control_entity_on(self):
#if 'constraint' is off - we dont need to do anything
if 'constraint' in self.args and not self.constrain_input_boolean(self.args['constraint']):
return
if self.get_state(self.args['control_entity']) == 'off':
self.turn_on(self.args['control_entity'])
self.log("Light on ({}).".format(self.args['control_entity']))
def control_entity_off(self, kwargs):
#if 'constraint' is off - we dont need to do anything
if 'constraint' in self.args and not self.constrain_input_boolean(self.args['constraint']):
return
if self.get_state(self.args['control_entity']) == 'on':
self.turn_off(self.args['control_entity'])
self.log("Light off.")
self.stop_timer()
def stop_timer(self):
if (self.timer == None):
return
self.log('stopping timer.')
self.cancel_timer(self.timer)
self.timer = None
@@ -0,0 +1,43 @@
#################################
# LIGHT CONTROL #
#################################
light_control_mainroom:
module: lightcontrol
class: LightControl
sensor: group.mainroom_motion
control_entity: group.mainroom_light
timeout: 60
constraint: input_boolean.mainroom_light_control
global_dependencies:
- globals
light_control_kitchen:
module: lightcontrol
class: LightControl
sensor: group.kitchen_motion
control_entity: group.kitchen_light
timeout: 300
constraint: input_boolean.kitchen_light_control
global_dependencies:
- globals
light_control_hall:
module: lightcontrol
class: LightControl
sensor: group.hall_motion
control_entity: group.hall_light
timeout: 120
constraint: input_boolean.hall_light_control
global_dependencies:
- globals
light_control_balcony:
module: lightcontrol
class: LightControl
sensor: group.balcony_door_window
control_entity: group.balcony_light
timeout: 3
constraint: input_boolean.balcony_light_control
after_sundown: True
global_dependencies:
- globals
@@ -0,0 +1,94 @@
import appdaemon.plugins.hass.hassapi as hass
#
# This app will use motion sensors to turn on and off control_entity for 'timeout' time.
# It work with xiaomi motion sensors.
# main algorithm: when sensor turns on - turn on the light; when sensor is off - set timer for 'timeout';
# if sensor goes on - turn off the timer; when timer was hit - turn off the light.
#
# Args:
#
# sensor = motion sensor to use (will work with other sensor types too)
# timeout = timeout after sensor will turned off (keep in mind that xiaomi motion sensor has own timeout!)
# control_entity = entity, that will be controlled (example: group of light bulbs)
# constraint = you can define constraint (input_boolean), that will turn on and off this app.
#
# Release Notes
#
# Version 1.0:
# Initial Version
class LightControlBath(hass.Hass):
timer = None
door_state_changed = None
def initialize(self):
if "sensor" not in self.args or 'door_sensor' not in self.args or "timeout" not in self.args or "control_entity" not in self.args:
self.error("Please provide sensor, door_sensor, control_entity and timeout in config!")
return
self.listen_state(self.sensor_trigger, self.args['sensor'])
self.listen_state(self.door_sensor_trigger, self.args['door_sensor'])
def sensor_trigger(self, entity, attribute, old, new, kwargs):
#sensor is off
if new == 'off' and old == 'on':
self.log('door_state_changed:{}'.format(self.door_state_changed))
if self.door_state_changed == False:
return
self.log('Sensor is off - running timer for {}s.'.format(self.args['timeout']))
self.run_timer()
#sensor is on
if new == 'on' and old == 'off':
#turnin on control entity and wait while sensor will be off, than run timer.
self.control_entity_on()
self.stop_timer()
door_state = self.get_state(self.args['door_sensor'])
if door_state == "off":
self.door_state_changed = False
else :
self.door_state_changed = None
def door_sensor_trigger(self, entity, attribute, old, new, kwargs):
#door is open
sensor_state = self.get_state(self.args['sensor'])
if new == 'on' and old == 'off' and sensor_state == 'off':
self.log('door is opened now, runnig the timer')
if self.timer == None:
self.run_timer()
if self.door_state_changed == False:
self.door_state_changed = True
self.log('door_state_changed:{}'.format(self.door_state_changed))
############ TIMER ########################
def control_entity_on(self):
#if 'constraint' is off - we dont need to do anything
if 'constraint' in self.args and not self.constrain_input_boolean(self.args['constraint']):
return
if self.get_state(self.args['control_entity']) == 'off':
self.turn_on(self.args['control_entity'])
self.log("Light on.")
def control_entity_off(self, kwargs):
#if 'constraint' is off - we dont need to do anything
if 'constraint' in self.args and not self.constrain_input_boolean(self.args['constraint']):
return
if self.door_state_changed == False:
self.log('Motion is detected inside and door is still closed - dont turn off the light')
return
if self.get_state(self.args['control_entity']) == 'on':
self.turn_off(self.args['control_entity'])
self.log("Light off.")
self.stop_timer()
def run_timer(self):
if self.timer != None:
self.stop_timer()
self.timer = self.run_in(self.control_entity_off, self.args['timeout'])
def stop_timer(self):
if (self.timer == None):
return
self.log('stopping timer.')
self.cancel_timer(self.timer)
self.timer = None
@@ -0,0 +1,13 @@
#################################
# LIGHT CONTROL #
#################################
light_control_bath:
module: lightcontrol_bath
class: LightControlBath
sensor: group.bath_motion
door_sensor: binary_sensor.door_window_sensor_158d000111a469 #TODO: make it group
control_entity: group.bath_light
timeout: 60
constraint: input_boolean.bath_light_control
global_dependencies:
- globals