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,16 +1,13 @@
|
||||
"""Mocks for the august component."""
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
from unittest.mock import MagicMock, PropertyMock
|
||||
|
||||
from asynctest import mock
|
||||
from august.activity import Activity, DoorOperationActivity, LockOperationActivity
|
||||
from august.api import Api
|
||||
from august.activity import DoorOperationActivity, LockOperationActivity
|
||||
from august.authenticator import AuthenticationState
|
||||
from august.doorbell import Doorbell, DoorbellDetail
|
||||
from august.exceptions import AugustApiHTTPError
|
||||
from august.lock import Lock, LockDetail
|
||||
|
||||
from homeassistant.components.august import (
|
||||
@@ -18,10 +15,8 @@ from homeassistant.components.august import (
|
||||
CONF_PASSWORD,
|
||||
CONF_USERNAME,
|
||||
DOMAIN,
|
||||
AugustData,
|
||||
)
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import dt
|
||||
|
||||
from tests.common import load_fixture
|
||||
|
||||
@@ -37,8 +32,8 @@ def _mock_get_config():
|
||||
}
|
||||
|
||||
|
||||
@mock.patch("homeassistant.components.august.Api")
|
||||
@mock.patch("homeassistant.components.august.Authenticator.authenticate")
|
||||
@mock.patch("homeassistant.components.august.gateway.Api")
|
||||
@mock.patch("homeassistant.components.august.gateway.Authenticator.authenticate")
|
||||
async def _mock_setup_august(hass, api_instance, authenticate_mock, api_mock):
|
||||
"""Set up august integration."""
|
||||
authenticate_mock.side_effect = MagicMock(
|
||||
@@ -84,6 +79,9 @@ async def _create_august_with_devices(hass, devices, api_call_side_effects=None)
|
||||
def get_lock_detail_side_effect(access_token, device_id):
|
||||
return _get_device_detail("locks", device_id)
|
||||
|
||||
def get_doorbell_detail_side_effect(access_token, device_id):
|
||||
return _get_device_detail("doorbells", device_id)
|
||||
|
||||
def get_operable_locks_side_effect(access_token):
|
||||
return _get_base_devices("locks")
|
||||
|
||||
@@ -109,6 +107,8 @@ async def _create_august_with_devices(hass, devices, api_call_side_effects=None)
|
||||
|
||||
if "get_lock_detail" not in api_call_side_effects:
|
||||
api_call_side_effects["get_lock_detail"] = get_lock_detail_side_effect
|
||||
if "get_doorbell_detail" not in api_call_side_effects:
|
||||
api_call_side_effects["get_doorbell_detail"] = get_doorbell_detail_side_effect
|
||||
if "get_operable_locks" not in api_call_side_effects:
|
||||
api_call_side_effects["get_operable_locks"] = get_operable_locks_side_effect
|
||||
if "get_doorbells" not in api_call_side_effects:
|
||||
@@ -143,6 +143,11 @@ async def _mock_setup_august_with_api_side_effects(hass, api_call_side_effects):
|
||||
if api_call_side_effects["get_doorbells"]:
|
||||
api_instance.get_doorbells.side_effect = api_call_side_effects["get_doorbells"]
|
||||
|
||||
if api_call_side_effects["get_doorbell_detail"]:
|
||||
api_instance.get_doorbell_detail.side_effect = api_call_side_effects[
|
||||
"get_doorbell_detail"
|
||||
]
|
||||
|
||||
if api_call_side_effects["get_house_activities"]:
|
||||
api_instance.get_house_activities.side_effect = api_call_side_effects[
|
||||
"get_house_activities"
|
||||
@@ -160,106 +165,6 @@ async def _mock_setup_august_with_api_side_effects(hass, api_call_side_effects):
|
||||
return await _mock_setup_august(hass, api_instance)
|
||||
|
||||
|
||||
class MockAugustApiFailing(Api):
|
||||
"""A mock for py-august Api class that always has an AugustApiHTTPError."""
|
||||
|
||||
def _call_api(self, *args, **kwargs):
|
||||
"""Mock the time activity started."""
|
||||
raise AugustApiHTTPError("This should bubble up as its user consumable")
|
||||
|
||||
|
||||
class MockActivity(Activity):
|
||||
"""A mock for py-august Activity class."""
|
||||
|
||||
def __init__(
|
||||
self, action=None, activity_start_timestamp=None, activity_end_timestamp=None
|
||||
):
|
||||
"""Init the py-august Activity class mock."""
|
||||
self._action = action
|
||||
self._activity_start_timestamp = activity_start_timestamp
|
||||
self._activity_end_timestamp = activity_end_timestamp
|
||||
|
||||
@property
|
||||
def activity_start_time(self):
|
||||
"""Mock the time activity started."""
|
||||
return datetime.datetime.fromtimestamp(self._activity_start_timestamp)
|
||||
|
||||
@property
|
||||
def activity_end_time(self):
|
||||
"""Mock the time activity ended."""
|
||||
return datetime.datetime.fromtimestamp(self._activity_end_timestamp)
|
||||
|
||||
@property
|
||||
def action(self):
|
||||
"""Mock the action."""
|
||||
return self._action
|
||||
|
||||
|
||||
class MockAugustComponentData(AugustData):
|
||||
"""A wrapper to mock AugustData."""
|
||||
|
||||
# AugustData support multiple locks, however for the purposes of
|
||||
# mocking we currently only mock one lockid
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
last_lock_status_update_timestamp=1,
|
||||
last_door_state_update_timestamp=1,
|
||||
api=MockAugustApiFailing(),
|
||||
access_token="mocked_access_token",
|
||||
locks=[],
|
||||
doorbells=[],
|
||||
):
|
||||
"""Mock AugustData."""
|
||||
self._last_lock_status_update_time_utc = dt.as_utc(
|
||||
datetime.datetime.fromtimestamp(last_lock_status_update_timestamp)
|
||||
)
|
||||
self._last_door_state_update_time_utc = dt.as_utc(
|
||||
datetime.datetime.fromtimestamp(last_lock_status_update_timestamp)
|
||||
)
|
||||
self._api = api
|
||||
self._access_token = access_token
|
||||
self._locks = locks
|
||||
self._doorbells = doorbells
|
||||
self._lock_status_by_id = {}
|
||||
self._lock_last_status_update_time_utc_by_id = {}
|
||||
|
||||
def set_mocked_locks(self, locks):
|
||||
"""Set lock mocks."""
|
||||
self._locks = locks
|
||||
|
||||
def set_mocked_doorbells(self, doorbells):
|
||||
"""Set doorbell mocks."""
|
||||
self._doorbells = doorbells
|
||||
|
||||
def get_last_lock_status_update_time_utc(self, device_id):
|
||||
"""Mock to get last lock status update time."""
|
||||
return self._last_lock_status_update_time_utc
|
||||
|
||||
def set_last_lock_status_update_time_utc(self, device_id, update_time):
|
||||
"""Mock to set last lock status update time."""
|
||||
self._last_lock_status_update_time_utc = update_time
|
||||
|
||||
def get_last_door_state_update_time_utc(self, device_id):
|
||||
"""Mock to get last door state update time."""
|
||||
return self._last_door_state_update_time_utc
|
||||
|
||||
def set_last_door_state_update_time_utc(self, device_id, update_time):
|
||||
"""Mock to set last door state update time."""
|
||||
self._last_door_state_update_time_utc = update_time
|
||||
|
||||
|
||||
def _mock_august_authenticator():
|
||||
authenticator = MagicMock(name="august.authenticator")
|
||||
authenticator.should_refresh = MagicMock(
|
||||
name="august.authenticator.should_refresh", return_value=0
|
||||
)
|
||||
authenticator.refresh_access_token = MagicMock(
|
||||
name="august.authenticator.refresh_access_token"
|
||||
)
|
||||
return authenticator
|
||||
|
||||
|
||||
def _mock_august_authentication(token_text, token_timestamp):
|
||||
authentication = MagicMock(name="august.authentication")
|
||||
type(authentication).state = PropertyMock(
|
||||
@@ -321,20 +226,12 @@ def _mock_august_lock_data(lockid="mocklockid1", houseid="mockhouseid1"):
|
||||
}
|
||||
|
||||
|
||||
def _mock_operative_august_lock_detail(lockid):
|
||||
operative_lock_detail_data = _mock_august_lock_data(lockid=lockid)
|
||||
return LockDetail(operative_lock_detail_data)
|
||||
async def _mock_operative_august_lock_detail(hass):
|
||||
return await _mock_lock_from_fixture(hass, "get_lock.online.json")
|
||||
|
||||
|
||||
def _mock_inoperative_august_lock_detail(lockid):
|
||||
inoperative_lock_detail_data = _mock_august_lock_data(lockid=lockid)
|
||||
del inoperative_lock_detail_data["Bridge"]
|
||||
return LockDetail(inoperative_lock_detail_data)
|
||||
|
||||
|
||||
def _mock_doorsense_enabled_august_lock_detail(lockid):
|
||||
doorsense_lock_detail_data = _mock_august_lock_data(lockid=lockid)
|
||||
return LockDetail(doorsense_lock_detail_data)
|
||||
async def _mock_inoperative_august_lock_detail(hass):
|
||||
return await _mock_lock_from_fixture(hass, "get_lock.offline.json")
|
||||
|
||||
|
||||
async def _mock_lock_from_fixture(hass, path):
|
||||
@@ -354,10 +251,12 @@ async def _load_json_fixture(hass, path):
|
||||
return json.loads(fixture)
|
||||
|
||||
|
||||
def _mock_doorsense_missing_august_lock_detail(lockid):
|
||||
doorsense_lock_detail_data = _mock_august_lock_data(lockid=lockid)
|
||||
del doorsense_lock_detail_data["LockStatus"]["doorState"]
|
||||
return LockDetail(doorsense_lock_detail_data)
|
||||
async def _mock_doorsense_enabled_august_lock_detail(hass):
|
||||
return await _mock_lock_from_fixture(hass, "get_lock.online_with_doorsense.json")
|
||||
|
||||
|
||||
async def _mock_doorsense_missing_august_lock_detail(hass):
|
||||
return await _mock_lock_from_fixture(hass, "get_lock.online_missing_doorsense.json")
|
||||
|
||||
|
||||
def _mock_lock_operation_activity(lock, action):
|
||||
|
||||
@@ -9,6 +9,7 @@ from homeassistant.const import (
|
||||
SERVICE_UNLOCK,
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
STATE_UNAVAILABLE,
|
||||
)
|
||||
|
||||
from tests.components.august.mocks import (
|
||||
@@ -69,3 +70,21 @@ async def test_create_doorbell(hass):
|
||||
"binary_sensor.k98gidt45gul_name_ding"
|
||||
)
|
||||
assert binary_sensor_k98gidt45gul_name_ding.state == STATE_OFF
|
||||
|
||||
|
||||
async def test_create_doorbell_offline(hass):
|
||||
"""Test creation of a doorbell that is offline."""
|
||||
doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.offline.json")
|
||||
doorbell_details = [doorbell_one]
|
||||
await _create_august_with_devices(hass, doorbell_details)
|
||||
|
||||
binary_sensor_tmt100_name_motion = hass.states.get(
|
||||
"binary_sensor.tmt100_name_motion"
|
||||
)
|
||||
assert binary_sensor_tmt100_name_motion.state == STATE_UNAVAILABLE
|
||||
binary_sensor_tmt100_name_online = hass.states.get(
|
||||
"binary_sensor.tmt100_name_online"
|
||||
)
|
||||
assert binary_sensor_tmt100_name_online.state == STATE_OFF
|
||||
binary_sensor_tmt100_name_ding = hass.states.get("binary_sensor.tmt100_name_ding")
|
||||
assert binary_sensor_tmt100_name_ding.state == STATE_UNAVAILABLE
|
||||
@@ -0,0 +1,195 @@
|
||||
"""Test the August config flow."""
|
||||
from asynctest import patch
|
||||
from august.authenticator import ValidationResult
|
||||
|
||||
from homeassistant import config_entries, setup
|
||||
from homeassistant.components.august.const import (
|
||||
CONF_ACCESS_TOKEN_CACHE_FILE,
|
||||
CONF_INSTALL_ID,
|
||||
CONF_LOGIN_METHOD,
|
||||
DOMAIN,
|
||||
VERIFICATION_CODE_KEY,
|
||||
)
|
||||
from homeassistant.components.august.exceptions import (
|
||||
CannotConnect,
|
||||
InvalidAuth,
|
||||
RequireValidation,
|
||||
)
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_TIMEOUT, CONF_USERNAME
|
||||
|
||||
|
||||
async def test_form(hass):
|
||||
"""Test we get the form."""
|
||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] == "form"
|
||||
assert result["errors"] == {}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.august.config_flow.AugustGateway.authenticate",
|
||||
return_value=True,
|
||||
), patch(
|
||||
"homeassistant.components.august.async_setup", return_value=True
|
||||
) as mock_setup, patch(
|
||||
"homeassistant.components.august.async_setup_entry", return_value=True,
|
||||
) as mock_setup_entry:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_LOGIN_METHOD: "email",
|
||||
CONF_USERNAME: "my@email.tld",
|
||||
CONF_PASSWORD: "test-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] == "create_entry"
|
||||
assert result2["title"] == "my@email.tld"
|
||||
assert result2["data"] == {
|
||||
CONF_LOGIN_METHOD: "email",
|
||||
CONF_USERNAME: "my@email.tld",
|
||||
CONF_PASSWORD: "test-password",
|
||||
CONF_INSTALL_ID: None,
|
||||
CONF_TIMEOUT: 10,
|
||||
CONF_ACCESS_TOKEN_CACHE_FILE: ".my@email.tld.august.conf",
|
||||
}
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_invalid_auth(hass):
|
||||
"""Test we handle invalid auth."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.august.config_flow.AugustGateway.authenticate",
|
||||
side_effect=InvalidAuth,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_LOGIN_METHOD: "email",
|
||||
CONF_USERNAME: "my@email.tld",
|
||||
CONF_PASSWORD: "test-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] == "form"
|
||||
assert result2["errors"] == {"base": "invalid_auth"}
|
||||
|
||||
|
||||
async def test_form_cannot_connect(hass):
|
||||
"""Test we handle cannot connect error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.august.config_flow.AugustGateway.authenticate",
|
||||
side_effect=CannotConnect,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_LOGIN_METHOD: "email",
|
||||
CONF_USERNAME: "my@email.tld",
|
||||
CONF_PASSWORD: "test-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert result2["type"] == "form"
|
||||
assert result2["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
||||
async def test_form_needs_validate(hass):
|
||||
"""Test we present validation when we need to validate."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.august.config_flow.AugustGateway.authenticate",
|
||||
side_effect=RequireValidation,
|
||||
), patch(
|
||||
"homeassistant.components.august.gateway.Authenticator.send_verification_code",
|
||||
return_value=True,
|
||||
) as mock_send_verification_code:
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_LOGIN_METHOD: "email",
|
||||
CONF_USERNAME: "my@email.tld",
|
||||
CONF_PASSWORD: "test-password",
|
||||
},
|
||||
)
|
||||
|
||||
assert len(mock_send_verification_code.mock_calls) == 1
|
||||
assert result2["type"] == "form"
|
||||
assert result2["errors"] is None
|
||||
assert result2["step_id"] == "validation"
|
||||
|
||||
# Try with the WRONG verification code give us the form back again
|
||||
with patch(
|
||||
"homeassistant.components.august.config_flow.AugustGateway.authenticate",
|
||||
side_effect=RequireValidation,
|
||||
), patch(
|
||||
"homeassistant.components.august.gateway.Authenticator.validate_verification_code",
|
||||
return_value=ValidationResult.INVALID_VERIFICATION_CODE,
|
||||
) as mock_validate_verification_code, patch(
|
||||
"homeassistant.components.august.gateway.Authenticator.send_verification_code",
|
||||
return_value=True,
|
||||
) as mock_send_verification_code, patch(
|
||||
"homeassistant.components.august.async_setup", return_value=True
|
||||
) as mock_setup, patch(
|
||||
"homeassistant.components.august.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
result3 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {VERIFICATION_CODE_KEY: "incorrect"},
|
||||
)
|
||||
|
||||
# Make sure we do not resend the code again
|
||||
# so they have a chance to retry
|
||||
assert len(mock_send_verification_code.mock_calls) == 0
|
||||
assert len(mock_validate_verification_code.mock_calls) == 1
|
||||
assert result3["type"] == "form"
|
||||
assert result3["errors"] is None
|
||||
assert result3["step_id"] == "validation"
|
||||
|
||||
# Try with the CORRECT verification code and we setup
|
||||
with patch(
|
||||
"homeassistant.components.august.config_flow.AugustGateway.authenticate",
|
||||
return_value=True,
|
||||
), patch(
|
||||
"homeassistant.components.august.gateway.Authenticator.validate_verification_code",
|
||||
return_value=ValidationResult.VALIDATED,
|
||||
) as mock_validate_verification_code, patch(
|
||||
"homeassistant.components.august.gateway.Authenticator.send_verification_code",
|
||||
return_value=True,
|
||||
) as mock_send_verification_code, patch(
|
||||
"homeassistant.components.august.async_setup", return_value=True
|
||||
) as mock_setup, patch(
|
||||
"homeassistant.components.august.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
result4 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"], {VERIFICATION_CODE_KEY: "correct"},
|
||||
)
|
||||
|
||||
assert len(mock_send_verification_code.mock_calls) == 0
|
||||
assert len(mock_validate_verification_code.mock_calls) == 1
|
||||
assert result4["type"] == "create_entry"
|
||||
assert result4["title"] == "my@email.tld"
|
||||
assert result4["data"] == {
|
||||
CONF_LOGIN_METHOD: "email",
|
||||
CONF_USERNAME: "my@email.tld",
|
||||
CONF_PASSWORD: "test-password",
|
||||
CONF_INSTALL_ID: None,
|
||||
CONF_TIMEOUT: 10,
|
||||
CONF_ACCESS_TOKEN_CACHE_FILE: ".my@email.tld.august.conf",
|
||||
}
|
||||
await hass.async_block_till_done()
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
@@ -0,0 +1,49 @@
|
||||
"""The gateway tests for the august platform."""
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from asynctest import mock
|
||||
|
||||
from homeassistant.components.august.const import DOMAIN
|
||||
from homeassistant.components.august.gateway import AugustGateway
|
||||
|
||||
from tests.components.august.mocks import _mock_august_authentication, _mock_get_config
|
||||
|
||||
|
||||
async def test_refresh_access_token(hass):
|
||||
"""Test token refreshes."""
|
||||
await _patched_refresh_access_token(hass, "new_token", 5678)
|
||||
|
||||
|
||||
@mock.patch("homeassistant.components.august.gateway.Authenticator.authenticate")
|
||||
@mock.patch("homeassistant.components.august.gateway.Authenticator.should_refresh")
|
||||
@mock.patch(
|
||||
"homeassistant.components.august.gateway.Authenticator.refresh_access_token"
|
||||
)
|
||||
async def _patched_refresh_access_token(
|
||||
hass,
|
||||
new_token,
|
||||
new_token_expire_time,
|
||||
refresh_access_token_mock,
|
||||
should_refresh_mock,
|
||||
authenticate_mock,
|
||||
):
|
||||
authenticate_mock.side_effect = MagicMock(
|
||||
return_value=_mock_august_authentication("original_token", 1234)
|
||||
)
|
||||
august_gateway = AugustGateway(hass)
|
||||
mocked_config = _mock_get_config()
|
||||
august_gateway.async_setup(mocked_config[DOMAIN])
|
||||
august_gateway.authenticate()
|
||||
|
||||
should_refresh_mock.return_value = False
|
||||
await august_gateway.async_refresh_access_token_if_needed()
|
||||
refresh_access_token_mock.assert_not_called()
|
||||
|
||||
should_refresh_mock.return_value = True
|
||||
refresh_access_token_mock.return_value = _mock_august_authentication(
|
||||
new_token, new_token_expire_time
|
||||
)
|
||||
await august_gateway.async_refresh_access_token_if_needed()
|
||||
refresh_access_token_mock.assert_called()
|
||||
assert august_gateway.access_token == new_token
|
||||
assert august_gateway.authentication.access_token_expires == new_token_expire_time
|
||||
@@ -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",
|
||||
}
|
||||
@@ -6,45 +6,65 @@ from homeassistant.const import (
|
||||
SERVICE_LOCK,
|
||||
SERVICE_UNLOCK,
|
||||
STATE_LOCKED,
|
||||
STATE_UNAVAILABLE,
|
||||
STATE_UNLOCKED,
|
||||
)
|
||||
|
||||
from tests.components.august.mocks import (
|
||||
_create_august_with_devices,
|
||||
_mock_doorsense_enabled_august_lock_detail,
|
||||
_mock_lock_from_fixture,
|
||||
)
|
||||
|
||||
|
||||
async def test_one_lock_operation(hass):
|
||||
"""Test creation of a lock with doorsense and bridge."""
|
||||
lock_one = await _mock_lock_from_fixture(
|
||||
hass, "get_lock.online_with_doorsense.json"
|
||||
)
|
||||
lock_one = await _mock_doorsense_enabled_august_lock_detail(hass)
|
||||
lock_details = [lock_one]
|
||||
await _create_august_with_devices(hass, lock_details)
|
||||
|
||||
lock_abc_name = hass.states.get("lock.abc_name")
|
||||
lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name")
|
||||
|
||||
assert lock_abc_name.state == STATE_LOCKED
|
||||
assert lock_online_with_doorsense_name.state == STATE_LOCKED
|
||||
|
||||
assert lock_abc_name.attributes.get("battery_level") == 92
|
||||
assert lock_abc_name.attributes.get("friendly_name") == "ABC Name"
|
||||
assert lock_online_with_doorsense_name.attributes.get("battery_level") == 92
|
||||
assert (
|
||||
lock_online_with_doorsense_name.attributes.get("friendly_name")
|
||||
== "online_with_doorsense Name"
|
||||
)
|
||||
|
||||
data = {}
|
||||
data[ATTR_ENTITY_ID] = "lock.abc_name"
|
||||
data[ATTR_ENTITY_ID] = "lock.online_with_doorsense_name"
|
||||
assert await hass.services.async_call(
|
||||
LOCK_DOMAIN, SERVICE_UNLOCK, data, blocking=True
|
||||
)
|
||||
|
||||
lock_abc_name = hass.states.get("lock.abc_name")
|
||||
assert lock_abc_name.state == STATE_UNLOCKED
|
||||
lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name")
|
||||
assert lock_online_with_doorsense_name.state == STATE_UNLOCKED
|
||||
|
||||
assert lock_abc_name.attributes.get("battery_level") == 92
|
||||
assert lock_abc_name.attributes.get("friendly_name") == "ABC Name"
|
||||
assert lock_online_with_doorsense_name.attributes.get("battery_level") == 92
|
||||
assert (
|
||||
lock_online_with_doorsense_name.attributes.get("friendly_name")
|
||||
== "online_with_doorsense Name"
|
||||
)
|
||||
|
||||
assert await hass.services.async_call(
|
||||
LOCK_DOMAIN, SERVICE_LOCK, data, blocking=True
|
||||
)
|
||||
|
||||
lock_abc_name = hass.states.get("lock.abc_name")
|
||||
assert lock_abc_name.state == STATE_LOCKED
|
||||
lock_online_with_doorsense_name = hass.states.get("lock.online_with_doorsense_name")
|
||||
assert lock_online_with_doorsense_name.state == STATE_LOCKED
|
||||
|
||||
|
||||
async def test_one_lock_unknown_state(hass):
|
||||
"""Test creation of a lock with doorsense and bridge."""
|
||||
lock_one = await _mock_lock_from_fixture(
|
||||
hass, "get_lock.online.unknown_state.json",
|
||||
)
|
||||
lock_details = [lock_one]
|
||||
await _create_august_with_devices(hass, lock_details)
|
||||
|
||||
lock_brokenid_name = hass.states.get("lock.brokenid_name")
|
||||
# Once we have bridge_is_online support in py-august
|
||||
# this can change to STATE_UNKNOWN
|
||||
assert lock_brokenid_name.state == STATE_UNAVAILABLE
|
||||
@@ -0,0 +1,84 @@
|
||||
"""The sensor tests for the august platform."""
|
||||
|
||||
from tests.components.august.mocks import (
|
||||
_create_august_with_devices,
|
||||
_mock_doorbell_from_fixture,
|
||||
_mock_lock_from_fixture,
|
||||
)
|
||||
|
||||
|
||||
async def test_create_doorbell(hass):
|
||||
"""Test creation of a doorbell."""
|
||||
doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.json")
|
||||
await _create_august_with_devices(hass, [doorbell_one])
|
||||
|
||||
sensor_k98gidt45gul_name_battery = hass.states.get(
|
||||
"sensor.k98gidt45gul_name_battery"
|
||||
)
|
||||
assert sensor_k98gidt45gul_name_battery.state == "96"
|
||||
assert sensor_k98gidt45gul_name_battery.attributes["unit_of_measurement"] == "%"
|
||||
|
||||
|
||||
async def test_create_doorbell_offline(hass):
|
||||
"""Test creation of a doorbell that is offline."""
|
||||
doorbell_one = await _mock_doorbell_from_fixture(hass, "get_doorbell.offline.json")
|
||||
await _create_august_with_devices(hass, [doorbell_one])
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
sensor_tmt100_name_battery = hass.states.get("sensor.tmt100_name_battery")
|
||||
assert sensor_tmt100_name_battery.state == "81"
|
||||
assert sensor_tmt100_name_battery.attributes["unit_of_measurement"] == "%"
|
||||
|
||||
entry = entity_registry.async_get("sensor.tmt100_name_battery")
|
||||
assert entry
|
||||
assert entry.unique_id == "tmt100_device_battery"
|
||||
|
||||
|
||||
async def test_create_doorbell_hardwired(hass):
|
||||
"""Test creation of a doorbell that is hardwired without a battery."""
|
||||
doorbell_one = await _mock_doorbell_from_fixture(
|
||||
hass, "get_doorbell.nobattery.json"
|
||||
)
|
||||
await _create_august_with_devices(hass, [doorbell_one])
|
||||
|
||||
sensor_tmt100_name_battery = hass.states.get("sensor.tmt100_name_battery")
|
||||
assert sensor_tmt100_name_battery is None
|
||||
|
||||
|
||||
async def test_create_lock_with_linked_keypad(hass):
|
||||
"""Test creation of a lock with a linked keypad that both have a battery."""
|
||||
lock_one = await _mock_lock_from_fixture(hass, "get_lock.doorsense_init.json")
|
||||
await _create_august_with_devices(hass, [lock_one])
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
sensor_a6697750d607098bae8d6baa11ef8063_name_battery = hass.states.get(
|
||||
"sensor.a6697750d607098bae8d6baa11ef8063_name_battery"
|
||||
)
|
||||
assert sensor_a6697750d607098bae8d6baa11ef8063_name_battery.state == "88"
|
||||
assert (
|
||||
sensor_a6697750d607098bae8d6baa11ef8063_name_battery.attributes[
|
||||
"unit_of_measurement"
|
||||
]
|
||||
== "%"
|
||||
)
|
||||
entry = entity_registry.async_get(
|
||||
"sensor.a6697750d607098bae8d6baa11ef8063_name_battery"
|
||||
)
|
||||
assert entry
|
||||
assert entry.unique_id == "A6697750D607098BAE8D6BAA11EF8063_device_battery"
|
||||
|
||||
sensor_a6697750d607098bae8d6baa11ef8063_name_keypad_battery = hass.states.get(
|
||||
"sensor.a6697750d607098bae8d6baa11ef8063_name_keypad_battery"
|
||||
)
|
||||
assert sensor_a6697750d607098bae8d6baa11ef8063_name_keypad_battery.state == "60"
|
||||
assert (
|
||||
sensor_a6697750d607098bae8d6baa11ef8063_name_keypad_battery.attributes[
|
||||
"unit_of_measurement"
|
||||
]
|
||||
== "%"
|
||||
)
|
||||
entry = entity_registry.async_get(
|
||||
"sensor.a6697750d607098bae8d6baa11ef8063_name_keypad_battery"
|
||||
)
|
||||
assert entry
|
||||
assert entry.unique_id == "A6697750D607098BAE8D6BAA11EF8063_linked_keypad_battery"
|
||||
Reference in new issue
Block a user