HA integration: Not connected errors #2905
-
|
Hello, In my integration's @dataclass
class FlointData:
"Keeps the AsyncModbusTcpClient instance."
client: AsyncModbusTcpClient
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up from config entry."""
ip_address = entry.data["IP address"]
client = AsyncModbusTcpClient(ip_address, port=502)
await client.connect()
# Save the client instance to reuse it from entities
entry.runtime_data = FlointData(client=client)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return TrueIn the class TotalImportedEnergy(SensorEntity):
def __init__(self, entry: ConfigEntry):
self.client = self._entry.runtime_data.client
async def async_update(self) -> None:
"""Fetch new state data for the sensor."""
_LOGGER.debug("Client state before reading registers: %s", self.client)
_LOGGER.debug("Client %s", type(self.client))
_LOGGER.debug(
"is socket open? %s",
self.client.connected,
)
# Error happens in next line
result = await self.client.read_input_registers(13036, count=2, device_id=1)
if result.isError():
_LOGGER.error(
"Error reading registers for IP %s, register 13036: %s",
self._ip_address,
result,
)
return
value = self.client.convert_from_registers(
result.registers, data_type=self.client.DATATYPE.UINT32, word_order="little"
)
self._attr_native_value = value * 0.1the marked line gives me this backtrace: Claude told me, that the connection is silently dropped, but https://pymodbus.readthedocs.io/en/latest/source/client.html#asynchronous-example says that it should auto-reconnect. Also, there is no other instance using that client. What can be the problem here? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
|
It is hard to see what happens since you have not enabled pymodbus debut log. Did the client connect to the device and then lost connection...or did it never connect ? The connection is never dropped in a client...but if the device drops the connection it will automatically reconnect. Please activate the pymodbus debug log, then you will get a lot more information about what actually happens. It would also be good to check the return code of the connect call. |
Beta Was this translation helpful? Give feedback.
-
Ok, my apologies! I did not know there were two projects with a very similar name. What is pymodbus-n about? It is just an old version of that project (pymodbus-dev)? The "Edit on GitHub" link from the pymodbus-n docs links to that projects here...?
For completeness, the code now looks like import logging
from pymodbus import pymodbus_apply_logging_config
from pymodbus.client import AsyncModbusTcpClient
import asyncio
registers = {"version_1": 2581, "version_2": 2596, "load_power": 13007}
async def get_load_power(client):
result = await client.read_input_registers(
registers["load_power"], count=2, device_id=1
)
if result.isError():
print("Error reading registers")
return None
value = client.convert_from_registers(
result.registers, data_type=client.DATATYPE.INT32, word_order="little"
)
return value
async def main():
pymodbus_apply_logging_config("DEBUG")
client = AsyncModbusTcpClient("192.168.0.36")
print("Connecting to device...")
await client.connect()
print("Connected to device")
value = await get_load_power(client)
print("Load Power 1:", value)
await asyncio.sleep(2)
value = await get_load_power(client)
print("Load Power 2:", value)
client.close()
if __name__ == "__main__":
asyncio.run(main())producing log output: My conclusion so far
Can you agree with that?
I will investigate, thanks for the hint. Best, Florian |
Beta Was this translation helpful? Give feedback.
-
|
I have no idea what pymodbus-n is about, I suggest you ask the project ! Your conclusion seems correct. |
Beta Was this translation helpful? Give feedback.
-
|
Please be aware of the timing: Recv at 10:08:22,830 So it is very likely that the device did not like the request, are you sure the address is an input register, and not e.g. a holding register. Later you see that pymodbus connects successfully, but the device immediately closes, which typically signals the device is in a problematic state, and maybe needs a reset. |
Beta Was this translation helpful? Give feedback.
The example you refer to is from another project !!! this is the pymodbus project.
All our examples like e.g. https://pymodbus.readthedocs.io/en/latest/source/examples.html#ready-to-run-examples contains the correct call.