Updated JSON CEC for Kodi

I’ve added to the code for the JSON-CEC on Kodi so that it will conditionally stop playback and then turn off the television.

Before, I had this:

media_player:
  - platform: kodi
    host: 192.168.86.2
    name: "Living Room Kodi"
    turn_on_action:
      service: media_player.kodi_call_method
      data:
        entity_id: media_player.living_room_kodi
        method: Addons.ExecuteAddon
        addonid: script.json-cec
        params:
          command: activate
    turn_off_action:
      service: media_player.kodi_call_method
      data:
        entity_id: media_player.living_room_kodi
        method: Addons.ExecuteAddon
        addonid: script.json-cec
        params:
          command: standby

Which is less than ideal. If I was streaming or using Live TV, the turn_off_action would turn off the television, but leave the stream going. So I added this:

    turn_off_action:
      - service: media_player.stop
      - service: media_player.kodi_call_method
        data:
          entity_id: media_player.living_room_kodi
          method: Addons.ExecuteAddon
          addonid: script.json-cec
          params:
            command: standby

… which was better, but if a ‘turn_off_action’ was issued for any reason (Alexa, for instance), then the television would “Wake up” because it received a Stop command, and then go on standby again. I wrote code to fix that, now it simply says:

    turn_off_action:
      service: media_player.kodi_call_method
      data:
        entity_id: media_player.living_room_kodi
        method: Addons.ExecuteAddon
        addonid: script.json-cec
        params:
          command: stop_and_standby

And now it performs as expected.

This integrates very well with Alexa:

homeassistant:
  customize:
    media_player.living_room_kodi:
      emulated_hue: true
      emulated_hue_name: "TV"

Code is available at https://github.com/YankeeGreggo/script.json-cec/tree/feature_stop_and_standby if you wish to update your JSON-CEC on Kodi to support this.

Hi. Nice work, did you attempt to get this merged upstream? Do you mind if I try? (I don’t see any active pull requests.)

Ah my mistake, yes you did (but not using a pull request).

For anyone stumbling upon this thread, this feature was added to the upstream repository. However, it’s not documented in the README so I’ll try to get that fixed. The stop_and_standby command by @YankeeGreggo was added in the upstream (“original”) repository (https://github.com/joshjowen/script.json-cec) on 24 Jan 2019.

Does this still work?

I get

Run API method media_player.kodi_pi.Addons.ExecuteAddon({'addonid': 'script.json-cec', 'params': {'command': 'activate'}}) error: {'code': -32602, 'message': 'Invalid params.'}