<s> """<STR_LIT>""" <EOL> import logging <EOL> from datetime import timedelta <EOL> from urllib . parse import urlparse <EOL> import homeassistant . util as util <EOL> from homeassistant . components . media_player import ( <EOL> SUPPORT_NEXT_TRACK , SUPPORT_PAUSE , SUPPORT_PREVIOUS_TRACK , <EOL> SUPPORT_TURN_OFF , SUPPORT_VOLUME_MUTE , SUPPORT_VOLUME_STEP , <EOL> SUPPORT_SELECT_SOURCE , SUPPORT_PLAY_MEDIA , MEDIA_TYPE_CHANNEL , <EOL> MediaPlayerDevice ) <EOL> from homeassistant . const import ( <EOL> CONF_HOST , STATE_OFF , STATE_PLAYING , STATE_PAUSED , STATE_UNKNOWN ) <EOL> from homeassistant . loader import get_component <EOL> _CONFIGURING = { } <EOL> _LOGGER = logging . getLogger ( __name__ ) <EOL> REQUIREMENTS = [ '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ] <EOL> SUPPORT_WEBOSTV = SUPPORT_PAUSE | SUPPORT_VOLUME_STEP | SUPPORT_VOLUME_MUTE | SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | SUPPORT_TURN_OFF | SUPPORT_SELECT_SOURCE | SUPPORT_PLAY_MEDIA <EOL> MIN_TIME_BETWEEN_SCANS = timedelta ( seconds = <NUM_LIT:10> ) <EOL> MIN_TIME_BETWEEN_FORCED_SCANS = timedelta ( seconds = <NUM_LIT:1> ) <EOL> def setup_platform ( hass , config , add_devices , discovery_info = None ) : <EOL> """<STR_LIT>""" <EOL> if discovery_info is not None : <EOL> host = urlparse ( discovery_info [ <NUM_LIT:1> ] ) . hostname <EOL> else : <EOL> host = config . get ( CONF_HOST , None ) <EOL> if host is None : <EOL> _LOGGER . error ( '<STR_LIT>' ) <EOL> return False <EOL> if host in _CONFIGURING : <EOL> return <EOL> setup_tv ( host , hass , add_devices ) <EOL> def setup_tv ( host , hass , add_devices ) : <EOL> """<STR_LIT>""" <EOL> from pylgtv import WebOsClient <EOL> from pylgtv import PyLGTVPairException <EOL> client = WebOsClient ( host ) <EOL> if not client . is_registered ( ) : <EOL> if host in _CONFIGURING : <EOL> try : <EOL> client . register ( ) <EOL> except PyLGTVPairException : <EOL> _LOGGER . warning ( <EOL> '<STR_LIT>' , host ) <EOL> return <EOL> except OSError : <EOL> _LOGGER . error ( '<STR_LIT>' , host ) <EOL> return <EOL> else : <EOL> _LOGGER . warning ( '<STR_LIT>' , host ) <EOL> request_configuration ( host , hass , add_devices ) <EOL> return <EOL> if client . is_registered ( ) and host in _CONFIGURING : <EOL> request_id = _CONFIGURING . pop ( host ) <EOL> configurator = get_component ( '<STR_LIT>' ) <EOL> configurator . request_done ( request_id ) <EOL> add_devices ( [ LgWebOSDevice ( host ) ] ) <EOL> def request_configuration ( host , hass , add_devices ) : <EOL> """<STR_LIT>""" <EOL> configurator = get_component ( '<STR_LIT>' ) <EOL> if host in _CONFIGURING : <EOL> configurator . notify_errors ( <EOL> _CONFIGURING [ host ] , '<STR_LIT>' ) <EOL> return <EOL> def lgtv_configuration_callback ( data ) : <EOL> """<STR_LIT>""" <EOL> setup_tv ( host , hass , add_devices ) <EOL> _CONFIGURING [ host ] = configurator . request_config ( <EOL> hass , '<STR_LIT>' , lgtv_configuration_callback , <EOL> description = '<STR_LIT>' , <EOL> description_image = '<STR_LIT>' , <EOL> submit_caption = '<STR_LIT>' <EOL> ) <EOL> class LgWebOSDevice ( MediaPlayerDevice ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host ) : <EOL> """<STR_LIT>""" <EOL> from pylgtv import WebOsClient <EOL> self . _client = WebOsClient ( host ) <EOL> self . _name = '<STR_LIT>' <EOL> self . _muted = False <EOL> self . _playing = True <EOL> self . _volume = <NUM_LIT:0> <EOL> self . _current_source = None <EOL> self . _current_source_id = None <EOL> self . _source_list = None <EOL> self . _source_label_list = None <EOL> self . _state = STATE_UNKNOWN <EOL> self . _app_list = None <EOL> self . update ( ) <EOL> @ util . Throttle ( MIN_TIME_BETWEEN_SCANS , MIN_TIME_BETWEEN_FORCED_SCANS ) <EOL> def update ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . _state = STATE_PLAYING <EOL> self . _muted = self . _client . get_muted ( ) <EOL> self . _volume = self . _client . get_volume ( ) <EOL> self . _current_source_id = self . _client . get_input ( ) <EOL> self . _source_list = { } <EOL> self . _source_label_list = [ ] <EOL> self . _app_list = { } <EOL> for app in self . _client . get_apps ( ) : <EOL> self . _app_list [ app [ '<STR_LIT:id>' ] ] = app <EOL> for source in self . _client . get_inputs ( ) : <EOL> self . _source_list [ source [ '<STR_LIT:label>' ] ] = source <EOL> self . _app_list [ source [ '<STR_LIT>' ] ] = source <EOL> self . _source_label_list . append ( source [ '<STR_LIT:label>' ] ) <EOL> if source [ '<STR_LIT>' ] == self . _current_source_id : <EOL> self . _current_source = source [ '<STR_LIT:label>' ] <EOL> except OSError : <EOL> self . _state = STATE_OFF <EOL> @ property <EOL> def name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _name <EOL> @ property <EOL> def state ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _state <EOL> @ property <EOL> def is_volume_muted ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _muted <EOL> @ property <EOL> def volume_level ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _volume / <NUM_LIT> <EOL> @ property <EOL> def source ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _current_source <EOL> @ property <EOL> def source_list ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _source_label_list <EOL> @ property <EOL> def media_content_type ( self ) : <EOL> """<STR_LIT>""" <EOL> return MEDIA_TYPE_CHANNEL <EOL> @ property <EOL> def media_image_url ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _app_list [ self . _current_source_id ] [ '<STR_LIT>' ] <EOL> @ property <EOL> def supported_media_commands ( self ) : <EOL> """<STR_LIT>""" <EOL> return SUPPORT_WEBOSTV <EOL> def turn_off ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _state = STATE_OFF <EOL> self . _client . power_off ( ) <EOL> def volume_up ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _client . volume_up ( ) <EOL> def volume_down ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _client . volume_down ( ) <EOL> def set_volume_level ( self , volume ) : <EOL> """<STR_LIT>""" <EOL> tv_volume = volume * <NUM_LIT:100> <EOL> self . _client . set_volume ( tv_volume ) <EOL> def mute_volume ( self , mute ) : <EOL> """<STR_LIT>""" <EOL> self . _muted = mute <EOL> self . _client . set_mute ( mute ) <EOL> def media_play_pause ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _playing : <EOL> self . media_pause ( ) <EOL> else : <EOL> self . media_play ( ) <EOL> def select_source ( self , source ) : <EOL> """<STR_LIT>""" <EOL> self . _current_source_id = self . _source_list [ source ] [ '<STR_LIT>' ] <EOL> self . _current_source = self . _source_list [ source ] [ '<STR_LIT:label>' ] <EOL> self . _client . set_input ( self . _source_list [ source ] [ '<STR_LIT:id>' ] ) <EOL> def media_play ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _playing = True <EOL> self . _state = STATE_PLAYING <EOL> self . _client . play ( ) <EOL> def media_pause ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _playing = False <EOL> self . _state = STATE_PAUSED <EOL> self . _client . pause ( ) <EOL> def media_next_track ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _client . fast_forward ( ) <EOL> def media_previous_track ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _client . rewind ( ) </s>
