Smarthome Functionen
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

modules.py 14KB

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