Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speaker code... #169

Open
davemen opened this issue Feb 15, 2025 · 0 comments
Open

Speaker code... #169

davemen opened this issue Feb 15, 2025 · 0 comments

Comments

@davemen
Copy link

davemen commented Feb 15, 2025

Has anyone gotten speaker code working? I keep getting "This accessory is not currently supported by the Home App"

import Foundation
import HAP
import Logging

fileprivate let logger = Logger(label: "homekit-speaker")

#if os(Linux)
import Dispatch
#endif

var keepRunning = true

// ✅ Fix: Use a global function for signal handling
func stopSignalHandler(_ signal: Int32) {
DispatchQueue.main.async {
logger.info("Shutting down...")
keepRunning = false
}
}

// MARK: - HomeKit Speaker Accessory
class SimpleSpeaker: Accessory {
init() {
// Required Characteristic: Mute
let muteCharacteristic = AnyCharacteristic(GenericCharacteristic(
type: .mute,
value: false,
permissions: [.read, .write, .events]
))

    // Optional Characteristic: Volume (0-100)
    let volumeCharacteristic = AnyCharacteristic(GenericCharacteristic(
        type: .volume,
        value: 50,
        permissions: [.read, .write, .events],
        maxValue: 100, minValue: 0
    ))

    let speakerService = Service(
        type: .speaker,
        characteristics: [muteCharacteristic, volumeCharacteristic]
    )

    super.init(
        info: .init(
            name: "Basic Speaker",
            serialNumber: "12345678",
            manufacturer: "MyCompany",
            model: "Speaker-001",
            firmwareRevision: "1.0"
        ),
        type: .speaker,
        services: [speakerService]
    )
}

}

// MARK: - Start HAP Server
do {
// Reset Pairing Storage (Clears stale pairings)
let storageFile = "hap-storage.json"
try? FileManager.default.removeItem(atPath: storageFile)

// Set up persistent storage
let storage = try FileStorage(filename: storageFile)

// Create the HomeKit speaker accessory
let speaker = SimpleSpeaker()

// Initialize the HomeKit device
let device = HAP.Device(
    setupCode: "700-70-700",
    storage: storage,
    accessory: speaker
)

let server = try HAP.Server(device: device, listenPort: 0)

// ✅ Fix: Use a global function for handling signals
signal(SIGINT, stopSignalHandler)
signal(SIGTERM, stopSignalHandler)

print()
print("Scan the following QR code using your iPhone to pair this device:")
print()
print(device.setupQRCode.asText)
print()

// Keep server running
while keepRunning {
    RunLoop.current.run(mode: .default, before: Date.distantFuture)
}

try server.stop()
logger.info("Stopped")

} catch {
logger.error("❌ Failed to start HomeKit accessory: (error)")
exit(1)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant