Smarthome Functionen
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

videv.py 7.2KB

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