Skip to content

Commit

Permalink
Handle locks with encrypted codes
Browse files Browse the repository at this point in the history
  • Loading branch information
jason0x43 committed Mar 7, 2020
1 parent 2ecf4d4 commit f52b4b9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
9 changes: 6 additions & 3 deletions custom_components/hubitat/lock.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from logging import getLogger
from typing import Any, Dict, Optional, cast
from typing import Any, Dict, Optional, Union, cast

import hubitatmaker as hm
import voluptuous as vol
Expand Down Expand Up @@ -66,8 +66,11 @@ def code_length(self) -> Optional[int]:
return self.get_int_attr(hm.ATTR_CODE_LENGTH)

@property
def codes(self) -> Optional[Dict[str, Dict[str, str]]]:
return self.get_json_attr(hm.ATTR_LOCK_CODES)
def codes(self) -> Union[str, Dict[str, Dict[str, str]], None]:
try:
return self.get_json_attr(hm.ATTR_LOCK_CODES)
except Exception:
return self.get_str_attr(hm.ATTR_LOCK_CODES)

@property
def last_code_name(self) -> Optional[str]:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
import hubitatmaker
import pytest


@pytest.mark.asyncio
Expand All @@ -11,7 +11,7 @@ async def check_config():
check_called = True

FakeHub = mocker.patch.object(hubitatmaker, "Hub")
checker = mocker.patch.object(FakeHub.return_value, "check_config", check_config)
mocker.patch.object(FakeHub.return_value, "check_config", check_config)

from custom_components.hubitat import config_flow

Expand Down
37 changes: 37 additions & 0 deletions tests/test_lock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import hubitatmaker as hm


def test_normal_lock_codes(mocker) -> None:
hub = mocker.MagicMock()
device = mocker.MagicMock()
device.attributes = {
hm.ATTR_LOCK_CODES: hm.Attribute(
{
"name": hm.ATTR_LOCK_CODES,
"currentValue": '{"1":{"name":"Test","code":"1234"}}',
}
)
}

from custom_components.hubitat.lock import HubitatLock

lock = HubitatLock(hub=hub, device=device)
assert isinstance(lock.codes, dict)


def test_encrypted_lock_codes(mocker) -> None:
hub = mocker.MagicMock()
device = mocker.MagicMock()
device.attributes = {
hm.ATTR_LOCK_CODES: hm.Attribute(
{"name": hm.ATTR_LOCK_CODES, "currentValue": "abc1235Qbxyz"}
)
}

from custom_components.hubitat.lock import HubitatLock

lock = HubitatLock(hub=hub, device=device)

# A lock with encrypted codes should return a string for the `codes`
# property
assert isinstance(lock.codes, str)

0 comments on commit f52b4b9

Please sign in to comment.