Smarthome Functionen
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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.callback_device_list[self[self.KEY_ACTIVE_DEVICE]][0] == device:
  63. self.choose_next_device()
  64. def choose_prev_device(self, device=None, key=None, data=None):
  65. if self[self.KEY_ACTIVE_DEVICE] is not None:
  66. start_value = self[self.KEY_ACTIVE_DEVICE]
  67. for i in range(0, len(self.brightness_device_list)):
  68. target_state = (start_value - i - 1) % (len(self.brightness_device_list))
  69. if self.device_states[target_state]:
  70. self.set(self.KEY_ACTIVE_DEVICE, target_state)
  71. return
  72. self.set(self.KEY_ACTIVE_DEVICE, None)
  73. def choose_next_device(self, device=None, key=None, data=None):
  74. if self[self.KEY_ACTIVE_DEVICE] is not None:
  75. start_value = self[self.KEY_ACTIVE_DEVICE]
  76. for i in range(0, len(self.brightness_device_list)):
  77. target_state = (start_value + i + 1) % (len(self.brightness_device_list))
  78. if self.device_states[target_state]:
  79. self.set(self.KEY_ACTIVE_DEVICE, target_state)
  80. return
  81. self.set(self.KEY_ACTIVE_DEVICE, None)
  82. def brightness_action(self, device, key, data):
  83. if self[self.KEY_ACTIVE_DEVICE] is not None:
  84. target = self.brightness_device_list[self[self.KEY_ACTIVE_DEVICE]]
  85. if data == devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG:
  86. logger.info("Increasing \"%s\" - %s", type(self).__name__, target.topic)
  87. target.default_inc()
  88. elif data == devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_LONG:
  89. logger.info("Decreasing \"%s\" - %s", type(self).__name__, target.topic)
  90. target.default_dec()
  91. elif data in [devices.tradfri_button.ACTION_BRIGHTNESS_UP_RELEASE, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_RELEASE]:
  92. target.default_stop()
  93. class timer_on_activation(common_base):
  94. KEY_TIMER = 'timer'
  95. #
  96. DEFAULT_VALUES = {
  97. KEY_TIMER: 0
  98. }
  99. def __init__(self, sw_device, sw_key, timer_reload_value):
  100. super().__init__()
  101. #
  102. self.timer_reload_value = timer_reload_value
  103. #
  104. sw_device.add_callback(sw_key, None, self.circ_pump_actions, True)
  105. #
  106. self.ct = task.periodic(6, self.cyclic_task)
  107. self.ct.run()
  108. def circ_pump_actions(self, device, key, data):
  109. if data is True:
  110. self.set(self.KEY_TIMER, self.timer_reload_value)
  111. else:
  112. self.set(self.KEY_TIMER, 0)
  113. def cyclic_task(self, rt):
  114. timer_value = self[self.KEY_TIMER] - self.ct.cycle_time
  115. if timer_value <= 0:
  116. self.set(self.KEY_TIMER, 0)
  117. else:
  118. self.set(self.KEY_TIMER, timer_value)
  119. class heating_function(common_base):
  120. KEY_USER_TEMPERATURE_SETPOINT = 'user_temperature_setpoint'
  121. KEY_TEMPERATURE_SETPOINT = 'temperature_setpoint'
  122. KEY_TEMPERATURE_CURRENT = 'temperature_current'
  123. KEY_AWAY_MODE = 'away_mode'
  124. KEY_SUMMER_MODE = 'summer_mode'
  125. KEY_START_BOOST = 'start_boost'
  126. KEY_SET_DEFAULT_TEMPERATURE = 'set_default_temperature'
  127. KEY_BOOST_TIMER = 'boost_timer'
  128. #
  129. BOOST_TEMPERATURE = 30
  130. AWAY_REDUCTION = 5
  131. SUMMER_TEMPERATURE = 5
  132. def __init__(self, heating_valve, default_temperature):
  133. db_data = get_radiator_data(heating_valve.topic)
  134. super().__init__({
  135. self.KEY_USER_TEMPERATURE_SETPOINT: db_data[2] or default_temperature,
  136. self.KEY_TEMPERATURE_SETPOINT: db_data[3] or default_temperature,
  137. self.KEY_TEMPERATURE_CURRENT: None,
  138. self.KEY_AWAY_MODE: db_data[0] or False,
  139. self.KEY_SUMMER_MODE: db_data[1] or False,
  140. self.KEY_START_BOOST: True,
  141. self.KEY_SET_DEFAULT_TEMPERATURE: False,
  142. self.KEY_BOOST_TIMER: 0
  143. })
  144. #
  145. self.default_temperature = default_temperature
  146. self.heating_valve = heating_valve
  147. self.heating_valve.set_heating_setpoint(self[self.KEY_TEMPERATURE_SETPOINT])
  148. #
  149. self.heating_valve.add_callback(self.heating_valve.KEY_HEATING_SETPOINT, None, self.get_radiator_setpoint)
  150. self.heating_valve.add_callback(self.heating_valve.KEY_TEMPERATURE, None, self.get_radiator_temperature)
  151. #
  152. self.add_callback(self.KEY_USER_TEMPERATURE_SETPOINT, None, self.user_temperature_setpoint, False)
  153. self.add_callback(self.KEY_TEMPERATURE_SETPOINT, None, self.set_heating_setpoint, True)
  154. self.add_callback(self.KEY_AWAY_MODE, None, self.away_mode, True)
  155. self.add_callback(self.KEY_SUMMER_MODE, None, self.summer_mode, True)
  156. self.add_callback(self.KEY_SET_DEFAULT_TEMPERATURE, None, self.setpoint_to_default)
  157. self.add_callback(self.KEY_START_BOOST, True, self.boost, False)
  158. self.add_callback(self.KEY_BOOST_TIMER, 0, self.timer_expired, True)
  159. # cyclic task initialisation
  160. self.ct = task.periodic(1, self.cyclic_task)
  161. self.ct.run()
  162. def timer_expired(self, device, data, key):
  163. self.set(self.KEY_TEMPERATURE_SETPOINT, self[self.KEY_USER_TEMPERATURE_SETPOINT])
  164. self.heating_valve.logger.info('Timer expired. returning to regular temperature setpoint %.1f°C.',
  165. self[self.KEY_TEMPERATURE_SETPOINT])
  166. def cyclic_task(self, rt):
  167. timer_value = self[self.KEY_BOOST_TIMER] - self.ct.cycle_time
  168. if self[self.KEY_BOOST_TIMER] <= 0:
  169. self.set(self.KEY_BOOST_TIMER, 0)
  170. else:
  171. self.set(self.KEY_BOOST_TIMER, timer_value)
  172. def cancel_boost(self):
  173. self.set(self.KEY_BOOST_TIMER, 0, block_callback=[self.timer_expired])
  174. def set(self, key, data, block_callback=[]):
  175. rv = super().set(key, data, block_callback)
  176. set_radiator_data(self.heating_valve.topic, self[self.KEY_AWAY_MODE], self[self.KEY_SUMMER_MODE],
  177. self[self.KEY_USER_TEMPERATURE_SETPOINT], self[self.KEY_TEMPERATURE_SETPOINT])
  178. return rv
  179. def away_mode(self, device, key, value):
  180. if value is True:
  181. self.cancel_boost()
  182. self.set(self.KEY_SUMMER_MODE, False, [self.summer_mode])
  183. self.set(self.KEY_TEMPERATURE_SETPOINT, self[self.KEY_USER_TEMPERATURE_SETPOINT] - self.AWAY_REDUCTION)
  184. else:
  185. self.set(self.KEY_TEMPERATURE_SETPOINT, self[self.KEY_USER_TEMPERATURE_SETPOINT])
  186. def summer_mode(self, device, key, value):
  187. if value is True:
  188. self.cancel_boost()
  189. self.set(self.KEY_AWAY_MODE, False, [self.away_mode])
  190. self.set(self.KEY_TEMPERATURE_SETPOINT, self.SUMMER_TEMPERATURE)
  191. else:
  192. self.set(self.KEY_TEMPERATURE_SETPOINT, self[self.KEY_USER_TEMPERATURE_SETPOINT])
  193. def boost(self, device, key, data):
  194. if self[self.KEY_BOOST_TIMER] == 0:
  195. self.heating_valve.logger.info('Starting boost mode with setpoint %.1f°C.', self.BOOST_TEMPERATURE)
  196. self.set(self.KEY_BOOST_TIMER, 15*60)
  197. self.set(self.KEY_TEMPERATURE_SETPOINT, self.BOOST_TEMPERATURE)
  198. else:
  199. self.set(self.KEY_BOOST_TIMER, min(self[self.KEY_BOOST_TIMER] + 15 * 60, 60 * 60))
  200. self.set(self.KEY_AWAY_MODE, False, [self.away_mode])
  201. self.set(self.KEY_SUMMER_MODE, False, [self.summer_mode])
  202. def setpoint_to_default(self, device, key, data):
  203. self.cancel_boost()
  204. self.set(self.KEY_AWAY_MODE, False, [self.away_mode])
  205. self.set(self.KEY_SUMMER_MODE, False, [self.summer_mode])
  206. self.set(self.KEY_USER_TEMPERATURE_SETPOINT, self.default_temperature, [self.user_temperature_setpoint])
  207. self.set(self.KEY_TEMPERATURE_SETPOINT, self.default_temperature)
  208. def user_temperature_setpoint(self, device, key, data):
  209. self.cancel_boost()
  210. self.set(self.KEY_AWAY_MODE, False, [self.away_mode])
  211. self.set(self.KEY_SUMMER_MODE, False, [self.summer_mode])
  212. self.set(self.KEY_TEMPERATURE_SETPOINT, data)
  213. def set_heating_setpoint(self, device, key, data):
  214. self.heating_valve.set_heating_setpoint(data)
  215. def get_radiator_setpoint(self, device, key, data):
  216. if self[self.KEY_BOOST_TIMER] == 0 and not self[self.KEY_AWAY_MODE] and not self[self.KEY_SUMMER_MODE]:
  217. self.set(self.KEY_USER_TEMPERATURE_SETPOINT, data, block_callback=[self.set_heating_setpoint])
  218. def get_radiator_temperature(self, device, key, data):
  219. self.set(self.KEY_TEMPERATURE_CURRENT, data)
  220. class motion_sensor_light(common_base):
  221. KEY_TIMER = 'timer'
  222. KEY_MOTION_SENSOR = 'motion_%d'
  223. KEY_MOTION_SENSOR_0 = 'motion_%d' % 0
  224. KEY_MOTION_SENSOR_1 = 'motion_%d' % 1
  225. KEY_MOTION_SENSOR_2 = 'motion_%d' % 2
  226. KEY_MOTION_SENSOR_3 = 'motion_%d' % 3
  227. KEY_MOTION_SENSOR_4 = 'motion_%d' % 4
  228. def __init__(self, sw_device, sw_method, *args, timer_value=30):
  229. """
  230. sw_device is the device switching the light, args are 0-n motion sensors
  231. """
  232. dv = dict.fromkeys([self.KEY_MOTION_SENSOR % i for i in range(0, len(args))])
  233. for key in dv:
  234. dv[key] = False
  235. dv[self.KEY_TIMER] = 0
  236. super().__init__(default_values=dv)
  237. #
  238. self.sw_device = sw_device
  239. self.sw_method = sw_method
  240. self.args = args
  241. self.timer_reload_value = timer_value
  242. #
  243. sw_device.add_callback(devices.shelly.KEY_OUTPUT_0, True, self.reload_timer, True)
  244. sw_device.add_callback(devices.shelly.KEY_OUTPUT_0, False, self.reset_timer, True)
  245. for motion_sensor in args:
  246. motion_sensor.add_callback(motion_sensor.KEY_OCCUPANCY, None, self.set_motion_detected, True)
  247. #
  248. self.add_callback(self.KEY_TIMER, 0, self.timer_expired, True)
  249. #
  250. cyclic_task = task.periodic(1, self.cyclic_task)
  251. cyclic_task.run()
  252. def reload_timer(self, device, key, data):
  253. self.set(self.KEY_TIMER, self.timer_reload_value)
  254. def reset_timer(self, device=None, key=None, data=None):
  255. self.set(self.KEY_TIMER, 0)
  256. def set_motion_detected(self, device, key, data):
  257. for sensor_index, arg_device in enumerate(self.args):
  258. if arg_device.topic == device.topic:
  259. break
  260. self.set(self.KEY_MOTION_SENSOR % sensor_index, data)
  261. if now() < sunrise_time(60) or now() > sunset_time(-60):
  262. if data is True:
  263. logger.info("%s: Motion detected - Switching on main light %s", device.topic, self.sw_device.topic)
  264. self.sw_method(True)
  265. def motion_detected(self):
  266. for i in range(0, len(self.args)):
  267. if self[self.KEY_MOTION_SENSOR % i]:
  268. return True
  269. return False
  270. def timer_expired(self, device, key, data):
  271. logger.info("No motion and time ran out - Switching off main light %s", self.sw_device.topic)
  272. self.sw_method(False)
  273. def cyclic_task(self, cyclic_task):
  274. min_value = 10 if self.motion_detected() else 0
  275. self.set(self.KEY_TIMER, max(min_value, self[self.KEY_TIMER] - cyclic_task.cycle_time))