init home-assistant
This commit is contained in:
1
home-assistant/tests/components/melcloud/__init__.py
Normal file
1
home-assistant/tests/components/melcloud/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Tests for the MELCloud integration."""
|
||||
@@ -0,0 +1,25 @@
|
||||
# serializer version: 1
|
||||
# name: test_get_config_entry_diagnostics
|
||||
dict({
|
||||
'entities': dict({
|
||||
}),
|
||||
'entry': dict({
|
||||
'data': dict({
|
||||
}),
|
||||
'disabled_by': None,
|
||||
'discovery_keys': dict({
|
||||
}),
|
||||
'domain': 'melcloud',
|
||||
'entry_id': 'TEST_ENTRY_ID',
|
||||
'minor_version': 1,
|
||||
'options': dict({
|
||||
}),
|
||||
'pref_disable_new_entities': False,
|
||||
'pref_disable_polling': False,
|
||||
'source': 'user',
|
||||
'title': 'melcloud',
|
||||
'unique_id': 'UNIQUE_TEST_ID',
|
||||
'version': 1,
|
||||
}),
|
||||
})
|
||||
# ---
|
||||
@@ -0,0 +1,50 @@
|
||||
"""Test the MELCloud ATW zone sensor."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.melcloud.sensor import ATW_ZONE_SENSORS, AtwZoneSensor
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_device():
|
||||
"""Mock MELCloud device."""
|
||||
with patch("homeassistant.components.melcloud.MelCloudDevice") as mock:
|
||||
mock.name = "name"
|
||||
mock.device.serial = 1234
|
||||
mock.device.mac = "11:11:11:11:11:11"
|
||||
yield mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_zone_1():
|
||||
"""Mock zone 1."""
|
||||
with patch("pymelcloud.atw_device.Zone") as mock:
|
||||
mock.zone_index = 1
|
||||
yield mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_zone_2():
|
||||
"""Mock zone 2."""
|
||||
with patch("pymelcloud.atw_device.Zone") as mock:
|
||||
mock.zone_index = 2
|
||||
yield mock
|
||||
|
||||
|
||||
def test_zone_unique_ids(mock_device, mock_zone_1, mock_zone_2) -> None:
|
||||
"""Test unique id generation correctness."""
|
||||
sensor_1 = AtwZoneSensor(
|
||||
mock_device,
|
||||
mock_zone_1,
|
||||
ATW_ZONE_SENSORS[0], # room_temperature
|
||||
)
|
||||
assert sensor_1.unique_id == "1234-11:11:11:11:11:11-room_temperature"
|
||||
|
||||
sensor_2 = AtwZoneSensor(
|
||||
mock_device,
|
||||
mock_zone_2,
|
||||
ATW_ZONE_SENSORS[0], # room_temperature
|
||||
)
|
||||
assert sensor_2.unique_id == "1234-11:11:11:11:11:11-room_temperature-zone-2"
|
||||
400
home-assistant/tests/components/melcloud/test_config_flow.py
Normal file
400
home-assistant/tests/components/melcloud/test_config_flow.py
Normal file
@@ -0,0 +1,400 @@
|
||||
"""Test the MELCloud config flow."""
|
||||
|
||||
from http import HTTPStatus
|
||||
from unittest.mock import patch
|
||||
|
||||
from aiohttp import ClientError, ClientResponseError
|
||||
import pymelcloud
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.melcloud.const import DOMAIN
|
||||
from homeassistant.const import CONF_PASSWORD
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_login():
|
||||
"""Mock pymelcloud login."""
|
||||
with patch(
|
||||
"homeassistant.components.melcloud.config_flow.pymelcloud.login"
|
||||
) as mock:
|
||||
mock.return_value = "test-token"
|
||||
yield mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_get_devices():
|
||||
"""Mock pymelcloud get_devices."""
|
||||
with patch(
|
||||
"homeassistant.components.melcloud.config_flow.pymelcloud.get_devices"
|
||||
) as mock:
|
||||
mock.return_value = {
|
||||
pymelcloud.DEVICE_TYPE_ATA: [],
|
||||
pymelcloud.DEVICE_TYPE_ATW: [],
|
||||
}
|
||||
yield mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_request_info():
|
||||
"""Mock RequestInfo to create ClientResponseErrors."""
|
||||
with patch("aiohttp.RequestInfo") as mock_ri:
|
||||
mock_ri.return_value.real_url.return_value = ""
|
||||
yield mock_ri
|
||||
|
||||
|
||||
async def test_form(hass: HomeAssistant, mock_login, mock_get_devices) -> None:
|
||||
"""Test we get the form."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] is None
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.melcloud.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{"username": "test-email@test-domain.com", "password": "test-password"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "test-email@test-domain.com"
|
||||
assert result2["data"] == {
|
||||
"username": "test-email@test-domain.com",
|
||||
"token": "test-token",
|
||||
}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("error", "reason"),
|
||||
[(ClientError(), "cannot_connect"), (TimeoutError(), "cannot_connect")],
|
||||
)
|
||||
async def test_form_errors(
|
||||
hass: HomeAssistant, mock_login, mock_get_devices, error, reason
|
||||
) -> None:
|
||||
"""Test we handle cannot connect error."""
|
||||
mock_login.side_effect = error
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
data={"username": "test-email@test-domain.com", "password": "test-password"},
|
||||
)
|
||||
|
||||
assert len(mock_login.mock_calls) == 1
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == reason
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("error", "message"),
|
||||
[
|
||||
(HTTPStatus.UNAUTHORIZED, "invalid_auth"),
|
||||
(HTTPStatus.FORBIDDEN, "invalid_auth"),
|
||||
(HTTPStatus.INTERNAL_SERVER_ERROR, "cannot_connect"),
|
||||
],
|
||||
)
|
||||
async def test_form_response_errors(
|
||||
hass: HomeAssistant, mock_login, mock_get_devices, mock_request_info, error, message
|
||||
) -> None:
|
||||
"""Test we handle response errors."""
|
||||
mock_login.side_effect = ClientResponseError(mock_request_info(), (), status=error)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
data={"username": "test-email@test-domain.com", "password": "test-password"},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == message
|
||||
|
||||
|
||||
async def test_token_refresh(hass: HomeAssistant, mock_login, mock_get_devices) -> None:
|
||||
"""Re-configuration with existing username should refresh token."""
|
||||
mock_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={"username": "test-email@test-domain.com", "token": "test-original-token"},
|
||||
unique_id="test-email@test-domain.com",
|
||||
)
|
||||
mock_entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.melcloud.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": config_entries.SOURCE_USER},
|
||||
data={
|
||||
"username": "test-email@test-domain.com",
|
||||
"password": "test-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_setup_entry.mock_calls) == 0
|
||||
|
||||
entries = hass.config_entries.async_entries(DOMAIN)
|
||||
assert len(entries) == 1
|
||||
|
||||
entry = entries[0]
|
||||
assert entry.data["username"] == "test-email@test-domain.com"
|
||||
assert entry.data["token"] == "test-token"
|
||||
|
||||
|
||||
async def test_token_reauthentication(
|
||||
hass: HomeAssistant,
|
||||
mock_login,
|
||||
mock_get_devices,
|
||||
) -> None:
|
||||
"""Re-configuration with existing username should refresh token, if made invalid."""
|
||||
mock_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={"username": "test-email@test-domain.com", "token": "test-original-token"},
|
||||
unique_id="test-email@test-domain.com",
|
||||
)
|
||||
mock_entry.add_to_hass(hass)
|
||||
|
||||
result = await mock_entry.start_reauth_flow(hass)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.melcloud.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{"username": "test-email@test-domain.com", "password": "test-password"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reauth_successful"
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("error", "reason"),
|
||||
[
|
||||
(TimeoutError(), "cannot_connect"),
|
||||
(AttributeError(name="get"), "invalid_auth"),
|
||||
],
|
||||
)
|
||||
async def test_form_errors_reauthentication(
|
||||
hass: HomeAssistant, mock_login, error, reason
|
||||
) -> None:
|
||||
"""Test we handle cannot connect error."""
|
||||
mock_login.side_effect = error
|
||||
mock_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={"username": "test-email@test-domain.com", "token": "test-original-token"},
|
||||
unique_id="test-email@test-domain.com",
|
||||
)
|
||||
mock_entry.add_to_hass(hass)
|
||||
|
||||
result = await mock_entry.start_reauth_flow(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.melcloud.async_setup_entry",
|
||||
return_value=True,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{"username": "test-email@test-domain.com", "password": "test-password"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"]["base"] == reason
|
||||
|
||||
mock_login.side_effect = None
|
||||
with patch(
|
||||
"homeassistant.components.melcloud.async_setup_entry",
|
||||
return_value=True,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{"username": "test-email@test-domain.com", "password": "test-password"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reauth_successful"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("error", "reason"),
|
||||
[
|
||||
(HTTPStatus.UNAUTHORIZED, "invalid_auth"),
|
||||
(HTTPStatus.FORBIDDEN, "invalid_auth"),
|
||||
(HTTPStatus.INTERNAL_SERVER_ERROR, "cannot_connect"),
|
||||
],
|
||||
)
|
||||
async def test_client_errors_reauthentication(
|
||||
hass: HomeAssistant, mock_login, mock_request_info, error, reason
|
||||
) -> None:
|
||||
"""Test we handle cannot connect error."""
|
||||
mock_login.side_effect = ClientResponseError(mock_request_info(), (), status=error)
|
||||
mock_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={"username": "test-email@test-domain.com", "token": "test-original-token"},
|
||||
unique_id="test-email@test-domain.com",
|
||||
)
|
||||
mock_entry.add_to_hass(hass)
|
||||
|
||||
result = await mock_entry.start_reauth_flow(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.melcloud.async_setup_entry",
|
||||
return_value=True,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{"username": "test-email@test-domain.com", "password": "test-password"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["errors"]["base"] == reason
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
|
||||
mock_login.side_effect = None
|
||||
with patch(
|
||||
"homeassistant.components.melcloud.async_setup_entry",
|
||||
return_value=True,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{"username": "test-email@test-domain.com", "password": "test-password"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reauth_successful"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("error", "reason"),
|
||||
[
|
||||
(HTTPStatus.UNAUTHORIZED, "invalid_auth"),
|
||||
(HTTPStatus.FORBIDDEN, "invalid_auth"),
|
||||
(HTTPStatus.INTERNAL_SERVER_ERROR, "cannot_connect"),
|
||||
],
|
||||
)
|
||||
async def test_reconfigure_flow(
|
||||
hass: HomeAssistant, mock_login, mock_request_info, error, reason
|
||||
) -> None:
|
||||
"""Test re-configuration flow."""
|
||||
mock_login.side_effect = ClientResponseError(mock_request_info(), (), status=error)
|
||||
mock_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={"username": "test-email@test-domain.com", "token": "test-original-token"},
|
||||
unique_id="test-email@test-domain.com",
|
||||
)
|
||||
mock_entry.add_to_hass(hass)
|
||||
|
||||
result = await mock_entry.start_reconfigure_flow(hass)
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.melcloud.async_setup_entry",
|
||||
return_value=True,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_PASSWORD: "test-password"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["errors"]["base"] == reason
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
|
||||
mock_login.side_effect = None
|
||||
with patch(
|
||||
"homeassistant.components.melcloud.async_setup_entry",
|
||||
return_value=True,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_PASSWORD: "test-password"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
entry = hass.config_entries.async_get_entry(mock_entry.entry_id)
|
||||
assert entry
|
||||
assert entry.title == "Mock Title"
|
||||
assert entry.data == {
|
||||
"username": "test-email@test-domain.com",
|
||||
"token": "test-token",
|
||||
"password": "test-password",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("error", "reason"),
|
||||
[
|
||||
(TimeoutError(), "cannot_connect"),
|
||||
(AttributeError(name="get"), "invalid_auth"),
|
||||
],
|
||||
)
|
||||
async def test_form_errors_reconfigure(
|
||||
hass: HomeAssistant, mock_login, error, reason
|
||||
) -> None:
|
||||
"""Test we handle cannot connect error."""
|
||||
mock_login.side_effect = error
|
||||
mock_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={"username": "test-email@test-domain.com", "token": "test-original-token"},
|
||||
unique_id="test-email@test-domain.com",
|
||||
)
|
||||
mock_entry.add_to_hass(hass)
|
||||
|
||||
result = await mock_entry.start_reconfigure_flow(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.melcloud.async_setup_entry",
|
||||
return_value=True,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_PASSWORD: "test-password"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"]["base"] == reason
|
||||
|
||||
mock_login.side_effect = None
|
||||
with patch(
|
||||
"homeassistant.components.melcloud.async_setup_entry",
|
||||
return_value=True,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{CONF_PASSWORD: "test-password"},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reconfigure_successful"
|
||||
entry = hass.config_entries.async_get_entry(mock_entry.entry_id)
|
||||
assert entry
|
||||
assert entry.title == "Mock Title"
|
||||
assert entry.data == {
|
||||
"username": "test-email@test-domain.com",
|
||||
"token": "test-token",
|
||||
"password": "test-password",
|
||||
}
|
||||
40
home-assistant/tests/components/melcloud/test_diagnostics.py
Normal file
40
home-assistant/tests/components/melcloud/test_diagnostics.py
Normal file
@@ -0,0 +1,40 @@
|
||||
"""Test the DSMR Reader component diagnostics."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from syrupy import SnapshotAssertion
|
||||
from syrupy.filters import props
|
||||
|
||||
from homeassistant.components.melcloud.const import DOMAIN
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||
from tests.typing import ClientSessionGenerator
|
||||
|
||||
|
||||
async def test_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
hass_client: ClientSessionGenerator,
|
||||
) -> None:
|
||||
"""Test if get_config_entry_diagnostics returns the correct data."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title=DOMAIN,
|
||||
options={},
|
||||
entry_id="TEST_ENTRY_ID",
|
||||
unique_id="UNIQUE_TEST_ID",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.melcloud.async_setup_entry", return_value=True
|
||||
):
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
diagnostics = await get_diagnostics_for_config_entry(
|
||||
hass, hass_client, config_entry
|
||||
)
|
||||
assert diagnostics == snapshot(exclude=props("created_at", "modified_at"))
|
||||
Reference in New Issue
Block a user