Here are some automations that I have for Xiaomi stuff - hopefully it will help you to get started:
import appdaemon.appapi as appapi
import globals
import flags
#
# Xiaomi Stuff
#
# Args:
#
class xiaomi(appapi.AppDaemon):
def initialize(self):
self.listen_event(self.x_button1_single, "click", entity_id = globals.x_switch_1, click_type = "single")
self.listen_event(self.x_button1_double, "click", entity_id = globals.x_switch_1, click_type = "double")
self.listen_event(self.x_button2_single, "click", entity_id = globals.x_switch_2, click_type = "single")
self.listen_state(self.garage_door, globals.entry_garage, new="on")
self.listen_event(self.cube_rotate, "cube_action", action_type="rotate", entity_id = globals.cube)
self.listen_event(self.cube_tap, "cube_action", action_type="tap_twice", entity_id = globals.cube)
self.listen_event(self.cube_flip90, "cube_action", action_type="flip90", entity_id = globals.cube)
self.togglec = 1
self.togglec2 = 1
def x_button1_single(self,event_name, data, kwargs):
self.log("button 1 press...")
self.toggle("light.hg_office_oh_lights")
def x_button1_double(self,event_name, data, kwargs):
self.log("button 1 double click...")
def x_button2_single(self,event_name, data, kwargs):
self.log("button 2 press...")
if self.entities.light.hannahs_br.state == "off":
self.turn_on("light.hannahs_br")
self.turn_on("light.hannahs_br")
else:
self.turn_off("light.hannahs_br")
self.turn_off("light.hannahs_br")
def garage_door(self, entity, attribute, old, new, kwargs):
self.log("garage entry door")
self.call_service("xiaomi_aqara/play_ringtone",gw_mac = globals.x_gw_office, ringtone_id = 10025,ringtone_vol = 2)
def cube_rotate(self,event_name, data, kwargs):
self.log("cube rotating")
self.log(data["action_value"])
if self.entities.light.office_desk.state !="off":
self.cur_brightness=self.entities.light.office_desk.attributes.brightness
else:
self.cur_brightness=0
self.log(self.cur_brightness)
self.cube_value=data["action_value"]
self.new_brightness = (self.cube_value * 3) + self.cur_brightness
self.log("new bright " + str(self.new_brightness))
if self.new_brightness >= 255:
self.new_brightness = 255
elif self.new_brightness <= 0:
self.new_brightness = 0
if self.new_brightness == 0:
self.turn_off("light.office_desk")
else:
self.turn_on("light.office_desk", brightness=self.new_brightness)
def cube_tap(self,event_name, data, kwargs):
#self.toggle("light.office_desk")
if self.togglec2 == 1:
self.togglec2 = 0
self.set_state(globals.motion_detector_garage,state="on")
else:
self.togglec2 = 1
self.set_state(globals.motion_detector_garage,state="off")
def cube_flip90(self,event_name, data, kwargs):
self.log("cube flip90")
#if self.get_state(globals.motion_detector_garage) == "off":
#self.log("it's off - setting garage motion on")
#self.set_state(globals.motion_detector_garage,state="on")
#else:
#self.log("it's on - setting garage motion off")
#self.set_state(globals.motion_detector_garage,state="off")
if 1 == 2:
self.call_service("xiaomi_aqara/play_ringtone",gw_mac = globals.x_gw_office, ringtone_id = 10025,ringtone_vol = 10)
#self.log(flags.timer_heater)
flags.timer_heater = "active"
#self.log(flags.timer_heater)
if self.togglec == 1:
self.togglec = 0
self.set_state(globals.garage_1,state="on")
else:
self.togglec = 1
self.set_state(globals.garage_1,state="off")
Let me know if you have any questions - good luck!
Tom