Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calculate hub2 light intensity & split hub2 sensor temperature entity #290

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions switchbot/adv_parsers/hub2.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,48 @@ def process_wohub2(data: bytes | None, mfr_data: bytes | None) -> dict[str, Any]
# Data should be flat, but we keep the original structure for now
"temp": {"c": _temp_c, "f": _temp_f},
"temperature": _temp_c,
"sensor_temperature": _temp_c,
"fahrenheit": bool(temp_data[2] & 0b10000000),
"humidity": humidity,
"lightLevel": light_level,
"illuminance": calculate_light_intensity(light_level),
}

return _wohub2_data


def calculate_light_intensity(light_level: int) -> int:
"""
Convert Hub 2 light level (1-21) to actual light intensity value
Args:
light_level: Integer from 1-21
Returns:
Corresponding light intensity value or 0 if invalid input
"""
light_map = {
1: 0,
2: 10,
3: 20,
4: 30,
5: 40,
6: 50,
7: 60,
8: 70,
9: 80,
10: 90,
11: 105,
12: 205,
13: 317,
14: 416,
15: 510,
16: 610,
17: 707,
18: 801,
19: 897,
20: 1023,
21: 1091,
}

if not isinstance(light_level, int) or light_level < 1 or light_level > 21:
return 0
return light_map[light_level]
2 changes: 2 additions & 0 deletions tests/test_adv_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,8 @@ def test_wohub2_passive_and_active():
"lightLevel": 2,
"temp": {"c": 26.7, "f": 80.06},
"temperature": 26.7,
"illuminance": 10,
"sensor_temperature": 26.7,
},
"isEncrypted": False,
"model": "v",
Expand Down
20 changes: 20 additions & 0 deletions tests/test_hub2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from switchbot.adv_parsers.hub2 import calculate_light_intensity


def test_calculate_light_intensity():
"""Test calculating light intensity from Hub 2 light level."""

# Test valid inputs
assert calculate_light_intensity(1) == 0
assert calculate_light_intensity(2) == 10
assert calculate_light_intensity(10) == 90
assert calculate_light_intensity(15) == 510
assert calculate_light_intensity(21) == 1091

# Test invalid inputs
assert calculate_light_intensity(0) == 0
assert calculate_light_intensity(22) == 0
assert calculate_light_intensity(-1) == 0
assert calculate_light_intensity(3.5) == 0
assert calculate_light_intensity("5") == 0
assert calculate_light_intensity(None) == 0
Loading