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 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. PROPERTIES = [
  44. "state",
  45. ]
  46. def __init__(self, mqtt_client, topic):
  47. super().__init__(mqtt_client, topic)
  48. self["state"] = "off"
  49. #
  50. self.mqtt_client.add_callback(self.topic + '/set', self.__rx_set__)
  51. self.mqtt_client.add_callback(self.topic + '/get', self.__rx_get__)
  52. def set_state(self, value):
  53. self.__set__("state", "on" if value else "off")
  54. self.send_device_status()
  55. def __rx_set__(self, client, userdata, message):
  56. data = json.loads(message.payload)
  57. self.logger.info("Received set data: %s", repr(data))
  58. for key in data:
  59. self.__set__(key, data[key])
  60. self.send_device_status()
  61. if "state" in data and data.get("state", 'OFF').lower() == "on":
  62. self.power_on()
  63. def __rx_get__(self, client, userdata, message):
  64. self.send_device_status()
  65. def power_on_action(self):
  66. self.send_device_status()
  67. def send_device_status(self):
  68. data = json.dumps(self)
  69. self.logger.info("Sending status: %s", repr(data))
  70. self.mqtt_client.send(self.topic, data)
  71. class sw_br(sw):
  72. """A tradfri device with switching and brightness functionality
  73. Args:
  74. mqtt_client (mqtt.mqtt_client): A MQTT Client instance
  75. topic (str): the base topic for this device
  76. """
  77. PROPERTIES = sw.PROPERTIES + [
  78. "brightness",
  79. ]
  80. def __init__(self, mqtt_client, topic):
  81. super().__init__(mqtt_client, topic)
  82. self["brightness"] = 64
  83. class sw_br_ct(sw_br):
  84. """A tradfri device with switching, brightness and colortemp functionality
  85. Args:
  86. mqtt_client (mqtt.mqtt_client): A MQTT Client instance
  87. topic (str): the base topic for this device
  88. """
  89. PROPERTIES = sw_br.PROPERTIES + [
  90. "color_temp",
  91. ]
  92. def __init__(self, mqtt_client, topic):
  93. super().__init__(mqtt_client, topic)
  94. self["color_temp"] = 413