MQTT Home Emulation
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

shelly.py 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. PROPERTIES = [
  43. "relay/0",
  44. ]
  45. def __init__(self, mqtt_client, topic):
  46. super().__init__(mqtt_client, topic)
  47. self["state"] = "off"
  48. #
  49. self.__auto_off__ = None
  50. #
  51. self.mqtt_client.add_callback(self.topic + '/relay/0/command', self.__rx_set__)
  52. def __rx_set__(self, client, userdata, message):
  53. data = message.payload.decode('utf-8')
  54. key = message.topic.split('/')[-3] + '/' + message.topic.split('/')[-2]
  55. self.logger.info("Received set data for %s: %s", key, repr(data))
  56. self.__set__(key, data)
  57. self.send_device_status(key)
  58. if key == "relay/0":
  59. if data.lower() == "on":
  60. self.power_on()
  61. if self.__auto_off__ is not None:
  62. self.__auto_off__.run()
  63. else:
  64. if self.__auto_off__ is not None:
  65. self.__auto_off__.stop()
  66. def send_device_status(self, key):
  67. data = self[key]
  68. self.logger.info("Sending status for %s: %s", key, repr(data))
  69. self.mqtt_client.send(self.topic + '/' + key, data)
  70. def auto_off(self, sec):
  71. self.__auto_off__ = task.delayed(sec, self.__auto_off_func__)
  72. def __auto_off_func__(self):
  73. if self.get(self.PROPERTIES[0]).lower() != 'off':
  74. self.__set__(self.PROPERTIES[0], "off")
  75. self.send_device_status(self.PROPERTIES[0])