Sony Bravia TV component with Pre-Shared Key

Using Home Assistant for some weeks now and it works great :sunglasses:

I have a Sony Bravia TV and I have used the braviatv platform, but that was not working very well for me.
After some days the state card wasn’t updating anymore and I got a lot of errors in the log. Only a restart of HASS would fix that problem.
I think this was caused by the PIN/cookie method which isn’t working fine (at least not in my setup).

That’s why I have made a custom component which is using a Pre-Shared Key to connect to the TV instead of the PIN/cookie method.
I am using this component for some weeks now without any issues, so now sharing it here and hopefully other Sony Bravia owners are pleased to test and use it as well.

Please let me know if you have found any issues or have suggestions to improve the component :smiley:.

The changes I have made to the braviatv component are:

  • use of Pre-Shared Key instead of connecting with a pin and the use of a cookie
  • option for amplifier: don’t show volume slider when amp is attached as the volume slider only works for TV speakers
  • option for Android: turn TV on with other method as WakeOn LAN doesn’t work on Android
  • possibility to filter source list to avoid a long list with unused channels and radio stations

The following changes are only available if the built-in TV tuner is used

  • show program info on second line of state card, this will also show the start and end time of the program
  • pause/play TV when TV is playing, otherwise normal pause/play function is used
  • channel up/down with next and previous buttons when TV is playing, otherwise normal previous/next function is used

Screenshot
screenprint1

See the bottom of this post for some more screenshots.

Installation instructions Home Assistant
Download media_player.py and __init.py from here and place that file in the folder custom_components/braviatv_psk/ in your config folder (for HA 0.88 and newer, for older versions of HA rename the file to braviatv_psk.py and place it in the folder custom_components/media_player/).

Add the following to your configuration.yaml file

media_player:
  - platform: braviatv_psk
    name: MyBraviaTV
    host: 192.168.1.191
    psk: sony
    mac: AA:BB:CC:DD:EE:FF
    amp: True
    android: True
    sourcefilter:
      - ' HD'
      - HDMI

Configuration variables:

  • name (Optional): The name to use on the frontend, defaults to Sony Bravia TV
  • host (Required): The IP of the Sony Bravia TV, eg. 192.168.1.191
  • psk (Required): The Pre-Shared Key of the Sony Bravia TV, eg. sony (see below for instructions how to configure this on the TV)
  • mac (Required): The MAC address of the Sony Bravia TV (see below for instructions how to get this from the TV)
  • amp (Optional): Boolean, defaults to False. Set this to True if you have an amplifier attached to the TV and not use the internal TV speakers. Then the volume slider will not be shown as this doesn’t work for the amplifier. Mute and volume up and down buttons are there and working with an amplifier.
  • android (Optional): Boolean, defaults to False. Set this to True when you have an Android TV as these TV’s don’t respond to WakeOn LAN commands, so another method of turning on the TV can be used.
  • sourcefilter (Optional): List of text that is used to filter the source list, eg. ’ HD’ (with quotes) will only show TV channels in the source list which contain ‘HD’, eg. ‘NPO 3 HD’ (in my config this will only show HD channels)

Installation instructions TV

  1. Enable remote start on your TV: [Settings] => [Network] => [Home Network Setup] => [Remote Start] => [On]
  2. Enable pre-shared key on your TV: [Settings] => [Network] => [Home Network Setup] => [IP Control] => [Authentication] => [Normal and Pre-Shared Key]
  3. Set pre-shared key on your TV: [Settings] => [Network] => [Home Network Setup] => [IP Control] => [Pre-Shared Key] => sony
  4. Give your TV a static IP address, or make a DHCP reservation for a specific IP address in your router
  5. Determine the MAC address of your TV: [Settings] => [Network] => [Network Setup] => [View Network Status]

To do
I would like to add some more functionalities, but need some help from the community to get this working.
Any ideas how to add the following points?

  1. The Play/Pause button is not shown on the card (between the previous and next buttons).
  2. I want to show the progress bar (the orange bar in eg. the Sonos component) which shows the progress of the current TV program. I have tried to add this using this and this, but the bar is not shown. Maybe that has to do with point 1?
  3. When starting the TV with the on/off button, the TV will only respond around a minute after starting. That is also the time the card will show that the TV is on. I would like the card to instantly show the TV is on and with the text ‘TV starting’.

More screenshots
Some automations added with a TV channel list and a play/pause TV button

Available buttons

19 Likes

This works perfectly, thank you so much! I have only had time to test for a few minutes this morning but all channels appeared and I could turn the TV off remotely. I also saw the TV’s status.

1 Like

Does this also work with HASSIO?

Yes, I am running this component on hass.io :grinning:

2 Likes

Awesome work!! Looking forward to getting this installed

1 Like

will this work with any Bravia tv that can use the sony ‘Video & TV SideView’ app?
or is it still only going to work with TVs from a certain generation?

Almost all Sony Bravia TV from 2013 and newer are supported.

brilliant work!

Ive been hunting for a means to filter the source list as i only need/ want the HDMI inputs!

One question…
the volume…in your OP you say to set the option amp to True if you dont use the TVs built in speaker.

I dont, and I have an LG soundbar connected via optical cable, the mute and volume up/down buttons work but change the volume on the TV itself?

Is there a way to have these control the soundbar at all?
(I expect not?)

1 Like

Thanks, good to see you like this component :sunglasses:

I have an amplifier which is connected via HDMI. When I use the volume up/down buttons it changes the volume of the amplifier.
The volume buttons in Home Assistant have the same function as the volume buttons on the remote control.
Maybe there is something you can change in the TV settings regarding the audio?

I can’t change anything for this in the script. There are 2 options to control the sound, one only controls the TV speaker (the amp option is only used to hide the volume slider that controls that TV speaker) and the other option is the volume up/down which has the same function as the remote buttons.

understood,
its only a soundbar so not so much a sound system and its hooked direct to TV via optical cable

I got this error.
and none of my media player component working after adding custom component.
emphasized text
[homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/usr/lib/python3.5/asyncio/tasks.py”, line 241, in _step
result = coro.throw(exc)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/setup.py”, line 60, in async_setup_component
return (yield from task)
File “/usr/lib/python3.5/asyncio/futures.py”, line 380, in iter
yield self # This tells Task to wait for completion.
File “/usr/lib/python3.5/asyncio/tasks.py”, line 304, in _wakeup
future.result()
File “/usr/lib/python3.5/asyncio/futures.py”, line 293, in result
raise self._exception
File “/usr/lib/python3.5/asyncio/tasks.py”, line 239, in _step
result = coro.send(None)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/setup.py”, line 159, in _async_setup_component
conf_util.async_process_component_config(hass, config, domain)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/config.py”, line 619, in async_process_component_config
platform = get_platform(domain, p_name)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/loader.py”, line 104, in get_platform
return get_component(PLATFORM_FORMAT.format(domain, platform))
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/loader.py”, line 142, in get_component
module = importlib.import_module(path)
File “/usr/lib/python3.5/importlib/init.py”, line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “”, line 986, in _gcd_import
File “”, line 969, in _find_and_load
File “”, line 958, in _find_and_load_unlocked
File “”, line 673, in _load_unlocked
File “”, line 669, in exec_module
File “”, line 775, in get_code
File “”, line 735, in source_to_code
File “”, line 222, in _call_with_frames_removed
File “/home/homeassistant/.homeassistant/custom_components/media_player/braviatv_psk.py”, line 7

^
SyntaxError: invalid syntax
2017-11-26 13:51:26 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/usr/lib/python3.5/asyncio/tasks.py”, line 239, in _step
result = coro.send(None)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/discovery.py”, line 122, in new_service_found
hass, component, platform, info, config)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/discovery.py”, line 164, in async_load_platform
hass, component, hass_config)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/setup.py”, line 49, in async_setup_component
return (yield from setup_tasks[domain])
File “/usr/lib/python3.5/asyncio/futures.py”, line 382, in iter
return self.result() # May raise too.
File “/usr/lib/python3.5/asyncio/futures.py”, line 293, in result
raise self._exception
File “/usr/lib/python3.5/asyncio/tasks.py”, line 241, in _step
result = coro.throw(exc)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/setup.py”, line 60, in async_setup_component
return (yield from task)
File “/usr/lib/python3.5/asyncio/futures.py”, line 380, in iter
yield self # This tells Task to wait for completion.
File “/usr/lib/python3.5/asyncio/tasks.py”, line 304, in _wakeup
future.result()
File “/usr/lib/python3.5/asyncio/futures.py”, line 293, in result
raise self._exception
File “/usr/lib/python3.5/asyncio/tasks.py”, line 239, in _step
result = coro.send(None)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/setup.py”, line 159, in _async_setup_component
conf_util.async_process_component_config(hass, config, domain)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/config.py”, line 619, in async_process_component_config
platform = get_platform(domain, p_name)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/loader.py”, line 104, in get_platform
return get_component(PLATFORM_FORMAT.format(domain, platform))
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/loader.py”, line 142, in get_component
module = importlib.import_module(path)
File “/usr/lib/python3.5/importlib/init.py”, line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “”, line 986, in _gcd_import
File “”, line 969, in _find_and_load
File “”, line 958, in _find_and_load_unlocked
File “”, line 673, in _load_unlocked
File “”, line 669, in exec_module
File “”, line 775, in get_code
File “”, line 735, in source_to_code
File “”, line 222, in _call_with_frames_removed
File “/home/homeassistant/.homeassistant/custom_components/media_player/braviatv_psk.py”, line 7

^
SyntaxError: invalid syntax
2017-11-26 13:51:27 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/usr/lib/python3.5/asyncio/tasks.py”, line 239, in _step
result = coro.send(None)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/discovery.py”, line 122, in new_service_found
hass, component, platform, info, config)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/discovery.py”, line 164, in async_load_platform
hass, component, hass_config)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/setup.py”, line 49, in async_setup_component
return (yield from setup_tasks[domain])
File “/usr/lib/python3.5/asyncio/futures.py”, line 382, in iter
return self.result() # May raise too.
File “/usr/lib/python3.5/asyncio/futures.py”, line 293, in result
raise self._exception
File “/usr/lib/python3.5/asyncio/tasks.py”, line 239, in _step
result = coro.send(None)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/discovery.py”, line 122, in new_service_found
hass, component, platform, info, config)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/discovery.py”, line 164, in async_load_platform
hass, component, hass_config)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/setup.py”, line 49, in async_setup_component
return (yield from setup_tasks[domain])
File “/usr/lib/python3.5/asyncio/futures.py”, line 382, in iter
return self.result() # May raise too.
File “/usr/lib/python3.5/asyncio/futures.py”, line 293, in result
raise self._exception
File “/usr/lib/python3.5/asyncio/tasks.py”, line 241, in _step
result = coro.throw(exc)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/setup.py”, line 60, in async_setup_component
return (yield from task)
File “/usr/lib/python3.5/asyncio/futures.py”, line 380, in iter
yield self # This tells Task to wait for completion.
File “/usr/lib/python3.5/asyncio/tasks.py”, line 304, in _wakeup
future.result()
File “/usr/lib/python3.5/asyncio/futures.py”, line 293, in result
raise self._exception
File “/usr/lib/python3.5/asyncio/tasks.py”, line 239, in _step
result = coro.send(None)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/setup.py”, line 159, in _async_setup_component
conf_util.async_process_component_config(hass, config, domain)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/config.py”, line 619, in async_process_component_config
platform = get_platform(domain, p_name)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/loader.py”, line 104, in get_platform
return get_component(PLATFORM_FORMAT.format(domain, platform))
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/loader.py”, line 142, in get_component
module = importlib.import_module(path)
File “/usr/lib/python3.5/importlib/init.py”, line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “”, line 986, in _gcd_import
File “”, line 969, in _find_and_load
File “”, line 958, in _find_and_load_unlocked
File “”, line 673, in _load_unlocked
File “”, line 669, in exec_module
File “”, line 775, in get_code
File “”, line 735, in source_to_code
File “”, line 222, in _call_with_frames_removed
File “/home/homeassistant/.homeassistant/custom_components/media_player/braviatv_psk.py”, line 7

^
SyntaxError: invalid syntax
2017-11-26 13:55:22 ERROR (Thread-14)

Can you please check if the content of your braviatv_psk.py is correct compared to this file?

You have an error on line 7 and that is a line containing comments, so no actual code.

Hi there,

This looks great, however - I am unable to utilise this component due to the fact my Sony Bravia does not have a Pre-Shared Key option in the TV’s settings… Is there a work around for this? I’ve been unable to get the stock braviatv platform going again.

Thanks so much! :slight_smile:

The Bravia component will only work with a PIN or with the Pre-Shared Key in my version.

If that’s both not working for you then I guess you have found a good reason to buy a new TV :wink:

I have updated the component on Github and the point mentioned above is now implemented.
You can download the latest version at https://github.com/gerard33/home-assistant.

Ahh okay… Yes the stock Bravia TV Component using the code is very unstable, and has since stopped working for me.

Time to get a Bravia and make sure it has a PSK option I reckon!

Thanks for you help, all the best with your project! :slight_smile:

I have been having problems with it losing connection to HA for a couple of sec and then returning. This results in automation problems but otherwise there seem to be no affect to the TV watching when this happen. I am not sure if it’s the XBR 900e or the component. Any help would be appreciated.

ASYNC UPDATE
2017-12-18 15:12:58 ERROR (SyncWorker_9) [braviarc.braviarc_psk] Exception: HTTPConnectionPool(host=‘192.168.2.112’, port=80): Max retries exceeded with url: /sony/audio (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x10eb35e10>, ‘Connection to 192.168.2.112 timed out. (connect timeout=5)’))
2017-12-18 15:12:58 ERROR (SyncWorker_9) [custom_components.media_player.braviatv_psk] No data received from TV, probably it has just been turned off. Error message is:
2017-12-18 15:12:58 ERROR (SyncWorker_9) [custom_components.media_player.braviatv_psk] ‘NoneType’ object has no attribute 'get’
2017-12-18 15:19:57 ERROR (SyncWorker_1) [braviarc.braviarc_psk] Exception: HTTPConnectionPool(host=‘192.168.2.112’, port=80): Max retries exceeded with url: /sony/audio (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x10fa179b0>, ‘Connection to 192.168.2.112 timed out. (connect timeout=5)’))
2017-12-18 15:19:57 ERROR (SyncWorker_1) [custom_components.media_player.braviatv_psk] No data received from TV, probably it has just been turned off. Error message is:
2017-12-18 15:19:57 ERROR (SyncWorker_1) [custom_components.media_player.braviatv_psk] ‘NoneType’ object has no attribute 'get’
2017-12-18 15:33:08 ERROR (SyncWorker_5) [braviarc.braviarc_psk] Exception: HTTPConnectionPool(host=‘192.168.2.112’, port=80): Max retries exceeded with url: /sony/system (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x10f9c5fd0>, ‘Connection to 192.168.2.112 timed out. (connect timeout=5)’))
2017-12-18 15:33:08 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py”, line 182, in _step
result = coro.throw(exc)
File “/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/homeassistant/core.py”, line 1031, in _event_to_service_call
yield from service_handler.func(service_call)
File “/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/homeassistant/components/media_player/init.py”, line 408, in async_service_handler
yield from getattr(player, method[‘method’])(**params)
File “/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/futures.py”, line 332, in iter
yield self # This tells Task to wait for completion.
File “/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py”, line 250, in _wakeup
future.result()
File “/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/futures.py”, line 245, in result
raise self._exception
File “/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/concurrent/futures/thread.py”, line 56, in run
result = self.fn(*self.args, **self.kwargs)
File “/Users/jmk/.homeassistant/custom_components/media_player/braviatv_psk.py”, line 328, in turn_off
self._braviarc.turn_off()
File “/Users/jmk/.homeassistant/deps/lib/python/site-packages/braviarc/braviarc_psk.py”, line 311, in turn_off
self.send_req_ircc(self.get_command_code(‘PowerOff’))
File “/Users/jmk/.homeassistant/deps/lib/python/site-packages/braviarc/braviarc_psk.py”, line 252, in get_command_code
self._refresh_commands()
File “/Users/jmk/.homeassistant/deps/lib/python/site-packages/braviarc/braviarc_psk.py”, line 245, in _refresh_commands
if not resp.get(‘error’):
AttributeError: ‘NoneType’ object has no attribute ‘get’

Do you still have these issues? If so I can update the timeout in braviarc_psk.py as your error seems to be due to the timeout, which I currently have set to 5 seconds.

No, I don’t get the errors anymore, i think it was a network issue which has been resolved. Thanks for your help.

1 Like

works fine.
Managed to change channel list to dvbs and sorted it.
But there are still more than 700 channels.
Is there a way to load only favourites (e.g. favourites:Tv)?