8none1 / bravialib

Python 2 library for talking to some Sony Bravia TVs
GNU General Public License v3.0
15 stars 7 forks source link

Answer to your issue with the power being off to the TV #3

Open kdschlosser opened 6 years ago

kdschlosser commented 6 years ago

The network card stays powered on and the API will still allow for authentication if the TV is in a Powered Off (standby) state.

otherwise the command of

{
    "id": 50,
    "method": "getPowerStatus",
    "params": [],
    "version": "1.0"
}

would not be able to respond with

{
    "id": 50,
    "result": [{"status": "standby"}]
}

If you want to power on the TV You can give this command a try and see if the TV does not kick back an error (see the end of the post for error messages from the TV) It all depends on the API level of the TV

{
    "id": 55,
    "method": "setPowerStatus",
    "params": [{"status": true}],
    "version": "1.0"
}

if that fails then you will want to send this command

{
    "id": 50,
    "method": "getWolMode",
    "params": [],
    "version": "1.0"
}

and if you do not get this for an answer

{
    "id": 50,
    "result": [{"enabled": true}]
}

then send this command

{
    "id": 55,
    "method": "setWolMode",
    "params": [{"enabled": true}],
    "version": "1.0"
}

then You will be able to run this command

{
    "id": 55,
    "method": "getSystemSupportedFunction",
    "params": [],
    "version": "1.0"
}

which will respond with

{
    "id": 55,
    "result": [[{
        "value": "00:00:00:00:00:00:00:E0",
        "option": "WOL"
    }]]
}

be sure to iterate through the result and look for the "WOL". this will give you the MAC address to run this next piece of code.

import struct
import socket

def send_wol(mac):
    address_byte = tuple(
        int(b, 16) for b in mac.split(':')
    )
    hw_address = struct.pack('BBBBBB', *address_byte)
    msg = b'\xff' * 6 + hw_address * 16

    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.setsockopt(
        socket.SOL_SOCKET,
        socket.SO_BROADCAST,
        1
    )

    sock.sendto(msg, ('<broadcast>', 9))
    sock.close()

This will allow you to turn the TV on. I set it up so that if the TV is being newly registered to set the WOL mode to enabled right out of the gate. and when a connection is made then to grab that mac address right away this way the power on and off will function properly. I do know that when the TV is in a standby state there are parts of the API that will do nothing or kick back an error. below is how to handle those errors.

The attached file contains all of the errors and related messages that the TV can have. you will need to change the .txt to .py exception.txt

sample code for using the exceptions. The code only shows how to handle an error that has been returned by the TV

import exception

err = response.get('error', None)
if err:
    err_id = err[0]
    if err_id in exception.EXCEPTIONS:
        raise exception.EXCEPTIONS[err_id](err)
    if err_id < 32767:
        raise exception.ScalarWebAPISystemError(err)
    else:
        raise exception.SonyAPIError(err)

Hope that helps with your power issue