Core: Fire events on homekit TV remote key press

Created on 11 Jun 2020  Â·  74Comments  Â·  Source: home-assistant/core

The problem


HomeKit TV remote key presses do not work with Universal Media Player

Environment

Problem-relevant configuration.yaml

homekit:
  entity config:
    media_player.living_room
      feature_list:
        - feature: on_off
        - feature: play_pause
        - feature: play_stop
        - feature: toggle_mute

Traceback/Error logs


Additional information

@bdraco
In the HomeKit documentation, television media players only allow the following:
All media players that have tv as their device_class. Represented as Television and Remote accessories in HomeKit to control on / off, play / pause, select source, or volume increase / decrease, depending on supported_features of entity. Requires iOS 12.2/macOS 10.14.4 or later.

It does not include the directional keys to use the TV remote key press.

In the Universal Media Player, it only allows for:

(string)(Optional)
Commands to be overwritten. Possible entries are turn_on, turn_off, select_source, volume_set, volume_up, volume_down and volume_mute.

It does not include play and pause, menu and directional keys.

Can these please be included in both the universal media player and HomeKit so that we can use the TV remote key presses.

Thanks

homekit

All 74 comments

homekit documentation
homekit source
(message by IssueLinks)

Hey there @bdraco, mind taking a look at this issue as its been labeled with a integration (homekit) you are listed as a codeowner for? Thanks!
(message by CodeOwnersMention)

You need to setup an automation per the documentation here: https://www.home-assistant.io/integrations/homekit#ios-remote-widget

Hi @bdraco, I've tried this but it does not work as expected. I have set up an automation (example below) but get no response when pressing the key:

automation...
  trigger:
    platform: event
    event_type: homekit_tv_remote_key_pressed
    event_data:
      key_name: arrow_right
  action:
    service: remote.send_command
    entity_id: remote.harmony
    data:
      device: <device_id>
      command: DirectionRight

I am using a Harmony remote to execute the commands through the universal media player.

Previously, before the key presses were allowed, I was able to control the volume using the physical volume buttons on my iPhone. Now, it sends some additional remote command (think it's the back/ return command) and then only does the volume up and down work. This never happened before, so I think there might be a problem with the way key presses were implemented, or it's conflicting with the media player control of volume??

Also, sometimes I get a error message on the virtual remote on my phone saying that the TV cannot be found - make sure you on the same wifi network etc..

Further to this, I've noticed that I get the error when switching the TV off:
media_player.living_room_tv: Sources out of sync. Restart Home Assistant

This only occurs when the TV is switched off. When the TV is on and the sources are selected, there is no error in the logs. It seems like in the code, the media player is looking for a source when the device is switched off, when there is none??

Copy of type_media_players.py:

        # Set active input
        if self.support_select_source and self.sources:
            source_name = new_state.attributes.get(ATTR_INPUT_SOURCE)
            _LOGGER.debug("%s: Set current input to %s", self.entity_id, source_name)
            if source_name in self.sources:
                index = self.sources.index(source_name)
                if self.char_input_source.value != index:
                    self.char_input_source.set_value(index)
            else:
                _LOGGER.warning(
                    "%s: Sources out of sync. Restart Home Assistant", self.entity_id,
                )
                if self.char_input_source.value != 0:
                    self.char_input_source.set_value(0)

I didn't add the keypress events, so I haven't had a chance to play with them yet.

@nickw444 might have better input here.

Interesting, this has been working perfectly for me since it made it in the last release.

My configuration is like so:

https://github.com/nickw444/home/blob/da0bc85b8a98f0af13ec8c465612df2bc017417b/home-assistant/martin-pl/integrations/entities/samsung_smart_tv.yaml#L22-L119

As you can see, I have not bound the volume up/down keys, but the functionality continues to work as it did previously, using the media_player volume up/down service calls to adjust volume.

so I think there might be a problem with the way key presses were implemented, or it's conflicting with the media player control of volume??

I think you're onto something here. It looks like prior to my PR the set_remote_key function performed call_service for more than _just_ the play/pause key:

https://github.com/home-assistant/core/blob/742e36ba26c3b41430912d06e5ab2a2c56bcb93f/homeassistant/components/homekit/type_media_players.py#L363-L378

But after my PR it doesn't do this anymore:

https://github.com/home-assistant/core/blob/2b06fbbcf0379379a7a15ba4d721b1e9b27bff6a/homeassistant/components/homekit/type_media_players.py#L378-L402

Must have been an oversight on my behalf. I'll raise a patch later today.

Actually, scratch that - the existing code _only_ knew how to handle media play/pause, so I don't think that's the case here:

https://github.com/home-assistant/core/blob/742e36ba26c3b41430912d06e5ab2a2c56bcb93f/homeassistant/components/homekit/type_media_players.py#L72-L86

Further to this, I've noticed that I get the error when switching the TV off:
media_player.living_room_tv: Sources out of sync. Restart Home Assistant

I think this is a separate issue, at least in my case. I was getting this error since I added my TV to Home Assistant about 9 months ago.

@nickw444 i've looked at your setup and modified mine slightly, but still no luck...
In Developer Tools > Events, it allows you to Listen to events
In the Event to Subscribe to, I've entered homekit_tv_remote_key_pressed and hit START LISTENING.
I then open the media player remote on my iPhone and press one of the buttons, but I don't get a response on HA through the listening?
Surely I should see something here?

Here's a recording of my events and corresponding capture from my phone when pressing each key. As you can see in the recording, all keys are handled except for the play/pause + vol up/down as they are handled directly via the media_player entity.

2020-06-19 09 22 53

Surely I should see something here?

I definitely think you should. Out of interest, have you set the device_class for the entity:

https://github.com/nickw444/home/blob/da0bc85b8a98f0af13ec8c465612df2bc017417b/home-assistant/martin-pl/integrations/entities/samsung_smart_tv.yaml#L2-L4

Also for the record, my homekit config looks like this:

https://github.com/nickw444/home/blob/da0bc85b8a98f0af13ec8c465612df2bc017417b/home-assistant/martin-pl/integrations/homekit.yaml#L1

@nickw444, yes the device_class is set as tv in customize, and I see the TV icon with power and source control in the Home App.
The power and source selection works perfectly. The remote component does not work though :(
Do you suspect that it's an issue with the universal media player?

media_player:
  - platform: universal
    name: Living Room TV
    commands:
      turn on:
        service: ....

@wasp100 can you provide the configuration you're encountering the issue with, I want to see if I can reproduce the issue on my end

@nickw444, Here is the setup:

media_player:
  - platform: universal
    name: Media Room TV
    commands:
      turn_on:
        service: remote.turn_on
        entity_id: remote.media_room_harmony
        data:
          activity: Watch Apple TV
      turn_off:
        service: remote.turn_off
        entity_id: remote.media_room_harmony
      volume_up:
        service: remote.send_command
        entity_id: remote.media_room_harmony
        data:
          device: Receiver
          command: VolumeUp 
      volume_down:
        service: remote.send_command
        entity_id: remote.media_room_harmony
        data:
          device: Receiver
          command: VolumeDown 
      select_source:
        service: remote.turn_on
        data_template:
          entity_id: remote.media_room_harmony
          activity: >-
            {% if source == 'Watch Apple TV' %}
              Watch Apple TV
            {% elif source == 'Watch Blu-ray' %}
              Watch Blu-ray
            {% elif source == 'Play PS3' %}
              Play PS3
            {% elif source == 'Play Xbox' %}
              Play Xbox
            {% else %}
            {% endif %}
    attributes:
      state: remote.media_room_harmony
      source: remote.media_room_harmony|current_activity

customize.yaml

media_player.media_room_tv:
  source_list: [Watch Apple TV,Watch Blu-ray,Play PS3,Play Xbox]
  device_class: tv

Example of automation for key presses

  alias: 'homekit remote: media key arrow_right'
  description: ''
  trigger:
  - platform: event
    event_type: homekit_tv_remote_key_pressed
    event_data:
      key_name: arrow_right
  condition: []
  action:
  - service: remote.send_command
    entity_id: remote.media_room_harmony
    data:
      command: DirectionRight
      device: Apple TV

Hope this helps!

Hey @nickw444 , same goes here. Power on/off and source select works perfect from Home app and Volume up/down works as well from iOS remote widget, but no other keys (tried to listen to the event homekit_tv_remote_key_pressed but nothing comes up when pressing the keys from the widget).

Here's my setup (I'm using smartir as the media_player platform):

configuration.yaml:

```# SmartIR remotes
smartir:
check_updates: false
media_player:

  • platform: smartir
    name: Living room TV
    unique_id: tv
    device_code: 1060
    controller_data: 10.0.0.33
    power_sensor: sensor.tv_sensor
    ...
    homekit:
    filter:
    exclude_domains:

    • device_tracker

    • person

    • automation

      media_player.tv:

      device_class: tv

      source_list: [Mibox,Apple TV]

      entity_config:

      media_player.tv:

      name: "TV"

      feature_list:

      - feature: on

      - feature: off

      - feature: volumeUp

      - feature: volumeDown

here's the automation attached to it (which works if I simulate the event myself)
> automations.yaml:
```- id: '1592688761428'
  alias: tv-up-arrow
  description: ''
  trigger:
  - event_data:
      key_name: arrow_up
    event_type: homekit_tv_remote_key_pressed
    platform: event
  condition: []
  action:
  - data:
      host: 10.0.0.33
      packet: JgC2ACMRFSYVEhUcFSYVExMUFB0VHBYmFDEVAAGUIRMVJhQTFB0UJxQUFRMUHRQdFCgVMRQAAZUiExQnFRIVHBUnFBMVEhQdFRwUJxQxEwABlyITFSYVEhQdFSYVEhQUFB0VHBUnFi4VAAGVIRMTKBMUEx4TKBQTFhIVHBUcFSYVMBQAAZYjEhUmFRMUHRQnFBMTFBMeFB0VJhQxFQABlSAUFCcUFBUcFSYVExUSFRwVHBUmFTATAA0FAAA=
    service: broadlink.send

and if needed, here's my smartir configuration (from which I use only the on/off volume up/down)

custom_components/smartir/codes/media_player/1060.json:
{ "manufacturer": "Samsung", "supportedModels": [ "UE40F6500" ], "supportedController": "Broadlink", "commandsEncoding": "Base64", "commands": { "off": "JgBGAJOVEDoQOhA6DxYPFhAVEBUQFRA6ETkROREUERQRFBEUEBUQFREUERQRORE5EBUQFRE5ETkRORE5ERUQFRA6DzsPFhAADQUAAA==", "on": "JgBGAJKVETkRORA6ERQRFBEUERQRFBE5ETkQOhAVEBUQFREUEBUQOhEUERQRORE5EBURFBA6EBUQOhE5EBUQFRA6EDoRFBEADQUAAA==", "volumeDown": "JgDuAAYcCzkHHAsXChkGHAs6CBsGHAs6Bj4LOgk7CxgLGAoABaMLGAs5DBcGHAsYCTwGHAs5CzoGHAsYBxsLGAk8Bj4LAAWhCxkKOwsYChkFHQoYCTwGHAsXCzoLOgs6Bj4LFwsYBgAFqAcbCzoLGAYcCxcHPgsXCjsHPgsXCRoHGwsYBj4MOQkABaULGAY+DBYKGQYcCxgGPwsXCxgGPgs5CzoKOwsXBxwKAAWkCxcLOgYcCxgKGAY/CxgGPgw4ChkGHAsYCRkIPQs6CgAFowoZBj8LFwccChgKGQY+CxgGHAs6CjoHPgw4CxgGHAsADQUAAAAAAAAAAAAA", "volumeUp": "JgCCAAYcCxgKGAs5CzoHPgsXCxgGHAsYBj4LOgsABaMLFwc+CxcJGgYcCzoGHAsYChgHPgo6DDkHPQsYBxsLAAWjCxgKOwYcDBYKGQYcCzoGPwo6DBcGHAsXChkGPwo7CgAFowsYBj4KGQYcCxcKOwccChkKGAs5CzoGPgs6CBsKGAsADQUAAAAAAAA", "mute": "JgDuAAsXCjsLFwsYChgKGAsYChgKGAs6DDkMOQw5DBcKGAsABdcLFwo7CxcLGAoYCjsLOgs6CzoLFwsYChgLFws6DDkMAAWQDBcLOgsXCxgKGAoYCxgKGAoYCzoLOgw5DDkMFwoYCgAF1wwXCjsLFwoYCxgKOws6CzoLOgsXChgLGAoYCjsLOgsABZELFws6DBcKGAsXCxgKGAsYChgKOws6CzoLOgsXCxgKAAXXCxcLOgsYChgKGAs6CzoMOQs6CxgKGAoYCxgKOwo7CwAFkQsXCjsLFwsYChgKGAsYChgKGQo6CzoLOgs6DBcKGAoADQUAAAAAAAAAAAAA", "play_pause": "JgC2ACETFSYUExUcFSYVEhUTEzIUHRUSFTAVAAGWIxETKBQTEx8TJxQUFBQUMBUcFRMTMRUAAZUiEhUnFRIVHBUmFRIVExMxFB0VExQxFQABlCITFCcTFBMeEyYWExYSFTAVHBUSFDEVAAGVIRMVJhYSFRwVJhUTFBMTMhMeExQUMRQAAZYiExUmFxAUHhMoExQVExMxFR0UExQxFAABlSITFCcUExUcFSYVEhQUEzEUHRYSFTAVAA0FAAA=", "key_arrow_up": "JgC2ACMRFSYVEhUcFSYVExMUFB0VHBYmFDEVAAGUIRMVJhQTFB0UJxQUFRMUHRQdFCgVMRQAAZUiExQnFRIVHBUnFBMVEhQdFRwUJxQxEwABlyITFSYVEhQdFSYVEhQUFB0VHBUnFi4VAAGVIRMTKBMUEx4TKBQTFhIVHBUcFSYVMBQAAZYjEhUmFRMUHRQnFBMTFBMeFB0VJhQxFQABlSAUFCcUFBUcFSYVExUSFRwVHBUmFTATAA0FAAA=", "key_arrow_down": "JgC2ACITFSYUExUcFSYVEhUTEx4TKBMoFBMVAAGoIhMUJxQTFB0VJhMUFhIVHBUmFSYVExQAAakgFBQnFRIVHBUnFRIVEhUcFSYVJhQUEwABqiITEygUExQdFSYWERQVFB0VJhQnFBMVAAGoIhIVJhcRFRwVJhUSFRMTHhMoEycVExUAAakkERMoExQUHRQnFRIWEhUcFSYVJhUSFQABqCMSFSYVExQdFCcUExMUFB4VJhQoFBMVAA0FAAA=", "key_arrow_left": "JgC2ACETFSYVExMeEygTFBUSFiUVMRQdFRwVAAGUIRMVJhQUFRwVJhUTFBMUJxYvEx4THhQAAZUiExUmFRIUHhQmFBQVExQnFTAUHRQdFQABlCEUFCcVEhUcFScVEhUTFCcUMBQeEx4TAAGWIhMVJhUSFRwVJhUSFBQVJhUwFRwVHRQAAZUgFBMnFBQVHBQnFRMVExQmFDITHhQdFAABlSITFSYVExQcFSYUFBMUFiYVMBQdFB0UAA0FAAA=", "key_arrow_right": "JgC2ACEUEygTFBQdFSYVEhYSFTAVEhUSFSYVAAGpIxMUKBMVFB0VJhQTFRIUMhMUExQUJxQAAakiExUmFRIVHBUmFRMUExUxFBMVEhUmFQABqSAUFCcVEhYcFSYVEhQUFDAVExQTEygTAAGrIhMUJxQTFB0VJhQTFBQVMBUSFRMUJxQAAagiExQnFRIVHBUnFBMVEhUwFRIVExMoEwABqiMSFSYVEhQdFSYVExMUFTAVEhUTFSYVAA0FAAA=", "sources": { "Apple TV": "JgBGAJKVETkRORE5ERQQFRAVERQQFRE5ETkROREUERQRFBEUERQRORAVERUPOxAVDzsQOhA6EBUQOhA6EBUQOhAVEBUPFhAADQUAAA==", "Mibox": "JgBGAJOUEDoRORE5ERQRFRAUERQRFBE5EDsQOhAVEBUPFg8WDxYPFhA6EBUPFg8WEBUROQ87EDoQFQ87EDoQOhA6EBUPFhAADQUAAA==" } } }
Thanks!
Noam

Hi all

Can only confirm the previous comments. Got 1 LG webos TV and 2 harmony remotes. The LG OS remote widget is working perfectly fine, but the harmony remote widgets do not work except for the volume control.

Within the developer tools, listening for the event homekit_tv_remote_key_pressed does not show any triggered using the harmony remotes - only the LG. Further firing the event myself with the harmony as entity does work perfectly, which shows that my automation would work.

Harmony Remote #1 Config:

- platform: universal
  name: Guest Room TV
  commands:
    turn_on:
      service: remote.turn_on
      entity_id: remote.harmony_gaestezimmer
      data:
        activity: Fernsehen
    turn_off:
      service: remote.turn_off
      entity_id: remote.harmony_gaestezimmer
    volume_up:
      service: remote.send_command
      entity_id: remote.harmony_gaestezimmer
      data:
          device: 65667640
          command: VolumeUp
    volume_down:
      service: remote.send_command
      entity_id: remote.harmony_gaestezimmer
      data:
          device: 65667640
          command: VolumeDown
    select_source:
      service: remote.turn_on
      data_template:
        entity_id: remote.harmony_gaestezimmer
        activity: >-
          {% if source == 'Fernsehen' %}
            Fernsehen
          {% elif source == 'Apple TV' %}
            Apple TV sehen
          {% else %}
            Fernsehen
          {% endif %}
  attributes:
    state: remote.harmony_gaestezimmer
    source: input_select.guest_room_tv

Automation - Harmony # 1

``` - alias: Call guestroom.button on HomeKit Remote key presses
trigger:
event_data:
entity_id: media_player.guest_room_tv
event_type: homekit_tv_remote_key_pressed
platform: event
action:
data_template:
command: "{% set key_map = {'arrow_right': 'DirectionRight', 'arrow_down' : 'DirectionDown', 'arrow_left':
'DirectionLeft', 'arrow_up': 'DirectionUp', 'select': 'Select', 'back': 'BACK', 'information':
'Info'} %} {{ key_map[trigger.event.data['key_name']] }}"
entity_id: remote.harmony_gaestezimmer
device: 65667640
service: remote.send_command

LG  Automation:
  • alias: Call webostv.button on HomeKit Remote key presses
    trigger:
    event_data:
    entity_id: media_player.lg_webos_smart_tv
    event_type: homekit_tv_remote_key_pressed
    platform: event
    action:
    data_template:
    button: "{% set key_map = {'arrow_right': 'RIGHT', 'arrow_down' : 'DOWN', 'arrow_left':
    'LEFT', 'arrow_up': 'UP', 'select': 'ENTER', 'back': 'BACK', 'information':
    'HOME'} %} {{ key_map[trigger.event.data['key_name']] }}"
    entity_id: "{{ trigger.event.data['entity_id'] }}"
    service: webostv.button
    ```

Thanks for sharing the config. I will cut out some time this weekend and see if I can reproduce this on my end.

@nickw444 Thanks for looking at this. Another issue I want to bring your attention to:
When I first open the Apple remote widget and press the volume up or volume down is pressed on the iPhone, it seems like Homekit checks whether the TV is on, and switches on the TV, even though it's on already. This only happens when the remote is first invoked. Thereafter the volume control works well. If I close the widget and open it again, I notice the same behaviour.

From the Harmony remote side of things, this causes some issues.

Homekit should first determine that the media player is on... if on, then execute the command. If not, then turn media player on and execute command.

@sefki21 I trust you seeing this behaviour as well?

More eloquently put, see https://community.home-assistant.io/t/homekit-new-options-tv-source/98912/69

Everything works including volume control via the phone’s volume buttons while in the Remote app.

The one quirk it has is that after opening the Remote app, the first time I press a volume button, the soundbar turns off. I suspect this is the Remote app’s way of trying to make sure the soundbar is on, but the IR code for the ‘on’ command is the same as the ‘off’ command. Luckily, the volume IR codes also turn the soundbar back on, so subsequent volume button presses turn the soundbar back on. I’ve worked around this quirk by just sending the volume up IR code for turn_on since it appears that also can turn the soundbar on.

Sorry mis-fire on the wrong issue.

Hi @bdraco,
Do you mind looking at the issue where an error occurs in the logs when the Media Player is switched off:
media_player.living_room_tv: Sources out of sync. Restart Home Assistant

This only occurs when the TV is switched off. When the TV is on and the sources are selected, there is no error in the logs. It seems like in the code, the media player is looking for a source when the device is switched off, when there is none??

Hi @bdraco,
Do you mind looking at the issue where an error occurs in the logs when the Media Player is switched off:
media_player.living_room_tv: Sources out of sync. Restart Home Assistant

This only occurs when the TV is switched off. When the TV is on and the sources are selected, there is no error in the logs. It seems like in the code, the media player is looking for a source when the device is switched off, when there is none??

Is this causing an issue? It looks like we could suppress the warning when the device is turned off but it wouldn't actually be a functional change.

@bdraco , nope it’s causing no issues.

Hey @wasp100 thanks for the bug report. I've been able to narrow it down on my end - the issue is that in your configuration, the media player has no "children" and does not support play/pause, which causes the key handling to not be registered. I have raised a draft PR with a proposed fix: https://github.com/home-assistant/core/pull/37180

Hi @nickw444, could you explain what do you mean by "children"? As for supporting play/pause, I enabled it for my media_player (since i'm using the _smartir_ component with a "dumb" tv I can simply "create" this feature) but still no events fired from the iOS remote widget.

Hi @nickw444 thanks for raising the draft PR.

@bdraco Does the change also fix this issue where:

When I first open the Apple remote widget and press the volume up or volume down is pressed on the iPhone, it seems like Homekit checks whether the TV is on, and switches on the TV, even though it's on already. This only happens when the remote is first invoked. Thereafter the volume control works well. If I close the widget and open it again, I notice the same behaviour.

Homekit should first determine that the media player is on... if on, then execute the command. If not, then turn media player on and execute command.

Hi @nickw444 thanks for raising the draft PR.

@bdraco Does the change also fix this issue where:

When I first open the Apple remote widget and press the volume up or volume down is pressed on the iPhone, it seems like Homekit checks whether the TV is on, and switches on the TV, even though it's on already. This only happens when the remote is first invoked. Thereafter the volume control works well. If I close the widget and open it again, I notice the same behaviour.
Homekit should first determine that the media player is on... if on, then execute the command. If not, then turn media player on and execute command.

I don't think have logic to turn the tv on if its off on remote press, thats all homekit itself so its not something we can control.

@bdraco I don't think I'm explaining it well. Please see https://community.home-assistant.io/t/homekit-new-options-tv-source/98912/69

Everything works including volume control via the phone’s volume buttons while in the Remote app.

The one quirk it has is that after opening the Remote app, the first time I press a volume button, the soundbar turns off. I suspect this is the Remote app’s way of trying to make sure the soundbar is on, but the IR code for the ‘on’ command is the same as the ‘off’ command. Luckily, the volume IR codes also turn the soundbar back on, so subsequent volume button presses turn the soundbar back on. I’ve worked around this quirk by just sending the volume up IR code for turn_on since it appears that also can turn the soundbar on.

Surely there must be a way to tell Homekit that the media player is on, so that it does not attempt to switch it on?
The behaviour I experience - if I am using the Harmony remote to control the Apple TV, and I go into the remote widget and press volume up. It exits me from the streaming app I was last viewing. :(

@wasp100

    def set_remote_key(self, value):
        """Send remote key value if call came from HomeKit."""
        _LOGGER.debug("%s: Set remote key to %s", self.entity_id, value)
        key_name = MEDIA_PLAYER_KEYS.get(value)
        if key_name is None:
            _LOGGER.warning("%s: Unhandled key press for %s", self.entity_id, value)
            return

        if key_name == KEY_PLAY_PAUSE:
            # Handle Play Pause by directly updating the media player entity.
            state = self.hass.states.get(self.entity_id).state
            if state in (STATE_PLAYING, STATE_PAUSED):
                service = (
                    SERVICE_MEDIA_PLAY if state == STATE_PAUSED else SERVICE_MEDIA_PAUSE
                )
            else:
                service = SERVICE_MEDIA_PLAY_PAUSE
            params = {ATTR_ENTITY_ID: self.entity_id}
            self.call_service(DOMAIN, service, params)
        else:
            # Other keys can be handled by listening to the event bus
            self.hass.bus.fire(
                EVENT_HOMEKIT_TV_REMOTE_KEY_PRESSED,
                {ATTR_KEY_NAME: key_name, ATTR_ENTITY_ID: self.entity_id},
            )


There isn't any code that turns the tv on or off when a key is pressed

    def set_volume(self, value):
        """Send volume step value if call came from HomeKit."""
        _LOGGER.debug("%s: Set volume to %s", self.entity_id, value)
        params = {ATTR_ENTITY_ID: self.entity_id, ATTR_MEDIA_VOLUME_LEVEL: value}
        self.call_service(DOMAIN, SERVICE_VOLUME_SET, params)

    def set_volume_step(self, value):
        """Send volume step value if call came from HomeKit."""
        _LOGGER.debug("%s: Step volume by %s", self.entity_id, value)
        service = SERVICE_VOLUME_DOWN if value else SERVICE_VOLUME_UP
        params = {ATTR_ENTITY_ID: self.entity_id}
        self.call_service(DOMAIN, service, params)

There also isn't any code that turns the the tv on/off when the volume is changed.

Maybe the underlying tv entity is doing something to turn itself on/off when it gets a volume command?

Hi @bdraco,
So, an interesting observation:
In the universal media player config, I removed the volume up and volume down config, and reset the HomeKit tv entity.
What I noticed is that when I went into the remote widget and pressed the volume up/ volume down on the iPhone, the TV would still attempt to switch itself on (even though it's on)!
Via the HomeAssistant interface (with volume up and down enabled) it works perfectly. It's only HomeKit that does this :(

@bdraco I've added back the volume up and down config and checked the logs when pressing the volume up button:

2020-07-01 11:55:28 DEBUG (Thread-13) [pyhap.hap_server] Request PUT from address '('192.168.1.10', 55257)' for path '/characteristics'.
2020-07-01 11:55:28 DEBUG (Thread-13) [pyhap.hap_server] Set characteristics content: {'characteristics': [{'aid': 4227428133, 'iid': 18, 'value': 0}]}
2020-07-01 11:55:28 DEBUG (Thread-13) [pyhap.characteristic] client_update_value: VolumeSelector to 0 from client: ('192.168.1.10', 55257)
2020-07-01 11:55:28 DEBUG (Thread-13) [homeassistant.components.homekit.type_media_players] media_player.living_room_tv: Step volume by 0
2020-07-01 11:55:29 INFO (Thread-13) [pyhap.hap_server] 192.168.1.10 - "PUT /characteristics HTTP/1.1" 204 -
2020-07-01 11:55:29 DEBUG (Thread-13) [pyhap.hap_server] Request PUT from address '('192.168.1.10', 55257)' for path '/characteristics'.
2020-07-01 11:55:29 DEBUG (Thread-13) [pyhap.hap_server] Set characteristics content: {'characteristics': [{'aid': 4227428133, 'iid': 9, 'value': 1}]}
2020-07-01 11:55:29 DEBUG (Thread-13) [pyhap.characteristic] client_update_value: Active to 1 from client: ('192.168.1.10', 55257)
2020-07-01 11:55:29 DEBUG (Thread-13) [homeassistant.components.homekit.type_media_players] media_player.living_room_tv: Set switch state for "on_off" to 1

The last line shows a state change for switch on_off to 1?:
2020-07-01 11:55:29 DEBUG (Thread-13) [homeassistant.components.homekit.type_media_players] media_player.living_room_tv: Set switch state for "on_off" to 1

This shouldn't be the case as the device is on already.

Hope this helps in narrowing down where the issue is coming from.

@bdraco I've added back the volume up and down config and checked the logs when pressing the volume up button:

2020-07-01 11:55:28 DEBUG (Thread-13) [pyhap.hap_server] Request PUT from address '('192.168.1.10', 55257)' for path '/characteristics'.

2020-07-01 11:55:28 DEBUG (Thread-13) [pyhap.hap_server] Set characteristics content: {'characteristics': [{'aid': 4227428133, 'iid': 18, 'value': 0}]}

2020-07-01 11:55:28 DEBUG (Thread-13) [pyhap.characteristic] client_update_value: VolumeSelector to 0 from client: ('192.168.1.10', 55257)

2020-07-01 11:55:28 DEBUG (Thread-13) [homeassistant.components.homekit.type_media_players] media_player.living_room_tv: Step volume by 0

2020-07-01 11:55:29 INFO (Thread-13) [pyhap.hap_server] 192.168.1.10 - "PUT /characteristics HTTP/1.1" 204 -

2020-07-01 11:55:29 DEBUG (Thread-13) [pyhap.hap_server] Request PUT from address '('192.168.1.10', 55257)' for path '/characteristics'.

2020-07-01 11:55:29 DEBUG (Thread-13) [pyhap.hap_server] Set characteristics content: {'characteristics': [{'aid': 4227428133, 'iid': 9, 'value': 1}]}

2020-07-01 11:55:29 DEBUG (Thread-13) [pyhap.characteristic] client_update_value: Active to 1 from client: ('192.168.1.10', 55257)

2020-07-01 11:55:29 DEBUG (Thread-13) [homeassistant.components.homekit.type_media_players] media_player.living_room_tv: Set switch state for "on_off" to 1

The last line shows a state change for switch on_off to 1?:

2020-07-01 11:55:29 DEBUG (Thread-13) [homeassistant.components.homekit.type_media_players] media_player.living_room_tv: Set switch state for "on_off" to 1

This shouldn't be the case as the device is on already.

Hope this helps in narrowing down where the issue is coming from.

The HomeKit controllers/iOS device is turning it on. It’s possible that calling the on service for your device doesn’t turn it on but instead Toggles the device on off. Which integration is providing the underlying device?

@bdraco, I'm using the universal media player which is using the Harmony Remote control

media_player:
  - platform: universal
    name: Media Room TV
    commands:
      turn_on:
        service: remote.turn_on
        entity_id: remote.media_room_harmony
        data:
          activity: Watch Apple TV
      turn_off:
        service: remote.turn_off
        entity_id: remote.media_room_harmony
      volume_up:
        service: remote.send_command
        entity_id: remote.media_room_harmony
        data:
          device: Receiver
          command: VolumeUp 
      volume_down:
        service: remote.send_command
        entity_id: remote.media_room_harmony
        data:
          device: Receiver
          command: VolumeDown 
      select_source:
        service: remote.turn_on
        data_template:
          entity_id: remote.media_room_harmony
          activity: >-
            {% if source == 'Watch Apple TV' %}
              Watch Apple TV
            {% elif source == 'Watch Blu-ray' %}
              Watch Blu-ray
            {% elif source == 'Play PS3' %}
              Play PS3
            {% elif source == 'Play Xbox' %}
              Play Xbox
            {% else %}
            {% endif %}

Lots going on here for sure. I should be able to put together a test setup like that when I get back next weekend (11th/12th) to see if I can replicate the issue.

Thanks @bdraco
Further to this, logs when the tv (media player) is switched on:

2020-07-01 11:49:30 DEBUG (MainThread) [homeassistant.components.homekit.type_media_players] media_player.living_room_tv: Set current active state to 1
2020-07-01 11:49:30 DEBUG (MainThread) [homeassistant.components.homekit.type_media_players] media_player.living_room_tv: Set current mute state to False
2020-07-01 11:49:30 DEBUG (MainThread) [homeassistant.components.homekit.type_media_players] media_player.living_room_tv: Set current input to Watch Apple TV

it seems to set the current active state to 1. No idea why it checks for on_off state as well when the volume button is pressed as Homekit knows that the current active state is on already.

When the tv is switched on and on another source other than Apple TV, and you hit the volume button, does it go right to Apple TV?

@bdraco Yes, it goes to the Apple TV. My assumption is that this is due to the TV turn on command is set to switch on the Apple TV activity

      turn_on:
        service: remote.turn_on
        entity_id: remote.media_room_harmony
        data:
          activity: Watch Apple TV

I'm pretty sure I know whats wrong. I'll test it out in a bit

@bdraco Awesome, thanks!

@wasp100 https://github.com/home-assistant/core/pull/37559

Checkout the example yaml configuration.

    attributes:
      state: remote.alexander_down_guest
      source_list: remote.alexander_down_guest|activity_list
      source: remote.alexander_down_guest|current_activity

source_list needs that PR, but state should work with the production version.

Edit: current_activity will work without that PR

@bdraco

my setup includes a customize:

media_player.media_room_tv:
  source_list: [Watch Apple TV,Watch Blu-ray,Play PS3,Play Xbox]
  device_class: tv

my setup has the following attributes:

    attributes:
      state: remote.media_room_harmony
      source: remote.media_room_harmony|current_activity

When I included source_list: media_player.media_room_tv|source_list in the attributes, it would not display correctly in HomeKit.

When I omit the source_list, shows the source list correctly, as it's explicitly defined in the customize.

So, with the new setup, I'm assuming that it would not be necessary to define the source_list in the customize in the future?

So, with the new setup, I'm assuming that it would not be necessary to define the source_list in the customize in the future?

Correct

@bdraco,
source: remote.media_room_harmony|current_activity does work in the current production version

@bdraco,
source: remote.media_room_harmony|current_activity does work in the current production version

Right, I forgot that does already exist. Updated my comment above.

@wasp100 Try this version as a custom_components https://github.com/bdraco/harmony/

Change

      turn_on:
        service: remote.turn_on
        entity_id: remote.media_room_harmony
        data:
          activity: Watch Apple TV

to

      turn_on:
        service: remote.turn_on
        entity_id: remote.media_room_harmony

Be sure to set Update current activity on start of activity switch. in the options flow.
And set the default activity to Previously Active Activity

Screen Shot 2020-07-06 at 10 58 43 AM

@wasp100 I also took care of that annoying restart log message in #37567

Hi all,
First, thanks for all of the updates, really, much appreciated!

Any news regarding the homekit_tv_remote_key_pressed? I've just updated to 0.112.3 (running HassOS 4.10)

Hi all,
First, thanks for all of the updates, really, much appreciated!

Any news regarding the homekit_tv_remote_key_pressed? I've just updated to 0.112.3 (running HassOS 4.10)

@nickw444 just has a few finishing touches left on the PR and then its ready to go.

@bdraco, apologies for not having responded sooner. I'm unable to test the new version as a custom component, as I don't have access to my setup at the moment. But I'm sure that it will work just fine.
Any idea when it will be put into production? I see that it has been labeled as a breaking change. Does this mean that it will only be released in 0.113?
Thanks again for all your help. Much appreciated.

@bdraco, apologies for not having responded sooner. I'm unable to test the new version as a custom component, as I don't have access to my setup at the moment. But I'm sure that it will work just fine.

Any idea when it will be put into production? I see that it has been labeled as a breaking change. Does this mean that it will only be released in 0.113?

Thanks again for all your help. Much appreciated.

Yes 0.113 since it has a breaking change and new functionality

Hi @bdraco.

I've just installed 0.113 and noticed that the volume control is still not working correctly. It still "switches on the device" when I first invoke the remote widget. media_player.living_room_tv: Set switch state for "on_off" to 1
Thereafter it works fine while I'm in the widget.
If I exit and return to the remote, the same thing occurs

See log below

2020-07-22 21:34:08 DEBUG (Thread-11) [pyhap.hap_server] Request PUT from address '('192.168.1.10', 50291)' for path '/characteristics'.
2020-07-22 21:34:08 DEBUG (Thread-11) [pyhap.hap_server] Set characteristics content: {'characteristics': [{'aid': 4227428133, 'iid': 18, 'value': 1}]}
2020-07-22 21:34:08 DEBUG (Thread-11) [pyhap.characteristic] client_update_value: VolumeSelector to 1 from client: ('192.168.1.10', 50291)
2020-07-22 21:34:08 DEBUG (Thread-11) [homeassistant.components.homekit.type_media_players] media_player.living_room_tv: Step volume by 1
2020-07-22 21:34:08 INFO (Thread-11) [pyhap.hap_server] 192.168.1.10 - "PUT /characteristics HTTP/1.1" 204 -
2020-07-22 21:34:09 DEBUG (Thread-11) [pyhap.hap_server] Request PUT from address '('192.168.1.10', 50291)' for path '/characteristics'.
2020-07-22 21:34:09 DEBUG (Thread-11) [pyhap.hap_server] Set characteristics content: {'characteristics': [{'aid': 4227428133, 'iid': 9, 'value': 1}]}
2020-07-22 21:34:09 DEBUG (Thread-11) [pyhap.characteristic] client_update_value: Active to 1 from client: ('192.168.1.10', 50291)
2020-07-22 21:34:09 DEBUG (Thread-11) [homeassistant.components.homekit.type_media_players] media_player.living_room_tv: Set switch state for "on_off" to 1

I've removed the media player from HomeKit and re-added it, but this did not help.

However, what I noticed is that the TV is now called Other in HomeKit. It should be called Living Room TV.
It correctly pulled the name across before, but not anymore.

Could it be related to this previous change?: https://github.com/home-assistant/core/pull/37571

Hi @bdraco.

I've just installed 0.113 and noticed that the volume control is still not working correctly. It still "switches on the device" when I first invoke the remote widget. media_player.living_room_tv: Set switch state for "on_off" to 1
Thereafter it works fine while I'm in the widget.
If I exit and return to the remote, the same thing occurs

Is it not showing as on when its already on?

See log below

2020-07-22 21:34:08 DEBUG (Thread-11) [pyhap.hap_server] Request PUT from address '('192.168.1.10', 50291)' for path '/characteristics'.
2020-07-22 21:34:08 DEBUG (Thread-11) [pyhap.hap_server] Set characteristics content: {'characteristics': [{'aid': 4227428133, 'iid': 18, 'value': 1}]}
2020-07-22 21:34:08 DEBUG (Thread-11) [pyhap.characteristic] client_update_value: VolumeSelector to 1 from client: ('192.168.1.10', 50291)
2020-07-22 21:34:08 DEBUG (Thread-11) [homeassistant.components.homekit.type_media_players] media_player.living_room_tv: Step volume by 1
2020-07-22 21:34:08 INFO (Thread-11) [pyhap.hap_server] 192.168.1.10 - "PUT /characteristics HTTP/1.1" 204 -
2020-07-22 21:34:09 DEBUG (Thread-11) [pyhap.hap_server] Request PUT from address '('192.168.1.10', 50291)' for path '/characteristics'.
2020-07-22 21:34:09 DEBUG (Thread-11) [pyhap.hap_server] Set characteristics content: {'characteristics': [{'aid': 4227428133, 'iid': 9, 'value': 1}]}
2020-07-22 21:34:09 DEBUG (Thread-11) [pyhap.characteristic] client_update_value: Active to 1 from client: ('192.168.1.10', 50291)
2020-07-22 21:34:09 DEBUG (Thread-11) [homeassistant.components.homekit.type_media_players] media_player.living_room_tv: Set switch state for "on_off" to 1

I've removed the media player from HomeKit and re-added it, but this did not help.

However, what I noticed is that the TV is now called Other in HomeKit. It should be called Living Room TV.
It correctly pulled the name across before, but not anymore.

Could it be related to this previous change?: #37571

You might have to rename it one time but then it should hold the name.

Is it not showing as on when its already on?

@bdraco If I understand your question correctly... yes, the logs show the tv is on
[homeassistant.components.homekit.type_media_players] media_player.living_room_tv: Set current active state to 1

The log below:

2020-07-22 22:52:46 DEBUG (MainThread) [homeassistant.components.homekit.type_switches] remote.harmony_hub: Set current state to True
2020-07-22 22:52:46 DEBUG (MainThread) [pyhap.characteristic] set_value: On to True
2020-07-22 22:52:46 DEBUG (MainThread) [homeassistant.components.homekit.accessories] New_state: <state media_player.living_room_tv=on; source_list=['Watch Apple TV', 'Play PS3'], is_volume_muted=False, source=Watch Apple TV, friendly_name=Living Room TV, supported_features=3456, device_class=tv @ 2020-07-22T22:52:46.765506+02:00>
2020-07-22 22:52:46 DEBUG (MainThread) [homeassistant.components.homekit.type_media_players] media_player.living_room_tv: Set current active state to 1
2020-07-22 22:52:46 DEBUG (MainThread) [pyhap.characteristic] set_value: Active to 1
2020-07-22 22:52:46 DEBUG (MainThread) [homeassistant.components.homekit.type_media_players] media_player.living_room_tv: Set current mute state to False
2020-07-22 22:52:46 DEBUG (MainThread) [homeassistant.components.homekit.type_media_players] media_player.living_room_tv: Set current input to Watch Apple TV
2020-07-22 22:52:46 DEBUG (MainThread) [pyhap.characteristic] set_value: ActiveIdentifier to 1

However as soon as I press the volume button using the remote widget, the volume will change one step
[homeassistant.components.homekit.type_media_players] media_player.living_room_tv: Step volume by 0
and then it sends a set switch on_off to 1
[homeassistant.components.homekit.type_media_players] media_player.living_room_tv: Set switch state for "on_off" to 1

Is the device showing as On or Off in homekit before you press the volume volume button from the remote widget?

@bdraco The device is showing as on. (The TV icon is lit up and it shows the source, which is the current activity, for eg. Living Room TV - Watch Apple TV).
When it's off, it shows Living Room TV - Off
I've just checked the logs, and when the TV is switch off, switch state for on_off is set to 0.
2020-07-22 23:09:45 DEBUG (Thread-11) [homeassistant.components.homekit.type_media_players] media_player.living_room_tv: Set switch state for "on_off" to 0

The switch state for on_off is not set to 1 when the tv is switched on.

Edit: On/Off and source select all work perfectly in HomeKit.

switch state in this case is the Active char so it looks like homekit is setting it to 1 even though its already 1 when you hit the volume up/down the first time.

Seems like a bug in iOS

2020-07-22 21:44:42 DEBUG (MainThread) [homeassistant.components.homekit.type_media_players] media_player.master_bed_tv: Set current active state to 1
2020-07-22 21:44:42 DEBUG (MainThread) [homeassistant.components.homekit.type_media_players] media_player.master_bed_tv: Set current mute state to False
2020-07-22 21:44:42 DEBUG (MainThread) [homeassistant.components.homekit.type_media_players] media_player.master_bed_tv: Set current input to Watch Apple TV

@bdraco homekit is setting the switch state for on_off to 1, not current active state when the volume button is pressed the first time.
It is also setting the state for on_off to 0 when the tv is changed to off.

Would it be possible to set on_off to 1 when current active state is set to 1? Would this not solve the problem, as HomeKit would then see the state as a 1 and not attempt to make it a 1 before the volume is changed.

In this case on_off is the Active char. They are one in the same. The log message is just misleading.

        self.char_active = serv_tv.configure_char(
            CHAR_ACTIVE, setter_callback=self.set_on_off
        )
    def set_on_off(self, value):
        """Move switch state to value if call came from HomeKit."""
        _LOGGER.debug('%s: Set switch state for "on_off" to %s', self.entity_id, value)
        service = SERVICE_TURN_ON if value else SERVICE_TURN_OFF
        params = {ATTR_ENTITY_ID: self.entity_id}
        self.call_service(DOMAIN, service, params)

@bdraco
Seems like a bug in iOS
I'm don't think this is the case, as I don't see others bringing up this issue when implementing the HomeKit TV using LG WebOS or other TV integrations.

Looking at the code: https://github.com/home-assistant/core/blob/dev/homeassistant/components/homekit/type_media_players.py

Trying to find the issue... I see that there are two instances of def set_on_off at lines 175 and 342

    def set_on_off(self, value):
        """Move switch state to value if call came from HomeKit."""
        _LOGGER.debug('%s: Set switch state for "on_off" to %s', self.entity_id, value)
        service = SERVICE_TURN_ON if value else SERVICE_TURN_OFF
        params = {ATTR_ENTITY_ID: self.entity_id}
        self.call_service(DOMAIN, service, params)

Would this perhaps be causing the issue?

@bdraco
Seems like a bug in iOS
I'm don't think this is the case, as I don't see others bringing up this issue when implementing the HomeKit TV using LG WebOS or other TV integrations.

Harmony responds a bit differently to an additional turn on command than these integrations so its not a problem for them.

Looking at the code: https://github.com/home-assistant/core/blob/dev/homeassistant/components/homekit/type_media_players.py

Trying to find the issue... I see that there are two instances of def set_on_off at lines 175 and 342

The one on line 175 is in the non-tv media player class. Its a completely separate class so its not relevant here.

    def set_on_off(self, value):
        """Move switch state to value if call came from HomeKit."""
        _LOGGER.debug('%s: Set switch state for "on_off" to %s', self.entity_id, value)
        service = SERVICE_TURN_ON if value else SERVICE_TURN_OFF
        params = {ATTR_ENTITY_ID: self.entity_id}
        self.call_service(DOMAIN, service, params)

Would this perhaps be causing the issue?

We could suppress the turn on even if the device is already active, but that might be a breaking change.

Hi, I‘ve updated as well and my harmony media players still don‘t fire any homekit_tv_remote_key_pressed events? Do I need to change something or what am I missing to get it working?

Hi, I‘ve updated as well and my harmony media players still don‘t fire any homekit_tv_remote_key_pressed events? Do I need to change something or what am I missing to get it working?

https://github.com/home-assistant/core/pull/37180 isn't merged yet

Apologies, my mistake. Any idea if it will be soon merged?

Apologies, my mistake. Any idea if it will be soon merged?

Its almost done, but its waiting for @nickw444 to pick it back up and get it over the finish line. I don't have knowledge of his schedule so I can't give you a good answer

We could suppress the turn on even if the device is already active, but that might be a breaking change.

@bdraco It would be great if it could be implemented. It will also make the key presses work properly with the harmony remote once it’s implemented.

I don’t understand why it would be a breaking change? Is it not a bug fix as it’s fixing a current functionality that’s not working correctly?

We could suppress the turn on even if the device is already active, but that might be a breaking change.

@bdraco It would be great if it could be implemented. It will also make the key presses work properly with the harmony remote once it’s implemented.

I don’t understand why it would be a breaking change? Is it not a bug fix as it’s fixing a current functionality that’s not working correctly?

Some device may be relying on the service call to turn on the media player before the volume can be sent. Its not really possible to predict.

@bdraco I agree, but shouldn’t the logic be:
(a) If media player is on and volume is called then do not attempt to switch on media player because it’s on already, and
(b) If volume is called and media player off, then turn on media player.

@bdraco I agree, but shouldn’t the logic be:
(a) If media player is on and volume is called then do not attempt to switch on media player because it’s on already, and
(b) If volume is called and media player off, then turn on media player.

That makes sense to me, but that's not how Apple has implemented it. We try not to suppress any events coming from the controller (iOS device) since we can't predict how Apple may change things in the future.

Its easy enough to suppress the on event, but we would have to mark it as a breaking change and possibly deal with the maint burden should Apple change the behavior in the future.

@bdraco thanks for the clarification. I didn’t think that Apple implemented it in that manner. I thought that there was perhaps a bug in the media player code that was creating this strange behavior.

Another question regarding the code - line 409

        # Power state television
        hk_state = 0
        if current_state not in MEDIA_PLAYER_OFF_STATES:
            hk_state = 1

Should it not read if current state not... hk_state = 1 else hk_state = 0, rather than defining hk_state as 0 from the onset?

@bdraco thanks for the clarification. I didn’t think that Apple implemented it in that manner. I thought that there was perhaps a bug in the media player code that was creating this strange behavior.

Another question regarding the code - line 409

        # Power state television
        hk_state = 0
        if current_state not in MEDIA_PLAYER_OFF_STATES:
            hk_state = 1

Should it not read if current state not... hk_state = 1 else hk_state = 0, rather than defining hk_state as 0 from the onset?

It can be written either way, it doesn't make a difference

@wasp100

After looking at this a bit more, I don't think we should change the homekit integration. I think the issue is that harmony's turn_on is doing a bit more than turn_on. Instead harmony should not change the activity when it gets a turn on event when that activity is already active. https://github.com/home-assistant/core/pull/38183

2020-07-24 23:15:40 DEBUG (Thread-87) [homeassistant.components.homekit.type_media_players] media_player.master_bed_tv: Set switch state for "on_off" to 1
2020-07-24 23:15:40 DEBUG (MainThread) [homeassistant.components.harmony.remote] Harmony Hub 2: Turn On
2020-07-24 23:15:40 DEBUG (MainThread) [homeassistant.components.harmony.remote] Harmony Hub 2: Find activity ID based on name
2020-07-24 23:15:40 DEBUG (MainThread) [homeassistant.components.harmony.remote] Harmony Hub 2: Current activity is already Watch Apple TV

Hi @bdraco Will #38183 and #37180 be pushed in 0.113.3?

@wasp100 0.114 . Beta is on 8/5, release on 8/12

is "homekit_tv_remote_key_pressed" supposed to fire in 0.114? i cant get it to work

Was this page helpful?
0 / 5 - 0 ratings