mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 10:17:51 +00:00

* Initial commit with errors * Commitable * Use triggercmd user id as hub name * Validate the token * Use switch type, no trigger yet * Working integration * Use triggercmd module instead of httpx * Add tests for triggercmd integration * Add triggercmd to requirements_test_all.txt * Add untested triggercmd files to .coveragerc * Implement cgarwood's PR suggestions * Address PR feedback * Update homeassistant/components/triggercmd/config_flow.py Co-authored-by: Robert Resch <robert@resch.dev> * Update homeassistant/components/triggercmd/hub.py Co-authored-by: Robert Resch <robert@resch.dev> * Update homeassistant/components/triggercmd/strings.json Co-authored-by: Robert Resch <robert@resch.dev> * Update homeassistant/components/triggercmd/hub.py Co-authored-by: Robert Resch <robert@resch.dev> * Get user id via triggercmd module, and better check for status 200 code * PR feedback fixes * Update homeassistant/components/triggercmd/switch.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/triggercmd/switch.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * More PR feedback fixes * Update homeassistant/components/triggercmd/config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/triggercmd/strings.json Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/triggercmd/switch.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * More PR feedback fixes * Update tests/components/triggercmd/test_config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Changes for PR feedback * Changes to address PR comments * Fix connection error when no internet * Update homeassistant/components/triggercmd/__init__.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/triggercmd/config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/triggercmd/config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/triggercmd/config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update tests/components/triggercmd/test_config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Updates for PR feedback * Update tests/components/triggercmd/test_config_flow.py --------- Co-authored-by: Robert Resch <robert@resch.dev> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
162 lines
5.0 KiB
Python
162 lines
5.0 KiB
Python
"""Define tests for the triggercmd config flow."""
|
|
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
from triggercmd import TRIGGERcmdConnectionError
|
|
|
|
from homeassistant.components.triggercmd.const import CONF_TOKEN, DOMAIN
|
|
from homeassistant.config_entries import SOURCE_USER
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.data_entry_flow import FlowResultType
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
invalid_token_with_length_100_or_more = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEyMzQ1Njc4OTBxd2VydHl1aW9wYXNkZiIsImlhdCI6MTcxOTg4MTU4M30.E4T2S4RQfuI2ww74sUkkT-wyTGrV5_VDkgUdae5yo4E"
|
|
invalid_token_id = "1234567890qwertyuiopasdf"
|
|
invalid_token_with_length_100_or_more_and_no_id = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJub2lkIjoiMTIzNDU2Nzg5MHF3ZXJ0eXVpb3Bhc2RmIiwiaWF0IjoxNzE5ODgxNTgzfQ.MaJLNWPGCE51Zibhbq-Yz7h3GkUxLurR2eoM2frnO6Y"
|
|
|
|
|
|
async def test_full_flow(
|
|
hass: HomeAssistant,
|
|
) -> None:
|
|
"""Test config flow happy path."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
context={"source": SOURCE_USER},
|
|
)
|
|
|
|
assert result["errors"] == {}
|
|
assert result["step_id"] == "user"
|
|
assert result["type"] is FlowResultType.FORM
|
|
|
|
with (
|
|
patch(
|
|
"homeassistant.components.triggercmd.client.async_connection_test",
|
|
return_value=200,
|
|
),
|
|
patch(
|
|
"homeassistant.components.triggercmd.ha.Hub",
|
|
),
|
|
):
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{CONF_TOKEN: invalid_token_with_length_100_or_more},
|
|
)
|
|
|
|
assert result["data"] == {CONF_TOKEN: invalid_token_with_length_100_or_more}
|
|
assert result["result"].unique_id == invalid_token_id
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("test_input", "expected"),
|
|
[
|
|
(invalid_token_with_length_100_or_more_and_no_id, {"base": "unknown"}),
|
|
("not-a-token", {CONF_TOKEN: "invalid_token"}),
|
|
],
|
|
)
|
|
async def test_config_flow_user_invalid_token(
|
|
hass: HomeAssistant,
|
|
test_input: str,
|
|
expected: dict,
|
|
) -> None:
|
|
"""Test the initial step of the config flow."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
context={"source": SOURCE_USER},
|
|
)
|
|
|
|
with (
|
|
patch(
|
|
"homeassistant.components.triggercmd.client.async_connection_test",
|
|
return_value=200,
|
|
),
|
|
patch(
|
|
"homeassistant.components.triggercmd.ha.Hub",
|
|
),
|
|
):
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{CONF_TOKEN: test_input},
|
|
)
|
|
|
|
assert result["errors"] == expected
|
|
assert result["step_id"] == "user"
|
|
assert result["type"] is FlowResultType.FORM
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{CONF_TOKEN: invalid_token_with_length_100_or_more},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
|
|
|
|
|
async def test_config_flow_entry_already_configured(hass: HomeAssistant) -> None:
|
|
"""Test user input for config_entry that already exists."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
context={"source": SOURCE_USER},
|
|
)
|
|
|
|
MockConfigEntry(
|
|
domain=DOMAIN,
|
|
data={CONF_TOKEN: invalid_token_with_length_100_or_more},
|
|
unique_id=invalid_token_id,
|
|
).add_to_hass(hass)
|
|
|
|
with (
|
|
patch(
|
|
"homeassistant.components.triggercmd.client.async_connection_test",
|
|
return_value=200,
|
|
),
|
|
):
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{CONF_TOKEN: invalid_token_with_length_100_or_more},
|
|
)
|
|
|
|
assert result["reason"] == "already_configured"
|
|
assert result["type"] is FlowResultType.ABORT
|
|
|
|
|
|
async def test_config_flow_connection_error(hass: HomeAssistant) -> None:
|
|
"""Test a connection error."""
|
|
result = await hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
context={"source": SOURCE_USER},
|
|
)
|
|
|
|
with (
|
|
patch(
|
|
"homeassistant.components.triggercmd.client.async_connection_test",
|
|
side_effect=TRIGGERcmdConnectionError,
|
|
),
|
|
):
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{CONF_TOKEN: invalid_token_with_length_100_or_more},
|
|
)
|
|
|
|
assert result["errors"] == {
|
|
"base": "connection_error",
|
|
}
|
|
assert result["type"] is FlowResultType.FORM
|
|
|
|
with (
|
|
patch(
|
|
"homeassistant.components.triggercmd.client.async_connection_test",
|
|
return_value=200,
|
|
),
|
|
patch(
|
|
"homeassistant.components.triggercmd.ha.Hub",
|
|
),
|
|
):
|
|
result = await hass.config_entries.flow.async_configure(
|
|
result["flow_id"],
|
|
{CONF_TOKEN: invalid_token_with_length_100_or_more},
|
|
)
|
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|