core/tests/components/nexia/util.py

64 lines
2.1 KiB
Python
Raw Normal View History

"""Tests for the nexia integration."""
2021-01-01 21:31:56 +00:00
from unittest.mock import patch
import uuid
from nexia.home import NexiaHome
from homeassistant.components.nexia.const import DOMAIN
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry, load_fixture
2022-05-18 23:08:02 +00:00
from tests.test_util.aiohttp import mock_aiohttp_client
async def async_init_integration(
2020-08-27 11:56:20 +00:00
hass: HomeAssistant,
skip_setup: bool = False,
Fix not retrying on connection reset during nexia config entry setup (#93576) * Fix not retrying on connection reset during nexia config entry setup fixes ``` 2023-05-26 00:15:39.129 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry Alexander for nexia Traceback (most recent call last): File "/usr/local/lib/python3.11/site-packages/aiohttp/client.py", line 558, in _request resp = await req.send(conn) ^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/aiohttp/client_reqrep.py", line 670, in send await writer.write_headers(status_line, self.headers) File "/usr/local/lib/python3.11/site-packages/aiohttp/http_writer.py", line 130, in write_headers self._write(buf) File "/usr/local/lib/python3.11/site-packages/aiohttp/http_writer.py", line 75, in _write raise ConnectionResetError("Cannot write to closing transport") ConnectionResetError: Cannot write to closing transport The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/config_entries.py", line 387, in async_setup result = await component.async_setup_entry(hass, self) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/components/nexia/__init__.py", line 47, in async_setup_entry await nexia_home.login() File "/usr/local/lib/python3.11/site-packages/nexia/home.py", line 385, in login request = await self.post_url(self.API_MOBILE_ACCOUNTS_SIGN_IN_URL, payload) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/nexia/home.py", line 157, in post_url response: aiohttp.ClientResponse = await self.session.post( ^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/aiohttp/client.py", line 572, in _request raise ClientOSError(*exc.args) from exc aiohttp.client_exceptions.ClientOSError: Cannot write to closing transport ``` * coverage
2023-05-26 01:52:44 +00:00
exception: Exception | None = None,
) -> MockConfigEntry:
"""Set up the nexia integration in Home Assistant."""
house_fixture = "nexia/mobile_houses_123456.json"
session_fixture = "nexia/session_123456.json"
sign_in_fixture = "nexia/sign_in.json"
set_fan_speed_fixture = "nexia/set_fan_speed_2293892.json"
2022-05-18 23:08:02 +00:00
with mock_aiohttp_client() as mock_session, patch(
"nexia.home.load_or_create_uuid", return_value=uuid.uuid4()
):
2022-05-18 23:08:02 +00:00
nexia = NexiaHome(mock_session)
Fix not retrying on connection reset during nexia config entry setup (#93576) * Fix not retrying on connection reset during nexia config entry setup fixes ``` 2023-05-26 00:15:39.129 ERROR (MainThread) [homeassistant.config_entries] Error setting up entry Alexander for nexia Traceback (most recent call last): File "/usr/local/lib/python3.11/site-packages/aiohttp/client.py", line 558, in _request resp = await req.send(conn) ^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/aiohttp/client_reqrep.py", line 670, in send await writer.write_headers(status_line, self.headers) File "/usr/local/lib/python3.11/site-packages/aiohttp/http_writer.py", line 130, in write_headers self._write(buf) File "/usr/local/lib/python3.11/site-packages/aiohttp/http_writer.py", line 75, in _write raise ConnectionResetError("Cannot write to closing transport") ConnectionResetError: Cannot write to closing transport The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/config_entries.py", line 387, in async_setup result = await component.async_setup_entry(hass, self) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/src/homeassistant/homeassistant/components/nexia/__init__.py", line 47, in async_setup_entry await nexia_home.login() File "/usr/local/lib/python3.11/site-packages/nexia/home.py", line 385, in login request = await self.post_url(self.API_MOBILE_ACCOUNTS_SIGN_IN_URL, payload) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/nexia/home.py", line 157, in post_url response: aiohttp.ClientResponse = await self.session.post( ^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/aiohttp/client.py", line 572, in _request raise ClientOSError(*exc.args) from exc aiohttp.client_exceptions.ClientOSError: Cannot write to closing transport ``` * coverage
2023-05-26 01:52:44 +00:00
if exception:
async def _raise_exception(*args, **kwargs):
raise exception
mock_session.post(
nexia.API_MOBILE_SESSION_URL, side_effect=_raise_exception
)
else:
mock_session.post(
nexia.API_MOBILE_SESSION_URL, text=load_fixture(session_fixture)
)
2022-05-18 23:08:02 +00:00
mock_session.get(
nexia.API_MOBILE_HOUSES_URL.format(house_id=123456),
text=load_fixture(house_fixture),
)
2022-05-18 23:08:02 +00:00
mock_session.post(
nexia.API_MOBILE_ACCOUNTS_SIGN_IN_URL,
text=load_fixture(sign_in_fixture),
)
mock_session.post(
"https://www.mynexia.com/mobile/xxl_thermostats/2293892/fan_speed",
text=load_fixture(set_fan_speed_fixture),
)
entry = MockConfigEntry(
domain=DOMAIN, data={CONF_USERNAME: "mock", CONF_PASSWORD: "mock"}
)
entry.add_to_hass(hass)
if not skip_setup:
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
return entry