Smarthome Functionen
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

modules.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import config
  5. import devices
  6. from function.db import get_gui_radiator_data, set_gui_radiator_data
  7. from function.rooms import room_shelly
  8. import logging
  9. import task
  10. try:
  11. from config import APP_NAME as ROOT_LOGGER_NAME
  12. except ImportError:
  13. ROOT_LOGGER_NAME = 'root'
  14. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  15. class brightness_choose_n_action(object):
  16. def __init__(self, mqtt_client, button_tradfri, topic_led):
  17. self.gui_led_active_device = devices.nodered_gui_leds(mqtt_client, topic_led)
  18. # brightness change
  19. button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
  20. devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_LONG, self.brightness_action)
  21. button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG, self.brightness_action)
  22. button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
  23. devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_RELEASE, self.brightness_action)
  24. button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION,
  25. devices.tradfri_button.ACTION_BRIGHTNESS_UP_RELEASE, self.brightness_action)
  26. # device change
  27. button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_UP, self.choose_next_device)
  28. button_tradfri.add_callback(devices.tradfri_button.KEY_ACTION, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN, self.choose_prev_device)
  29. #
  30. self.brightness_device_list = []
  31. self.callback_device_list = []
  32. self.device_states = []
  33. self.active_device_state = None
  34. self.update_active_device_led()
  35. def add(self, brightness_device, callback_device, callback_key):
  36. """
  37. brightness_device: A device for brightness function needs to have the following methods:
  38. * .default_inc()
  39. * .default_dec()
  40. * .default_stop()
  41. callback_device: A device for installing callback which are executed, when the device is switched on or off. It needs the following method:
  42. * .add_callback(key, data or None, callback, on_changes_only)
  43. """
  44. if len(self.brightness_device_list) >= len(devices.nodered_gui_leds.KEY_LED_LIST):
  45. raise ValueError("Number of devices is limited by number of leds in devices.nodered_gui_leds.")
  46. self.brightness_device_list.append(brightness_device)
  47. self.callback_device_list.append((callback_device, callback_key))
  48. self.device_states.append(False)
  49. callback_device.add_callback(callback_key, True, self.device_state_action, True)
  50. callback_device.add_callback(callback_key, False, self.device_state_action, True)
  51. def device_state_action(self, device, key, data):
  52. self.device_states[self.callback_device_list.index((device, key))] = data
  53. if data is True:
  54. self.active_device_state = self.callback_device_list.index((device, key))
  55. self.update_active_device_led()
  56. else:
  57. self.choose_next_device()
  58. def update_active_device_led(self):
  59. for i in range(0, len(self.brightness_device_list)):
  60. self.gui_led_active_device.set_led(devices.nodered_gui_leds.KEY_LED_LIST[i], self.active_device_state == i)
  61. def choose_prev_device(self, device=None, key=None, data=None):
  62. if self.active_device_state is not None:
  63. start_value = self.active_device_state
  64. for i in range(0, len(self.brightness_device_list)):
  65. target_state = (start_value - i - 1) % (len(self.brightness_device_list))
  66. if self.device_states[target_state]:
  67. self.active_device_state = target_state
  68. self.update_active_device_led()
  69. return
  70. self.active_device_state = None
  71. self.update_active_device_led()
  72. def choose_next_device(self, device=None, key=None, data=None):
  73. if self.active_device_state is not None:
  74. start_value = self.active_device_state
  75. for i in range(0, len(self.brightness_device_list)):
  76. target_state = (start_value + i + 1) % (len(self.brightness_device_list))
  77. if self.device_states[target_state]:
  78. self.active_device_state = target_state
  79. self.update_active_device_led()
  80. return
  81. self.active_device_state = None
  82. self.update_active_device_led()
  83. def brightness_action(self, device, key, data):
  84. if self.active_device_state is not None:
  85. target = self.brightness_device_list[self.active_device_state]
  86. if data == devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG:
  87. logger.info("Increasing \"%s\" - %s", type(self).__name__, target.topic)
  88. target.default_inc()
  89. elif data == devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_LONG:
  90. logger.info("Decreasing \"%s\" - %s", type(self).__name__, target.topic)
  91. target.default_dec()
  92. elif data in [devices.tradfri_button.ACTION_BRIGHTNESS_UP_RELEASE, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_RELEASE]:
  93. target.default_stop()
  94. class circulation_pump(room_shelly):
  95. def __init__(self, mqtt_client):
  96. super().__init__(mqtt_client, config.TOPIC_FFE_KITCHEN_CIRCULATION_PUMP_SHELLY, config.TOPIC_FFE_KITCHEN_CIRCULATION_PUMP_GUI)
  97. #
  98. self.main_light_shelly.add_callback(devices.shelly.KEY_OUTPUT_0, None, self.circ_pump_actions, True)
  99. #
  100. self.gui_main_light.set_timer('-')
  101. #
  102. self.ct = task.periodic(6, self.cyclic_task)
  103. self.pump_timer = None
  104. #
  105. self.ct.run()
  106. def circ_pump_actions(self, device, key, data):
  107. if data is True:
  108. self.pump_timer = 10
  109. self.gui_main_light.set_timer(self.pump_timer)
  110. else:
  111. self.pump_timer = None
  112. self.gui_main_light.set_timer('-')
  113. def cyclic_task(self, rt):
  114. if self.pump_timer is not None:
  115. if self.pump_timer <= 0:
  116. self.pump_timer = None
  117. self.gui_main_light.set_timer('-')
  118. else:
  119. self.gui_main_light.set_timer(self.pump_timer)
  120. self.pump_timer -= self.ct.cycle_time / 60
  121. class radiator_function(object):
  122. BOOST_TEMPERATURE = 30
  123. AWAY_REDUCTION = 5
  124. SUMMER_TEMPERATURE = 5
  125. def __init__(self, mqtt_client, topic_valve, topic_gui, default_temperature):
  126. self.default_temperature = default_temperature
  127. self.boost_timer = None
  128. # device initialisation
  129. self.heating_valve = devices.brennenstuhl_heatingvalve(mqtt_client, topic_valve)
  130. self.gui_heating = devices.nodered_gui_radiator(mqtt_client, topic_gui)
  131. # db-stored data initialisation
  132. db_data = get_gui_radiator_data(topic_gui)
  133. self.__away_mode__ = db_data[0] or False
  134. self.__summer_mode__ = db_data[1] or False
  135. self.__user_temperature_setpoint__ = db_data[2] or default_temperature
  136. if self.__away_mode__:
  137. self.away_mode(None, None, True)
  138. elif self.__summer_mode__:
  139. self.summer_mode(None, None, True)
  140. else:
  141. self.set_heating_setpoint(None, None, self.__user_temperature_setpoint__)
  142. # callback initialisation
  143. self.heating_valve.add_callback(devices.brennenstuhl_heatingvalve.KEY_TEMPERATURE, None, self.gui_heating.set_temperature_mcb)
  144. self.heating_valve.add_callback(devices.brennenstuhl_heatingvalve.KEY_HEATING_SETPOINT, None, self.get_radiator_setpoint)
  145. #
  146. self.gui_heating.add_callback(devices.nodered_gui_radiator.KEY_SETPOINT_TEMP, None, self.set_heating_setpoint)
  147. self.gui_heating.add_callback(devices.nodered_gui_radiator.KEY_BOOST, None, self.boost)
  148. self.gui_heating.add_callback(devices.nodered_gui_radiator.KEY_SETPOINT_TO_DEFAULT, None, self.setpoint_to_default)
  149. self.gui_heating.add_callback(devices.nodered_gui_radiator.KEY_AWAY, None, self.away_mode)
  150. self.gui_heating.add_callback(devices.nodered_gui_radiator.KEY_SUMMER, None, self.summer_mode)
  151. # cyclic task initialisation
  152. self.ct = task.periodic(1, self.cyclic_task)
  153. self.ct.run()
  154. def cyclic_task(self, rt):
  155. if self.boost_timer is not None:
  156. self.gui_heating.set_timer(round(self.boost_timer / 60, 1))
  157. #
  158. self.boost_timer -= self.ct.cycle_time
  159. if self.boost_timer <= 0:
  160. self.cancel_boost()
  161. self.heating_valve.set_heating_setpoint(self.__user_temperature_setpoint__)
  162. self.heating_valve.logger.info('Timer expired. returning to regular temperature setpoint %.1f°C.', self.__user_temperature_setpoint__)
  163. def cancel_boost(self, device=None, key=None, data=None):
  164. if self.boost_timer is not None:
  165. self.boost_timer = None
  166. self.gui_heating.set_timer('-')
  167. def update_states(self, away_mode=None, summer_mode=None, user_temperature_setpoint=None):
  168. if away_mode is not None:
  169. self.__away_mode__ = away_mode
  170. if summer_mode is not None:
  171. self.__summer_mode__ = summer_mode
  172. if user_temperature_setpoint is not None:
  173. self.__user_temperature_setpoint__ = user_temperature_setpoint
  174. set_gui_radiator_data(self.gui_heating.topic, self.__away_mode__, self.__summer_mode__, self.__user_temperature_setpoint__)
  175. #
  176. self.gui_heating.set_away(self.__away_mode__)
  177. self.gui_heating.set_summer(self.__summer_mode__)
  178. self.gui_heating.set_enable(not self.__away_mode__ and not self.__summer_mode__)
  179. def away_mode(self, device, key, value):
  180. if value is True:
  181. self.cancel_boost()
  182. self.update_states(away_mode=value, summer_mode=False)
  183. self.heating_valve.set_heating_setpoint(self.__user_temperature_setpoint__ - self.AWAY_REDUCTION)
  184. else:
  185. self.update_states(away_mode=value)
  186. self.heating_valve.set_heating_setpoint(self.__user_temperature_setpoint__)
  187. def summer_mode(self, device, key, value):
  188. if value is True:
  189. self.cancel_boost()
  190. self.update_states(away_mode=False, summer_mode=value)
  191. self.heating_valve.set_heating_setpoint(self.SUMMER_TEMPERATURE)
  192. else:
  193. self.update_states(summer_mode=value)
  194. self.heating_valve.set_heating_setpoint(self.__user_temperature_setpoint__)
  195. def boost(self, device, key, data):
  196. if self.boost_timer is None:
  197. self.heating_valve.logger.info('Starting boost mode with setpoint %.1f°C.', self.BOOST_TEMPERATURE)
  198. self.boost_timer = 15*60
  199. self.heating_valve.set_heating_setpoint(self.BOOST_TEMPERATURE)
  200. else:
  201. self.boost_timer += 15 * 60
  202. if self.boost_timer > 60 * 60:
  203. self.boost_timer = 60 * 60
  204. self.update_states(away_mode=False, summer_mode=False)
  205. def setpoint_to_default(self, device, key, data):
  206. self.set_heating_setpoint(device, key, self.default_temperature)
  207. def set_heating_setpoint(self, device, key, data):
  208. self.cancel_boost()
  209. self.update_states(away_mode=False, summer_mode=False, user_temperature_setpoint=data)
  210. self.heating_valve.set_heating_setpoint(data)
  211. def get_radiator_setpoint(self, device, key, data):
  212. if self.boost_timer is None and not self.__away_mode__ and not self.__summer_mode__:
  213. self.update_states(user_temperature_setpoint=data)
  214. else:
  215. self.update_states()
  216. self.gui_heating.set_setpoint_temperature(data)