73 lines
3.0 KiB
Python
73 lines
3.0 KiB
Python
"""
|
|
Thermostat Accessory Implementation for HomeKit
|
|
|
|
Implements thermostat control according to homekit_mapping.md:
|
|
- CurrentTemperature (read-only)
|
|
- TargetTemperature (read/write)
|
|
- CurrentHeatingCoolingState (fixed: 1 = heating)
|
|
- TargetHeatingCoolingState (fixed: 3 = auto)
|
|
"""
|
|
|
|
from pyhap.accessory import Accessory
|
|
from pyhap.const import CATEGORY_THERMOSTAT
|
|
|
|
|
|
class ThermostatAccessory(Accessory):
|
|
"""Thermostat with temperature control."""
|
|
|
|
category = CATEGORY_THERMOSTAT
|
|
|
|
def __init__(self, driver, device, api_client, display_name=None, *args, **kwargs):
|
|
"""
|
|
Initialize the thermostat 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 Thermostat service
|
|
self.thermostat_service = self.add_preload_service('Thermostat')
|
|
|
|
# Get characteristics
|
|
self.current_temp_char = self.thermostat_service.get_characteristic('CurrentTemperature')
|
|
self.target_temp_char = self.thermostat_service.get_characteristic('TargetTemperature')
|
|
self.current_heating_cooling_char = self.thermostat_service.get_characteristic('CurrentHeatingCoolingState')
|
|
self.target_heating_cooling_char = self.thermostat_service.get_characteristic('TargetHeatingCoolingState')
|
|
|
|
# Set callback for target temperature
|
|
self.target_temp_char.setter_callback = self.set_target_temperature
|
|
|
|
# Set fixed heating/cooling states (mode is always "auto")
|
|
# CurrentHeatingCoolingState: 0=Off, 1=Heat, 2=Cool
|
|
self.current_heating_cooling_char.set_value(1) # Always heating
|
|
|
|
# TargetHeatingCoolingState: 0=Off, 1=Heat, 2=Cool, 3=Auto
|
|
self.target_heating_cooling_char.set_value(3) # Always auto
|
|
|
|
# Set temperature range (5-30°C as per UI)
|
|
self.target_temp_char.properties['minValue'] = 5
|
|
self.target_temp_char.properties['maxValue'] = 30
|
|
self.target_temp_char.properties['minStep'] = 0.5
|
|
|
|
def set_target_temperature(self, value):
|
|
"""Called when HomeKit wants to change target temperature."""
|
|
payload = {
|
|
"type": "thermostat",
|
|
"payload": {"target": float(value)}
|
|
}
|
|
self.api_client.post_device_set(self.device.device_id, payload["type"], payload["payload"])
|
|
|
|
def update_state(self, state_payload):
|
|
"""Update state from API event."""
|
|
if "current" in state_payload:
|
|
self.current_temp_char.set_value(float(state_payload["current"]))
|
|
if "target" in state_payload:
|
|
self.target_temp_char.set_value(float(state_payload["target"]))
|