Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix services #36

Merged
merged 7 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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