2020-03-19 15:50:17 +00:00
|
|
|
"""Test the Powerwall config flow."""
|
|
|
|
|
2024-01-10 21:21:53 +00:00
|
|
|
from datetime import timedelta
|
2022-11-20 15:38:30 +00:00
|
|
|
from unittest.mock import MagicMock, patch
|
2021-01-01 21:31:56 +00:00
|
|
|
|
Fix missing timeout exception check in powerwall config flow (#107899)
* Fix missing timeout exception check in powerwall config flow
powerwall recently switched to asyncio, and every place we
check for unreachable we need to check for timeout error.
There was one missed
```
09:08 homeassistant homeassistant[546]: 2024-01-12 10:09:08.899 ERROR (MainThread) [homeassistant.components.powerwall.config_flow] Unexpected exception
Jan 12 20:09:08 homeassistant homeassistant[546]: Traceback (most recent call last):
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/src/homeassistant/homeassistant/components/powerwall/config_flow.py", line 168, in _async_try_connect
Jan 12 20:09:08 homeassistant homeassistant[546]: info = await validate_input(self.hass, user_input)
Jan 12 20:09:08 homeassistant homeassistant[546]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/src/homeassistant/homeassistant/components/powerwall/config_flow.py", line 76, in validate_input
Jan 12 20:09:08 homeassistant homeassistant[546]: site_info, gateway_din = await _login_and_fetch_site_info(
Jan 12 20:09:08 homeassistant homeassistant[546]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/src/homeassistant/homeassistant/components/powerwall/config_flow.py", line 43, in _login_and_fetch_site_info
Jan 12 20:09:08 homeassistant homeassistant[546]: await power_wall.login(password)
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/tesla_powerwall/powerwall.py", line 58, in login
Jan 12 20:09:08 homeassistant homeassistant[546]: return await self.login_as(User.CUSTOMER, password, email, force_sm_off)
Jan 12 20:09:08 homeassistant homeassistant[546]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/tesla_powerwall/powerwall.py", line 49, in login_as
Jan 12 20:09:08 homeassistant homeassistant[546]: response = await self._api.login(user, email, password, force_sm_off)
Jan 12 20:09:08 homeassistant homeassistant[546]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/tesla_powerwall/api.py", line 172, in login
Jan 12 20:09:08 homeassistant homeassistant[546]: return await self.post(
Jan 12 20:09:08 homeassistant homeassistant[546]: ^^^^^^^^^^^^^^^^
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/tesla_powerwall/api.py", line 146, in post
Jan 12 20:09:08 homeassistant homeassistant[546]: response = await self._http_session.post(
Jan 12 20:09:08 homeassistant homeassistant[546]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/aiohttp/client.py", line 601, in _request
Jan 12 20:09:08 homeassistant homeassistant[546]: await resp.start(conn)
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/aiohttp/client_reqrep.py", line 960, in start
Jan 12 20:09:08 homeassistant homeassistant[546]: with self._timer:
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/aiohttp/helpers.py", line 735, in __exit__
Jan 12 20:09:08 homeassistant homeassistant[546]: raise asyncio.TimeoutError from None
Jan 12 20:09:08 homeassistant homeassistant[546]: TimeoutError
```
* cov
2024-01-12 20:39:27 +00:00
|
|
|
import pytest
|
2021-02-10 19:50:38 +00:00
|
|
|
from tesla_powerwall import (
|
|
|
|
AccessDeniedError,
|
|
|
|
MissingAttributeError,
|
|
|
|
PowerwallUnreachableError,
|
|
|
|
)
|
2020-03-19 15:50:17 +00:00
|
|
|
|
2021-10-07 10:58:00 +00:00
|
|
|
from homeassistant import config_entries
|
2021-11-21 13:56:22 +00:00
|
|
|
from homeassistant.components import dhcp
|
2020-04-13 19:59:50 +00:00
|
|
|
from homeassistant.components.powerwall.const import DOMAIN
|
2024-04-05 15:37:00 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntryState
|
2021-02-10 19:50:38 +00:00
|
|
|
from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD
|
2023-02-07 14:01:16 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-07-07 19:28:18 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResultType
|
2024-01-10 21:21:53 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2020-03-19 15:50:17 +00:00
|
|
|
|
2022-02-03 17:39:57 +00:00
|
|
|
from .mocks import (
|
|
|
|
MOCK_GATEWAY_DIN,
|
|
|
|
_mock_powerwall_side_effect,
|
|
|
|
_mock_powerwall_site_name,
|
2022-11-20 15:38:30 +00:00
|
|
|
_mock_powerwall_with_fixtures,
|
2022-02-03 17:39:57 +00:00
|
|
|
)
|
2020-03-19 15:50:17 +00:00
|
|
|
|
2024-01-10 21:21:53 +00:00
|
|
|
from tests.common import MockConfigEntry, async_fire_time_changed
|
2021-01-17 02:09:04 +00:00
|
|
|
|
2021-02-10 19:50:38 +00:00
|
|
|
VALID_CONFIG = {CONF_IP_ADDRESS: "1.2.3.4", CONF_PASSWORD: "00GGX"}
|
|
|
|
|
2020-03-19 15:50:17 +00:00
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_form_source_user(hass: HomeAssistant) -> None:
|
2020-03-19 15:50:17 +00:00
|
|
|
"""Test we get config flow setup form as a user."""
|
2021-10-07 10:58:00 +00:00
|
|
|
|
2020-03-19 15:50:17 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
2024-04-03 07:53:20 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2020-03-19 15:50:17 +00:00
|
|
|
assert result["errors"] == {}
|
|
|
|
|
2024-01-10 21:21:53 +00:00
|
|
|
mock_powerwall = await _mock_powerwall_site_name(hass, "MySite")
|
2020-03-19 15:50:17 +00:00
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
|
|
|
return_value=mock_powerwall,
|
|
|
|
),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry,
|
|
|
|
):
|
2020-03-19 15:50:17 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
2020-08-27 11:56:20 +00:00
|
|
|
result["flow_id"],
|
2021-02-10 19:50:38 +00:00
|
|
|
VALID_CONFIG,
|
2020-03-19 15:50:17 +00:00
|
|
|
)
|
2020-10-24 14:20:56 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-03-19 15:50:17 +00:00
|
|
|
|
2024-04-03 07:53:20 +00:00
|
|
|
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
2024-01-10 21:21:53 +00:00
|
|
|
assert result2["title"] == "MySite"
|
2021-02-10 19:50:38 +00:00
|
|
|
assert result2["data"] == VALID_CONFIG
|
2020-03-19 15:50:17 +00:00
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
2024-03-19 08:01:07 +00:00
|
|
|
@pytest.mark.parametrize("exc", [PowerwallUnreachableError, TimeoutError])
|
Fix missing timeout exception check in powerwall config flow (#107899)
* Fix missing timeout exception check in powerwall config flow
powerwall recently switched to asyncio, and every place we
check for unreachable we need to check for timeout error.
There was one missed
```
09:08 homeassistant homeassistant[546]: 2024-01-12 10:09:08.899 ERROR (MainThread) [homeassistant.components.powerwall.config_flow] Unexpected exception
Jan 12 20:09:08 homeassistant homeassistant[546]: Traceback (most recent call last):
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/src/homeassistant/homeassistant/components/powerwall/config_flow.py", line 168, in _async_try_connect
Jan 12 20:09:08 homeassistant homeassistant[546]: info = await validate_input(self.hass, user_input)
Jan 12 20:09:08 homeassistant homeassistant[546]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/src/homeassistant/homeassistant/components/powerwall/config_flow.py", line 76, in validate_input
Jan 12 20:09:08 homeassistant homeassistant[546]: site_info, gateway_din = await _login_and_fetch_site_info(
Jan 12 20:09:08 homeassistant homeassistant[546]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/src/homeassistant/homeassistant/components/powerwall/config_flow.py", line 43, in _login_and_fetch_site_info
Jan 12 20:09:08 homeassistant homeassistant[546]: await power_wall.login(password)
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/tesla_powerwall/powerwall.py", line 58, in login
Jan 12 20:09:08 homeassistant homeassistant[546]: return await self.login_as(User.CUSTOMER, password, email, force_sm_off)
Jan 12 20:09:08 homeassistant homeassistant[546]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/tesla_powerwall/powerwall.py", line 49, in login_as
Jan 12 20:09:08 homeassistant homeassistant[546]: response = await self._api.login(user, email, password, force_sm_off)
Jan 12 20:09:08 homeassistant homeassistant[546]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/tesla_powerwall/api.py", line 172, in login
Jan 12 20:09:08 homeassistant homeassistant[546]: return await self.post(
Jan 12 20:09:08 homeassistant homeassistant[546]: ^^^^^^^^^^^^^^^^
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/tesla_powerwall/api.py", line 146, in post
Jan 12 20:09:08 homeassistant homeassistant[546]: response = await self._http_session.post(
Jan 12 20:09:08 homeassistant homeassistant[546]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/aiohttp/client.py", line 601, in _request
Jan 12 20:09:08 homeassistant homeassistant[546]: await resp.start(conn)
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/aiohttp/client_reqrep.py", line 960, in start
Jan 12 20:09:08 homeassistant homeassistant[546]: with self._timer:
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/aiohttp/helpers.py", line 735, in __exit__
Jan 12 20:09:08 homeassistant homeassistant[546]: raise asyncio.TimeoutError from None
Jan 12 20:09:08 homeassistant homeassistant[546]: TimeoutError
```
* cov
2024-01-12 20:39:27 +00:00
|
|
|
async def test_form_cannot_connect(hass: HomeAssistant, exc: Exception) -> None:
|
2020-03-19 15:50:17 +00:00
|
|
|
"""Test we handle cannot connect error."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
Fix missing timeout exception check in powerwall config flow (#107899)
* Fix missing timeout exception check in powerwall config flow
powerwall recently switched to asyncio, and every place we
check for unreachable we need to check for timeout error.
There was one missed
```
09:08 homeassistant homeassistant[546]: 2024-01-12 10:09:08.899 ERROR (MainThread) [homeassistant.components.powerwall.config_flow] Unexpected exception
Jan 12 20:09:08 homeassistant homeassistant[546]: Traceback (most recent call last):
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/src/homeassistant/homeassistant/components/powerwall/config_flow.py", line 168, in _async_try_connect
Jan 12 20:09:08 homeassistant homeassistant[546]: info = await validate_input(self.hass, user_input)
Jan 12 20:09:08 homeassistant homeassistant[546]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/src/homeassistant/homeassistant/components/powerwall/config_flow.py", line 76, in validate_input
Jan 12 20:09:08 homeassistant homeassistant[546]: site_info, gateway_din = await _login_and_fetch_site_info(
Jan 12 20:09:08 homeassistant homeassistant[546]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/src/homeassistant/homeassistant/components/powerwall/config_flow.py", line 43, in _login_and_fetch_site_info
Jan 12 20:09:08 homeassistant homeassistant[546]: await power_wall.login(password)
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/tesla_powerwall/powerwall.py", line 58, in login
Jan 12 20:09:08 homeassistant homeassistant[546]: return await self.login_as(User.CUSTOMER, password, email, force_sm_off)
Jan 12 20:09:08 homeassistant homeassistant[546]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/tesla_powerwall/powerwall.py", line 49, in login_as
Jan 12 20:09:08 homeassistant homeassistant[546]: response = await self._api.login(user, email, password, force_sm_off)
Jan 12 20:09:08 homeassistant homeassistant[546]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/tesla_powerwall/api.py", line 172, in login
Jan 12 20:09:08 homeassistant homeassistant[546]: return await self.post(
Jan 12 20:09:08 homeassistant homeassistant[546]: ^^^^^^^^^^^^^^^^
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/tesla_powerwall/api.py", line 146, in post
Jan 12 20:09:08 homeassistant homeassistant[546]: response = await self._http_session.post(
Jan 12 20:09:08 homeassistant homeassistant[546]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/aiohttp/client.py", line 601, in _request
Jan 12 20:09:08 homeassistant homeassistant[546]: await resp.start(conn)
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/aiohttp/client_reqrep.py", line 960, in start
Jan 12 20:09:08 homeassistant homeassistant[546]: with self._timer:
Jan 12 20:09:08 homeassistant homeassistant[546]: File "/usr/local/lib/python3.12/site-packages/aiohttp/helpers.py", line 735, in __exit__
Jan 12 20:09:08 homeassistant homeassistant[546]: raise asyncio.TimeoutError from None
Jan 12 20:09:08 homeassistant homeassistant[546]: TimeoutError
```
* cov
2024-01-12 20:39:27 +00:00
|
|
|
mock_powerwall = await _mock_powerwall_side_effect(site_info=exc)
|
2020-03-19 15:50:17 +00:00
|
|
|
|
|
|
|
with patch(
|
2020-04-13 19:59:50 +00:00
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
2020-03-19 15:50:17 +00:00
|
|
|
return_value=mock_powerwall,
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
2020-08-27 11:56:20 +00:00
|
|
|
result["flow_id"],
|
2021-02-10 19:50:38 +00:00
|
|
|
VALID_CONFIG,
|
2020-03-19 15:50:17 +00:00
|
|
|
)
|
|
|
|
|
2024-04-03 07:53:20 +00:00
|
|
|
assert result2["type"] is FlowResultType.FORM
|
2021-02-10 19:50:38 +00:00
|
|
|
assert result2["errors"] == {CONF_IP_ADDRESS: "cannot_connect"}
|
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_invalid_auth(hass: HomeAssistant) -> None:
|
2021-02-10 19:50:38 +00:00
|
|
|
"""Test we handle invalid auth error."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
2024-01-10 21:21:53 +00:00
|
|
|
mock_powerwall = await _mock_powerwall_side_effect(
|
|
|
|
site_info=AccessDeniedError("any")
|
|
|
|
)
|
2021-02-10 19:50:38 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
|
|
|
return_value=mock_powerwall,
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
VALID_CONFIG,
|
|
|
|
)
|
|
|
|
|
2024-04-03 07:53:20 +00:00
|
|
|
assert result2["type"] is FlowResultType.FORM
|
2021-02-10 19:50:38 +00:00
|
|
|
assert result2["errors"] == {CONF_PASSWORD: "invalid_auth"}
|
2020-04-27 00:14:53 +00:00
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_form_unknown_exeption(hass: HomeAssistant) -> None:
|
2021-01-17 02:09:04 +00:00
|
|
|
"""Test we handle an unknown exception."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
2024-01-10 21:21:53 +00:00
|
|
|
mock_powerwall = await _mock_powerwall_side_effect(site_info=ValueError)
|
2021-01-17 02:09:04 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
|
|
|
return_value=mock_powerwall,
|
|
|
|
):
|
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
2021-02-10 19:50:38 +00:00
|
|
|
result["flow_id"], VALID_CONFIG
|
2021-01-17 02:09:04 +00:00
|
|
|
)
|
|
|
|
|
2024-04-03 07:53:20 +00:00
|
|
|
assert result2["type"] is FlowResultType.FORM
|
2021-01-17 02:09:04 +00:00
|
|
|
assert result2["errors"] == {"base": "unknown"}
|
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_form_wrong_version(hass: HomeAssistant) -> None:
|
2020-04-27 00:14:53 +00:00
|
|
|
"""Test we can handle wrong version error."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
|
|
)
|
|
|
|
|
2024-01-10 21:21:53 +00:00
|
|
|
mock_powerwall = await _mock_powerwall_side_effect(
|
2020-10-08 13:26:44 +00:00
|
|
|
site_info=MissingAttributeError({}, "")
|
|
|
|
)
|
2020-04-27 00:14:53 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
|
|
|
return_value=mock_powerwall,
|
|
|
|
):
|
|
|
|
result3 = await hass.config_entries.flow.async_configure(
|
2020-08-27 11:56:20 +00:00
|
|
|
result["flow_id"],
|
2021-02-10 19:50:38 +00:00
|
|
|
VALID_CONFIG,
|
2020-04-27 00:14:53 +00:00
|
|
|
)
|
|
|
|
|
2024-04-03 07:53:20 +00:00
|
|
|
assert result3["type"] is FlowResultType.FORM
|
2020-04-27 00:14:53 +00:00
|
|
|
assert result3["errors"] == {"base": "wrong_version"}
|
2021-01-17 02:09:04 +00:00
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_already_configured(hass: HomeAssistant) -> None:
|
2021-01-17 02:09:04 +00:00
|
|
|
"""Test we abort when already configured."""
|
|
|
|
|
|
|
|
config_entry = MockConfigEntry(domain=DOMAIN, data={CONF_IP_ADDRESS: "1.1.1.1"})
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_DHCP},
|
2021-11-21 13:56:22 +00:00
|
|
|
data=dhcp.DhcpServiceInfo(
|
|
|
|
ip="1.1.1.1",
|
2024-02-14 22:11:25 +00:00
|
|
|
macaddress="aabbcceeddff",
|
2021-11-21 13:56:22 +00:00
|
|
|
hostname="any",
|
|
|
|
),
|
2021-01-17 02:09:04 +00:00
|
|
|
)
|
2024-04-03 07:53:20 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2021-01-17 02:09:04 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_already_configured_with_ignored(hass: HomeAssistant) -> None:
|
2021-01-17 02:09:04 +00:00
|
|
|
"""Test ignored entries do not break checking for existing entries."""
|
|
|
|
|
2021-04-25 09:27:40 +00:00
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN, data={}, source=config_entries.SOURCE_IGNORE
|
|
|
|
)
|
2021-01-17 02:09:04 +00:00
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
2022-02-03 17:39:57 +00:00
|
|
|
mock_powerwall = await _mock_powerwall_site_name(hass, "Some site")
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
|
|
|
return_value=mock_powerwall,
|
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_DHCP},
|
|
|
|
data=dhcp.DhcpServiceInfo(
|
|
|
|
ip="1.1.1.1",
|
2024-02-14 22:11:25 +00:00
|
|
|
macaddress="aabbcceeddff",
|
2022-02-03 17:39:57 +00:00
|
|
|
hostname="00GGX",
|
|
|
|
),
|
|
|
|
)
|
2024-04-03 07:53:20 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2022-02-03 17:39:57 +00:00
|
|
|
assert result["errors"] is None
|
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
|
|
|
return_value=mock_powerwall,
|
|
|
|
),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry,
|
|
|
|
):
|
2022-02-03 17:39:57 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2024-04-03 07:53:20 +00:00
|
|
|
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
2022-02-03 17:39:57 +00:00
|
|
|
assert result2["title"] == "Some site"
|
|
|
|
assert result2["data"] == {"ip_address": "1.1.1.1", "password": "00GGX"}
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
2021-01-17 02:09:04 +00:00
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_dhcp_discovery_manual_configure(hass: HomeAssistant) -> None:
|
2022-02-03 17:39:57 +00:00
|
|
|
"""Test we can process the discovery from dhcp and manually configure."""
|
|
|
|
mock_powerwall = await _mock_powerwall_site_name(hass, "Some site")
|
2021-10-07 10:58:00 +00:00
|
|
|
|
2022-02-03 17:39:57 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall.login",
|
|
|
|
side_effect=AccessDeniedError("xyz"),
|
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_DHCP},
|
|
|
|
data=dhcp.DhcpServiceInfo(
|
|
|
|
ip="1.1.1.1",
|
2024-02-14 22:11:25 +00:00
|
|
|
macaddress="aabbcceeddff",
|
2022-02-03 17:39:57 +00:00
|
|
|
hostname="any",
|
|
|
|
),
|
|
|
|
)
|
2024-04-03 07:53:20 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2021-01-17 02:09:04 +00:00
|
|
|
assert result["errors"] == {}
|
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
|
|
|
return_value=mock_powerwall,
|
|
|
|
),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry,
|
|
|
|
):
|
2021-01-17 02:09:04 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
2021-02-10 19:50:38 +00:00
|
|
|
VALID_CONFIG,
|
2021-01-17 02:09:04 +00:00
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2024-04-03 07:53:20 +00:00
|
|
|
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
2021-01-17 02:09:04 +00:00
|
|
|
assert result2["title"] == "Some site"
|
2021-02-10 19:50:38 +00:00
|
|
|
assert result2["data"] == VALID_CONFIG
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_dhcp_discovery_auto_configure(hass: HomeAssistant) -> None:
|
2022-02-03 17:39:57 +00:00
|
|
|
"""Test we can process the discovery from dhcp and auto configure."""
|
|
|
|
mock_powerwall = await _mock_powerwall_site_name(hass, "Some site")
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
|
|
|
return_value=mock_powerwall,
|
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_DHCP},
|
|
|
|
data=dhcp.DhcpServiceInfo(
|
|
|
|
ip="1.1.1.1",
|
2024-02-14 22:11:25 +00:00
|
|
|
macaddress="aabbcceeddff",
|
2022-02-03 17:39:57 +00:00
|
|
|
hostname="00GGX",
|
|
|
|
),
|
|
|
|
)
|
2024-04-03 07:53:20 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2022-02-03 17:39:57 +00:00
|
|
|
assert result["errors"] is None
|
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
|
|
|
return_value=mock_powerwall,
|
|
|
|
),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry,
|
|
|
|
):
|
2022-02-03 17:39:57 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2024-04-03 07:53:20 +00:00
|
|
|
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
2022-02-03 17:39:57 +00:00
|
|
|
assert result2["title"] == "Some site"
|
|
|
|
assert result2["data"] == {"ip_address": "1.1.1.1", "password": "00GGX"}
|
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_dhcp_discovery_cannot_connect(hass: HomeAssistant) -> None:
|
2022-02-03 17:39:57 +00:00
|
|
|
"""Test we can process the discovery from dhcp and we cannot connect."""
|
2024-01-10 21:21:53 +00:00
|
|
|
mock_powerwall = await _mock_powerwall_side_effect(
|
|
|
|
site_info=PowerwallUnreachableError
|
|
|
|
)
|
2022-02-03 17:39:57 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
|
|
|
return_value=mock_powerwall,
|
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_DHCP},
|
|
|
|
data=dhcp.DhcpServiceInfo(
|
|
|
|
ip="1.1.1.1",
|
2024-02-14 22:11:25 +00:00
|
|
|
macaddress="aabbcceeddff",
|
2022-02-03 17:39:57 +00:00
|
|
|
hostname="00GGX",
|
|
|
|
),
|
|
|
|
)
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2022-02-03 17:39:57 +00:00
|
|
|
assert result["reason"] == "cannot_connect"
|
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_form_reauth(hass: HomeAssistant) -> None:
|
2021-02-10 19:50:38 +00:00
|
|
|
"""Test reauthenticate."""
|
|
|
|
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=VALID_CONFIG,
|
2022-02-03 17:39:57 +00:00
|
|
|
unique_id=MOCK_GATEWAY_DIN,
|
2021-02-10 19:50:38 +00:00
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2022-02-03 17:39:57 +00:00
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_REAUTH, "entry_id": entry.entry_id},
|
|
|
|
data=entry.data,
|
2021-02-10 19:50:38 +00:00
|
|
|
)
|
2024-04-03 07:53:20 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2021-02-10 19:50:38 +00:00
|
|
|
assert result["errors"] == {}
|
|
|
|
|
|
|
|
mock_powerwall = await _mock_powerwall_site_name(hass, "My site")
|
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
|
|
|
return_value=mock_powerwall,
|
|
|
|
),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
) as mock_setup_entry,
|
|
|
|
):
|
2021-02-10 19:50:38 +00:00
|
|
|
result2 = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
{
|
|
|
|
CONF_PASSWORD: "new-test-password",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2024-04-03 07:53:20 +00:00
|
|
|
assert result2["type"] is FlowResultType.ABORT
|
2021-02-10 19:50:38 +00:00
|
|
|
assert result2["reason"] == "reauth_successful"
|
2021-01-17 02:09:04 +00:00
|
|
|
assert len(mock_setup_entry.mock_calls) == 1
|
2022-02-03 17:39:57 +00:00
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_dhcp_discovery_update_ip_address(hass: HomeAssistant) -> None:
|
2022-02-03 17:39:57 +00:00
|
|
|
"""Test we can update the ip address from dhcp."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=VALID_CONFIG,
|
|
|
|
unique_id=MOCK_GATEWAY_DIN,
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
2022-11-20 15:38:30 +00:00
|
|
|
mock_powerwall = MagicMock(login=MagicMock(side_effect=PowerwallUnreachableError))
|
2024-01-10 21:21:53 +00:00
|
|
|
mock_powerwall.__aenter__.return_value = mock_powerwall
|
2022-02-03 17:39:57 +00:00
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
|
|
|
return_value=mock_powerwall,
|
|
|
|
),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
),
|
2022-02-03 17:39:57 +00:00
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_DHCP},
|
|
|
|
data=dhcp.DhcpServiceInfo(
|
|
|
|
ip="1.1.1.1",
|
2024-02-14 22:11:25 +00:00
|
|
|
macaddress="aabbcceeddff",
|
2022-02-03 17:39:57 +00:00
|
|
|
hostname=MOCK_GATEWAY_DIN.lower(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2022-02-03 17:39:57 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
assert entry.data[CONF_IP_ADDRESS] == "1.1.1.1"
|
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_dhcp_discovery_does_not_update_ip_when_auth_fails(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
2022-11-20 15:38:30 +00:00
|
|
|
"""Test we do not switch to another interface when auth is failing."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=VALID_CONFIG,
|
|
|
|
unique_id=MOCK_GATEWAY_DIN,
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
mock_powerwall = MagicMock(login=MagicMock(side_effect=AccessDeniedError("any")))
|
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
|
|
|
return_value=mock_powerwall,
|
|
|
|
),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
),
|
2022-11-20 15:38:30 +00:00
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_DHCP},
|
|
|
|
data=dhcp.DhcpServiceInfo(
|
|
|
|
ip="1.1.1.1",
|
2024-02-14 22:11:25 +00:00
|
|
|
macaddress="aabbcceeddff",
|
2022-11-20 15:38:30 +00:00
|
|
|
hostname=MOCK_GATEWAY_DIN.lower(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2022-11-20 15:38:30 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
assert entry.data[CONF_IP_ADDRESS] == "1.2.3.4"
|
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_dhcp_discovery_does_not_update_ip_when_auth_successful(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
2022-11-20 15:38:30 +00:00
|
|
|
"""Test we do not switch to another interface when auth is successful."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=VALID_CONFIG,
|
|
|
|
unique_id=MOCK_GATEWAY_DIN,
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
mock_powerwall = MagicMock(login=MagicMock(return_value=True))
|
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
|
|
|
return_value=mock_powerwall,
|
|
|
|
),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
),
|
2022-11-20 15:38:30 +00:00
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_DHCP},
|
|
|
|
data=dhcp.DhcpServiceInfo(
|
|
|
|
ip="1.1.1.1",
|
2024-02-14 22:11:25 +00:00
|
|
|
macaddress="aabbcceeddff",
|
2022-11-20 15:38:30 +00:00
|
|
|
hostname=MOCK_GATEWAY_DIN.lower(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2022-11-20 15:38:30 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
assert entry.data[CONF_IP_ADDRESS] == "1.2.3.4"
|
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_dhcp_discovery_updates_unique_id(hass: HomeAssistant) -> None:
|
2022-02-03 17:39:57 +00:00
|
|
|
"""Test we can update the unique id from dhcp."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=VALID_CONFIG,
|
|
|
|
unique_id="1.2.3.4",
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
mock_powerwall = await _mock_powerwall_site_name(hass, "Some site")
|
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
|
|
|
return_value=mock_powerwall,
|
|
|
|
),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
),
|
2022-02-03 17:39:57 +00:00
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_DHCP},
|
|
|
|
data=dhcp.DhcpServiceInfo(
|
|
|
|
ip="1.2.3.4",
|
2024-02-14 22:11:25 +00:00
|
|
|
macaddress="aabbcceeddff",
|
2022-02-03 17:39:57 +00:00
|
|
|
hostname=MOCK_GATEWAY_DIN.lower(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2022-02-03 17:39:57 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
assert entry.data[CONF_IP_ADDRESS] == "1.2.3.4"
|
|
|
|
assert entry.unique_id == MOCK_GATEWAY_DIN
|
2022-11-20 15:38:30 +00:00
|
|
|
|
|
|
|
|
2023-02-08 15:48:54 +00:00
|
|
|
async def test_dhcp_discovery_updates_unique_id_when_entry_is_failed(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
2022-11-20 15:38:30 +00:00
|
|
|
"""Test we can update the unique id from dhcp in a failed state."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=VALID_CONFIG,
|
|
|
|
unique_id="1.2.3.4",
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
2024-04-05 15:37:00 +00:00
|
|
|
entry.mock_state(hass, ConfigEntryState.SETUP_ERROR)
|
2022-11-20 15:38:30 +00:00
|
|
|
mock_powerwall = await _mock_powerwall_site_name(hass, "Some site")
|
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
|
|
|
return_value=mock_powerwall,
|
|
|
|
),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.async_setup_entry",
|
|
|
|
return_value=True,
|
|
|
|
),
|
2022-11-20 15:38:30 +00:00
|
|
|
):
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_DHCP},
|
|
|
|
data=dhcp.DhcpServiceInfo(
|
|
|
|
ip="1.2.3.4",
|
2024-02-14 22:11:25 +00:00
|
|
|
macaddress="aabbcceeddff",
|
2022-11-20 15:38:30 +00:00
|
|
|
hostname=MOCK_GATEWAY_DIN.lower(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2022-11-20 15:38:30 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
assert entry.data[CONF_IP_ADDRESS] == "1.2.3.4"
|
|
|
|
assert entry.unique_id == MOCK_GATEWAY_DIN
|
|
|
|
|
|
|
|
|
2023-02-07 14:01:16 +00:00
|
|
|
async def test_discovered_wifi_does_not_update_ip_if_is_still_online(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
2022-11-20 15:38:30 +00:00
|
|
|
"""Test a discovery does not update the ip unless the powerwall at the old ip is offline."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=VALID_CONFIG,
|
|
|
|
unique_id=MOCK_GATEWAY_DIN,
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
mock_powerwall = await _mock_powerwall_with_fixtures(hass)
|
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
|
|
|
return_value=mock_powerwall,
|
|
|
|
),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.Powerwall", return_value=mock_powerwall
|
|
|
|
),
|
2022-11-20 15:38:30 +00:00
|
|
|
):
|
|
|
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_DHCP},
|
|
|
|
data=dhcp.DhcpServiceInfo(
|
|
|
|
ip="1.2.3.5",
|
2024-02-14 22:11:25 +00:00
|
|
|
macaddress="aabbcceeddff",
|
2022-11-20 15:38:30 +00:00
|
|
|
hostname=MOCK_GATEWAY_DIN.lower(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2022-11-20 15:38:30 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
assert entry.data[CONF_IP_ADDRESS] == "1.2.3.4"
|
2024-01-10 21:21:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovered_wifi_does_not_update_ip_online_but_access_denied(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> None:
|
|
|
|
"""Test a discovery does not update the ip unless the powerwall at the old ip is offline."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=VALID_CONFIG,
|
|
|
|
unique_id=MOCK_GATEWAY_DIN,
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
mock_powerwall = await _mock_powerwall_with_fixtures(hass)
|
|
|
|
mock_powerwall_no_access = await _mock_powerwall_with_fixtures(hass)
|
|
|
|
mock_powerwall_no_access.login.side_effect = AccessDeniedError("any")
|
|
|
|
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.config_flow.Powerwall",
|
|
|
|
return_value=mock_powerwall_no_access,
|
|
|
|
),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.powerwall.Powerwall", return_value=mock_powerwall
|
|
|
|
),
|
2024-01-10 21:21:53 +00:00
|
|
|
):
|
|
|
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# Now mock the powerwall to be offline to force
|
|
|
|
# the discovery flow to probe to see if its online
|
|
|
|
# which will result in an access denied error, which
|
|
|
|
# means its still online and we should not update the ip
|
2024-02-05 11:14:37 +00:00
|
|
|
mock_powerwall.get_meters.side_effect = TimeoutError
|
2024-01-10 21:21:53 +00:00
|
|
|
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=60))
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_DHCP},
|
|
|
|
data=dhcp.DhcpServiceInfo(
|
|
|
|
ip="1.2.3.5",
|
2024-02-14 22:11:25 +00:00
|
|
|
macaddress="aabbcceeddff",
|
2024-01-10 21:21:53 +00:00
|
|
|
hostname=MOCK_GATEWAY_DIN.lower(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2024-04-02 21:21:50 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2024-01-10 21:21:53 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
assert entry.data[CONF_IP_ADDRESS] == "1.2.3.4"
|