楼主
|
发表于 2023-4-15 17:15:42
|
显示全部楼层
本帖最后由 relliky 于 2023-4-15 17:19 编辑
因为用的函数,其实每个部分都抽象化了,生成的yaml重复代码很多。python的实际代码都挺简单的。
self.automations += [{
"alias" : "ZL-" + self.automation_room_name + "Applies Different Scenes Based on Scene Selections (State Execution)" + "-" + self.room_name,
"configured": self.cfg_scene,
"trigger": [
{
"platform": "state",
"entity_id": self.room_scene_ctl,
}
],
"mode":"restart", # using restart to improve responsiveness of a scene execution
"action": [
{
"choose": [
self.callSceneServiceIfSelected("All White"),
self.callSceneServiceIfSelected("Ceiling Light White"),
self.callSceneServiceIfSelected("Lamp LED White"),
self.callSceneServiceIfSelected("LED White"),
self.callSceneServiceIfSelected("Hue"),
self.callSceneServiceIfSelected("Night Mode"),
self.callSceneServiceIfSelected("Dark Night Mode"),
self.callSceneServiceIfSelected("All Off"),
self.callSceneServiceIfSelected("Lights on in hot sunshine"),
self.callSceneServiceIfSelected("Lights on when bright outdoor"),
self.callSceneServiceIfSelected("Lights on when dark outdoor")
]
}
]
}]
复制代码
callSceneServiceIfSelected 再调用具体的callSceneService的实现。
def callSceneService(self, scene_name):
parallel_enable = True
scene_service = []
if scene_name == 'All White':
scene_service += [self.turn(self.lamps, "on"),
self.turn(self.ceiling_lights, "on"),
self.turn(self.leds, "on"),
self.turn(self.tvs, tv_brightness=3)]
elif scene_name == 'Ceiling Light White':
scene_service += [self.turn(self.leds + self.lamps, "off"),
self.turn(self.ceiling_lights, "on"),
self.turn(self.tvs, tv_brightness=3)]
#elif scene_name == 'Ceiling Light White with Curtain Open':
# scene_service += [self.turn(self.leds + self.lamps, "off"),
# self.turn(self.ceiling_lights, "on"),
# self.turn(self.tvs, tv_brightness=3),
# self.turn(self.curtains, 'on')]
elif scene_name == 'Lamp LED White':
scene_service += [self.turn(self.ceiling_lights, "off"),
self.turn(self.lamps, "on"),
self.turn(self.leds, "on"),
self.turn(self.tvs, tv_brightness=3)]
elif scene_name == 'LED White':
scene_service += [self.turn(self.ceiling_lights, "off"),
self.turn(self.lamps, "off"),
self.turn(self.leds, "on"),
self.turn(self.tvs, tv_brightness=2)]
elif scene_name == 'Hue':
parallel_enable = False
scene_service += [# turn on lights to let adaptive lighting run
self.turn(self.lamps + self.leds, "on"),
# overwrite adaptive lighting brightiness and set it to max
self.turn(self.lamps + self.leds, "on", light_brightness=100),
# apply colours
{ "service" : "pyscript.set_rgb_light_list",
"data": {"light_list": self.lamps + self.ceiling_lights+ self.leds}},
self.turn(self.tvs, tv_brightness=2)]
elif scene_name == 'Night Mode':
scene_service += [{ "service": "homeassistant.turn_on",
"entity_id": "scene." + self.room_entity + "_night_mode" },
self.turn(self.tvs, tv_brightness=2)]
elif scene_name == 'Dark Night Mode':
scene_service += [{ "service": "homeassistant.turn_on",
"entity_id": "scene." + self.room_entity + "_dark_night_mode" },
self.turn(self.tvs, tv_brightness=1)]
elif scene_name == 'All Off':
scene_service += [self.turn(self.ceiling_lights, "off"),
self.turn(self.lamps, "off"),
self.turn(self.leds, "off")]
elif scene_name in ['Lights on in hot sunshine',
'Lights on when bright outdoor',
'Lights on when dark outdoor']:
scene_service += [{"parallel":[
{"if": self.continueIf(self.ceiling_light_control_when[scene_name], "on"), "then": self.turn(self.ceiling_lights, "on")},
{"if": self.continueIf(self.lamp_control_when[scene_name] , "on"), "then": self.turn(self.lamps, "on")},
{"if": self.continueIf(self.led_control_when[scene_name] , "on"), "then": self.turn(self.leds, "on")},
{"if": self.continueIf(self.curtain_control_when[scene_name] , "on"), "then": self.turn(self.curtains, "on")}
]}]
else:
raise TypeError( "\n" +\
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" + \
"Scene " + scene_name + " is not supported." + "\n" + \
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n")
if parallel_enable == True:
scene_service = [{"parallel":scene_service}]
# Convert to a single service/entry instead of a list
return self.convertToSingleService(scene_service, alias=scene_name)
复制代码
代码的复杂度可以自行扩展