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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 sw_plain(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, **kwargs):
  45. super().__init__(mqtt_client, topic, **kwargs)
  46. if getattr(self, 'cd_r0', None) is None:
  47. self.cd_r0 = []
  48. self["state"] = "off"
  49. #
  50. self.mqtt_client.add_callback(self.topic + '/relay/0/command', self.__rx_set__)
  51. def __rx_set__(self, client, userdata, message):
  52. data = message.payload.decode('utf-8')
  53. key = message.topic.split('/')[-3] + '/' + message.topic.split('/')[-2]
  54. self.logger.info("Received set data for %s: %s", key, repr(data))
  55. self.__set__(key, data)
  56. self.send_device_status(key)
  57. if key == "relay/0":
  58. for d in self.cd_r0:
  59. d.set_state(data.lower() == "on")
  60. def send_device_status(self, key):
  61. data = self[key]
  62. self.logger.info("Sending status for %s: %s", key, repr(data))
  63. self.mqtt_client.send(self.topic + '/' + key, data)