Ape / samsungctl

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

add support for ssl and token based authentication #94

Closed eclair4151 closed 5 months ago

kdschlosser commented 5 years ago

any way this can be made to work automatically. without the need to specify which type.

use the same idea as far as the token file. if there is one that it will grab the token and assume the SSL connection. if there is not a token file it will assume the old way. when the connection is made the old way if the TV has the upgraded firmware it will return an error. if that error comes up then connect using the new mechanism and save the token to the file.

eclair4151 commented 5 years ago

i mean it think it would be even easier to use the port. if they specify 8001 its regular, 8002 we know its ssl and can straight use the correct url

eclair4151 commented 5 years ago

After more thought this implementation needs to be changed. Doing it this way means you can’t use this library to connect to multiple TVs they will all try to use the same token file which will be invalid. We need to store tokens on a device to device basis

kdschlosser commented 5 years ago

IDK if there are any docs for this package. If there is this would need to be updated to explain the 2 ports and also the fact they will get that allow deny on the TV.

eclair4151 commented 5 years ago

@kdschlosser i fixed the issue and added an example to the readme. everyone should use port 8002 now anyway since its supported on all tvs. Although i havent heard anything from @Ape. We may need to start thinking about creating a fork and hosting it ourselves if none of our changes get merged in

kdschlosser commented 5 years ago

would you be willing to do me a favor?? i do not have one of the newer Samsung TVs so I am not privileged enough to have the issues LOL.

I think they changed more then just the web socket aspect of things. I do not know what OS you are running but if you are running on Windows you can download a mess of tools for UPNP developing it's released by Intel. "Developer tools for UPNP technology" One of the tools in there is called a device sniffer. It basically sends out SSDP packets to discover devices on your network. If the TV is on then it should reply. I am looking for a specific class/ST /urn I want to know if the TVs are still sending out packets for urn:samsung.com:device:RemoteControlReceiver:1

I created a PR #96 that adds automatic discovery of the TV. I think it may not be working on the TV's that have the newest firmware. You can test the PR and see if it works or not. I can add some code to it to do some scraping of all the UPNP responses from Samsung devices they may have moved something. If they did I would be really surprised because the serial number that is broadcast in the UPNP stuff is the same for all TV's and that has never been fixed.

eclair4151 commented 5 years ago

Hey sorry about the delay. I will attempt to do this tonight and get back to you

kdschlosser commented 5 years ago

I found out the UPNP still does work. it was a coding blunder of mine with not setting the namespace when parsing the xml. doah!!!..

But I did also discover some kew things. even tho most of the UPNP API is locked out, some of the Get functions are not. so one can get feedback from the TV for things like setting the volume, muting. changing the channel. I do not believe there is one for the inputs I am still digging into that aspect of it.

vitalets commented 5 years ago

@kdschlosser Could you share the resulting UPNP requests when you've done? Thanks a lot for your research!

kdschlosser commented 5 years ago

I will add the upnp stuff to this library

joeljacobs commented 5 years ago

I can't make it work with your latest version "just use one connection method." Always get either "ConnectionResetError: [Errno 54] Connection reset by peer" or "websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed." But if I revert to the previous commit, and change method to "websocketssl" it works just fine. (I obviously changed to port 8002 before any of these tests)

kdschlosser commented 5 years ago

I have to fix the PR I know what is wrong. I will do it now.

** EDIT. My bad, I thought that was directed at me.

But I did make some changes to the automatic discovery via UPNP. It has better handling of multiple NIC's. It also grabs the device ID because the serial number for the upnp end of things is the same for every TV.

@eclair4151 are you still wanting to possibly create a fork of this??

eclair4151 commented 5 years ago

@kdschlosser i think at this point the best idea would be to fork this project. I know that Ape has been mostly hands off recently, and that's totally understandable considering how many unavoidable issues something like this will have given the vast set of different TVs, Firmware versions, and the fact that Samsung can change things at any point.

Unfortunately I know that i do not have the time to maintain something like this properly, and it will end up going down the same path. If someone else wants to fork it i will be more than happy to contribute my findings, and new features. With a new fork we will also be able to merge in all the other pending changes from other people, such as WOL to turn tvs on, and tv discovery using ssdp. If you or who ever decides to create a fork let me know and i will raise a new PR with these changes

kdschlosser commented 5 years ago

I already have a fork of this repo. So if you wanted to submit a PR against mine go ahead and I will accept it. I know that Ape is probably getting tired of dealing with Samsung changing things about all the time. I run a project where people depend on the plugins for it working properly. So I am always maintaining code for that and it wouldn't be that hard to keep this going as well. The only thing I am unable to do is I am unable to test against the newer TV's. I do not own one and nor will I ever purchase another piece of Samsung anything. I got burned real bad with a washing machine and dryer they made. was supposed to have the ability to control/monitor the things remotely. It has never functioned properly in that department. Now Samsung has gone and dropped support for the things all together. which causes a big issue because when the machine first gets powered on they try to connect. and they keep on doing so for 5 minutes. and I cannot use the machines until it is done. I would leave them on but after 2 minutes they automatically shut them selves off. I am quite pissed about this. they were not cheap. They are only 4 years old as well. and the life is expected to be around 15years for a family of 4. and then they started doing this BS with the TV's and locking them out from everything except smartthings. So i do feel for the users and I am willing to spend time to keep something like this project going just for the simple reason of them possibly not selling a smartthings to someone for the sheer ability to control a TV

fluxdigital commented 5 years ago

Is there an update on this issue at all? It sounds like you have a solution using tokens to authenticate but the changes need merging into the repo. I just bought a raspberry pi and installed this to control my Samsung TV and was disappointed to find out it doesn’t work. I’m an .NET developer so don’t really know python very well but I’m happy to try and help if I can.

kdschlosser commented 5 years ago

https://github.com/kdschlosser/samsungctl

fluxdigital commented 5 years ago

Ok so are you saying @eclair4151 needs to submit a PR with his fixes to your repo? this can then be merged and used instead of this repo?

fluxdigital commented 5 years ago

Ahr looking at it you've already done that: https://github.com/kdschlosser/samsungctl/commits/master

fluxdigital commented 5 years ago

@eclair4151 So what needs to happen to this library to use these fixes then? https://github.com/eclair4151/AlexaControlledSamsungTV

fluxdigital commented 5 years ago

It's ok I worked it out I think. Git clone this repo and then run: python -m samsungctl to update the pip library. Sorry I don't really do python. Hopefully this fixes the issues but I'll let you know. Thanks for your help and hard work on this.

kdschlosser commented 5 years ago

no worries m8. glad to have something available for you do use. thank @eclair4151 for providing the code to solve the problem.

sorry about not letting you know the fix was applied. there are several locations that have conversations going on this issue. I am not sure if Ape is going to continue maintaining this repo. You should chekc in on my repo for the time being for any updates. If Ape starts maintaining this one I will let people know. He is the one that created this library and I would never take from that. I am not sure if he is on vacation/holiday. or is burnt out and needs a break. If and when he domes back whatever changes have been made I will transfer over to him at that time. There are way to many people that rely on a working version. so we had to provide a place to get one.

fluxdigital commented 5 years ago

@kdschlosser and @eclair4151 thanks for your help with this. I used pip to update samsungctl to 0.7.1 and then I have updated mqtt_server.py to use port 8002 in the source from here: https://github.com/eclair4151/AlexaControlledSamsungTV

  elif tv_dict[tv_mac_address]['tv_model'][4] >= 'K':
        port = 8002
        method = 'websocket'

However I get:

Received a new message: 
b'{"operation": "TurnOff", "endpointid": "68:27:37:B2:B2:FE"}'
from topic: 
power/e515bf90-50cd-43c6-b7d8-6e464bce0583
--------------
Failed to send message to TV: Connection is already closed.
^CTraceback (most recent call last):
  File "alexasmartcli.py", line 181, in <module>
    mqtt_server.startServer(options.mute)
  File "/home/pi/Documents/AlexaControlledSamsungTV/helpers/mqtt_server.py", line 248, in startServer
    time.sleep(1)
KeyboardInterrupt

I have no devices showing as connected under connected devices on my Samsung TV so I'm unsure what I'm doing wrong here. Any ideas?

eclair4151 commented 5 years ago

Hey, I’m on vacation right now with no access to my tv for testing. I’ll get back to you on Tuesday when I return and we can try to sort out what’s going on 😊

fluxdigital commented 5 years ago

No worries @eclair4151. Enjoy your vacation.

kdschlosser commented 5 years ago

unfortunatly (well maybe not) I do not own a new Samsung TV. I have a D series and those do not have this type of connection. So I am not able to test it either. I know nothing about mqtt either. so not going to be much help there.

There have been reports of needing to change the timeout value in the script. I need to see the responses from the TV in order to make any changes if there is a timeout related issue.

You are a programmer. so you can make the changes needed in order to get the output I need. python is all about the tabs. (4 spaces) that is how it knows the code blocks. where as C/CPP is the semicolon. other then that it is vocabulary. (and not having to declare variables and variables being able to change what data they contain on the fly).

so you will want to open the remote_websocket.py file.

locate the method _read_response

def _read_response(self):

after the line

response = json.loads(response)

add this line

print(json.dumps(response), indent=4)

make sure to keep the same indent level as the line above.

then run the script. This will print out json data to the console I need to see that data. and once we manage to get the pairing working I will need to see that data when you send a couple of commands with the package. this is going to allow me to locate a marker I can use when a response comes in to release a lock that i will use instead of a sleep with a pre defined time. some commands will take longer then other for the TV to process. so if you want to change the volume rapidly you do not want a 5 second timeout between key presses. the use of a static timeout means that it would have to be set to whatever command it is that takes the longest to respond. I think this static timeout is what is causing the problem because the .5 seconds it is set to is simply not long enough for the TV to do it's thing before the package disconnects from the TV

Plus we also want to give the user some time to be able to click on the allow button when it appears on the TV.

Here is what I am going to do I will make a branch of my repo that will institute a longer timeout if there is no token stored. I will set it to say 10 seconds. and if there is a token then it will default to the .5 we can start of there and see if we can get it to connect.,

kdschlosser commented 5 years ago

ok here is the updated branch. we can chit chat about any issues or findings on my repo for this. I do not want to continue to clutter up Ape's repo

https://github.com/kdschlosser/samsungctl/tree/timeout_fix

nickw444 commented 4 years ago

Tested this with my Samsung RU8000, and works a charm! Would be great to get this merged in so upstream users in home-assistant can reap the rewards of this library 👍