Write testable HomeKit automations in Swift that run 24/7 on your server.
- Write in Swift: Testable, type-safe automations vs HomeKit's limited rules
- Complex Logic: Multi-sensor triggers, weather APIs, energy pricing, time-based conditions
- 24/7 Server Runtime: Runs independently of iOS devices being unlocked
- Built-in Automations: Motion-activated lighting, window monitoring, garden watering, energy optimization
public struct MotionAtNight: Automatable {
public let motionSensor: MotionSensorDevice
public let lightSensor: MotionSensorDevice
public let lights: [SwitchDevice]
public func shouldTrigger(with event: HomeEvent, using hm: HomeManagable) async throws -> Bool {
let motionDetected = try await motionSensor.motionDetectedState(with: hm)
let lux = try await lightSensor.illuminanceState(with: hm)
return motionDetected && lux.value < 60.0
}
public func execute(using hm: HomeManagable) async throws {
await lights.forEach { $0.setBrightness(to: 0.3, with: hm) }
try await Task.sleep(for: .seconds(60))
await lights.forEach { $0.turnOff(with: hm) }
}
}More Examples: MotionAtNight.swift β’ WindowOpen.swift β’ GardenWatering.swift
- HomeKit Setup: Existing HomeKit home with compatible devices
- Apple Device: iPhone/iPad or Mac (FlowKit Adapter must run in foreground)
- Server: Docker + MySQL-compatible database
- Developer Tools: Swift 6.0+, Xcode (for building iOS apps)
- Optional: Tibber API key (for energy price automations)
-
Deploy Server:
docker-compose build docker-compose up -d
-
Configure Database: Set
DATABASE_URLenvironment variable (MySQL connection string) -
Build & Run FlowKit Adapter:
cd Apps/FlowKitAdapter xcodebuild -scheme "FlowKit Adapter" -configuration Release
Launch app, grant HomeKit permissions, configure server URL
-
Write Automations: Implement
Automatableprotocol inSources/HAImplementations/Automations/
flowchart LR
HomeKit[HomeKit Devices] <--> Adapter[FlowKit Adapter<br/>iOS/Mac App]
Adapter <-->|WebSocket| Server[FlowKit Server<br/>Docker/Vapor]
Server <--> DB[(MySQL)]
Server --> Automations[Your Swift<br/>Automations]
Distributed System: Adapter forwards HomeKit events to server via WebSocket, server executes automations and sends commands back.
Sources/HAImplementations/Automations/- Built-in automation examplesSources/HAModels/- Shared models (entities, events, devices)Sources/Server/- Vapor server business logicApps/FlowKitAdapter/- iOS/macOS HomeKit bridge appApps/FlowKitController/- iOS management app
Implement the Automatable protocol:
public protocol Automatable {
var name: String { get }
var triggerEntityIds: Set<EntityId> { get } // Sensors/devices that trigger this
func shouldTrigger(with event: HomeEvent, using hm: HomeManagable) async throws -> Bool
func execute(using hm: HomeManagable) async throws
}Add your automation to Sources/HAImplementations/Automations/ and register in AnyAutomation.