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

videv.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. from function.rooms import room, room_collection
  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. my_key = self.__display_dict__[(id(ext_device), ext_key)]
  44. self[my_key] = data
  45. self.__tx__(my_key, data)
  46. def __tx__(self, key, data):
  47. if key in self.__control_dict__:
  48. self.__active_tx__[key] = (time.time(), data)
  49. if type(data) not in (str, ):
  50. data = json.dumps(data)
  51. self.mqtt_client.send(self.topic + '/' + key, data)
  52. self.__tx_capabilities__()
  53. def __tx_capabilities__(self):
  54. self.mqtt_client.send(self.topic + '/' + self.KEY_INFO, json.dumps(self.capabilities))
  55. def add_control(self, my_key, ext_device, ext_key, on_change_only=True):
  56. """
  57. listen to videv information and pass data to ext_device
  58. """
  59. self[my_key] = None
  60. # store information to identify callback from videv
  61. self.__control_dict__[my_key] = (ext_device, ext_key, on_change_only)
  62. # add callback for videv changes
  63. self.mqtt_client.add_callback(self.topic + '/' + my_key, self.__rx_videv_data__)
  64. def __rx_videv_data__(self, client, userdata, message):
  65. my_key = message.topic.split('/')[-1]
  66. try:
  67. data = json.loads(message.payload)
  68. except json.decoder.JSONDecodeError:
  69. data = message.payload
  70. if my_key in self.__active_tx__:
  71. tm, tx_data = self.__active_tx__.pop(my_key)
  72. do_ex = data != tx_data and time.time() - tm < 2
  73. else:
  74. do_ex = True
  75. if do_ex:
  76. ext_device, ext_key, on_change_only = self.__control_dict__[my_key]
  77. if my_key in self.keys():
  78. if data != self[my_key] or not on_change_only:
  79. ext_device.send_command(ext_key, data)
  80. self.set(my_key, data)
  81. else:
  82. self.logger.info("Ignoring rx message with topic %s", message.topic)
  83. def add_routing(self, my_key, ext_device, ext_key, on_change_only_disp=True, on_change_only_videv=True):
  84. """
  85. listen to data changes of ext_device and update videv information
  86. and
  87. listen to videv information and pass data to ext_device
  88. """
  89. # add display
  90. self.add_display(my_key, ext_device, ext_key, on_change_only_disp)
  91. self.add_control(my_key, ext_device, ext_key, on_change_only_videv)
  92. @property
  93. def capabilities(self):
  94. if self.__capabilities__ is None:
  95. self.__capabilities__ = {}
  96. self.__capabilities__['__type__'] = self.__class__.__name__
  97. for key in self.__control_dict__:
  98. if not key in self.__capabilities__:
  99. self.__capabilities__[key] = {}
  100. self.__capabilities__[key]['control'] = True
  101. for key in self.__display_dict__.values():
  102. if not key in self.__capabilities__:
  103. self.__capabilities__[key] = {}
  104. self.__capabilities__[key]['display'] = True
  105. return self.__capabilities__
  106. class videv_switching(base):
  107. KEY_STATE = 'state'
  108. def __init__(self, mqtt_client, topic, sw_device, sw_key):
  109. super().__init__(mqtt_client, topic)
  110. self.add_routing(self.KEY_STATE, sw_device, sw_key)
  111. #
  112. self.__tx_capabilities__()
  113. class videv_switching_timer(base):
  114. KEY_STATE = 'state'
  115. KEY_TIMER = 'timer'
  116. def __init__(self, mqtt_client, topic, sw_device, sw_key, tm_device, tm_key):
  117. super().__init__(mqtt_client, topic)
  118. self.add_routing(self.KEY_STATE, sw_device, sw_key)
  119. self.add_display(self.KEY_TIMER, tm_device, tm_key)
  120. #
  121. self.__tx_capabilities__()
  122. class videv_switching_motion(base):
  123. KEY_STATE = 'state'
  124. #
  125. KEY_TIMER = 'timer'
  126. KEY_MOTION_SENSOR = 'motion_%d'
  127. def __init__(self, mqtt_client, topic, sw_device, sw_key, motion_function):
  128. self.motion_sensors = motion_function.motion_sensors
  129. #
  130. super().__init__(mqtt_client, topic)
  131. self.add_routing(self.KEY_STATE, sw_device, sw_key)
  132. self.add_display(self.KEY_TIMER, motion_function, motion_function.KEY_TIMER)
  133. # motion sensor state
  134. for index, motion_sensor in enumerate(self.motion_sensors):
  135. self.add_display(self.KEY_MOTION_SENSOR % index, motion_sensor, motion_sensor.KEY_OCCUPANCY)
  136. #
  137. self.__tx_capabilities__()
  138. class videv_switch_brightness(base):
  139. KEY_STATE = 'state'
  140. KEY_BRIGHTNESS = 'brightness'
  141. def __init__(self, mqtt_client, topic, sw_device, sw_key, br_device, br_key):
  142. super().__init__(mqtt_client, topic)
  143. self.add_routing(self.KEY_STATE, sw_device, sw_key)
  144. self.add_routing(self.KEY_BRIGHTNESS, br_device, br_key)
  145. #
  146. self.__tx_capabilities__()
  147. class videv_switch_brightness_color_temp(base):
  148. KEY_STATE = 'state'
  149. KEY_BRIGHTNESS = 'brightness'
  150. KEY_COLOR_TEMP = 'color_temp'
  151. def __init__(self, mqtt_client, topic, sw_device, sw_key, br_device, br_key, ct_device, ct_key):
  152. super().__init__(mqtt_client, topic)
  153. self.add_routing(self.KEY_STATE, sw_device, sw_key)
  154. self.add_routing(self.KEY_BRIGHTNESS, br_device, br_key)
  155. self.add_routing(self.KEY_COLOR_TEMP, ct_device, ct_key)
  156. #
  157. self.__tx_capabilities__()
  158. class videv_heating(base):
  159. KEY_USER_TEMPERATURE_SETPOINT = 'user_temperature_setpoint'
  160. KEY_VALVE_TEMPERATURE_SETPOINT = 'valve_temperature_setpoint'
  161. KEY_AWAY_MODE = 'away_mode'
  162. KEY_SUMMER_MODE = 'summer_mode'
  163. KEY_START_BOOST = 'start_boost'
  164. KEY_SET_DEFAULT_TEMPERATURE = 'set_default_temperature'
  165. KEY_BOOST_TIMER = 'boost_timer'
  166. #
  167. KEY_TEMPERATURE = 'temperature'
  168. def __init__(self, mqtt_client, topic, heating_function):
  169. super().__init__(mqtt_client, topic)
  170. #
  171. self.add_routing(self.KEY_USER_TEMPERATURE_SETPOINT, heating_function, heating_function.KEY_USER_TEMPERATURE_SETPOINT)
  172. self.add_routing(self.KEY_AWAY_MODE, heating_function, heating_function.KEY_AWAY_MODE)
  173. self.add_routing(self.KEY_SUMMER_MODE, heating_function, heating_function.KEY_SUMMER_MODE)
  174. #
  175. self.add_control(self.KEY_START_BOOST, heating_function, heating_function.KEY_START_BOOST, False)
  176. self.add_control(self.KEY_SET_DEFAULT_TEMPERATURE, heating_function, heating_function.KEY_SET_DEFAULT_TEMPERATURE, False)
  177. #
  178. self.add_display(self.KEY_VALVE_TEMPERATURE_SETPOINT, heating_function, heating_function.KEY_TEMPERATURE_SETPOINT)
  179. self.add_display(self.KEY_BOOST_TIMER, heating_function, heating_function.KEY_BOOST_TIMER)
  180. self.add_display(self.KEY_TEMPERATURE, heating_function, heating_function.KEY_TEMPERATURE_CURRENT, False)
  181. #
  182. self.__tx_capabilities__()
  183. class videv_multistate(base):
  184. KEY_STATE = 'state_%d'
  185. def __init__(self, mqtt_client, topic, key_for_device, device, num_states, default_values=None):
  186. super().__init__(mqtt_client, topic)
  187. self.num_states = num_states
  188. # send default values
  189. for i in range(0, num_states):
  190. self.__tx__(self.KEY_STATE % i, False)
  191. #
  192. device.add_callback(key_for_device, None, self.__index_rx__, True)
  193. #
  194. self.__tx_capabilities__()
  195. def __index_rx__(self, device, key, data):
  196. for i in range(0, self.num_states):
  197. self.__tx__(self.KEY_STATE % i, i == data)
  198. #
  199. self.__tx_capabilities__()
  200. class videv_audio_player(base):
  201. KEY_ACTIVE_PLAYER = 'player_%d'
  202. KEY_TITLE = 'title'
  203. NO_TITLE = '---'
  204. def __init__(self, mqtt_client, topic, *args):
  205. super().__init__(mqtt_client, topic)
  206. for i, device in enumerate(args):
  207. self.add_display(self.KEY_ACTIVE_PLAYER % i, device, device.KEY_STATE)
  208. #
  209. for audio_device in args:
  210. audio_device.add_callback(audio_device.KEY_TITLE, None, self.__title_rx__, True)
  211. #
  212. self.__tx_capabilities__()
  213. def __title_rx__(self, device, key, data):
  214. self.__tx__(self.KEY_TITLE, data or self.NO_TITLE)
  215. @property
  216. def capabilities(self):
  217. super().capabilities
  218. self.__capabilities__[self.KEY_TITLE] = {'display': True}
  219. return self.__capabilities__
  220. class videv_warnings(base):
  221. MAX_WARNINGS = 10
  222. KEY_WARNING = 'html_short'
  223. def __init__(self, mqtt_client, topic, default_values=None):
  224. super().__init__(mqtt_client, topic, default_values)
  225. self.__warnings__ = []
  226. def warningcollector(self, client, key, data):
  227. self.__warnings__.append(data)
  228. self.__warnings__ = self.__warnings__[-self.MAX_WARNINGS:]
  229. txt = "<br><br>".join([time.asctime(w[w.KEY_TM]) + "<br>" + w[w.KEY_TEXT] + "<br>" + w[w.KEY_ID] for w in self.__warnings__])
  230. self.__tx__(self.KEY_WARNING, txt)
  231. class all_off(base):
  232. ALLOWED_CLASSES = (room, room_collection, )
  233. def __init__(self, mqtt_client, topic, room_collection):
  234. super().__init__(mqtt_client, topic)
  235. self.__room_collection__ = room_collection
  236. # init __inst_dict__
  237. self.__inst_dict__ = {}
  238. self.__add_instances__("all", self.__room_collection__)
  239. # register mqtt callbacks for all my keys
  240. for key in self.__inst_dict__:
  241. mqtt_client.add_callback(topic + "/" + key, self.all_off)
  242. #
  243. self.__tx_capabilities__()
  244. def __check_inst_capabilities__(self, name, inst):
  245. # fits to specified classes
  246. if isinstance(inst, self.ALLOWED_CLASSES):
  247. try:
  248. # all_off method is callable
  249. return callable(inst.all_off)
  250. except AttributeError:
  251. # all_off method does not exist
  252. return False
  253. return False
  254. def __add_instances__(self, name, inst, level=0):
  255. if self.__check_inst_capabilities__(name, inst):
  256. # add given instance to my __inst_dict__
  257. self.__inst_dict__[name] = inst
  258. # iterate over all attribute names of instance
  259. for sub_name in dir(inst):
  260. # attribute name is not private
  261. if not sub_name.startswith("__"):
  262. sub = getattr(inst, sub_name)
  263. # recurse with this object
  264. if level == 0:
  265. self.__add_instances__(sub_name, sub, level=level+1)
  266. else:
  267. self.__add_instances__(name + "/" + sub_name, sub, level=level+1)
  268. def all_off(self, client, userdata, message):
  269. key = message.topic[len(self.topic) + 1:]
  270. self.__inst_dict__[key].all_off()
  271. self.__tx_capabilities__()
  272. @property
  273. def capabilities(self):
  274. if self.__capabilities__ is None:
  275. self.__capabilities__ = {}
  276. self.__capabilities__['__type__'] = self.__class__.__name__
  277. for key in self.__inst_dict__:
  278. self.__capabilities__[key] = {}
  279. self.__capabilities__[key]['control'] = True
  280. return self.__capabilities__