diff --git a/custom_components/braviatv_psk/media_player.py b/custom_components/braviatv_psk/media_player.py index e6e6d06..df4d0e4 100644 --- a/custom_components/braviatv_psk/media_player.py +++ b/custom_components/braviatv_psk/media_player.py @@ -26,7 +26,6 @@ ) try: from homeassistant.components.media_player.const import ( - DOMAIN, SUPPORT_NEXT_TRACK, SUPPORT_PAUSE, SUPPORT_PREVIOUS_TRACK, @@ -43,7 +42,6 @@ ) except ImportError: from homeassistant.components.media_player import ( - DOMAIN, SUPPORT_NEXT_TRACK, SUPPORT_PAUSE, SUPPORT_PREVIOUS_TRACK, @@ -59,10 +57,12 @@ MEDIA_TYPE_TVSHOW, ) -__version__ = "0.3.4" +__version__ = "0.3.5" _LOGGER = logging.getLogger(__name__) +DOMAIN = "braviatv_psk" + SUPPORT_BRAVIA = ( SUPPORT_PAUSE | SUPPORT_VOLUME_STEP @@ -158,16 +158,22 @@ ) SERVICE_BRAVIA_COMMAND = "bravia_command" +SERVICE_BRAVIA_OPEN_APP = "bravia_open_app" ATTR_COMMAND_ID = "command_id" +ATTR_URI = "uri" BRAVIA_COMMAND_SCHEMA = vol.Schema( { - vol.Optional(ATTR_ENTITY_ID): cv.entity_id, + vol.Required(ATTR_ENTITY_ID): cv.entity_id, vol.Required(ATTR_COMMAND_ID): cv.string, } ) +BRAVIA_OPEN_APP_SCHEMA = vol.Schema( + {vol.Required(ATTR_ENTITY_ID): cv.entity_id, vol.Required(ATTR_URI): cv.string} +) + # pylint: disable=unused-argument @@ -207,13 +213,26 @@ def setup_platform(hass, config, add_devices, discovery_info=None): ) add_devices([device]) + def send_command(call): + """Send command to TV.""" + command_id = call.data.get(ATTR_COMMAND_ID) + device.send_command(command_id) + + def open_app(call): + """Open app on TV.""" + uri = call.data.get(ATTR_URI) + device.open_app(uri) + hass.services.register( - DOMAIN, - SERVICE_BRAVIA_COMMAND, - lambda service: device.send_command(service.data[ATTR_COMMAND_ID]), - schema=BRAVIA_COMMAND_SCHEMA, + DOMAIN, SERVICE_BRAVIA_COMMAND, send_command, schema=BRAVIA_COMMAND_SCHEMA ) + # Only add the open_app service when TV is Android + if android: + hass.services.register( + DOMAIN, SERVICE_BRAVIA_OPEN_APP, open_app, schema=BRAVIA_OPEN_APP_SCHEMA + ) + class BraviaTVEntity(MediaPlayerEntity): """Representation of a Sony Bravia TV.""" @@ -305,6 +324,9 @@ def update(self): elif self._program_name == TV_WAIT: # TV is starting up, takes some time before it responds _LOGGER.info("TV is starting, no info available yet") + # elif power_status == "standby": + # self._refresh_channels() + # self._state = STATE_OFF else: self._state = STATE_OFF @@ -568,8 +590,16 @@ def play_media(self, media_type, media_id, **kwargs): def send_command(self, command_id): """Send arbitrary command to TV via HA service.""" + if self._state == STATE_OFF: + return self._braviarc.send_command(command_id) + def open_app(self, uri): + """Open app with given uri.""" + if self._state == STATE_OFF: + return + self._braviarc.open_app(uri) + def _convert_title_to_label(self, title): return_value = title if self._user_labels: diff --git a/custom_components/braviatv_psk/services.yaml b/custom_components/braviatv_psk/services.yaml index f957413..4cb8ef7 100644 --- a/custom_components/braviatv_psk/services.yaml +++ b/custom_components/braviatv_psk/services.yaml @@ -3,7 +3,22 @@ bravia_command: fields: entity_id: description: Name(s) of entities to turn on. - example: 'media_player.bravia_tv' + example: media_player.sony_bravia_tv command_id: - description: The ID of the command to send to TV. - example: 'ChannelUp' + description: > + The ID of the command to send to TV. + Check the Github page of this component for the list with command_ids. + example: ChannelUp +bravia_open_app: + description: Opens an app on Bravia TV. + fields: + entity_id: + description: Name(s) of entities to turn on. + example: media_player.sony_bravia_tv + uri: + description: > + The uri of the app to open on the TV. + Check the Github page of this component for a list with uris of apps. + The example below is for Youtube. + example: > + com.sony.dtv.com.google.android.youtube.tv.com.google.android.apps.youtube.tv.activity.ShellActivity