mirror of
https://github.com/home-assistant/core.git
synced 2026-11-30 01:33:12 -04:00
Add usercode support to totalconnect (#39199)
* Add test for invalid usercode * Add usercodes to totalconnect. * Update existing tests for usercodes * Fix tests * Add test for invalid usercode * Add usercodes to totalconnect. * Update existing tests for usercodes * Fix tests * Remove YAML support * Fix conflict * Bump to total_connect_client 0.56 * Change Exception to HomeAssistantError * Fix config_flow.py * Simplify async_setup since no yaml * Remove import from config flow and tests * Add reauth and test for it. Various other fixes. * Fix pylint in __init__ * Show config yaml as deprecated * separate config_flow and init tests * Assert ENTRY_STATE_SETUP_ERROR in init test * Add test for reauth flow * Fix reauth and tests * Fix strings * Restore username and usercode with new passord * Correct the integration name * Update tests/components/totalconnect/test_config_flow.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update tests/components/totalconnect/test_init.py Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * Update .coveragerc * Add test for invalid auth during reauth * Bump total-connect-client to 0.57 * Fix .coveragerc * More tests for usercodes * Fix usercode test * Reload config entry on reauth Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
co-authored by
Martin Hjelmare
parent
d61d39de08
commit
e5aef45bd7
@@ -3,7 +3,7 @@ from unittest.mock import patch
|
||||
|
||||
from total_connect_client import TotalConnectClient
|
||||
|
||||
from homeassistant.components.totalconnect import DOMAIN
|
||||
from homeassistant.components.totalconnect.const import CONF_USERCODES, DOMAIN
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
@@ -29,13 +29,19 @@ USER = {
|
||||
}
|
||||
|
||||
RESPONSE_AUTHENTICATE = {
|
||||
"ResultCode": 0,
|
||||
"ResultCode": TotalConnectClient.TotalConnectClient.SUCCESS,
|
||||
"SessionID": 1,
|
||||
"Locations": LOCATIONS,
|
||||
"ModuleFlags": MODULE_FLAGS,
|
||||
"UserInfo": USER,
|
||||
}
|
||||
|
||||
RESPONSE_AUTHENTICATE_FAILED = {
|
||||
"ResultCode": TotalConnectClient.TotalConnectClient.BAD_USER_OR_PASSWORD,
|
||||
"ResultData": "test bad authentication",
|
||||
}
|
||||
|
||||
|
||||
PARTITION_DISARMED = {
|
||||
"PartitionID": "1",
|
||||
"ArmingState": TotalConnectClient.TotalConnectLocation.DISARMED,
|
||||
@@ -101,6 +107,32 @@ RESPONSE_DISARM_FAILURE = {
|
||||
"ResultCode": TotalConnectClient.TotalConnectClient.COMMAND_FAILED,
|
||||
"ResultData": "Command Failed",
|
||||
}
|
||||
RESPONSE_USER_CODE_INVALID = {
|
||||
"ResultCode": TotalConnectClient.TotalConnectClient.USER_CODE_INVALID,
|
||||
"ResultData": "testing user code invalid",
|
||||
}
|
||||
RESPONSE_SUCCESS = {"ResultCode": TotalConnectClient.TotalConnectClient.SUCCESS}
|
||||
|
||||
USERNAME = "username@me.com"
|
||||
PASSWORD = "password"
|
||||
USERCODES = {123456: "7890"}
|
||||
CONFIG_DATA = {
|
||||
CONF_USERNAME: USERNAME,
|
||||
CONF_PASSWORD: PASSWORD,
|
||||
CONF_USERCODES: USERCODES,
|
||||
}
|
||||
CONFIG_DATA_NO_USERCODES = {CONF_USERNAME: USERNAME, CONF_PASSWORD: PASSWORD}
|
||||
|
||||
|
||||
USERNAME = "username@me.com"
|
||||
PASSWORD = "password"
|
||||
USERCODES = {123456: "7890"}
|
||||
CONFIG_DATA = {
|
||||
CONF_USERNAME: USERNAME,
|
||||
CONF_PASSWORD: PASSWORD,
|
||||
CONF_USERCODES: USERCODES,
|
||||
}
|
||||
CONFIG_DATA_NO_USERCODES = {CONF_USERNAME: USERNAME, CONF_PASSWORD: PASSWORD}
|
||||
|
||||
|
||||
async def setup_platform(hass, platform):
|
||||
@@ -108,7 +140,7 @@ async def setup_platform(hass, platform):
|
||||
# first set up a config entry and add it to hass
|
||||
mock_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={CONF_USERNAME: "user@email.com", CONF_PASSWORD: "password"},
|
||||
data=CONFIG_DATA,
|
||||
)
|
||||
mock_entry.add_to_hass(hass)
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ from homeassistant.const import (
|
||||
STATE_ALARM_ARMED_HOME,
|
||||
STATE_ALARM_DISARMED,
|
||||
)
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
|
||||
from .common import (
|
||||
RESPONSE_ARM_FAILURE,
|
||||
@@ -23,6 +24,7 @@ from .common import (
|
||||
RESPONSE_DISARM_FAILURE,
|
||||
RESPONSE_DISARM_SUCCESS,
|
||||
RESPONSE_DISARMED,
|
||||
RESPONSE_USER_CODE_INVALID,
|
||||
setup_platform,
|
||||
)
|
||||
|
||||
@@ -72,12 +74,31 @@ async def test_arm_home_failure(hass):
|
||||
await setup_platform(hass, ALARM_DOMAIN)
|
||||
assert STATE_ALARM_DISARMED == hass.states.get(ENTITY_ID).state
|
||||
|
||||
with pytest.raises(Exception) as e:
|
||||
with pytest.raises(HomeAssistantError) as err:
|
||||
await hass.services.async_call(
|
||||
ALARM_DOMAIN, SERVICE_ALARM_ARM_HOME, DATA, blocking=True
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert f"{e.value}" == "TotalConnect failed to arm home test."
|
||||
assert f"{err.value}" == "TotalConnect failed to arm home test."
|
||||
assert STATE_ALARM_DISARMED == hass.states.get(ENTITY_ID).state
|
||||
|
||||
|
||||
async def test_arm_home_invalid_usercode(hass):
|
||||
"""Test arm home method with invalid usercode."""
|
||||
responses = [RESPONSE_DISARMED, RESPONSE_USER_CODE_INVALID, RESPONSE_DISARMED]
|
||||
with patch(
|
||||
"homeassistant.components.totalconnect.TotalConnectClient.TotalConnectClient.request",
|
||||
side_effect=responses,
|
||||
):
|
||||
await setup_platform(hass, ALARM_DOMAIN)
|
||||
assert STATE_ALARM_DISARMED == hass.states.get(ENTITY_ID).state
|
||||
|
||||
with pytest.raises(HomeAssistantError) as err:
|
||||
await hass.services.async_call(
|
||||
ALARM_DOMAIN, SERVICE_ALARM_ARM_HOME, DATA, blocking=True
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert f"{err.value}" == "TotalConnect failed to arm home test."
|
||||
assert STATE_ALARM_DISARMED == hass.states.get(ENTITY_ID).state
|
||||
|
||||
|
||||
@@ -108,12 +129,12 @@ async def test_arm_away_failure(hass):
|
||||
await setup_platform(hass, ALARM_DOMAIN)
|
||||
assert STATE_ALARM_DISARMED == hass.states.get(ENTITY_ID).state
|
||||
|
||||
with pytest.raises(Exception) as e:
|
||||
with pytest.raises(HomeAssistantError) as err:
|
||||
await hass.services.async_call(
|
||||
ALARM_DOMAIN, SERVICE_ALARM_ARM_AWAY, DATA, blocking=True
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert f"{e.value}" == "TotalConnect failed to arm away test."
|
||||
assert f"{err.value}" == "TotalConnect failed to arm away test."
|
||||
assert STATE_ALARM_DISARMED == hass.states.get(ENTITY_ID).state
|
||||
|
||||
|
||||
@@ -144,10 +165,29 @@ async def test_disarm_failure(hass):
|
||||
await setup_platform(hass, ALARM_DOMAIN)
|
||||
assert STATE_ALARM_ARMED_AWAY == hass.states.get(ENTITY_ID).state
|
||||
|
||||
with pytest.raises(Exception) as e:
|
||||
with pytest.raises(HomeAssistantError) as err:
|
||||
await hass.services.async_call(
|
||||
ALARM_DOMAIN, SERVICE_ALARM_DISARM, DATA, blocking=True
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert f"{e.value}" == "TotalConnect failed to disarm test."
|
||||
assert f"{err.value}" == "TotalConnect failed to disarm test."
|
||||
assert STATE_ALARM_ARMED_AWAY == hass.states.get(ENTITY_ID).state
|
||||
|
||||
|
||||
async def test_disarm_invalid_usercode(hass):
|
||||
"""Test disarm method failure."""
|
||||
responses = [RESPONSE_ARMED_AWAY, RESPONSE_USER_CODE_INVALID, RESPONSE_ARMED_AWAY]
|
||||
with patch(
|
||||
"homeassistant.components.totalconnect.TotalConnectClient.TotalConnectClient.request",
|
||||
side_effect=responses,
|
||||
):
|
||||
await setup_platform(hass, ALARM_DOMAIN)
|
||||
assert STATE_ALARM_ARMED_AWAY == hass.states.get(ENTITY_ID).state
|
||||
|
||||
with pytest.raises(HomeAssistantError) as err:
|
||||
await hass.services.async_call(
|
||||
ALARM_DOMAIN, SERVICE_ALARM_DISARM, DATA, blocking=True
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert f"{err.value}" == "TotalConnect failed to disarm test."
|
||||
assert STATE_ALARM_ARMED_AWAY == hass.states.get(ENTITY_ID).state
|
||||
|
||||
@@ -1,78 +1,97 @@
|
||||
"""Tests for the iCloud config flow."""
|
||||
"""Tests for the TotalConnect config flow."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant import data_entry_flow
|
||||
from homeassistant.components.totalconnect.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.components.totalconnect.const import CONF_LOCATION, DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_PASSWORD
|
||||
|
||||
from .common import (
|
||||
CONFIG_DATA,
|
||||
CONFIG_DATA_NO_USERCODES,
|
||||
RESPONSE_AUTHENTICATE,
|
||||
RESPONSE_DISARMED,
|
||||
RESPONSE_SUCCESS,
|
||||
RESPONSE_USER_CODE_INVALID,
|
||||
USERNAME,
|
||||
)
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
USERNAME = "username@me.com"
|
||||
PASSWORD = "password"
|
||||
|
||||
|
||||
async def test_user(hass):
|
||||
"""Test user config."""
|
||||
# no data provided so show the form
|
||||
"""Test user step."""
|
||||
# user starts with no data entered, so show the user form
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data=None,
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
# now data is provided, so check if login is correct and create the entry
|
||||
with patch(
|
||||
"homeassistant.components.totalconnect.config_flow.TotalConnectClient.TotalConnectClient"
|
||||
) as client_mock:
|
||||
client_mock.return_value.is_valid_credentials.return_value = True
|
||||
|
||||
async def test_user_show_locations(hass):
|
||||
"""Test user locations form."""
|
||||
# user/pass provided, so check if valid then ask for usercodes on locations form
|
||||
responses = [
|
||||
RESPONSE_AUTHENTICATE,
|
||||
RESPONSE_DISARMED,
|
||||
RESPONSE_USER_CODE_INVALID,
|
||||
RESPONSE_SUCCESS,
|
||||
]
|
||||
|
||||
with patch("zeep.Client", autospec=True), patch(
|
||||
"homeassistant.components.totalconnect.TotalConnectClient.TotalConnectClient.request",
|
||||
side_effect=responses,
|
||||
) as mock_request, patch(
|
||||
"homeassistant.components.totalconnect.TotalConnectClient.TotalConnectClient.get_zone_details",
|
||||
return_value=True,
|
||||
), patch(
|
||||
"homeassistant.components.totalconnect.async_setup_entry", return_value=True
|
||||
):
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data={CONF_USERNAME: USERNAME, CONF_PASSWORD: PASSWORD},
|
||||
data=CONFIG_DATA_NO_USERCODES,
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
# first it should show the locations form
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "locations"
|
||||
# client should have sent two requests, authenticate and get status
|
||||
assert mock_request.call_count == 2
|
||||
|
||||
|
||||
async def test_import(hass):
|
||||
"""Test import step with good username and password."""
|
||||
with patch(
|
||||
"homeassistant.components.totalconnect.config_flow.TotalConnectClient.TotalConnectClient"
|
||||
) as client_mock:
|
||||
client_mock.return_value.is_valid_credentials.return_value = True
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
data={CONF_USERNAME: USERNAME, CONF_PASSWORD: PASSWORD},
|
||||
# user enters an invalid usercode
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_LOCATION: "bad"},
|
||||
)
|
||||
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result2["step_id"] == "locations"
|
||||
# client should have sent 3rd request to validate usercode
|
||||
assert mock_request.call_count == 3
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
# user enters a valid usercode
|
||||
result3 = await hass.config_entries.flow.async_configure(
|
||||
result2["flow_id"],
|
||||
user_input={CONF_LOCATION: "7890"},
|
||||
)
|
||||
assert result3["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||
# client should have sent another request to validate usercode
|
||||
assert mock_request.call_count == 4
|
||||
|
||||
|
||||
async def test_abort_if_already_setup(hass):
|
||||
"""Test abort if the account is already setup."""
|
||||
MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={CONF_USERNAME: USERNAME, CONF_PASSWORD: PASSWORD},
|
||||
data=CONFIG_DATA,
|
||||
unique_id=USERNAME,
|
||||
).add_to_hass(hass)
|
||||
|
||||
# Should fail, same USERNAME (import)
|
||||
with patch(
|
||||
"homeassistant.components.totalconnect.config_flow.TotalConnectClient.TotalConnectClient"
|
||||
) as client_mock:
|
||||
client_mock.return_value.is_valid_credentials.return_value = True
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
data={CONF_USERNAME: USERNAME, CONF_PASSWORD: PASSWORD},
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "already_configured"
|
||||
|
||||
# Should fail, same USERNAME (flow)
|
||||
with patch(
|
||||
"homeassistant.components.totalconnect.config_flow.TotalConnectClient.TotalConnectClient"
|
||||
@@ -81,7 +100,7 @@ async def test_abort_if_already_setup(hass):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data={CONF_USERNAME: USERNAME, CONF_PASSWORD: PASSWORD},
|
||||
data=CONFIG_DATA,
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
@@ -97,8 +116,51 @@ async def test_login_failed(hass):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_USER},
|
||||
data={CONF_USERNAME: USERNAME, CONF_PASSWORD: PASSWORD},
|
||||
data=CONFIG_DATA,
|
||||
)
|
||||
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["errors"] == {"base": "invalid_auth"}
|
||||
|
||||
|
||||
async def test_reauth(hass):
|
||||
"""Test reauth."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=CONFIG_DATA,
|
||||
unique_id=USERNAME,
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": "reauth"}, data=entry.data
|
||||
)
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(result["flow_id"])
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.totalconnect.config_flow.TotalConnectClient.TotalConnectClient"
|
||||
) as client_mock:
|
||||
# first test with an invalid password
|
||||
client_mock.return_value.is_valid_credentials.return_value = False
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={CONF_PASSWORD: "password"}
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
assert result["step_id"] == "reauth_confirm"
|
||||
assert result["errors"] == {"base": "invalid_auth"}
|
||||
|
||||
# now test with the password valid
|
||||
client_mock.return_value.is_valid_credentials.return_value = True
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], user_input={CONF_PASSWORD: "password"}
|
||||
)
|
||||
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
||||
assert result["reason"] == "reauth_successful"
|
||||
|
||||
assert len(hass.config_entries.async_entries()) == 1
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
"""Tests for the TotalConnect init process."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.components.totalconnect.const import DOMAIN
|
||||
from homeassistant.config_entries import ENTRY_STATE_SETUP_ERROR
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from .common import CONFIG_DATA
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_reauth_started(hass):
|
||||
"""Test that reauth is started when we have login errors."""
|
||||
mock_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=CONFIG_DATA,
|
||||
)
|
||||
mock_entry.add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.totalconnect.TotalConnectClient.TotalConnectClient",
|
||||
autospec=True,
|
||||
) as mock_client:
|
||||
mock_client.return_value.is_valid_credentials.return_value = False
|
||||
assert await async_setup_component(hass, DOMAIN, {})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_entry.state == ENTRY_STATE_SETUP_ERROR
|
||||
Reference in New Issue
Block a user