123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- #!/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):
- # http://shelly1l-84CCA8AD1148
- def __init__(self, mqtt_client):
- super().__init__(mqtt_client, "shellies/gfw/floor/main_light", "gui/gfw/floor/main_light/switch",
- "zigbee/gfw/floor/main_light/a", "gui/gfw/floor/main_light/br_ct")
- #
- # Callback initialisation
- #
- self.main_light_tradfri_2 = devices.tradfri_light(mqtt_client, "zigbee/gfw/floor/main_light/b")
- self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.main_light_tradfri_2.set_brightness_mcb)
- self.main_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.main_light_tradfri_2.set_color_temp_mcb)
-
- def send_init_message_main_light(self):
- super().send_init_message_main_light()
- self.main_light_tradfri_2.mqtt_client.send(self.main_light_tradfri_2.topic + "/get", '{"state": ""}')
-
-
- class ground_floor_west_marion(room_shelly):
- # http://shelly1l-E8DB84A1E067
- def __init__(self, mqtt_client):
- super().__init__(mqtt_client, "shellies/gfw/marion/main_light", "gui/gfw/marion/main_light/switch")
-
-
- 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
- #
- LED_ACTIVE_DEVICE_MAIN_LIGHT = devices.nodered_gui_leds.KEY_LED_0
- LED_ACTIVE_DEVICE_DESK_LIGHT = devices.nodered_gui_leds.KEY_LED_1
- LED_ACTIVE_DEVICE_AMPLIFIER = devices.nodered_gui_leds.KEY_LED_2
- #
- 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
-
- # http://shelly1l-3C6105E44F27
- def __init__(self, mqtt_client):
- super().__init__(mqtt_client, "shellies/gfw/dirk/main_light", "gui/gfw/dirk/main_light/switch", "zigbee/gfw/dirk/main_light", "gui/gfw/dirk/main_light/br_ct")
- #
- self.powerplug_common = devices.my_powerplug(mqtt_client, "my_apps/gfw/dirk/powerplug")
- self.desk_light_tradfri = devices.tradfri_light(mqtt_client, "zigbee/gfw/dirk/desk_light")
- self.button_tradfri = devices.tradfri_button(mqtt_client, "zigbee/gfw/dirk/input_device")
- #
- self.gui_switch_desk_light = devices.nodered_gui_switch(mqtt_client, "gui/gfw/dirk/desk_light/switch")
- self.gui_br_cr_desk_light = devices.nodered_gui_brightness_color_temp(mqtt_client, "gui/gfw/dirk/desk_light/br_ct")
- #
- self.gui_switch_amplifier = devices.nodered_gui_switch(mqtt_client, "gui/gfw/dirk/amplifier/switch")
- self.gui_switch_cd_player = devices.nodered_gui_switch(mqtt_client, "gui/gfw/dirk/cd_player/switch")
- self.gui_switch_pc_dock = devices.nodered_gui_switch(mqtt_client, "gui/gfw/dirk/pc_dock/switch")
- #
- self.remote_amplifier = devices.remote(mqtt_client, "my_apps/gfw/dirk/remote/RAS5")
- self.gui_led_active_device = devices.nodered_gui_leds(mqtt_client, "gui/gfw/dirk/active_device_state/led")
- #
- self.spotify_state = devices.audio_status(mqtt_client, "my_apps/gfw/dirk/hifi/spotify")
- self.mpd_state = devices.audio_status(mqtt_client, "my_apps/gfw/dirk/hifi/mpd")
- #
- self.delayed_task = task.delayed(1.0, self.send_audio_source)
- #
- self.cvi = changed_value_indicator()
- #
- # Callback initialisation
- #
-
- # main light
- self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_TOGGLE,
- self.main_light_shelly.toggle_output_0_mcb)
-
- # desk light
- # switch
- self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_RIGHT,
- self.powerplug_common.toggle_output_1_mcb)
- self.gui_switch_desk_light.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_common.set_output_1_mcb)
- self.powerplug_common.add_callback(self.KEY_POWERPLUG_DESK_LIGHT, None, self.gui_switch_desk_light.set_state_mcb)
- # brightness and color temp
- self.gui_br_cr_desk_light.add_callback(devices.nodered_gui_brightness_color_temp.KEY_BRIGHTNESS,
- None, self.desk_light_tradfri.set_brightness_mcb)
- self.gui_br_cr_desk_light.add_callback(devices.nodered_gui_brightness_color_temp.KEY_COLOR_TEMP,
- None, self.desk_light_tradfri.set_color_temp_mcb)
- self.powerplug_common.add_callback(self.KEY_POWERPLUG_DESK_LIGHT, None, self.gui_br_cr_desk_light.set_enable_mcb)
- self.desk_light_tradfri.add_callback(devices.tradfri_light.KEY_BRIGHTNESS, None, self.gui_br_cr_desk_light.set_brightness_mcb)
- self.desk_light_tradfri.add_callback(devices.tradfri_light.KEY_COLOR_TEMP, None, self.gui_br_cr_desk_light.set_color_temp_mcb)
-
- # amplifier
- self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_LEFT_LONG,
- self.powerplug_common.toggle_output_0_mcb)
- self.gui_switch_amplifier.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_common.set_output_0_mcb)
- self.powerplug_common.add_callback(self.KEY_POWERPLUG_AMPLIFIER, None, self.gui_switch_amplifier.set_state_mcb)
- # amplifier auto on
- 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)
- # audio source
- 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.audio_source = self.AUDIO_SOURCE_PC
-
- # cd player
- self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_RIGHT_LONG,
- self.powerplug_common.toggle_output_2_mcb)
- self.gui_switch_cd_player.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_common.set_output_2_mcb)
- self.powerplug_common.add_callback(self.KEY_POWERPLUG_CD_PLAYER, None, self.gui_switch_cd_player.set_state_mcb)
-
- # pc dock
- self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_LEFT,
- self.powerplug_common.toggle_output_3_mcb)
- self.gui_switch_pc_dock.add_callback(devices.nodered_gui_switch.KEY_STATE, None, self.powerplug_common.set_output_3_mcb)
- self.powerplug_common.add_callback(self.KEY_POWERPLUG_PC_DOCK, None, self.gui_switch_pc_dock.set_state_mcb)
-
- # brightness
- self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, None, self.brightness_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.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_UP, self.choose_next_device)
- self.button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN, self.choose_prev_device)
- self.active_device_state = None
- self.update_active_device_led()
-
- def all_off(self, device=None, key=None, data=None):
- super().all_off(device, key, data)
- self.powerplug_common.set_output_all(False)
-
- 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 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
- self.update_active_device_led()
- 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
- self.update_active_device_led()
- 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
- self.update_active_device_led()
- else:
- self.choose_next_device()
-
- 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 update_active_device_led(self):
- self.gui_led_active_device.set_led(self.LED_ACTIVE_DEVICE_AMPLIFIER, self.active_device_state == self.STATE_ACTIVE_DEVICE_AMPLIFIER)
- self.gui_led_active_device.set_led(self.LED_ACTIVE_DEVICE_MAIN_LIGHT, self.active_device_state == self.STATE_ACTIVE_DEVICE_MAIN_LIGHT)
- self.gui_led_active_device.set_led(self.LED_ACTIVE_DEVICE_DESK_LIGHT, self.active_device_state == self.STATE_ACTIVE_DEVICE_DESK_LIGHT)
-
- 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
- self.update_active_device_led()
- return
- self.active_device_state = None
- self.update_active_device_led()
-
- 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
- self.update_active_device_led()
- return
- self.active_device_state = None
- self.update_active_device_led()
-
- 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
|