Add Config flow to august (#32133)
* Add battery sensors for August devices * Additional tests and cleanup in prep for config flow and device registry * pylint * update name for new style guidelines - https://developers.home-assistant.io/docs/development_guidelines/#use-new-style-string-formatting * Config Flow for august push * Update homeassistant/components/august/__init__.py Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io> * Address review items * Update tests Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
1 parent
900714a3ee
commit
2925e0617c
25 files changed
+1686
-444
No files matched your search
@@ -1,136 +1,146 @@
|
||||
"""The tests for the august platform."""
|
||||
import asyncio
|
||||
from unittest.mock import MagicMock
|
||||
from asynctest import patch
|
||||
from august.exceptions import AugustApiHTTPError
|
||||
|
||||
from august.lock import LockDetail
|
||||
from requests import RequestException
|
||||
|
||||
from homeassistant.components import august
|
||||
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,
|
||||
)
|
||||
from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
CONF_PASSWORD,
|
||||
CONF_TIMEOUT,
|
||||
CONF_USERNAME,
|
||||
SERVICE_LOCK,
|
||||
SERVICE_UNLOCK,
|
||||
STATE_LOCKED,
|
||||
STATE_ON,
|
||||
)
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from tests.components.august.mocks import (
|
||||
MockAugustApiFailing,
|
||||
MockAugustComponentData,
|
||||
_mock_august_authentication,
|
||||
_mock_august_authenticator,
|
||||
_mock_august_lock,
|
||||
_create_august_with_devices,
|
||||
_mock_doorsense_enabled_august_lock_detail,
|
||||
_mock_doorsense_missing_august_lock_detail,
|
||||
_mock_get_config,
|
||||
_mock_inoperative_august_lock_detail,
|
||||
_mock_operative_august_lock_detail,
|
||||
)
|
||||
|
||||
|
||||
def test_get_lock_name():
|
||||
"""Get the lock name from August data."""
|
||||
data = MockAugustComponentData(last_lock_status_update_timestamp=1)
|
||||
lock = _mock_august_lock()
|
||||
data.set_mocked_locks([lock])
|
||||
assert data.get_lock_name("mocklockid1") == "mocklockid1 Name"
|
||||
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)
|
||||
|
||||
def _unlock_return_activities_side_effect(access_token, device_id):
|
||||
raise AugustApiHTTPError("This should bubble up as its user consumable")
|
||||
|
||||
def test_unlock_throws_august_api_http_error():
|
||||
"""Test unlock."""
|
||||
data = MockAugustComponentData(api=MockAugustApiFailing())
|
||||
lock = _mock_august_lock()
|
||||
data.set_mocked_locks([lock])
|
||||
await _create_august_with_devices(
|
||||
hass,
|
||||
[mocked_lock_detail],
|
||||
api_call_side_effects={
|
||||
"unlock_return_activities": _unlock_return_activities_side_effect
|
||||
},
|
||||
)
|
||||
last_err = None
|
||||
data = {ATTR_ENTITY_ID: "lock.a6697750d607098bae8d6baa11ef8063_name"}
|
||||
try:
|
||||
data.unlock("mocklockid1")
|
||||
await hass.services.async_call(LOCK_DOMAIN, SERVICE_UNLOCK, data, blocking=True)
|
||||
except HomeAssistantError as err:
|
||||
last_err = err
|
||||
assert (
|
||||
str(last_err)
|
||||
== "mocklockid1 Name: This should bubble up as its user consumable"
|
||||
== "A6697750D607098BAE8D6BAA11EF8063 Name: This should bubble up as its user consumable"
|
||||
)
|
||||
|
||||
|
||||
def test_lock_throws_august_api_http_error():
|
||||
"""Test lock."""
|
||||
data = MockAugustComponentData(api=MockAugustApiFailing())
|
||||
lock = _mock_august_lock()
|
||||
data.set_mocked_locks([lock])
|
||||
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):
|
||||
raise AugustApiHTTPError("This should bubble up as its user consumable")
|
||||
|
||||
await _create_august_with_devices(
|
||||
hass,
|
||||
[mocked_lock_detail],
|
||||
api_call_side_effects={
|
||||
"lock_return_activities": _lock_return_activities_side_effect
|
||||
},
|
||||
)
|
||||
last_err = None
|
||||
data = {ATTR_ENTITY_ID: "lock.a6697750d607098bae8d6baa11ef8063_name"}
|
||||
try:
|
||||
data.unlock("mocklockid1")
|
||||
await hass.services.async_call(LOCK_DOMAIN, SERVICE_LOCK, data, blocking=True)
|
||||
except HomeAssistantError as err:
|
||||
last_err = err
|
||||
assert (
|
||||
str(last_err)
|
||||
== "mocklockid1 Name: This should bubble up as its user consumable"
|
||||
== "A6697750D607098BAE8D6BAA11EF8063 Name: This should bubble up as its user consumable"
|
||||
)
|
||||
|
||||
|
||||
def test_inoperative_locks_are_filtered_out():
|
||||
async def test_inoperative_locks_are_filtered_out(hass):
|
||||
"""Ensure inoperative locks do not get setup."""
|
||||
august_operative_lock = _mock_operative_august_lock_detail("oplockid1")
|
||||
data = _create_august_data_with_lock_details(
|
||||
[august_operative_lock, _mock_inoperative_august_lock_detail("inoplockid1")]
|
||||
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]
|
||||
)
|
||||
|
||||
assert len(data.locks) == 1
|
||||
assert data.locks[0].device_id == "oplockid1"
|
||||
lock_abc_name = hass.states.get("lock.abc_name")
|
||||
assert lock_abc_name is None
|
||||
lock_a6697750d607098bae8d6baa11ef8063_name = hass.states.get(
|
||||
"lock.a6697750d607098bae8d6baa11ef8063_name"
|
||||
)
|
||||
assert lock_a6697750d607098bae8d6baa11ef8063_name.state == STATE_LOCKED
|
||||
|
||||
|
||||
def test_lock_has_doorsense():
|
||||
async def test_lock_has_doorsense(hass):
|
||||
"""Check to see if a lock has doorsense."""
|
||||
data = _create_august_data_with_lock_details(
|
||||
[
|
||||
_mock_doorsense_enabled_august_lock_detail("doorsenselock1"),
|
||||
_mock_doorsense_missing_august_lock_detail("nodoorsenselock1"),
|
||||
RequestException("mocked request error"),
|
||||
RequestException("mocked request error"),
|
||||
]
|
||||
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])
|
||||
|
||||
binary_sensor_online_with_doorsense_name_open = hass.states.get(
|
||||
"binary_sensor.online_with_doorsense_name_open"
|
||||
)
|
||||
|
||||
assert data.lock_has_doorsense("doorsenselock1") is True
|
||||
assert data.lock_has_doorsense("nodoorsenselock1") is False
|
||||
|
||||
# The api calls are mocked to fail on the second
|
||||
# run of async_get_lock_detail
|
||||
#
|
||||
# This will be switched to await data.async_get_lock_detail("doorsenselock1")
|
||||
# once we mock the full home assistant setup
|
||||
data._update_locks_detail()
|
||||
# doorsenselock1 should be false if we cannot tell due
|
||||
# to an api error
|
||||
assert data.lock_has_doorsense("doorsenselock1") is False
|
||||
|
||||
|
||||
async def test__refresh_access_token(hass):
|
||||
"""Test refresh of the access token."""
|
||||
authentication = _mock_august_authentication("original_token", 1234)
|
||||
authenticator = _mock_august_authenticator()
|
||||
token_refresh_lock = asyncio.Lock()
|
||||
|
||||
data = august.AugustData(
|
||||
hass, MagicMock(name="api"), authentication, authenticator, token_refresh_lock
|
||||
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"
|
||||
)
|
||||
await data._async_refresh_access_token_if_needed()
|
||||
authenticator.refresh_access_token.assert_not_called()
|
||||
|
||||
authenticator.should_refresh.return_value = 1
|
||||
authenticator.refresh_access_token.return_value = _mock_august_authentication(
|
||||
"new_token", 5678
|
||||
)
|
||||
await data._async_refresh_access_token_if_needed()
|
||||
authenticator.refresh_access_token.assert_called()
|
||||
assert data._access_token == "new_token"
|
||||
assert data._access_token_expires == 5678
|
||||
assert binary_sensor_missing_doorsense_id_name_open is None
|
||||
|
||||
|
||||
def _create_august_data_with_lock_details(lock_details):
|
||||
locks = []
|
||||
for lock in lock_details:
|
||||
if isinstance(lock, LockDetail):
|
||||
locks.append(_mock_august_lock(lock.device_id))
|
||||
authentication = _mock_august_authentication("original_token", 1234)
|
||||
authenticator = _mock_august_authenticator()
|
||||
token_refresh_lock = MagicMock()
|
||||
api = MagicMock()
|
||||
api.get_lock_detail = MagicMock(side_effect=lock_details)
|
||||
api.get_operable_locks = MagicMock(return_value=locks)
|
||||
api.get_doorbells = MagicMock(return_value=[])
|
||||
return august.AugustData(
|
||||
MagicMock(), api, authentication, authenticator, token_refresh_lock
|
||||
)
|
||||
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(
|
||||
"homeassistant.components.august.async_setup_august", return_value=True,
|
||||
) as mock_setup_august, patch(
|
||||
"homeassistant.components.august.config_flow.AugustGateway.authenticate",
|
||||
return_value=True,
|
||||
):
|
||||
mocked_config = _mock_get_config()
|
||||
assert await async_setup_component(hass, "august", mocked_config)
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_setup_august.mock_calls) == 1
|
||||
call = mock_setup_august.call_args
|
||||
args, kwargs = call
|
||||
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",
|
||||
}
|
||||
Reference in new issue
Block a user