MQTT Home Emulation
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.

tradfri.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. tradfri devices
  6. ===============
  7. **Author:**
  8. * Dirk Alders <sudo-dirk@mount-mockery.de>
  9. **Description:**
  10. Emulation of tradfri devices
  11. Communication (MQTT)
  12. tradfri_light {
  13. | "state": ["ON" / "OFF" / "TOGGLE"]
  14. | "linkquality": [0...255] lqi
  15. | "brightness": [0...254]
  16. | "color_mode": ["color_temp"]
  17. | "color_temp": ["coolest", "cool", "neutral", "warm", "warmest", 250...454]
  18. | "color_temp_startup": ["coolest", "cool", "neutral", "warm", "warmest", "previous", 250...454]
  19. | "update": []
  20. | }
  21. +- get {
  22. | "state": ""
  23. | }
  24. +- set {
  25. "state": ["ON" / "OFF"]
  26. "brightness": [0...256]
  27. "color_temp": [250...454]
  28. "transition": [0...] seconds
  29. "brightness_move": [-X...0...X] X/s
  30. "brightness_step": [-X...0...X]
  31. "color_temp_move": [-X...0...X] X/s
  32. "color_temp_step": [-X...0...X]
  33. }
  34. """
  35. from devices.base import base
  36. import json
  37. class sw(base):
  38. """A tradfri device with switching functionality
  39. Args:
  40. mqtt_client (mqtt.mqtt_client): A MQTT Client instance
  41. topic (str): the base topic for this device
  42. """
  43. KEY_OUTPUT_0 = "state"
  44. PROPERTIES = [
  45. KEY_OUTPUT_0,
  46. ]
  47. def __init__(self, mqtt_client, topic):
  48. super().__init__(mqtt_client, topic)
  49. self[self.KEY_OUTPUT_0] = "off"
  50. #
  51. self.mqtt_client.add_callback(self.topic + '/set', self.__rx_set__)
  52. self.mqtt_client.add_callback(self.topic + '/get', self.__rx_get__)
  53. #
  54. cmd_base = self.topic.replace('/', '.') + '.'
  55. self.user_cmds = {
  56. cmd_base + 'toggle': self.__ui_toggle_output_0__,
  57. }
  58. def set_state(self, value):
  59. self.__set__(self.KEY_OUTPUT_0, "on" if value else "off")
  60. self.send_device_status()
  61. def __rx_set__(self, client, userdata, message):
  62. data = json.loads(message.payload)
  63. self.logger.info("Received set data: %s", repr(data))
  64. for key in data:
  65. self.__set__(key, data[key])
  66. self.send_device_status()
  67. if self.KEY_OUTPUT_0 in data and data.get(self.KEY_OUTPUT_0, 'OFF').lower() == "on":
  68. self.power_on(self.KEY_OUTPUT_0)
  69. def __rx_get__(self, client, userdata, message):
  70. self.send_device_status()
  71. def power_on_action(self):
  72. self[self.KEY_OUTPUT_0] = "on"
  73. self.send_device_status()
  74. def send_device_status(self):
  75. data = json.dumps(self)
  76. self.logger.info("Sending status: %s", repr(data))
  77. self.mqtt_client.send(self.topic, data)
  78. def __ui_toggle_output_0__(self, *args):
  79. self.__set__(self.KEY_OUTPUT_0, 'off' if self.get(self.KEY_OUTPUT_0).lower() == 'on' else 'on')
  80. self.send_device_status()
  81. class sw_br(sw):
  82. """A tradfri device with switching and brightness functionality
  83. Args:
  84. mqtt_client (mqtt.mqtt_client): A MQTT Client instance
  85. topic (str): the base topic for this device
  86. """
  87. PROPERTIES = sw.PROPERTIES + [
  88. "brightness",
  89. ]
  90. def __init__(self, mqtt_client, topic):
  91. super().__init__(mqtt_client, topic)
  92. self["brightness"] = 64
  93. class sw_br_ct(sw_br):
  94. """A tradfri device with switching, brightness and colortemp functionality
  95. Args:
  96. mqtt_client (mqtt.mqtt_client): A MQTT Client instance
  97. topic (str): the base topic for this device
  98. """
  99. PROPERTIES = sw_br.PROPERTIES + [
  100. "color_temp",
  101. ]
  102. def __init__(self, mqtt_client, topic):
  103. super().__init__(mqtt_client, topic)
  104. self["color_temp"] = 413