samsungctl icon indicating copy to clipboard operation
samsungctl copied to clipboard

Python3 Homeassistant

Open AidasK opened this issue 7 years ago • 56 comments

I would like to use this lib in home assistant, but it is running on python3. Any volunteers to make this code compatible with py3?

AidasK avatar Jan 11 '19 18:01 AidasK

the code is python 3 compatible. You stumbled across a bug I need to fix. You were using the wrong connection method to connect to your TV.

There is a Samsung plugin added to home assistant. This one is a fork of the one that is being used actually. I do not believe the original author of this library is maintaining it anymore. If you want to see this library added to home assistant you need to tell the folks over at home assistant to do it.

kdschlosser avatar Jan 11 '19 19:01 kdschlosser

actually you didn't stumble across a bug. you didn't specify the connection method so it defaulted to the legacy connection method, which your TV is not going to respond to. so that error was because it was trying to convert an empty string into an int. which it cannot do. so it kicked out an error.

kdschlosser avatar Jan 11 '19 19:01 kdschlosser

Since the original lib is not being maintained, @kdschlosser can you publish yours to PyPi? HA folks cannot publish your repo 😉

arsaboo avatar Jan 11 '19 20:01 arsaboo

I would have to create a new repo and change the name of it. I do not have the username and password for the existing Pypi account. I can try and e-mail the original author to is he would be wiling to add me to the accounts for ghithub and pypi.

kdschlosser avatar Jan 11 '19 22:01 kdschlosser

@AidasK I'm on leave next week so will have some time to work on the HA side

jnimmo avatar Jan 11 '19 23:01 jnimmo

I cannot publish it under the name "samsungctl" that is already taken. Ideally I would like to keep the name the same due to people being familiar with it.

as far as Home Assistant goes. you may want to have the person that wrote the samsungctl to HA bridge drop a line to me if possible. I can go over all of the new features that can be added people will be really happy with the additions and how much control this library does and is going to offer. Even with the new TV's the ability to get all of the applications.. with their associated icons.. as well as any application categories and content. DLNA support for all generations. Older TV's will see the most functionality until we can reverse engineer the websocket communications. the issue is that even Samsung does not have anything that offers full control.

If I had a newer TV I would simply develop an application for the TV that would expose the whole API on the TV to an external connection. I know they have the TV Emulator but it does not work on AMD processors so that doesn't help me much because I am all AMD

kdschlosser avatar Jan 12 '19 04:01 kdschlosser

I have tested this code with home assistant and it works. All we need is to have:

  • https://github.com/home-assistant/home-assistant/pull/19992
  • https://github.com/kdschlosser/samsungctl/issues/34

these pull requests to be merged.

AidasK avatar Jan 12 '19 09:01 AidasK

@AidasK #19992 is not sustainable in the long run. Ideally, it will be great if @kdschlosser can become the co-owner of the original samsungctl lib and update the pypi package. If that does not happen, may be we can publish this under a different name.

arsaboo avatar Jan 12 '19 17:01 arsaboo

@Ape Could you collaborate on this? Maybe kdschlosser code could be merged to your repo?

AidasK avatar Jan 12 '19 17:01 AidasK

I have tested the https://github.com/arsaboo/homeassistant-config/tree/master/custom_components/samsungctl version as a custom component but I had to hardcode the path to token.txt in remote_websocket.py to make it work with Hass.io also I had to change the wait to 40 to get rid of the "Unable to power off the TV" errormessage. It, works for poweroff, volume up/down and mute but poweron seems to fail, but I'm not a pro so I have probably missed something. My TV is a Samsung Q7 model, running Hass.os 2.5 and Hass.io 0.85.1 on a Tinkerboard S.

KentEkl avatar Jan 15 '19 07:01 KentEkl

I had no problems with token.txt file, but I have problems with power on, read it here https://github.com/kdschlosser/samsungctl/issues/30#issuecomment-453732567

AidasK avatar Jan 15 '19 08:01 AidasK

@AidasK Strange that the path worked for you, are you also running Hass.io ? Or is it Home Assistant.

KentEkl avatar Jan 15 '19 09:01 KentEkl

I am running hass.io 0.85.1, but I Have upgraded it from 0.79. Both versions worked, though I haven't exported it as a custom component. Could you share your custom component code?

AidasK avatar Jan 15 '19 09:01 AidasK

Of course, but you will laugh when you see it, I have never coded something in python but I've solved it via trial and error :-) Inserted this lines after line 36 in remote_websocket.py and after that it worked with token.txt but I assume I really don't know what i'm doing. path = "/config/custom_components/samsungctl" token_file = os.path.join(path, "token.txt")

KentEkl avatar Jan 15 '19 09:01 KentEkl

don't be getting too cozy with how the token information is saved. That is going to be changing. It is going to be added to the config data that you pass into the library. and thee data structure that is getting passed in is going to be changed. I am going to leave the passing of a dict for backwards compatibility. it is going to be what you see below

import samsungctl

config=samsungctl.Config(
    name='samsungctl',
    method='websocket',
    port=8002
)

# this is going to be if you want to load the config data from a file
# config = samsingctl.Config.load('\path\filename')

with samsungctl.Remote(config) as remote:
    remote.command('SOME COMMAND')

# this is optional and it is also how the token would get saved to a file
# config.save('path\filename')

# if you do not want to save the file and you want to have the token to enter it manually
# print(config.token)

kdschlosser avatar Jan 15 '19 10:01 kdschlosser

@kdschlosser Since, @Ape has not responded, I am wondering if you would be open to publishing your lib on PyPi.

arsaboo avatar Jan 19 '19 16:01 arsaboo

I am open to it. Need to come up with a name for the library, create a new GitHub repo. and then publish it to Pypi under this new name.

kdschlosser avatar Jan 19 '19 21:01 kdschlosser

pysamsung or samsungpy :)

arsaboo avatar Jan 19 '19 22:01 arsaboo

I also need to find someone that I can make a secondary on the GitHub account as well as the PyPi account. I am not going to do what happened with Ape and vanish leaving all the users of the library to fend for themselves. I want to make sure that development can still progress in the event I say "screw all this BS" and run off

kdschlosser avatar Jan 19 '19 23:01 kdschlosser

You should add someone from home-assistant community, I think they have most interest in this. You can add me third, I am not going to be active, but not going to wanish either. @kennedyshead ^

AidasK avatar Jan 20 '19 10:01 AidasK

Add me to, I will stay active :)

kennedyshead avatar Jan 20 '19 20:01 kennedyshead

One thing tho. before anything gets released to pypi it the library needs to be fully functioning along with full documentation that is built from docstrings using sphinx. When someone asks how to do something with the library i just want to point to a documentation page and not have to give instructions.

I am sure the folks at HomeAssistant would like to have a library that is as close to glitch free as we can get it. and also one that is fully documented on it's use

This is going to take a while to do by myself. So unless i have people with python knowledge specifically in the area of sphinx syntax it will be a while before it gets published.

kdschlosser avatar Jan 20 '19 23:01 kdschlosser

@kdschlosser I must disagree. I have created an opensource projects before (e.g. @flowjs) with thousands of stars and I have some experience.

My suggestion would be to make as many alpha releases as possible and documentation will be built on the go by the community. Don't worry about breaking things if necessary while it is in alpha, just notice everyone to pin a version they are using. There is nothing better than a clean and adaptable code.

And you don't need sphinx, markdown in readme.md document is perfectly fine for such a small project. You should stay flexible, and don't burden yourself with tasks such as fully documented, fully tested code, they only make things more troublesome to introduce new changes later.

AidasK avatar Jan 21 '19 14:01 AidasK

the reason I am concerning myself with it is because of thee number of other pieces of software that use this library.

Take a look at what the broken client-websocket library did. cause all kinds of headaches. all because someone had the same thought process as you. personally I cannot stand having to fix code that someone else wrote. It really drives me bonkers. Now that being said. what is on GitHub is one thing. what gets published to pypi is another. It is far easier to document/maintain documentation if it is done for each method/function/class then it is to simply toss it all into an RST file. sphinx is not all that hard. the time consuming piece is writing the docs. whether it be in an RST file or as docstings makes no never mind. sphinx does a really nice job of building HTML documentation that is easy to follow. for small projects yes i can understand. just the UPNP portion of this library is going to contain some 350 methods which each one is going to need documentation on use. Now I personally would not want to keep on scrolling down a 1000+ line RST file hunting to locate a set of directions.

to properly explain all of the properties and methods for the applications is going to require a lot of documentation. how to use the mouse feature. that is another. The use of the Config class i am going to implement that stores things like the token, UPNP XML file locations the TV's IP and the connection port. it also has convenience methods for saving and loading config files. for the UPNP end of things I added a nice convenience feature that breaks thee whole UPNP structure down and turns it into a dictionary. including things like max values min values. increments. default values choices and thee data type. it does this for every single UPNP function for the data that needs to be passed to thee function and for the returned values. This is to make it much easier for a project like Home Assistant to add controls for setting things like the Aspect Ratio (which needs to be passed a string and has specific choices). I did this because of differences in UPNP between models and years.

the fully tested thing.. That is almost impossible to do. But I sure as hell am not going to do a websocket-client sort of a thing to another project. This is not only going to piss off the users of the other software. but it also adds an excess amount of support requests for something that could have been avoided.

I am not sure what you mean by the community documenting it. I am thinking in respect to that it would be a user posting an issue asking how to do something. then another user comes along and asks the same question but does not search the issues. then either I have to re key the directions or I end up going hunting through the issues and then give them a link. either way it is more work then simply saying.. "Hey have you looked at the documentation??? It is located HERE". I maintain another project and there are about 30,000 downloads of the program a year, on our support forum there has almost 7000 members. Trust you me. I wish we had better documentation for it. that project has a system like you describe. and it is a bloody nightmare. And I have had to on many occasions patch code that was written by someone else. It is a real hassle to have to make a special release because of bad code in a dependent library. Don't get me wrong, I do fumble the football. But i do this in pre releases. or closed group testing. It is the reason why i set up the unit tests for this library. so my football fumbling is kept to a minimum.

I do also have an existence on this planet outside of GitHub, With a wife that I do have to pay attention to. as well as a house that I am in the process of remodeling (i should have built one from scratch would have been easier) and other coding projects I work on. like python_openzwave, I am the guy that made it so that Home Assistant can install python_openzwave on a Windows PC.

kdschlosser avatar Jan 21 '19 15:01 kdschlosser

all of the "thee" 's are supposed to be "the" I have a screwy keyboard. sorry about that.

kdschlosser avatar Jan 21 '19 15:01 kdschlosser

Macbook pro new keyboard?

I agree with both of you, but there is really no need for documentation other than usage in RRADME.md ... as far as testing goes I favor "test things that are crusial".

kennedyshead avatar Jan 21 '19 16:01 kennedyshead

That is what I am doing. and that is also the reason why I have not done any real work as of yet on the docs end of things. But when it comes to a release there should be full documentation.

I am not one for Crapintosh. Now when you can build the exact same machine (specs wise) for around 300 dollars USD and install OSX on it. and they charge 2000.00 USD for it. Another thing is that Apple has a nasty habit of deciding when your devices are obsolete. Example would be the latest version of OSX. according to them the older MacBooks are not capable of running it. I have one of said MacBooks here running the latest OSX that will tell you differently. Upgrade to an SSD and add more RAM, doesn't have a single issue running it.

People could give a crap about the actual device. what they like is the OS. So put the OS on non Mac branded hardware at a fraction of the cost = happy person.

kdschlosser avatar Jan 21 '19 17:01 kdschlosser

It's a keyboard I just wore out after 6 months. which is pretty typical. I keep on looking for one that will last longer. I finally found a mouse that is still working after 2 years. I was burning those up at a rate of 1-2 a year. and the charge on it lasts for 6-7 days on top of it!

kdschlosser avatar Jan 21 '19 17:01 kdschlosser

pysamsung or samsungpy :)

maybe similar to the LG library: pysamsungtv

andreas-bulling avatar Jan 22 '19 04:01 andreas-bulling

@AidasK I'm on leave next week so will have some time to work on the HA side

Any progress/updates here? Let me know if I shall test something for you - I'm very interested in getting samsungctl (or whatever it will be called eventually) to work in HA, particularly the encrypted websocket version.

andreas-bulling avatar Jan 22 '19 05:01 andreas-bulling