Smarthome Functionen
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 config
  14. import devices
  15. from function.helpers import day_state
  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 switched_light(object):
  24. def __init__(self, sw_device, sw_key, li_device):
  25. sw_device.add_callback(devices.shelly.KEY_OUTPUT_0, True, li_device.request_data, True)
  26. class brightness_choose_n_action(common_base):
  27. KEY_ACTIVE_DEVICE = 'active_device'
  28. #
  29. DEFAULT_VALUES = {KEY_ACTIVE_DEVICE: None}
  30. def __init__(self, button_tradfri):
  31. super().__init__()
  32. # brightness change
  33. button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_DOWN_LONG, self.brightness_action)
  34. button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_UP_LONG, self.brightness_action)
  35. button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_DOWN_RELEASE, self.brightness_action)
  36. button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_UP_RELEASE, self.brightness_action)
  37. # device change
  38. button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_UP, self.choose_next_device)
  39. button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_DOWN, self.choose_prev_device)
  40. #
  41. self.brightness_device_list = []
  42. self.callback_device_list = []
  43. self.device_states = []
  44. def add(self, brightness_device, callback_device, callback_key):
  45. """
  46. brightness_device: A device for brightness function needs to have the following methods:
  47. * .default_inc()
  48. * .default_dec()
  49. * .default_stop()
  50. callback_device: A device for installing callback which are executed, when the device is switched on or off. It needs the following method:
  51. * .add_callback(key, data or None, callback, on_changes_only)
  52. """
  53. self.brightness_device_list.append(brightness_device)
  54. self.callback_device_list.append((callback_device, callback_key))
  55. self.device_states.append(False)
  56. callback_device.add_callback(callback_key, True, self.device_state_action, True)
  57. callback_device.add_callback(callback_key, False, self.device_state_action, True)
  58. def device_state_action(self, device, key, data):
  59. self.device_states[self.callback_device_list.index((device, key))] = data
  60. if data is True:
  61. self.set(self.KEY_ACTIVE_DEVICE, self.callback_device_list.index((device, key)))
  62. else:
  63. if self[self.KEY_ACTIVE_DEVICE] is not None:
  64. if self.callback_device_list[self[self.KEY_ACTIVE_DEVICE]][0] == device:
  65. self.choose_next_device()
  66. def choose_prev_device(self, device=None, key=None, data=None):
  67. if self[self.KEY_ACTIVE_DEVICE] is not None:
  68. start_value = self[self.KEY_ACTIVE_DEVICE]
  69. for i in range(0, len(self.brightness_device_list)):
  70. target_state = (start_value - i - 1) % (len(self.brightness_device_list))
  71. if self.device_states[target_state]:
  72. self.set(self.KEY_ACTIVE_DEVICE, target_state)
  73. return
  74. self.set(self.KEY_ACTIVE_DEVICE, None)
  75. def choose_next_device(self, device=None, key=None, data=None):
  76. if self[self.KEY_ACTIVE_DEVICE] is not None:
  77. start_value = self[self.KEY_ACTIVE_DEVICE]
  78. for i in range(0, len(self.brightness_device_list)):
  79. target_state = (start_value + i + 1) % (len(self.brightness_device_list))
  80. if self.device_states[target_state]:
  81. self.set(self.KEY_ACTIVE_DEVICE, target_state)
  82. return
  83. self.set(self.KEY_ACTIVE_DEVICE, None)
  84. def brightness_action(self, device, key, data):
  85. if self[self.KEY_ACTIVE_DEVICE] is not None:
  86. target = self.brightness_device_list[self[self.KEY_ACTIVE_DEVICE]]
  87. if data == devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG:
  88. logger.info("Increasing \"%s\" - %s", type(self).__name__, target.topic)
  89. target.default_inc()
  90. elif data == devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_LONG:
  91. logger.info("Decreasing \"%s\" - %s", type(self).__name__, target.topic)
  92. target.default_dec()
  93. elif data in [devices.tradfri_button.ACTION_BRIGHTNESS_UP_RELEASE, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_RELEASE]:
  94. target.default_stop()
  95. class timer_on_activation(common_base):
  96. KEY_TIMER = 'timer'
  97. #
  98. DEFAULT_VALUES = {
  99. KEY_TIMER: 0
  100. }
  101. def __init__(self, sw_device, sw_key, timer_reload_value):
  102. super().__init__()
  103. #
  104. self.timer_reload_value = timer_reload_value
  105. #
  106. sw_device.add_callback(sw_key, None, self.circ_pump_actions, True)
  107. #
  108. self.ct = task.periodic(6, self.cyclic_task)
  109. self.ct.run()
  110. def circ_pump_actions(self, device, key, data):
  111. if data is True:
  112. self.set(self.KEY_TIMER, self.timer_reload_value)
  113. else:
  114. self.set(self.KEY_TIMER, 0)
  115. def cyclic_task(self, rt):
  116. timer_value = self[self.KEY_TIMER] - self.ct.cycle_time
  117. if timer_value <= 0:
  118. self.set(self.KEY_TIMER, 0)
  119. else:
  120. self.set(self.KEY_TIMER, timer_value)
  121. class heating_function(common_base):
  122. KEY_USER_TEMPERATURE_SETPOINT = 'user_temperature_setpoint'
  123. KEY_TEMPERATURE_SETPOINT = 'temperature_setpoint'
  124. KEY_TEMPERATURE_CURRENT = 'temperature_current'
  125. KEY_AWAY_MODE = 'away_mode'
  126. KEY_SUMMER_MODE = 'summer_mode'
  127. KEY_START_BOOST = 'start_boost'
  128. KEY_SET_DEFAULT_TEMPERATURE = 'set_default_temperature'
  129. KEY_BOOST_TIMER = 'boost_timer'
  130. #
  131. BOOST_TEMPERATURE = 30
  132. AWAY_REDUCTION = 5
  133. SUMMER_TEMPERATURE = 5
  134. def __init__(self, heating_valve, default_temperature, **kwargs):
  135. self.heating_valve = heating_valve
  136. self.default_temperature = default_temperature
  137. #
  138. super().__init__({
  139. self.KEY_USER_TEMPERATURE_SETPOINT: kwargs.get(self.KEY_USER_TEMPERATURE_SETPOINT, self.default_temperature),
  140. self.KEY_TEMPERATURE_SETPOINT: kwargs.get(self.KEY_TEMPERATURE_SETPOINT, self.default_temperature),
  141. self.KEY_TEMPERATURE_CURRENT: kwargs.get(self.KEY_TEMPERATURE_CURRENT, None),
  142. self.KEY_AWAY_MODE: kwargs.get(self.KEY_AWAY_MODE, False),
  143. self.KEY_SUMMER_MODE: kwargs.get(self.KEY_SUMMER_MODE, False),
  144. self.KEY_START_BOOST: kwargs.get(self.KEY_START_BOOST, True),
  145. self.KEY_SET_DEFAULT_TEMPERATURE: kwargs.get(self.KEY_SET_DEFAULT_TEMPERATURE, False),
  146. self.KEY_BOOST_TIMER: kwargs.get(self.KEY_BOOST_TIMER, 0)
  147. })
  148. #
  149. self.heating_valve.set_heating_setpoint(self[self.KEY_TEMPERATURE_SETPOINT])
  150. #
  151. self.heating_valve.add_callback(self.heating_valve.KEY_HEATING_SETPOINT, None, self.get_radiator_setpoint)
  152. self.heating_valve.add_callback(self.heating_valve.KEY_TEMPERATURE, None, self.get_radiator_temperature)
  153. #
  154. self.add_callback(self.KEY_USER_TEMPERATURE_SETPOINT, None, self.user_temperature_setpoint, False)
  155. self.add_callback(self.KEY_TEMPERATURE_SETPOINT, None, self.set_heating_setpoint, True)
  156. self.add_callback(self.KEY_AWAY_MODE, None, self.away_mode, True)
  157. self.add_callback(self.KEY_SUMMER_MODE, None, self.summer_mode, True)
  158. self.add_callback(self.KEY_SET_DEFAULT_TEMPERATURE, None, self.setpoint_to_default)
  159. self.add_callback(self.KEY_START_BOOST, True, self.boost, False)
  160. self.add_callback(self.KEY_BOOST_TIMER, 0, self.timer_expired, True)
  161. # cyclic task initialisation
  162. self.ct = task.periodic(1, self.cyclic_task)
  163. self.ct.run()
  164. def timer_expired(self, device, data, key):
  165. self.set(self.KEY_TEMPERATURE_SETPOINT, self[self.KEY_USER_TEMPERATURE_SETPOINT])
  166. self.heating_valve.logger.info('Timer expired. returning to regular temperature setpoint %.1f°C.',
  167. self[self.KEY_TEMPERATURE_SETPOINT])
  168. def cyclic_task(self, rt):
  169. timer_value = self[self.KEY_BOOST_TIMER] - self.ct.cycle_time
  170. if self[self.KEY_BOOST_TIMER] <= 0:
  171. self.set(self.KEY_BOOST_TIMER, 0)
  172. else:
  173. self.set(self.KEY_BOOST_TIMER, timer_value)
  174. def cancel_boost(self):
  175. self.set(self.KEY_BOOST_TIMER, 0, block_callback=[self.timer_expired])
  176. def send_command(self, key, data, block_callback=[]):
  177. return super().set(key, data, block_callback)
  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.motion_sensors = 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.motion_sensors):
  257. if arg_device.topic == device.topic:
  258. break
  259. self.set(self.KEY_MOTION_SENSOR % sensor_index, data)
  260. # auto light on with state sunset -> time_offset_sunrise=60 (longer sunset) and time_offset_sunset=-60 (longer sunset)
  261. if day_state(None, None, 60, -60).get_state() == day_state.KEY_SUNSET:
  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.motion_sensors)):
  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. if self[self.KEY_TIMER] != 0:
  276. self.set(self.KEY_TIMER, max(min_value, self[self.KEY_TIMER] - cyclic_task.cycle_time))