How To Use HomeAssistant to update TP-Link (KASA) devices

Background:
Most (if not all) IoT vendors have their devices phone home and will likely auto update the device’s firmware to a newer version if available. In most cases, these firmware updates do not require consent from the user. Vendors are also known to restrict or out right disable functionality of devices with new firmware releases. This guide is to show how TP-Link (KASA) IoT devices can be updated while being blocked from accessing the internet and without the KASA App.

Requirements:

  • Home Assistant is running with local folder access (www) enabled (you can search the forum on how to enable local folder access)
  • HTTPS access disabled in Home Assistant (this can be done temporarily)
  • Python modules for accessing the TP-Link device. This guide will use tplink-smartplug
  • TP-Link devices are able to connect to Home Assistant

EDIT
Rather than using the HASS as the web server, it’s somewhat simpler to use the available Python web server module.

Steps:

  1. Download the required firmware from TP-Link. Eg: smartPlug_FCC_HS105US_1.5.4_Build_181224_Rel._1549088397494.bin.
  2. Copy (or move) the firmware to the Home Assistant www directory.
  3. Verify that the file can be downloaded from the Home Assistant url http://homeassistant:8123/local/smartPlug_FCC_HS105US_1.5.4_Build_181224_Rel._1549088397494.bin
  4. Using tplink_smartplug.py, connect to the TP-Link device:
./tplink_smartplug.py -t <device IP> -c info
Sent:      {"system":{"get_sysinfo":{}}}
Received:  {"system":{"get_sysinfo":{"sw_ver":"1.0.7 Build 210407 Rel.173024","hw_ver":"2.0","model":"HS220(US)"....}
  1. Download the firmware to the TP-Link device:
./tplink_smartplug.py -t <device IP> -j '{"system":{"download_firmware":{"url":"http://homeassistant:8123/local/HS220_FCC_HS220US_1.0.7_Build_210407_Rel.173024_2021-04-07_17.56.35.bin"}}}'

The device will apply the firmware automatically once the download completes.
6. Check the download status:

./tplink_smartplug.py -t <device IP> -j '{"system":{"get_download_state":{}}}
Sent:      {"system":{"get_download_state":{}}}
Received:  {"system":{"get_download_state":{"status":0,"ratio":0,"reboot_time":5,"flash_time":5,"err_code":0}}}

status seems to go from 0 - 6 - I do not know the meaning of the values
ratio seems to go from 0 - 100 - I assume this is the download progress
7. Verify the new firmware was successfully applied:

./tplink_smartplug.py -t <device IP> -c info
Sent:      {"system":{"get_sysinfo":{}}}
Received:  {"system":{"get_sysinfo":{"sw_ver":"1.0.7 Build 210407 Rel.173024","hw_ver":"2.0","model":"HS220(US)"....}
  1. Done

NOTE: The firmware for the HS220USv2 listed in this guide was downloaded from here which was found in the TP-Link forum.
The HS105US link was taken from a paper by Andrew Halterman

4 Likes

Hi,

Can you please advise on how to install/use these python scripts?

I have managed to run the script, but the when I am trying to connect to the device, it says 'could not connect to host ‘ip address’ … pls advise

@sominaik Is this the first time setting up your tplink/kasa device(s)? To use the script as indicated above, your device must already be connected with an IP assigned.

If you are connecting the device for the first time, see the readme in the git repo for commands to connect your tplink/kasa devices to your network.

Hi @hass , thanks for the response. No, it’s not the first time.
These devices were setup previously and have been working flawlessly with HA. Recently, they have stopped working (2-3 out of 10) most likely after the firmware upgrade etc. Pls advise

@sominaik In order to connect with the python tool, the computer needs to be on a network with access to the tplink devices (ideally, on the same network). If HA cannot connect to your tplink devices, it’s unlikely the python script will work (HA uses the same python module).

Firstly, try connection to one of the 7 working tplink devices that’s visible in HA. You can check the firmware of the working devices using:

tplink_smartplug.py -t <IP Address> -c info

Hi @hass , Yes I am able to connect to other working tplink devices, just that I am unable to connect to the ones which have stopped working in HA … btw, these 2-3 devices are still controllable via the app

I am not familiar with the app, but you will likely have to reach out to tplink support to push the firmware to the 2-3 devices to enable local control. Here is the TP-Link Support link. Unfortunately, without local control, the python script and HA will not work.