jesserizzo / envoy_reader

MIT License
37 stars 26 forks source link

Change inverters_production request_sync call to async #28

Closed gtdiehl closed 4 years ago

gtdiehl commented 4 years ago

With HA 0.109, inverter data collection is broken in my setup with an older Envoy device.

HA is displaying a message of WARNING (MainThread) [homeassistant.util.async_] Detected I/O inside the event loop. This is causing stability issues. Please report issue to the custom component author for Line 156 of the sensor code which calls inverters_production() on the envoy_reader side.

Changing the sync to an async call might fix the problem

gtdiehl commented 4 years ago

Investigating this I was unable to use the requests_async or the aiohttp libraries with Digest Authentication. The one library I did find was httpx.

Changing https://github.com/jesserizzo/envoy_reader/blob/ee176190ebe95d98d6414deecf6d710438270cf9/envoy_reader/envoy_reader.py#L359-L363 to

            async with httpx.AsyncClient() as client:
                response = await client.get("http://{}/api/v1/production/inverters"
                                            .format(self.host),
                                            auth=httpx.DigestAuth(self.username, self.password))

seems to address the issue were HA complains with the Detected I/O ... message, but now I am seeing some other exceptions being thrown from httpx

Traceback (most recent call last):

  File "/usr/local/lib/python3.7/site-packages/h11/_state.py", line 249, in _fire_event_triggered_transitions

    new_state = EVENT_TRIGGERED_TRANSITIONS[role][state][event_type]

KeyError: <class 'h11._events.ConnectionClosed'>
....
h11._util.RemoteProtocolError: can't handle event type ConnectionClosed when role=SERVER and state=SEND_RESPONSE
......
httpx._exceptions.ConnectionClosed: can't handle event type ConnectionClosed when role=SERVER and state=SEND_RESPONSE