MQTT Home Emulation
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

shelly.py 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """ Communication (MQTT)
  5. shelly
  6. +- relay
  7. | +- 0 ["on" / "off"] <- status
  8. | | +- command ["on"/ "off"] <- command
  9. | | +- energy [numeric] <- status
  10. | +- 1 ["on" / "off"] <- status
  11. | +- command ["on"/ "off"] <- command
  12. | +- energy [numeric] <- status
  13. +- input
  14. | +- 0 [0 / 1] <- status
  15. | +- 1 [0 / 1] <- status
  16. +- input_event
  17. | +- 0 <- status
  18. | +- 1 <- status
  19. +- logpush
  20. | +- 0 [0 / 1] <- status
  21. | +- 1 [0 / 1] <- status
  22. +- temperature [numeric] °C <- status
  23. +- temperature_f [numeric] F <- status
  24. +- overtemperature [0 / 1] <- status
  25. +- id <- status
  26. +- model <- status
  27. +- mac <- status
  28. +- ip <- status
  29. +- new_fw <- status
  30. +- fw_ver <- status
  31. """
  32. from devices.base import base
  33. import json
  34. class shelly_sw1(base):
  35. """A shelly device with switching functionality
  36. Args:
  37. mqtt_client (mqtt.mqtt_client): A MQTT Client instance
  38. topic (str): the base topic for this device
  39. kwargs (**dict): cd_r0=list of devices connected to relay/0
  40. """
  41. PROPERTIES = [
  42. "relay/0",
  43. ]
  44. def __init__(self, mqtt_client, topic):
  45. super().__init__(mqtt_client, topic)
  46. self["state"] = "off"
  47. #
  48. self.mqtt_client.add_callback(self.topic + '/relay/0/command', self.__rx_set__)
  49. def __rx_set__(self, client, userdata, message):
  50. data = message.payload.decode('utf-8')
  51. key = message.topic.split('/')[-3] + '/' + message.topic.split('/')[-2]
  52. self.logger.info("Received set data for %s: %s", key, repr(data))
  53. self.__set__(key, data)
  54. self.send_device_status(key)
  55. if key == "relay/0" and data.lower() == "on":
  56. self.power_on()
  57. def send_device_status(self, key):
  58. data = self[key]
  59. self.logger.info("Sending status for %s: %s", key, repr(data))
  60. self.mqtt_client.send(self.topic + '/' + key, data)