49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
"""
|
|
Contact Sensor Accessory Implementation for HomeKit
|
|
|
|
Implements contact sensor (window/door sensors):
|
|
- ContactSensorState (read-only): 0=Detected, 1=Not Detected
|
|
"""
|
|
|
|
from pyhap.accessory import Accessory
|
|
from pyhap.const import CATEGORY_SENSOR
|
|
|
|
|
|
class ContactAccessory(Accessory):
|
|
"""Contact sensor for doors and windows."""
|
|
|
|
category = CATEGORY_SENSOR
|
|
|
|
def __init__(self, driver, device, api_client, display_name=None, *args, **kwargs):
|
|
"""
|
|
Initialize the contact sensor accessory.
|
|
|
|
Args:
|
|
driver: HAP driver instance
|
|
device: Device object from DeviceRegistry
|
|
api_client: ApiClient for sending commands
|
|
display_name: Optional display name (defaults to device.friendly_name)
|
|
"""
|
|
name = display_name or device.friendly_name or device.name
|
|
super().__init__(driver, name, *args, **kwargs)
|
|
self.device = device
|
|
self.api_client = api_client
|
|
|
|
# Add ContactSensor service
|
|
self.contact_service = self.add_preload_service('ContactSensor')
|
|
|
|
# Get ContactSensorState characteristic
|
|
self.contact_state_char = self.contact_service.get_characteristic('ContactSensorState')
|
|
|
|
# Initialize with "not detected" (closed)
|
|
self.contact_state_char.set_value(1)
|
|
|
|
def update_state(self, state_payload):
|
|
"""Update state from API event."""
|
|
if "contact" in state_payload:
|
|
# API sends: "open" or "closed"
|
|
# HomeKit: 0=Contact Detected (closed), 1=Contact Not Detected (open)
|
|
is_open = state_payload["contact"] == "open"
|
|
homekit_state = 1 if is_open else 0
|
|
self.contact_state_char.set_value(homekit_state)
|