home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.69k stars 30.82k forks source link

Samsung TV turns ON after restarting HA #92676

Closed VenominousX closed 1 year ago

VenominousX commented 1 year ago

The problem

Hello, my Samsung TV turns on after restarting Home Assistant. I have been able to reproduce this behavior several times. The integration sets the status to ON even if the TV was turned off before the restart and the integration set the status to OFF. This happens both when using sqlite or MariaDB.

I don't have any automations or scripts that turn on my TV after reboot.

I restart my Home Asisstant instances very early every morning, as it provides the best reliability throughout the day after a few months of testing. A TV that then turns on without a specific request is no good. Since intentionally turning on the TV triggers a number of other automations, logically those are then triggered when Home Assistant is restarted. Besides wasting energy, it is also not nice for visitors to be woken up by the sudden switch-on.

Best regards Marcus

What version of Home Assistant Core has the issue?

2023.4.6, same on 2023.5.2

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

Samsung Smart TV

Link to integration documentation on our website

https://www.home-assistant.io/integrations/samsungtv

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

Before restart:
2023-05-06 13:03:43.060 DEBUG (MainThread) [homeassistant.components.samsungtv] TV 10.11.12.101 state updated to off

After restart:
2023-05-06 13:05:48.404 DEBUG (MainThread) [homeassistant.components.samsungtv] Checking if TV 10.11.12.101 is on using websocket
2023-05-06 13:05:48.404 DEBUG (MainThread) [homeassistant.components.samsungtv] Create SamsungTVWSBridge for 10.11.12.101
2023-05-06 13:05:51.513 DEBUG (MainThread) [homeassistant.components.samsungtv] Failed to get remote for 10.11.12.101: OSError(113, "Connect call failed ('10.11.12.101', 8002)")
2023-05-06 13:05:51.513 DEBUG (MainThread) [homeassistant.components.samsungtv] TV 10.11.12.101 state updated to off
...
2023-05-06 13:08:01.543 DEBUG (MainThread) [homeassistant.components.samsungtv] Create SamsungTVWSBridge for 10.11.12.101
2023-05-06 13:08:02.672 DEBUG (MainThread) [homeassistant.components.samsungtv] Created SamsungTVWSBridge for 10.11.12.101
2023-05-06 13:08:02.749 DEBUG (MainThread) [homeassistant.components.samsungtv] Device info on 10.11.12.101 is: {'device': {'FrameTVSupport': 'false', 'GamePadSupport': 'true', 'ImeSyncedSupport': 'true', 'Language': 'de_DE', 'OS': 'Tizen', 'PowerState': 'standby', 'TokenAuthSupport': 'true', 'VoiceSupport': 'true', 'WallScreenRatio': '-1', 'WallService': 'false', 'countryCode': 'DE', 'description': 'Samsung DTV RCR', 'developerIP': '0.0.0.0', 'developerMode': '0', 'duid': 'uuid:704ffe42-66c8-435d-8fdc-bfc9e6d1627c', 'firmwareVersion': 'Unknown', 'id': 'uuid:704ffe42-66c8-435d-8fdc-bfc9e6d1627c', 'ip': '10.11.12.101', 'model': '21_NIKEM2_QTV', 'modelName': 'GQ55QN85AATXZG', 'name': 'VenominousTV', 'networkType': 'wired', 'resolution': '3840x2160', 'smartHubAgreement': 'true', 'type': 'Samsung SmartTV', 'udn': 'uuid:704ffe42-66c8-435d-8fdc-bfc9e6d1627c', 'wifiMac': '7c:0a:3f:b1:bd:2d'}, 'id': 'uuid:704ffe42-66c8-435d-8fdc-bfc9e6d1627c', 'isSupport': '{"DMP_DRM_PLAYREADY":"false","DMP_DRM_WIDEVINE":"false","DMP_available":"true","EDEN_available":"true","FrameTVSupport":"false","ImeSyncedSupport":"true","TokenAuthSupport":"true","remote_available":"true","remote_fourDirections":"true","remote_touchPad":"true","remote_voiceControl":"true"}\n', 'name': 'VenominousTV', 'remote': '1.0', 'type': 'Samsung SmartTV', 'uri': 'https://10.11.12.101:8002/api/v2/', 'version': '2.0.25'}
2023-05-06 13:08:02.750 DEBUG (MainThread) [homeassistant.components.samsungtv] TV 10.11.12.101 state updated to on

Additional information

No response

home-assistant[bot] commented 1 year ago

Hey there @chemelli74, @epenet, mind taking a look at this issue as it has been labeled with an integration (samsungtv) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `samsungtv` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign samsungtv` Removes the current integration label and assignees on the issue, add the integration domain after the command.

(message by CodeOwnersMention)


samsungtv documentation samsungtv source (message by IssueLinks)

holysoles commented 1 year ago

Hey @VenominousX, could you share your logbook data for the full time range (before restart until the power on event)?

VenominousX commented 1 year ago

Hey @holysoles , here it is:

before restart

turn off TV

after restart

Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 980, 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 1076, in create_connection raise exceptions[0] File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1060, in create_connection sock = await self._connect_sock( File "/usr/local/lib/python3.10/asyncio/base_events.py", line 969, in _connect_sock await self.sock_connect(sock, address) File "/usr/local/lib/python3.10/asyncio/selector_events.py", line 501, in sock_connect return await fut File "/usr/local/lib/python3.10/asyncio/selector_events.py", line 541, in _sock_connect_cb raise OSError(err, f'Connect call failed {address}') ConnectionRefusedError: [Errno 111] Connect call failed ('10.11.12.101', 9197)

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

Traceback (most recent call last): File "/usr/local/lib/python3.10/site-packages/async_upnp_client/aiohttp.py", line 170, in async_http_request return await self._async_http_request(method, url, headers, body) File "/usr/local/lib/python3.10/site-packages/async_upnp_client/aiohttp.py", line 204, in _async_http_request async with self._session.request( File "/usr/local/lib/python3.10/site-packages/aiohttp/client.py", line 1141, in aenter self._resp = await self._coro File "/usr/local/lib/python3.10/site-packages/aiohttp/client.py", line 536, in _request conn = await self._connector.connect( File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 540, in connect proto = await self._create_connection(req, traces, timeout) File "/usr/local/lib/python3.10/site-packages/aiohttp/connector.py", line 901, 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 988, in _wrap_create_connection raise client_error(req.connection_key, exc) from exc aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host 10.11.12.101:9197 ssl:default [Connect call failed ('10.11.12.101', 9197)]

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

Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/samsungtv/media_player.py", line 287, in _async_startup_dmr upnp_device = await upnp_factory.async_create_device( File "/usr/local/lib/python3.10/site-packages/async_upnp_client/client_factory.py", line 72, in async_create_device root_el = await self._async_get(description_url) File "/usr/local/lib/python3.10/site-packages/async_upnp_client/client_factory.py", line 394, in _async_get ) = await self.requester.async_http_request("GET", url) File "/usr/local/lib/python3.10/site-packages/async_upnp_client/aiohttp.py", line 172, in async_http_request raise UpnpConnectionError(repr(err)) from err async_upnp_client.exceptions.UpnpConnectionError: ('ClientConnectorError(ConnectionKey(host=\'10.11.12.101\', port=9197, is_ssl=False, ssl=None, proxy=None, proxy_auth=None, proxy_headers_hash=-8223295724473837405), ConnectionRefusedError(111, "Connect call failed (\'10.11.12.101\', 9197)"))', None)

image

holysoles commented 1 year ago

I think this was introduced with PR #67541

HA polls for the device power status using SamsungTVWSBridge.async_is_on(). However on startup, when we test the contents of self._device_infohere, it isn't defined yet since the device hasn't been polled yet.
As a result we fall back to usingSamsungTVWSBaseBridge.async_is_on() to get the device power state, but as noted here, using a websocket connection to a powered off TV can cause it to be powered on. This explains why in your logs the integration is trying to connect via websocket, then eventually starts connecting via device info later since self.device_info becomes defined.

We should implement the same init check done here, I'll get a PR opened up.

VenominousX commented 1 year ago

That sounds comprehensible. Thank you @holysoles for your effort and support.

leaskovski commented 1 year ago

Watching this... I think this might be the reason why my samsung TV keeps flagging as being truned on, yet isnt actually on.

issue-triage-workflows[bot] commented 1 year ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.