init home-assistant
This commit is contained in:
9
home-assistant/tests/components/switchbee/__init__.py
Normal file
9
home-assistant/tests/components/switchbee/__init__.py
Normal file
@@ -0,0 +1,9 @@
|
||||
"""Tests for the SwitchBee Smart Home integration."""
|
||||
|
||||
MOCK_FAILED_TO_LOGIN_MSG = (
|
||||
"Central Unit replied with failure: {'status': 'LOGIN_FAILED'}"
|
||||
)
|
||||
MOCK_INVALID_TOKEN_MGS = (
|
||||
"Error fetching switchbee data: Error communicating with API: data Request failed"
|
||||
" due to INVALID_TOKEN, trying to re-login"
|
||||
)
|
||||
@@ -0,0 +1,135 @@
|
||||
{
|
||||
"status": "OK",
|
||||
"data": {
|
||||
"mac": "A8-21-08-E7-67-B6",
|
||||
"name": "Residence",
|
||||
"version": "1.4.4(4)",
|
||||
"lastConfChange": 1661856874511,
|
||||
"zones": [
|
||||
{
|
||||
"name": "Sensor Setting",
|
||||
"items": [
|
||||
{
|
||||
"id": 200000,
|
||||
"name": "home",
|
||||
"hw": "VIRTUAL",
|
||||
"type": "ALARM_SYSTEM"
|
||||
},
|
||||
{
|
||||
"id": 200010,
|
||||
"name": "away",
|
||||
"hw": "VIRTUAL",
|
||||
"type": "ALARM_SYSTEM"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "General",
|
||||
"items": [
|
||||
{
|
||||
"operations": [113],
|
||||
"id": 100080,
|
||||
"name": "All Lights",
|
||||
"hw": "VIRTUAL",
|
||||
"type": "GROUP_SWITCH"
|
||||
},
|
||||
{
|
||||
"operations": [
|
||||
{ "itemId": 21, "value": 100 },
|
||||
{ "itemId": 333, "value": 100 }
|
||||
],
|
||||
"id": 100160,
|
||||
"name": "Sunrise",
|
||||
"hw": "VIRTUAL",
|
||||
"type": "SCENARIO"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Entrance",
|
||||
"items": [
|
||||
{
|
||||
"id": 113,
|
||||
"name": "Staircase Lights",
|
||||
"hw": "DIMMABLE_SWITCH",
|
||||
"type": "TIMED_SWITCH"
|
||||
},
|
||||
{
|
||||
"id": 222,
|
||||
"name": "Front Door",
|
||||
"hw": "REGULAR_SWITCH",
|
||||
"type": "TIMED_SWITCH"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Kitchen",
|
||||
"items": [
|
||||
{ "id": 21, "name": "Shutter ", "hw": "SHUTTER", "type": "SHUTTER" },
|
||||
{
|
||||
"operations": [593, 581, 171],
|
||||
"id": 481,
|
||||
"name": "Leds",
|
||||
"hw": "DIMMABLE_SWITCH",
|
||||
"type": "GROUP_SWITCH"
|
||||
},
|
||||
{
|
||||
"id": 12,
|
||||
"name": "Walls",
|
||||
"hw": "DIMMABLE_SWITCH",
|
||||
"type": "DIMMER"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Two Way Zone",
|
||||
"items": [
|
||||
{
|
||||
"operations": [113],
|
||||
"id": 72,
|
||||
"name": "Staircase Lights",
|
||||
"hw": "DIMMABLE_SWITCH",
|
||||
"type": "TWO_WAY"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Facilities ",
|
||||
"items": [
|
||||
{
|
||||
"id": 321,
|
||||
"name": "Boiler",
|
||||
"hw": "TIMED_POWER_SWITCH",
|
||||
"type": "TIMED_POWER"
|
||||
},
|
||||
{
|
||||
"modes": ["COOL", "HEAT", "FAN"],
|
||||
"temperatureUnits": "CELSIUS",
|
||||
"id": 271,
|
||||
"name": "HVAC",
|
||||
"hw": "THERMOSTAT",
|
||||
"type": "THERMOSTAT"
|
||||
},
|
||||
{
|
||||
"id": 571,
|
||||
"name": "Repeater",
|
||||
"hw": "REPEATER",
|
||||
"type": "REPEATER"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Alarm",
|
||||
"items": [
|
||||
{
|
||||
"operations": [{ "itemId": 113, "value": 100 }],
|
||||
"id": 81,
|
||||
"name": "Open Home",
|
||||
"hw": "STIKER_SWITCH",
|
||||
"type": "SCENARIO"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
181
home-assistant/tests/components/switchbee/test_config_flow.py
Normal file
181
home-assistant/tests/components/switchbee/test_config_flow.py
Normal file
@@ -0,0 +1,181 @@
|
||||
"""Test the SwitchBee Smart Home config flow."""
|
||||
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.switchbee.config_flow import SwitchBeeError
|
||||
from homeassistant.components.switchbee.const import DOMAIN
|
||||
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from . import MOCK_FAILED_TO_LOGIN_MSG, MOCK_INVALID_TOKEN_MGS
|
||||
|
||||
from tests.common import MockConfigEntry, load_fixture
|
||||
|
||||
|
||||
@pytest.mark.parametrize("test_cucode_in_coordinator_data", [False, True])
|
||||
async def test_form(hass: HomeAssistant, test_cucode_in_coordinator_data) -> None:
|
||||
"""Test we get the form."""
|
||||
|
||||
coordinator_data = json.loads(load_fixture("switchbee.json", "switchbee"))
|
||||
|
||||
if test_cucode_in_coordinator_data:
|
||||
coordinator_data["data"]["cuCode"] = "300F123456"
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
with (
|
||||
patch(
|
||||
"switchbee.api.polling.CentralUnitPolling.get_configuration",
|
||||
return_value=coordinator_data,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.switchbee.async_setup_entry",
|
||||
return_value=True,
|
||||
),
|
||||
patch(
|
||||
"switchbee.api.polling.CentralUnitPolling.fetch_states", return_value=None
|
||||
),
|
||||
patch("switchbee.api.polling.CentralUnitPolling._login", return_value=None),
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: "1.1.1.1",
|
||||
CONF_USERNAME: "test-username",
|
||||
CONF_PASSWORD: "test-password",
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == "1.1.1.1"
|
||||
assert result2["data"] == {
|
||||
CONF_HOST: "1.1.1.1",
|
||||
CONF_USERNAME: "test-username",
|
||||
CONF_PASSWORD: "test-password",
|
||||
}
|
||||
|
||||
|
||||
async def test_form_invalid_auth(hass: HomeAssistant) -> None:
|
||||
"""Test we handle invalid auth."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"switchbee.api.polling.CentralUnitPolling._login",
|
||||
side_effect=SwitchBeeError(MOCK_FAILED_TO_LOGIN_MSG),
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: "1.1.1.1",
|
||||
CONF_USERNAME: "test-username",
|
||||
CONF_PASSWORD: "test-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
assert result2["errors"] == {"base": "invalid_auth"}
|
||||
|
||||
|
||||
async def test_form_cannot_connect(hass: HomeAssistant) -> None:
|
||||
"""Test we handle cannot connect error."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"switchbee.api.polling.CentralUnitPolling._login",
|
||||
side_effect=SwitchBeeError(MOCK_INVALID_TOKEN_MGS),
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: "1.1.1.1",
|
||||
CONF_USERNAME: "test-username",
|
||||
CONF_PASSWORD: "test-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
assert result2["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
||||
async def test_form_unknown_error(hass: HomeAssistant) -> None:
|
||||
"""Test we handle an unknown error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"switchbee.api.polling.CentralUnitPolling._login",
|
||||
side_effect=Exception,
|
||||
):
|
||||
form_result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: "1.1.1.1",
|
||||
CONF_USERNAME: "test-username",
|
||||
CONF_PASSWORD: "test-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert form_result["type"] is FlowResultType.FORM
|
||||
assert form_result["errors"] == {"base": "unknown"}
|
||||
|
||||
|
||||
async def test_form_entry_exists(hass: HomeAssistant) -> None:
|
||||
"""Test we handle an already existing entry."""
|
||||
|
||||
coordinator_data = json.loads(load_fixture("switchbee.json", "switchbee"))
|
||||
MockConfigEntry(
|
||||
unique_id="a8:21:08:e7:67:b6",
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
CONF_HOST: "1.1.1.1",
|
||||
CONF_USERNAME: "test-username",
|
||||
CONF_PASSWORD: "test-password",
|
||||
},
|
||||
title="1.1.1.1",
|
||||
).add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with (
|
||||
patch("switchbee.api.polling.CentralUnitPolling._login", return_value=None),
|
||||
patch(
|
||||
"homeassistant.components.switchbee.async_setup_entry",
|
||||
return_value=True,
|
||||
),
|
||||
patch(
|
||||
"switchbee.api.polling.CentralUnitPolling.get_configuration",
|
||||
return_value=coordinator_data,
|
||||
),
|
||||
patch(
|
||||
"switchbee.api.polling.CentralUnitPolling.fetch_states", return_value=None
|
||||
),
|
||||
):
|
||||
form_result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_HOST: "1.2.2.2",
|
||||
CONF_USERNAME: "test-username",
|
||||
CONF_PASSWORD: "test-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert form_result["type"] is FlowResultType.ABORT
|
||||
assert form_result["reason"] == "already_configured"
|
||||
Reference in New Issue
Block a user