项目作者: leech001

项目描述 :
Python class library for generate and send to Home Assistant over MQTT (AutoDiscovery) device data
高级语言: Python
项目地址: git://github.com/leech001/hass-mqtt-discovery.git
创建时间: 2021-05-08T15:02:39Z
项目社区:https://github.com/leech001/hass-mqtt-discovery

开源协议:

下载


HASS-MQTT-DISCOVERY

Python class library for generating and sending data to the Home Assistant via
MQTT (AutoDiscovery) of data from sensors and devices.

Build and Install

build wheel:

  1. # from top folder:
  2. pip install -e .
  3. flit build

install wheel:

  1. # inside dist folder:
  2. pip install hass_mqtt_discovery

Usage

  1. from hass
  2. mqtt_client = mqtt.Client("user")
  3. mqtt_client.on_connect = on_connect
  4. mqtt_client.on_message = on_message
  5. mqtt_client.username_pw_set("user", "pass")
  6. mqtt_client.connect("mqtt.mqtt.ru", 1883, 60)
  7. mqtt_client.loop_forever()

Sensor

  1. example_device = Device.from_config("example_device.yaml")
  2. inside_temperature_sensor = Sensor(
  3. mqtt_client,
  4. "Temperature 1",
  5. parent_device=example_device,
  6. unit_of_measurement="°C",
  7. topic_parent_level="inside",
  8. )
  9. outside_temperature_sensor = Sensor(
  10. mqtt_client,
  11. "Temperature 2",
  12. parent_device=example_device,
  13. unit_of_measurement="°C",
  14. topic_parent_level="outside",
  15. )
  16. inside_temperature_sensor.send(22)
  17. outside_temperature_sensor.send(5)

Component value read function

If the hardware sensor is used solely to publish to mqtt, the Sensor can fetch
the value by itself so keeping a reference to both the hardware and the mqtt
sensor is not required.

For example:

  1. inside_temperature_sensor.set_value_read_function(lambda: round(hardware_sensor.temperature, 2))

From here publishing to the mqtt server would be:

  1. inside_temperature_sensor.send()

Device tracker

  1. HA_gps = Tracker(mqtt_client, "MotoBoard GPS")
  2. HA_gps.send(55.5, 37.5, 0)

Binary sensor

  1. HA_alarm = Binary(mqtt_client, "MotoBoard alarm", "mdi:alarm-light")
  2. HA_alarm.send(1)