unfoldedcircle / integration-home-assistant

Home-Assistant Integration for Remote Two
Mozilla Public License 2.0
27 stars 5 forks source link

Support "remote"-entities #23

Closed pr11me closed 6 months ago

pr11me commented 1 year ago

The "Remotes" section in the web configurator currently supports only IR-remotes.

This is a request for it to also support the HA "remote" enitity exposed by some integrations (e.g. remote.philips_tv).

Currently, it is necessary to create a script for each button in order to use the remote via HA. This is not only tedious to create but also problematic since an activity only allows 50 active entities and each press is an entity.

With remote entity support, you would just add the remote entity and thus expose all commands to Remote Two like with IR remotes.

davidspek commented 1 year ago

This code comment seems somewhat related: https://github.com/unfoldedcircle/integration-home-assistant/blob/47903d484d09ee0f9534caaf553dfd625d06b2b7/src/client/service/media_player.rs#L87C44-L87C44

mvdwetering commented 10 months ago

This would be cool, but not sure how UC would get to know about the supported commands for that specific Remote entity. Unfortunately HA has no mechanism to indicate which commands are supported.

There was an architectural discussion about something like this, but that seems to have died out https://github.com/home-assistant/architecture/discussions/875

Edit: that being said, just having a textbox or something where we could just copy-paste the supported commands from the Home Assistant documentation would be a great help.

albaintor commented 9 months ago

This would be cool, but not sure how UC would get to know about the supported commands for that specific Remote entity. Unfortunately HA has no mechanism to indicate which commands are supported.

There was an architectural discussion about something like this, but that seems to have died out home-assistant/architecture#875

Edit: that being said, just having a textbox or something where we could just copy-paste the supported commands from the Home Assistant documentation would be a great help.

Indeed it could only be the same behaviour as the "send command" function where you type the corresponding command in plain text. Actually this is a big limitation because the media player component has very limited functions (play/pause, next/previous) and not the additional commands (such as arrow keys/OK commands). And without the bluetooth devices integration this is a must have Thank you

albaintor commented 9 months ago

Hi again, I have moved forward to implement the remote entity into the home assistant integration :

zehnm commented 9 months ago

To integrate HA remote-entites requires support in the integration API, core services and web-configurator. They don't fully match the current internal IR remote-entity and will require additional logic in the core services. If they currently slip through as available entities, then this is a bug and not a feature :-) Only the documented entities in the Core-Api are supported.

Supporting more HA entities, including remotes, is planned though.

albaintor commented 9 months ago

Ok thank you this is what I feared. I thought one could associate the HASS remote entity to reuse an existing RC2 entity type.

Meantime I have initialized the code for the hass integration part. I have not been able to submit it through a pull request, so here is a patch file based on head revision. remote_entity.patch

EDIT : actually (without the patch above) remote entities are correctly filtered by the HASS integration. The filter in the web ui lets you select remote type but no entity is displayed which is expected at this state

antifuchs commented 8 months ago

I've worked around this limitation in a slightly annoying way, which nevertheless seems to work a bit better than the built-in appletv remote for e.g. play/pause buttons on the remote UI (e.g. https://github.com/unfoldedcircle/feature-and-bug-tracker/issues/315). Here's what I did:

  1. Make one button helper entity per button on the appletv remote. Name them Home Theater Remote - <command> This is annoying to do, but you only have do it once. I don't think there's a way to export the yaml for the buttons I created, otherwise I'd paste it here to ease your suffering.
  2. Make an automation that hooks the appletv remote entity up to each of those buttons:
    id: '1708186917053'
    alias: Translate AppleTV buttons to remote events
    description: ''
    trigger:
    - platform: state
    entity_id:
      - input_button.home_theater_cursor_up
      - input_button.home_theater_cursor_down
      - input_button.home_theater_cursor_left
      - input_button.home_theater_cursor_right
      - input_button.home_theater_ok
      - input_button.home_theater_remote_menu
      - input_button.home_theater_remote_turn_off
      - input_button.home_theater_remote_turn_on
      - input_button.home_theater_remote_play_pause
      - input_button.home_theater_remote_home
      - input_button.home_theater_remote_volume_down
      - input_button.home_theater_remote_volume_up
      - input_button.home_theater_remote_channel_down
      - input_button.home_theater_remote_channel_up
      - input_button.home_theater_remote_next
      - input_button.home_theater_remote_previous
    condition: []
    action:
    - service: logbook.log
    metadata: {}
    data:
      name: hi
      message: |-
        {{ trigger.from_state.attributes.friendly_name|regex_replace(
          find='Home Theater Remote - ', 
          replace='') }}
    alias: Log command we're sending
    - service: remote.send_command
    metadata: {}
    data:
      command:
        - |-
          {{ trigger.from_state.attributes.friendly_name|regex_replace(
              find='Home Theater Remote - ', 
              replace='') 
          }}
    target:
      entity_id: remote.home_theater
    mode: single
  3. Reload the home-assistant integration on the remote & import all the "Home Theater Remote - " buttons
  4. Then, you can add those to your appletv activity & use the buttons on operations that should always work (e.g. play/pause, the menu & select button, skip forward/back)

I think until we have reliable reconnection working in the appletv remote integration, this works a lot better: You still wait for the remote to wake up, but it can send the command to hass on the first try & that reliably pauses media!

albaintor commented 7 months ago

Hi,

there is a more efficient approach (unless I missed something) : instead of input buttons + using automations, you should use scripts directly. Create one script per apple TV command, and import the scripts in the remote. This is more efficient because you have : remote => HA script call => Apple TV Command instead of remote => event => automation trigger => Apple TV command

antifuchs commented 7 months ago

Good idea! In the meantime, I actually stopped using this (it did have some annoying latency when the remote went to sleep, even though it didn't show the red "internal server error" banner anymore); instead, I disable remote sleep while the appletv-using activity is running. That reduces latency to near-zero and hasn't shown me a single error yet.

albaintor commented 7 months ago

Hi,

just some thoughts for @zehnm after I checked after the code and possibilities of Home assistant : to say it shortly, we are kind of stuck because of Home assistant limitations. Media player entities from assistant are very limited : a few playback commands, the ability to select a source or a sound mode and that is pretty much. I raised a feature request for this as I don't understand why HA does not propose additional (common) features : arrow keys and OK, back, (context) menu, number digits, and alpha keys Also they chose to separate media player and remote entities which has not many sense as this is same device most of the time, but I think this was designed for remotes with no media player skills. But even with this remote entity we are stuck because there are no predefined commands at all : just a custom command string as a parameter which depends on the device. So there is no possibility at this state to map the RC2 commands to the remote entity. The only possibility is to design a custom text command field on the RC2 side to be transmitted to the HA remote entity when sending commands.

Also @zehnm I saw in Rust code of HA integration that you use the get states request to retrieve all the entities, am I right ? I didn't find any other requests to do this job, which is problematic because this request does not return the link between entities : for ex usually a media player entity is linked to the remote entity through the same device or integration. But none of these 2 links are returned

zehnm commented 6 months ago

Work has been started in branch feat/remote-entity. It doesn't work yet with the current core version. A new simulator- and beta-version will be released in the following days. Only tested with the Android Remote Tv so far, but already works pretty well.

The remote entities provided by Remote Two integrations can be used in activities and macros, but won't be exposed in the web-configurator "Remotes" sections yet. This will follow later, maybe only in the new v2 version.

The HA remote-entity is indeed very limited. I was also surprised that there's no way to retrieve the available commands, not even if they are all defined in the HA configuration file (e.g. the Global Cache integration). An enhancement is proposed here: https://github.com/home-assistant/architecture/discussions/875 Please vote it up 👍

zehnm commented 6 months ago

just having a textbox or something where we could just copy-paste the supported commands from the Home Assistant documentation would be a great help.

With the remote-entity restrictions in HA, there won't be a list of available commands exposed in simple_commands. Only the on, off, toggle and generic send commands are available. The single HA send_command is split into a "Send command" and "Command sequence" for easier handling in the web-configurator. (More infos in https://github.com/unfoldedcircle/core-api/pull/44).

This is how it's going to look like with an Android TV Remote entity:

image

The command parameter names are still missing in the web-configurator. This will be fixed in a future update.

For the Android TV Remote entity, at least "all" keycodes can be sent (supported keycodes in Android TV is yet another sad issue though).

I saw in Rust code of HA integration that you use the get states request to retrieve all the entities, am I right ? I didn't find any other requests to do this job, which is problematic because this request does not return the link between entities

The HA WebSocket API isn't that sophisticated or at least not that well documented. If I'm not mistaken, there are more calls possible which are not documented. Or it's just a feature with the generic call_service and one has to figure out how it works 😄

One hacky way would be to search related devices based on the entity-id suffix and replace the prefix with media_player. For some, it would be straight forward:

image

Others require a wider search filter (media_player.office_tv is associated to the Google Cast integration...)

image

With some luck it's possible to get the related entities with the WS API, but that will require some digging.

Well, just one more reason to replace the current HA integration with a "Remote Two integration for Home Assistant" to have full configuration access and to reduce the (very) talkative get_states function & event subscriptions.

zehnm commented 6 months ago

One step closer in linking entities of the same device:

  1. Retrieve data from entity_id with config/entity_registry/get
    {
    "type": "config/entity_registry/get",
    "entity_id": "remote.office_tv",
    "id": 10
    }
  2. Read device_id
  3. Search related entities with device_id. This seems to require reading the full list with config/entity_registry/list :-( So we could already use it for step 1 and cache it.
  4. Do some other magic based on platform value, e.g. expose Android TV Remote keycodes as simple commands for "androidtv_remote" or known Apple TV commands for "apple_tv". Ugly and sad having to do that, but would provide a much better user experience. Even better: merge it into the exposed media-player entity as simple commands, then we'd have keypad navigation and other features right in the Remote Two media player...
  5. Profit!

So this would be possible, just with a bit of overhead due to missing filter options and having to deal with pretty big message responses. Going forward with a "Remote Two integration for Home Assistant" would be the better option.

zehnm commented 6 months ago

Part of beta firmware release 1.7.10

zehnm commented 6 months ago

Another creative way of getting the related entities of the same device: execute a template with the REST API:

curl --location 'http://homeassistant.local:8123/api/template' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer $TOKEN \
--data '{"template": "{{device_entities(device_id('\''remote.office_tv'\''))}}"}'

And back we get some non-json compatible data:

['media_player.office_tv_2', 'remote.office_tv']

But it's fragile: if the entity-id doesn't exist, the template returns some completely unrelated entities (likely the ones without a device association).

A well-typed API would be so much nicer...

albaintor commented 6 months ago

Good catch, but indeed the websocket API is poor on hass and would need more APIs