Support for pre 2012 models LG Nestcast TV's needed

Hello @wokar, @fabaff, @balloob, @shmuelzon guys. Anyone of you could factor in support for the pre 2012 models LG Netcast TV’s? below is a working script for it.

When I first posted it I hope support was there when you came up with the lg_netcast.py component.
But it was for 2012 and up. Hope you would be so kind in making one for pre 2012 for the less wealthy among is that can’t go out and just buy a new TV. :grin:

Hi @dennisaion
I will take a look week - seems pretty easy to support the pre 2012 models. I will let you know when I have something to test :wink:

Thanks in advance :grin:

I have now added support for pre 2012 TVs to the pylgnetcast library which is used in HA.
Before I update the HA side, please test the library with your TV. Sending commands should work now, but HA also use some calls to get channel and volume info. I could not find any information if this calls will also work with your TV.
The only way to find out is to try it ;-), so please download the pylgnetcast tool from https://github.com/wokar/pylgnetcast and try:

python -m pylgnetcast --host <IP of your TV> --pairing_key <pairing key of your TV> --protocol hdcp --command 24

This should increase the volume of your TV and display some channel and volume info on the command line. If you get something like “Can not retrieve channel info - …” we can not display the TV channels in the HA UI, but the command should still work.

Currently HA uses following commands 1 (turn off), 24 (volume up), 25 (volume down), 26 (mute), 33 (play), 34 (pause), 36 (next track), 37 (previous track). It would be great if you could try them all to make sure that the HA binding will work.

Thanks Wokar. Will test it rights now.

Okay had to run it like this

python3 -m pylgnetcast --host 192.168.0.104  --pairing_key XXXXXX --command 24

Nothing happens

python3 -m pylgnetcast --host 192.168.0.104  --pairing_key XXXXXX

also nothing

With python command I get failure messages.
With python command no faI lure messages but nothing happens.
No failure messages, no actions, nothing.

Think you missed the --protocol hdcp parameter to use the protocol for the older TVs.

Hey Wokar.

Yeah sorry I missed that.

I ran

python3 pylgnetcast.py --host 192.168.0.104  --pairing_key XXXXXX  --protocol hdcp --command 24

and other commands but still nothing. I tried with roap also but nothing. when I use the lgcommander.py it works no problem.

The pylgnetcast.py though runs with no errors and does nothing to TV whatever command I run.

I was wondering about this part.

    def _send_to_tv(self, message_type, message=None, payload=None):
        """Send message of given type to the tv."""
        if message_type != 'command' and self.protocol == LG_PROTOCOL.HDCP:
            message_type = 'dtv_wifirc'
        url = '%s%s' % (self.url, message_type)
        if message:
            response = requests.post(url, data=message, headers=self.HEADER,
                                     timeout=DEFAULT_TIMEOUT)
        else:
            response = requests.get(url, params=payload, headers=self.HEADER,
                                    timeout=DEFAULT_TIMEOUT)
        return response

lgcommander formats the command like this

def handleCommand(cmdcode):
    conn = http.client.HTTPConnection( lgtv["ipaddress"], port=8080)
    cmdText = "<?xml version=\"1.0\" encoding=\"utf-8\"?><command><session>" \
                + lgtv["session"]  \
                + "</session><type>HandleKeyInput</type><value>" \
                + cmdcode \
                + "</value></command>"
    conn.request("POST", "/hdcp/api/dtv_wifirc", cmdText, headers=headers)
    httpResponse = conn.getresponse()

Could it be missing some?

Hi,

The message is created in the send_command() method, and it works fine with the 2013 model. When I switch to the hdcp protocol I get an exception immediately. Looking at the log the URL is the same as in the lgcommander:

INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): 192.168.24.9
DEBUG:requests.packages.urllib3.connectionpool:"POST /hdcp/api/dtv_wifirc HTTP/1.1" 400 131
Traceback (most recent call last):
....

Please make sure you have the requests library installed (pip3 install requests) and start the tool with --verbose which logs all the requests set to your TV. Hope this helps…

Hey there.

Requests was already installed. Did an upgrade but that did not do anything either.added --verbose but seeing zero logs.

Help? :slight_smile:

Did complete new reinstall plus requirements

Then did a sudo python3 setup.py install installing pylgnetcast from the pylgnetcast-master.

After running below command you see the output

pi@Livingroom:~/.homeassistant/pylgnetcast-master$ clear
pi@Livingroom:~/.homeassistant/pylgnetcast-master$ sudo python3 -m pylgnetcast --host 192.168.0.104 --pairing_key XXXXXX  --protocol hdcp --command 24 --verbose
DEBUG:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): 192.168.0.104
DEBUG:requests.packages.urllib3.connectionpool:http://192.168.0.104:8080 "POST /hdcp/api/dtv_wifirc HTTP/1.1" 406 134
Traceback (most recent call last):
  File "/usr/lib/python3.4/runpy.py", line 170, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.4/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/pi/.homeassistant/pylgnetcast-master/pylgnetcast/__main__.py", line 61, in <module>
    sys.exit(main())
  File "/home/pi/.homeassistant/pylgnetcast-master/pylgnetcast/__main__.py", line 37, in main
    args.protocol) as client:
  File "/home/pi/.homeassistant/pylgnetcast-master/pylgnetcast/pylgnetcast.py", line 134, in __enter__
    self._session = self._get_session_id()
  File "/home/pi/.homeassistant/pylgnetcast-master/pylgnetcast/pylgnetcast.py", line 178, in _get_session_id
    raise SessionIdError('Can not get session id from TV.')
pylgnetcast.pylgnetcast.SessionIdError: Can not get session id from TV.
pi@Livingroom:~/.homeassistant/pylgnetcast-master$

Please also note on my 55LW5500 commands are

Channel up 0
Channel down 1
Volume up 2
Volume down 3

Good - at least an exception :wink:
I have made a fix for the get_session_id method and created release v0.3.1 - please give it a try.

Thanks for your TVs commands - it seems that the commands changed a bit between models. I guess I have to introduce some kind of mapping to call the correct commands. I will compile all the commands lists I can find on the net to support as many models as possible. If you could please complete the commands used by HA:

  • 1 (turn off)
  • 24 (volume up) -> 2
  • 25 (volume down) -> 3
  • 26 (mute)
  • 33 (play)
  • 34 (pause)
  • 36 (next track)
  • 37 (previous track)
    Your TV is a 2011 model - right?

Hey Wokar. Will give it a go. thx. :grin:

No luck still.

pi@Livingroom:~/.homeassistant/pylgnetcast-master$ sudo python3 setup.py install
running install
running bdist_egg
running egg_info
creating pylgnetcast.egg-info
writing dependency_links to pylgnetcast.egg-info/dependency_links.txt
writing pylgnetcast.egg-info/PKG-INFO
writing top-level names to pylgnetcast.egg-info/top_level.txt
writing manifest file 'pylgnetcast.egg-info/SOURCES.txt'
reading manifest file 'pylgnetcast.egg-info/SOURCES.txt'
writing manifest file 'pylgnetcast.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-armv7l/egg
running install_lib
running build_py
creating build
creating build/lib
creating build/lib/pylgnetcast
copying pylgnetcast/__main__.py -> build/lib/pylgnetcast
copying pylgnetcast/pylgnetcast.py -> build/lib/pylgnetcast
copying pylgnetcast/__init__.py -> build/lib/pylgnetcast
creating build/bdist.linux-armv7l
creating build/bdist.linux-armv7l/egg
creating build/bdist.linux-armv7l/egg/pylgnetcast
copying build/lib/pylgnetcast/__main__.py -> build/bdist.linux-armv7l/egg/pylgnetcast
copying build/lib/pylgnetcast/pylgnetcast.py -> build/bdist.linux-armv7l/egg/pylgnetcast
copying build/lib/pylgnetcast/__init__.py -> build/bdist.linux-armv7l/egg/pylgnetcast
byte-compiling build/bdist.linux-armv7l/egg/pylgnetcast/__main__.py to __main__.cpython-34.pyc
byte-compiling build/bdist.linux-armv7l/egg/pylgnetcast/pylgnetcast.py to pylgnetcast.cpython-34.pyc
byte-compiling build/bdist.linux-armv7l/egg/pylgnetcast/__init__.py to __init__.cpython-34.pyc
creating build/bdist.linux-armv7l/egg/EGG-INFO
copying pylgnetcast.egg-info/PKG-INFO -> build/bdist.linux-armv7l/egg/EGG-INFO
copying pylgnetcast.egg-info/SOURCES.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
copying pylgnetcast.egg-info/dependency_links.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
copying pylgnetcast.egg-info/not-zip-safe -> build/bdist.linux-armv7l/egg/EGG-INFO
copying pylgnetcast.egg-info/top_level.txt -> build/bdist.linux-armv7l/egg/EGG-INFO
creating dist
creating 'dist/pylgnetcast-0.3.1-py3.4.egg' and adding 'build/bdist.linux-armv7l/egg' to it
removing 'build/bdist.linux-armv7l/egg' (and everything under it)
Processing pylgnetcast-0.3.1-py3.4.egg
creating /usr/local/lib/python3.4/dist-packages/pylgnetcast-0.3.1-py3.4.egg
Extracting pylgnetcast-0.3.1-py3.4.egg to /usr/local/lib/python3.4/dist-packages
Removing pylgnetcast 0.3.0 from easy-install.pth file
Adding pylgnetcast 0.3.1 to easy-install.pth file

Installed /usr/local/lib/python3.4/dist-packages/pylgnetcast-0.3.1-py3.4.egg
Processing dependencies for pylgnetcast==0.3.1
Finished processing dependencies for pylgnetcast==0.3.1
pi@Livingroom:~/.homeassistant/pylgnetcast-master$ sudo python3 -m pylgnetcast --host 192.168.0.104 --protocol hdcp
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connection.py", line 138, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/util/connection.py", line 98, in create_connection
    raise err
  File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/util/connection.py", line 88, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connectionpool.py", line 594, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connectionpool.py", line 361, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python3.4/http/client.py", line 1090, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python3.4/http/client.py", line 1128, in _send_request
    self.endheaders(body)
  File "/usr/lib/python3.4/http/client.py", line 1086, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python3.4/http/client.py", line 924, in _send_output
    self.send(msg)
  File "/usr/lib/python3.4/http/client.py", line 859, in send
    self.connect()
  File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connection.py", line 163, in connect
    conn = self._new_conn()
  File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connection.py", line 147, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
requests.packages.urllib3.exceptions.NewConnectionError: <requests.packages.urllib3.connection.HTTPConnection object at 0x762931d0>: Failed to establish a new connection: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/requests/adapters.py", line 423, in send
    timeout=timeout
  File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/connectionpool.py", line 643, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/local/lib/python3.4/dist-packages/requests/packages/urllib3/util/retry.py", line 363, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
requests.packages.urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='192.168.0.104', port=8080): Max retries exceeded with url: /hdcp/api/dtv_wifirc (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x762931d0>: Failed to establish a new connection: [Errno 111] Connection refused',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.4/runpy.py", line 170, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.4/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/pi/.homeassistant/pylgnetcast-master/pylgnetcast/__main__.py", line 61, in <module>
    sys.exit(main())
  File "/home/pi/.homeassistant/pylgnetcast-master/pylgnetcast/__main__.py", line 37, in main
    args.protocol) as client:
  File "/home/pi/.homeassistant/pylgnetcast-master/pylgnetcast/pylgnetcast.py", line 134, in __enter__
    self._session = self._get_session_id()
  File "/home/pi/.homeassistant/pylgnetcast-master/pylgnetcast/pylgnetcast.py", line 172, in _get_session_id
    self._display_pair_key()
  File "/home/pi/.homeassistant/pylgnetcast-master/pylgnetcast/pylgnetcast.py", line 186, in _display_pair_key
    self._send_to_tv('auth', self.KEY)
  File "/home/pi/.homeassistant/pylgnetcast-master/pylgnetcast/pylgnetcast.py", line 195, in _send_to_tv
    timeout=DEFAULT_TIMEOUT)
  File "/usr/local/lib/python3.4/dist-packages/requests/api.py", line 110, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.4/dist-packages/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/requests/adapters.py", line 487, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='192.168.0.104', port=8080): Max retries exceeded with url: /hdcp/api/dtv_wifirc (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x762931d0>: Failed to establish a new connection: [Errno 111] Connection refused',))

The commands you seek are these:

Supported Models Information
LCD/LED TV LZ9800, LZ970*, LZ9700, LZ9600\nLW9600, LW950*, LW9500, LW770*, LW7700, LW7500, \nLW650*, LW6500, LW570*, LW5700, LW5600, LW550*, LW5500\nLV570*, LV5700, 
LV550W, LV550T, LV5500(except size 26&apos;&apos;, 22&apos;&apos;, and Hong Kong), LV5400\nLV375*, LV373S, LV372S, LV3700\n\nNorth America Only \nLK550, LK530\n\n\n- 
PDP TV\nKorea, Australia, New Zealand, Asia\nPZ950, PZ570\n\nCentral Asia\nPZ950\n\nNorth America\nPZ950, PZ750, PZ550, PZ540\n\nLatin America\nPZ950\nPZ570 (Brazil, 
Peru, Chile Only)\n\nEurope\nPZ950, PZ750

key_code_power_on_off = 8
key_code_num_0 = 16
key_code_num_1 = 17
key_code_num_2 = 18
key_code_num_3 = 19
key_code_num_4 = 20
key_code_num_5 = 21
key_code_num_6 = 22
key_code_num_7 = 23
key_code_num_8 = 24
key_code_num_9 = 25
key_code_mute = 9
key_code_menu = 67
key_code_dash = 76
key_code_flashback = 26
key_code_chlist = 83
key_code_enter = 68
key_code_ch_up = 0
key_code_ch_down = 1
key_code_vol_up = 2
key_code_vol_down = 3
key_code_arrow_up = 64
key_code_arrow_down = 65
key_code_arrow_left = 7
key_code_arrow_right = 6
key_code_return = 40
key_code_exit_cancel = 91
key_code_confirm = 68
key_code_cursor_ok = 2
key_code_quick_menu = 69
key_code_red_button = 114
key_code_green_button = 113
key_code_yellow_button = 99
key_code_blue_button = 97
key_code_mm_live_tv = 158
key_code_mm_stop = 177
key_code_mm_play = 176
key_code_mm_pause = 186
key_code_mm_skip_backward = 143
key_code_mm_skip_forward = 142
key_code_mm_record = 189
key_code_epg = 169
key_code_energy_saving = 149
key_code_av_mode = 48
key_code_input = 11
key_code_fav = 30
key_code_simplink = 126
key_code_ratio = 121
key_code_info = 170
key_code_netcast = 89
key_code_guide = 169
key_code_subtitle = 57
key_code_teletext = 32
key_code_t_opt = 33
key_code_ad = 145

Looks like lg_netcast is still using 0.2.0 and not the 0.3.1?

Seems to be a minor change to support the latest version and supply an additional protocol parameter?

Is that all I need to do? Or do I also need to map the commands across? I think the commands are handled in pylgnetcast.

Cheers,

Has there been any update on this? I have a 2011 LG 42LV5400 with Netcast.
I know @dennisaion created a workaround here: [WORKAROUND] LG TV Remote but I am using the latest Hass.io 0.64.3 and the netcast component doesn’t work for me. Any way to get around this? I tried the CEC component and the kodi JSON-RPC CEC, which is the closest I’ve gotten but works only to turn on the TV. Thanks so much.

Hope to add support for 42LM7600 too.

1 Like

For all others searching for LG Netcast TV (pre 2012 models) component: I rewrote the component and overrided the original one (used for 2012+ models). Everything is working except the turning the TV on (Power Off is working well), because oldest models does not support wake on lan. I also disabled the power button when the TV is off. There is HDMI CEC option to power the TV, but it is a different question with different answer. There is a functionality to switch to concrete channel. In order this to happen you should add all channels you want in to the FAV list (it doesn’t matter which - A,B,C or D). All channels you want to be able to switchto directly (without arrows, next and prev buttons of the player) should be added in one of the FAV lists.

Edit: Sorry for the external links to the files, but I have not enough time to format it with the preformatted text button (</>). If the text on the links is in Russian, like it is when I open them, press the “Скачать” labeled link (download in Russian :slight_smile: ).

Steps to do it yourself:

  1. In your config folder create a new folder called custom_components

  2. In the custom_components folder create a new folder called media_player

  3. In the media_player folder create a new file called lg_netcast.py

  4. Add the following code in to the file lg_netcast.py
    lg_netcast.py (customised)

  5. In your config folder create a new folder called www and inside of it upload a picture called livetv.jpg - this image will be displayed when the TV is ON
    livetv.jpg (from webos)

  6. In your configuration.yaml add your TV as this:

     media_player:
     - platform: lg_netcast
       host: 192.168.1.101
       access_token: DLHSLF
       name: Kitchen TV
    

Change the host with the IP of your TV (you can see it from the ‘network’ menu or the in your router). Leaving blank access_token should show your access token on the TV then fill it on the place of mine. Write the name of your TV and save the configuration.yaml
Finally restart home assistant.

Maybe this is the place to thank to @dennisaion for helping me to understand some python script things and for the list of the wi-fi remote codes … :slight_smile:

1 Like

No probleem :grin:

UPDATED!!! 13.12.2018

lg_netcast.py (customised) 13.12.2018

The links in previous post are updated too.

I implemented an optional ‘turn_on_action’ in case you can find a way to wake up your TV somehow. I found an option for me - I bought a cheap hdmi miracast device and Sonoff SV Switch (with tasmota uploaded), then I made the following connection:
Phone Charger (With USB output) -> Sonoff SV -> HDMI Miracast -> TV HDMI
I added the Sonnof SV Switch to HA config and named it ‘Netcast TV Switch’, this way its entity id is: switch.netcast_tv_switch
After that I added turn_on_action part to the end. With the latest edition if you add it, the power on button of the media player will be available and will call the service you’ve populated (Idea: it could be even a service call to IR Blaster to turn the TV on). One notice - if you make it like me, with HDMI CEC device (the miracast for example), you will have to write an automation to turn the device off after 8 or 9 seconds in order to avoid further switching to the HDMI input of the waking up HDMI device itself. This way after the TV is powered on it will switch to the latest source used.

media_player:
  - platform: lg_netcast
    host: 192.168.1.101
    access_token: DLHSLF
    name: Kitchen TV
    turn_on_action:
      service: homeassistant.turn_on
      data:
        entity_id: switch.netcast_tv_switch

The script and automation from my use case (with the Sonoff SV and HDMI Miracast device) to auto turn off the switch after 8 seconds:

scripts.yaml entry:

kitchen_tv_turn_off_timer:
    alias: "Kitchen TV turn off timer"
    sequence:
        - delay: '00:00:08'
        - service: homeassistant.turn_off
          data:
              entity_id: switch.netcast_tv_switch

automations.yaml entry:

- alias: "Kitchen TV switch auto turn off"
  trigger:
      - platform: state
        entity_id: switch.netcast_tv_switch
        to: 'on'
  action:
      - service: script.turn_on
        entity_id: script.kitchen_tv_turn_off_timer

Link to the exact brand and model of the Miracast HDMI device, that I have and I know it is working for the purpose of auto waking up the TV via HDMI-CEC

Link to the Sonoff SV device. That was the best price I’ve found before 3 months. Now there might be even cheaper offer…

IMPORTANT: If you want to make the same power on scheme (with the HDMI Miracast and Sonoff SV) you will need SOLDERING SKILLS AND SOLDERING IRON. Also you’ll need one USB to MicroUSB cable, that you’ll have to cut in to pieces in order to solder it to the input and the output of the Sonoff SV board (the red wire is +5V, the black wire is Ground, White/Yellow is Data + and not needed, the green one is Data - and not needed too. See the image below.) . The cable part with the USB end has to be solder to the Sonoff SV input and the part with the MircoUSB has to be soldered to the Sonoff SV output. If you choose different HDMI device, please check what USB cable you will need.

USB-cable-wiring1