Hello X2 users,
The official X2 integration enables communication in both directions:
- The X2 hub can trigger automations in Home Assistant
- Home Assistant can trigger commands/macros/favorites on the X2 hub
The following explains how to perform the latter: trigger commands/macros/favorites on the X2 hub, from Home Assistant.
This guide assumes that the official integration is successfully installed and working.
Approach
When an X2 hub is successfully configured in the integration, it adds a remote entity to Home Assistant. It appears that the default name of this entity is remote.x2_hub.
- This entity implements Home Assistantâs remote platform, and is used for sending commands to the hub.
To trigger a command on the X2 hub from Home Assistant, the type, activity_id and the key_id must be known, so they can be used when we call the remote.send_command Action.
Retrieving type, activity_id and key_id
For now, the X2 integration does not make it entirely trivial to retrieve the codes needed.
Probably the easiest way currently is to enable debug logging for the integration, and then use the virtual remote UI card the integration adds to Home Assistant to click buttons/macros/favorites.
The log file then produces everything needed to replay those commands in custom UIs, automations and scripts.
- Enable debug logging for the Integration
- In Home Assistant:
Settings -> Devices & Services. Find theSofabaton Hubintegration and click it. - On the
Sofabaton Hubintegration page: in the top-right corner, click the 3-dots button. In the menu that appears: click Enable debug logging.
- In Home Assistant:
- Open the Home Assistant log file
- While still on the
Sofabaton Hubintegration page, click on your X2 HUB. This leads to your hubâs âdeviceâ page. This page contains switches for each of your Activities, as well as theremoteentity which also is called X2 HUB. - Open a second browser and open Home Assistant. You have Home Assistant open in 2 browsers at the same time now. In the second browser, go to:
Settings -> System -> Logs. In the top-right corner, click the 3-dots button. In the menu that appears, clickShow raw logs. - At the top of the screen, where it says âSearch Logsâ, enter precisely this:
[custom_components.sofabaton_hub.remote] command:
This will filter the log for any commands sent from the integrationâs virtual remote. This will likely be empty for now. Stuff will appear here soon.
- While still on the
- Use the integrationâs virtual remote to trigger commands on the hub
- In the first browser, that we still have open and on the hubâs device page: Switch an Activity ON with one of the provided switches. Wait a few seconds as the hub runs its Activity start macro.
- Click on X2 HUB (remote.x2_hub), which sits somewhere in that list with the Activity switches. Do NOT click on the switch next to âX2 HUBâ, click on the text itself. A popup will open containing the integrationâs virtual remote.
- Click a single button on the virtual remote (one that youâve actually mapped a command to)
- Go to browser 2, the one with the log. In the top-right of the screen, click the Refresh button. You will now see a log line appear that looks a lot like this:
command: ['type:send_assigned_key', 'activity_id:101', 'key_id:184']
Make note of this line, so that later on you remember what command this line triggers. Keep everything after âcommand:â, as we will needtype,activity_idandkey_idlater on.
Important: It may take up to 20 seconds for the Home Assistant log to actually update with the latest data. - Repeat this process for every command you want to capture.
Sending commands to the hub
Now that type, activity_id and key_id are logged for the required commands, the remote.send_command action can be used to trigger the command on the X2 hub.
-
Try it out
- In Home Assistant, go to:
Developer tools -> Actions. In the Action dropdown, findremote.send_command. - Click
Add targetand find X2 HUB (remote.x2_hub) and select it. - Uncheck all checkboxes: Repeats, Delay seconds, Hold seconds: uncheck them all.
- In the Command field, paste exactly the line we retrieved from the log, minus âcommand:â. So based on the example previously used, we would enter this under Command:
['type:send_assigned_key', 'activity_id:101', 'key_id:184'] - Click
Perform action
The hub will now execute the command linked to the codes we sent it.
- In Home Assistant, go to:
-
Use in an automation
What we did inDeveloper tools -> Actionsto try it out, can be directly applied in an automation or script. If for example we want to trigger a command whenever the office light is switched on, this would be the flow:- In Home Assistant, go to:
Settings -> Automations & Scenes. - Click
Create automation -> Create new automation - Click
Add trigger -> Entity -> State - In the panel on the right, in the Select an entity dropdown, select
light.office - In the same panel, in the From dropdown, select
Off - In the same panel, in the To dropdown, select
On
This completes the Trigger for this automation. Weâve instructed it to fire whenever light.office switches from Off to On. Now letâs configure the Action to perform when our Trigger fires.
- Back in the center of the page, click
Add action -> Remote -> Send command - In the panel on the right, youâll recognize the exact configuration UI as in
Developer tools -> Actions, when we tried out our commands. So Add target, uncheck the checkboxes and paste in the command to trigger when the office light is switched on. - Click
Saveand give the automation a name.
- In Home Assistant, go to:
The configured command will now be executed by the X2 hub whenever the office lights are switched on.
Useful information
You will find that many of the codes are predictable:
- The "key_id"s for the remoteâs hard buttons (volume up, channel down etc) are always the same, across Activities. This is how they are mapped:
# Directional keys and OK
"up": ["key_id": 174]
"down": ["key_id": 178]
"left": ["key_id": 175]
"right": ["key_id": 177]
"ok": ["key_id": 176]
# Function keys
"back": ["key_id": 179]
"home": ["key_id": 180]
"menu": ["key_id": 181]
# Volume and channel
"volume_up": ["key_id": 182]
"volume_down": ["key_id": 185]
"channel_up": ["key_id": 183]
"channel_down": ["key_id": 186]
"mute": ["key_id": 184]
"guide": ["key_id": 157]
# Media control
"rewind": ["key_id": 187]
"play": ["key_id": 156]
"fast_forward": ["key_id": 189]
"dvr": ["key_id": 155]
"pause": ["key_id": 188]
"exit": ["key_id": 154]
# Color keys
"red": ["key_id": 190]
"green": ["key_id": 191]
"yellow": ["key_id": 192]
"blue": ["key_id": 193]
# Custom keys
"a": ["key_id": 153]
"b": ["key_id": 152]
"c": ["key_id": 151]
- The âtypeâ is one of 3 options:
- send_assigned_key (the âhardâ buttons)
- send_macro_key (the macros)
- send_favorite_key (the favorites)
- The send_favorite_key type is actually an interesting one. It allows us to send any command directly to a âDeviceâ, at any time, regardless of the Activity that is running, or whether one is running at all.
In the Sofabaton hub, Devices and Activities actually exist within the same ID range. Devices start at 1 and Activities start at 101. Youâll note that when you capture âassigned_keysâ or âmacrosâ that the âactivity_idâ is always upwards from 101.
Favorites are different, the âactivity_idâ retrieved here is upwards from 1. What is shown as an activity_id is within the hub itself in reality the device_id.
So with that knowledge, if we retrieve the âactivity_idâ for our devices (in reality weâre looking for the device_id), we can use remote.send_command to directly fire any command, on any device, regardless of what else is going on with the X2 hub.