Skip to content

Commit

Permalink
feat: intial support for binary_sensor devices (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jc2k authored Aug 11, 2022
1 parent 402ea18 commit fc0ff14
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Sphinx = {version = "^5.0", optional = true}
sphinx-rtd-theme = {version = "^1.0", optional = true}
myst-parser = {version = "^0.18", optional = true}
home-assistant-bluetooth = ">=1.3.0"
sensor-state-data = ">=2.0.2"
sensor-state-data = ">=2.1.2"
bluetooth-sensor-state-data = ">=1.5.0"
pycryptodomex = ">=3.15.0"
bleak-retry-connector = ">=1.4.0"
Expand Down
25 changes: 20 additions & 5 deletions src/xiaomi_ble/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@
from bluetooth_sensor_state_data import BluetoothData
from Cryptodome.Cipher import AES
from home_assistant_bluetooth import BluetoothServiceInfo
from sensor_state_data import DeviceClass, SensorLibrary, SensorUpdate, Units
from sensor_state_data import (
BinarySensorDeviceClass,
DeviceClass,
SensorLibrary,
SensorUpdate,
Units,
)

from .const import CHARACTERISTIC_BATTERY, TIMEOUT_1DAY
from .devices import DEVICE_TYPES
Expand Down Expand Up @@ -304,7 +310,10 @@ def obj000f(
if device_type in ["MJYD02YL", "RTCGQ02LM"]:
# MJYD02YL: 1 - moving no light, 100 - moving with light
# RTCGQ02LM: 0 - moving no light, 256 - moving with light
return {"motion": 1, "motion timer": 1, "light": int(illum >= 100)}
device.update_predefined_binary_sensor(
BinarySensorDeviceClass.LIGHT, bool(illum >= 100)
)
return {"motion": 1, "motion timer": 1}
elif device_type == "CGPR1":
# CGPR1: moving, value is illumination in lux
device.update_predefined_sensor(SensorLibrary.LIGHT__LIGHT_LUX, illum)
Expand Down Expand Up @@ -525,7 +534,9 @@ def obj1007(
if device_type in ["MJYD02YL", "MCCGQ02HL"]:
# 100 means light, else dark (0 or 1)
# MCCGQ02HL might use obj1018 for light sensor, just added here to be sure.
return {"light": 1 if illum == 100 else 0}
device.update_predefined_binary_sensor(
BinarySensorDeviceClass.LIGHT, illum == 100
)
elif device_type in ["HHCCJCY01", "GCLS002"]:
# illumination in lux
device.update_predefined_sensor(SensorLibrary.LIGHT__LIGHT_LUX, illum)
Expand Down Expand Up @@ -603,14 +614,18 @@ def obj1014(
xobj: bytes, device: XiaomiBluetoothDeviceData, device_type: str
) -> dict[str, Any]:
"""Moisture"""
return {"moisture": xobj[0]}
device.update_predefined_binary_sensor(
BinarySensorDeviceClass.MOISTURE, xobj[0] > 0
)
return {}


def obj1015(
xobj: bytes, device: XiaomiBluetoothDeviceData, device_type: str
) -> dict[str, Any]:
"""Smoke"""
return {"smoke detector": xobj[0]}
device.update_predefined_binary_sensor(BinarySensorDeviceClass.SMOKE, xobj[0] > 0)
return {}


def obj1017(
Expand Down
19 changes: 15 additions & 4 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import pytest
from home_assistant_bluetooth import BluetoothServiceInfo
from sensor_state_data import (
BinarySensorDescription,
BinarySensorDeviceClass,
BinarySensorValue,
DeviceClass,
DeviceKey,
SensorDescription,
Expand All @@ -23,6 +26,7 @@
KEY_ILLUMINANCE = DeviceKey(key="illuminance", device_id=None)
KEY_CONDUCTIVITY = DeviceKey(key="conductivity", device_id=None)
KEY_MOISTURE = DeviceKey(key="moisture", device_id=None)
KEY_SMOKE = DeviceKey(key="smoke", device_id=None)


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -686,12 +690,19 @@ def test_Xiaomi_JTYJGD03MI_smoke():
name="Signal Strength", device_key=KEY_SIGNAL_STRENGTH, native_value=-60
),
},
binary_entity_descriptions={
KEY_SMOKE: BinarySensorDescription(
device_key=KEY_SMOKE,
device_class=BinarySensorDeviceClass.SMOKE,
),
},
binary_entity_values={
KEY_SMOKE: BinarySensorValue(
name="Smoke", device_key=KEY_SMOKE, native_value=True
),
},
)

assert device.unhandled == {
"smoke detector": 1,
}


def test_Xiaomi_JTYJGD03MI_press():
"""Test Xiaomi parser for JTYJGD03MI."""
Expand Down

0 comments on commit fc0ff14

Please sign in to comment.