Smarthome Functionen
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

modules.py 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. import time
  19. try:
  20. from config import APP_NAME as ROOT_LOGGER_NAME
  21. except ImportError:
  22. ROOT_LOGGER_NAME = 'root'
  23. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  24. class switched_light(object):
  25. def __init__(self, sw_device, sw_key, li_device):
  26. sw_device.add_callback(sw_device.KEY_OUTPUT_0, True, li_device.request_data, True)
  27. class brightness_choose_n_action(common_base):
  28. KEY_ACTIVE_DEVICE = 'active_device'
  29. #
  30. DEFAULT_VALUES = {KEY_ACTIVE_DEVICE: None}
  31. def __init__(self, button_tradfri):
  32. super().__init__()
  33. # brightness change
  34. button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_DOWN_LONG, self.brightness_action)
  35. button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_UP_LONG, self.brightness_action)
  36. button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_DOWN_RELEASE, self.brightness_action)
  37. button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_UP_RELEASE, self.brightness_action)
  38. # device change
  39. button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_UP, self.choose_next_device)
  40. button_tradfri.add_callback(button_tradfri.KEY_ACTION, button_tradfri.ACTION_BRIGHTNESS_DOWN, self.choose_prev_device)
  41. #
  42. self.brightness_device_list = []
  43. self.callback_device_list = []
  44. self.device_states = []
  45. def add(self, brightness_device, callback_device, callback_key):
  46. """
  47. brightness_device: A device for brightness function needs to have the following methods:
  48. * .default_inc()
  49. * .default_dec()
  50. * .default_stop()
  51. callback_device: A device for installing callback which are executed, when the device is switched on or off. It needs the following method:
  52. * .add_callback(key, data or None, callback, on_changes_only)
  53. """
  54. self.brightness_device_list.append(brightness_device)
  55. self.callback_device_list.append((callback_device, callback_key))
  56. self.device_states.append(False)
  57. callback_device.add_callback(callback_key, True, self.device_state_action, True)
  58. callback_device.add_callback(callback_key, False, self.device_state_action, True)
  59. def device_state_action(self, device, key, data):
  60. self.device_states[self.callback_device_list.index((device, key))] = data
  61. if data is True:
  62. self.set(self.KEY_ACTIVE_DEVICE, self.callback_device_list.index((device, key)))
  63. else:
  64. if self[self.KEY_ACTIVE_DEVICE] is not None:
  65. if self.callback_device_list[self[self.KEY_ACTIVE_DEVICE]][0] == device:
  66. self.choose_next_device()
  67. def choose_prev_device(self, device=None, key=None, data=None):
  68. if self[self.KEY_ACTIVE_DEVICE] is not None:
  69. start_value = self[self.KEY_ACTIVE_DEVICE]
  70. for i in range(0, len(self.brightness_device_list)):
  71. target_state = (start_value - i - 1) % (len(self.brightness_device_list))
  72. if self.device_states[target_state]:
  73. self.set(self.KEY_ACTIVE_DEVICE, target_state)
  74. return
  75. self.set(self.KEY_ACTIVE_DEVICE, None)
  76. def choose_next_device(self, device=None, key=None, data=None):
  77. if self[self.KEY_ACTIVE_DEVICE] is not None:
  78. start_value = self[self.KEY_ACTIVE_DEVICE]
  79. for i in range(0, len(self.brightness_device_list)):
  80. target_state = (start_value + i + 1) % (len(self.brightness_device_list))
  81. if self.device_states[target_state]:
  82. self.set(self.KEY_ACTIVE_DEVICE, target_state)
  83. return
  84. self.set(self.KEY_ACTIVE_DEVICE, None)
  85. def brightness_action(self, device, key, data):
  86. if self[self.KEY_ACTIVE_DEVICE] is not None:
  87. target = self.brightness_device_list[self[self.KEY_ACTIVE_DEVICE]]
  88. if data == devices.tradfri_button.ACTION_BRIGHTNESS_UP_LONG:
  89. logger.info("Increasing \"%s\" - %s", type(self).__name__, target.topic)
  90. target.default_inc()
  91. elif data == devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_LONG:
  92. logger.info("Decreasing \"%s\" - %s", type(self).__name__, target.topic)
  93. target.default_dec()
  94. elif data in [devices.tradfri_button.ACTION_BRIGHTNESS_UP_RELEASE, devices.tradfri_button.ACTION_BRIGHTNESS_DOWN_RELEASE]:
  95. target.default_stop()
  96. class timer_on_activation(common_base):
  97. KEY_TIMER = 'timer'
  98. #
  99. DEFAULT_VALUES = {
  100. KEY_TIMER: 0
  101. }
  102. def __init__(self, sw_device, sw_key, timer_reload_value):
  103. super().__init__()
  104. #
  105. self.timer_reload_value = timer_reload_value
  106. #
  107. sw_device.add_callback(sw_key, None, self.circ_pump_actions, True)
  108. #
  109. self.ct = task.periodic(6, self.cyclic_task)
  110. self.ct.run()
  111. def circ_pump_actions(self, device, key, data):
  112. if data is True:
  113. self.set(self.KEY_TIMER, self.timer_reload_value)
  114. else:
  115. self.set(self.KEY_TIMER, 0)
  116. def cyclic_task(self, rt):
  117. timer_value = self[self.KEY_TIMER] - self.ct.cycle_time
  118. if timer_value <= 0:
  119. self.set(self.KEY_TIMER, 0)
  120. else:
  121. self.set(self.KEY_TIMER, timer_value)
  122. class heating_function(common_base):
  123. KEY_USER_TEMPERATURE_SETPOINT = 'user_temperature_setpoint'
  124. KEY_TEMPERATURE_SETPOINT = 'temperature_setpoint'
  125. KEY_TEMPERATURE_CURRENT = 'temperature_current'
  126. KEY_AWAY_MODE = 'away_mode'
  127. KEY_SUMMER_MODE = 'summer_mode'
  128. KEY_START_BOOST = 'start_boost'
  129. KEY_SET_DEFAULT_TEMPERATURE = 'set_default_temperature'
  130. KEY_BOOST_TIMER = 'boost_timer'
  131. #
  132. BOOST_TEMPERATURE = 30
  133. AWAY_REDUCTION = 5
  134. SUMMER_TEMPERATURE = 5
  135. class value_timeout_list(object):
  136. MAX_DELAY = 10
  137. def __init__(self):
  138. self.__data__ = []
  139. self.__time__ = []
  140. def __cleanup__(self):
  141. now = time.time()
  142. for i, tm in enumerate(self.__time__):
  143. if tm + self.MAX_DELAY < now:
  144. del (self.__data__[i])
  145. del (self.__time__[i])
  146. def new(self, item):
  147. self.__cleanup__()
  148. self.__data__.append(item)
  149. self.__time__.append(time.time())
  150. def is_valid_value(self, data):
  151. self.__cleanup__()
  152. return data not in self.__data__
  153. def __init__(self, heating_valve, default_temperature, **kwargs):
  154. self.heating_valve = heating_valve
  155. self.default_temperature = default_temperature
  156. #
  157. self.valve_value = self.value_timeout_list()
  158. #
  159. super().__init__({
  160. self.KEY_USER_TEMPERATURE_SETPOINT: kwargs.get(self.KEY_USER_TEMPERATURE_SETPOINT, self.default_temperature),
  161. self.KEY_TEMPERATURE_SETPOINT: kwargs.get(self.KEY_TEMPERATURE_SETPOINT, self.default_temperature),
  162. self.KEY_TEMPERATURE_CURRENT: kwargs.get(self.KEY_TEMPERATURE_CURRENT, None),
  163. self.KEY_AWAY_MODE: kwargs.get(self.KEY_AWAY_MODE, False),
  164. self.KEY_SUMMER_MODE: kwargs.get(self.KEY_SUMMER_MODE, False),
  165. self.KEY_START_BOOST: kwargs.get(self.KEY_START_BOOST, True),
  166. self.KEY_SET_DEFAULT_TEMPERATURE: kwargs.get(self.KEY_SET_DEFAULT_TEMPERATURE, False),
  167. self.KEY_BOOST_TIMER: kwargs.get(self.KEY_BOOST_TIMER, 0)
  168. })
  169. #
  170. self.heating_valve.set_heating_setpoint(self[self.KEY_TEMPERATURE_SETPOINT])
  171. #
  172. self.heating_valve.add_callback(self.heating_valve.KEY_HEATING_SETPOINT, None, self.get_radiator_setpoint)
  173. self.heating_valve.add_callback(self.heating_valve.KEY_TEMPERATURE, None, self.get_radiator_temperature)
  174. #
  175. self.add_callback(self.KEY_USER_TEMPERATURE_SETPOINT, None, self.user_temperature_setpoint, False)
  176. self.add_callback(self.KEY_TEMPERATURE_SETPOINT, None, self.set_heating_setpoint, True)
  177. self.add_callback(self.KEY_AWAY_MODE, None, self.away_mode, True)
  178. self.add_callback(self.KEY_SUMMER_MODE, None, self.summer_mode, True)
  179. self.add_callback(self.KEY_SET_DEFAULT_TEMPERATURE, None, self.setpoint_to_default)
  180. self.add_callback(self.KEY_START_BOOST, True, self.boost, False)
  181. self.add_callback(self.KEY_BOOST_TIMER, 0, self.timer_expired, True)
  182. # cyclic task initialisation
  183. self.ct = task.periodic(1, self.cyclic_task)
  184. self.ct.run()
  185. def timer_expired(self, device, data, key):
  186. self.set(self.KEY_TEMPERATURE_SETPOINT, self[self.KEY_USER_TEMPERATURE_SETPOINT])
  187. self.heating_valve.logger.info('Timer expired. returning to regular temperature setpoint %.1f°C.',
  188. self[self.KEY_TEMPERATURE_SETPOINT])
  189. def cyclic_task(self, rt):
  190. timer_value = self[self.KEY_BOOST_TIMER] - self.ct.cycle_time
  191. if self[self.KEY_BOOST_TIMER] <= 0:
  192. self.set(self.KEY_BOOST_TIMER, 0)
  193. else:
  194. self.set(self.KEY_BOOST_TIMER, timer_value)
  195. def cancel_boost(self):
  196. self.set(self.KEY_BOOST_TIMER, 0, block_callback=[self.timer_expired])
  197. def send_command(self, key, data, block_callback=[]):
  198. return super().set(key, data, block_callback)
  199. def away_mode(self, device, key, value):
  200. if value is True:
  201. self.cancel_boost()
  202. self.set(self.KEY_SUMMER_MODE, False, [self.summer_mode])
  203. self.set(self.KEY_TEMPERATURE_SETPOINT, self[self.KEY_USER_TEMPERATURE_SETPOINT] - self.AWAY_REDUCTION)
  204. else:
  205. self.set(self.KEY_TEMPERATURE_SETPOINT, self[self.KEY_USER_TEMPERATURE_SETPOINT])
  206. def summer_mode(self, device, key, value):
  207. if value is True:
  208. self.cancel_boost()
  209. self.set(self.KEY_AWAY_MODE, False, [self.away_mode])
  210. self.set(self.KEY_TEMPERATURE_SETPOINT, self.SUMMER_TEMPERATURE)
  211. else:
  212. self.set(self.KEY_TEMPERATURE_SETPOINT, self[self.KEY_USER_TEMPERATURE_SETPOINT])
  213. def boost(self, device, key, data):
  214. if self[self.KEY_BOOST_TIMER] == 0:
  215. self.heating_valve.logger.info('Starting boost mode with setpoint %.1f°C.', self.BOOST_TEMPERATURE)
  216. self.set(self.KEY_BOOST_TIMER, 15*60)
  217. self.set(self.KEY_TEMPERATURE_SETPOINT, self.BOOST_TEMPERATURE)
  218. else:
  219. self.set(self.KEY_BOOST_TIMER, min(self[self.KEY_BOOST_TIMER] + 15 * 60, 60 * 60))
  220. self.set(self.KEY_AWAY_MODE, False, [self.away_mode])
  221. self.set(self.KEY_SUMMER_MODE, False, [self.summer_mode])
  222. def setpoint_to_default(self, device, key, data):
  223. self.cancel_boost()
  224. self.set(self.KEY_AWAY_MODE, False, [self.away_mode])
  225. self.set(self.KEY_SUMMER_MODE, False, [self.summer_mode])
  226. self.set(self.KEY_USER_TEMPERATURE_SETPOINT, self.default_temperature, [self.user_temperature_setpoint])
  227. self.set(self.KEY_TEMPERATURE_SETPOINT, self.default_temperature)
  228. def user_temperature_setpoint(self, device, key, data):
  229. self.cancel_boost()
  230. self.set(self.KEY_AWAY_MODE, False, [self.away_mode])
  231. self.set(self.KEY_SUMMER_MODE, False, [self.summer_mode])
  232. self.set(self.KEY_TEMPERATURE_SETPOINT, data)
  233. def set_heating_setpoint(self, device, key, data):
  234. self.valve_value.new(data)
  235. self.heating_valve.set_heating_setpoint(data)
  236. def get_radiator_setpoint(self, device, key, data):
  237. if self.valve_value.is_valid_value(data):
  238. if self[self.KEY_BOOST_TIMER] == 0 and not self[self.KEY_AWAY_MODE] and not self[self.KEY_SUMMER_MODE]:
  239. self.set(self.KEY_USER_TEMPERATURE_SETPOINT, data, block_callback=[self.set_heating_setpoint])
  240. def get_radiator_temperature(self, device, key, data):
  241. self.set(self.KEY_TEMPERATURE_CURRENT, data)
  242. class motion_sensor_light(common_base):
  243. KEY_TIMER = 'timer'
  244. KEY_MOTION_SENSOR = 'motion_%d'
  245. KEY_MOTION_SENSOR_0 = 'motion_%d' % 0
  246. KEY_MOTION_SENSOR_1 = 'motion_%d' % 1
  247. KEY_MOTION_SENSOR_2 = 'motion_%d' % 2
  248. KEY_MOTION_SENSOR_3 = 'motion_%d' % 3
  249. KEY_MOTION_SENSOR_4 = 'motion_%d' % 4
  250. def __init__(self, sw_device, sw_method, *args, timer_value=30):
  251. """
  252. sw_device is the device switching the light, args are 0-n motion sensors
  253. """
  254. dv = dict.fromkeys([self.KEY_MOTION_SENSOR % i for i in range(0, len(args))])
  255. for key in dv:
  256. dv[key] = False
  257. dv[self.KEY_TIMER] = 0
  258. super().__init__(default_values=dv)
  259. #
  260. self.sw_device = sw_device
  261. self.sw_method = sw_method
  262. self.motion_sensors = args
  263. self.timer_reload_value = timer_value
  264. #
  265. sw_device.add_callback(devices.shelly_sw1.KEY_OUTPUT_0, True, self.reload_timer, True)
  266. sw_device.add_callback(devices.shelly_sw1.KEY_OUTPUT_0, False, self.reset_timer, True)
  267. for motion_sensor in args:
  268. motion_sensor.add_callback(motion_sensor.KEY_OCCUPANCY, None, self.set_motion_detected, True)
  269. #
  270. self.add_callback(self.KEY_TIMER, 0, self.timer_expired, True)
  271. #
  272. cyclic_task = task.periodic(1, self.cyclic_task)
  273. cyclic_task.run()
  274. def reload_timer(self, device, key, data):
  275. self.set(self.KEY_TIMER, self.timer_reload_value)
  276. def reset_timer(self, device=None, key=None, data=None):
  277. self.set(self.KEY_TIMER, 0)
  278. def set_motion_detected(self, device, key, data):
  279. for sensor_index, arg_device in enumerate(self.motion_sensors):
  280. if arg_device.topic == device.topic:
  281. break
  282. self.set(self.KEY_MOTION_SENSOR % sensor_index, data)
  283. # auto light on with state sunset -> time_offset_sunrise=60 (longer sunset) and time_offset_sunset=-60 (longer sunset)
  284. if day_state(None, None, 60, -60).get_state() == day_state.KEY_SUNSET:
  285. if data is True:
  286. logger.info("%s: Motion detected - Switching on main light %s", device.topic, self.sw_device.topic)
  287. self.sw_method(True)
  288. def motion_detected(self):
  289. for i in range(0, len(self.motion_sensors)):
  290. if self[self.KEY_MOTION_SENSOR % i]:
  291. return True
  292. return False
  293. def timer_expired(self, device, key, data):
  294. logger.info("No motion and time ran out - Switching off main light %s", self.sw_device.topic)
  295. self.sw_method(False)
  296. def cyclic_task(self, cyclic_task):
  297. min_value = 10 if self.motion_detected() else 0
  298. if self[self.KEY_TIMER] != 0:
  299. self.set(self.KEY_TIMER, max(min_value, self[self.KEY_TIMER] - cyclic_task.cycle_time))