seems to work
This commit is contained in:
48
apps/homekit/accessories/contact.py
Normal file
48
apps/homekit/accessories/contact.py
Normal file
@@ -0,0 +1,48 @@
|
||||
"""
|
||||
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)
|
||||
Reference in New Issue
Block a user