Skip to content

KL430 causing "non-hexadecimal number found in fromhex() arg at position 2" error in smartdevice.py #159

@Roddd

Description

@Roddd

Read up a little on the KL430 support and found this repo. Tried it out with following environment:

QNAP NAS with Container Station
Home Assistant 2021.4.6 docker container
Alpine Linux 3.12
Python 3.8.7
pip 20.2.4

python-kasa was installed with pip. I have two kasa devices, a smart socket and the KL430 LED strip. The discovery was getting stuck like below:

bash-5.0# kasa                                                                                                                                                                                                                                                                                                                                                                                                                       
No host name given, trying discovery..                                                                                                                                                                                                                                                                                                                                                                                               
Discovering devices on 255.255.255.255 for 3 seconds                                                                                                                                                                                                                                                                                                                                                                                 
== Computer Room Light - HS103(US) ==                                                                                                                                                                                                                                                                                                                                                                                                
        Host: 192.168.1.92                                                                                                                                                                                                                                                                                                                                                                                                           
        Device state: ON                                                                                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                                                                                                                                                     
        == Generic information ==                                                                                                                                                                                                                                                                                                                                                                                                    
        Time:         2021-05-01 22:39:51                                                                                                                                                                                                                                                                                                                                                                                            
        Hardware:     2.1                                                                                                                                                                                                                                                                                                                                                                                                            
        Software:     1.1.3 Build 200804 Rel.095135                                                                                                                                                                                                                                                                                                                                                                                  
        MAC (rssi):   D8:07:B6:AA:06:CC (-36)                                                                                                                                                                                                                                                                                                                                                                                        
        Location:     {'latitude': <this is>, 'longitude': <my house>}                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                                                                     
        == Device specific information ==                                                                                                                                                                                                                                                                                                                                                                                            
        LED state: True                                                                                                                                                                                                                                                                                                                                                                                                              
        On since: 2021-05-02 04:21:39.587177                                                                                                                                                                                                                                                                                                                                                                                         
                                                                                                                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                                                                                                                     
== Test Light - KL430(US) ==                                                                                                                                                                                                                                                                                                                                                                                                         
        Host: 192.168.1.93                                                                                                                                                                                                                                                                                                                                                                                                           
        Device state: ON                                                                                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                                                                                                                                                     
        == Generic information ==                                                                                                                                                                                                                                                                                                                                                                                                    
        Time:         None                                                                                                                                                                                                                                                                                                                                                                                                           
        Hardware:     2.0                                                                                                                                                                                                                                                                                                                                                                                                            
        Software:     1.0.8 Build 210121 Rel.084339
Traceback (most recent call last):                                                                                                                                                                                                                                                                                                                                                                                                   
  File "/usr/local/bin/kasa", line 8, in <module>                                                                                                                                                                                                                                                                                                                                                                                    
    sys.exit(cli())                                                                                                                                                                                                                                                                                                                                                                                                                  
  File "/usr/local/lib/python3.8/site-packages/asyncclick/core.py", line 862, in __call__                                                                                                                                                                                                                                                                                                                                            
    return anyio.run(self._main, main, args, kwargs, **({"backend":_anyio_backend} if _anyio_backend is not None else {}))                                                                                                                                                                                                                                                                                                           
  File "/usr/local/lib/python3.8/site-packages/anyio/_core/_eventloop.py", line 55, in run                                                                                                                                                                                                                                                                                                                                           
    return asynclib.run(func, *args, **backend_options)  # type: ignore                                                                                                                                                                                                                                                                                                                                                              
  File "/usr/local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 230, in run                                                                                                                                                                                                                                                                                                                                        
    return native_run(wrapper(), debug=debug)                                                                                                                                                                                                                                                                                                                                                                                        
  File "/usr/local/lib/python3.8/asyncio/runners.py", line 44, in run                                                                                                                                                                                                                                                                                                                                                                
    return loop.run_until_complete(main)                                                                                                                                                                                                                                                                                                                                                                                             
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete                                                                                                                                                                                                                                                                                                                                            
    return future.result()                                                                                                                                                                                                                                                                                                                                                                                                           
  File "/usr/local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 225, in wrapper                                                                                                                                                                                                                                                                                                                                    
    return await func(*args)                                                                                                                                                                                                                                                                                                                                                                                                         
  File "/usr/local/lib/python3.8/site-packages/asyncclick/core.py", line 865, in _main                                                                                                                                                                                                                                                                                                                                               
    return await main(*args, **kwargs)                                                                                                                                                                                                                                                                                                                                                                                               
  File "/usr/local/lib/python3.8/site-packages/asyncclick/core.py", line 814, in main                                                                                                                                                                                                                                                                                                                                                
    rv = await self.invoke(ctx)                                                                                                                                                                                                                                                                                                                                                                                                      
  File "/usr/local/lib/python3.8/site-packages/asyncclick/core.py", line 1272, in invoke                                                                                                                                                                                                                                                                                                                                             
    return await Command.invoke(self, ctx)                                                                                                                                                                                                                                                                                                                                                                                           
  File "/usr/local/lib/python3.8/site-packages/asyncclick/core.py", line 1102, in invoke                                                                                                                                                                                                                                                                                                                                             
    return await ctx.invoke(self.callback, **ctx.params)                                                                                                                                                                                                                                                                                                                                                                             
  File "/usr/local/lib/python3.8/site-packages/asyncclick/core.py", line 644, in invoke                                                                                                                                                                                                                                                                                                                                              
    rv = await rv                                                                                                                                                                                                                                                                                                                                                                                                                    
  File "/usr/local/lib/python3.8/site-packages/kasa/cli.py", line 72, in cli                                                                                                                                                                                                                                                                                                                                                         
    await ctx.invoke(discover)                                                                                                                                                                                                                                                                                                                                                                                                       
  File "/usr/local/lib/python3.8/site-packages/asyncclick/core.py", line 644, in invoke                                                                                                                                                                                                                                                                                                                                              
    rv = await rv                                                                                                                                                                                                                                                                                                                                                                                                                    
  File "/usr/local/lib/python3.8/site-packages/kasa/cli.py", line 188, in discover                                                                                                                                                                                                                                                                                                                                                   
    await ctx.invoke(state)                                                                                                                                                                                                                                                                                                                                                                                                          
  File "/usr/local/lib/python3.8/site-packages/asyncclick/core.py", line 644, in invoke                                                                                                                                                                                                                                                                                                                                              
    rv = await rv                                                                                                                                                                                                                                                                                                                                                                                                                    
  File "/usr/local/lib/python3.8/site-packages/asyncclick/core.py", line 644, in invoke                                                                                                                                                                                                                                                                                                                                              
    rv = await rv                                                                                                                                                                                                                                                                                                                                                                                                                    
  File "/usr/local/lib/python3.8/site-packages/kasa/cli.py", line 252, in state                                                                                                                                                                                                                                                                                                                                                      
    click.echo(f"\tMAC (rssi):   {dev.mac} ({dev.rssi})")                                                                                                                                                                                                                                                                                                                                                                            
  File "/usr/local/lib/python3.8/site-packages/kasa/smartdevice.py", line 115, in wrapped                                                                                                                                                                                                                                                                                                                                            
    return f(*args, **kwargs)                                                                                                                                                                                                                                                                                                                                                                                                        
  File "/usr/local/lib/python3.8/site-packages/kasa/smartdevice.py", line 412, in mac                                                                                                                                                                                                                                                                                                                                                
    format(s, "02x") for s in bytes.fromhex(sys_info["mic_mac"])                                                                                                                                                                                                                                                                                                                                                                     
ValueError: non-hexadecimal number found in fromhex() arg at position 2

I took a look at error and code and I think it's because the value is already in the proper hex format which is causing it to bug out. It is expecting a MAC that probably looks like "AB CD EF 12 34 56" but it is getting "AB:CD:EF:12:34:56" from mic_mac. I modified a few lines to have it print out what it was seeing and it told me this:

bash-5.0# kasa discover                                                                                                                                                                                                                                                                                                                                                                                                              
Discovering devices on 255.255.255.255 for 3 seconds                                                                                                                                                                                                                                                                                                                                                                                 
== Test Light - KL430(US) ==                                                                                                                                                                                                                                                                                                                                                                                                         
        Host: 192.168.1.93                                                                                                                                                                                                                                                                                                                                                                                                           
        Device state: ON                                                                                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                                                                                                                                                     
        == Generic information ==                                                                                                                                                                                                                                                                                                                                                                                                    
        Time:         None                                                                                                                                                                                                                                                                                                                                                                                                           
        Hardware:     2.0                                                                                                                                                                                                                                                                                                                                                                                                            
        Software:     1.0.8 Build 210121 Rel.084339                                                                                                                                                                                                                                                                                                                                                                                  
{'sw_ver': '1.0.8 Build 210121 Rel.084339', 'hw_ver': '2.0', 'model': 'KL430(US)', 'deviceId': '80124E149F74D26791659E9E79C48E7D1DCBD580', 'oemId': '0D8D030103F88173CD7D202D63137DF3', 'hwId': '5F8CB8CB51F43A1C335C4D97FB33532B', 'rssi': -34, 'lati                                                                                                                                                                               
tude_i': <redacted>, 'longitude_i': <cuz my house>, 'alias': 'Test Light', 'status': 'new', 'description': 'Kasa Smart Light Strip, Multicolor', 'mic_type': 'IOT.SMARTBULB', 'mic_mac': 'C0:C9:E3:4B:72:61', 'dev_state': 'normal', 'is_factory': False, 'disco                                                                                                                                                                               
_ver': '1.0', 'ctrl_protocols': {'name': 'Linkie', 'version': '1.0'}, 'active_mode': 'none', 'is_dimmable': 1, 'is_color': 1, 'is_variable_color_temp': 1, 'LEF': 1, 'length': 16, 'light_state': {'on_off': 1, 'mode': 'normal', 'hue': 194, 'saturat                                                                                                                                                                               
ion': 50, 'color_temp': 0, 'brightness': 53}, 'lighting_effect_state': {'enable': 1, 'name': 'Ocean', 'custom': 0, 'id': 'oJjUMosgEMrdumfPANKbkFmBcAdEQsPy', 'brightness': 30}, 'preferred_state': [], 'err_code': 0}                                                                                                                                                                                                                
Traceback (most recent call last):                                                                                                                                                                                                                                                                                                                                                                                                   
  File "/usr/local/bin/kasa", line 8, in <module>                                                                                                                                                                                                                                                                                                                                                                                    
    sys.exit(cli())                                                                                                                                                                                                                                                                                                                                                                                                                  
  File "/usr/local/lib/python3.8/site-packages/asyncclick/core.py", line 862, in __call__                                                                                                                                                                                                                                                                                                                                            
    return anyio.run(self._main, main, args, kwargs, **({"backend":_anyio_backend} if _anyio_backend is not None else {}))                                                                                                                                                                                                                                                                                                           
  File "/usr/local/lib/python3.8/site-packages/anyio/_core/_eventloop.py", line 55, in run                                                                                                                                                                                                                                                                                                                                           
    return asynclib.run(func, *args, **backend_options)  # type: ignore                                                                                                                                                                                                                                                                                                                                                              
  File "/usr/local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 230, in run                                                                                                                                                                                                                                                                                                                                        
    return native_run(wrapper(), debug=debug)                                                                                                                                                                                                                                                                                                                                                                                        
  File "/usr/local/lib/python3.8/asyncio/runners.py", line 44, in run                                                                                                                                                                                                                                                                                                                                                                
    return loop.run_until_complete(main)                                                                                                                                                                                                                                                                                                                                                                                             
  File "/usr/local/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete                                                                                                                                                                                                                                                                                                                                            
    return future.result()                                                                                                                                                                                                                                                                                                                                                                                                           
  File "/usr/local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 225, in wrapper                                                                                                                                                                                                                                                                                                                                    
    return await func(*args)                                                                                                                                                                                                                                                                                                                                                                                                         
  File "/usr/local/lib/python3.8/site-packages/asyncclick/core.py", line 865, in _main                                                                                                                                                                                                                                                                                                                                               
    return await main(*args, **kwargs)                                                                                                                                                                                                                                                                                                                                                                                               
  File "/usr/local/lib/python3.8/site-packages/asyncclick/core.py", line 814, in main                                                                                                                                                                                                                                                                                                                                                
    rv = await self.invoke(ctx)                                                                                                                                                                                                                                                                                                                                                                                                      
  File "/usr/local/lib/python3.8/site-packages/asyncclick/core.py", line 1295, in invoke                                                                                                                                                                                                                                                                                                                                             
    return await _process_result(await sub_ctx.command.invoke(sub_ctx))                                                                                                                                                                                                                                                                                                                                                              
  File "/usr/local/lib/python3.8/site-packages/asyncclick/core.py", line 1102, in invoke                                                                                                                                                                                                                                                                                                                                             
    return await ctx.invoke(self.callback, **ctx.params)                                                                                                                                                                                                                                                                                                                                                                             
  File "/usr/local/lib/python3.8/site-packages/asyncclick/core.py", line 644, in invoke                                                                                                                                                                                                                                                                                                                                              
    rv = await rv                                                                                                                                                                                                                                                                                                                                                                                                                    
  File "/usr/local/lib/python3.8/site-packages/kasa/cli.py", line 188, in discover                                                                                                                                                                                                                                                                                                                                                   
    await ctx.invoke(state)                                                                                                                                                                                                                                                                                                                                                                                                          
  File "/usr/local/lib/python3.8/site-packages/asyncclick/core.py", line 644, in invoke                                                                                                                                                                                                                                                                                                                                              
    rv = await rv                                                                                                                                                                                                                                                                                                                                                                                                                    
  File "/usr/local/lib/python3.8/site-packages/asyncclick/core.py", line 644, in invoke                                                                                                                                                                                                                                                                                                                                              
    rv = await rv                                                                                                                                                                                                                                                                                                                                                                                                                    
  File "/usr/local/lib/python3.8/site-packages/kasa/cli.py", line 252, in state                                                                                                                                                                                                                                                                                                                                                      
    click.echo(f"\tMAC (rssi):   {dev.mac} ({dev.rssi})")                                                                                                                                                                                                                                                                                                                                                                            
  File "/usr/local/lib/python3.8/site-packages/kasa/smartdevice.py", line 115, in wrapped                                                                                                                                                                                                                                                                                                                                            
    return f(*args, **kwargs)                                                                                                                                                                                                                                                                                                                                                                                                        
  File "/usr/local/lib/python3.8/site-packages/kasa/smartdevice.py", line 413, in mac                                                                                                                                                                                                                                                                                                                                                
    format(s, "02x") for s in bytes.fromhex(sys_info["mic_mac"])                                                                                                                                                                                                                                                                                                                                                                     
ValueError: non-hexadecimal number found in fromhex() arg at position 2

This is by no means a fix but I'm just having it bypass the else if logic to see it can continue:
(around L412 of smartdevice.py)

        if "mac" in sys_info:                                                                                                                                                                                                                                                                                                                                                                                                        
            return str(sys_info["mac"])                                                                                                                                                                                                                                                                                                                                                                                              
        if "mic_mac" in sys_info:                                                                                                                                                                                                                                                                                                                                                                                                    
            return str(sys_info["mic_mac"])    
bash-5.0# kasa discover                                                                                                                                                                                                                                                                                                                                                                                                              
Discovering devices on 255.255.255.255 for 3 seconds                                                                                                                                                                                                                                                                                                                                                                                 
== Test Light - KL430(US) ==                                                                                                                                                                                                                                                                                                                                                                                                         
        Host: 192.168.1.93                                                                                                                                                                                                                                                                                                                                                                                                           
        Device state: ON                                                                                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                                                                                                                                                     
        == Generic information ==                                                                                                                                                                                                                                                                                                                                                                                                    
        Time:         None                                                                                                                                                                                                                                                                                                                                                                                                           
        Hardware:     2.0                                                                                                                                                                                                                                                                                                                                                                                                            
        Software:     1.0.8 Build 210121 Rel.084339                                                                                                                                                                                                                                                                                                                                                                                  
        MAC (rssi):   C0:C9:E3:4B:72:61 (-33)                                                                                                                                                                                                                                                                                                                                                                                        
        Location:     {'latitude': <yep>, 'longitude': <finally working>}                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                                                                     
        == Device specific information ==                                                                                                                                                                                                                                                                                                                                                                                            
        Brightness: 53                                                                                                                                                                                                                                                                                                                                                                                                               
        Is dimmable: True                                                                                                                                                                                                                                                                                                                                                                                                            
        Color temperature: 0                                                                                                                                                                                                                                                                                                                                                                                                         
        Valid temperature range: (2500, 9000)                                                                                                                                                                                                                                                                                                                                                                                        
        HSV: (194, 50, 53)                                                                                                                                                                                                                                                                                                                                                                                                           
        Length: 16                                                                                                                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                                                                                                                     
        == Current State ==                                                                                                                                                                                                                                                                                                                                                                                                          
        {'power_mw': 6243, 'total_wh': 122, 'err_code': 0}                                                                                                                                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                                                                                                                                                                     
== Computer Room Light - HS103(US) ==                                                                                                                                                                                                                                                                                                                                                                                                
        Host: 192.168.1.92                                                                                                                                                                                                                                                                                                                                                                                                           
        Device state: ON                                                                                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                                                                                                                                                     
        == Generic information ==                                                                                                                                                                                                                                                                                                                                                                                                    
        Time:         2021-05-01 23:01:09                                                                                                                                                                                                                                                                                                                                                                                            
        Hardware:     2.1                                                                                                                                                                                                                                                                                                                                                                                                            
        Software:     1.1.3 Build 200804 Rel.095135                                                                                                                                                                                                                                                                                                                                                                                  
        MAC (rssi):   D8:07:B6:AA:06:CC (-42)                                                                                                                                                                                                                                                                                                                                                                                        
        Location:     {'latitude': <this can be>, 'longitude': <ignored>}                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                                                                     
        == Device specific information ==                                                                                                                                                                                                                                                                                                                                                                                            
        LED state: True                                                                                                                                                                                                                                                                                                                                                                                                              
        On since: 2021-05-02 04:21:39.267777

Unsure if this a firmware issue and as far as I can tell I'm fully patched. I hope someone more dedicated can create a PR and fix this. \o/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions