-
-
Notifications
You must be signed in to change notification settings - Fork 28.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unique ID to config entry in Luftdaten (#62176)
- Loading branch information
Showing
4 changed files
with
122 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
"""Fixtures for Luftdaten tests.""" | ||
from __future__ import annotations | ||
|
||
from collections.abc import Generator | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from homeassistant.components.luftdaten import DOMAIN | ||
from homeassistant.components.luftdaten.const import CONF_SENSOR_ID | ||
|
||
from tests.common import MockConfigEntry | ||
|
||
|
||
@pytest.fixture | ||
def mock_config_entry() -> MockConfigEntry: | ||
"""Return the default mocked config entry.""" | ||
return MockConfigEntry( | ||
title="12345", | ||
domain=DOMAIN, | ||
data={CONF_SENSOR_ID: 123456}, | ||
unique_id="12345", | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def mock_setup_entry() -> Generator[None, None, None]: | ||
"""Mock setting up a config entry.""" | ||
with patch( | ||
"homeassistant.components.luftdaten.async_setup_entry", return_value=True | ||
): | ||
yield |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,114 @@ | ||
"""Define tests for the Luftdaten config flow.""" | ||
from datetime import timedelta | ||
from unittest.mock import patch | ||
from unittest.mock import MagicMock, patch | ||
|
||
from homeassistant import data_entry_flow | ||
from homeassistant.components.luftdaten import DOMAIN, config_flow | ||
from luftdaten.exceptions import LuftdatenConnectionError | ||
|
||
from homeassistant.components.luftdaten import DOMAIN | ||
from homeassistant.components.luftdaten.const import CONF_SENSOR_ID | ||
from homeassistant.config_entries import SOURCE_USER | ||
from homeassistant.const import CONF_SCAN_INTERVAL, CONF_SHOW_ON_MAP | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.data_entry_flow import ( | ||
RESULT_TYPE_ABORT, | ||
RESULT_TYPE_CREATE_ENTRY, | ||
RESULT_TYPE_FORM, | ||
) | ||
|
||
from tests.common import MockConfigEntry | ||
|
||
|
||
async def test_duplicate_error(hass): | ||
async def test_duplicate_error( | ||
hass: HomeAssistant, mock_config_entry: MockConfigEntry | ||
) -> None: | ||
"""Test that errors are shown when duplicates are added.""" | ||
conf = {CONF_SENSOR_ID: "12345abcde"} | ||
mock_config_entry.add_to_hass(hass) | ||
|
||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": SOURCE_USER} | ||
) | ||
|
||
assert result.get("type") == RESULT_TYPE_FORM | ||
assert result.get("step_id") == SOURCE_USER | ||
assert "flow_id" in result | ||
|
||
MockConfigEntry(domain=DOMAIN, data=conf).add_to_hass(hass) | ||
flow = config_flow.LuftDatenFlowHandler() | ||
flow.hass = hass | ||
result2 = await hass.config_entries.flow.async_configure( | ||
result["flow_id"], | ||
user_input={CONF_SENSOR_ID: 12345}, | ||
) | ||
|
||
result = await flow.async_step_user(user_input=conf) | ||
assert result["errors"] == {CONF_SENSOR_ID: "already_configured"} | ||
assert result2.get("type") == RESULT_TYPE_ABORT | ||
assert result2.get("reason") == "already_configured" | ||
|
||
|
||
async def test_communication_error(hass): | ||
async def test_communication_error(hass: HomeAssistant) -> None: | ||
"""Test that no sensor is added while unable to communicate with API.""" | ||
conf = {CONF_SENSOR_ID: "12345abcde"} | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": SOURCE_USER} | ||
) | ||
|
||
flow = config_flow.LuftDatenFlowHandler() | ||
flow.hass = hass | ||
assert result.get("type") == RESULT_TYPE_FORM | ||
assert result.get("step_id") == SOURCE_USER | ||
assert "flow_id" in result | ||
|
||
with patch("luftdaten.Luftdaten.get_data", return_value=None): | ||
result = await flow.async_step_user(user_input=conf) | ||
assert result["errors"] == {CONF_SENSOR_ID: "invalid_sensor"} | ||
with patch("luftdaten.Luftdaten.get_data", side_effect=LuftdatenConnectionError): | ||
result2 = await hass.config_entries.flow.async_configure( | ||
result["flow_id"], | ||
user_input={CONF_SENSOR_ID: 12345}, | ||
) | ||
|
||
assert result2.get("type") == RESULT_TYPE_FORM | ||
assert result2.get("step_id") == SOURCE_USER | ||
assert result2.get("errors") == {CONF_SENSOR_ID: "cannot_connect"} | ||
|
||
async def test_invalid_sensor(hass): | ||
|
||
async def test_invalid_sensor(hass: HomeAssistant) -> None: | ||
"""Test that an invalid sensor throws an error.""" | ||
conf = {CONF_SENSOR_ID: "12345abcde"} | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": SOURCE_USER} | ||
) | ||
|
||
flow = config_flow.LuftDatenFlowHandler() | ||
flow.hass = hass | ||
assert result.get("type") == RESULT_TYPE_FORM | ||
assert result.get("step_id") == SOURCE_USER | ||
assert "flow_id" in result | ||
|
||
with patch("luftdaten.Luftdaten.get_data", return_value=False), patch( | ||
"luftdaten.Luftdaten.validate_sensor", return_value=False | ||
): | ||
result = await flow.async_step_user(user_input=conf) | ||
assert result["errors"] == {CONF_SENSOR_ID: "invalid_sensor"} | ||
|
||
|
||
async def test_show_form(hass): | ||
"""Test that the form is served with no input.""" | ||
flow = config_flow.LuftDatenFlowHandler() | ||
flow.hass = hass | ||
result2 = await hass.config_entries.flow.async_configure( | ||
result["flow_id"], | ||
user_input={CONF_SENSOR_ID: 12345}, | ||
) | ||
|
||
result = await flow.async_step_user(user_input=None) | ||
assert result2.get("type") == RESULT_TYPE_FORM | ||
assert result2.get("step_id") == SOURCE_USER | ||
assert result2.get("errors") == {CONF_SENSOR_ID: "invalid_sensor"} | ||
|
||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM | ||
assert result["step_id"] == "user" | ||
|
||
|
||
async def test_step_user(hass): | ||
async def test_step_user(hass: HomeAssistant, mock_setup_entry: MagicMock) -> None: | ||
"""Test that the user step works.""" | ||
conf = { | ||
CONF_SENSOR_ID: "12345abcde", | ||
CONF_SHOW_ON_MAP: False, | ||
CONF_SCAN_INTERVAL: timedelta(minutes=5), | ||
} | ||
result = await hass.config_entries.flow.async_init( | ||
DOMAIN, context={"source": SOURCE_USER} | ||
) | ||
|
||
flow = config_flow.LuftDatenFlowHandler() | ||
flow.hass = hass | ||
assert result.get("type") == RESULT_TYPE_FORM | ||
assert result.get("step_id") == SOURCE_USER | ||
assert "flow_id" in result | ||
|
||
with patch("luftdaten.Luftdaten.get_data", return_value=True), patch( | ||
"luftdaten.Luftdaten.validate_sensor", return_value=True | ||
): | ||
result = await flow.async_step_user(user_input=conf) | ||
|
||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY | ||
assert result["title"] == "12345abcde" | ||
assert result["data"] == { | ||
CONF_SENSOR_ID: "12345abcde", | ||
CONF_SHOW_ON_MAP: False, | ||
CONF_SCAN_INTERVAL: 300, | ||
} | ||
result2 = await hass.config_entries.flow.async_configure( | ||
result["flow_id"], | ||
user_input={ | ||
CONF_SENSOR_ID: 12345, | ||
CONF_SHOW_ON_MAP: False, | ||
}, | ||
) | ||
|
||
assert result2.get("type") == RESULT_TYPE_CREATE_ENTRY | ||
assert result2.get("title") == "12345" | ||
assert result2.get("data") == { | ||
CONF_SENSOR_ID: 12345, | ||
CONF_SHOW_ON_MAP: False, | ||
CONF_SCAN_INTERVAL: 600.0, | ||
} |