giachello / beoplay

Home Assistant component to control BeoPlay including TVs, Speakers and others.
23 stars 8 forks source link

Exception thrown when starting HA #24

Closed KimCarlsen closed 1 year ago

KimCarlsen commented 1 year ago

HA version: 2022.12.8 Beoplay version: 2023.1.4 When restarting HA it throws exception as it cannot find TV. TV is in ECO mode, so it it not available on network all the time.

Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 986, in _wrap_create_connection return await self._loop.create_connection(*args, **kwargs) # type: ignore[return-value] # noqa File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1064, in create_connection raise exceptions[0] File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1049, in create_connection sock = await self._connect_sock( File "/usr/local/lib/python3.10/asyncio/base_events.py", line 960, in _connect_sock await self.sock_connect(sock, address) File "/usr/local/lib/python3.10/asyncio/selector_events.py", line 500, in sock_connect return await fut File "/usr/local/lib/python3.10/asyncio/selector_events.py", line 535, in _sock_connect_cb raise OSError(err, f'Connect call failed {address}') OSError: [Errno 113] Connect call failed ('192.168.0.106', 8080)

Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 281, in _async_setup_platform await asyncio.shield(task) File "/config/custom_components/beoplay/media_player.py", line 211, in async_setup_entry await _add_player(hass, async_add_entities, host) File "/config/custom_components/beoplay/media_player.py", line 132, in _add_player await speaker.async_update() File "/config/custom_components/beoplay/media_player.py", line 535, in async_update await self._speaker.async_get_device_info() File "/usr/local/lib/python3.10/site-packages/pybeoplay/init.py", line 249, in async_get_device_info r = await self.async_getReq("BeoDevice") File "/usr/local/lib/python3.10/site-packages/pybeoplay/init.py", line 96, in async_getReq async with self._clientsession.get( File "/usr/local/lib/python3.10/site-packages/aiohttp/client.py", line 1138, in aenter self._resp = await self._coro File "/usr/local/lib/python3.10/site-packages/aiohttp/client.py", line 535, in _request conn = await self._connector.connect( File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 542, in connect proto = await self._create_connection(req, traces, timeout) File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 907, in _createconnection , proto = await self._create_direct_connection(req, traces, timeout) File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 1206, in _create_direct_connection raise last_exc File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 1175, in _create_direct_connection transp, proto = await self._wrap_create_connection( File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 992, in _wrap_create_connection raise client_error(req.connection_key, exc) from exc aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host 192.168.0.106:8080 ssl:default [Connect call failed ('192.168.0.106', 8080)]

giachello commented 1 year ago

Hi thanks for reporting the bug. That's interesting. I thought eco mode only affects speakers (that's what the manual of my Avant 55 says). Anyhow can you please try this, and write back what you get: (replace the IP address with that of the TV, you can get it from its network configuration screen or from your router):

in your browser, try both TV in Eco mode and not in Eco mode: http://192.168.1.83:8080/BeoDevice/powerManagement/standby http://192.168.1.83:8080/BeoDevice

using curl (from https://curl.se).

curl --location --request POST 'http://192.168.1.83:8080/BeoZone/Zone/ActiveSourceType'  --header 'Content-Type: application/json' --data-raw '{"sourceType":{"type":"HDMI"}}'  

Send me back what you get... thanks!

giachello commented 1 year ago

By the way, Have you enabled Wake On Lan ?

image
KimCarlsen commented 1 year ago

Hi, did some more investigation. My TVs are BV11 and V1 Wake on lan is active. But i have not enabled quickstart, so after ~10 sec after poweroff of the TV, it stops replying on ping, and then the error comes. When quickstart is enabled, it replies to ping all the time and there is no errors. I run without quickstart for power saving.

Your integration works perfectly, as i'm able to do automations based on TV powerstate, only thing is the ugly excepetion in the logs when i turn them off.

giachello commented 1 year ago

got it. this is an easy fix, will go into the next version

KimCarlsen commented 1 year ago

Thanks. just updated through HACS, but it still fails. after restart. Looks like a new error. note: its random which TV(IP) that throws the error.

This error originated from a custom integration.

Logger: custom_components.beoplay.media_player Source: helpers/entity.py:538 Integration: Bang & Olufsen BeoPlay Devices First occurred: 06:58:41 (1 occurrences) Last logged: 06:58:41

Unexpected error in Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 986, in _wrap_create_connection return await self._loop.create_connection(*args, **kwargs) # type: ignore[return-value] # noqa File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1064, in create_connection raise exceptions[0] File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1049, in create_connection sock = await self._connect_sock( File "/usr/local/lib/python3.10/asyncio/base_events.py", line 960, in _connect_sock await self.sock_connect(sock, address) File "/usr/local/lib/python3.10/asyncio/selector_events.py", line 500, in sock_connect return await fut File "/usr/local/lib/python3.10/asyncio/selector_events.py", line 535, in _sock_connect_cb raise OSError(err, f'Connect call failed {address}') OSError: [Errno 113] Connect call failed ('192.168.0.124', 8080)

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

Traceback (most recent call last): File "/config/custom_components/beoplay/media_player.py", line 343, in async_update_status await self._speaker.async_notificationsTask(notif_callback) File "/usr/local/lib/python3.10/site-packages/pybeoplay/init.py", line 158, in async_notificationsTask async with self._clientsession.get(self._host_notifications) as response: File "/usr/local/lib/python3.10/site-packages/aiohttp/client.py", line 1138, in aenter self._resp = await self._coro File "/usr/local/lib/python3.10/site-packages/aiohttp/client.py", line 535, in _request conn = await self._connector.connect( File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 542, in connect proto = await self._create_connection(req, traces, timeout) File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 907, in _createconnection , proto = await self._create_direct_connection(req, traces, timeout) File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 1206, in _create_direct_connection raise last_exc File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 1175, in _create_direct_connection transp, proto = await self._wrap_create_connection( File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 992, in _wrap_create_connection raise client_error(req.connection_key, exc) from exc aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host 192.168.0.124:8080 ssl:default [Connect call failed ('192.168.0.124', 8080)]

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/config/custom_components/beoplay/media_player.py", line 309, in _start_poll_command await self.async_update_status() File "/config/custom_components/beoplay/media_player.py", line 347, in async_update_status self.async_schedule_update_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 690, in async_schedule_update_ha_state self.async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 538, in async_write_ha_state raise RuntimeError(f"Attribute hass is None for {self}") RuntimeError: Attribute hass is None for

KimCarlsen commented 1 year ago

btw its on 2023.1.6 of the integration

giachello commented 1 year ago

Try now. I cannot repro exactly the same situation but this may work. Note that running the plugin on power-saved devices creates stray entities so it's not recommended. Once the device is powered on, it should reconnect automatically.

KimCarlsen commented 1 year ago

Thanks. Tried with 2023.1.7.
Now when turning TV off i get a warning, which is way better than before, so no worries there.: "Logger: homeassistant.helpers.entity Source: helpers/entity.py:527 First occurred: 16:42:28 (1 occurrences) Last logged: 16:42:28 Update of media_player.stue is taking over 10 seconds"

However i still get an error after restart of HA. don't know if thats fixable? This error originated from a custom integration.

Logger: custom_components.beoplay.media_player Source: helpers/entity.py:538 Integration: Bang & Olufsen BeoPlay Devices First occurred: 16:45:14 (2 occurrences) Last logged: 16:45:14

Unexpected error in Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 986, in _wrap_create_connection return await self._loop.create_connection(*args, **kwargs) # type: ignore[return-value] # noqa File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1064, in create_connection raise exceptions[0] File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1049, in create_connection sock = await self._connect_sock( File "/usr/local/lib/python3.10/asyncio/base_events.py", line 960, in _connect_sock await self.sock_connect(sock, address) File "/usr/local/lib/python3.10/asyncio/selector_events.py", line 500, in sock_connect return await fut File "/usr/local/lib/python3.10/asyncio/selector_events.py", line 535, in _sock_connect_cb raise OSError(err, f'Connect call failed {address}') OSError: [Errno 113] Connect call failed ('192.168.0.124', 8080)

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

Traceback (most recent call last): File "/config/custom_components/beoplay/media_player.py", line 289, in async_update_status await self._speaker.async_notificationsTask(notif_callback) File "/usr/local/lib/python3.10/site-packages/pybeoplay/init.py", line 158, in async_notificationsTask async with self._clientsession.get(self._host_notifications) as response: File "/usr/local/lib/python3.10/site-packages/aiohttp/client.py", line 1138, in aenter self._resp = await self._coro File "/usr/local/lib/python3.10/site-packages/aiohttp/client.py", line 535, in _request conn = await self._connector.connect( File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 542, in connect proto = await self._create_connection(req, traces, timeout) File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 907, in _createconnection , proto = await self._create_direct_connection(req, traces, timeout) File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 1206, in _create_direct_connection raise last_exc File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 1175, in _create_direct_connection transp, proto = await self._wrap_create_connection( File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 992, in _wrap_create_connection raise client_error(req.connection_key, exc) from exc aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host 192.168.0.124:8080 ssl:default [Connect call failed ('192.168.0.124', 8080)]

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/config/custom_components/beoplay/media_player.py", line 255, in _start_poll_command await self.async_update_status() File "/config/custom_components/beoplay/media_player.py", line 293, in async_update_status self.async_schedule_update_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 690, in async_schedule_update_ha_state self.async_write_ha_state() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 538, in async_write_ha_state raise RuntimeError(f"Attribute hass is None for {self}") RuntimeError: Attribute hass is None for

giachello commented 1 year ago

That's odd. I thought I fixed it in the last code -- I tried to unplug one of my devices and it didn't do the exception. It's hard to debug this so won't have time for a week or two. hang on tight.

giachello commented 1 year ago

Hi @KimCarlsen , so I did look into this. Unfortunately, I am not going to change the behavior of the plugin to accommodate this use case. Home Assistant needs to confirm the device identity before adding it to the system. If the device doesn't respond when it's added to the system, it won't register. I would recommend turning on the speaker/tv when you restart HA. That way, even if it goes back to eco mode, the configuration will stick.

KimCarlsen commented 1 year ago

That will be pretty annoying since HA requires a lot of restarts when you edit something. And usually i'm playing with this while the family sleeps, so i can't go to bedrooms to turn them on.

It works for my Roku integration. where the Roku is powered off by the outlet. (actually i have an integration that turns on the power to the Roku, when the BV11 turns on. similar for off) When restarting HA, it makes some noise in log, and is shown as unavailable. when power is turned on, it becomes available in HA shortly after.

giachello commented 1 year ago

@KimCarlsen
Ok, actually I found an easy way to implement this, using ConfigEntryNotReady in home assistant. Check it out.

KimCarlsen commented 1 year ago

Awesome. it works perfectly. Some warnings in HA log as expected at reboot. TVs get available shortly after they are turned on.

Thanks for fixing it