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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. import task
  35. class shelly_sw1(base):
  36. """A shelly device with switching functionality
  37. Args:
  38. mqtt_client (mqtt.mqtt_client): A MQTT Client instance
  39. topic (str): the base topic for this device
  40. kwargs (**dict): cd_r0=list of devices connected to relay/0
  41. """
  42. KEY_OUTPUT_0 = "relay/0"
  43. #
  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.__auto_off__ = None
  52. #
  53. self.mqtt_client.add_callback(self.topic + '/' + self.KEY_OUTPUT_0 + '/command', self.__rx_set__)
  54. #
  55. cmd_base = self.topic.replace('/', '.') + '.'
  56. self.user_cmds = {
  57. cmd_base + 'toggle': self.__ui_toggle_output_0__,
  58. }
  59. def __rx_set__(self, client, userdata, message):
  60. data = message.payload.decode('utf-8')
  61. key = message.topic.split('/')[-3] + '/' + message.topic.split('/')[-2]
  62. self.logger.info("Received set data for %s: %s", key, repr(data))
  63. self.__set__(key, data)
  64. def __set__(self, key, data):
  65. base.__set__(self, key, data)
  66. self.send_device_status(key)
  67. if key == self.KEY_OUTPUT_0:
  68. if data.lower() == "on":
  69. self.power_on(key)
  70. if self.__auto_off__ is not None:
  71. self.__auto_off__.run()
  72. else:
  73. if self.__auto_off__ is not None:
  74. self.__auto_off__.stop()
  75. def send_device_status(self, key):
  76. data = self[key]
  77. self.logger.info("Sending status for %s: %s", key, repr(data))
  78. self.mqtt_client.send(self.topic + '/' + key, data)
  79. def auto_off(self, sec):
  80. self.__auto_off__ = task.delayed(sec, self.__auto_off_func__)
  81. def __auto_off_func__(self):
  82. if self.get(self.KEY_OUTPUT_0).lower() != 'off':
  83. self.__set__(self.KEY_OUTPUT_0, "off")
  84. self.send_device_status(self.KEY_OUTPUT_0)
  85. def __ui_toggle_output_0__(self, *args):
  86. self.__set__(self.KEY_OUTPUT_0, 'off' if self.get(self.KEY_OUTPUT_0).lower() == 'on' else 'on')