Use shared aiohttp.ClientSession in bond (#48669)

This commit is contained in:
J. Nick Koston 2021-04-04 12:12:58 -10:00 committed by GitHub
parent 9553ae8196
commit 32daa63265
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 10 deletions

View File

@ -10,6 +10,7 @@ from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.entity import SLOW_UPDATE_WARNING from homeassistant.helpers.entity import SLOW_UPDATE_WARNING
from .const import BPUP_STOP, BPUP_SUBS, BRIDGE_MAKE, DOMAIN, HUB from .const import BPUP_STOP, BPUP_SUBS, BRIDGE_MAKE, DOMAIN, HUB
@ -25,7 +26,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
token = entry.data[CONF_ACCESS_TOKEN] token = entry.data[CONF_ACCESS_TOKEN]
config_entry_id = entry.entry_id config_entry_id = entry.entry_id
bond = Bond(host=host, token=token, timeout=ClientTimeout(total=_API_TIMEOUT)) bond = Bond(
host=host,
token=token,
timeout=ClientTimeout(total=_API_TIMEOUT),
session=async_get_clientsession(hass),
)
hub = BondHub(bond) hub = BondHub(bond)
try: try:
await hub.setup() await hub.setup()

View File

@ -15,6 +15,8 @@ from homeassistant.const import (
CONF_NAME, CONF_NAME,
HTTP_UNAUTHORIZED, HTTP_UNAUTHORIZED,
) )
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.typing import DiscoveryInfoType from homeassistant.helpers.typing import DiscoveryInfoType
from .const import DOMAIN from .const import DOMAIN
@ -30,10 +32,12 @@ DISCOVERY_SCHEMA = vol.Schema({vol.Required(CONF_ACCESS_TOKEN): str})
TOKEN_SCHEMA = vol.Schema({}) TOKEN_SCHEMA = vol.Schema({})
async def _validate_input(data: dict[str, Any]) -> tuple[str, str]: async def _validate_input(hass: HomeAssistant, data: dict[str, Any]) -> tuple[str, str]:
"""Validate the user input allows us to connect.""" """Validate the user input allows us to connect."""
bond = Bond(data[CONF_HOST], data[CONF_ACCESS_TOKEN]) bond = Bond(
data[CONF_HOST], data[CONF_ACCESS_TOKEN], session=async_get_clientsession(hass)
)
try: try:
hub = BondHub(bond) hub = BondHub(bond)
await hub.setup(max_devices=1) await hub.setup(max_devices=1)
@ -71,7 +75,9 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
online longer then the allowed setup period, and we will online longer then the allowed setup period, and we will
instead ask them to manually enter the token. instead ask them to manually enter the token.
""" """
bond = Bond(self._discovered[CONF_HOST], "") bond = Bond(
self._discovered[CONF_HOST], "", session=async_get_clientsession(self.hass)
)
try: try:
response = await bond.token() response = await bond.token()
except ClientConnectionError: except ClientConnectionError:
@ -82,7 +88,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
return return
self._discovered[CONF_ACCESS_TOKEN] = token self._discovered[CONF_ACCESS_TOKEN] = token
_, hub_name = await _validate_input(self._discovered) _, hub_name = await _validate_input(self.hass, self._discovered)
self._discovered[CONF_NAME] = hub_name self._discovered[CONF_NAME] = hub_name
async def async_step_zeroconf(self, discovery_info: DiscoveryInfoType) -> dict[str, Any]: # type: ignore async def async_step_zeroconf(self, discovery_info: DiscoveryInfoType) -> dict[str, Any]: # type: ignore
@ -127,7 +133,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
CONF_HOST: self._discovered[CONF_HOST], CONF_HOST: self._discovered[CONF_HOST],
} }
try: try:
_, hub_name = await _validate_input(data) _, hub_name = await _validate_input(self.hass, data)
except InputValidationError as error: except InputValidationError as error:
errors["base"] = error.base errors["base"] = error.base
else: else:
@ -155,7 +161,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
errors = {} errors = {}
if user_input is not None: if user_input is not None:
try: try:
bond_id, hub_name = await _validate_input(user_input) bond_id, hub_name = await _validate_input(self.hass, user_input)
except InputValidationError as error: except InputValidationError as error:
errors["base"] = error.base errors["base"] = error.base
else: else:

View File

@ -3,7 +3,7 @@
"name": "Bond", "name": "Bond",
"config_flow": true, "config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/bond", "documentation": "https://www.home-assistant.io/integrations/bond",
"requirements": ["bond-api==0.1.11"], "requirements": ["bond-api==0.1.12"],
"zeroconf": ["_bond._tcp.local."], "zeroconf": ["_bond._tcp.local."],
"codeowners": ["@prystupa"], "codeowners": ["@prystupa"],
"quality_scale": "platinum" "quality_scale": "platinum"

View File

@ -374,7 +374,7 @@ blockchain==1.4.4
# bme680==1.0.5 # bme680==1.0.5
# homeassistant.components.bond # homeassistant.components.bond
bond-api==0.1.11 bond-api==0.1.12
# homeassistant.components.amazon_polly # homeassistant.components.amazon_polly
# homeassistant.components.route53 # homeassistant.components.route53

View File

@ -208,7 +208,7 @@ blebox_uniapi==1.3.2
blinkpy==0.17.0 blinkpy==0.17.0
# homeassistant.components.bond # homeassistant.components.bond
bond-api==0.1.11 bond-api==0.1.12
# homeassistant.components.braviatv # homeassistant.components.braviatv
bravia-tv==1.0.8 bravia-tv==1.0.8