JeffLIrion / homeassistant_native_firetv

43 stars 16 forks source link

Power on Fire TV Cube #21

Closed noahwt closed 5 years ago

noahwt commented 5 years ago

Is there a way to issue the command in Hassio to turn on the Fire TV Cube - essentially as if the power button on the fire tv remote was pressed? That way it would also turn on/off my TV and Soundbar.

Currently, if I press the Power button for Fire TV in Hassio -> it will stop playing the video and turn the screen blank. My TV and Soundbar remains powered on.

I am still new to Hassio so any help is much appreciated!

JeffLIrion commented 5 years ago

I don't have a Fire TV Cube, but it sounds like the power function is doing what it's supposed to -- turning the Fire TV device on/off. I don't know if you can replicate the full behavior if the Fire TV Cube, but if you can connect the TV and sound at to Home Assistant then you can automate turning them on and off.

JeffLIrion commented 5 years ago

You can use this version of the component and try sending various ADB commands to accomplish what you want. This version of the component registers a service called media_player.firetv_adb_shell. You can send key commands via:

{"entity_id": "media_player.fire_tv_living_room", "cmd": "input keyevent <KEY>"}

Here is a site that documents the various key commands: https://developer.android.com/reference/android/view/KeyEvent

JeffLIrion commented 5 years ago

According to this post, you can get the TV to power on by sending a Home command. The HA service data is:

{"entity_id": "media_player.fire_tv_living_room", "cmd": "input keyevent 3"}
noahwt commented 5 years ago

I'm not sure how he is using the Home key (keyevent 3) to power on the TV/Soundbar from the fire cube... sounds like he might be using a python script on the fire tv or he is relying on CEC instead of the Fire Cube IR blasters?

I was using logcat to try to figure out what was going on when I pressed the Power Button on the Fire TV remote (which turns on/off the TV and Soundbar via IR Blasters). I have not been successful in reproducing the commands from adb shell. Below is some of the log from a Power command on the remote. Any help is appreciated!

01-03 22:14:06.368 4183 24088 I OZ-DCS:AggregateCommandCallback: Start init started, counter is: 0 01-03 22:14:06.368 4183 24088 I OZ-DCS:AggregateCommandCallback: Start init finished, counter is: 1 01-03 22:14:06.369 4183 24088 I OZ-DCS:StatePropagator: StateFeedUpdate : TV, function : POWER_TOGGLE, [key : null, value: null, backup value: null], executeAlways: false, alreadyInState: false, skip command : false, skipIfUnknown: false, skipButUpdateState: false, overrideState true 01-03 22:14:06.369 4183 24088 I OZ-DCS:Device: Putting function to execute [TV]: POWER_TOGGLE 01-03 22:14:06.370 4183 4529 I OZ-DCS:IrProtocol: ....taking function to execute [IR] [-]: POWER_TOGGLE 01-03 22:14:06.370 4183 24088 I OZ-DCS:StatePropagator: StateFeedUpdate : SOUNDBAR, function : POWER_TOGGLE, [key : null, value: null, backup value: null], executeAlways: false, alreadyInState: false, skip command : false, skipIfUnknown: false, skipButUpdateState: false, overrideState true 01-03 22:14:06.371 4183 24088 I OZ-DCS:Device: Putting function to execute [SOUNDBAR]: POWER_TOGGLE 01-03 22:14:06.371 4183 24088 I OZ-DCS:AggregateCommandCallback: End init started, counter is: 3 01-03 22:14:06.371 4183 4532 I OZ-DCS:IrProtocol: ....taking function to execute [IR] [-]: POWER_TOGGLE 01-03 22:14:06.371 4183 24088 I OZ-DCS:AggregateCommandCallback: End init finished, counter is: 2 01-03 22:14:06.372 4183 24088 I OZ-DCS:KeyPressReceiver: Remote button pressed. keyCode: 26, keyEvent: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_POWER, scanCode=116, metaState=0, flags=0x8, repeatCount=0, eventTime=706980030, downTime=706980030, deviceId=27, source=0x301 } 01-03 22:14:06.377 4183 4529 I OZ-DCS:IrExecutorProxy: .... Submitting to blaster[IR] [Tv] : POWER_TOGGLE 01-03 22:14:06.377 4183 4529 I OZ-DCS:IrExecutorProxy: Customer Service:IR code was blasted towards Tv. Code: [447, 447, 52, 165, 52, 165, 52, 165, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 165, 52, 165, 52, 165, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 165, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 165, 52, 52, 52, 165, 52, 165, 52, 165, 52, 165, 52, 165, 52, 165, 52, 4728] 01-03 22:14:06.379 4183 4532 I OZ-DCS:IrExecutorProxy: .... Submitting to blaster[IR] [Soundbar] : POWER_TOGGLE 01-03 22:14:06.379 4183 4532 I OZ-DCS:IrExecutorProxy: Customer Service:IR code was blasted towards Soundbar. Code: [912, 457, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 170, 55, 170, 55, 170, 55, 170, 55, 170, 55, 170, 55, 170, 55, 170, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 170, 55, 55, 55, 170, 55, 170, 55, 170, 55, 170, 55, 170, 55, 170, 55, 55, 55, 170, 55, 4133, 912, 228, 55, 9750] 01-03 22:14:06.383 4183 4526 I OZ-DCS:ConsumerIrBlaster: --->>>>--->>>>--->>>>--->>>> BLASTING IR: 0000 006D 0000 0022 00AA 00AA 0014 003F 0014 003F 0014 003F 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 003F 0014 003F 0014 003F 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 003F 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 0014 003F 0014 0014 0014 003F 0014 003F 0014 003F 0014 003F 0014 003F 0014 003F 0014 0706 01-03 22:14:06.383 4183 4526 I OZ-DCS:ConsumerIrBlaster: <<<< SUBMITTING IR BEGIN >>>> timestamp [1546575246383] freq: 38028 code: [447, 447, 52, 165, 52, 165, 52, 165, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 165, 52, 165, 52, 165, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 165, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 165, 52, 52, 52, 165, 52, 165, 52, 165, 52, 165, 52, 165, 52, 165, 52, 4728] 01-03 22:14:06.383 4183 4526 I OZ-MET:Metric: #### Record Metric:Counter metric 01-03 22:14:06.383 4183 4526 I OZ-MET:Metric: Duration:0 01-03 22:14:06.383 4183 4526 I OZ-MET:Metric: Metric Name:blast_without_ty 01-03 22:14:06.383 4183 4526 I OZ-MET:Metric: Source Name:Tyr 01-03 22:14:06.384 4183 4526 I OZ-MET:Metric: Counter:1 01-03 22:14:06.385 3612 4202 I ConsumerIrHal: keycode is 447s447s52s165s52s165s52s165s52s52s52s52s52s52s52s52s52s52s52s165s52s165s52s165s52s52s52s52s52s52s52s52s52s52s52s52s52s165s52s52s52s52s52s52s52s52s52s52s52s52s52s165s52s52s52s165s52s165s52s165s52s165s52s165s52s165s52s4728s 01-03 22:14:06.411 3740 4053 E bt_btif : bta_hh_le_input_rpt_notify uuid=0x2a4d srv_handle=79 is_bl_svc_primary=1 01-03 22:14:06.412 4183 24089 I OZ-DCS:KeyPressReceiver: Received keyCode: KEYCODE_POWER 01-03 22:14:06.413 4183 24089 I OZ-DCS:KeyPressReceiver: Remote button pressed. keyCode: 26, keyEvent: KeyEvent { action=ACTION_UP, keyCode=KEYCODE_POWER, scanCode=116, metaState=0, flags=0x8, repeatCount=0, eventTime=706980090, downTime=706980030, deviceId=27, source=0x301 } 01-03 22:14:06.490 4183 4526 I OZ-DCS:ConsumerIrBlaster: <<<< SUBMITTING IR END >>>> timestamp [1546575246489] 01-03 22:14:06.495 4183 4526 I OZ-DCS:ConsumerIrBlaster: --->>>>--->>>>--->>>>--->>>> BLASTING IR: 0000 006D 0022 0002 015B 00AE 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0015 0041 0015 0015 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0041 0015 0015 0015 0041 0015 0624 015B 0057 0015 0E7C 01-03 22:14:06.498 4183 4526 I OZ-DCS:ConsumerIrBlaster: <<<< SUBMITTING IR BEGIN >>>> timestamp [1546575246496] freq: 38028 code: [912, 457, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 170, 55, 170, 55, 170, 55, 170, 55, 170, 55, 170, 55, 170, 55, 170, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 170, 55, 55, 55, 170, 55, 170, 55, 170, 55, 170, 55, 170, 55, 170, 55, 55, 55, 170, 55, 4133, 912, 228, 55, 9750] 01-03 22:14:06.498 4183 4526 I OZ-MET:Metric: #### Record Metric:Counter metric 01-03 22:14:06.498 4183 4526 I OZ-MET:Metric: Duration:0 01-03 22:14:06.498 4183 4526 I OZ-MET:Metric: Metric Name:blast_without_ty 01-03 22:14:06.498 4183 4526 I OZ-MET:Metric: Source Name:Tyr 01-03 22:14:06.498 4183 4526 I OZ-MET:Metric: Counter:1 01-03 22:14:06.499 3612 3622 I ConsumerIrHal: keycode is 912s457s55s55s55s55s55s55s55s55s55s55s55s55s55s55s55s55s55s170s55s170s55s170s55s170s55s170s55s170s55s170s55s170s55s55s55s55s55s55s55s55s55s55s55s55s55s170s55s55s55s170s55s170s55s170s55s170s55s170s55s170s55s55s55s170s55s4133s912s228s55s9750s 01-03 22:14:06.717 4183 4526 I OZ-DCS:ConsumerIrBlaster: <<<< SUBMITTING IR END >>>> timestamp [1546575246716] 01-03 22:14:06.879 4183 4532 I OZ-DCS:AggregateCommandCallback: [AggregateCommandCallback@122771167][ApiCallback@null] Aggregated commands are executed; callback is null called 511 milliseconds after creation 01-03 22:14:07.071 3263 3379 I AUDIOALG: asyncProcess : process @@@ 01-03 22:14:07.075 3263 3379 I AUDIOALG: RefDlyEstimation: maxRxy 0.000000 maxPos 0 01-03 22:14:07.075 3263 3379 I AUDIOALG: asyncProcess : waiting... --------- beginning of system

Codydanieljones commented 5 years ago

I made a custom component to send the home keys for power on. Below is a code snippet from this. I added a use_home config variable you set in the configuration.yaml. When the Turn on function is called in the custom component it checks the value of use_home and either sends the turn on command or the turn on and home command via adb.


        """Turn on the device."""
        if self._use_home:
            self.firetv.turn_on()
            self.firetv.home()
        else:
            self.firetv.turn_on()
        if self._set_states:
            self._state = STATE_IDLE
Codydanieljones commented 5 years ago

@noahwt Yes this rely's on HDMI CEC. I do not have a Cube with blasters.

noahwt commented 5 years ago

Thanks @Codydanieljones

Unfortunately, I do not have CEC available to use and must rely on the IR features to turn on/off the TV and Soundbar.

Codydanieljones commented 5 years ago

@noahwt I understand. I have one TV that does not support CEC. I did a little more research. And per the Amazon Developer Page, the Key Events for the Power and Vol on the newer Fire TV remotes cannot be intercepted by third-party apps. And I have not been able to locate the adb key events for these keys.. You could possibly connect to the cube via ADB in CMD and run a getevent in the adb shell. Then on the remote press the keys to see if they trigger a key event in the ADB Console. https://source.android.com/devices/input/getevent

These keys may only be accessible via the FireOS Kernal though leaving it impossible to do this over ADB.

This just came to mind. Not sure if this is possible, as I do not know the complete functionality of the Cube, but if you can control the TV/Soundbar via Alexa Voice Commands. If there is a way to trigger Alexa Commands from HA without speaking to Alexa directly. These scripts could be set up in HA to power on your devices. I am not sure if that functionality exists though.....

The last option would be is a third party app was developed that would run a web API on the Fire Cube. This app would have access to the IR Blaster and could be configured for your devices. Similar too older Samsung phones that had IR Blasters in them. HA would send an HTTP request to the background running an app on the fire tv to trigger the IR command. This is obviously the most advanced solution if nothing else works.

JeffLIrion commented 5 years ago

@Codydanieljones thanks for looking into this!

@noahwt, if it's not possible to do this with your Fire TV device, I would recommend a Broadlink RM Pro. If you don't need the RF functionality, I think you can buy a different brand/model and save a few bucks. I really like my RM Pro!

noahwt commented 5 years ago

Thanks @Codydanieljones I used getevent as you suggested, but I can't figure out how to translate it to adb commands (if possible). If you have any suggestions please let me know. I did the following with no success: adb shell input keyevent KEYCODE_POWER adb shell input keyevent KEY_POWER adb shell input keyevent KEYCODE_VOLUMEUP adb shell input keyevent KEY_VOLUMEUP

Side note - it does turn on/off with Alexa the way I need it to. However, I don't think it is possible to trigger an Alexa command without speaking to Alexa... if you know of a way then please let me know!

Key Commands: volume up, volume down, power off, power on

[ 280624.256010] EV_MSC MSC_SCAN 000c00e9 [ 280624.256010] EV_KEY KEY_VOLUMEUP DOWN [ 280624.256010] EV_SYN SYN_REPORT 00000000 [ 280624.395861] EV_MSC MSC_SCAN 000c00e9 [ 280624.395861] EV_KEY KEY_VOLUMEUP UP [ 280624.395861] EV_SYN SYN_REPORT 00000000 [ 280625.015892] EV_MSC MSC_SCAN 000c00ea [ 280625.015892] EV_KEY KEY_VOLUMEDOWN DOWN [ 280625.015892] EV_SYN SYN_REPORT 00000000 [ 280625.176285] EV_MSC MSC_SCAN 000c00ea [ 280625.176285] EV_KEY KEY_VOLUMEDOWN UP [ 280625.176285] EV_SYN SYN_REPORT 00000000 [ 280633.776172] EV_MSC MSC_SCAN 00070066 [ 280633.776172] EV_KEY KEY_POWER DOWN [ 280633.776172] EV_SYN SYN_REPORT 00000000 [ 280633.836075] EV_MSC MSC_SCAN 00070066 [ 280633.836075] EV_KEY KEY_POWER UP [ 280633.836075] EV_SYN SYN_REPORT 00000000 [ 280638.676108] EV_MSC MSC_SCAN 00070066 [ 280638.676108] EV_KEY KEY_POWER DOWN [ 280638.676108] EV_SYN SYN_REPORT 00000000 [ 280638.776156] EV_MSC MSC_SCAN 00070066 [ 280638.776156] EV_KEY KEY_POWER UP [ 280638.776156] EV_SYN SYN_REPORT 00000000

Right and Left Dpad below just for comparison:

[ 280615.816448] EV_MSC MSC_SCAN 0007004f [ 280615.816448] EV_KEY KEY_RIGHT UP [ 280615.816448] EV_SYN SYN_REPORT 00000000 [ 280617.358028] EV_MSC MSC_SCAN 00070050 [ 280617.358028] EV_KEY KEY_LEFT DOWN [ 280617.358028] EV_SYN SYN_REPORT 00000000

@JeffLIrion Thanks for your help as well! If this doesn't work then I may have to use an IR Hub just for the TV/Soundbar Power, but I would really love to get this all integrated with as few systems as possible since the Fire Cube should be capable of doing this.

Codydanieljones commented 5 years ago

@noahwt https://community.home-assistant.io/t/send-commands-to-alexa/75301

Check this out. I havent personally tried it. Just searched and found it. It may be able to send the alexa command to power on tv/soundbar with out using voice.