MQTT Home Emulation
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

tradfri.py 3.7KB

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