318 lines
17 KiB
Python
318 lines
17 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
|
|
from function.helpers import changed_value_indicator
|
|
import task
|
|
|
|
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
|
|
#
|
|
KEY_POWERPLUG_AMPLIFIER = devices.my_powerplug.KEY_OUTPUT_0
|
|
KEY_POWERPLUG_CD_PLAYER = devices.my_powerplug.KEY_OUTPUT_2
|
|
KEY_POWERPLUG_DESK_LIGHT = devices.my_powerplug.KEY_OUTPUT_1
|
|
KEY_POWERPLUG_PC_DOCK = devices.my_powerplug.KEY_OUTPUT_3
|
|
#
|
|
AUDIO_SOURCE_PC = 0
|
|
AUDIO_SOURCE_CD = 1
|
|
AUDIO_SOURCE_RASPI = 2
|
|
#
|
|
KEY_SPOTIFY = "hifi/spotify/state"
|
|
KEY_MPD = "hifi/mpd/state"
|
|
|
|
# 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.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_active_device_state")
|
|
#
|
|
self.spotify_state = devices.audio_status(mqtt_client, "hifi/spotify")
|
|
self.mpd_state = devices.audio_status(mqtt_client, "hifi/mpd")
|
|
#
|
|
self.delayed_task = task.delayed(1.0, self.send_audio_source)
|
|
#
|
|
self.cvi = changed_value_indicator()
|
|
#
|
|
self.powerplug_common.add_callback(None, None, self.powerplug_gui_feedback_actions)
|
|
self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, None, self.cd_amplifier_synchronisation)
|
|
self.spotify_state.add_callback(devices.status.KEY_STATE, None, self.raspi_amplifier_synchronisation)
|
|
self.mpd_state.add_callback(devices.status.KEY_STATE, None, self.raspi_amplifier_synchronisation)
|
|
#
|
|
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.powerplug_common.add_callback(self.KEY_POWERPLUG_AMPLIFIER, None, self.audio_source_selector)
|
|
self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, None, self.audio_source_selector)
|
|
self.spotify_state.add_callback(devices.status.KEY_STATE, None, self.audio_source_selector)
|
|
self.mpd_state.add_callback(devices.status.KEY_STATE, None, self.audio_source_selector)
|
|
#
|
|
self.active_device_state = None
|
|
#
|
|
self.audio_source = self.AUDIO_SOURCE_PC
|
|
|
|
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 == self.KEY_POWERPLUG_AMPLIFIER:
|
|
self.gui_switch_amplifier.set_feedback(data)
|
|
elif key == self.KEY_POWERPLUG_DESK_LIGHT:
|
|
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 == self.KEY_POWERPLUG_CD_PLAYER:
|
|
self.gui_switch_cd_player.set_feedback(data)
|
|
elif key == self.KEY_POWERPLUG_PC_DOCK:
|
|
self.gui_switch_pc_dock.set_feedback(data)
|
|
|
|
def cd_amplifier_synchronisation(self, device, key, data):
|
|
if self.cvi.changed_here(device.topic, key, data) and device.previous_value(key) is not None:
|
|
logger.info("Syncing \"%s\" amplifier with cd player: %s", type(self).__name__, data)
|
|
self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, data)
|
|
|
|
def raspi_amplifier_synchronisation(self, device, key, data):
|
|
if self.cvi.changed_here(device.topic, key, data) and device.previous_value(key) is not None:
|
|
logger.info("Syncing \"%s\" amplifier with raspi player: %s", type(self).__name__, data)
|
|
self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, 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.get(self.KEY_POWERPLUG_AMPLIFIER))
|
|
self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, "toggle")
|
|
else:
|
|
logger.info("Setting \"%s\" desk light: %s", type(self).__name__, data)
|
|
self.powerplug_common.set_output(self.KEY_POWERPLUG_DESK_LIGHT, 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.get(self.KEY_POWERPLUG_AMPLIFIER))
|
|
self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, "toggle")
|
|
else:
|
|
logger.info("Setting \"%s\" amplifier: %s", type(self).__name__, data)
|
|
self.powerplug_common.set_output(self.KEY_POWERPLUG_AMPLIFIER, 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.get(self.KEY_POWERPLUG_CD_PLAYER))
|
|
self.powerplug_common.set_output(self.KEY_POWERPLUG_CD_PLAYER, "toggle")
|
|
else:
|
|
logger.info("Setting \"%s\" cd_player: %s", type(self).__name__, data)
|
|
self.powerplug_common.set_output(self.KEY_POWERPLUG_CD_PLAYER, 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.get(self.KEY_POWERPLUG_PC_DOCK))
|
|
self.powerplug_common.set_output(self.KEY_POWERPLUG_PC_DOCK, "toggle")
|
|
else:
|
|
logger.info("Setting \"%s\" pc_dock: %s", type(self).__name__, data)
|
|
self.powerplug_common.set_output(self.KEY_POWERPLUG_PC_DOCK, data)
|
|
|
|
def device_chooser_action(self, device, key, data):
|
|
if device == self.main_light_shelly:
|
|
if self.cvi.changed_here(device.topic, key, data):
|
|
if data is True:
|
|
self.active_device_state = self.STATE_ACTIVE_DEVICE_MAIN_LIGHT
|
|
else:
|
|
self.choose_next_device()
|
|
elif device == self.powerplug_common and key == self.KEY_POWERPLUG_DESK_LIGHT:
|
|
if self.cvi.changed_here(device.topic, key, data):
|
|
if data is True:
|
|
self.active_device_state = self.STATE_ACTIVE_DEVICE_DESK_LIGHT
|
|
else:
|
|
self.choose_next_device()
|
|
elif device == self.powerplug_common and key == self.KEY_POWERPLUG_AMPLIFIER:
|
|
if self.cvi.changed_here(device.topic, key, data):
|
|
if data is True:
|
|
self.active_device_state = self.STATE_ACTIVE_DEVICE_AMPLIFIER
|
|
else:
|
|
self.choose_next_device()
|
|
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.get(self.KEY_POWERPLUG_DESK_LIGHT)
|
|
elif state == self.STATE_ACTIVE_DEVICE_AMPLIFIER:
|
|
return self.powerplug_common.get(self.KEY_POWERPLUG_AMPLIFIER)
|
|
|
|
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()
|
|
|
|
def audio_source_selector(self, device, key, data):
|
|
if device == self.powerplug_common and key == self.KEY_POWERPLUG_CD_PLAYER:
|
|
if self.cvi.changed_here(device.topic, key, data) and data is True:
|
|
# switch on of cd player
|
|
self.audio_source = self.AUDIO_SOURCE_CD
|
|
elif device in [self.spotify_state, self.mpd_state]:
|
|
if self.cvi.changed_here(device.topic, key, data) and data is True:
|
|
# switch on raspi-source
|
|
self.audio_source = self.AUDIO_SOURCE_RASPI
|
|
elif device == self.powerplug_common and key == self.KEY_POWERPLUG_AMPLIFIER:
|
|
if self.cvi.changed_here(device.topic, key, data) and data is True:
|
|
# switch on of amplifier -> select source and reset stored source value
|
|
self.delayed_task.run()
|
|
|
|
def send_audio_source(self):
|
|
if self.audio_source == self.AUDIO_SOURCE_PC:
|
|
logger.info("Sending IR command to change audio source to pc")
|
|
self.remote_amplifier.set_line3()
|
|
elif self.audio_source == self.AUDIO_SOURCE_CD:
|
|
logger.info("Sending IR command to change audio source to cd")
|
|
self.remote_amplifier.set_cd()
|
|
elif self.audio_source == self.AUDIO_SOURCE_RASPI:
|
|
logger.info("Sending IR command to change audio source to raspi")
|
|
self.remote_amplifier.set_line1()
|
|
self.audio_source = self.AUDIO_SOURCE_PC
|