[mh] MQTT Items and Discovery

Dave Neudoerffer <[email protected]> Thu, 4 Feb 2021 16:53:42 -0500
Newsgroups gmane.comp.misc.misterhouse.user
Message-ID <CAOFx8LPrOxbH7CZ-jbcGFH=DY-cX_weNStoGjtrubsO+uwFTcQ@mail.gmail.com>
Here it is -- mqtt items and discovery.

I have just submitted a pull request that adds mqtt item support as well as
mqtt discovery as defined by
Home Assistant.  Full documentation is found in mqtt_items.pm, but I
summarize here.

I have been working on this for quite a while.  It isn't finished, but then
stuff like this rarely is.  But this
functionality does work well for me, so I wanted to get it available to all.

mqtt_items.pm:
- An mqtt item is a misterhouse object representing the mqtt device,
  managing all messages associated with that device.
- There are mqtt_RemoteItems for mqtt devices.  The item will receive state
messages
  from the device and publish command topics to the device.  This
  supports Tasmota, IOT4, and likely ESPurna devices
- Also supported are mqtt_LocalItems which are linked to an existing
  misterhouse item and will publish state messages and receive command
messages
  for that existing item
- MQTT LWT messages are also supported for online/offline status as
  well as other topics such as the brightness topic for dimmable devices.
  I have not yet added support for colour lights, but if someone can
  point me at a good MH implementation of a light with colour, I will take
a look.
- MQTT types supported: switch, light, binary_sensor, sensor
- .mht support for MQTT_REMOTEITEM, MQTT_LOCALITEM and MQTT_INSTMQTT
- MQTT_REMOTEITEMS support Tasmota, IOT4, and likely ESPurna devices
- MQTT_LOCALITEM supports almost any existing mh device that can be mapped
to an MQTT type
- MQTT_INSTMQTT supports devices published by the insteon-mqtt project
- These are statically defined MQTT objects, and do not need discovery, but
  defining the objects statically can be tedious.  See discovery below.

mqtt_discovery.pm:
- Discovery allows for the processing of discovery messages as published by
  mqtt devices.  Most mqtt devices have some support for discovery as
  defined by Home Assistant.  Usually it needs to be turned on on
  the device (eg. Tasmota -- setOption19 1).
  Misterhouse items are created (mqtt_RemoteItem) for
  the discovered devices.  These items can be written to a .mht file
  so the next time misterhouse is run, they will appear as first class
  misterhouse items.
- This module will also publish discovery information for mqtt_LocalItems,
  allowing misterhouse items to be discovered by Home Assistant, or by
another
  instance of misterhouse or anything else that can consume discovery
messages.
- I have tried this with TASMOTA and IOT4 devices, but it should work
  much more broadly.
- .mht support for MQTT_DISCOVERY class to process discovery messages and
  MQTT_DISCOVERED_ITEM item type for discovered items written to a .mht
  file by mqtt_Discovery::write_discovered_items().

mqtt.pm:
- These new modules use the existing broker support in mqtt.pm.
- I have tried very hard not to break anything in the existing mqtt.pm,
  including the existing mqtt_item class, but there have been some
  bug fixes and small enhancements made to support the new functionality:
    - multiple mqtt broker support has been fixed.  The code was there,
      but it shared a receive buffer across brokers which didn't work very
well :-)
    - enhanced debugging support with debug levels 1-3
    - the ability for an object to listen to a list of topics or topic
patterns.
      (thanx Gilles for the topic pattern support!)
    - I had to remove Gilles' short circuit when an object was found
matching
      a message topic because there are cases where multiple objects need to
      receive the same topic
    - optionally unloaded the set function, with the addition of a new
object
      method $obj->receive_mqtt_message().  This makes the item code more
understandable.
    - added methods for managing retained topics

Enjoy.  Open to any and all feedback or suggestions.

Dave

________________________________________________________
To unsubscribe from this list, go to: https://lists.sourceforge.net/lists/listinfo/misterhouse-users