Smarthome Functionen
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

modules.py 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import devices
  5. import logging
  6. import task
  7. try:
  8. from config import APP_NAME as ROOT_LOGGER_NAME
  9. except ImportError:
  10. ROOT_LOGGER_NAME = 'root'
  11. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  12. class brightness_choose_n_action(object):
  13. def __init__(self, mqtt_client, button_tradfri, topic_led):
  14. self.gui_led_active_device = devices.nodered_gui_leds(mqtt_client, topic_led)
  15. # brightness change
  16. button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
  17. devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_LONG, self.brightness_action)
  18. button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG, self.brightness_action)
  19. button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
  20. devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_RELEASE, self.brightness_action)
  21. button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
  22. devices.tradfri_button.ACTION_BRIGHTNESS_UP_RELEASE, self.brightness_action)
  23. # device change
  24. button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_UP, self.choose_next_device)
  25. button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN, self.choose_prev_device)
  26. #
  27. self.brightness_device_list = []
  28. self.callback_device_list = []
  29. self.device_states = []
  30. self.active_device_state = None
  31. self.update_active_device_led()
  32. def add(self, brightness_device, callback_device, callback_key):
  33. """
  34. brightness_device: A device for brightness function needs to have the following methods:
  35. * .default_inc()
  36. * .default_dec()
  37. * .default_stop()
  38. callback_device: A device for installing callback which are executed, when the device is switched on or off. It needs the following method:
  39. * .add_callback(key, data or None, callback, on_changes_only)
  40. """
  41. if len(self.brightness_device_list) >= len(devices.nodered_gui_leds.RX_KEYS):
  42. raise ValueError("Number of devices is limited by number of leds in devices.nodered_gui_leds.")
  43. self.brightness_device_list.append(brightness_device)
  44. self.callback_device_list.append((callback_device, callback_key))
  45. self.device_states.append(False)
  46. callback_device.add_callback(callback_key, True, self.device_state_action, True)
  47. callback_device.add_callback(callback_key, False, self.device_state_action, True)
  48. def device_state_action(self, device, key, data):
  49. self.device_states[self.callback_device_list.index((device, key))] = data
  50. if data is True:
  51. self.active_device_state = self.callback_device_list.index((device, key))
  52. self.update_active_device_led()
  53. else:
  54. self.choose_next_device()
  55. def update_active_device_led(self):
  56. for i in range(0, len(self.brightness_device_list)):
  57. self.gui_led_active_device.set_led(devices.nodered_gui_leds.RX_KEYS[i], self.active_device_state == i)
  58. def choose_prev_device(self, device=None, key=None, data=None):
  59. if self.active_device_state is not None:
  60. start_value = self.active_device_state
  61. for i in range(0, len(self.brightness_device_list)):
  62. target_state = (start_value - i - 1) % (len(self.brightness_device_list))
  63. if self.device_states[target_state]:
  64. self.active_device_state = target_state
  65. self.update_active_device_led()
  66. return
  67. self.active_device_state = None
  68. self.update_active_device_led()
  69. def choose_next_device(self, device=None, key=None, data=None):
  70. if self.active_device_state is not None:
  71. start_value = self.active_device_state
  72. for i in range(0, len(self.brightness_device_list)):
  73. target_state = (start_value + i + 1) % (len(self.brightness_device_list))
  74. if self.device_states[target_state]:
  75. self.active_device_state = target_state
  76. self.update_active_device_led()
  77. return
  78. self.active_device_state = None
  79. self.update_active_device_led()
  80. def brightness_action(self, device, key, data):
  81. if self.active_device_state is not None:
  82. target = self.brightness_device_list[self.active_device_state]
  83. if data == devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG:
  84. logger.info("Increasing \"%s\" - %s", type(self).__name__, target.topic)
  85. target.default_inc()
  86. elif data == devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_LONG:
  87. logger.info("Decreasing \"%s\" - %s", type(self).__name__, target.topic)
  88. target.default_dec()
  89. elif data in [devices.tradfri_button.ACTION_BRIGHTNESS_UP_RELEASE, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_RELEASE]:
  90. target.default_stop()
  91. class heating_function_brennenstuhl(object):
  92. RETURN_TO_DEFAULT_TIME = 45 * 60
  93. BOOST_TEMP_OFFSET = 5
  94. def __init__(self, mqtt_client, topic_valve, default_temperature, topic_boost, topic_setpoint, topic_led):
  95. self.ct = task.periodic(1, self.cyclic_task)
  96. #
  97. self.topic = topic_valve
  98. self.default_temperature = default_temperature
  99. #
  100. self.heating_valve = devices.brennenstuhl_heatingvalve(mqtt_client, topic_valve)
  101. self.heating_valve.set_heating_setpoint(self.default_temperature)
  102. self.heating_valve.add_callback(
  103. devices.brennenstuhl_heatingvalve.KEY_HEATING_SETPOINT, None, self.heating_setpoint_actions)
  104. self.gui_value_temp_setp = devices.nodered_gui_heatvalve(mqtt_client, topic_setpoint)
  105. self.gui_value_temp_setp.add_callback(
  106. devices.nodered_gui_heatvalve.KEY_HEATING_SETPOINT, None, self.heating_setpoint_actions)
  107. self.gui_button_boost = devices.nodered_gui_heatvalve(mqtt_client, topic_boost)
  108. self.gui_button_boost.add_callback(None, None, self.boost_actions)
  109. self.gui_led_boost = devices.nodered_gui_heatvalve(mqtt_client, topic_led)
  110. #
  111. self.return_to_default_timer = None
  112. self.return_to_default_setpoint = None
  113. self.gui_led_boost.set_feedback(False)
  114. #
  115. self.ct.run()
  116. def heating_setpoint_actions(self, device, key, data):
  117. if device.topic == self.heating_valve.topic:
  118. # valve setpoint action
  119. self.gui_value_temp_setp.set_feedback(data)
  120. if data > self.default_temperature:
  121. if data != self.return_to_default_setpoint:
  122. logger.info('Got heating setpoint (%.1f°C) \"%s\" with deviation to the default value (%.1f°C). Starting timer for returning to default.',
  123. data, self.topic, self.default_temperature)
  124. self.return_to_default_timer = self.RETURN_TO_DEFAULT_TIME
  125. self.return_to_default_setpoint = data
  126. self.gui_led_boost.set_feedback(True)
  127. else:
  128. if self.return_to_default_timer is not None:
  129. logger.info('Deleting timer \"%s\" for returning to default.', self.topic)
  130. self.return_to_default_timer = None
  131. self.return_to_default_setpoint = None
  132. self.gui_led_boost.set_feedback(False)
  133. elif device.topic == self.gui_value_temp_setp.topic:
  134. # user setpoint action
  135. logger.info('Setpoint change \"%s\" to %.1f°C', self.topic, data)
  136. self.default_temperature = data
  137. self.heating_valve.set_heating_setpoint(self.default_temperature)
  138. self.return_to_default_timer = None
  139. self.return_to_default_setpoint = None
  140. self.gui_led_boost.set_feedback(False)
  141. def boost_actions(self, davice, key, data):
  142. logger.info('Starting boost mode \"%s\" with setpoint %.1f°C.', self.topic, self.default_temperature + self.BOOST_TEMP_OFFSET)
  143. self.heating_valve.set_heating_setpoint(self.default_temperature + self.BOOST_TEMP_OFFSET)
  144. def cyclic_task(self, rt):
  145. if self.return_to_default_timer is not None:
  146. self.return_to_default_timer -= self.ct.cycle_time
  147. if self.return_to_default_timer <= 0:
  148. logger.info('Return to default timer expired \"%s\".', self.topic)
  149. self.heating_valve.set_heating_setpoint(self.default_temperature)
  150. self.return_to_default_timer = None
  151. self.return_to_default_setpoint = None
  152. self.gui_led_boost.set_feedback(False)