supersaiyanmode / PyWebOSTV

Python API for controlling LG TVs (Web OS)
MIT License
271 stars 50 forks source link

Client key not reusable #14

Closed 0dragosh closed 5 years ago

0dragosh commented 5 years ago

I'm attempting to register with the TV once, and then store the dict in a config.json. I can register and issue commands successfully on the first run. However, on the 2nd run of my script, it loads the store dict from the config file properly, but the tv does not execute any command whatsoever.

My code looks something like this:

store = {}

client = WebOSClient(tv_ip)

if os.path.isfile(config_file):
    try:
        with open(config_file, 'r') as configfile:
            store = json.load(configfile)
        print(store)
        client.connect()
    except Exception as e:
        print("Couldn't open file object with registration token not found!")
        raise e
else:
    print("Couldn't find registration token. Attempting registration...")
    client.connect()
    for status in client.register(store):
        if status == WebOSClient.PROMPTED:
            print("Please accept the connect on the TV!")
        elif status == WebOSClient.REGISTERED:
            print("Registration successful!")
    print(store)
    with open(config_file, 'w') as configfile:
            json.dump(store, configfile, indent=2)

Any command issued does nothing.

Am I doing something wrong?

Thanks!

supersaiyanmode commented 5 years ago

You should always call register(..) regardless of whether it is the first call or not. Your structure should look somewhat like:

if os.path.isfile(config_file):
    with open(config_file) as f:
        store = json.load(f)
else:
    store = {}

client.connect()
for status in client.register(store):
    if status == WebOSClient.PROMPTED:
        # **NOTE**: This will not happen if the store contains valid keys.
        print("Please accept the connect on the TV!")
    elif status == WebOSClient.REGISTERED:
        print("Registration successful!")

# json.dump(store) back to the config_file here.
0dragosh commented 5 years ago

@supersaiyanmode Thanks, that solved it!