Skip to content

Commit cf97c68

Browse files
authored
fix(entity): split_code returns a user_id=1 if the code is lesser than 7 digits (#184)
1 parent d842e5e commit cf97c68

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

custom_components/econnect_metronet/helpers.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from typing import List, Tuple, Union
23

34
import voluptuous as vol
@@ -9,6 +10,8 @@
910

1011
from .const import CONF_SYSTEM_NAME, DOMAIN
1112

13+
_LOGGER = logging.getLogger(__name__)
14+
1215

1316
class select(multi_select):
1417
"""Extension for the multi_select helper to handle selections of tuples.
@@ -101,9 +104,13 @@ def split_code(code: str) -> Tuple[str, str]:
101104
CodeError: If the input code is less than 7 characters long, indicating it does not
102105
conform to the expected format.
103106
"""
104-
if len(code) <= 6:
107+
if not code:
105108
raise CodeError("Your code must be in the format <USER_ID><CODE> without spaces.")
106109

110+
if len(code) < 7:
111+
_LOGGER.debug("Client | Your configuration may require a code in the format <USER_ID><CODE> without spaces.")
112+
return "1", code
113+
107114
user_id_part, code_part = code[:-6], code[-6:]
108115
if not (user_id_part.isdigit() and code_part.isdigit()):
109116
raise CodeError("Both user ID and code must be numbers.")

tests/test_devices.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,7 +1283,7 @@ def test_device_arm_code_error_with_user_id(alarm_device, mocker):
12831283
# Test
12841284
alarm_device._connection._session_id = "test"
12851285
with pytest.raises(CodeError):
1286-
alarm_device.arm("1234", sectors=[4])
1286+
alarm_device.arm("asdf1234", sectors=[4])
12871287

12881288

12891289
def test_device_arm_error(client, mocker):
@@ -1408,7 +1408,7 @@ def test_device_disarm_code_error_with_user_id(alarm_device, mocker):
14081408
# Test
14091409
alarm_device._connection._session_id = "test"
14101410
with pytest.raises(CodeError):
1411-
alarm_device.disarm("1234", sectors=[4])
1411+
alarm_device.disarm("asdf1234", sectors=[4])
14121412

14131413

14141414
def test_device_disarm_error(client, mocker):

tests/test_helpers.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import logging
2+
13
import pytest
24
from elmo.api.exceptions import CodeError
35
from homeassistant.core import valid_entity_id
@@ -57,12 +59,13 @@ def test_split_code_with_valid_digits():
5759
assert split_code(code) == ("123456", "789012")
5860

5961

60-
def test_split_code_with_exact_six_chars_raises_error():
61-
# Should raise CodeError for code with less than 7 characters
62-
code = "123456"
63-
with pytest.raises(CodeError) as exc_info:
64-
split_code(code)
65-
assert "format <USER_ID><CODE> without spaces" in str(exc_info.value)
62+
def test_split_code_with_exact_six_chars_raises_error(caplog):
63+
# Should log a debug message and return a tuple with user ID 1 and the code
64+
caplog.set_level(logging.DEBUG)
65+
assert split_code("123456") == ("1", "123456")
66+
debug_msg = [record for record in caplog.records if record.levelname == "DEBUG"]
67+
assert len(debug_msg) == 1
68+
assert "format <USER_ID><CODE> without spaces" in debug_msg[0].message
6669

6770

6871
def test_split_code_with_alphanumeric_user_id_raises_error():

0 commit comments

Comments
 (0)