123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- from devices.base import base
- from devices.base import warning
- import logging
-
-
- class silvercrest_powerplug(base):
- """ Communication (MQTT)
-
- silvercrest_powerplug {
- | "state": ["ON" / "OFF"]
- | "linkquality": [0...255] lqi
- | }
- +- get {
- | "state": ""
- | }
- +- set {
- "state": ["ON" / "OFF"]
- }
- """
- KEY_LINKQUALITY = "linkquality"
- KEY_OUTPUT_0 = "state"
- #
- TX_TYPE = base.TX_DICT
- TX_FILTER_DATA_KEYS = [KEY_OUTPUT_0]
- #
- RX_KEYS = [KEY_LINKQUALITY, KEY_OUTPUT_0]
- RX_FILTER_DATA_KEYS = [KEY_OUTPUT_0]
-
- def __init__(self, mqtt_client, topic):
- super().__init__(mqtt_client, topic)
-
- #
- # RX
- #
- @property
- def output_0(self):
- """rv: [True, False]"""
- return self.get(self.KEY_OUTPUT_0)
-
- @property
- def linkquality(self):
- """rv: numeric value"""
- return self.get(self.KEY_LINKQUALITY)
-
- #
- # TX
- #
- def set_output_0(self, state):
- """state: [True, False]"""
- self.send_command(self.KEY_OUTPUT_0, state)
-
- def set_output_0_mcb(self, device, key, data):
- self.logger.log(logging.INFO if data != self.output_0 else logging.DEBUG, "Changing output 0 to %s", str(data))
- self.set_output_0(data)
-
- def toggle_output_0_mcb(self, device, key, data):
- self.logger.info("Toggeling output 0")
- self.set_output_0(not self.output_0)
-
- def all_off(self):
- if self.output_0:
- self.set_output_0(False)
-
-
- class silvercrest_motion_sensor(base):
- """ Communication (MQTT)
-
- silvercrest_motion_sensor {
- battery: [0...100] %
- battery_low: [True, False]
- linkquality: [0...255] lqi
- occupancy: [True, False]
- tamper: [True, False]
- voltage: [0...] mV
- }
- """
- KEY_BATTERY = "battery"
- KEY_BATTERY_LOW = "battery_low"
- KEY_LINKQUALITY = "linkquality"
- KEY_OCCUPANCY = "occupancy"
- KEY_UNMOUNTED = "tamper"
- KEY_VOLTAGE = "voltage"
- #
- TX_TYPE = base.TX_DICT
- #
- RX_KEYS = [KEY_BATTERY, KEY_BATTERY_LOW, KEY_LINKQUALITY, KEY_OCCUPANCY, KEY_UNMOUNTED, KEY_VOLTAGE]
-
- def __init__(self, mqtt_client, topic):
- super().__init__(mqtt_client, topic)
- #
- self.add_callback(self.KEY_BATTERY_LOW, True, self.__warning__, True)
-
- #
- # WARNING CALL
- #
- def __warning__(self, client, key, data):
- w = warning(self.topic, warning.TYPE_BATTERY_LOW, "Battery low (%.1f%%)", self.get(self.KEY_BATTERY) or math.nan)
- self.logger.warning(w)
- self.set(self.KEY_WARNING, w)
-
- #
- # RX
- #
- @property
- def linkquality(self):
- """rv: numeric value"""
- return self.get(self.KEY_LINKQUALITY)
-
- @property
- def battery(self):
- """rv: numeric value"""
- return self.get(self.KEY_BATTERY)
|