Hyundai-Kia-Connect / hyundai_kia_connect_api

This is a Kia UVO and Hyundai Bluelink written in python. It is primary consumed by home assistant. If you are looking for a home assistant Kia / Hyundai implementation please look here: https://github.com/Hyundai-Kia-Connect/kia_uvo. Much of this base code came from reading bluelinky and contributions to the kia_uvo home assistant project.
MIT License
132 stars 75 forks source link

Lazy load self.sessions in __init__ for USA & CA #659

Closed maruel closed 3 weeks ago

maruel commented 3 weeks ago

Description

Home Assistant complains about blocking the main loop.

A fix could be to change the requests.Session() calls to be lazy loaded instead, so replace:

def __init__(...):
  (...)
  self.sessions = requests.Session()
  self.sessions.mount("https://" + self.BASE_URL, cipherAdapter())

with:

def __init__(...):
  (...)
  self._session = None

@property
def session(self):
  if not self._sessions:
    self._sessions = requests.Session()
    self._sessions.mount("https://" + self.BASE_URL, cipherAdapter())
  return self._sessions

The following are affected:

These do not seem to be affected:

I will submit a PR. I did a local patch and it seemed to have worked.

Logs

2024-10-28 14:00:56.753 WARNING (MainThread) [homeassistant.util.loop] Detected blocking call to load_default_
certs with args (<ssl.SSLContext object at 0x7e49364e0fd0>, <Purpose.SERVER_AUTH: _ASN1Object(nid=129, shortna
me='serverAuth', longname='TLS Web Server Authentication', oid='1.3.6.1.5.5.7.3.1')>) inside the event loop by
 custom integration 'kia_uvo' at custom_components/kia_uvo/coordinator.py, line 55: self.vehicle_manager = Veh
icleManager( (offender: /usr/local/lib/python3.12/ssl.py, line 713: context.load_default_certs(purpose)), plea
se create a bug report at https://github.com/Hyundai-Kia-Connect/kia_uvo/issues                               
For developers, please see https://developers.home-assistant.io/docs/asyncio_blocking_operations/#load_default
_certs                                                                                                        
Traceback (most recent call last):                                                                            
  File "<frozen runpy>", line 198, in _run_module_as_main                                             
  File "<frozen runpy>", line 88, in _run_code
  File "/usr/src/homeassistant/homeassistant/__main__.py", line 223, in <module>                              
    sys.exit(main())                                                                                          
  File "/usr/src/homeassistant/homeassistant/__main__.py", line 209, in main                                  
    exit_code = runner.run(runtime_conf)
  File "/usr/src/homeassistant/homeassistant/runner.py", line 189, in run                      
    return loop.run_until_complete(setup_and_run_hass(runtime_config))                                        
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 674, in run_until_complete                    
    self.run_forever()                                                                                        
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 641, in run_forever                           
    self._run_once()       
  File "/usr/local/lib/python3.12/asyncio/base_events.py", line 1990, in _run_once
    handle._run()
  File "/usr/local/lib/python3.12/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/src/homeassistant/homeassistant/setup.py", line 165, in async_setup_component
    result = await _async_setup_component(hass, domain, config)
  File "/usr/src/homeassistant/homeassistant/setup.py", line 461, in _async_setup_component
    await asyncio.gather(
  File "/usr/src/homeassistant/homeassistant/setup.py", line 463, in <genexpr>
    create_eager_task(
  File "/usr/src/homeassistant/homeassistant/util/async_.py", line 45, in create_eager_task
    return Task(coro, loop=loop, name=name, eager_start=True)
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 742, in async_setup_locked
    await self.async_setup(hass, integration=integration)
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 594, in async_setup
    result = await component.async_setup_entry(hass, self)
  File "/config/custom_components/kia_uvo/__init__.py", line 50, in async_setup_entry
    coordinator = HyundaiKiaConnectDataUpdateCoordinator(hass, config_entry)
  File "/config/custom_components/kia_uvo/coordinator.py", line 55, in __init__
    self.vehicle_manager = VehicleManager(
cdnninja commented 3 weeks ago

Great! Thanks for creating a pr!