Skip to content

Commit

Permalink
Fix services (#36)
Browse files Browse the repository at this point in the history
* Update services.yaml

* Fix the services

* Update media_player.py

* Update media_player.py

* Update media_player.py

* Update media_player.py

* Update services.yaml
  • Loading branch information
gerard33 authored May 25, 2020
1 parent e040910 commit fb7da25
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
46 changes: 38 additions & 8 deletions custom_components/braviatv_psk/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
)
try:
from homeassistant.components.media_player.const import (
DOMAIN,
SUPPORT_NEXT_TRACK,
SUPPORT_PAUSE,
SUPPORT_PREVIOUS_TRACK,
Expand All @@ -43,7 +42,6 @@
)
except ImportError:
from homeassistant.components.media_player import (
DOMAIN,
SUPPORT_NEXT_TRACK,
SUPPORT_PAUSE,
SUPPORT_PREVIOUS_TRACK,
Expand All @@ -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
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
21 changes: 18 additions & 3 deletions custom_components/braviatv_psk/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fb7da25

Please sign in to comment.