Files
Marcel Hoppe 8fbf3540bd Discovery (#74)
* add support for Home Assistants MQTT discovery

* add format_topic a flexible separator

* add mqtt discovery for miflora

* add mqtt discovery for mithermometer

* add mqtt discovery for thermostat

* add unit to illuminance in miflora

* switch to 'away_mode_command_topic' instead of 'away_command_topic'

* revert changing the date format

* add mac to ientifiers and change the id generation

* add mac to ientifiers and change the id generation on thermostat

* add mac to ientifiers and change the id generation for mithermometer
2019-06-21 12:25:27 +02:00

79 lines
3.1 KiB
Python

from mqtt import MqttMessage, MqttConfigMessage
from workers.base import BaseWorker
import logger
REQUIREMENTS = ['miflora', 'bluepy']
monitoredAttrs = ["temperature", "moisture", "light", "conductivity", "battery"]
_LOGGER = logger.get(__name__)
class MifloraWorker(BaseWorker):
def _setup(self):
from miflora.miflora_poller import MiFloraPoller
from btlewrap.bluepy import BluepyBackend
_LOGGER.info("Adding %d %s devices", len(self.devices), repr(self))
for name, mac in self.devices.items():
_LOGGER.debug("Adding %s device '%s' (%s)", repr(self), name, mac)
self.devices[name] = {"mac": mac, "poller": MiFloraPoller(mac, BluepyBackend)}
def config(self):
ret = []
for name, data in self.devices.items():
ret += self.config_device(name, data["mac"])
return ret
def config_device(self, name, mac):
ret = []
device={"identifiers": [mac, self.format_id(name, separator="_")],
"manufacturer": "Xiaomi",
"model": "MiFlora",
"name": self.format_topic(name, separator=" ").title()}
for attr in monitoredAttrs:
if attr == 'light':
payload = {"unique_id": self.format_id(name, 'illuminance', separator="_"),
"state_topic": self.format_topic(name, attr),
"device_class": 'illuminance',
"unit_of_measurement": "lux",
"device": device}
elif attr == 'moisture':
payload = {"unique_id": self.format_id(name, attr, separator="_"),
"state_topic": self.format_topic(name, attr),
"icon": 'mdi:water',
"unit_of_measurement": "%",
"device": device}
elif attr == 'conductivity':
payload = {"unique_id": self.format_id(name, attr, separator="_"),
"state_topic": self.format_topic(name, attr),
"icon": 'mdi:leaf',
"unit_of_measurement": "µS/cm",
"device": device}
else:
payload = {"unique_id": self.format_id(name, attr, separator="_"),
"state_topic": self.format_topic(name, attr),
"device_class": attr,
"device": device}
ret.append(MqttConfigMessage(MqttConfigMessage.SENSOR, self.format_topic(name, attr, separator="_"), payload=payload))
return ret
def status_update(self):
_LOGGER.info("Updating %d %s devices", len(self.devices), repr(self))
ret = []
for name, data in self.devices.items():
_LOGGER.debug("Updating %s device '%s' (%s)", repr(self), name, data["mac"])
from btlewrap import BluetoothBackendException
try:
ret += self.update_device_state(name, data["poller"])
except BluetoothBackendException as e:
logger.log_exception(_LOGGER, "Error during update of %s device '%s' (%s): %s", repr(self), name, data["mac"], type(e).__name__, suppress=True)
return ret
def update_device_state(self, name, poller):
ret = []
poller.clear_cache()
for attr in monitoredAttrs:
ret.append(MqttMessage(topic=self.format_topic(name, attr), payload=poller.parameter_value(attr)))
return ret