home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
74.36k stars 31.26k forks source link

Fire events on homekit TV remote key press #36679

Closed wasp100 closed 4 years ago

wasp100 commented 4 years ago

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:

commands
(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

probot-home-assistant[bot] commented 4 years ago

homekit documentation homekit source (message by IssueLinks)

probot-home-assistant[bot] commented 4 years ago

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)

bdraco commented 4 years ago

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

wasp100 commented 4 years ago

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)
bdraco commented 4 years ago

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.

nickw444 commented 4 years ago

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.

nickw444 commented 4 years ago

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

nickw444 commented 4 years ago

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.

wasp100 commented 4 years 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?

nickw444 commented 4 years ago

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

wasp100 commented 4 years ago

@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: ....
nickw444 commented 4 years ago

@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

wasp100 commented 4 years ago

@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!

noamdrory commented 4 years ago

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:
        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:

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

sefki21 commented 4 years ago

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

    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
nickw444 commented 4 years ago

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

wasp100 commented 4 years ago

@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.

bdraco commented 4 years ago

Sorry mis-fire on the wrong issue.

wasp100 commented 4 years ago

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??

bdraco commented 4 years ago

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.

wasp100 commented 4 years ago

@bdraco , nope it’s causing no issues.

nickw444 commented 4 years ago

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

noamdrory commented 4 years ago

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.

wasp100 commented 4 years ago

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.

bdraco commented 4 years ago

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.

wasp100 commented 4 years ago

@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. :(

bdraco commented 4 years ago

@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?

wasp100 commented 4 years ago

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 :(

wasp100 commented 4 years ago

@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 commented 4 years ago

@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?

wasp100 commented 4 years ago

@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 %}
bdraco commented 4 years ago

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.

wasp100 commented 4 years ago

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.

bdraco commented 4 years ago

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?

wasp100 commented 4 years ago

@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
bdraco commented 4 years ago

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

wasp100 commented 4 years ago

@bdraco Awesome, thanks!

bdraco commented 4 years ago

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

Checkout the example yaml configuration.

bdraco commented 4 years ago
    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

wasp100 commented 4 years ago

@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?

bdraco commented 4 years ago

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

wasp100 commented 4 years ago

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

bdraco commented 4 years ago

@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.

bdraco commented 4 years ago

@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
bdraco commented 4 years ago

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

noamdrory commented 4 years ago

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)

bdraco commented 4 years ago

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.

wasp100 commented 4 years ago

@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 commented 4 years ago

@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

wasp100 commented 4 years ago

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