183 lines
5.2 KiB
Python
183 lines
5.2 KiB
Python
"""
|
|
Support for LG TV from 2011
|
|
"""
|
|
|
|
from datetime import timedelta
|
|
import logging
|
|
|
|
from requests import RequestException
|
|
import voluptuous as vol
|
|
|
|
from homeassistant import util
|
|
from homeassistant.components.media_player import PLATFORM_SCHEMA, MediaPlayerEntity
|
|
from homeassistant.components.media_player.const import (
|
|
MEDIA_TYPE_CHANNEL,
|
|
SUPPORT_NEXT_TRACK,
|
|
SUPPORT_PAUSE,
|
|
SUPPORT_PLAY,
|
|
SUPPORT_PREVIOUS_TRACK,
|
|
SUPPORT_SELECT_SOURCE,
|
|
SUPPORT_TURN_OFF,
|
|
SUPPORT_VOLUME_MUTE,
|
|
SUPPORT_VOLUME_STEP,
|
|
)
|
|
from homeassistant.const import (
|
|
CONF_ACCESS_TOKEN,
|
|
CONF_HOST,
|
|
CONF_NAME,
|
|
STATE_OFF,
|
|
STATE_PAUSED,
|
|
STATE_PLAYING,
|
|
STATE_UNKNOWN,
|
|
)
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
DEFAULT_NAME = 'LG TV Remote'
|
|
|
|
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(seconds=1)
|
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
|
|
|
SUPPORT_LGTV = SUPPORT_PAUSE | SUPPORT_VOLUME_STEP | \
|
|
SUPPORT_VOLUME_MUTE | SUPPORT_TURN_OFF | \
|
|
SUPPORT_SELECT_SOURCE | SUPPORT_PLAY
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
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,
|
|
})
|
|
|
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
|
"""Set up the LG TV platform."""
|
|
|
|
host = config.get(CONF_HOST)
|
|
access_token = config.get(CONF_ACCESS_TOKEN)
|
|
name = config.get(CONF_NAME)
|
|
|
|
|
|
add_entities([LgTVDevice(host, access_token, name)], True)
|
|
|
|
|
|
class LgTVDevice(MediaPlayerEntity):
|
|
"""Representation of a LG TV."""
|
|
|
|
def __init__(self, host, access_token, name):
|
|
"""Initialize the LG TV device."""
|
|
self._client = None
|
|
self._host = host
|
|
self._access_token = access_token
|
|
self._name = name
|
|
self._muted = False
|
|
# Assume that the TV is in Play mode
|
|
self._playing = True
|
|
self._state = STATE_PLAYING
|
|
self._volume = 0
|
|
self._source = STATE_UNKNOWN
|
|
self._sources = { "Antenna" : "ANTENNA", "USB1" : "USB1", "USB2" : "USB2", "HDMI1" : "HDMI1", "HDMI2" : "HDMI2", "HDMI3" : "HDMI3", "HDMI4" : "HDMI4", "AV1" : "AV1", "AV2" : "AV2", "AV3" : "AV3", "Component" : "COMPONENT" }
|
|
self.reconnect()
|
|
|
|
def reconnect(self):
|
|
from python_lgtv import lg
|
|
try:
|
|
self._client = lg.Remote(self._host, self._access_token)
|
|
self._state = STATE_PLAYING
|
|
except Exception as e:
|
|
pass
|
|
|
|
def send_command(self, command):
|
|
"""Send remote control commands to the TV."""
|
|
try:
|
|
self._client.send_command(command)
|
|
except Exception as e:
|
|
self._state = STATE_OFF
|
|
self.reconnect()
|
|
|
|
@util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
|
|
def update(self):
|
|
"""Retrieve the latest data from the LG TV."""
|
|
try:
|
|
self._client.get_session()
|
|
self._client.send_command(command)
|
|
except Exception as e:
|
|
self.reconnect()
|
|
#self._state = STATE_OFF
|
|
|
|
@property
|
|
def name(self):
|
|
"""Return the name of the device."""
|
|
return self._name
|
|
|
|
@property
|
|
def state(self):
|
|
"""Return the state of the device."""
|
|
return self._state
|
|
|
|
@property
|
|
def is_volume_muted(self):
|
|
"""Boolean if volume is currently muted."""
|
|
return self._muted
|
|
|
|
@property
|
|
def volume_level(self):
|
|
"""Volume level of the media player (0..1)."""
|
|
return self._volume / 100.0
|
|
|
|
@property
|
|
def source(self):
|
|
"""Return the current input source."""
|
|
return self._source
|
|
|
|
@property
|
|
def source_list(self):
|
|
"""List of available input sources."""
|
|
return list(self._sources.keys())
|
|
|
|
@property
|
|
def supported_features(self):
|
|
"""Flag media player features that are supported."""
|
|
return SUPPORT_LGTV
|
|
|
|
def turn_off(self):
|
|
"""Turn off media player."""
|
|
#self.send_command(1)
|
|
pass
|
|
|
|
def volume_up(self):
|
|
"""Volume up the media player."""
|
|
self.send_command(self._client.audio_controls["VOLUME_UP"])
|
|
|
|
def volume_down(self):
|
|
"""Volume down media player."""
|
|
self.send_command(self._client.audio_controls["VOLUME_DOWN"])
|
|
|
|
def mute_volume(self, mute):
|
|
"""Send mute command."""
|
|
self.send_command(self._client.audio_controls["MUTE"])
|
|
|
|
def select_source(self, source):
|
|
"""Select input source."""
|
|
self._source = source
|
|
self.send_command(self._client.input_controls[source])
|
|
|
|
def media_play_pause(self):
|
|
"""Simulate play pause media player."""
|
|
if self._playing:
|
|
self.media_pause()
|
|
else:
|
|
self.media_play()
|
|
|
|
def media_play(self):
|
|
"""Send play command."""
|
|
self._playing = True
|
|
self._state = STATE_PLAYING
|
|
self.send_command(self._client.playback_controls["PLAY"])
|
|
|
|
def media_pause(self):
|
|
"""Send media pause command to media player."""
|
|
self._playing = False
|
|
self._state = STATE_PAUSED
|
|
self.send_command(self._client.playback_controls["PAUSE"])
|