2020-02-12 06:13:54 +00:00
|
|
|
"""The tests for the august platform."""
|
2020-03-11 00:10:00 +00:00
|
|
|
import asyncio
|
|
|
|
|
2020-09-16 15:35:01 +00:00
|
|
|
from aiohttp import ClientResponseError
|
|
|
|
from august.authenticator_common import AuthenticationState
|
2020-03-09 20:54:05 +00:00
|
|
|
from august.exceptions import AugustApiAIOHTTPError
|
2020-02-25 18:18:15 +00:00
|
|
|
|
|
|
|
from homeassistant import setup
|
|
|
|
from homeassistant.components.august.const import (
|
|
|
|
CONF_ACCESS_TOKEN_CACHE_FILE,
|
|
|
|
CONF_INSTALL_ID,
|
|
|
|
CONF_LOGIN_METHOD,
|
|
|
|
DEFAULT_AUGUST_CONFIG_FILE,
|
2020-03-11 00:10:00 +00:00
|
|
|
DOMAIN,
|
2020-02-25 18:18:15 +00:00
|
|
|
)
|
|
|
|
from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN
|
2020-09-16 15:35:01 +00:00
|
|
|
from homeassistant.config_entries import (
|
|
|
|
ENTRY_STATE_SETUP_ERROR,
|
|
|
|
ENTRY_STATE_SETUP_RETRY,
|
|
|
|
)
|
2020-02-25 18:18:15 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_TIMEOUT,
|
|
|
|
CONF_USERNAME,
|
|
|
|
SERVICE_LOCK,
|
|
|
|
SERVICE_UNLOCK,
|
|
|
|
STATE_LOCKED,
|
|
|
|
STATE_ON,
|
|
|
|
)
|
2020-02-17 18:30:14 +00:00
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2020-02-25 18:18:15 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2020-02-12 06:13:54 +00:00
|
|
|
|
2020-04-30 20:29:50 +00:00
|
|
|
from tests.async_mock import patch
|
2020-03-11 00:10:00 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2020-02-12 23:35:07 +00:00
|
|
|
from tests.components.august.mocks import (
|
2020-02-25 18:18:15 +00:00
|
|
|
_create_august_with_devices,
|
2020-09-16 15:35:01 +00:00
|
|
|
_mock_august_authentication,
|
2020-02-18 20:11:05 +00:00
|
|
|
_mock_doorsense_enabled_august_lock_detail,
|
|
|
|
_mock_doorsense_missing_august_lock_detail,
|
2020-02-25 18:18:15 +00:00
|
|
|
_mock_get_config,
|
2020-02-18 20:11:05 +00:00
|
|
|
_mock_inoperative_august_lock_detail,
|
|
|
|
_mock_operative_august_lock_detail,
|
2020-02-12 23:35:07 +00:00
|
|
|
)
|
2020-02-12 06:13:54 +00:00
|
|
|
|
|
|
|
|
2020-03-11 00:10:00 +00:00
|
|
|
async def test_august_is_offline(hass):
|
|
|
|
"""Config entry state is ENTRY_STATE_SETUP_RETRY when august is offline."""
|
|
|
|
|
|
|
|
config_entry = MockConfigEntry(
|
2020-08-27 11:56:20 +00:00
|
|
|
domain=DOMAIN,
|
|
|
|
data=_mock_get_config()[DOMAIN],
|
|
|
|
title="August august",
|
2020-03-11 00:10:00 +00:00
|
|
|
)
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
await setup.async_setup_component(hass, "persistent_notification", {})
|
|
|
|
with patch(
|
|
|
|
"august.authenticator_async.AuthenticatorAsync.async_authenticate",
|
|
|
|
side_effect=asyncio.TimeoutError,
|
|
|
|
):
|
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
2020-09-16 15:35:01 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-03-11 00:10:00 +00:00
|
|
|
|
|
|
|
assert config_entry.state == ENTRY_STATE_SETUP_RETRY
|
|
|
|
|
|
|
|
|
2020-02-25 18:18:15 +00:00
|
|
|
async def test_unlock_throws_august_api_http_error(hass):
|
|
|
|
"""Test unlock throws correct error on http error."""
|
|
|
|
mocked_lock_detail = await _mock_operative_august_lock_detail(hass)
|
2020-02-17 18:30:14 +00:00
|
|
|
|
2020-02-25 18:18:15 +00:00
|
|
|
def _unlock_return_activities_side_effect(access_token, device_id):
|
2020-03-09 20:54:05 +00:00
|
|
|
raise AugustApiAIOHTTPError("This should bubble up as its user consumable")
|
2020-02-17 18:30:14 +00:00
|
|
|
|
2020-02-25 18:18:15 +00:00
|
|
|
await _create_august_with_devices(
|
|
|
|
hass,
|
|
|
|
[mocked_lock_detail],
|
|
|
|
api_call_side_effects={
|
|
|
|
"unlock_return_activities": _unlock_return_activities_side_effect
|
|
|
|
},
|
|
|
|
)
|
2020-02-17 18:30:14 +00:00
|
|
|
last_err = None
|
2020-02-25 18:18:15 +00:00
|
|
|
data = {ATTR_ENTITY_ID: "lock.a6697750d607098bae8d6baa11ef8063_name"}
|
2020-02-17 18:30:14 +00:00
|
|
|
try:
|
2020-02-25 18:18:15 +00:00
|
|
|
await hass.services.async_call(LOCK_DOMAIN, SERVICE_UNLOCK, data, blocking=True)
|
2020-02-17 18:30:14 +00:00
|
|
|
except HomeAssistantError as err:
|
|
|
|
last_err = err
|
|
|
|
assert (
|
2020-02-18 20:11:05 +00:00
|
|
|
str(last_err)
|
2020-02-25 18:18:15 +00:00
|
|
|
== "A6697750D607098BAE8D6BAA11EF8063 Name: This should bubble up as its user consumable"
|
2020-02-17 18:30:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-02-25 18:18:15 +00:00
|
|
|
async def test_lock_throws_august_api_http_error(hass):
|
|
|
|
"""Test lock throws correct error on http error."""
|
|
|
|
mocked_lock_detail = await _mock_operative_august_lock_detail(hass)
|
|
|
|
|
|
|
|
def _lock_return_activities_side_effect(access_token, device_id):
|
2020-03-09 20:54:05 +00:00
|
|
|
raise AugustApiAIOHTTPError("This should bubble up as its user consumable")
|
2020-02-25 18:18:15 +00:00
|
|
|
|
|
|
|
await _create_august_with_devices(
|
|
|
|
hass,
|
|
|
|
[mocked_lock_detail],
|
|
|
|
api_call_side_effects={
|
|
|
|
"lock_return_activities": _lock_return_activities_side_effect
|
|
|
|
},
|
|
|
|
)
|
2020-02-17 18:30:14 +00:00
|
|
|
last_err = None
|
2020-02-25 18:18:15 +00:00
|
|
|
data = {ATTR_ENTITY_ID: "lock.a6697750d607098bae8d6baa11ef8063_name"}
|
2020-02-17 18:30:14 +00:00
|
|
|
try:
|
2020-02-25 18:18:15 +00:00
|
|
|
await hass.services.async_call(LOCK_DOMAIN, SERVICE_LOCK, data, blocking=True)
|
2020-02-17 18:30:14 +00:00
|
|
|
except HomeAssistantError as err:
|
|
|
|
last_err = err
|
|
|
|
assert (
|
2020-02-18 20:11:05 +00:00
|
|
|
str(last_err)
|
2020-02-25 18:18:15 +00:00
|
|
|
== "A6697750D607098BAE8D6BAA11EF8063 Name: This should bubble up as its user consumable"
|
2020-02-18 20:11:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-02-25 18:18:15 +00:00
|
|
|
async def test_inoperative_locks_are_filtered_out(hass):
|
2020-02-18 20:11:05 +00:00
|
|
|
"""Ensure inoperative locks do not get setup."""
|
2020-02-25 18:18:15 +00:00
|
|
|
august_operative_lock = await _mock_operative_august_lock_detail(hass)
|
|
|
|
august_inoperative_lock = await _mock_inoperative_august_lock_detail(hass)
|
|
|
|
await _create_august_with_devices(
|
|
|
|
hass, [august_operative_lock, august_inoperative_lock]
|
2020-02-18 20:11:05 +00:00
|
|
|
)
|
|
|
|
|
2020-02-25 18:18:15 +00:00
|
|
|
lock_abc_name = hass.states.get("lock.abc_name")
|
|
|
|
assert lock_abc_name is None
|
|
|
|
lock_a6697750d607098bae8d6baa11ef8063_name = hass.states.get(
|
|
|
|
"lock.a6697750d607098bae8d6baa11ef8063_name"
|
2020-02-17 18:30:14 +00:00
|
|
|
)
|
2020-02-25 18:18:15 +00:00
|
|
|
assert lock_a6697750d607098bae8d6baa11ef8063_name.state == STATE_LOCKED
|
2020-02-17 18:30:14 +00:00
|
|
|
|
2020-02-18 20:11:05 +00:00
|
|
|
|
2020-02-25 18:18:15 +00:00
|
|
|
async def test_lock_has_doorsense(hass):
|
|
|
|
"""Check to see if a lock has doorsense."""
|
|
|
|
doorsenselock = await _mock_doorsense_enabled_august_lock_detail(hass)
|
|
|
|
nodoorsenselock = await _mock_doorsense_missing_august_lock_detail(hass)
|
|
|
|
await _create_august_with_devices(hass, [doorsenselock, nodoorsenselock])
|
2020-02-17 18:30:14 +00:00
|
|
|
|
2020-02-25 18:18:15 +00:00
|
|
|
binary_sensor_online_with_doorsense_name_open = hass.states.get(
|
|
|
|
"binary_sensor.online_with_doorsense_name_open"
|
2020-02-12 06:13:54 +00:00
|
|
|
)
|
2020-02-25 18:18:15 +00:00
|
|
|
assert binary_sensor_online_with_doorsense_name_open.state == STATE_ON
|
|
|
|
binary_sensor_missing_doorsense_id_name_open = hass.states.get(
|
|
|
|
"binary_sensor.missing_doorsense_id_name_open"
|
2020-02-18 20:11:05 +00:00
|
|
|
)
|
2020-02-25 18:18:15 +00:00
|
|
|
assert binary_sensor_missing_doorsense_id_name_open is None
|
|
|
|
|
|
|
|
|
|
|
|
async def test_set_up_from_yaml(hass):
|
|
|
|
"""Test to make sure config is imported from yaml."""
|
|
|
|
|
|
|
|
await setup.async_setup_component(hass, "persistent_notification", {})
|
|
|
|
with patch(
|
2020-08-27 11:56:20 +00:00
|
|
|
"homeassistant.components.august.async_setup_august",
|
|
|
|
return_value=True,
|
2020-02-25 18:18:15 +00:00
|
|
|
) as mock_setup_august, patch(
|
2020-03-09 20:54:05 +00:00
|
|
|
"homeassistant.components.august.config_flow.AugustGateway.async_authenticate",
|
2020-02-25 18:18:15 +00:00
|
|
|
return_value=True,
|
|
|
|
):
|
2020-03-11 00:10:00 +00:00
|
|
|
assert await async_setup_component(hass, DOMAIN, _mock_get_config())
|
2020-02-25 18:18:15 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(mock_setup_august.mock_calls) == 1
|
|
|
|
call = mock_setup_august.call_args
|
2020-09-16 15:35:01 +00:00
|
|
|
args, _ = call
|
2020-02-25 18:18:15 +00:00
|
|
|
imported_config_entry = args[1]
|
|
|
|
# The import must use DEFAULT_AUGUST_CONFIG_FILE so they
|
|
|
|
# do not loose their token when config is migrated
|
|
|
|
assert imported_config_entry.data == {
|
|
|
|
CONF_ACCESS_TOKEN_CACHE_FILE: DEFAULT_AUGUST_CONFIG_FILE,
|
|
|
|
CONF_INSTALL_ID: None,
|
|
|
|
CONF_LOGIN_METHOD: "email",
|
|
|
|
CONF_PASSWORD: "mocked_password",
|
|
|
|
CONF_TIMEOUT: None,
|
|
|
|
CONF_USERNAME: "mocked_username",
|
|
|
|
}
|
2020-09-16 15:35:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_auth_fails(hass):
|
|
|
|
"""Config entry state is ENTRY_STATE_SETUP_ERROR when auth fails."""
|
|
|
|
|
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=_mock_get_config()[DOMAIN],
|
|
|
|
title="August august",
|
|
|
|
)
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
assert hass.config_entries.flow.async_progress() == []
|
|
|
|
|
|
|
|
await setup.async_setup_component(hass, "persistent_notification", {})
|
|
|
|
with patch(
|
|
|
|
"august.authenticator_async.AuthenticatorAsync.async_authenticate",
|
|
|
|
side_effect=ClientResponseError(None, None, status=401),
|
|
|
|
):
|
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert config_entry.state == ENTRY_STATE_SETUP_ERROR
|
|
|
|
|
|
|
|
flows = hass.config_entries.flow.async_progress()
|
|
|
|
|
|
|
|
assert flows[0]["step_id"] == "user"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_bad_password(hass):
|
|
|
|
"""Config entry state is ENTRY_STATE_SETUP_ERROR when the password has been changed."""
|
|
|
|
|
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=_mock_get_config()[DOMAIN],
|
|
|
|
title="August august",
|
|
|
|
)
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
assert hass.config_entries.flow.async_progress() == []
|
|
|
|
|
|
|
|
await setup.async_setup_component(hass, "persistent_notification", {})
|
|
|
|
with patch(
|
|
|
|
"august.authenticator_async.AuthenticatorAsync.async_authenticate",
|
|
|
|
return_value=_mock_august_authentication(
|
|
|
|
"original_token", 1234, AuthenticationState.BAD_PASSWORD
|
|
|
|
),
|
|
|
|
):
|
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert config_entry.state == ENTRY_STATE_SETUP_ERROR
|
|
|
|
|
|
|
|
flows = hass.config_entries.flow.async_progress()
|
|
|
|
|
|
|
|
assert flows[0]["step_id"] == "user"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_http_failure(hass):
|
|
|
|
"""Config entry state is ENTRY_STATE_SETUP_RETRY when august is offline."""
|
|
|
|
|
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=_mock_get_config()[DOMAIN],
|
|
|
|
title="August august",
|
|
|
|
)
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
assert hass.config_entries.flow.async_progress() == []
|
|
|
|
|
|
|
|
await setup.async_setup_component(hass, "persistent_notification", {})
|
|
|
|
with patch(
|
|
|
|
"august.authenticator_async.AuthenticatorAsync.async_authenticate",
|
|
|
|
side_effect=ClientResponseError(None, None, status=500),
|
|
|
|
):
|
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert config_entry.state == ENTRY_STATE_SETUP_RETRY
|
|
|
|
|
|
|
|
assert hass.config_entries.flow.async_progress() == []
|
|
|
|
|
|
|
|
|
|
|
|
async def test_unknown_auth_state(hass):
|
|
|
|
"""Config entry state is ENTRY_STATE_SETUP_ERROR when august is in an unknown auth state."""
|
|
|
|
|
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=_mock_get_config()[DOMAIN],
|
|
|
|
title="August august",
|
|
|
|
)
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
assert hass.config_entries.flow.async_progress() == []
|
|
|
|
|
|
|
|
await setup.async_setup_component(hass, "persistent_notification", {})
|
|
|
|
with patch(
|
|
|
|
"august.authenticator_async.AuthenticatorAsync.async_authenticate",
|
|
|
|
return_value=_mock_august_authentication("original_token", 1234, None),
|
|
|
|
):
|
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert config_entry.state == ENTRY_STATE_SETUP_ERROR
|
|
|
|
|
|
|
|
flows = hass.config_entries.flow.async_progress()
|
|
|
|
|
|
|
|
assert flows[0]["step_id"] == "user"
|
|
|
|
|
|
|
|
|
|
|
|
async def test_requires_validation_state(hass):
|
|
|
|
"""Config entry state is ENTRY_STATE_SETUP_ERROR when august requires validation."""
|
|
|
|
|
|
|
|
config_entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data=_mock_get_config()[DOMAIN],
|
|
|
|
title="August august",
|
|
|
|
)
|
|
|
|
config_entry.add_to_hass(hass)
|
|
|
|
assert hass.config_entries.flow.async_progress() == []
|
|
|
|
|
|
|
|
await setup.async_setup_component(hass, "persistent_notification", {})
|
|
|
|
with patch(
|
|
|
|
"august.authenticator_async.AuthenticatorAsync.async_authenticate",
|
|
|
|
return_value=_mock_august_authentication(
|
|
|
|
"original_token", 1234, AuthenticationState.REQUIRES_VALIDATION
|
|
|
|
),
|
|
|
|
):
|
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert config_entry.state == ENTRY_STATE_SETUP_ERROR
|
|
|
|
|
|
|
|
assert hass.config_entries.flow.async_progress() == []
|