Skip to content
Merged
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
19 changes: 17 additions & 2 deletions homeassistant/components/lg_netcast/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
SUPPORT_PREVIOUS_TRACK,
SUPPORT_SELECT_SOURCE,
SUPPORT_TURN_OFF,
SUPPORT_TURN_ON,
SUPPORT_VOLUME_MUTE,
SUPPORT_VOLUME_STEP,
)
Expand All @@ -28,11 +29,14 @@
STATE_PLAYING,
)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.script import Script

_LOGGER = logging.getLogger(__name__)

DEFAULT_NAME = "LG TV Remote"

CONF_ON_ACTION = "turn_on_action"

MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(seconds=1)
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)

Expand All @@ -49,6 +53,7 @@

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_ON_ACTION): cv.SCRIPT_SCHEMA,
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_ACCESS_TOKEN): vol.All(cv.string, vol.Length(max=6)),
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
Expand All @@ -62,20 +67,23 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
host = config.get(CONF_HOST)
access_token = config.get(CONF_ACCESS_TOKEN)
name = config.get(CONF_NAME)
on_action = config.get(CONF_ON_ACTION)

client = LgNetCastClient(host, access_token)
on_action_script = Script(hass, on_action) if on_action else None

add_entities([LgTVDevice(client, name)], True)
add_entities([LgTVDevice(client, name, on_action_script)], True)


class LgTVDevice(MediaPlayerDevice):
"""Representation of a LG TV."""

def __init__(self, client, name):
def __init__(self, client, name, on_action_script):
"""Initialize the LG TV device."""
self._client = client
self._name = name
self._muted = False
self._on_action_script = on_action_script
# Assume that the TV is in Play mode
self._playing = True
self._volume = 0
Expand Down Expand Up @@ -180,6 +188,8 @@ def media_title(self):
@property
def supported_features(self):
"""Flag media player features that are supported."""
if self._on_action_script:
return SUPPORT_LGTV | SUPPORT_TURN_ON
return SUPPORT_LGTV

@property
Expand All @@ -191,6 +201,11 @@ def turn_off(self):
"""Turn off media player."""
self.send_command(1)

def turn_on(self):
"""Turn on the media player."""
if self._on_action_script:
self._on_action_script.run()

def volume_up(self):
"""Volume up the media player."""
self.send_command(24)
Expand Down