Skip to content

Commit 98c1311

Browse files
authored
Add light level to ambient luminance conversion for Hub2 Illuminate entity (#290)
* feat: calculate hub2 light intensity (cherry picked from commit be2a418) * checkout * feat: refactor light intensity calculation using a constant map * fix: light intensity calculation and update tests * docs: add docstring for light intensity mapping in hub2.py
1 parent 6e977fd commit 98c1311

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

switchbot/adv_parsers/hub2.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from typing import Any
66

7+
from ..const.hub2 import LIGHT_INTENSITY_MAP
8+
79

810
def process_wohub2(data: bytes | None, mfr_data: bytes | None) -> dict[str, Any]:
911
"""Process woHub2 sensor manufacturer data."""
@@ -35,6 +37,20 @@ def process_wohub2(data: bytes | None, mfr_data: bytes | None) -> dict[str, Any]
3537
"fahrenheit": bool(temp_data[2] & 0b10000000),
3638
"humidity": humidity,
3739
"lightLevel": light_level,
40+
"illuminance": calculate_light_intensity(light_level),
3841
}
3942

4043
return _wohub2_data
44+
45+
46+
def calculate_light_intensity(light_level: int) -> int:
47+
"""
48+
Convert Hub 2 light level (1-21) to actual light intensity value
49+
Args:
50+
light_level: Integer from 1-21
51+
Returns:
52+
Corresponding light intensity value or 0 if invalid input
53+
"""
54+
if not light_level:
55+
return 0
56+
return LIGHT_INTENSITY_MAP.get(max(0, min(light_level, 22)), 0)

switchbot/const/hub2.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Mapping of light levels to lux measurement values for SwitchBot Hub 2.
3+
4+
Source: After-sales consultation, line chart data provided by switchbot developers
5+
"""
6+
7+
LIGHT_INTENSITY_MAP = {
8+
1: 0,
9+
2: 10,
10+
3: 20,
11+
4: 30,
12+
5: 40,
13+
6: 50,
14+
7: 60,
15+
8: 70,
16+
9: 80,
17+
10: 90,
18+
11: 105,
19+
12: 205,
20+
13: 317,
21+
14: 416,
22+
15: 510,
23+
16: 610,
24+
17: 707,
25+
18: 801,
26+
19: 897,
27+
20: 1023,
28+
21: 1091,
29+
}

tests/test_adv_parser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,7 @@ def test_wohub2_passive_and_active():
947947
"fahrenheit": False,
948948
"humidity": 50,
949949
"lightLevel": 2,
950+
"illuminance": 10,
950951
"temp": {"c": 26.7, "f": 80.06},
951952
"temperature": 26.7,
952953
},

tests/test_hub2.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from switchbot.adv_parsers.hub2 import calculate_light_intensity
2+
3+
4+
def test_calculate_light_intensity():
5+
"""Test calculating light intensity from Hub 2 light level."""
6+
# Test valid inputs
7+
assert calculate_light_intensity(1) == 0
8+
assert calculate_light_intensity(2) == 10
9+
assert calculate_light_intensity(10) == 90
10+
assert calculate_light_intensity(15) == 510
11+
assert calculate_light_intensity(21) == 1091
12+
13+
# Test invalid inputs
14+
assert calculate_light_intensity(0) == 0
15+
assert calculate_light_intensity(22) == 0
16+
assert calculate_light_intensity(-1) == 0
17+
assert calculate_light_intensity(3.5) == 0
18+
assert calculate_light_intensity(None) == 0

0 commit comments

Comments
 (0)