Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 95386

[SOLVED] Automate switching of Sony Bravia TV Scenes to change post-processing or picture mode for certain content

$
0
0

@shawly wrote:

Edit: I got it working now, see below for the solution


So I’ve been trying to get the “Scenes” from my Sony Bravia so I can add them to an input_select list and change the scene of my Bravia. I’ve been able to change the scene as well as get the current scene from the API, but I’m having difficulties mapping the values from the json array of scenes.

This is the raw response from my TVs API:

JSON Response
{
  "id": 40,
  "result": [
    {
      "candidate": [
        {
          "value": "general"
        },
        {
          "value": "auto"
        },
        {
          "value": "auto24pSync"
        },
        {
          "value": "photo"
        },
        {
          "value": "music"
        },
        {
          "value": "cinema"
        },
        {
          "value": "game"
        },
        {
          "value": "graphics"
        },
        {
          "value": "sports"
        },
        {
          "value": "animation"
        }
      ],
      "currentValue": "game"
    }
  ]
}

And here are my configurations for accessing the TV API:

sensors.yaml
######### Sony Bravia KDL-55W805B ###########
############# Current Scene Sensor ##########
  - platform: rest
    name: Sony Bravia TV Power Status
    resource_template: !secret bravia_systemurl
    headers:
      x-auth-psk: !secret bravia_psk
    method: POST
    payload: '{"method":"getPowerStatus","params":[""],"id":1,"version":"1.0"}'
    value_template:  >
      {%- if value_json is defined -%}
        {{- value_json.result[0].status -}}
      {%- else -%}
        offline
      {%- endif -%}
    scan_interval: 5
############# Current Scene Sensor ##########
  - platform: rest
    name: Sony Bravia TV Current Scene
    resource_template: !secret bravia_videoscreenurl
    headers:
      x-auth-psk: !secret bravia_psk
    method: POST
    payload: '{"method":"getSceneSetting","params":[""],"id":40,"version":"1.0"}'
    value_template:  >
      {%- if value_json is defined -%}
        {{- value_json.result[0].currentValue -}}
      {%- else -%}
        auto
      {%- endif -%}
    scan_interval: 5
############# All Scenes Sensor #############
  - platform: rest
    name: Sony Bravia TV Scene Setting
    resource_template: !secret bravia_videoscreenurl
    headers:
      x-auth-psk: !secret bravia_psk
    method: POST
    payload: '{"method":"getSceneSetting","params":[""],"id":40,"version":"1.0"}'
    json_attributes_path: "$.result.[0]"
    json_attributes:
      - candidate
      - currentValue
    value_template:  'OK'
input_selects.yaml
  sony_bravia_tv_scenes:
    name: Sony Bravia TV Scenes
    icon: mdi:image-filter-hdr
    options: [ None ]
rest_commands.yaml
######### Sony Bravia KDL-55W805B ###########
############ Set Scene Command ##############
  sony_bravia_tv_set_scene_setting:
    url: 'http://{{ host }}/sony/videoScreen'
    method: POST
    headers:
      x-auth-psk: '{{ psk }}'
      accept: 'application/json'
    payload: '{"method":"setSceneSetting","params":[{"value":"{{ scene }}"}],"id":40,"version": "1.0" }'
    content_type:  'application/json; charset=utf-8'
############ Fill input command #############
  sony_bravia_tv_input_select_set_options:
    url: '{{ url }}/services/input_select/set_options'
    method: POST
    headers:
      authorization: 'Bearer {{ token }}'
      content-type: 'application/json'
    payload: >-
      {
        "entity_id": "input_select.sony_bravia_tv_scenes",
        "options": [
      {%- for item in state_attr("sensor.sony_bravia_tv_scene_setting", "candidate")|map(attribute="value")|list -%}
        "{{- item -}}"{% if not loop.last %}, {% endif %}
      {%- endfor -%}
        ]
      }
automations.yaml
######### Sony Bravia KDL-55W805B ###########
####### Automation to disable automations when TV is off #######
  - alias: Sony Bravia TV Automations
    initial_state: 'off'
    trigger:
      - platform: state
        entity_id: sensor.sony_bravia_tv_power_status
        to: 'standby'
      - platform: state
        entity_id: sensor.sony_bravia_tv_power_status
        to: 'offline'
    action:
      # Turn off automations
      - service: automation.turn_off
        data:
          entity_id: automation.sony_bravia_tv_input_select_scene_set
      - service: automation.turn_off
        data:
          entity_id: automation.sony_bravia_tv_input_select_current_scene_populator
      - service: input_select.set_options
        data_template:
          entity_id: input_select.sony_bravia_tv_scenes
          options: [ 'None' ]
####### Automation to query scenes from Sony Bravia TV for input select #######
  - alias: Sony Bravia TV Input Select Scenes Populator
    initial_state: 'on'
    trigger:
      - platform: homeassistant
        event: start
      - platform: state
        entity_id: sensor.sony_bravia_tv_power_status
        to: 'active'
    action:
      # Turn off other automations
      - service: automation.turn_off
        data:
          entity_id: automation.sony_bravia_tv_input_select_scene_set
      - service: automation.turn_off
        data:
          entity_id: automation.sony_bravia_tv_input_select_current_scene_populator
      # Fill input_select.sony_bravia_tv_scenes with all available scenes
      - service: rest_command.sony_bravia_tv_input_select_set_options
        data:
          url: !secret internal_api_url
          token: !secret internal_api_token
      - service: input_select.select_option
        data_template:
          entity_id: input_select.sony_bravia_tv_scenes
          option: '{{ states("sensor.sony_bravia_tv_current_scene") }}'
      # Turn other automations on again
      - service: automation.turn_on
        data:
          entity_id: automation.sony_bravia_tv_input_select_scene_set
      - service: automation.turn_on
        data:
          entity_id: automation.sony_bravia_tv_input_select_current_scene_populator
      - service: automation.turn_on
        data:
          entity_id: automation.sony_bravia_tv_automations
####### Automation for sending the selected scene to Sony Bravia TV #######
  - alias: Sony Bravia TV Input Select Scene Set
    initial_state: 'off'
    trigger:
      - platform: state
        entity_id: input_select.sony_bravia_tv_scenes
    action:
      - service: rest_command.sony_bravia_tv_set_scene_setting
        data_template:
          host: !secret bravia_host
          psk: !secret bravia_psk
          scene: '{{ states("input_select.sony_bravia_tv_scenes") }}'
####### Automation to query current scene from Sony Bravia TV for input select #######
  - alias: Sony Bravia TV Input Select Current Scene Populator
    initial_state: 'off'
    trigger:
      - platform: state
        entity_id: sensor.sony_bravia_tv_current_scene
    action:
      - service: automation.turn_off
        data:
          entity_id: automation.sony_bravia_tv_input_select_scene_set
      - service: input_select.select_option
        data_template:
          entity_id: input_select.sony_bravia_tv_scenes
          option: '{{ states("sensor.sony_bravia_tv_current_scene") }}'
      - service: automation.turn_on
        data:
          entity_id: automation.sony_bravia_tv_input_select_scene_set
media_players.yaml
  - platform: braviatv
    name: Sony Bravia KDL-55W805B
    host: !secret bravia_host
secrets.yaml
bravia_host: '192.168.0.200' # this is your bravias ip
bravia_psk: '0000' # this is your bravias psk (set it within "Network Settings" on the TV)
bravia_videoscreenurl: 'http://192.168.100.49/sony/videoScreen' # this is the api url to get tv scenes, insert your bravias ip here
bravia_systemurl: 'http://192.168.100.49/sony/system' # this is the api url to get tv power state, insert your bravias ip here
internal_api_url: 'http://hass.example.com:8123/api' # make sure this url points to your hass api  otherwise it will result in 401 and 403 errors
internal_api_token: '<your token>' # generate a new long lived token in your profile settings
configuration.yaml
# enable api
api:

automation: !include automations.yaml
sensor: !include sensors.yaml
media_player: !include media_players.yaml
rest_command: !include rest_commands.yaml
input_select: !include input_selects.yaml

This configuration basically pulls the available scenes from the TVs API including the currently set scene of the TV. The “Sony Bravia TV Current Scene” sensor pulls the current scene from the API every 5 seconds. If the value has changed (when you manually change it with your remote for example) it will update the current option of the input_select to the current value of the API response. If you select a scene in the input_select it will send the new value to the TV updating the scene.
I wrote this automation so I can turn off the post processing when I want to play games on the TV.

This can be combined for stuff like changing the scene to game when your AVR or TV changes to a gaming console input, effectively reducing the post processing of the TV. Or if you want to watch anime it can change to animation or cinema when watching Netflix.

I also implemented a better state detection for the TVs on/off state via API since the media_player for Bravias isn’t as fast and isn’t implementing API calls properly.

I can confirm that this is working for my Sony Bravia KDL-55W805B, but if the API didn’t change it should effectively work for most Sony Bravia models released after 2013. I can’t guarantee it of course, you have to look through the API yourself.

Here are some useful resources for getting info on your Bravias API:
https://pro-bravia.sony.net/develop/integrate/rest-api/spec/index.html

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 95386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>