Skip to content

Commit

Permalink
feat: motion events (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernst79 authored Jan 4, 2023
1 parent a4ba752 commit 2a52c1e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
20 changes: 20 additions & 0 deletions src/xiaomi_ble/events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Event constants for xiaomi-ble."""
from __future__ import annotations

from sensor_state_data.enum import StrEnum


class EventDeviceKeys(StrEnum):
"""Keys for devices that send events."""

# Button
BUTTON = "button"

# Dimmer
DIMMER = "dimmer"

# Motion
MOTION = "motion"

# Rocker switch
SWITCH = "switch"
13 changes: 8 additions & 5 deletions src/xiaomi_ble/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
TIMEOUT_1DAY,
)
from .devices import DEVICE_TYPES
from .events import EventDeviceKeys

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -157,11 +158,13 @@ def obj0003(
xobj: bytes, device: XiaomiBluetoothDeviceData, device_type: str
) -> dict[str, Any]:
"""Motion"""
# Not implemented yet. 0x0003 is used by MUE4094RT
# MUE4094 does not send motion clear, so needs a motion timer to reset motion.
# device.update_predefined_binary_sensor(
# BinarySensorDeviceClass.MOTION, bool(xobj[0])
# )
# 0x0003 is only used by MUE4094RT, which does not send motion clear.
# This object is therefore added as event (motion detected).
device.fire_event(
key=EventDeviceKeys.MOTION,
event_type="motion_detected",
event_properties=None,
)
return {}


Expand Down
31 changes: 15 additions & 16 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
BinarySensorValue,
DeviceClass,
DeviceKey,
Event,
SensorDescription,
SensorDeviceInfo,
SensorUpdate,
Expand All @@ -34,6 +35,7 @@
)
KEY_BINARY_PRY_THE_DOOR = DeviceKey(key="pry_the_door", device_id=None)
KEY_CONDUCTIVITY = DeviceKey(key="conductivity", device_id=None)
KEY_EVENT_MOTION = DeviceKey(key="motion", device_id=None)
KEY_HUMIDITY = DeviceKey(key="humidity", device_id=None)
KEY_ILLUMINANCE = DeviceKey(key="illuminance", device_id=None)
KEY_MOISTURE = DeviceKey(key="moisture", device_id=None)
Expand Down Expand Up @@ -68,7 +70,7 @@ def bytes_to_service_info(
)


def test_blank_advertisemnts_then_encrypted():
def test_blank_advertisements_then_encrypted():
"""Test that we can reject empty payloads."""
device = XiaomiBluetoothDeviceData()

Expand All @@ -91,7 +93,7 @@ def test_blank_advertisemnts_then_encrypted():
assert device.pending is False


def test_blank_advertisemnts_then_unencrypted():
def test_blank_advertisements_then_unencrypted():
"""Test that we can reject empty payloads."""

# NOTE: THIS IS SYNTHETIC TEST DATA - i took a known unecrypted device and flipped
Expand All @@ -117,7 +119,7 @@ def test_blank_advertisemnts_then_unencrypted():
assert device.pending is False


def test_blank_advertisemnts_then_encrypted_last_service_info():
def test_blank_advertisements_then_encrypted_last_service_info():
"""Test that we can capture valid service info records"""
device = XiaomiBluetoothDeviceData()

Expand All @@ -137,7 +139,7 @@ def test_blank_advertisemnts_then_encrypted_last_service_info():
assert device.last_service_info == advertisement


def test_blank_advertisemnts_then_unencrypted_last_service_info():
def test_blank_advertisements_then_unencrypted_last_service_info():
"""Test that we can capture valid service info records."""

# NOTE: THIS IS SYNTHETIC TEST DATA - i took a known unecrypted device and flipped
Expand Down Expand Up @@ -1035,7 +1037,7 @@ def test_Xiaomi_MJYD02YL():

def test_Xiaomi_MUE4094RT():
"""Test Xiaomi parser for MUE4094RT."""
# Motion sensor hasn't been implemented yet, as it needs a motion reset timer.
# MUE4094RT only sends motion detected as an event.
data_string = b"@0\xdd\x03$\x03\x00\x01\x01"
advertisement = bytes_to_service_info(data_string, address="DE:70:E8:B2:39:0C")

Expand Down Expand Up @@ -1065,17 +1067,14 @@ def test_Xiaomi_MUE4094RT():
name="Signal Strength", device_key=KEY_SIGNAL_STRENGTH, native_value=-60
),
},
# binary_entity_descriptions={
# KEY_BINARY_MOTION: BinarySensorDescription(
# device_key=KEY_BINARY_MOTION,
# device_class=BinarySensorDeviceClass.MOTION,
# ),
# },
# binary_entity_values={
# KEY_BINARY_MOTION: BinarySensorValue(
# device_key=KEY_BINARY_MOTION, name="Motion", native_value=True
# ),
# },
events={
KEY_EVENT_MOTION: Event(
device_key=KEY_EVENT_MOTION,
name="Motion",
event_type="motion_detected",
event_properties=None,
),
},
)


Expand Down

0 comments on commit 2a52c1e

Please sign in to comment.