Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor blocking calls to async thread #85

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

kbullet
Copy link
Contributor

@kbullet kbullet commented Feb 26, 2025

Description

Refactor ssl context and load branches blocking calls to async thread.

  • Move the SSL context creation and load branches to async_add_executor_job
  • Loads and store branch data once during device init
  • Branch data stored in mem for later use
  • Moving the blocking I/O operation to init

e.g resolving the following warnings

2025-02-26 10:21:11.664 WARNING (MainThread) [homeassistant.util.loop] Detected blocking call to load_default_certs with args (<ssl.SSLContext object at 0x7f7ba5d74560>, <Purpose.SERVER_AUTH: _ASN1Object(nid=129, shortname='serverAuth', longname='TLS Web Server Authentication', oid='1.3.6.1.5.5.7.3.1')>) inside the event loop by custom integration 'nestup_evn' at custom_components/nestup_evn/nestup_evn.py, line 181: ssl_context = ssl.create_default_context() (offender: /usr/local/lib/python3.13/ssl.py, line 722: context.load_default_certs(purpose)), please create a bug report at https://github.com/trvqhuy/nestup_evn/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 227, in <module>
    sys.exit(main())
  File "/usr/src/homeassistant/homeassistant/__main__.py", line 213, in main
    exit_code = runner.run(runtime_conf)
  File "/usr/src/homeassistant/homeassistant/runner.py", line 154, in run
    return loop.run_until_complete(setup_and_run_hass(runtime_config))
  File "/usr/local/lib/python3.13/asyncio/base_events.py", line 707, in run_until_complete
    self.run_forever()
  File "/usr/local/lib/python3.13/asyncio/base_events.py", line 678, in run_forever
    self._run_once()
  File "/usr/local/lib/python3.13/asyncio/base_events.py", line 2033, in _run_once
    handle._run()
  File "/usr/local/lib/python3.13/asyncio/events.py", line 89, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 782, in async_setup_locked
    await self.async_setup(hass, integration=integration)
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 548, in async_setup
    await self.__async_setup_with_context(hass, integration)
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 637, in __async_setup_with_context
    result = await component.async_setup_entry(hass, self)
  File "/config/custom_components/nestup_evn/__init__.py", line 12, in async_setup_entry
    await hass.config_entries.async_forward_entry_setups(entry, ["sensor"])
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 2285, in async_forward_entry_setups
    await self._async_forward_entry_setups_locked(entry, platforms)
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 2296, in _async_forward_entry_setups_locked
    await asyncio.gather(
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 2298, 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 2377, in _async_forward_entry_setup
    await entry.async_setup(self.hass, integration=integration)
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 548, in async_setup
    await self.__async_setup_with_context(hass, integration)
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 637, in __async_setup_with_context
    result = await component.async_setup_entry(hass, self)
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 96, in async_setup_entry
    return await hass.data[DATA_COMPONENT].async_setup_entry(entry)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 193, in async_setup_entry
    return await self._platforms[key].async_setup_entry(config_entry)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 333, in async_setup_entry
    return await self._async_setup_platform(async_create_setup_awaitable)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 363, in _async_setup_platform
    awaitable = create_eager_task(awaitable, loop=hass.loop)
  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 "/config/custom_components/nestup_evn/sensor.py", line 51, in async_setup_entry
    await evn_device.async_create_coordinator(hass)
  File "/config/custom_components/nestup_evn/sensor.py", line 137, in async_create_coordinator
    await coordinator.async_config_entry_first_refresh()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 307, in async_config_entry_first_refresh
    await self._async_refresh(
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 380, in _async_refresh
    self.data = await self._async_update_data()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 281, in _async_update_data
    return await self.update_method()
  File "/config/custom_components/nestup_evn/sensor.py", line 123, in _async_update
    await self.update()
  File "/config/custom_components/nestup_evn/sensor.py", line 82, in update
    self._data = await self._api.request_update(
  File "/config/custom_components/nestup_evn/nestup_evn.py", line 122, in request_update
    fetch_data = await self.request_update_evnhcmc(
  File "/config/custom_components/nestup_evn/nestup_evn.py", line 477, in request_update_evnhcmc
    login_status = await self.login_evnhcmc(username, password)
  File "/config/custom_components/nestup_evn/nestup_evn.py", line 181, in login_evnhcmc
    ssl_context = ssl.create_default_context()

2025-02-26 10:21:13.324 WARNING (MainThread) [homeassistant.util.loop] Detected blocking call to open with args ('/config/custom_components/nestup_evn/evn_branches.json',) inside the event loop by custom integration 'nestup_evn' at custom_components/nestup_evn/nestup_evn.py, line 1072: with open(file_path) as f: (offender: /config/custom_components/nestup_evn/nestup_evn.py, line 1072: with open(file_path) as f:), please create a bug report at https://github.com/trvqhuy/nestup_evn/issues
For developers, please see https://developers.home-assistant.io/docs/asyncio_blocking_operations/#open
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 227, in <module>
    sys.exit(main())
  File "/usr/src/homeassistant/homeassistant/__main__.py", line 213, in main
    exit_code = runner.run(runtime_conf)
  File "/usr/src/homeassistant/homeassistant/runner.py", line 154, in run
    return loop.run_until_complete(setup_and_run_hass(runtime_config))
  File "/usr/local/lib/python3.13/asyncio/base_events.py", line 707, in run_until_complete
    self.run_forever()
  File "/usr/local/lib/python3.13/asyncio/base_events.py", line 678, in run_forever
    self._run_once()
  File "/usr/local/lib/python3.13/asyncio/base_events.py", line 2033, in _run_once
    handle._run()
  File "/usr/local/lib/python3.13/asyncio/events.py", line 89, in _run
    self._context.run(self._callback, *self._args)
  File "/config/custom_components/nestup_evn/sensor.py", line 58, in async_setup_entry
    async_add_entities(entities)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 524, in _async_schedule_add_entities_for_entry
    task = self.config_entry.async_create_task(
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 1144, in async_create_task
    task = hass.async_create_task_internal(
  File "/usr/src/homeassistant/homeassistant/core.py", line 832, in async_create_task_internal
    task = create_eager_task(target, name=name, loop=self.loop)
  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/helpers/entity_platform.py", line 658, in async_add_entities
    await add_func(coros, entities, timeout)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 609, in _async_add_entities
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 778, in _async_add_entity
    if self.config_entry and (device_info := entity.device_info):
  File "/config/custom_components/nestup_evn/sensor.py", line 206, in device_info
    return self._device.info
  File "/config/custom_components/nestup_evn/sensor.py", line 143, in info
    evn_area = nestup_evn.get_evn_info(self._customer_id)
  File "/config/custom_components/nestup_evn/nestup_evn.py", line 1072, in get_evn_info
    with open(file_path) as f:

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • [] Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Black
  • Flake8
  • Isort

Test Configuration:

  • OS version:
  • HA version:
  • HA platform:
  • Hardware:

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@kbullet kbullet marked this pull request as ready for review February 26, 2025 03:41
@kbullet
Copy link
Contributor Author

kbullet commented Feb 26, 2025

a e check/test qua giùm mình cái k biết còn thiếu gì k 😰 mình test mọi thứ ok trên HA 2025.2.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant