예제 #1
2
def get_service(hass, config, discovery_info=None):
    """Return the notify service."""
    from pylgtv import WebOsClient
    from pylgtv import PyLGTVPairException

    path = hass.config.path(config.get(CONF_FILENAME))
    client = WebOsClient(config.get(CONF_HOST), key_file_path=path)

    try:
        client.register()
    except PyLGTVPairException:
        _LOGGER.error("Pairing with TV failed")
        return None
    except OSError:
        _LOGGER.error("TV unreachable")
        return None

    return LgWebOSNotificationService(client)
예제 #2
0
def setup_tv(host, name, customize, hass, add_devices):
    """Setup a phue bridge based on host parameter."""
    from pylgtv import WebOsClient
    from pylgtv import PyLGTVPairException

    client = WebOsClient(host)

    if not client.is_registered():
        if host in _CONFIGURING:
            # Try to pair.
            try:
                client.register()
            except PyLGTVPairException:
                _LOGGER.warning(
                    "Connected to LG WebOS TV at %s but not paired", host)
                return
            except OSError:
                _LOGGER.error("Unable to connect to host %s", host)
                return
        else:
            # Not registered, request configuration.
            _LOGGER.warning('LG WebOS TV at %s needs to be paired.', host)
            request_configuration(host, name, customize, hass, add_devices)
            return

    # If we came here and configuring this host, mark as done.
    if client.is_registered() and host in _CONFIGURING:
        request_id = _CONFIGURING.pop(host)
        configurator = get_component('configurator')
        configurator.request_done(request_id)

    add_devices([LgWebOSDevice(host, name, customize)])
예제 #3
0
def get_service(hass, config):
    """Return the notify service."""
    if not validate_config({DOMAIN: config}, {DOMAIN: [CONF_HOST, CONF_NAME]},
                           _LOGGER):
        return None

    host = config.get(CONF_HOST, None)

    if not host:
        _LOGGER.error('No host provided.')
        return None

    from pylgtv import WebOsClient
    from pylgtv import PyLGTVPairException

    client = WebOsClient(host)

    try:
        client.register()
    except PyLGTVPairException:
        _LOGGER.error('Pairing failed.')
        return None
    except ConnectionRefusedError:
        _LOGGER.error('Host unreachable.')
        return None

    return LgWebOSNotificationService(client)
예제 #4
0
파일: webostv.py 프로젝트: lumavp/blumate
def get_service(hass, config):
    """Return the notify service."""
    if not validate_config({DOMAIN: config}, {DOMAIN: [CONF_HOST, CONF_NAME]},
                           _LOGGER):
        return None

    host = config.get(CONF_HOST, None)

    if not host:
        _LOGGER.error('No host provided.')
        return None

    from pylgtv import WebOsClient
    from pylgtv import PyLGTVPairException

    client = WebOsClient(host)

    try:
        client.register()
    except PyLGTVPairException:
        _LOGGER.error('Pairing failed.')
        return None
    except OSError:
        _LOGGER.error('Host unreachable.')
        return None

    return LgWebOSNotificationService(client)
예제 #5
0
def setup_tv(host, mac, name, customize, config, hass, add_devices):
    """Set up a LG WebOS TV based on host parameter."""
    from pylgtv import WebOsClient
    from pylgtv import PyLGTVPairException
    from websockets.exceptions import ConnectionClosed

    client = WebOsClient(host, config)

    if not client.is_registered():
        if host in _CONFIGURING:
            # Try to pair.
            try:
                client.register()
            except PyLGTVPairException:
                _LOGGER.warning(
                    "Connected to LG webOS TV %s but not paired", host)
                return
            except (OSError, ConnectionClosed, TypeError,
                    asyncio.TimeoutError):
                _LOGGER.error("Unable to connect to host %s", host)
                return
        else:
            # Not registered, request configuration.
            _LOGGER.warning("LG webOS TV %s needs to be paired", host)
            request_configuration(
                host, mac, name, customize, config, hass, add_devices)
            return

    # If we came here and configuring this host, mark as done.
    if client.is_registered() and host in _CONFIGURING:
        request_id = _CONFIGURING.pop(host)
        configurator = get_component('configurator')
        configurator.request_done(request_id)

    add_devices([LgWebOSDevice(host, mac, name, customize, config)], True)
예제 #6
0
파일: lg_tv.py 프로젝트: wnj00524/HomeHubv2
def run_app(tv_ip, app):
    try:
        webos_client = WebOsClient(tv_ip)
        webos_client.launch_app(app)
        return 0

    except:
        return -1
예제 #7
0
파일: lg_tv.py 프로젝트: wnj00524/HomeHubv2
def list_app(tv_ip):
    r = set()
    webos_client = WebOsClient(tv_ip)
    for app in webos_client.get_apps():
        print(app)
        #print(app['title'])
        r.add(app['title'])
        #for ap1 in r:
        #print(ap1)
        #print(r)
    return r
예제 #8
0
파일: martina.py 프로젝트: mnebot/Martina
    def __init__(self):

        # inicialitza el reconeixedor de veu i comença a escoltar
        aiy.i18n.set_language_code('ca-ES')
        self.recognizer = aiy.cloudspeech.get_recognizer()
        self.inicialitzaRecognizer()

        # inicialitza robot
        self.initResetMarty()

        # inicialitza la càmera
        self.camera = picamera.PiCamera()
        # redueix la resolució
        self.camera.resolution = (320, 240)
        print('Càmera inicialitzada')

        # inicialitza llums
        self.inicialitzaHue()

        # inicialitza la tele LG
        self.webos_client = WebOsClient('192.168.0.16')
        print('TV inicialitzada')

        # inicialitza model per a reconèixer objectes
        self.inicialitzaModelImatges()

        # inicialitza cares conegudes
        self.inicialitzaCares()

        #Saluda
        print('Martina inicialitzada')
        os.system(
            " echo 'Hola ja estic llesta!' | festival --language catalan --tts"
        )
예제 #9
0
def get_service(hass, config):
    """Return the notify service."""
    from pylgtv import WebOsClient
    from pylgtv import PyLGTVPairException

    client = WebOsClient(config.get(CONF_HOST))

    try:
        client.register()
    except PyLGTVPairException:
        _LOGGER.error('Pairing failed.')
        return None
    except OSError:
        _LOGGER.error('Host unreachable.')
        return None

    return LgWebOSNotificationService(client)
예제 #10
0
def get_service(hass, config):
    """Return the notify service."""
    from pylgtv import WebOsClient
    from pylgtv import PyLGTVPairException

    client = WebOsClient(config.get(CONF_HOST))

    try:
        client.register()
    except PyLGTVPairException:
        _LOGGER.error("Pairing with TV failed")
        return None
    except OSError:
        _LOGGER.error("TV unreachable")
        return None

    return LgWebOSNotificationService(client)
예제 #11
0
def get_service(hass, config, discovery_info=None):
    """Return the notify service."""
    from pylgtv import WebOsClient
    from pylgtv import PyLGTVPairException

    client = WebOsClient(config.get(CONF_HOST))

    try:
        client.register()
    except PyLGTVPairException:
        _LOGGER.error("Pairing with TV failed")
        return None
    except OSError:
        _LOGGER.error("TV unreachable")
        return None

    return LgWebOSNotificationService(client)
예제 #12
0
def get_service(hass, config, discovery_info=None):
    """Return the notify service."""
    from pylgtv import WebOsClient
    from pylgtv import PyLGTVPairException

    path = hass.config.path(config.get(CONF_FILENAME))
    client = WebOsClient(config.get(CONF_HOST), key_file_path=path)

    if not client.is_registered():
        try:
            client.register()
        except PyLGTVPairException:
            _LOGGER.error("Pairing with TV failed")
            return None
        except OSError:
            _LOGGER.error("TV unreachable")
            return None

    return LgWebOSNotificationService(client, config.get(CONF_ICON))
예제 #13
0
    def __init__(self, host):
        """Initialize the webos device."""
        from pylgtv import WebOsClient
        self._client = WebOsClient(host)

        self._name = 'LG WebOS TV Remote'
        # Assume that the TV is not muted
        self._muted = False
        # Assume that the TV is in Play mode
        self._playing = True
        self._volume = 0
        self._current_source = None
        self._current_source_id = None
        self._source_list = None
        self._source_label_list = None
        self._state = STATE_UNKNOWN
        self._app_list = None

        self.update()
예제 #14
0
    def __init__(self, host, name, customize, config, timeout, hass,
                 on_action):
        """Initialize the webos device."""
        from pylgtv import WebOsClient
        self._client = WebOsClient(host, config, timeout)
        self._on_script = Script(hass, on_action) if on_action else None
        self._customize = customize

        self._name = name
        # Assume that the TV is not muted
        self._muted = False
        # Assume that the TV is in Play mode
        self._playing = True
        self._volume = 0
        self._current_source = None
        self._current_source_id = None
        self._state = STATE_UNKNOWN
        self._source_list = {}
        self._app_list = {}
예제 #15
0
    def __init__(self, host, mac, name, customize, config):
        """Initialize the webos device."""
        from pylgtv import WebOsClient
        from wakeonlan import wol
        self._client = WebOsClient(host, config)
        self._wol = wol
        self._mac = mac
        self._customize = customize

        self._name = name
        # Assume that the TV is not muted
        self._muted = False
        # Assume that the TV is in Play mode
        self._playing = True
        self._volume = 0
        self._current_source = None
        self._current_source_id = None
        self._state = STATE_UNKNOWN
        self._source_list = {}
        self._app_list = {}
예제 #16
0
    def __init__(self, ip):
        self.client = WebOsClient(ip)

        self.commandLines = {
            'off': self.off,
            'on': self.wakeonlan,
            'software-info': self.software_info,
            'volume-up': self.volume_up,
            'volume-down': self.volume_down,
            'current-app': self.current_app,
            'apps': self.apps,
            'services': self.services,
            'get-volume': self.get_volume,
            'get-inputs': self.get_inputs,
            'app': self.app,
            'set-volume': self.set_volume,
            'close-app': self.close_app,
            'mute': self.mute,
            'unmute': self.unmute,
            'get-mute': self.get_mute,
            'get-input': self.get_input,
            'set-input': self.set_input,
            'channel-up': self.channel_up,
            'channel-down': self.channel_down,
            'channels': self.channels,
            'get-channel': self.get_channel,
            'info': self.info,
            'set-channel': self.set_channel,
            'play': self.play,
            'pause': self.pause,
            'stop': self.stop,
            'close': self.close,
            'rewind': self.rewind,
            'fast-forward': self.fast_forward,
            'send-message': self.send_message,
            'enter': self.enter,
            'delete': self.delete,

            #'3d-on': threeDOn,
            #'3d-off': threeDOff,
        }
예제 #17
0
class TV(object):
    def __init__(self):
        self.client = WebOsClient('192.168.1.75')

    @property
    def volume(self):
        return self.client.get_volume()

    @volume.setter
    def volume(self, value):
        assert isinstance(value, int)
        self.client.set_volume(value)

    def mute(self):
        self.client.set_mute(True)

    def unmute(self):
        self.client.set_mute(False)

    def toggle_mute(self):
        self.client.set_mute(not self.client.get_muted())
예제 #18
0
def setup_tv(host, hass, add_devices):
    """Setup a phue bridge based on host parameter."""
    from pylgtv import WebOsClient
    from pylgtv import PyLGTVPairException

    client = WebOsClient(host)

    if not client.is_registered():
        if host in _CONFIGURING:
            # Try to pair.
            try:
                client.register()
            except PyLGTVPairException:
                _LOGGER.warning(
                    'Connected to LG WebOS TV at %s but not paired.', host)
                return
            except ConnectionRefusedError:
                _LOGGER.error('Unable to connect to host %s.', host)
                return
        else:
            # Not registered, request configuration.
            _LOGGER.warning('LG WebOS TV at %s needs to be paired.', host)
            request_configuration(host, hass, add_devices)
            return

    # If we came here and configuring this host, mark as done.
    if client.is_registered() and host in _CONFIGURING:
        request_id = _CONFIGURING.pop(host)
        configurator = get_component('configurator')
        configurator.request_done(request_id)

    add_devices([LgWebOSDevice(host)])
예제 #19
0
def setup_tv(host, mac, name, customize, config, hass, add_devices):
    """Set up a LG WebOS TV based on host parameter."""
    from pylgtv import WebOsClient
    from pylgtv import PyLGTVPairException
    from websockets.exceptions import ConnectionClosed

    client = WebOsClient(host, config)

    if not client.is_registered():
        if host in _CONFIGURING:
            # Try to pair.
            try:
                client.register()
            except PyLGTVPairException:
                _LOGGER.warning("Connected to LG webOS TV %s but not paired",
                                host)
                return
            except (OSError, ConnectionClosed, TypeError,
                    asyncio.TimeoutError):
                _LOGGER.error("Unable to connect to host %s", host)
                return
        else:
            # Not registered, request configuration.
            _LOGGER.warning("LG webOS TV %s needs to be paired", host)
            request_configuration(host, mac, name, customize, config, hass,
                                  add_devices)
            return

    # If we came here and configuring this host, mark as done.
    if client.is_registered() and host in _CONFIGURING:
        request_id = _CONFIGURING.pop(host)
        configurator = get_component('configurator')
        configurator.request_done(request_id)

    add_devices([LgWebOSDevice(host, mac, name, customize, config)], True)
예제 #20
0
    def onStart(self):
        if Parameters["Mode6"] == "Debug":
            Domoticz.Debugging(1)
            Domoticz.Trace(True)
            self.debug = True
            DumpConfigToLog()

        Domoticz.Debug("onStart called")
        updateInterval = int(Parameters["Mode1"])
        if updateInterval > 30 or updateInterval < 7:
            updateInterval = 10
            Domoticz.Error("Invalid interval setting, changed to 10")

        Domoticz.Heartbeat(updateInterval)
        self.webos_client = WebOsClient(Parameters["Address"], "NONE", 5)

        # First get some TV details
        try:
            tv_details = self.webos_client.get_software_info()
            Domoticz.Log("Product name: " + tv_details["product_name"])
            Domoticz.Log("Software version: " + tv_details["major_ver"] + "." +
                         tv_details["minor_ver"])
            Domoticz.Log("MAC Address: " + tv_details["device_id"])
        except Exception as e:
            Domoticz.Error("Error at reading TV info")

        if not (1 in Devices):
            Domoticz.Device(Name="TV Status", Unit=1, Type=17,
                            Switchtype=17).Create()

        if not (2 in Devices):
            Domoticz.Device(Name="TV Volume",
                            Unit=2,
                            Type=244,
                            Subtype=73,
                            Switchtype=7,
                            Image=8).Create()

        return True
예제 #21
0
    def __init__(self, host, name, customize):
        """Initialize the webos device."""
        from pylgtv import WebOsClient
        self._client = WebOsClient(host)
        self._customize = customize

        self._name = name
        # Assume that the TV is not muted
        self._muted = False
        # Assume that the TV is in Play mode
        self._playing = True
        self._volume = 0
        self._current_source = None
        self._current_source_id = None
        self._source_list = None
        self._state = STATE_UNKNOWN
        self._app_list = None

        self.update()
예제 #22
0
    def __init__(self, host, name, customize, config, timeout,
                 hass, on_action):
        """Initialize the webos device."""
        from pylgtv import WebOsClient
        self._client = WebOsClient(host, config, timeout)
        self._on_script = Script(hass, on_action) if on_action else None
        self._customize = customize

        self._name = name
        # Assume that the TV is not muted
        self._muted = False
        # Assume that the TV is in Play mode
        self._playing = True
        self._volume = 0
        self._current_source = None
        self._current_source_id = None
        self._state = STATE_UNKNOWN
        self._source_list = {}
        self._app_list = {}
예제 #23
0
    def __init__(self, host, mac, name, customize, config):
        """Initialize the webos device."""
        from pylgtv import WebOsClient
        from wakeonlan import wol
        self._client = WebOsClient(host, config)
        self._wol = wol
        self._mac = mac
        self._customize = customize

        self._name = name
        # Assume that the TV is not muted
        self._muted = False
        # Assume that the TV is in Play mode
        self._playing = True
        self._volume = 0
        self._current_source = None
        self._current_source_id = None
        self._state = STATE_UNKNOWN
        self._source_list = {}
        self._app_list = {}
예제 #24
0
def setup_tv(host, name, customize, config, timeout, hass, add_entities,
             turn_on_action):
    """Set up a LG WebOS TV based on host parameter."""

    client = WebOsClient(host, config, timeout)

    if not client.is_registered():
        if host in _CONFIGURING:
            # Try to pair.
            try:
                client.register()
            except PyLGTVPairException:
                _LOGGER.warning("Connected to LG webOS TV %s but not paired",
                                host)
                return
            except (OSError, ConnectionClosed, asyncio.TimeoutError):
                _LOGGER.error("Unable to connect to host %s", host)
                return
        else:
            # Not registered, request configuration.
            _LOGGER.warning("LG webOS TV %s needs to be paired", host)
            request_configuration(
                host,
                name,
                customize,
                config,
                timeout,
                hass,
                add_entities,
                turn_on_action,
            )
            return

    # If we came here and configuring this host, mark as done.
    if client.is_registered() and host in _CONFIGURING:
        request_id = _CONFIGURING.pop(host)
        configurator = hass.components.configurator
        configurator.request_done(request_id)

    add_entities(
        [
            LgWebOSDevice(host, name, customize, config, timeout, hass,
                          turn_on_action)
        ],
        True,
    )
예제 #25
0
async def task_set(loop):
    global g_mute, g_volume
    try:
        tv = WebOsClient(WEBOSTV_IP)
        async with mqtt.local_client(True) as mqtt_client:
            await mqtt_client.subscribe(
                ('%s/mute/set' % TV_TOPIC, mqtt.QOS_1),
                ('%s/volume/set' % TV_TOPIC, mqtt.QOS_1))

            while True:
                message = await mqtt_client.deliver_message()
                data = message.data.decode('utf-8')
                LOGGER.debug('delivered on topic %d, message: %s',
                             message.topic, data)
                if message.topic == '%s/mute/set' % TV_TOPIC:
                    await tv.set_mute(data == '1' and True or False)
                elif message.topic == '%s/volume/set' % TV_TOPIC:
                    await tv.set_volume(int(data))

    except asyncio.CancelledError:
        pass
    except:
        LOGGER.exception('Error in task_set')
예제 #26
0
파일: example.py 프로젝트: maattdiy/pylgtv
from pylgtv import WebOsClient

webos_client = WebOsClient('192.168.0.112')
#webos_client.launch_app('netflix')

for app in webos_client.get_apps():
    print(app)
예제 #27
0
class LgWebOSDevice(MediaPlayerDevice):
    """Representation of a LG WebOS TV."""

    def __init__(self, host, name, customize, config, timeout,
                 hass, on_action):
        """Initialize the webos device."""
        from pylgtv import WebOsClient
        self._client = WebOsClient(host, config, timeout)
        self._on_script = Script(hass, on_action) if on_action else None
        self._customize = customize

        self._name = name
        # Assume that the TV is not muted
        self._muted = False
        # Assume that the TV is in Play mode
        self._playing = True
        self._volume = 0
        self._current_source = None
        self._current_source_id = None
        self._state = STATE_UNKNOWN
        self._source_list = {}
        self._app_list = {}
        self._channel = None

    @util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
    def update(self):
        """Retrieve the latest data."""
        from websockets.exceptions import ConnectionClosed
        try:
            current_input = self._client.get_input()
            if current_input is not None:
                self._current_source_id = current_input
                if self._state in (STATE_UNKNOWN, STATE_OFF):
                    self._state = STATE_PLAYING
            else:
                self._state = STATE_OFF
                self._current_source = None
                self._current_source_id = None
                self._channel = None

            if self._state is not STATE_OFF:
                self._muted = self._client.get_muted()
                self._volume = self._client.get_volume()
                self._channel = self._client.get_current_channel()

                self._source_list = {}
                self._app_list = {}
                conf_sources = self._customize.get(CONF_SOURCES, [])

                for app in self._client.get_apps():
                    self._app_list[app['id']] = app
                    if app['id'] == self._current_source_id:
                        self._current_source = app['title']
                        self._source_list[app['title']] = app
                    elif (not conf_sources or
                          app['id'] in conf_sources or
                          any(word in app['title']
                              for word in conf_sources) or
                          any(word in app['id']
                              for word in conf_sources)):
                        self._source_list[app['title']] = app

                for source in self._client.get_inputs():
                    if source['id'] == self._current_source_id:
                        self._current_source = source['label']
                        self._source_list[source['label']] = source
                    elif (not conf_sources or
                          source['label'] in conf_sources or
                          any(source['label'].find(word) != -1
                              for word in conf_sources)):
                        self._source_list[source['label']] = source
        except (OSError, ConnectionClosed, TypeError,
                asyncio.TimeoutError):
            self._state = STATE_OFF
            self._current_source = None
            self._current_source_id = None
            self._channel = None

    @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._current_source

    @property
    def source_list(self):
        """List of available input sources."""
        return sorted(self._source_list.keys())

    @property
    def media_content_type(self):
        """Content type of current playing media."""
        return MEDIA_TYPE_CHANNEL

    @property
    def media_title(self):
        """Title of current playing media."""
        if (self._channel is not None) and ('channelName' in self._channel):
            return self._channel['channelName']
        return None

    @property
    def media_image_url(self):
        """Image url of current playing media."""
        if self._current_source_id in self._app_list:
            icon = self._app_list[self._current_source_id]['largeIcon']
            if not icon.startswith('http'):
                icon = self._app_list[self._current_source_id]['icon']
            return icon
        return None

    @property
    def supported_features(self):
        """Flag media player features that are supported."""
        if self._on_script:
            return SUPPORT_WEBOSTV | SUPPORT_TURN_ON
        return SUPPORT_WEBOSTV

    def turn_off(self):
        """Turn off media player."""
        from websockets.exceptions import ConnectionClosed
        self._state = STATE_OFF
        try:
            self._client.power_off()
        except (OSError, ConnectionClosed, TypeError,
                asyncio.TimeoutError):
            pass

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

    def volume_up(self):
        """Volume up the media player."""
        self._client.volume_up()

    def volume_down(self):
        """Volume down media player."""
        self._client.volume_down()

    def set_volume_level(self, volume):
        """Set volume level, range 0..1."""
        tv_volume = volume * 100
        self._client.set_volume(tv_volume)

    def mute_volume(self, mute):
        """Send mute command."""
        self._muted = mute
        self._client.set_mute(mute)

    def media_play_pause(self):
        """Simulate play pause media player."""
        if self._playing:
            self.media_pause()
        else:
            self.media_play()

    def select_source(self, source):
        """Select input source."""
        source_dict = self._source_list.get(source)
        if source_dict is None:
            _LOGGER.warning("Source %s not found for %s", source, self.name)
            return
        self._current_source_id = source_dict['id']
        if source_dict.get('title'):
            self._current_source = source_dict['title']
            self._client.launch_app(source_dict['id'])
        elif source_dict.get('label'):
            self._current_source = source_dict['label']
            self._client.set_input(source_dict['id'])

    def play_media(self, media_type, media_id, **kwargs):
        """Play a piece of media."""
        _LOGGER.debug(
            "Call play media type <%s>, Id <%s>", media_type, media_id)

        if media_type == MEDIA_TYPE_CHANNEL:
            _LOGGER.debug("Searching channel...")
            partial_match_channel_id = None
            perfect_match_channel_id = None

            for channel in self._client.get_channels():
                if media_id == channel['channelNumber']:
                    perfect_match_channel_id = channel['channelId']
                    continue
                elif media_id.lower() == channel['channelName'].lower():
                    perfect_match_channel_id = channel['channelId']
                    continue
                elif media_id.lower() in channel['channelName'].lower():
                    partial_match_channel_id = channel['channelId']

            if perfect_match_channel_id is not None:
                _LOGGER.info(
                    "Switching to channel <%s> with perfect match",
                    perfect_match_channel_id)
                self._client.set_channel(perfect_match_channel_id)
            elif partial_match_channel_id is not None:
                _LOGGER.info(
                    "Switching to channel <%s> with partial match",
                    partial_match_channel_id)
                self._client.set_channel(partial_match_channel_id)

            return

    def media_play(self):
        """Send play command."""
        self._playing = True
        self._state = STATE_PLAYING
        self._client.play()

    def media_pause(self):
        """Send media pause command to media player."""
        self._playing = False
        self._state = STATE_PAUSED
        self._client.pause()

    def media_next_track(self):
        """Send next track command."""
        current_input = self._client.get_input()
        if current_input == LIVETV_APP_ID:
            self._client.channel_up()
        else:
            self._client.fast_forward()

    def media_previous_track(self):
        """Send the previous track command."""
        current_input = self._client.get_input()
        if current_input == LIVETV_APP_ID:
            self._client.channel_down()
        else:
            self._client.rewind()
예제 #28
0
#!/home/homeassistant/.homeassistant/bin/python
from pylgtv import WebOsClient
import sys

webos_client = WebOsClient('192.168.1.12')
webos_client.set_channel(str(sys.argv[-1]))
print(webos_client.get_current_channel())
#webos_client.launch_app('netflix')
#webos_client.launch_app('home theater')

#for app in webos_client.get_apps():
#    print(app)
예제 #29
0
class LgWebOSDevice(MediaPlayerDevice):
    """Representation of a LG WebOS TV."""
    def __init__(self, host, name, customize, config, timeout, hass,
                 on_action):
        """Initialize the webos device."""
        from pylgtv import WebOsClient

        self._client = WebOsClient(host, config, timeout)
        self._on_script = Script(hass, on_action) if on_action else None
        self._customize = customize

        self._name = name
        # Assume that the TV is not muted
        self._muted = False
        # Assume that the TV is in Play mode
        self._playing = True
        self._volume = 0
        self._current_source = None
        self._current_source_id = None
        self._state = None
        self._source_list = {}
        self._app_list = {}
        self._channel = None
        self._last_icon = None

    @util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
    def update(self):
        """Retrieve the latest data."""
        from websockets.exceptions import ConnectionClosed

        try:
            current_input = self._client.get_input()
            if current_input is not None:
                self._current_source_id = current_input
                if self._state in (None, STATE_OFF):
                    self._state = STATE_PLAYING
            else:
                self._state = STATE_OFF
                self._current_source = None
                self._current_source_id = None
                self._channel = None

            if self._state is not STATE_OFF:
                self._muted = self._client.get_muted()
                self._volume = self._client.get_volume()
                self._channel = self._client.get_current_channel()

                self._source_list = {}
                self._app_list = {}
                conf_sources = self._customize.get(CONF_SOURCES, [])

                for app in self._client.get_apps():
                    self._app_list[app["id"]] = app
                    if app["id"] == self._current_source_id:
                        self._current_source = app["title"]
                        self._source_list[app["title"]] = app
                    elif (not conf_sources or app["id"] in conf_sources
                          or any(word in app["title"] for word in conf_sources)
                          or any(word in app["id"] for word in conf_sources)):
                        self._source_list[app["title"]] = app

                for source in self._client.get_inputs():
                    if source["id"] == self._current_source_id:
                        self._current_source = source["label"]
                        self._source_list[source["label"]] = source
                    elif (not conf_sources or source["label"] in conf_sources
                          or any(source["label"].find(word) != -1
                                 for word in conf_sources)):
                        self._source_list[source["label"]] = source
        except (OSError, ConnectionClosed, TypeError, asyncio.TimeoutError):
            self._state = STATE_OFF
            self._current_source = None
            self._current_source_id = None
            self._channel = None

    @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._current_source

    @property
    def source_list(self):
        """List of available input sources."""
        return sorted(self._source_list.keys())

    @property
    def media_content_type(self):
        """Content type of current playing media."""
        return MEDIA_TYPE_CHANNEL

    @property
    def media_title(self):
        """Title of current playing media."""
        if (self._channel is not None) and ("channelName" in self._channel):
            return self._channel["channelName"]
        return None

    @property
    def media_image_url(self):
        """Image url of current playing media."""
        if self._current_source_id in self._app_list:
            icon = self._app_list[self._current_source_id]["largeIcon"]
            if not icon.startswith("http"):
                icon = self._app_list[self._current_source_id]["icon"]

            # 'icon' holds a URL with a transient key. Avoid unnecessary
            # updates by returning the same URL until the image changes.
            if self._last_icon and (icon.split("/")[-1]
                                    == self._last_icon.split("/")[-1]):
                return self._last_icon
            self._last_icon = icon
            return icon
        return None

    @property
    def supported_features(self):
        """Flag media player features that are supported."""
        if self._on_script:
            return SUPPORT_WEBOSTV | SUPPORT_TURN_ON
        return SUPPORT_WEBOSTV

    def turn_off(self):
        """Turn off media player."""
        from websockets.exceptions import ConnectionClosed

        self._state = STATE_OFF
        try:
            self._client.power_off()
        except (OSError, ConnectionClosed, TypeError, asyncio.TimeoutError):
            pass

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

    def volume_up(self):
        """Volume up the media player."""
        self._client.volume_up()

    def volume_down(self):
        """Volume down media player."""
        self._client.volume_down()

    def set_volume_level(self, volume):
        """Set volume level, range 0..1."""
        tv_volume = volume * 100
        self._client.set_volume(tv_volume)

    def mute_volume(self, mute):
        """Send mute command."""
        self._muted = mute
        self._client.set_mute(mute)

    def media_play_pause(self):
        """Simulate play pause media player."""
        if self._playing:
            self.media_pause()
        else:
            self.media_play()

    def select_source(self, source):
        """Select input source."""
        source_dict = self._source_list.get(source)
        if source_dict is None:
            _LOGGER.warning("Source %s not found for %s", source, self.name)
            return
        self._current_source_id = source_dict["id"]
        if source_dict.get("title"):
            self._current_source = source_dict["title"]
            self._client.launch_app(source_dict["id"])
        elif source_dict.get("label"):
            self._current_source = source_dict["label"]
            self._client.set_input(source_dict["id"])

    def play_media(self, media_type, media_id, **kwargs):
        """Play a piece of media."""
        _LOGGER.debug("Call play media type <%s>, Id <%s>", media_type,
                      media_id)

        if media_type == MEDIA_TYPE_CHANNEL:
            _LOGGER.debug("Searching channel...")
            partial_match_channel_id = None
            perfect_match_channel_id = None

            for channel in self._client.get_channels():
                if media_id == channel["channelNumber"]:
                    perfect_match_channel_id = channel["channelId"]
                    continue
                elif media_id.lower() == channel["channelName"].lower():
                    perfect_match_channel_id = channel["channelId"]
                    continue
                elif media_id.lower() in channel["channelName"].lower():
                    partial_match_channel_id = channel["channelId"]

            if perfect_match_channel_id is not None:
                _LOGGER.info(
                    "Switching to channel <%s> with perfect match",
                    perfect_match_channel_id,
                )
                self._client.set_channel(perfect_match_channel_id)
            elif partial_match_channel_id is not None:
                _LOGGER.info(
                    "Switching to channel <%s> with partial match",
                    partial_match_channel_id,
                )
                self._client.set_channel(partial_match_channel_id)

            return

    def media_play(self):
        """Send play command."""
        self._playing = True
        self._state = STATE_PLAYING
        self._client.play()

    def media_pause(self):
        """Send media pause command to media player."""
        self._playing = False
        self._state = STATE_PAUSED
        self._client.pause()

    def media_next_track(self):
        """Send next track command."""
        current_input = self._client.get_input()
        if current_input == LIVETV_APP_ID:
            self._client.channel_up()
        else:
            self._client.fast_forward()

    def media_previous_track(self):
        """Send the previous track command."""
        current_input = self._client.get_input()
        if current_input == LIVETV_APP_ID:
            self._client.channel_down()
        else:
            self._client.rewind()
예제 #30
0
class LgWebOSDevice(MediaPlayerDevice):
    """Representation of a LG WebOS TV."""

    def __init__(self, host, mac, name, customize):
        """Initialize the webos device."""
        from pylgtv import WebOsClient
        from wakeonlan import wol
        self._client = WebOsClient(host)
        self._wol = wol
        self._mac = mac
        self._customize = customize

        self._name = name
        # Assume that the TV is not muted
        self._muted = False
        # Assume that the TV is in Play mode
        self._playing = True
        self._volume = 0
        self._current_source = None
        self._current_source_id = None
        self._state = STATE_UNKNOWN
        self._source_list = {}
        self._app_list = {}

    @util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
    def update(self):
        """Retrieve the latest data."""
        from websockets.exceptions import ConnectionClosed
        try:
            self._state = STATE_PLAYING
            self._muted = self._client.get_muted()
            self._volume = self._client.get_volume()
            self._current_source_id = self._client.get_input()
            self._source_list = {}
            self._app_list = {}

            custom_sources = self._customize.get(CONF_SOURCES, [])

            for app in self._client.get_apps():
                self._app_list[app['id']] = app
                if app['id'] == self._current_source_id:
                    self._current_source = app['title']
                    self._source_list[app['title']] = app
                elif (app['id'] in custom_sources or
                      any(word in app['title'] for word in custom_sources) or
                      any(word in app['id'] for word in custom_sources)):
                    self._source_list[app['title']] = app

            for source in self._client.get_inputs():
                if not source['connected']:
                    continue
                app = self._app_list[source['appId']]
                self._source_list[app['title']] = app

        except (OSError, ConnectionClosed):
            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._current_source

    @property
    def source_list(self):
        """List of available input sources."""
        return sorted(self._source_list.keys())

    @property
    def media_content_type(self):
        """Content type of current playing media."""
        return MEDIA_TYPE_CHANNEL

    @property
    def media_image_url(self):
        """Image url of current playing media."""
        if self._current_source_id in self._app_list:
            return self._app_list[self._current_source_id]['largeIcon']
        return None

    @property
    def supported_media_commands(self):
        """Flag of media commands that are supported."""
        if self._mac:
            return SUPPORT_WEBOSTV | SUPPORT_TURN_ON
        return SUPPORT_WEBOSTV

    def turn_off(self):
        """Turn off media player."""
        from websockets.exceptions import ConnectionClosed
        self._state = STATE_OFF
        try:
            self._client.power_off()
        except (OSError, ConnectionClosed):
            pass

    def turn_on(self):
        """Turn on the media player."""
        if self._mac:
            self._wol.send_magic_packet(self._mac)

    def volume_up(self):
        """Volume up the media player."""
        self._client.volume_up()

    def volume_down(self):
        """Volume down media player."""
        self._client.volume_down()

    def set_volume_level(self, volume):
        """Set volume level, range 0..1."""
        tv_volume = volume * 100
        self._client.set_volume(tv_volume)

    def mute_volume(self, mute):
        """Send mute command."""
        self._muted = mute
        self._client.set_mute(mute)

    def media_play_pause(self):
        """Simulate play pause media player."""
        if self._playing:
            self.media_pause()
        else:
            self.media_play()

    def select_source(self, source):
        """Select input source."""
        self._current_source_id = self._source_list[source]['id']
        self._current_source = self._source_list[source]['title']
        self._client.launch_app(self._source_list[source]['id'])

    def media_play(self):
        """Send play command."""
        self._playing = True
        self._state = STATE_PLAYING
        self._client.play()

    def media_pause(self):
        """Send media pause command to media player."""
        self._playing = False
        self._state = STATE_PAUSED
        self._client.pause()

    def media_next_track(self):
        """Send next track command."""
        self._client.fast_forward()

    def media_previous_track(self):
        """Send the previous track command."""
        self._client.rewind()
예제 #31
0
class LgCommand(object):
    def __init__(self, ip):
        self.client = WebOsClient(ip)

        self.commandLines = {
            'off': self.off,
            'on': self.wakeonlan,
            'software-info': self.software_info,
            'volume-up': self.volume_up,
            'volume-down': self.volume_down,
            'current-app': self.current_app,
            'apps': self.apps,
            'services': self.services,
            'get-volume': self.get_volume,
            'get-inputs': self.get_inputs,
            'app': self.app,
            'set-volume': self.set_volume,
            'close-app': self.close_app,
            'mute': self.mute,
            'unmute': self.unmute,
            'get-mute': self.get_mute,
            'get-input': self.get_input,
            'set-input': self.set_input,
            'channel-up': self.channel_up,
            'channel-down': self.channel_down,
            'channels': self.channels,
            'get-channel': self.get_channel,
            'info': self.info,
            'set-channel': self.set_channel,
            'play': self.play,
            'pause': self.pause,
            'stop': self.stop,
            'close': self.close,
            'rewind': self.rewind,
            'fast-forward': self.fast_forward,
            'send-message': self.send_message,
            'enter': self.enter,
            'delete': self.delete,

            #'3d-on': threeDOn,
            #'3d-off': threeDOff,
        }

    def run(self, command, arg=None):
        return self.commandLines[command](arg)

    def send_message(self, arg):
        return self.client.send_message(arg)

    def delete(self, arg):
        return self.client.send_delete_key()

    def enter(self, arg):
        return self.client.send_enter_key()

    def play(self, arg):
        return self.client.play()

    def pause(self, arg):
        return self.client.pause()

    def stop(self, arg):
        return self.client.stop()

    def close(self, arg):
        return self.client.close()

    def rewind(self, arg):
        return self.client.rewind()

    def fast_forward(self, arg):
        return self.client.fast_forward()

    def info(self, arg):
        return self.client.get_channel_info()

    def set_channel(self, arg):
        return self.client.set_channel(arg)

    def get_channel(self, arg):
        return self.client.get_current_channel()

    def channels(self, arg):
        return self.client.get_channels()

    def channel_down(self, arg):
        return self.client.channel_down()

    def channel_up(self, arg):
        return self.client.channel_up()

    def get_input(self, arg):
        return self.client.get_input()

    def set_input(self, arg):
        return self.client.set_input(arg)

    def unmute(self, arg):
        return self.client.set_mute(False)

    def mute(self, arg):
        return self.client.set_mute(True)

    def get_mute(self, arg):
        return self.client.get_muted()

    def set_volume(self, arg):
        return self.client.set_volume(int(arg))

    def close_app(self, arg):
        return self.client.close_app(arg)

    def app(self, arg):
        return self.client.launch_app(arg)

    def get_inputs(self, arg):
        return self.client.get_inputs()

    def get_volume(self, arg):
        return self.client.get_volume()

    def services(self, arg):
        return self.client.get_services()

    def current_app(self, arg):
        return self.client.get_current_app()

    def apps(self, arg):
        return self.client.get_apps()

    def off(self, arg):
        self.client.power_off()
        return "TV has been turned off."

    def software_info(self, arg):
        return self.check()

    def volume_down(self, arg):
        return self.client.volume_down()

    def volume_up(self, arg):
        return self.client.volume_up()

    def wakeonlan(self, mac):
        if mac is not None:
            addr_byte = mac.split(':')
            hw_addr = struct.pack('BBBBBB', int(addr_byte[0], 16),
                                  int(addr_byte[1], 16), int(addr_byte[2], 16),
                                  int(addr_byte[3], 16), int(addr_byte[4], 16),
                                  int(addr_byte[5], 16))
            msg = b'\xff' * 6 + hw_addr * 16
            socket_instance = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            socket_instance.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST,
                                       1)
            socket_instance.sendto(msg, ('<broadcast>', 9))
            socket_instance.close()
            return "TV has been turned on."
        else:
            return "mac address (arg) not set. use -h for help."

    def check(self):
        return self.client.get_software_info()
예제 #32
0
from pylgtv import WebOsClient

import sys
import logging

logging.basicConfig(stream=sys.stdout, level=logging.INFO)

try:
    webos_client = WebOsClient('192.168.0.13')
    webos_client.launch_app('netflix')
    # zz = webos_client.load_key_file()
    # vv = webos_client.register()
    # print(vv)
    #
    # for app in webos_client.get_apps():
    #     print(app)
except:
    print("Error connecting to TV")
예제 #33
0
from pylgtv import WebOsClient

import sys
import logging

logging.basicConfig(stream=sys.stdout, level=logging.INFO)

try:
    tv = WebOsClient('192.168.0.54')
    tv.set_mute(False)
except:
    print("Error connecting to TV")
예제 #34
0
class LgWebOSDevice(MediaPlayerDevice):
    """Representation of a LG WebOS TV."""

    # pylint: disable=too-many-public-methods
    def __init__(self, host, name, customize):
        """Initialize the webos device."""
        from pylgtv import WebOsClient
        self._client = WebOsClient(host)
        self._customize = customize

        self._name = name
        # Assume that the TV is not muted
        self._muted = False
        # Assume that the TV is in Play mode
        self._playing = True
        self._volume = 0
        self._current_source = None
        self._current_source_id = None
        self._source_list = None
        self._state = STATE_UNKNOWN
        self._app_list = None

        self.update()

    @util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
    def update(self):
        """Retrieve the latest data."""
        try:
            self._state = STATE_PLAYING
            self._muted = self._client.get_muted()
            self._volume = self._client.get_volume()
            self._current_source_id = self._client.get_input()
            self._source_list = {}
            self._app_list = {}

            custom_sources = []
            for source in self._customize.get(CONF_SOURCES, []):
                app_id = WEBOS_APPS_SHORT.get(source, None)
                if app_id:
                    custom_sources.append(app_id)
                else:
                    custom_sources.append(source)

            for app in self._client.get_apps():
                self._app_list[app['id']] = app
                if app['id'] == self._current_source_id:
                    self._current_source = app['title']
                    self._source_list[app['title']] = app
                if app['id'] in custom_sources:
                    self._source_list[app['title']] = app

            for source in self._client.get_inputs():
                if not source['connected']:
                    continue
                app = self._app_list[source['appId']]
                self._source_list[app['title']] = app

        except OSError:
            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._current_source

    @property
    def source_list(self):
        """List of available input sources."""
        return sorted(self._source_list.keys())

    @property
    def media_content_type(self):
        """Content type of current playing media."""
        return MEDIA_TYPE_CHANNEL

    @property
    def media_image_url(self):
        """Image url of current playing media."""
        if self._current_source_id in self._app_list:
            return self._app_list[self._current_source_id]['largeIcon']
        return None

    @property
    def supported_media_commands(self):
        """Flag of media commands that are supported."""
        return SUPPORT_WEBOSTV

    def turn_off(self):
        """Turn off media player."""
        self._state = STATE_OFF
        self._client.power_off()

    def volume_up(self):
        """Volume up the media player."""
        self._client.volume_up()

    def volume_down(self):
        """Volume down media player."""
        self._client.volume_down()

    def set_volume_level(self, volume):
        """Set volume level, range 0..1."""
        tv_volume = volume * 100
        self._client.set_volume(tv_volume)

    def mute_volume(self, mute):
        """Send mute command."""
        self._muted = mute
        self._client.set_mute(mute)

    def media_play_pause(self):
        """Simulate play pause media player."""
        if self._playing:
            self.media_pause()
        else:
            self.media_play()

    def select_source(self, source):
        """Select input source."""
        self._current_source_id = self._source_list[source]['id']
        self._current_source = self._source_list[source]['title']
        self._client.launch_app(self._source_list[source]['id'])

    def media_play(self):
        """Send play command."""
        self._playing = True
        self._state = STATE_PLAYING
        self._client.play()

    def media_pause(self):
        """Send media pause command to media player."""
        self._playing = False
        self._state = STATE_PAUSED
        self._client.pause()

    def media_next_track(self):
        """Send next track command."""
        self._client.fast_forward()

    def media_previous_track(self):
        """Send the previous track command."""
        self._client.rewind()
예제 #35
0
#!/usr/bin/python3
from pylgtv import WebOsClient
import sys

try:
    ip = sys.argv[1]
    msg = sys.argv[2]
except:
    print("Usage : %s <ip> <msg>" % (sys.argv[0]))
    exit(-1)
webos_client = WebOsClient(ip)
webos_client.send_message(msg)
예제 #36
0
class LgWebOSDevice(MediaPlayerDevice):
    """Representation of a LG WebOS TV."""

    def __init__(self, host, mac, name, customize, config):
        """Initialize the webos device."""
        from pylgtv import WebOsClient
        from wakeonlan import wol
        self._client = WebOsClient(host, config)
        self._wol = wol
        self._mac = mac
        self._customize = customize

        self._name = name
        # Assume that the TV is not muted
        self._muted = False
        # Assume that the TV is in Play mode
        self._playing = True
        self._volume = 0
        self._current_source = None
        self._current_source_id = None
        self._state = STATE_UNKNOWN
        self._source_list = {}
        self._app_list = {}

    @util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
    def update(self):
        """Retrieve the latest data."""
        from websockets.exceptions import ConnectionClosed
        try:
            current_input = self._client.get_input()
            if current_input is not None:
                self._current_source_id = current_input
                if self._state in (STATE_UNKNOWN, STATE_OFF):
                    self._state = STATE_PLAYING
            else:
                self._state = STATE_OFF
                self._current_source = None
                self._current_source_id = None

            if self._state is not STATE_OFF:
                self._muted = self._client.get_muted()
                self._volume = self._client.get_volume()

                self._source_list = {}
                self._app_list = {}
                conf_sources = self._customize.get(CONF_SOURCES, [])

                for app in self._client.get_apps():
                    self._app_list[app['id']] = app
                    if conf_sources:
                        if app['id'] == self._current_source_id:
                            self._current_source = app['title']
                            self._source_list[app['title']] = app
                        elif (app['id'] in conf_sources or
                              any(word in app['title']
                                  for word in conf_sources) or
                              any(word in app['id']
                                  for word in conf_sources)):
                            self._source_list[app['title']] = app
                    else:
                        self._current_source = app['title']
                        self._source_list[app['title']] = app

                for source in self._client.get_inputs():
                    if conf_sources:
                        if source['id'] == self._current_source_id:
                            self._source_list[source['label']] = source
                        elif (source['label'] in conf_sources or
                              any(source['label'].find(word) != -1
                                  for word in conf_sources)):
                            self._source_list[source['label']] = source
                    else:
                        self._source_list[source['label']] = source
        except (OSError, ConnectionClosed, TypeError,
                asyncio.TimeoutError):
            self._state = STATE_OFF
            self._current_source = None
            self._current_source_id = None

    @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._current_source

    @property
    def source_list(self):
        """List of available input sources."""
        return sorted(self._source_list.keys())

    @property
    def media_content_type(self):
        """Content type of current playing media."""
        return MEDIA_TYPE_CHANNEL

    @property
    def media_image_url(self):
        """Image url of current playing media."""
        if self._current_source_id in self._app_list:
            icon = self._app_list[self._current_source_id]['largeIcon']
            if not icon.startswith('http'):
                icon = self._app_list[self._current_source_id]['icon']
            return icon
        return None

    @property
    def supported_features(self):
        """Flag media player features that are supported."""
        if self._mac:
            return SUPPORT_WEBOSTV | SUPPORT_TURN_ON
        return SUPPORT_WEBOSTV

    def turn_off(self):
        """Turn off media player."""
        from websockets.exceptions import ConnectionClosed
        self._state = STATE_OFF
        try:
            self._client.power_off()
        except (OSError, ConnectionClosed, TypeError,
                asyncio.TimeoutError):
            pass

    def turn_on(self):
        """Turn on the media player."""
        if self._mac:
            self._wol.send_magic_packet(self._mac)

    def volume_up(self):
        """Volume up the media player."""
        self._client.volume_up()

    def volume_down(self):
        """Volume down media player."""
        self._client.volume_down()

    def set_volume_level(self, volume):
        """Set volume level, range 0..1."""
        tv_volume = volume * 100
        self._client.set_volume(tv_volume)

    def mute_volume(self, mute):
        """Send mute command."""
        self._muted = mute
        self._client.set_mute(mute)

    def media_play_pause(self):
        """Simulate play pause media player."""
        if self._playing:
            self.media_pause()
        else:
            self.media_play()

    def select_source(self, source):
        """Select input source."""
        if self._source_list.get(source).get('title'):
            self._current_source_id = self._source_list[source]['id']
            self._current_source = self._source_list[source]['title']
            self._client.launch_app(self._source_list[source]['id'])
        elif self._source_list.get(source).get('label'):
            self._current_source_id = self._source_list[source]['id']
            self._current_source = self._source_list[source]['label']
            self._client.set_input(self._source_list[source]['id'])

    def media_play(self):
        """Send play command."""
        self._playing = True
        self._state = STATE_PLAYING
        self._client.play()

    def media_pause(self):
        """Send media pause command to media player."""
        self._playing = False
        self._state = STATE_PAUSED
        self._client.pause()

    def media_next_track(self):
        """Send next track command."""
        self._client.fast_forward()

    def media_previous_track(self):
        """Send the previous track command."""
        self._client.rewind()
예제 #37
0
파일: volume.py 프로젝트: l6989/raspberrypi
#!/home/homeassistant/.homeassistant/bin/python
from pylgtv import WebOsClient
import sys

webos_client = WebOsClient('192.168.1.12')
webos_client.set_volume(int(sys.argv[-1]))
#webos_client.launch_app('netflix')
#webos_client.launch_app('home theater')

#for app in webos_client.get_apps():
#    print(app)
예제 #38
0
#!/usr/bin/python3
from pylgtv import WebOsClient

import sys
import logging

host = '192.168.88.18'
config = '/home/homeassistant/.homeassistant/webostv.conf'
timeout = 5

logging.basicConfig(stream=sys.stdout, level=logging.INFO)

try:
    webos_client = WebOsClient(host, config, timeout)

except:
    print("Error connecting to TV")

try:
    #device = webos_client.get_software_info()
    #print("  Device: "+device("product_name"))
    #print("   Model: "+device("model_name"))
    #print("     MAC: "+device("device_id"))
    #print(" Version: "+device("major_ver")+"."+device("minor_ver"))

    for app in webos_client.get_apps():
        print(app)
        print("       ID: "+app["id"])
        print("    Title: "+app["title"])
        print("    Image: "+app["largeIcon"])
        print(" UserData: "+app["userData"])
예제 #39
0
 def __init__(self):
     self.client = WebOsClient('192.168.1.75')
예제 #40
0
class LgWebOSDevice(MediaPlayerDevice):
    """Representation of a LG WebOS TV."""
    def __init__(self, host, name, customize, config, timeout, hass,
                 on_action):
        """Initialize the webos device."""
        from pylgtv import WebOsClient
        self._client = WebOsClient(host, config, timeout)
        self._on_script = Script(hass, on_action) if on_action else None
        self._customize = customize

        self._name = name
        # Assume that the TV is not muted
        self._muted = False
        # Assume that the TV is in Play mode
        self._playing = True
        self._volume = 0
        self._current_source = None
        self._current_source_id = None
        self._state = STATE_UNKNOWN
        self._source_list = {}
        self._app_list = {}
        self._channel = None

    @util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
    def update(self):
        """Retrieve the latest data."""
        from websockets.exceptions import ConnectionClosed
        try:
            current_input = self._client.get_input()
            if current_input is not None:
                self._current_source_id = current_input
                if self._state in (STATE_UNKNOWN, STATE_OFF):
                    self._state = STATE_PLAYING
            else:
                self._state = STATE_OFF
                self._current_source = None
                self._current_source_id = None
                self._channel = None

            if self._state is not STATE_OFF:
                self._muted = self._client.get_muted()
                self._volume = self._client.get_volume()
                self._channel = self._client.get_current_channel()

                self._source_list = {}
                self._app_list = {}
                conf_sources = self._customize.get(CONF_SOURCES, [])

                for app in self._client.get_apps():
                    self._app_list[app['id']] = app
                    if app['id'] == self._current_source_id:
                        self._current_source = app['title']
                        self._source_list[app['title']] = app
                    elif (not conf_sources or app['id'] in conf_sources
                          or any(word in app['title'] for word in conf_sources)
                          or any(word in app['id'] for word in conf_sources)):
                        self._source_list[app['title']] = app

                for source in self._client.get_inputs():
                    if source['id'] == self._current_source_id:
                        self._current_source = source['label']
                        self._source_list[source['label']] = source
                    elif (not conf_sources or source['label'] in conf_sources
                          or any(source['label'].find(word) != -1
                                 for word in conf_sources)):
                        self._source_list[source['label']] = source
        except (OSError, ConnectionClosed, TypeError, asyncio.TimeoutError):
            self._state = STATE_OFF
            self._current_source = None
            self._current_source_id = None
            self._channel = None

    @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._current_source

    @property
    def source_list(self):
        """List of available input sources."""
        return sorted(self._source_list.keys())

    @property
    def media_content_type(self):
        """Content type of current playing media."""
        return MEDIA_TYPE_CHANNEL

    @property
    def media_title(self):
        """Title of current playing media."""
        if (self._channel is not None) and ('channelName' in self._channel):
            return self._channel['channelName']
        return None

    @property
    def media_image_url(self):
        """Image url of current playing media."""
        if self._current_source_id in self._app_list:
            icon = self._app_list[self._current_source_id]['largeIcon']
            if not icon.startswith('http'):
                icon = self._app_list[self._current_source_id]['icon']
            return icon
        return None

    @property
    def supported_features(self):
        """Flag media player features that are supported."""
        if self._on_script:
            return SUPPORT_WEBOSTV | SUPPORT_TURN_ON
        return SUPPORT_WEBOSTV

    def turn_off(self):
        """Turn off media player."""
        from websockets.exceptions import ConnectionClosed
        self._state = STATE_OFF
        try:
            self._client.power_off()
        except (OSError, ConnectionClosed, TypeError, asyncio.TimeoutError):
            pass

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

    def volume_up(self):
        """Volume up the media player."""
        self._client.volume_up()

    def volume_down(self):
        """Volume down media player."""
        self._client.volume_down()

    def set_volume_level(self, volume):
        """Set volume level, range 0..1."""
        tv_volume = volume * 100
        self._client.set_volume(tv_volume)

    def mute_volume(self, mute):
        """Send mute command."""
        self._muted = mute
        self._client.set_mute(mute)

    def media_play_pause(self):
        """Simulate play pause media player."""
        if self._playing:
            self.media_pause()
        else:
            self.media_play()

    def select_source(self, source):
        """Select input source."""
        source_dict = self._source_list.get(source)
        if source_dict is None:
            _LOGGER.warning("Source %s not found for %s", source, self.name)
            return
        self._current_source_id = source_dict['id']
        if source_dict.get('title'):
            self._current_source = source_dict['title']
            self._client.launch_app(source_dict['id'])
        elif source_dict.get('label'):
            self._current_source = source_dict['label']
            self._client.set_input(source_dict['id'])

    def media_play(self):
        """Send play command."""
        self._playing = True
        self._state = STATE_PLAYING
        self._client.play()

    def media_pause(self):
        """Send media pause command to media player."""
        self._playing = False
        self._state = STATE_PAUSED
        self._client.pause()

    def media_next_track(self):
        """Send next track command."""
        self._client.fast_forward()

    def media_previous_track(self):
        """Send the previous track command."""
        self._client.rewind()
예제 #41
0
class LgWebOSDevice(MediaPlayerDevice):
    """Representation of a LG WebOS TV."""

    # pylint: disable=too-many-public-methods
    def __init__(self, host):
        """Initialize the webos device."""
        from pylgtv import WebOsClient
        self._client = WebOsClient(host)

        self._name = 'LG WebOS TV Remote'
        # Assume that the TV is not muted
        self._muted = False
        # Assume that the TV is in Play mode
        self._playing = True
        self._volume = 0
        self._current_source = None
        self._current_source_id = None
        self._source_list = None
        self._source_label_list = None
        self._state = STATE_UNKNOWN
        self._app_list = None

        self.update()

    @util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
    def update(self):
        """Retrieve the latest data."""
        try:
            self._state = STATE_PLAYING
            self._muted = self._client.get_muted()
            self._volume = self._client.get_volume()
            self._current_source_id = self._client.get_input()

            self._source_list = {}
            self._source_label_list = []
            self._app_list = {}
            for app in self._client.get_apps():
                self._app_list[app['id']] = app

            for source in self._client.get_inputs():
                self._source_list[source['label']] = source
                self._app_list[source['appId']] = source
                self._source_label_list.append(source['label'])
                if source['appId'] == self._current_source_id:
                    self._current_source = source['label']

        except ConnectionRefusedError:
            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._current_source

    @property
    def source_list(self):
        """List of available input sources."""
        return self._source_label_list

    @property
    def media_content_type(self):
        """Content type of current playing media."""
        return MEDIA_TYPE_CHANNEL

    @property
    def media_image_url(self):
        """Image url of current playing media."""
        return self._app_list[self._current_source_id]['icon']

    @property
    def supported_media_commands(self):
        """Flag of media commands that are supported."""
        return SUPPORT_WEBOSTV

    def turn_off(self):
        """Turn off media player."""
        self._client.power_off()

    def volume_up(self):
        """Volume up the media player."""
        self._client.volume_up()

    def volume_down(self):
        """Volume down media player."""
        self._client.volume_down()

    def set_volume_level(self, volume):
        """Set volume level, range 0..1."""
        tv_volume = volume * 100
        self._client.set_volume(tv_volume)

    def mute_volume(self, mute):
        """Send mute command."""
        self._muted = mute
        self._client.set_mute(mute)

    def media_play_pause(self):
        """Simulate play pause media player."""
        if self._playing:
            self.media_pause()
        else:
            self.media_play()

    def select_source(self, source):
        """Select input source."""
        self._current_source_id = self._source_list[source]['appId']
        self._current_source = self._source_list[source]['label']
        self._client.set_input(self._source_list[source]['id'])

    def media_play(self):
        """Send play command."""
        self._playing = True
        self._state = STATE_PLAYING
        self._client.play()

    def media_pause(self):
        """Send media pause command to media player."""
        self._playing = False
        self._state = STATE_PAUSED
        self._client.pause()

    def media_next_track(self):
        """Send next track command."""
        self._client.fast_forward()

    def media_previous_track(self):
        """Send the previous track command."""
        self._client.rewind()