home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.22k stars 30.58k forks source link

TP-Link switch stopped working after upgrade to 2023.7.0 (fixed in 2023.7.1) #95938

Closed wzaatar closed 1 year ago

wzaatar commented 1 year ago

Please read before posting

If you are seeing the problem, please capture a wire shark or tcpdump and post it in this issue.

If you have a different model than the ones listed below please post the model

Otherwise give this issue a 👍 instead of posting to let us know you see the issue.

This will keep the issue nice and clean

The problem

After upgrading to 2023.7.0, the following errors started appearing in the log. Below devices are pingable. I also rebooted them, restarted HA. The same error appears again.

What version of Home Assistant Core has the issue?

core-2023.7.0

What was the last working version of Home Assistant Core?

core-2023.6.x

What type of installation are you running?

Home Assistant OS

Integration causing the issue

TP Link Kasa Smart

Link to integration documentation on our website

https://www.home-assistant.io/integrations/tplink/

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

Logger: homeassistant.config_entries
Source: config_entries.py:1257`
First occurred: 5:14:18 PM (18 occurrences) 
Last logged: 5:35:35 PM

Config entry '**MASKED**' for tplink integration not ready yet: Unable to query the device 172.16.13.53: unexpected end of data: line 1 column 434 (char 433); Retrying in background
Config entry '**MASKED**' for tplink integration not ready yet: Unable to query the device 172.16.13.66: unexpected end of data: line 1 column 435 (char 434); Retrying in background
Config entry '**MASKED**' for tplink integration not ready yet: Unable to query the device 172.16.13.67: unexpected end of data: line 1 column 485 (char 484); Retrying in background
Config entry '**MASKED**' for tplink integration not ready yet: Unable to query the device 172.16.13.64: unexpected end of data: line 1 column 552 (char 551); Retrying in background
Config entry ' **MASKED**' for tplink integration not ready yet: Unable to query the device 172.16.13.63: unexpected end of data: line 1 column 325 (char 324); Retrying in background

Additional information

No response

home-assistant[bot] commented 1 year ago

Hey there @rytilahti, @thegardenmonkey, mind taking a look at this issue as it has been labeled with an integration (tplink) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `tplink` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign tplink` Removes the current integration label and assignees on the issue, add the integration domain after the command.

(message by CodeOwnersMention)


tplink documentation tplink source (message by IssueLinks)

geekofweek commented 1 year ago

Noticed the same problem, seems to be an issue introduced with python-kasa 0.5.2. Rolling that back to 0.5.1 and the problem went away

wzaatar commented 1 year ago

Noticed the same problem, seems to be an issue introduced with python-kasa 0.5.2. Rolling that back to 0.5.1 and the problem went away

Possible with HA OS?

bdraco commented 1 year ago

Which models are the devices that stopped working?

Is it all the same model?

Do some of the same model work and some don't?

geekofweek commented 1 year ago

In my situation it was both KP115 devices. EP40 and HS300 were not impacted and still functioned. using the kasa-python command line I would get the following errors trying to discover devices:

Discovering devices on 255.255.255.255 for 3 seconds
ERROR    Exception in callback _SelectorDatagramTransport._read_ready()                                                                                                                           base_events.py:1771
         handle: <Handle _SelectorDatagramTransport._read_ready()>                                                                                                                                                   
         Traceback (most recent call last):                                                                                                                                                                          
           File "/usr/local/lib/python3.11/asyncio/events.py", line 80, in _run                                                                                                                                      
             self._context.run(self._callback, *self._args)                                                                                                                                                          
           File "/usr/local/lib/python3.11/asyncio/selector_events.py", line 1169, in _read_ready                                                                                                                    
             self._protocol.datagram_received(data, addr)                                                                                                                                                            
           File "/usr/local/lib/python3.11/site-packages/kasa/discover.py", line 79, in datagram_received                                                                                                            
             info = json_loads(TPLinkSmartHomeProtocol.decrypt(data))                                                                                                                                                
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                
         orjson.JSONDecodeError: unexpected end of data: line 1 column 382 (char 381)                                                                                                                                
ERROR    Exception in callback _SelectorDatagramTransport._read_ready()                                                                                                                           base_events.py:1771
         handle: <Handle _SelectorDatagramTransport._read_ready()>                                                                                                                                                   
         Traceback (most recent call last):                                                                                                                                                                          
           File "/usr/local/lib/python3.11/asyncio/events.py", line 80, in _run                                                                                                                                      
             self._context.run(self._callback, *self._args)                                                                                                                                                          
           File "/usr/local/lib/python3.11/asyncio/selector_events.py", line 1169, in _read_ready                                                                                                                    
             self._protocol.datagram_received(data, addr)                                                                                                                                                            
           File "/usr/local/lib/python3.11/site-packages/kasa/discover.py", line 79, in datagram_received                                                                                                            
             info = json_loads(TPLinkSmartHomeProtocol.decrypt(data))    
wzaatar commented 1 year ago

Which models are the devices that stopped working?

Is it all the same model?

Do some of the same model work and some don't?

3rd answer in my case. Some devices of a certain model work, others don’t.

bdraco commented 1 year ago

Thanks. Ordered a KP115. Should be here early next week. Will see if I can figure out what's going wrong

bdraco commented 1 year ago

I bet it's a null/0 byte in the encrypted data and it's prematurely truncating the string in the conversion.

If you can get a wire shark dump of it happening I can probably fix it without waiting for the device

bdraco commented 1 year ago

Tcpdump or any type of pcap will work as well

wzaatar commented 1 year ago

@bdraco This is not only related to KP115. I have other switches that are behaving the same way, specificallyHS100, HS200, HS220.

bdraco commented 1 year ago

Just need one to trigger the issue. Any of the ones not working with the same symptom should probably be enough to find the issue

bdraco commented 1 year ago

But thanks for answering my original question and providing the model numbers

redrob49 commented 1 year ago

I have 6 Kasa KP125 smart plugs. After upgrading to 2023.7 one of the plugs stopped working and gave me a setup error in Home Assistant. Works without issue in 2023.6.3. All KP125 are the same with hardware version 1.0 and firmware version 1.0.10

bdraco commented 1 year ago

I have 6 Kasa KP125 smart plugs. After upgrading to 2023.7 one of the plugs stopped working and gave me a setup error in Home Assistant. Works without issue in 2023.6.3. All KP125 are the same with hardware version 1.0 and firmware version 1.0.10

If you rename that device in the tplink app does it fix it?

wzaatar commented 1 year ago

@geekofweek suggested that a rollback to python-kasa 0.5.1 fixed it for him. Maybe a code diff?

bdraco commented 1 year ago

It's likely a problem with the encryption library changes. Rolling back should fix it. Ideally we find source the problem instead of reverting it right away because it just means it will come back next time we upgrade the library.

keith721 commented 1 year ago

Same problem here, with one HS105 outlet and two separate HS200 switches.

bdraco commented 1 year ago

Should be fixed in https://github.com/bdraco/kasa-crypt/pull/3 if someone wants to install that and test

bdraco commented 1 year ago

I pushed that change to my production system and all is well. All was well before though.

Can't test it in person since I'm on an airplane still for another few hours

wzaatar commented 1 year ago

I pushed that change to my production system and all is well. All was well before though.

Can't test it in person since I'm on an airplane still for another few hours

Dedication has no limits! Thanks for your prompt response.

pyrodex commented 1 year ago

Should be fixed in bdraco/kasa-crypt#3 if someone wants to install that and test

Is there a quick way to pull this into an existing HA docker setup? I have a few broken devices but it is weird, its not affecting all my Kasa devices just two EP10s and ironically I have others that are fine....

bdraco commented 1 year ago

Should be fixed in bdraco/kasa-crypt#3 if someone wants to install that and test

Is there a quick way to pull this into an existing HA docker setup? I have a few broken devices but it is weird, its not affecting all my Kasa devices just two EP10s and ironically I have others that are fine....

Check out the PR in the container, and do a pip install on it. You'll need to remount /tmp to allow it to execute first

pyrodex commented 1 year ago

Should be fixed in bdraco/kasa-crypt#3 if someone wants to install that and test

Is there a quick way to pull this into an existing HA docker setup? I have a few broken devices but it is weird, its not affecting all my Kasa devices just two EP10s and ironically I have others that are fine....

Never mind, I manually brought in the changes and still seeing issues with the SAME two after that. All my other KASA devices are working and reporting. Just not this two...

bdraco commented 1 year ago

I'll do a release of the lib after I finish manual testing. Than you can install the wheel in the container. That will be a lot easier

wzaatar commented 1 year ago

Piggybacking on @pyrodex‘s question: Any temporary fix for HA OS installs?

bdraco commented 1 year ago

0.2.1 published

You can install it with the advanced ssh addon with protection mode disabled if you are using x86_64

docker exec -it homeassistant /bin/bash
pip3 install --upgrade kasa-crypt==0.2.1

Than restart ha

bdraco commented 1 year ago

For other architectures not x86_64

Wait for https://github.com/home-assistant/core/actions/runs/5470867696 to finish and run the below:

docker exec -it homeassistant /bin/bash
pip3 install --find-links=$WHEELS_LINKS --upgrade kasa-crypt==0.2.1
keith721 commented 1 year ago

0.2.1 published

You can install it with the advanced ssh addon with protection mode disabled if you are using x86_64

docker exec -it homeassistant /bin/bash
pip3 install --upgrade kasa-crypt==0.2.1

Than restart ha

HAOS 10.2 here on Intel Nuc i3 Applied kasa-crypt==0.2.1 and restarted Home Assistant. My three devices are not initializing (still). Log content below

2023-07-05 22:32:11.845 WARNING (MainThread) [homeassistant.config_entries] Config entry 'Cable Modem HS105(US)' for tplink integration not ready yet: Unable to connect to the device: 192.168.1.112: ; Retrying in background
2023-07-05 22:32:11.875 WARNING (MainThread) [homeassistant.config_entries] Config entry 'Front Porch' for tplink integration not ready yet: Unable to connect to the device: 192.168.1.119: ; Retrying in background
2023-07-05 22:32:11.883 WARNING (MainThread) [homeassistant.config_entries] Config entry 'Garage Lights' for tplink integration not ready yet: Unable to connect to the device: 192.168.1.245: ; Retrying in background
bdraco commented 1 year ago

Unable to connect to the device: 192.168.1.119

@keith721 That is not the same issue as the OP posted so I wouldn't expect the new library to resolve the problem.

Please start a new issue. It might be the same as https://github.com/home-assistant/core/issues/95028 though

redrob49 commented 1 year ago

Renaming the device in the uplink app does not fix it. Going back to 2023.6.3 fixes it.

On Jul 5, 2023, at 4:36 PM, J. Nick Koston @.***> wrote:

I have 6 Kasa KP125 smart plugs. After upgrading to 2023.7 one of the plugs stopped working and gave me a setup error in Home Assistant. Works without issue in 2023.6.3. All KP125 are the same with hardware version 1.0 and firmware version 1.0.10

If you rename that device in the tplink app does it fix it?

— Reply to this email directly, view it on GitHub https://github.com/home-assistant/core/issues/95938#issuecomment-1622685806, or unsubscribe https://github.com/notifications/unsubscribe-auth/A5J55L2NDUZKI2X4J2NJFILXOX3AXANCNFSM6AAAAAAZ7QL76M. You are receiving this because you commented.

keith721 commented 1 year ago

Understood, but doesn't the encryption/decryption play a role in successfully connecting to these TP-Link devices? They are all on-line, and I can ping them. If my post isn't relevant, please feel free to remove or strike-through it.

Thanks for your speedy efforts!! I'm rolling back to 2023.6.3...

bdraco commented 1 year ago

Understood, but doesn't the encryption/decryption play a role in successfully connecting to these TP-Link devices? They are all on-line, and I can ping them. If my post isn't relevant, please feel free to remove or strike-through it.

Thanks for your speedy efforts!! I'm rolling back to 2023.6.3...

The encryption only happens after the connection is established. If the connection cannot be made the encryption can't be the problem

wzaatar commented 1 year ago

I can confirm that kasa-crypt 0.2.1 works perfectly on HA OS / HA Blue ODROID. All switches are now responding. Kudos @bdraco!

pyrodex commented 1 year ago

Yea, the pip worked without issues now.

bdraco commented 1 year ago

The dep isn't pinned in HA so the next HA release will automatically build with the new version

dev-tty commented 1 year ago

Thank you for the fix.

Had the same problem on a mix of 15 devices:

HS103(US)
Firmware: 1.1.4 Build 210409 Rel.113427
Hardware: 2.1

HS100(US)
Firmware: 1.2.6 Build 200727 Rel.120528
Hardware: 1.0

HS200(US)
Firmware: 1.1.3 Build 191205 Rel.103557

No problems with the:

KP400(US)
Firmware: 1.0.7 Build 210428 Rel.080330
Hardware: 2.0

HS220(US)
Firmware: 1.0.8 Build 210423 Rel.075507
Hardware: 2.0
bdraco commented 1 year ago

Closing this issue as a solution is available and 2023.7.1 is shipping in a few hours via https://github.com/home-assistant/core/pull/96006