Skip to content

Commit

Permalink
Add 'adb_response' attribute to Android TV / Fire TV (home-assistant#…
Browse files Browse the repository at this point in the history
…23960)

* Add 'adb_response' attribute to Android TV / Fire TV

* Use None instead of empty string for empty ADB responses

* Initialize self._adb_response as None, not empty string

* Update the state after sending an ADB command

This ensures that the `'adb_response'` attribute contains the response to the latest command
  • Loading branch information
JeffLIrion authored and alandtse committed Aug 6, 2019
1 parent ac462cb commit 92a5a85
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions homeassistant/components/androidtv/media_player.py
Expand Up @@ -157,10 +157,10 @@ def service_adb_command(service):
for target_device in target_devices:
output = target_device.adb_command(cmd)

# log the output if there is any
if output and (not isinstance(output, str) or output.strip()):
# log the output, if there is any
if output:
_LOGGER.info("Output of command '%s' from '%s': %s",
cmd, target_device.entity_id, repr(output))
cmd, target_device.entity_id, output)

hass.services.register(ANDROIDTV_DOMAIN, SERVICE_ADB_COMMAND,
service_adb_command,
Expand Down Expand Up @@ -225,6 +225,7 @@ def __init__(self, aftv, name, apps, turn_on_command,
self.exceptions = (ConnectionResetError, RuntimeError)

# Property attributes
self._adb_response = None
self._available = self.aftv.available
self._current_app = None
self._state = None
Expand All @@ -244,6 +245,11 @@ def available(self):
"""Return whether or not the ADB connection is valid."""
return self._available

@property
def device_state_attributes(self):
"""Provide the last ADB command's response as an attribute."""
return {'adb_response': self._adb_response}

@property
def name(self):
"""Return the device name."""
Expand Down Expand Up @@ -305,12 +311,24 @@ def adb_command(self, cmd):
"""Send an ADB command to an Android TV / Fire TV device."""
key = self._keys.get(cmd)
if key:
return self.aftv.adb_shell('input keyevent {}'.format(key))
self.aftv.adb_shell('input keyevent {}'.format(key))
self._adb_response = None
self.schedule_update_ha_state()
return

if cmd == 'GET_PROPERTIES':
return self.aftv.get_properties_dict()
self._adb_response = str(self.aftv.get_properties_dict())
self.schedule_update_ha_state()
return self._adb_response

response = self.aftv.adb_shell(cmd)
if isinstance(response, str) and response.strip():
self._adb_response = response.strip()
else:
self._adb_response = None

return self.aftv.adb_shell(cmd)
self.schedule_update_ha_state()
return self._adb_response


class AndroidTVDevice(ADBDevice):
Expand Down

0 comments on commit 92a5a85

Please sign in to comment.