Ape / samsungctl

Remote control Samsung televisions via a TCP/IP connection
MIT License
713 stars 191 forks source link

Library no longer functions for websocket commands since firmware update. "Invalid opcode" #93

Open eclair4151 opened 5 years ago

eclair4151 commented 5 years ago

My Samsung M series TV updated to firmware 1250 last night. They must have changed the websocket protocol because now this library and every other 3rd party Samsung app no longer works.

Traceback (most recent call last): File "C:/Users/tomer/PycharmProjects/samsung/main.py", line 15, in with samsungctl.Remote(tv_config) as remote: File "C:\Users\tomer\AppData\Local\Programs\Python\Python35-32\lib\site-packages\samsungctl\remote.py", line 10, in init self.remote = RemoteWebsocket(config) File "C:\Users\tomer\AppData\Local\Programs\Python\Python35-32\lib\site-packages\samsungctl\remote_websocket.py", line 25, in init self._read_response() File "C:\Users\tomer\AppData\Local\Programs\Python\Python35-32\lib\site-packages\samsungctl\remote_websocket.py", line 62, in _read_response response = self.connection.recv() File "C:\Users\tomer\AppData\Local\Programs\Python\Python35-32\lib\site-packages\websocket_core.py", line 293, in recv opcode, data = self.recv_data() File "C:\Users\tomer\AppData\Local\Programs\Python\Python35-32\lib\site-packages\websocket_core.py", line 310, in recv_data opcode, frame = self.recv_data_frame(control_frame) File "C:\Users\tomer\AppData\Local\Programs\Python\Python35-32\lib\site-packages\websocket_core.py", line 323, in recv_data_frame frame = self.recv_frame() File "C:\Users\tomer\AppData\Local\Programs\Python\Python35-32\lib\site-packages\websocket_core.py", line 357, in recv_frame return self.frame_buffer.recv_frame() File "C:\Users\tomer\AppData\Local\Programs\Python\Python35-32\lib\site-packages\websocket_abnf.py", line 362, in recv_frame frame.validate(self.skip_utf8_validation) File "C:\Users\tomer\AppData\Local\Programs\Python\Python35-32\lib\site-packages\websocket_abnf.py", line 173, in validate raise WebSocketProtocolException("Invalid close opcode.") websocket._exceptions.WebSocketProtocolException: Invalid close opcode.

Here is the crash although it most likely isn't very helpful. I will work on reverse engineering the official Samsung smart view app tomorrow which still works fine.

cnschulz commented 5 years ago

Just read the thread above. Download the ha 80 package and extract the Samsung TV component into a custom component folder.

On Tue, 8 Jan 2019, 03:40 Alok Saboo <notifications@github.com wrote:

@cnschulz https://github.com/cnschulz Can you please indicate how you integrated the latest changes in HA? I would love to test it out.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Ape/samsungctl/issues/93#issuecomment-451997260, or mute the thread https://github.com/notifications/unsubscribe-auth/ABNw5DNav8zmsYxDqi2WdoSbrBf3vXbuks5vA3h1gaJpZM4X7gQf .

arsaboo commented 5 years ago

I have it in a custom component already, just not sure how to link it to the new package. What do you have in REQUIREMENTS? In fact, can you hastebin your samsungtv.py file?

Murph24 commented 5 years ago

@arsaboo Check this link out. You can also put the new library as a custom component as well.

https://github.com/home-assistant/home-assistant/issues/17802#issuecomment-444746826

Things have changed a lot with this new library, so the guide is somewhat outdated. The basic idea is there, but you'll have to look over the new import statements and make adjustments as necessary

arsaboo commented 5 years ago

Thanks @Murph24

I updated the samsungtv.py and added the files from combination_branch to the samsungctl folder.

keatontaylor commented 5 years ago

@arsaboo does this new code you posted above work on your NU8000 series? I am getting a connection failed message. :(

arsaboo commented 5 years ago

@keatontaylor Yes, I am able to see the TV status and turn on the TV from HA. Nothing else is working for me at the moment.

You can copy/paste the structure that I have and it should work for you. I did manually update the firmware to 1202 on my TV (not sure if it makes a difference).

djungelola commented 5 years ago

Get this. samsungtv.py and samsungctl in custom_components

2019-01-10 21:15:15 INFO (MainThread) [homeassistant.loader] Loaded media_player.samsungtv from custom_components.media_player.samsungtv 2019-01-10 21:15:15 WARNING (MainThread) [homeassistant.loader] You are using a custom component for media_player.samsungtv which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you do experience issues with Home Assistant. 2019-01-10 21:15:20 INFO (MainThread) [homeassistant.components.media_player] Setting up media_player.samsungtv 2019-01-10 21:15:21 ERROR (MainThread) [homeassistant.components.media_player] Error while setting up platform samsungtv File "/config/custom_components/media_player/samsungtv.py", line 81, in setup_platform File "/config/custom_components/media_player/samsungtv.py", line 93, in init from custom_components.samsungctl import exceptions

fbnts commented 5 years ago

Not sure if this belongs on this issue thread or the library referenced above, I have just tried the new files but get errors in the log when trying to send any remote codes like vol up/down:

2019-01-09 00:50:54 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/homeassistant/helpers/service.py", line 279, in _handle_service_platform_call
    await func(entity, data)
  File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/homeassistant/.homeassistant/custom_components/media_player/samsungtv.py", line 237, in mute_volume
    self.send_key('KEY_MUTE')
  File "/home/homeassistant/.homeassistant/custom_components/media_player/samsungtv.py", line 166, in send_key
    self.get_remote().control(key)
  File "/home/homeassistant/.homeassistant/custom_components/media_player/samsungtv.py", line 151, in get_remote
    self._remote = self._remote_class(self._config)
  File "/home/homeassistant/.homeassistant/custom_components/samsungctl/remote.py", line 11, in __init__
    self.remote = RemoteLegacy(config)
  File "/home/homeassistant/.homeassistant/custom_components/samsungctl/remote_legacy.py", line 39, in __init__
    self._read_response(True)
  File "/home/homeassistant/.homeassistant/custom_components/samsungctl/remote_legacy.py", line 71, in _read_response
    tv_name_len = int(codecs.encode(header[1:3], 'hex'), 16)
ValueError: invalid literal for int() with base 16: b
nst197 commented 5 years ago

I'm trying to get the changes for this added to HA and having a load of grief. Can someone clarify please if you have to add a token parameter to a config file somewhere? Mine was complaining about an unknown key 'token' in the config dict.

Also, once this is working should it continue working without re-auth once the TV has been restarted. or do you need to allow it every time you turn it back on?

skynet01 commented 5 years ago

Is there any updates on this? Would be great if this gets merged and we can get our Samsung TVs working again :) :)

eclair4151 commented 5 years ago

@skynet01 check out this fork that is still actively maintained and has been updated for all TV's https://github.com/kdschlosser/samsungctl

skynet01 commented 5 years ago

Thanks @eclair4151, I am setting this up right now. Too bad it's not included in the official build since no new Samsung TVs will work with the current HA implementation

marysieek commented 5 years ago

For anyone trying to add Samsung TV to Home Assistant, I've created a simple custom component: https://github.com/marysieek/smart_home/tree/master/custom_components/samsungtv_custom

I can test the code only with UE55MU6102K. The component communicates through WSS, over :8002 port and does not require re-auth with token - I'm still working on making the experience easier. Please note it is not a professional solution - just a result of weekend coding.

sirs2k commented 5 years ago

For anyone trying to add Samsung TV to Home Assistant, I've created a simple custom component: https://github.com/marysieek/smart_home/tree/master/custom_components/samsungtv_custom

How can we do this on Hass.OS ?

skynet01 commented 5 years ago

Thanks for addon @marysieek, On hassio i can't seem to install samsung-api requirement since hassio comes with pytho2.7 and it needs at least 3 to install it. Unless i am doing something wrong

zaileion commented 4 years ago

So I'm new to samsungctl, just started using it last night... Its been an effort but now the TV is responding and asking me to allow, deny the device meaning my laptop. the problem is I get a permission denied and when I run the command again it simply re-asks me to allow/deny it again followed by command Traceback to permission denied again.
Samsung Model QN65Q9FNA

Here is the output any help would be appreciated $ samsungctl --host 192.168.1.27 --port 8002 --method websocket KEY_POWEROFF { "data": { "token": "81994378", "clients": [ { "isHost": false, "attributes": { "name": "c2Ftc3VuZ2N0bA==" }, "id": "40876f0-826a-4c27-acb4-649ef4d8d6d", "connectTime": 1572803997333, "deviceName": "c2Ftc3VuZ2N0bA==" } ], "id": "40876f0-826a-4c27-acb4-649ef4d8d6d" }, "event": "ms.channel.connect" } Traceback (most recent call last): File "/usr/local/bin/samsungctl", line 11, in <module> load_entry_point('samsungctl==0.7.1+1', 'console_scripts', 'samsungctl')() File "/usr/local/lib/python2.7/dist-packages/samsungctl-0.7.1+1-py2.7.egg/samsungctl/__main__.py", line 110, in main with Remote(config) as remote: File "/usr/local/lib/python2.7/dist-packages/samsungctl-0.7.1+1-py2.7.egg/samsungctl/remote.py", line 11, in __init__ self.remote = RemoteWebsocket(config) File "/usr/local/lib/python2.7/dist-packages/samsungctl-0.7.1+1-py2.7.egg/samsungctl/remote_websocket.py", line 55, in __init__ self._read_response() File "/usr/local/lib/python2.7/dist-packages/samsungctl-0.7.1+1-py2.7.egg/samsungctl/remote_websocket.py", line 100, in _read_response with open(self.token_file, "w") as token_file: IOError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/samsungctl-0.7.1+1-py2.7.egg/samsungctl/token.txt'