Files
home-automation/apps/homekit/accessories/sensor.py
2025-11-17 11:36:19 +01:00

47 lines
1.8 KiB
Python

"""
Temperature & Humidity Sensor Accessory Implementation for HomeKit
Implements combined temperature and humidity sensor:
- CurrentTemperature (read-only)
- CurrentRelativeHumidity (read-only)
"""
from pyhap.accessory import Accessory
from pyhap.const import CATEGORY_SENSOR
class TempHumidityAccessory(Accessory):
"""Combined temperature and humidity sensor."""
category = CATEGORY_SENSOR
def __init__(self, driver, device, api_client, display_name=None, *args, **kwargs):
"""
Initialize the temp/humidity 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 TemperatureSensor service
self.temp_service = self.add_preload_service('TemperatureSensor')
self.current_temp_char = self.temp_service.get_characteristic('CurrentTemperature')
# Add HumiditySensor service
self.humidity_service = self.add_preload_service('HumiditySensor')
self.current_humidity_char = self.humidity_service.get_characteristic('CurrentRelativeHumidity')
def update_state(self, state_payload):
"""Update state from API event."""
if "temperature" in state_payload:
self.current_temp_char.set_value(float(state_payload["temperature"]))
if "humidity" in state_payload:
self.current_humidity_char.set_value(float(state_payload["humidity"]))