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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. Functional Modules
  6. Targets:
  7. * Device like structure to be compatible with videv
  8. - KEY_* as part of the class for all parameters which needs to be accessed from videv
  9. - Method *.set(key, data) to pass data from videv to Module
  10. - Method .add_calback(key, data, callback, on_change_only=False) to register videv actualisation on changes
  11. """
  12. from base import common_base
  13. import devices
  14. from function.db import get_radiator_data, set_radiator_data
  15. from function.helpers import now, sunset_time, sunrise_time
  16. import logging
  17. import task
  18. try:
  19. from config import APP_NAME as ROOT_LOGGER_NAME
  20. except ImportError:
  21. ROOT_LOGGER_NAME = 'root'
  22. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  23. class brightness_choose_n_action(common_base):
  24. KEY_ACTIVE_DEVICE = 'active_device'
  25. #
  26. DEFAULT_VALUES = {KEY_ACTIVE_DEVICE: None}
  27. def __init__(self, button_tradfri):
  28. super().__init__()
  29. # brightness change
  30. button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_DOWN_LONG, self.brightness_action)
  31. button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_UP_LONG, self.brightness_action)
  32. button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_DOWN_RELEASE, self.brightness_action)
  33. button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_UP_RELEASE, self.brightness_action)
  34. # device change
  35. button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_UP, self.choose_next_device)
  36. button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_DOWN, self.choose_prev_device)
  37. #
  38. self.brightness_device_list = []
  39. self.callback_device_list = []
  40. self.device_states = []
  41. def add(self, brightness_device, callback_device, callback_key):
  42. """
  43. brightness_device: A device for brightness function needs to have the following methods:
  44. * .default_inc()
  45. * .default_dec()
  46. * .default_stop()
  47. callback_device: A device for installing callback which are executed, when the device is switched on or off. It needs the following method:
  48. * .add_callback(key, data or None, callback, on_changes_only)
  49. """
  50. if len(self.brightness_device_list) >= len(devices.nodered_gui_leds.KEY_LED_LIST):
  51. raise ValueError("Number of devices is limited by number of leds in devices.nodered_gui_leds.")
  52. self.brightness_device_list.append(brightness_device)
  53. self.callback_device_list.append((callback_device, callback_key))
  54. self.device_states.append(False)
  55. callback_device.add_callback(callback_key, True, self.device_state_action, True)
  56. callback_device.add_callback(callback_key, False, self.device_state_action, True)
  57. def device_state_action(self, device, key, data):
  58. self.device_states[self.callback_device_list.index((device, key))] = data
  59. if data is True:
  60. self.set(self.KEY_ACTIVE_DEVICE, self.callback_device_list.index((device, key)))
  61. else:
  62. if self[self.KEY_ACTIVE_DEVICE] is not None:
  63. if self.callback_device_list[self[self.KEY_ACTIVE_DEVICE]][0] == device:
  64. self.choose_next_device()
  65. def choose_prev_device(self, device=None, key=None, data=None):
  66. if self[self.KEY_ACTIVE_DEVICE] is not None:
  67. start_value = self[self.KEY_ACTIVE_DEVICE]
  68. for i in range(0, len(self.brightness_device_list)):
  69. target_state = (start_value - i - 1) % (len(self.brightness_device_list))
  70. if self.device_states[target_state]:
  71. self.set(self.KEY_ACTIVE_DEVICE, target_state)
  72. return
  73. self.set(self.KEY_ACTIVE_DEVICE, None)
  74. def choose_next_device(self, device=None, key=None, data=None):
  75. if self[self.KEY_ACTIVE_DEVICE] is not None:
  76. start_value = self[self.KEY_ACTIVE_DEVICE]
  77. for i in range(0, len(self.brightness_device_list)):
  78. target_state = (start_value + i + 1) % (len(self.brightness_device_list))
  79. if self.device_states[target_state]:
  80. self.set(self.KEY_ACTIVE_DEVICE, target_state)
  81. return
  82. self.set(self.KEY_ACTIVE_DEVICE, None)
  83. def brightness_action(self, device, key, data):
  84. if self[self.KEY_ACTIVE_DEVICE] is not None:
  85. target = self.brightness_device_list[self[self.KEY_ACTIVE_DEVICE]]
  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 timer_on_activation(common_base):
  95. KEY_TIMER = 'timer'
  96. #
  97. DEFAULT_VALUES = {
  98. KEY_TIMER: 0
  99. }
  100. def __init__(self, sw_device, sw_key, timer_reload_value):
  101. super().__init__()
  102. #
  103. self.timer_reload_value = timer_reload_value
  104. #
  105. sw_device.add_callback(sw_key, None, self.circ_pump_actions, True)
  106. #
  107. self.ct = task.periodic(6, self.cyclic_task)
  108. self.ct.run()
  109. def circ_pump_actions(self, device, key, data):
  110. if data is True:
  111. self.set(self.KEY_TIMER, self.timer_reload_value)
  112. else:
  113. self.set(self.KEY_TIMER, 0)
  114. def cyclic_task(self, rt):
  115. timer_value = self[self.KEY_TIMER] - self.ct.cycle_time
  116. if timer_value <= 0:
  117. self.set(self.KEY_TIMER, 0)
  118. else:
  119. self.set(self.KEY_TIMER, timer_value)
  120. class heating_function(common_base):
  121. KEY_USER_TEMPERATURE_SETPOINT = 'user_temperature_setpoint'
  122. KEY_TEMPERATURE_SETPOINT = 'temperature_setpoint'
  123. KEY_TEMPERATURE_CURRENT = 'temperature_current'
  124. KEY_AWAY_MODE = 'away_mode'
  125. KEY_SUMMER_MODE = 'summer_mode'
  126. KEY_START_BOOST = 'start_boost'
  127. KEY_SET_DEFAULT_TEMPERATURE = 'set_default_temperature'
  128. KEY_BOOST_TIMER = 'boost_timer'
  129. #
  130. BOOST_TEMPERATURE = 30
  131. AWAY_REDUCTION = 5
  132. SUMMER_TEMPERATURE = 5
  133. def __init__(self, heating_valve, default_temperature):
  134. db_data = get_radiator_data(heating_valve.topic)
  135. super().__init__({
  136. self.KEY_USER_TEMPERATURE_SETPOINT: db_data[2] or default_temperature,
  137. self.KEY_TEMPERATURE_SETPOINT: db_data[3] or default_temperature,
  138. self.KEY_TEMPERATURE_CURRENT: None,
  139. self.KEY_AWAY_MODE: db_data[0] or False,
  140. self.KEY_SUMMER_MODE: db_data[1] or False,
  141. self.KEY_START_BOOST: True,
  142. self.KEY_SET_DEFAULT_TEMPERATURE: False,
  143. self.KEY_BOOST_TIMER: 0
  144. })
  145. #
  146. self.default_temperature = default_temperature
  147. self.heating_valve = heating_valve
  148. self.heating_valve.set_heating_setpoint(self[self.KEY_TEMPERATURE_SETPOINT])
  149. #
  150. self.heating_valve.add_callback(self.heating_valve.KEY_HEATING_SETPOINT, None, self.get_radiator_setpoint)
  151. self.heating_valve.add_callback(self.heating_valve.KEY_TEMPERATURE, None, self.get_radiator_temperature)
  152. #
  153. self.add_callback(self.KEY_USER_TEMPERATURE_SETPOINT, None, self.user_temperature_setpoint, False)
  154. self.add_callback(self.KEY_TEMPERATURE_SETPOINT, None, self.set_heating_setpoint, True)
  155. self.add_callback(self.KEY_AWAY_MODE, None, self.away_mode, True)
  156. self.add_callback(self.KEY_SUMMER_MODE, None, self.summer_mode, True)
  157. self.add_callback(self.KEY_SET_DEFAULT_TEMPERATURE, None, self.setpoint_to_default)
  158. self.add_callback(self.KEY_START_BOOST, True, self.boost, False)
  159. self.add_callback(self.KEY_BOOST_TIMER, 0, self.timer_expired, True)
  160. # cyclic task initialisation
  161. self.ct = task.periodic(1, self.cyclic_task)
  162. self.ct.run()
  163. def timer_expired(self, device, data, key):
  164. self.set(self.KEY_TEMPERATURE_SETPOINT, self[self.KEY_USER_TEMPERATURE_SETPOINT])
  165. self.heating_valve.logger.info('Timer expired. returning to regular temperature setpoint %.1f°C.',
  166. self[self.KEY_TEMPERATURE_SETPOINT])
  167. def cyclic_task(self, rt):
  168. timer_value = self[self.KEY_BOOST_TIMER] - self.ct.cycle_time
  169. if self[self.KEY_BOOST_TIMER] <= 0:
  170. self.set(self.KEY_BOOST_TIMER, 0)
  171. else:
  172. self.set(self.KEY_BOOST_TIMER, timer_value)
  173. def cancel_boost(self):
  174. self.set(self.KEY_BOOST_TIMER, 0, block_callback=[self.timer_expired])
  175. def set(self, key, data, block_callback=[]):
  176. rv = super().set(key, data, block_callback)
  177. set_radiator_data(self.heating_valve.topic, self[self.KEY_AWAY_MODE], self[self.KEY_SUMMER_MODE],
  178. self[self.KEY_USER_TEMPERATURE_SETPOINT], self[self.KEY_TEMPERATURE_SETPOINT])
  179. return rv
  180. def away_mode(self, device, key, value):
  181. if value is True:
  182. self.cancel_boost()
  183. self.set(self.KEY_SUMMER_MODE, False, [self.summer_mode])
  184. self.set(self.KEY_TEMPERATURE_SETPOINT, self[self.KEY_USER_TEMPERATURE_SETPOINT] - self.AWAY_REDUCTION)
  185. else:
  186. self.set(self.KEY_TEMPERATURE_SETPOINT, self[self.KEY_USER_TEMPERATURE_SETPOINT])
  187. def summer_mode(self, device, key, value):
  188. if value is True:
  189. self.cancel_boost()
  190. self.set(self.KEY_AWAY_MODE, False, [self.away_mode])
  191. self.set(self.KEY_TEMPERATURE_SETPOINT, self.SUMMER_TEMPERATURE)
  192. else:
  193. self.set(self.KEY_TEMPERATURE_SETPOINT, self[self.KEY_USER_TEMPERATURE_SETPOINT])
  194. def boost(self, device, key, data):
  195. if self[self.KEY_BOOST_TIMER] == 0:
  196. self.heating_valve.logger.info('Starting boost mode with setpoint %.1f°C.', self.BOOST_TEMPERATURE)
  197. self.set(self.KEY_BOOST_TIMER, 15*60)
  198. self.set(self.KEY_TEMPERATURE_SETPOINT, self.BOOST_TEMPERATURE)
  199. else:
  200. self.set(self.KEY_BOOST_TIMER, min(self[self.KEY_BOOST_TIMER] + 15 * 60, 60 * 60))
  201. self.set(self.KEY_AWAY_MODE, False, [self.away_mode])
  202. self.set(self.KEY_SUMMER_MODE, False, [self.summer_mode])
  203. def setpoint_to_default(self, device, key, data):
  204. self.cancel_boost()
  205. self.set(self.KEY_AWAY_MODE, False, [self.away_mode])
  206. self.set(self.KEY_SUMMER_MODE, False, [self.summer_mode])
  207. self.set(self.KEY_USER_TEMPERATURE_SETPOINT, self.default_temperature, [self.user_temperature_setpoint])
  208. self.set(self.KEY_TEMPERATURE_SETPOINT, self.default_temperature)
  209. def user_temperature_setpoint(self, device, key, data):
  210. self.cancel_boost()
  211. self.set(self.KEY_AWAY_MODE, False, [self.away_mode])
  212. self.set(self.KEY_SUMMER_MODE, False, [self.summer_mode])
  213. self.set(self.KEY_TEMPERATURE_SETPOINT, data)
  214. def set_heating_setpoint(self, device, key, data):
  215. self.heating_valve.set_heating_setpoint(data)
  216. def get_radiator_setpoint(self, device, key, data):
  217. if self[self.KEY_BOOST_TIMER] == 0 and not self[self.KEY_AWAY_MODE] and not self[self.KEY_SUMMER_MODE]:
  218. self.set(self.KEY_USER_TEMPERATURE_SETPOINT, data, block_callback=[self.set_heating_setpoint])
  219. def get_radiator_temperature(self, device, key, data):
  220. self.set(self.KEY_TEMPERATURE_CURRENT, data)
  221. class motion_sensor_light(common_base):
  222. KEY_TIMER = 'timer'
  223. KEY_MOTION_SENSOR = 'motion_%d'
  224. KEY_MOTION_SENSOR_0 = 'motion_%d' % 0
  225. KEY_MOTION_SENSOR_1 = 'motion_%d' % 1
  226. KEY_MOTION_SENSOR_2 = 'motion_%d' % 2
  227. KEY_MOTION_SENSOR_3 = 'motion_%d' % 3
  228. KEY_MOTION_SENSOR_4 = 'motion_%d' % 4
  229. def __init__(self, sw_device, sw_method, *args, timer_value=30):
  230. """
  231. sw_device is the device switching the light, args are 0-n motion sensors
  232. """
  233. dv = dict.fromkeys([self.KEY_MOTION_SENSOR % i for i in range(0, len(args))])
  234. for key in dv:
  235. dv[key] = False
  236. dv[self.KEY_TIMER] = 0
  237. super().__init__(default_values=dv)
  238. #
  239. self.sw_device = sw_device
  240. self.sw_method = sw_method
  241. self.args = args
  242. self.timer_reload_value = timer_value
  243. #
  244. sw_device.add_callback(devices.shelly.KEY_OUTPUT_0, True, self.reload_timer, True)
  245. sw_device.add_callback(devices.shelly.KEY_OUTPUT_0, False, self.reset_timer, True)
  246. for motion_sensor in args:
  247. motion_sensor.add_callback(motion_sensor.KEY_OCCUPANCY, None, self.set_motion_detected, True)
  248. #
  249. self.add_callback(self.KEY_TIMER, 0, self.timer_expired, True)
  250. #
  251. cyclic_task = task.periodic(1, self.cyclic_task)
  252. cyclic_task.run()
  253. def reload_timer(self, device, key, data):
  254. self.set(self.KEY_TIMER, self.timer_reload_value)
  255. def reset_timer(self, device=None, key=None, data=None):
  256. self.set(self.KEY_TIMER, 0)
  257. def set_motion_detected(self, device, key, data):
  258. for sensor_index, arg_device in enumerate(self.args):
  259. if arg_device.topic == device.topic:
  260. break
  261. self.set(self.KEY_MOTION_SENSOR % sensor_index, data)
  262. if now() < sunrise_time(60) or now() > sunset_time(-60):
  263. if data is True:
  264. logger.info("%s: Motion detected - Switching on main light %s", device.topic, self.sw_device.topic)
  265. self.sw_method(True)
  266. def motion_detected(self):
  267. for i in range(0, len(self.args)):
  268. if self[self.KEY_MOTION_SENSOR % i]:
  269. return True
  270. return False
  271. def timer_expired(self, device, key, data):
  272. logger.info("No motion and time ran out - Switching off main light %s", self.sw_device.topic)
  273. self.sw_method(False)
  274. def cyclic_task(self, cyclic_task):
  275. min_value = 10 if self.motion_detected() else 0
  276. if self[self.KEY_TIMER] != 0:
  277. self.set(self.KEY_TIMER, max(min_value, self[self.KEY_TIMER] - cyclic_task.cycle_time))