Pass in the web session
Platinum rule inject-websession: the library accepts the web session of Home Assistant.
This is a Platinum rule. In quality_scale.yaml, it is called inject-websession.
The library of the integration must accept an HTTP session from the outside, and the integration must pass in the session that Home Assistant provides. Home Assistant has shared sessions for aiohttp and HTTPX, so the library uses one of those two.
Many integrations talk HTTP. If each library opens its own session, Home Assistant ends up with many connection pools, each with their own connections and SSL setup. A shared session reuses connections between integrations, and uses less memory and fewer sockets.
Pass the shared aiohttp session to the client in async_setup_entry:
from homeassistant.helpers.aiohttp_client import async_get_clientsession
async def async_setup_entry(
hass: HomeAssistant, entry: MyConfigEntry
) -> bool:
"""Set up My integration from a config entry."""
session = async_get_clientsession(hass)
client = MyClient(entry.data[CONF_HOST], session=session)
...For HTTPX, use get_async_client from homeassistant.helpers.httpx_client.
Sometimes a shared session is not what you want, for example when the library uses cookies that should not leak to other integrations. Then create a session of your own that Home Assistant still manages: async_create_clientsession for aiohttp, or create_async_httpx_client for HTTPX.
This rule is checked in review.
If the integration makes no HTTP requests, for example because it talks over Modbus, Bluetooth, or a raw TCP socket, this rule does not apply.
- async-dependency: use an async dependency.