Add independent session in honeywell (#108435)

This commit is contained in:
mkmer 2024-02-02 11:31:16 -05:00 committed by GitHub
parent 6b7a984314
commit 7608f0c9ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 5 deletions

View File

@ -8,7 +8,10 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.aiohttp_client import (
async_create_clientsession,
async_get_clientsession,
)
from .const import (
_LOGGER,
@ -48,9 +51,12 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
username = config_entry.data[CONF_USERNAME]
password = config_entry.data[CONF_PASSWORD]
client = aiosomecomfort.AIOSomeComfort(
username, password, session=async_get_clientsession(hass)
)
if len(hass.config_entries.async_entries(DOMAIN)) > 1:
session = async_create_clientsession(hass)
else:
session = async_get_clientsession(hass)
client = aiosomecomfort.AIOSomeComfort(username, password, session=session)
try:
await client.login()
await client.discover()
@ -76,7 +82,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
if len(devices) == 0:
_LOGGER.debug("No devices found")
return False
data = HoneywellData(config_entry.entry_id, client, devices)
hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = data
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)

View File

@ -39,6 +39,15 @@ def config_data():
}
@pytest.fixture
def another_config_data():
"""Provide configuration data for tests."""
return {
CONF_USERNAME: "user2",
CONF_PASSWORD: "fake2",
}
@pytest.fixture
def config_options():
"""Provide configuratio options for test."""
@ -55,6 +64,16 @@ def config_entry(config_data, config_options):
)
@pytest.fixture
def config_entry2(another_config_data, config_options):
"""Create a mock config entry."""
return MockConfigEntry(
domain=DOMAIN,
data=another_config_data,
options=config_options,
)
@pytest.fixture
def device():
"""Mock a somecomfort.Device."""

View File

@ -33,6 +33,22 @@ async def test_setup_entry(hass: HomeAssistant, config_entry: MockConfigEntry) -
) # 1 climate entity; 2 sensor entities
@patch("homeassistant.components.honeywell.UPDATE_LOOP_SLEEP_TIME", 0)
async def test_setup_multiple_entry(
hass: HomeAssistant, config_entry: MockConfigEntry, config_entry2: MockConfigEntry
) -> None:
"""Initialize the config entry."""
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.LOADED
config_entry2.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry2.entry_id)
await hass.async_block_till_done()
assert config_entry2.state is ConfigEntryState.LOADED
async def test_setup_multiple_thermostats(
hass: HomeAssistant,
config_entry: MockConfigEntry,