smart_brain/function/ground_floor_west.py

263 lines
14 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import devices
import logging
from function.rooms import room_shelly, room_shelly_tradfri_light, room_shelly_silvercrest_light
try:
from config import APP_NAME as ROOT_LOGGER_NAME
except ImportError:
ROOT_LOGGER_NAME = 'root'
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
class ground_floor_west_floor(room_shelly_silvercrest_light):
# https://shelly1l-84CCA8AD1148
def __init__(self, mqtt_client):
super().__init__(mqtt_client, "shellies/floor_eg_w", "gui/gfw_sw_floor",
"zigbee_eg_w/light/floor_eg_w/a", "gui/gfw_br_floor", "gui/gfw_ct_floor")
#
# Callback initialisation
#
self.main_light_tradfri_2 = devices.tradfri_light(mqtt_client, "zigbee_eg_w/light/floor_eg_w/b")
self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS,
None, self.sync_brightness_main_light)
self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP,
None, self.sync_color_temp_main_light)
def send_init_message_main_light(self):
return super().send_init_message_main_light()
self.main_light_tradfri_2.mqtt_client.send(self.main_light_tradfri_2.topic + "/get", '{"state": ""}')
def sync_brightness_main_light(self, device, key, data):
self.main_light_tradfri_2.set_brightness(data)
def sync_color_temp_main_light(self, device, key, data):
self.main_light_tradfri_2.set_color_temp(data)
class ground_floor_west_marion(room_shelly):
# https://shelly1l-E8DB84A1E067
def __init__(self, mqtt_client):
super().__init__(mqtt_client, "shellies/marion", "gui/gfw_sw_marion")
class ground_floor_west_dirk(room_shelly_tradfri_light):
STATE_ACTIVE_DEVICE_MAIN_LIGHT = 0
STATE_ACTIVE_DEVICE_DESK_LIGHT = 1
STATE_ACTIVE_DEVICE_AMPLIFIER = 2
STATE_ACTIVE_DEVICE_MAX_VALUE = STATE_ACTIVE_DEVICE_AMPLIFIER
# https://shelly1l-3C6105E44F27
def __init__(self, mqtt_client):
super().__init__(mqtt_client, "shellies/dirk", "gui/gfw_sw_dirk",
"zigbee_eg_w/light/dirk", "gui/gfw_br_dirk", "gui/gfw_ct_dirk")
#
self.powerplug_common = devices.my_powerplug(mqtt_client, "powerplug/dirk")
self.desk_light_tradfri = devices.tradfri_light(mqtt_client, "zigbee_eg_w/light/dirk_desk")
self.button_tradfri = devices.tradfri_button(mqtt_client, "zigbee_eg_w/input_device/dirk")
#
self.powerplug_common.add_callback(None, None, self.powerplug_gui_feedback_actions)
self.powerplug_common.add_callback(devices.my_powerplug.KEY_OUTPUT_2, None,
self.powerplug_amplifier_synchronisation)
#
self.gui_switch_desk_light = devices.nodered_gui(mqtt_client, "gui/gfw_sw_desk_light")
self.gui_brightness_desk_light = devices.nodered_gui(mqtt_client, "gui/gfw_br_desk_light")
self.gui_brightness_desk_light.enable(False)
self.gui_brightness_desk_light.set_feedback(0)
self.gui_color_temp_desk_light = devices.nodered_gui(mqtt_client, "gui/gfw_ct_desk_light")
self.gui_color_temp_desk_light.enable(False)
self.gui_color_temp_desk_light.set_feedback(0)
#
self.gui_switch_amplifier = devices.nodered_gui(mqtt_client, "gui/gfw_sw_amplifier")
self.gui_switch_cd_player = devices.nodered_gui(mqtt_client, "gui/gfw_sw_cd_player")
self.gui_switch_pc_dock = devices.nodered_gui(mqtt_client, "gui/gfw_sw_pc_dock")
#
self.remote_amplifier = devices.remote(mqtt_client, "hifi/remote/RAS5")
self.active_device_state_led = devices.status(mqtt_client, "gui/gfw_avtive_device_state")
#
self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
devices.tradfri_button.ACTION_TOGGLE, self.toggle_main_light)
self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
devices.tradfri_button.ACTION_RIGHT, self.desk_light_switch_action)
self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
devices.tradfri_button.ACTION_LEFT_LONG, self.amplifier_switch_action)
self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
devices.tradfri_button.ACTION_RIGHT_LONG, self.cd_player_switch_action)
self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
devices.tradfri_button.ACTION_LEFT, self.pc_dock_switch_action)
self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, None, self.brightness_action)
#
self.gui_switch_desk_light.add_callback(devices.nodered_gui.KEY_STATE, None, self.desk_light_switch_action)
self.gui_brightness_desk_light.add_callback(
devices.nodered_gui.KEY_BRIGHTNESS, None, self.desk_light_set_action)
self.gui_color_temp_desk_light.add_callback(
devices.nodered_gui.KEY_COLOR_TEMP, None, self.desk_light_set_action)
self.desk_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS,
None, self.desk_light_set_gui_params_action)
self.desk_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP,
None, self.desk_light_set_gui_params_action)
#
self.gui_switch_amplifier.add_callback(devices.nodered_gui.KEY_STATE, None, self.amplifier_switch_action)
self.gui_switch_cd_player.add_callback(devices.nodered_gui.KEY_STATE, None, self.cd_player_switch_action)
self.gui_switch_pc_dock.add_callback(devices.nodered_gui.KEY_STATE, None, self.pc_dock_switch_action)
#
self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.device_chooser_action)
self.powerplug_common.add_callback(None, None, self.device_chooser_action)
#
self.last_cd_player_powerplug_state = None
self.last_main_light_state = None
self.last_desk_light_state = None
self.last_amplifier_state = None
self.active_device_state = None
def all_off(self, device=None, key=None, data=None):
super().all_off(device, key, data)
self.powerplug_common.set_output_all(False)
def powerplug_gui_feedback_actions(self, device, key, data):
if key == devices.my_powerplug.KEY_OUTPUT_0:
self.gui_switch_amplifier.set_feedback(data)
elif key == devices.my_powerplug.KEY_OUTPUT_1:
self.gui_switch_desk_light.set_feedback(data)
self.gui_brightness_desk_light.enable(data)
self.gui_color_temp_desk_light.enable(data)
if not data:
self.gui_brightness_desk_light.set_feedback(0)
self.gui_color_temp_desk_light.set_feedback(0)
else:
self.gui_brightness_desk_light.set_feedback(self.desk_light_tradfri.brightness)
self.gui_color_temp_desk_light.set_feedback(self.desk_light_tradfri.color_temp / 10)
elif key == devices.my_powerplug.KEY_OUTPUT_2:
self.gui_switch_cd_player.set_feedback(data)
elif key == devices.my_powerplug.KEY_OUTPUT_3:
self.gui_switch_pc_dock.set_feedback(data)
def powerplug_amplifier_synchronisation(self, device, key, data):
if device == self.powerplug_common:
if data != self.last_cd_player_powerplug_state:
logger.info("Setting \"%s\" amplifier: %s", type(self).__name__, data)
self.powerplug_common.set_output_0(data)
self.last_cd_player_powerplug_state = data
def desk_light_switch_action(self, device, key, data):
if device == self.button_tradfri:
logger.info("Toggeling \"%s\" desk light to %s", type(self).__name__, not self.powerplug_common.output_1)
self.powerplug_common.set_output_1("toggle")
else:
logger.info("Setting \"%s\" desk light: %s", type(self).__name__, data)
self.powerplug_common.set_output_1(data)
def desk_light_set_gui_params_action(self, device, key, data):
if key == devices.nodered_gui.KEY_BRIGHTNESS:
self.gui_brightness_desk_light.set_feedback(data)
elif key == devices.nodered_gui.KEY_COLOR_TEMP:
self.gui_color_temp_desk_light.set_feedback(data / 10)
def desk_light_set_action(self, device, key, data):
if key == devices.nodered_gui.KEY_BRIGHTNESS:
logger.info("Setting brightness \"%s\" desk light: %s", type(self).__name__, data)
self.desk_light_tradfri.set_brightness(data)
elif key == devices.nodered_gui.KEY_COLOR_TEMP:
logger.info("Setting color_temp \"%s\" desk light: %s", type(self).__name__, data)
self.desk_light_tradfri.set_color_temp(data * 10)
def amplifier_switch_action(self, device, key, data):
if device == self.button_tradfri:
logger.info("Toggeling \"%s\" amplifier to %s", type(self).__name__, not self.powerplug_common.output_0)
self.powerplug_common.set_output_0("toggle")
else:
logger.info("Setting \"%s\" amplifier: %s", type(self).__name__, data)
self.powerplug_common.set_output_0(data)
def cd_player_switch_action(self, device, key, data):
if device == self.button_tradfri:
logger.info("Toggeling \"%s\" cd_player to %s", type(self).__name__, not self.powerplug_common.output_2)
self.powerplug_common.set_output_2("toggle")
else:
logger.info("Setting \"%s\" cd_player: %s", type(self).__name__, data)
self.powerplug_common.set_output_2(data)
def pc_dock_switch_action(self, device, key, data):
if device == self.button_tradfri:
logger.info("Toggeling \"%s\" pc_dock to %s", type(self).__name__, not self.powerplug_common.output_3)
self.powerplug_common.set_output_3("toggle")
else:
logger.info("Setting \"%s\" pc_dock: %s", type(self).__name__, data)
self.powerplug_common.set_output_3(data)
def device_chooser_action(self, device, key, data):
if device == self.main_light_shelly:
if self.last_main_light_state != data:
if data is True:
self.active_device_state = self.STATE_ACTIVE_DEVICE_MAIN_LIGHT
else:
self.choose_next_device()
self.last_main_light_state = data
elif device == self.powerplug_common and key == devices.my_powerplug.KEY_OUTPUT_1:
if self.last_desk_light_state != data:
if data is True:
self.active_device_state = self.STATE_ACTIVE_DEVICE_DESK_LIGHT
else:
self.choose_next_device()
self.last_desk_light_state = data
elif device == self.powerplug_common and key == devices.my_powerplug.KEY_OUTPUT_0:
if self.last_amplifier_state != data:
if data is True:
self.active_device_state = self.STATE_ACTIVE_DEVICE_AMPLIFIER
else:
self.choose_next_device()
self.last_amplifier_state = data
for num in range(0, self.STATE_ACTIVE_DEVICE_MAX_VALUE + 1):
self.active_device_state_led.set_state(num, self.active_device_state == num)
def get_activity_state(self, state):
if state == self.STATE_ACTIVE_DEVICE_MAIN_LIGHT:
return self.main_light_shelly.output_0
elif state == self.STATE_ACTIVE_DEVICE_DESK_LIGHT:
return self.powerplug_common.output_1
elif state == self.STATE_ACTIVE_DEVICE_AMPLIFIER:
return self.powerplug_common.output_0
def choose_next_device(self, device=None, key=None, data=None):
if self.active_device_state is not None:
start_value = self.active_device_state
for i in range(0, self.STATE_ACTIVE_DEVICE_MAX_VALUE + 1):
target_state = (start_value + i + 1) % (self.STATE_ACTIVE_DEVICE_MAX_VALUE + 1)
if self.get_activity_state(target_state):
self.active_device_state = target_state
return
self.active_device_state = None
def choose_prev_device(self, device=None, key=None, data=None):
if self.active_device_state is not None:
start_value = self.active_device_state
for i in range(0, self.STATE_ACTIVE_DEVICE_MAX_VALUE + 1):
target_state = (start_value - i - 1) % (self.STATE_ACTIVE_DEVICE_MAX_VALUE + 1)
if self.get_activity_state(target_state):
self.active_device_state = target_state
return
self.active_device_state = None
def brightness_action(self, device, key, data):
if self.active_device_state is not None:
brightness_func = {
self.STATE_ACTIVE_DEVICE_MAIN_LIGHT: self.main_light_tradfri,
self.STATE_ACTIVE_DEVICE_DESK_LIGHT: self.desk_light_tradfri,
self.STATE_ACTIVE_DEVICE_AMPLIFIER: self.remote_amplifier
}
target = brightness_func[self.active_device_state]
if target is None:
logger.error("Not yet implemented")
return
if data == devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG:
logger.info("Increasing \"%s\" - %s", type(self).__name__, target.topic)
target.default_inc()
elif data == devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_LONG:
logger.info("Decreasing \"%s\" - %s", type(self).__name__, target.topic)
target.default_dec()
elif data in [devices.tradfri_button.ACTION_BRIGHTNESS_UP_RELEASE, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_RELEASE]:
target.default_stop()