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.

videv.py 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 videv_base
  12. from function.rooms import room, room_collection
  13. import time
  14. try:
  15. from config import APP_NAME as ROOT_LOGGER_NAME
  16. except ImportError:
  17. ROOT_LOGGER_NAME = 'root'
  18. class videv_switching(videv_base):
  19. KEY_STATE = 'state'
  20. def __init__(self, mqtt_client, topic, sw_device, sw_key):
  21. super().__init__(mqtt_client, topic)
  22. self.add_routing(self.KEY_STATE, sw_device, sw_key)
  23. class videv_switching_timer(videv_base):
  24. KEY_STATE = 'state'
  25. KEY_TIMER = 'timer'
  26. def __init__(self, mqtt_client, topic, sw_device, sw_key, tm_device, tm_key):
  27. super().__init__(mqtt_client, topic)
  28. self.add_routing(self.KEY_STATE, sw_device, sw_key)
  29. self.add_display(self.KEY_TIMER, tm_device, tm_key)
  30. class videv_switching_motion(videv_base):
  31. KEY_STATE = 'state'
  32. #
  33. KEY_TIMER = 'timer'
  34. KEY_MOTION_SENSOR = 'motion_%d'
  35. def __init__(self, mqtt_client, topic, sw_device, sw_key, motion_function):
  36. self.motion_sensors = motion_function.motion_sensors
  37. #
  38. super().__init__(mqtt_client, topic)
  39. self.add_routing(self.KEY_STATE, sw_device, sw_key)
  40. self.add_display(self.KEY_TIMER, motion_function, motion_function.KEY_TIMER)
  41. # motion sensor state
  42. for index, motion_sensor in enumerate(self.motion_sensors):
  43. self.add_display(self.KEY_MOTION_SENSOR % index, motion_sensor, motion_sensor.KEY_OCCUPANCY)
  44. class videv_switch_brightness(videv_base):
  45. KEY_STATE = 'state'
  46. KEY_BRIGHTNESS = 'brightness'
  47. def __init__(self, mqtt_client, topic, sw_device, sw_key, br_device, br_key):
  48. super().__init__(mqtt_client, topic)
  49. self.add_routing(self.KEY_STATE, sw_device, sw_key)
  50. self.add_routing(self.KEY_BRIGHTNESS, br_device, br_key)
  51. class videv_switch_brightness_color_temp(videv_base):
  52. KEY_STATE = 'state'
  53. KEY_BRIGHTNESS = 'brightness'
  54. KEY_COLOR_TEMP = 'color_temp'
  55. def __init__(self, mqtt_client, topic, sw_device, sw_key, br_device, br_key, ct_device, ct_key):
  56. super().__init__(mqtt_client, topic)
  57. self.add_routing(self.KEY_STATE, sw_device, sw_key)
  58. self.add_routing(self.KEY_BRIGHTNESS, br_device, br_key)
  59. self.add_routing(self.KEY_COLOR_TEMP, ct_device, ct_key)
  60. class videv_heating(videv_base):
  61. KEY_USER_TEMPERATURE_SETPOINT = 'user_temperature_setpoint'
  62. KEY_VALVE_TEMPERATURE_SETPOINT = 'valve_temperature_setpoint'
  63. KEY_AWAY_MODE = 'away_mode'
  64. KEY_SUMMER_MODE = 'summer_mode'
  65. KEY_START_BOOST = 'start_boost'
  66. KEY_SET_DEFAULT_TEMPERATURE = 'set_default_temperature'
  67. KEY_BOOST_TIMER = 'boost_timer'
  68. #
  69. KEY_TEMPERATURE = 'temperature'
  70. def __init__(self, mqtt_client, topic, heating_function):
  71. super().__init__(mqtt_client, topic)
  72. #
  73. self.add_routing(self.KEY_USER_TEMPERATURE_SETPOINT, heating_function, heating_function.KEY_USER_TEMPERATURE_SETPOINT)
  74. self.add_routing(self.KEY_AWAY_MODE, heating_function, heating_function.KEY_AWAY_MODE)
  75. self.add_routing(self.KEY_SUMMER_MODE, heating_function, heating_function.KEY_SUMMER_MODE)
  76. #
  77. self.add_control(self.KEY_START_BOOST, heating_function, heating_function.KEY_START_BOOST, False)
  78. self.add_control(self.KEY_SET_DEFAULT_TEMPERATURE, heating_function, heating_function.KEY_SET_DEFAULT_TEMPERATURE, False)
  79. #
  80. self.add_display(self.KEY_VALVE_TEMPERATURE_SETPOINT, heating_function, heating_function.KEY_TEMPERATURE_SETPOINT)
  81. self.add_display(self.KEY_BOOST_TIMER, heating_function, heating_function.KEY_BOOST_TIMER)
  82. self.add_display(self.KEY_TEMPERATURE, heating_function, heating_function.KEY_TEMPERATURE_CURRENT, False)
  83. class videv_multistate(videv_base):
  84. KEY_STATE = 'state_%d'
  85. def __init__(self, mqtt_client, topic, key_for_device, device, num_states, default_values=None):
  86. super().__init__(mqtt_client, topic)
  87. self.num_states = num_states
  88. # send default values
  89. for i in range(0, num_states):
  90. self.__tx__(self.KEY_STATE % i, False)
  91. #
  92. device.add_callback(key_for_device, None, self.__index_rx__, True)
  93. def __index_rx__(self, device, key, data):
  94. for i in range(0, self.num_states):
  95. self.__tx__(self.KEY_STATE % i, i == data)
  96. class videv_audio_player(videv_base):
  97. KEY_ACTIVE_PLAYER = 'player_%d'
  98. KEY_TITLE = 'title'
  99. NO_TITLE = '---'
  100. def __init__(self, mqtt_client, topic, *args):
  101. super().__init__(mqtt_client, topic)
  102. for i, device in enumerate(args):
  103. self.add_display(self.KEY_ACTIVE_PLAYER % i, device, device.KEY_STATE)
  104. #
  105. for audio_device in args:
  106. audio_device.add_callback(audio_device.KEY_TITLE, None, self.__title_rx__, True)
  107. def __title_rx__(self, device, key, data):
  108. self.__tx__(self.KEY_TITLE, data or self.NO_TITLE)
  109. class videv_warnings(videv_base):
  110. MAX_WARNINGS = 10
  111. KEY_WARNING = 'html_short'
  112. def __init__(self, mqtt_client, topic, default_values=None):
  113. super().__init__(mqtt_client, topic, default_values)
  114. self.__warnings__ = []
  115. def warningcollector(self, client, key, data):
  116. self.__warnings__.append(data)
  117. self.__warnings__ = self.__warnings__[-self.MAX_WARNINGS:]
  118. 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__])
  119. self.__tx__(self.KEY_WARNING, txt)
  120. class all_off(videv_base):
  121. ALLOWED_CLASSES = (room, room_collection, )
  122. def __init__(self, mqtt_client, topic, room_collection):
  123. super().__init__(mqtt_client, topic)
  124. self.__room_collection__ = room_collection
  125. # init __inst_dict__
  126. self.__inst_dict__ = {}
  127. self.__add_instances__("all", self.__room_collection__)
  128. # register mqtt callbacks for all my keys
  129. for key in self.__inst_dict__:
  130. mqtt_client.add_callback(topic + "/" + key, self.all_off)
  131. def __check_inst_capabilities__(self, name, inst):
  132. # fits to specified classes
  133. if isinstance(inst, self.ALLOWED_CLASSES):
  134. try:
  135. # all_off method is callable
  136. return callable(inst.all_off)
  137. except AttributeError:
  138. # all_off method does not exist
  139. return False
  140. return False
  141. def __add_instances__(self, name, inst, level=0):
  142. if self.__check_inst_capabilities__(name, inst):
  143. # add given instance to my __inst_dict__
  144. self.__inst_dict__[name] = inst
  145. # iterate over all attribute names of instance
  146. for sub_name in dir(inst):
  147. # attribute name is not private
  148. if not sub_name.startswith("__"):
  149. sub = getattr(inst, sub_name)
  150. # recurse with this object
  151. if level == 0:
  152. self.__add_instances__(sub_name, sub, level=level+1)
  153. else:
  154. self.__add_instances__(name + "/" + sub_name, sub, level=level+1)
  155. def all_off(self, client, userdata, message):
  156. key = message.topic[len(self.topic) + 1:]
  157. self.__inst_dict__[key].all_off()