I’m trying to create an integration that is using modbus.
So far I have this code in my init.py
"""test modbus Integration."""
from __future__ import annotations
import logging
import asyncio
from pymodbus.client import ModbusTcpClient
from homeassistant.helpers.typing import ConfigType
from homeassistant.core import HomeAssistant
_LOGGER = logging.getLogger(__name__)
DOMAIN = 'test_modbus'
async def async_setup(hass, config):
"""Set up the modbus component."""
hass.states.async_set('test_modbus.test', 'Works!')
return True
async def myconnect():
client = ModbusTcpClient('192.168.22.210', 502)
response = client.read_holding_registers(508, 1, slave=1)
result = response.registers
hass.async_create_task(myconnect())
return True
The test_modbus.test entity is created en can be seen in the developer’s tools.
Only the myconnect function is not working.
The same code outside HA works, the following give back results.
#!/usr/bin/env python
import time
from pymodbus.client import ModbusTcpClient
def connect():
client = ModbusTcpClient('192.168.22.210', 502)
response = client.read_holding_registers(499, 40, slave=1)
result = response.registers
client.close()
print (result)
connect()
I have read the developer docs, I am probably doing something wrong.
Have been trying for a few days now, but can;t figure out what is missing.
There are no errors, nothing in the log.