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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. Virtual Device(s)
  6. Targets:
  7. * MQTT-Interface to control joined devices as one virtual device.
  8. * Primary signal routing
  9. * No functionality should be implemented here
  10. """
  11. from base import mqtt_base
  12. import devices
  13. import json
  14. import time
  15. try:
  16. from config import APP_NAME as ROOT_LOGGER_NAME
  17. except ImportError:
  18. ROOT_LOGGER_NAME = 'root'
  19. class base(mqtt_base):
  20. KEY_INFO = '__info__'
  21. def __init__(self, mqtt_client, topic, default_values=None):
  22. super().__init__(mqtt_client, topic, default_values=default_values)
  23. self.__display_dict__ = {}
  24. self.__control_dict__ = {}
  25. self.__capabilities__ = None
  26. self.__active_tx__ = {}
  27. def add_display(self, my_key, ext_device, ext_key, on_change_only=True):
  28. """
  29. listen to data changes of ext_device and update videv information
  30. """
  31. if ext_device.__class__.__name__ == "group":
  32. # store information to identify callback from ext_device
  33. self.__display_dict__[(id(ext_device[0]), ext_key)] = my_key
  34. # register a callback to listen for data from external device
  35. ext_device[0].add_callback(ext_key, None, self.__rx_ext_device_data__, on_change_only)
  36. else:
  37. # store information to identify callback from ext_device
  38. self.__display_dict__[(id(ext_device), ext_key)] = my_key
  39. # register a callback to listen for data from external device
  40. ext_device.add_callback(ext_key, None, self.__rx_ext_device_data__, on_change_only)
  41. # send default data to videv interface
  42. def __rx_ext_device_data__(self, ext_device, ext_key, data):
  43. self.__tx__(self.__display_dict__[(id(ext_device), ext_key)], data)
  44. def __tx__(self, key, data):
  45. if key in self.__control_dict__:
  46. self.__active_tx__[key] = (time.time(), data)
  47. if type(data) not in (str, ):
  48. data = json.dumps(data)
  49. self.mqtt_client.send(self.topic + '/' + key, data)
  50. self.__tx_capabilities__()
  51. def __tx_capabilities__(self):
  52. self.mqtt_client.send(self.topic + '/' + self.KEY_INFO, json.dumps(self.capabilities))
  53. def add_control(self, my_key, ext_device, ext_key, on_change_only=True):
  54. """
  55. listen to videv information and pass data to ext_device
  56. """
  57. self[my_key] = None
  58. # store information to identify callback from videv
  59. self.__control_dict__[my_key] = (ext_device, ext_key, on_change_only)
  60. # add callback for videv changes
  61. self.mqtt_client.add_callback(self.topic + '/' + my_key, self.__rx_videv_data__)
  62. def __rx_videv_data__(self, client, userdata, message):
  63. my_key = message.topic.split('/')[-1]
  64. try:
  65. data = json.loads(message.payload)
  66. except json.decoder.JSONDecodeError:
  67. data = message.payload
  68. if my_key in self.__active_tx__:
  69. tm, tx_data = self.__active_tx__.pop(my_key)
  70. do_ex = data != tx_data and time.time() - tm < 2
  71. else:
  72. do_ex = True
  73. if do_ex:
  74. ext_device, ext_key, on_change_only = self.__control_dict__[my_key]
  75. if my_key in self.keys():
  76. if data != self[my_key] or not on_change_only:
  77. ext_device.set(ext_key, data)
  78. self.set(my_key, data)
  79. else:
  80. self.logger.info("Ignoring rx message with topic %s", message.topic)
  81. def add_routing(self, my_key, ext_device, ext_key, on_change_only_disp=True, on_change_only_videv=True):
  82. """
  83. listen to data changes of ext_device and update videv information
  84. and
  85. listen to videv information and pass data to ext_device
  86. """
  87. # add display
  88. self.add_display(my_key, ext_device, ext_key, on_change_only_disp)
  89. self.add_control(my_key, ext_device, ext_key, on_change_only_videv)
  90. @property
  91. def capabilities(self):
  92. if self.__capabilities__ is None:
  93. self.__capabilities__ = {}
  94. self.__capabilities__['__type__'] = self.__class__.__name__
  95. for key in self.__control_dict__:
  96. if not key in self.__capabilities__:
  97. self.__capabilities__[key] = {}
  98. self.__capabilities__[key]['control'] = True
  99. for key in self.__display_dict__.values():
  100. if not key in self.__capabilities__:
  101. self.__capabilities__[key] = {}
  102. self.__capabilities__[key]['display'] = True
  103. return self.__capabilities__
  104. class videv_switching(base):
  105. KEY_STATE = 'state'
  106. def __init__(self, mqtt_client, topic, sw_device, sw_key):
  107. super().__init__(mqtt_client, topic)
  108. self.add_routing(self.KEY_STATE, sw_device, sw_key)
  109. #
  110. self.__tx_capabilities__()
  111. class videv_switching_timer(base):
  112. KEY_STATE = 'state'
  113. KEY_TIMER = 'timer'
  114. def __init__(self, mqtt_client, topic, sw_device, sw_key, tm_device, tm_key):
  115. super().__init__(mqtt_client, topic)
  116. self.add_routing(self.KEY_STATE, sw_device, sw_key)
  117. self.add_display(self.KEY_TIMER, tm_device, tm_key)
  118. #
  119. self.__tx_capabilities__()
  120. class videv_switching_motion(base):
  121. KEY_STATE = 'state'
  122. #
  123. KEY_TIMER = 'timer'
  124. KEY_MOTION_SENSOR = 'motion_%d'
  125. def __init__(self, mqtt_client, topic, sw_device, sw_key, motion_function):
  126. self.motion_sensors = motion_function.motion_sensors
  127. #
  128. super().__init__(mqtt_client, topic)
  129. self.add_routing(self.KEY_STATE, sw_device, sw_key)
  130. self.add_display(self.KEY_TIMER, motion_function, motion_function.KEY_TIMER)
  131. # motion sensor state
  132. for index, motion_sensor in enumerate(self.motion_sensors):
  133. self.add_display(self.KEY_MOTION_SENSOR % index, motion_sensor, motion_sensor.KEY_OCCUPANCY)
  134. #
  135. self.__tx_capabilities__()
  136. class videv_switch_brightness(base):
  137. KEY_STATE = 'state'
  138. KEY_BRIGHTNESS = 'brightness'
  139. def __init__(self, mqtt_client, topic, sw_device, sw_key, br_device, br_key):
  140. super().__init__(mqtt_client, topic)
  141. self.add_routing(self.KEY_STATE, sw_device, sw_key)
  142. self.add_routing(self.KEY_BRIGHTNESS, br_device, br_key)
  143. #
  144. self.__tx_capabilities__()
  145. class videv_switch_brightness_color_temp(base):
  146. KEY_STATE = 'state'
  147. KEY_BRIGHTNESS = 'brightness'
  148. KEY_COLOR_TEMP = 'color_temp'
  149. def __init__(self, mqtt_client, topic, sw_device, sw_key, br_device, br_key, ct_device, ct_key):
  150. super().__init__(mqtt_client, topic)
  151. self.add_routing(self.KEY_STATE, sw_device, sw_key)
  152. self.add_routing(self.KEY_BRIGHTNESS, br_device, br_key)
  153. self.add_routing(self.KEY_COLOR_TEMP, ct_device, ct_key)
  154. #
  155. self.__tx_capabilities__()
  156. class videv_heating(base):
  157. KEY_USER_TEMPERATURE_SETPOINT = 'user_temperature_setpoint'
  158. KEY_VALVE_TEMPERATURE_SETPOINT = 'valve_temperature_setpoint'
  159. KEY_AWAY_MODE = 'away_mode'
  160. KEY_SUMMER_MODE = 'summer_mode'
  161. KEY_START_BOOST = 'start_boost'
  162. KEY_SET_DEFAULT_TEMPERATURE = 'set_default_temperature'
  163. KEY_BOOST_TIMER = 'boost_timer'
  164. #
  165. KEY_TEMPERATURE = 'temperature'
  166. def __init__(self, mqtt_client, topic, heating_function):
  167. super().__init__(mqtt_client, topic)
  168. #
  169. self.add_routing(self.KEY_USER_TEMPERATURE_SETPOINT, heating_function, heating_function.KEY_USER_TEMPERATURE_SETPOINT)
  170. self.add_routing(self.KEY_AWAY_MODE, heating_function, heating_function.KEY_AWAY_MODE)
  171. self.add_routing(self.KEY_SUMMER_MODE, heating_function, heating_function.KEY_SUMMER_MODE)
  172. #
  173. self.add_control(self.KEY_START_BOOST, heating_function, heating_function.KEY_START_BOOST, False)
  174. self.add_control(self.KEY_SET_DEFAULT_TEMPERATURE, heating_function, heating_function.KEY_SET_DEFAULT_TEMPERATURE, False)
  175. #
  176. self.add_display(self.KEY_VALVE_TEMPERATURE_SETPOINT, heating_function, heating_function.KEY_TEMPERATURE_SETPOINT)
  177. self.add_display(self.KEY_BOOST_TIMER, heating_function, heating_function.KEY_BOOST_TIMER)
  178. self.add_display(self.KEY_TEMPERATURE, heating_function, heating_function.KEY_TEMPERATURE_CURRENT, False)
  179. #
  180. self.__tx_capabilities__()
  181. class videv_multistate(base):
  182. KEY_STATE = 'state_%d'
  183. def __init__(self, mqtt_client, topic, key_for_device, device, num_states, default_values=None):
  184. super().__init__(mqtt_client, topic)
  185. self.num_states = num_states
  186. # send default values
  187. for i in range(0, num_states):
  188. self.__tx__(self.KEY_STATE % i, False)
  189. #
  190. device.add_callback(key_for_device, None, self.__index_rx__, True)
  191. #
  192. self.__tx_capabilities__()
  193. def __index_rx__(self, device, key, data):
  194. for i in range(0, self.num_states):
  195. self.__tx__(self.KEY_STATE % i, i == data)
  196. #
  197. self.__tx_capabilities__()
  198. class videv_audio_player(base):
  199. KEY_ACTIVE_PLAYER = 'player_%d'
  200. KEY_TITLE = 'title'
  201. NO_TITLE = '---'
  202. def __init__(self, mqtt_client, topic, *args):
  203. super().__init__(mqtt_client, topic)
  204. for i, device in enumerate(args):
  205. self.add_display(self.KEY_ACTIVE_PLAYER % i, device, device.KEY_STATE)
  206. #
  207. for audio_device in args:
  208. audio_device.add_callback(audio_device.KEY_TITLE, None, self.__title_rx__, True)
  209. #
  210. self.__tx_capabilities__()
  211. def __title_rx__(self, device, key, data):
  212. self.__tx__(self.KEY_TITLE, data or self.NO_TITLE)
  213. @property
  214. def capabilities(self):
  215. super().capabilities
  216. self.__capabilities__[self.KEY_TITLE] = {'display': True}
  217. return self.__capabilities__