DCSBL / ha-homewizard-energy

The old custom Home Assistant integration for HomeWizard Energy. This integration is now available in Core.
https://community.home-assistant.io/t/custom-component-homewizard-energy-wifi-p1-meter-and-kwh-meter/227441
Apache License 2.0
26 stars 3 forks source link

Integration never starts working when networking is not (completely) ready during the startup/boot of HomeAssistant #67

Closed elwin1234 closed 2 years ago

elwin1234 commented 2 years ago

Describe the bug

When the network(ing) that is required to reach Energy devices is not fully operational during the start/boot of HASS (but restored later on), the integration never starts properly and all device states will become and stay unavailable.

Environment (please complete the following information)

To Reproduce

  1. Start home assistant without required network(ing) for Energy devices
  2. Enable networking

Expected behavior Integration will eventually init correct and Energy device states are updated, etc.

Log Output

2021-11-18 16:13:12 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry EnergySocket ( mysocket ) for homewizard_energy
Traceback (most recent call last):
  File "/snap/home-assistant-snap/x2/lib/python3.8/site-packages/aiohttp/connector.py", line 969, in _wrap_create_connection
    return await self._loop.create_connection(*args, **kwargs)  # type: ignore  # noqa
  File "/usr/lib/python3.8/asyncio/base_events.py", line 1025, in create_connection
    raise exceptions[0]
  File "/usr/lib/python3.8/asyncio/base_events.py", line 1010, in create_connection
    sock = await self._connect_sock(
  File "/usr/lib/python3.8/asyncio/base_events.py", line 924, in _connect_sock
    await self.sock_connect(sock, address)
  File "/usr/lib/python3.8/asyncio/selector_events.py", line 496, in sock_connect
    return await fut
  File "/usr/lib/python3.8/asyncio/selector_events.py", line 528, in _sock_connect_cb
    raise OSError(err, f'Connect call failed {address}')
OSError: [Errno 113] Connect call failed ('192.168.2.40', 80)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/var/snap/home-assistant-snap/x2/custom_components/homewizard_energy/__init__.py", line 143, in async_setup_entry
    await energy_api.initialize()
  File "/root/snap/home-assistant-snap/x2/.local/lib/python3.8/site-packages/aiohwenergy/hwenergy.py", line 48, in initialize
    status = await self.device.update()
  File "/root/snap/home-assistant-snap/x2/.local/lib/python3.8/site-packages/aiohwenergy/device.py", line 67, in update
    status, response = await self._request("get", "api")
  File "/root/snap/home-assistant-snap/x2/.local/lib/python3.8/site-packages/aiohwenergy/hwenergy.py", line 111, in request
    async with self._clientsession.request(
  File "/snap/home-assistant-snap/x2/lib/python3.8/site-packages/aiohttp/client.py", line 1117, in __aenter__
    self._resp = await self._coro
  File "/snap/home-assistant-snap/x2/lib/python3.8/site-packages/aiohttp/client.py", line 520, in _request
    conn = await self._connector.connect(
  File "/snap/home-assistant-snap/x2/lib/python3.8/site-packages/aiohttp/connector.py", line 535, in connect
    proto = await self._create_connection(req, traces, timeout)
  File "/snap/home-assistant-snap/x2/lib/python3.8/site-packages/aiohttp/connector.py", line 892, in _create_connection
    _, proto = await self._create_direct_connection(req, traces, timeout)
  File "/snap/home-assistant-snap/x2/lib/python3.8/site-packages/aiohttp/connector.py", line 1051, in _create_direct_connection
    raise last_exc
  File "/snap/home-assistant-snap/x2/lib/python3.8/site-packages/aiohttp/connector.py", line 1020, in _create_direct_connection
    transp, proto = await self._wrap_create_connection(
  File "/snap/home-assistant-snap/x2/lib/python3.8/site-packages/aiohttp/connector.py", line 975, in _wrap_create_connection
    raise client_error(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host 192.168.2.40:80 ssl:default [Connect call failed ('192.168.2.40', 80)]
DCSBL commented 2 years ago

Thanks! I've looked into this issue for a bit and I think it is already fixed in 0.12.0. Would you try to install that and check if your issue still persists?

It seems to be caused by an exception that is now handled properly. Home Assistant will now try to restart the connection even if it failed the first time.

elwin1234 commented 2 years ago

Yes, it looks like you are right! I installed 0.12.0 and now the integration just stars fine in case networking is not yet present. Tnx!