Modbus in Home Assistant
Modbus is a polling-based communication protocol, used over both serial lines and TCP. It is widely used in the smart home for devices like solar inverters, energy meters, and other industrial equipment that has made its way into our homes.
Writing Modbus libraries
We require each integration to implement a library that handles the device-specific communication. To help build these libraries, we maintain modbus-connection, a Python package.
modbus-connection provides:
- A common, backend-neutral interface on top of pymodbus and tmodbus, two popular Modbus libraries, so you can swap backends without changing your code.
- A device modelling framework to map a device's data to typed Python attributes and read it in as few requests as possible (example from Trovis).
- A
pytestplugin to make testing your library easy.
For a complete example of the pattern, see trovis-modbus, a device library built on modbus-connection, and trovis-modbus-hass, the Home Assistant integration that consumes it.