Smarthome Functionen
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ů.

silvercrest.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. from devices.base import base
  5. from devices.base import warning
  6. import logging
  7. class silvercrest_powerplug(base):
  8. """ Communication (MQTT)
  9. silvercrest_powerplug {
  10. | "state": ["ON" / "OFF"]
  11. | "linkquality": [0...255] lqi
  12. | }
  13. +- get {
  14. | "state": ""
  15. | }
  16. +- set {
  17. "state": ["ON" / "OFF"]
  18. }
  19. """
  20. KEY_LINKQUALITY = "linkquality"
  21. KEY_OUTPUT_0 = "state"
  22. #
  23. TX_TYPE = base.TX_DICT
  24. TX_FILTER_DATA_KEYS = [KEY_OUTPUT_0]
  25. #
  26. RX_KEYS = [KEY_LINKQUALITY, KEY_OUTPUT_0]
  27. RX_FILTER_DATA_KEYS = [KEY_OUTPUT_0]
  28. def __state_logging__(self, inst, key, data):
  29. if key in [self.KEY_OUTPUT_0]:
  30. self.logger.info("State change of '%s' to '%s'", key, repr(data))
  31. #
  32. # RX
  33. #
  34. @property
  35. def output_0(self):
  36. """rv: [True, False]"""
  37. return self.get(self.KEY_OUTPUT_0)
  38. @property
  39. def linkquality(self):
  40. """rv: numeric value"""
  41. return self.get(self.KEY_LINKQUALITY)
  42. #
  43. # TX
  44. #
  45. def set_output_0(self, state):
  46. """state: [True, False]"""
  47. self.send_command(self.KEY_OUTPUT_0, state)
  48. def set_output_0_mcb(self, device, key, data):
  49. self.set_output_0(data)
  50. def toggle_output_0_mcb(self, device, key, data):
  51. self.set_output_0(not self.output_0)
  52. def all_off(self):
  53. if self.output_0:
  54. self.set_output_0(False)
  55. class silvercrest_motion_sensor(base):
  56. """ Communication (MQTT)
  57. silvercrest_motion_sensor {
  58. battery: [0...100] %
  59. battery_low: [True, False]
  60. linkquality: [0...255] lqi
  61. occupancy: [True, False]
  62. tamper: [True, False]
  63. voltage: [0...] mV
  64. }
  65. """
  66. KEY_BATTERY = "battery"
  67. KEY_BATTERY_LOW = "battery_low"
  68. KEY_LINKQUALITY = "linkquality"
  69. KEY_OCCUPANCY = "occupancy"
  70. KEY_UNMOUNTED = "tamper"
  71. KEY_VOLTAGE = "voltage"
  72. #
  73. TX_TYPE = base.TX_DICT
  74. #
  75. RX_KEYS = [KEY_BATTERY, KEY_BATTERY_LOW, KEY_LINKQUALITY, KEY_OCCUPANCY, KEY_UNMOUNTED, KEY_VOLTAGE]
  76. def __init__(self, mqtt_client, topic):
  77. super().__init__(mqtt_client, topic)
  78. #
  79. self.add_callback(self.KEY_BATTERY_LOW, True, self.__warning__, True)
  80. def __state_logging__(self, inst, key, data):
  81. if key in [self.KEY_OCCUPANCY, self.KEY_UNMOUNTED]:
  82. self.logger.info("State change of '%s' to '%s'", key, repr(data))
  83. #
  84. # WARNING CALL
  85. #
  86. def __warning__(self, client, key, data):
  87. w = warning(self.topic, warning.TYPE_BATTERY_LOW, "Battery low (%.1f%%)", self.get(self.KEY_BATTERY) or math.nan)
  88. self.logger.warning(w)
  89. self.set(self.KEY_WARNING, w)
  90. #
  91. # RX
  92. #
  93. @property
  94. def linkquality(self):
  95. """rv: numeric value"""
  96. return self.get(self.KEY_LINKQUALITY)
  97. @property
  98. def battery(self):
  99. """rv: numeric value"""
  100. return self.get(self.KEY_BATTERY)