Smarthome Functionen
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 all_off(videv_base):
  110. ALLOWED_CLASSES = (room, room_collection, )
  111. def __init__(self, mqtt_client, topic, room_collection):
  112. super().__init__(mqtt_client, topic)
  113. self.__room_collection__ = room_collection
  114. # init __inst_dict__
  115. self.__inst_dict__ = {}
  116. self.__add_instances__("all", self.__room_collection__)
  117. # register mqtt callbacks for all my keys
  118. for key in self.__inst_dict__:
  119. mqtt_client.add_callback(topic + "/" + key, self.all_off)
  120. def __check_inst_capabilities__(self, name, inst):
  121. # fits to specified classes
  122. if isinstance(inst, self.ALLOWED_CLASSES):
  123. try:
  124. # all_off method is callable
  125. return callable(inst.all_off)
  126. except AttributeError:
  127. # all_off method does not exist
  128. return False
  129. return False
  130. def __add_instances__(self, name, inst, level=0):
  131. if self.__check_inst_capabilities__(name, inst):
  132. # add given instance to my __inst_dict__
  133. self.__inst_dict__[name] = inst
  134. # iterate over all attribute names of instance
  135. for sub_name in dir(inst):
  136. # attribute name is not private
  137. if not sub_name.startswith("__"):
  138. sub = getattr(inst, sub_name)
  139. # recurse with this object
  140. if level == 0:
  141. self.__add_instances__(sub_name, sub, level=level+1)
  142. else:
  143. self.__add_instances__(name + "/" + sub_name, sub, level=level+1)
  144. def all_off(self, client, userdata, message):
  145. key = message.topic[len(self.topic) + 1:]
  146. self.__inst_dict__[key].all_off()