dcnielsen90 / python-bravia-tv

MIT License
13 stars 6 forks source link

Partially working on KDL-46W704A #3

Open speed4618 opened 4 years ago

speed4618 commented 4 years ago

Hi!

Thank you so much for your work. I'm running on Home Assistant 0.108.2 and I checked it is running bravia-tv==1.0.1 .

TV model: KDL-46W704A Software Version: PKG4.600GAA

The issues I am facing is listed as the following

  1. I could not turn on the TV via the using the power on media button in Home Assistant

    • I enabled the option on the TV to allow "Remote Start"
    • I've also checked that my TV is pingable through the network while being off
    • I could remotely turn on the TV by using the "Video & TV SideView Remote" on play store.
  2. I could not get any App sources to trigger (Netflix, Youtube, Internet Browser, "a" Clock, etc).

    • Whenever I click on it, the TV does nothing and home-assistant reverts to the previously selected source. However, wired sources such as HDMI, etc work without an issue.

image

Other features such as volume control, switching wired sources and turning off the TV works well. Thank you so much for your work. Truly amazing and thank you for your time!

dcnielsen90 commented 4 years ago

I actually just shifted back to working on this library now that there is a PR in for config flow under review in HA's git. I've got a working solution for the apps; I'm looking into making the power issues and making periodic updates more reliable now. I apologise for the brevity, I'm on mobile.

speed4618 commented 4 years ago

That's great! Thanks again for your amazing project! Looking forward to the next release.

dcnielsen90 commented 4 years ago

I've about finished up, I'm testing now. I've pushed the branch to the public repo for testing if you want to check it out. It's here. The applications are fixed; and I think I might have fixed the power issue (that one is hard to say -- I'm trying to test that aspect now)

speed4618 commented 4 years ago

Amazing work. I've tested your latest repo. For anyone who wants to try, you can run the following in your home assistant docker / raspi pip install --upgrade git+https://github.com/dcnielsen90/python-bravia-tv@fix_apps

Great news! The app sources are now working. I can successfully switch between Youtube, Netflix and other apps. Small bug by the way: image

The Power On button still does not work. I have ensured that the bravia.conf has the correct MAC address. I have also tried your library in python, after setting up everything and tried a couple of commands which works fine, I proceed to try braviarc.turn_off() and braviarc.turn_on() which the TV could not turn on. If I use an android WakeOnLan app, there is no problem in turning the TV on.

I am not sure if this is an issue, but I noticed while the TV is off and I restart HASS, the card will have an error in setting up. Sometimes even when the TV in on, restarting HASS will also have this error.

Logger: homeassistant.components.media_player
Source: components/braviatv/media_player.py:80
Integration: Media player (documentation, issues)
First occurred: 8:56:20 PM (1 occurrences)
Last logged: 8:56:20 PM

Error while setting up braviatv platform for media_player
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 178, in _async_setup_platform
    await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT)
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 442, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/braviatv/media_player.py", line 80, in setup_platform
    unique_id = braviarc.get_system_info()["cid"].lower()
KeyError: 'cid' 

Thank you again so much for your time in developing this project! I am more than happy to continue to test your latest code when available. 😁

dcnielsen90 commented 4 years ago

Alright, I did a bit more troubleshooting last night and found that reducing the state timeout greatly improves performance (thereby removing warning logs saying it takes >10 seconds to update).

This:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 178, in _async_setup_platform
    await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT)
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 442, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/braviatv/media_player.py", line 80, in setup_platform
    unique_id = braviarc.get_system_info()["cid"].lower()
KeyError: 'cid' 

is an issue in home assistant. It should be fixed by this Add config flow for braviatv integration #33774

This: image is weird, I'm not seeing that.

Now on to the power issue. Does it work when you power on HA when the TV is on? I believe this is causing a condition in which the HA Bravia instance can't get the proper cookies to bring your TV out of standby. I plan on during a PR in HA for that after this PR. If this is not the case is your TV connected via WiFi, ethernet or both? I know you said it had the right MAC address; however, I'd be interested in seeing if the Bravia instance has the appropriate NIC's MAC. If none of these are the case -- I'd have to run more verbose debug code to determine what is going on.

speed4618 commented 4 years ago

Does it work when you power on HA when the TV is on?

It does not work regardless of TV power or HA state. The issue is exactly the same as described by #4. In my case, my TV was connected via WiFi. Thanks.

dcnielsen90 commented 4 years ago

I have also tried your library in python, after setting up everything and tried a couple of commands which works fine, I proceed to try braviarc.turn_off() and braviarc.turn_on() which the TV could not turn on. If I use an android WakeOnLan app, there is no problem in turning the TV on.

Can you try not specifying your mac address when you do this? I didn't update the documentation; however I made it so that manually putting in the mac address is completely unnecessary. For example:

from bravia_tv import BraviaRC
from time import sleep

ip_address = '192.178.X.X'
braviarc = BraviaRC(ip_address)
pin = '1234'
braviarc.connect(pin, 'my_device_id', 'my device name')
if braviarc.is_connected():
    braviarc.turn_off()
    sleep(3)
    braviarc.turn_on()
speed4618 commented 4 years ago

I just tried based your example. It could turn off the TV, but the TV doesn't turn on after 3 seconds. I tried this a few times but was not successful. Let me know what else can I try. Thanks.

Btw, I'm using the python-bravia-tv@fix_apps branch with your latest commits up till a41e565

dcnielsen90 commented 4 years ago

That's a tough one... I'll definitely need more debug statements to see what's going on. It could be as simple as blasting the WOL packet a couple more times. Possibly need to query the TV for an "on" iirc command. If your up for it; I'll make another branch for you to run and see if we can crack this. It really stinks this isn't consistent across all models.

speed4618 commented 4 years ago

Yeah... I'm up to it. I would be more than happy to help. Let me know when the new branch is up for me to test with the debug statements. All the best!

Knapoc commented 4 years ago

sorry for going a little bit off-topic: @dcnielsen90 are there any plans to further adopt RESTapi and gradually replace IRCC?

I made the observation, that IRCC is working kinda unreliable on TVs running Android Oreo. This is not an issue of the api provided in this repo, but with the TV itself since this issue could be replicated when sending http-requests directly.

My issue is the following: IRCC commands are not always registered on the first call and have to be send twice.

dcnielsen90 commented 4 years ago

@Knapoc interesting. I definitely prefer the RESTapi over IIRC. There is more programmatic feedback from that api which helps determine if the command went through properly.

@speed4618 I figure start off easy and see what happens. I want to see what IIRC commands your TV supports -- also I made it send the WOL packet 10 times in one second (overkill but if it works we'll see what we can do to water it down). Try reinstalling the branch again and run:

import logging
from bravia_tv import BraviaRC
from time import sleep

_LOGGER = logging.getLogger(__name__)

ip_address = '192.168.X.X'
braviarc = BraviaRC(ip_address)
pin = '1111'
braviarc.connect(pin, 'my_device_id', 'my device name')
if braviarc.is_connected():
    for x in braviarc.getRemoteControllerInfo():
        for y in x:
            print(y)
    print(braviarc.turn_off())
    sleep(3)
    print(braviarc.turn_on())
speed4618 commented 4 years ago

I think that's a good idea. I have reinstalled the branch and ran the code. The TV did not turn on even after you made it send the WOL packet 10 times in one second.

The following is the output:

bundled
type
{'name': 'PowerOff', 'value': 'AAAAAQAAAAEAAAAvAw=='}
{'name': 'Input', 'value': 'AAAAAQAAAAEAAAAlAw=='}
{'name': 'GGuide', 'value': 'AAAAAQAAAAEAAAAOAw=='}
{'name': 'EPG', 'value': 'AAAAAgAAAKQAAABbAw=='}
{'name': 'Favorites', 'value': 'AAAAAgAAAHcAAAB2Aw=='}
{'name': 'Display', 'value': 'AAAAAQAAAAEAAAA6Aw=='}
{'name': 'Home', 'value': 'AAAAAQAAAAEAAABgAw=='}
{'name': 'Options', 'value': 'AAAAAgAAAJcAAAA2Aw=='}
{'name': 'Return', 'value': 'AAAAAgAAAJcAAAAjAw=='}
{'name': 'Up', 'value': 'AAAAAQAAAAEAAAB0Aw=='}
{'name': 'Down', 'value': 'AAAAAQAAAAEAAAB1Aw=='}
{'name': 'Right', 'value': 'AAAAAQAAAAEAAAAzAw=='}
{'name': 'Left', 'value': 'AAAAAQAAAAEAAAA0Aw=='}
{'name': 'Confirm', 'value': 'AAAAAQAAAAEAAABlAw=='}
{'name': 'Red', 'value': 'AAAAAgAAAJcAAAAlAw=='}
{'name': 'Green', 'value': 'AAAAAgAAAJcAAAAmAw=='}
{'name': 'Yellow', 'value': 'AAAAAgAAAJcAAAAnAw=='}
{'name': 'Blue', 'value': 'AAAAAgAAAJcAAAAkAw=='}
{'name': 'Num1', 'value': 'AAAAAQAAAAEAAAAAAw=='}
{'name': 'Num2', 'value': 'AAAAAQAAAAEAAAABAw=='}
{'name': 'Num3', 'value': 'AAAAAQAAAAEAAAACAw=='}
{'name': 'Num4', 'value': 'AAAAAQAAAAEAAAADAw=='}
{'name': 'Num5', 'value': 'AAAAAQAAAAEAAAAEAw=='}
{'name': 'Num6', 'value': 'AAAAAQAAAAEAAAAFAw=='}
{'name': 'Num7', 'value': 'AAAAAQAAAAEAAAAGAw=='}
{'name': 'Num8', 'value': 'AAAAAQAAAAEAAAAHAw=='}
{'name': 'Num9', 'value': 'AAAAAQAAAAEAAAAIAw=='}
{'name': 'Num0', 'value': 'AAAAAQAAAAEAAAAJAw=='}
{'name': 'Num11', 'value': 'AAAAAQAAAAEAAAAKAw=='}
{'name': 'Num12', 'value': 'AAAAAQAAAAEAAAALAw=='}
{'name': 'VolumeUp', 'value': 'AAAAAQAAAAEAAAASAw=='}
{'name': 'VolumeDown', 'value': 'AAAAAQAAAAEAAAATAw=='}
{'name': 'Mute', 'value': 'AAAAAQAAAAEAAAAUAw=='}
{'name': 'ChannelUp', 'value': 'AAAAAQAAAAEAAAAQAw=='}
{'name': 'ChannelDown', 'value': 'AAAAAQAAAAEAAAARAw=='}
{'name': 'SubTitle', 'value': 'AAAAAgAAAJcAAAAoAw=='}
{'name': 'ClosedCaption', 'value': 'AAAAAgAAAKQAAAAQAw=='}
{'name': 'Enter', 'value': 'AAAAAQAAAAEAAAALAw=='}
{'name': 'DOT', 'value': 'AAAAAgAAAJcAAAAdAw=='}
{'name': 'Analog', 'value': 'AAAAAgAAAHcAAAANAw=='}
{'name': 'Teletext', 'value': 'AAAAAQAAAAEAAAA/Aw=='}
{'name': 'Exit', 'value': 'AAAAAQAAAAEAAABjAw=='}
{'name': 'Analog2', 'value': 'AAAAAQAAAAEAAAA4Aw=='}
{'name': '*AD', 'value': 'AAAAAgAAABoAAAA7Aw=='}
{'name': 'Digital', 'value': 'AAAAAgAAAJcAAAAyAw=='}
{'name': 'Analog?', 'value': 'AAAAAgAAAJcAAAAuAw=='}
{'name': 'BS', 'value': 'AAAAAgAAAJcAAAAsAw=='}
{'name': 'CS', 'value': 'AAAAAgAAAJcAAAArAw=='}
{'name': 'BSCS', 'value': 'AAAAAgAAAJcAAAAQAw=='}
{'name': 'Ddata', 'value': 'AAAAAgAAAJcAAAAVAw=='}
{'name': 'PicOff', 'value': 'AAAAAQAAAAEAAAA+Aw=='}
{'name': 'Tv_Radio', 'value': 'AAAAAgAAABoAAABXAw=='}
{'name': 'Theater', 'value': 'AAAAAgAAAHcAAABgAw=='}
{'name': 'SEN', 'value': 'AAAAAgAAABoAAAB9Aw=='}
{'name': 'InternetWidgets', 'value': 'AAAAAgAAABoAAAB6Aw=='}
{'name': 'InternetVideo', 'value': 'AAAAAgAAABoAAAB5Aw=='}
{'name': 'Netflix', 'value': 'AAAAAgAAABoAAAB8Aw=='}
{'name': 'SceneSelect', 'value': 'AAAAAgAAABoAAAB4Aw=='}
{'name': 'Mode3D', 'value': 'AAAAAgAAAHcAAABNAw=='}
{'name': 'iManual', 'value': 'AAAAAgAAABoAAAB7Aw=='}
{'name': 'Audio', 'value': 'AAAAAQAAAAEAAAAXAw=='}
{'name': 'Wide', 'value': 'AAAAAgAAAKQAAAA9Aw=='}
{'name': 'Jump', 'value': 'AAAAAQAAAAEAAAA7Aw=='}
{'name': 'PAP', 'value': 'AAAAAgAAAKQAAAB3Aw=='}
{'name': 'MyEPG', 'value': 'AAAAAgAAAHcAAABrAw=='}
{'name': 'ProgramDescription', 'value': 'AAAAAgAAAJcAAAAWAw=='}
{'name': 'WriteChapter', 'value': 'AAAAAgAAAHcAAABsAw=='}
{'name': 'TrackID', 'value': 'AAAAAgAAABoAAAB+Aw=='}
{'name': 'TenKey', 'value': 'AAAAAgAAAJcAAAAMAw=='}
{'name': 'AppliCast', 'value': 'AAAAAgAAABoAAABvAw=='}
{'name': 'acTVila', 'value': 'AAAAAgAAABoAAAByAw=='}
{'name': 'DeleteVideo', 'value': 'AAAAAgAAAHcAAAAfAw=='}
{'name': 'PhotoFrame', 'value': 'AAAAAgAAABoAAABVAw=='}
{'name': 'TvPause', 'value': 'AAAAAgAAABoAAABnAw=='}
{'name': 'KeyPad', 'value': 'AAAAAgAAABoAAAB1Aw=='}
{'name': 'Media', 'value': 'AAAAAgAAAJcAAAA4Aw=='}
{'name': 'SyncMenu', 'value': 'AAAAAgAAABoAAABYAw=='}
{'name': 'Forward', 'value': 'AAAAAgAAAJcAAAAcAw=='}
{'name': 'Play', 'value': 'AAAAAgAAAJcAAAAaAw=='}
{'name': 'Rewind', 'value': 'AAAAAgAAAJcAAAAbAw=='}
{'name': 'Prev', 'value': 'AAAAAgAAAJcAAAA8Aw=='}
{'name': 'Stop', 'value': 'AAAAAgAAAJcAAAAYAw=='}
{'name': 'Next', 'value': 'AAAAAgAAAJcAAAA9Aw=='}
{'name': 'Rec', 'value': 'AAAAAgAAAJcAAAAgAw=='}
{'name': 'Pause', 'value': 'AAAAAgAAAJcAAAAZAw=='}
{'name': 'Eject', 'value': 'AAAAAgAAAJcAAABIAw=='}
{'name': 'FlashPlus', 'value': 'AAAAAgAAAJcAAAB4Aw=='}
{'name': 'FlashMinus', 'value': 'AAAAAgAAAJcAAAB5Aw=='}
{'name': 'TopMenu', 'value': 'AAAAAgAAABoAAABgAw=='}
{'name': 'PopUpMenu', 'value': 'AAAAAgAAABoAAABhAw=='}
{'name': 'RakurakuStart', 'value': 'AAAAAgAAAHcAAABqAw=='}
{'name': 'OneTouchTimeRec', 'value': 'AAAAAgAAABoAAABkAw=='}
{'name': 'OneTouchView', 'value': 'AAAAAgAAABoAAABlAw=='}
{'name': 'OneTouchRec', 'value': 'AAAAAgAAABoAAABiAw=='}
{'name': 'OneTouchStop', 'value': 'AAAAAgAAABoAAABjAw=='}
None
None
dcnielsen90 commented 4 years ago

If I use an android WakeOnLan app, there is no problem in turning the TV on.

Can you send me a link for the exact app your using that works? I want to sniff some packets.

ThomasPrior commented 4 years ago

I used the same app in troubleshooting before setting up WoL via HomeAssistant.

You can find the app here: https://play.google.com/store/apps/details?id=co.uk.mrwebb.wakeonlan

speed4618 commented 4 years ago

If I use an android WakeOnLan app, there is no problem in turning the TV on.

Can you send me a link for the exact app your using that works? I want to sniff some packets.

I'm using this app: https://play.google.com/store/apps/details?id=ua.com.streamsoft.pingtools

dcnielsen90 commented 4 years ago

Thanks @ThomasPrior @speed4618. I just ran a packet capture this morning. pingtools by default was sending 10 packets to port 7 and wakeonlan by default sent 3 packets to port 9. Both tools had 0 wait time in between packets. I figure compromise and send 5 packets to each port with 0 sleep time and hope that this makes it work for everyone.
I committed this to branch: fix_turn_on

so:

pip install --upgrade git+https://github.com/dcnielsen90/python-bravia-tv@fix_turn_on

can be used for testing this fix.

You can use the same script as before to test if this works:

from bravia_tv import BraviaRC
from time import sleep

ip_address = '192.168.X.X'
braviarc = BraviaRC(ip_address)
pin = '1234'
braviarc.connect(pin, 'my_device_id', 'my device name')
if braviarc.is_connected():
    braviarc.turn_off()
    sleep(10)
    braviarc.turn_on()

If it doesn't work, trying uping the sleep time a bit.

dcnielsen90 commented 4 years ago

I realized that editing my previous comment with an update was probably a poor choice. Anyhow just in case you didn't see, I pushed an updated branch.

speed4618 commented 4 years ago

Sorry I did not notice the edited post as it wasn't sent to my email. Unfortunately it still did not work. It managed to turn the TV off, but it did not turn on after 10 seconds. Manually executing braviarc.turn_on() also failed.

bash-5.0# pip install --upgrade git+https://github.com/dcnielsen90/python-bravia-tv@fix_turn_on
Collecting git+https://github.com/dcnielsen90/python-bravia-tv@fix_turn_on
  Cloning https://github.com/dcnielsen90/python-bravia-tv (to revision fix_turn_on) to /tmp/pip-req-build-gmafc1wm
  Running command git clone -q https://github.com/dcnielsen90/python-bravia-tv /tmp/pip-req-build-gmafc1wm
  Running command git checkout -b fix_turn_on --track origin/fix_turn_on
  Branch 'fix_turn_on' set up to track remote branch 'fix_turn_on' from 'origin'.
  Switched to a new branch 'fix_turn_on'
Building wheels for collected packages: bravia-tv
  Building wheel for bravia-tv (setup.py) ... done
  Created wheel for bravia-tv: filename=bravia_tv-1.0.2-py3-none-any.whl size=6729 sha256=0121a76d879e6fc6a9a198bb480465b613e41403ac9d44bc1408fe04755de243
  Stored in directory: /tmp/pip-ephem-wheel-cache-eq4izyzn/wheels/ce/e8/5a/436102c6ef228af745f3c24e00487e90c05f82c917909da683
Successfully built bravia-tv
Installing collected packages: bravia-tv
  Attempting uninstall: bravia-tv
    Found existing installation: bravia-tv 1.0.1
    Uninstalling bravia-tv-1.0.1:
      Successfully uninstalled bravia-tv-1.0.1
Successfully installed bravia-tv-1.0.2
WARNING: You are using pip version 20.0.2; however, version 20.1 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
bash-5.0# python
Python 3.7.7 (default, Mar 24 2020, 22:28:40)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from bravia_tv import BraviaRC
>>> from time import sleep
>>>
>>> ip_address = '192.168.0.142'
>>> braviarc = BraviaRC(ip_address)
>>> pin = '9866'
>>> braviarc.connect(pin, 'my_device_id', 'my device name')
True
>>> if braviarc.is_connected():
...     braviarc.turn_off()
...     sleep(10)
...     braviarc.turn_on()
...
>>> braviarc.turn_on()
>>> braviarc.turn_on()
>>> braviarc.turn_on()
>>>
dcnielsen90 commented 4 years ago

@speed4618 sorry for dropping off for a while. I honestly ran out of ideas. Someone else brought up another issue that I think the fix might actually address this problem as well. I'm testing it now pip install --upgrade git+https://github.com/dcnielsen90/python-bravia-tv@fix_cookies_again

speed4618 commented 4 years ago

@dcnielsen90 it's okay. Hope you doing well over there. I tried the fix_cookies_again branch, but it is still not working.

There are some good news though, I just managed to get it working and figured out what was the problem. I will try my best to describe it here.

Under the def _wakeonlan(self):, I have noticed that you used <broadcast> to determine the broadcast IP

socket_instance.sendto(msg, ('<broadcast>', 9))

After changing it to my network's broadcast IP socket_instance.sendto(msg, ('192.168.0.255', 9)), the braviarc.turn_on() works without an issue.

Then I realized that the <broadcast> would not work because my Home Assistant is running in a docker instance which has a different broadcast IP. My apologies not mentioning that my HA is running on docker.

dcnielsen90 commented 4 years ago

Awesome! I'm glad you found a solution that works. I run in docker as well and don't have that issue, must be a difference in configuration. Definitely though we should put a dynamic variation of this to make the lib more robust. (And so you don't have to manually update this every upgrade you do)

speed4618 commented 4 years ago

Thanks. Maybe it is because I'm running docker on windows? Yes, maybe it is possible to refer to the IP address and subnet to determine the broadcast address.

I have another issue now, I can't seem to figure out how to link my locally installed bravia_tv library (with the broadcast IP changes) with the HA Integration. Still couldn't get the HA Integration to turn on the TV.

It seems that each time I restart my docker instance, the bravia_tv library will revert to the original bravia-tv==1.0.6 and braviarc.turn_on() doesn't work until I manually install the local library again.

Any tips? Thank you!

dcnielsen90 commented 4 years ago

Maybe it is because I'm running docker on windows?

Most likely -- I run mine on an Ubuntu server.

It seems that each time I restart my docker instance, the bravia_tv library will revert to the original bravia-tv==1.0.6

yeah... I'm not sure if your using docker-compose of just running docker commands but you could set it up the compose or command to have the pip command immediately after start up. You'll still need to restart home-assistant its self though (via the gui or add that to the start script) the only other option is to just don't shutdown the container. Only restart via the gui, or have a shortcut button to do it. I personally have it set up where I can just ask Alexa to restart HA which is extremely helpful when I'm testing.

I'm going to aim to get this in next release so you shouldn't have to hack for much longer.

dcnielsen90 commented 4 years ago

Alright I think it's good now on fix_cookies_again. I will need to make an update to Home Assistant's workflow as well as the docs, but this should give the ability to send WOL packets to TV's on different subnets than the calling host. You just have to specify a subnet mask for the TV's subnet. I'll likely make it an optional input as a lot of people aren't going to need it.