diff --git a/Sources/Wiring/App/App+Presence.swift b/Sources/Wiring/App/App+Presence.swift index 15cdbac..0c834eb 100644 --- a/Sources/Wiring/App/App+Presence.swift +++ b/Sources/Wiring/App/App+Presence.swift @@ -46,7 +46,7 @@ extension App { for person in presenceConfig.entries.keys { let name = "Presence \(person)" let stateTopic = "\(mqttConfig.baseTopic)/presence/\(person)" - let config = Mqtt.BinarySensor( + let binarySensorConfig = Mqtt.BinarySensor( availabilityTopic: mqttClient.stateTopic, device: .init( identifiers: stateTopic, @@ -62,7 +62,26 @@ extension App { ) await mqttClient.publish( topic: "\(mqttConfig.homeAssistantBaseTopic)/binary_sensor/\(mqttConfig.baseTopic)-presence/\(person)/config", - message: config, + message: binarySensorConfig, + retain: true + ) + let deviceTrackerConfig = Mqtt.DeviceTracker( + availabilityTopic: mqttClient.stateTopic, + device: .init( + identifiers: stateTopic, + name: name, + viaDevice: mqttClient.stateTopic + ), + name: .explicitNone, + payloadHome: Mqtt.BinarySensor.Payload.on.rawValue, + payloadNotHome: Mqtt.BinarySensor.Payload.off.rawValue, + sourceType: .router, + stateTopic: stateTopic, + uniqueId: stateTopic.toUniqueId() + ) + await mqttClient.publish( + topic: "\(mqttConfig.homeAssistantBaseTopic)/device_tracker/\(mqttConfig.baseTopic)-presence/\(person)/config", + message: deviceTrackerConfig, retain: true ) } diff --git a/Sources/Wiring/HomeAssistantMqtt/MqttDeviceTracker.swift b/Sources/Wiring/HomeAssistantMqtt/MqttDeviceTracker.swift new file mode 100644 index 0000000..5c03bfd --- /dev/null +++ b/Sources/Wiring/HomeAssistantMqtt/MqttDeviceTracker.swift @@ -0,0 +1,25 @@ +import Foundation + +extension Mqtt { + /// https://www.home-assistant.io/integrations/device_tracker/ + /// https://www.home-assistant.io/integrations/device_tracker.mqtt/ + struct DeviceTracker: Encodable { + let availabilityTopic: String? + let device: Device + /// Can be set to `.explicitNone` if only the device name is relevant. + let name: String?? + let payloadHome: String? + let payloadNotHome: String? + let sourceType: SourceType + let stateTopic: String + let uniqueId: String + + /// https://www.home-assistant.io/integrations/device_tracker.mqtt/#source_type + enum SourceType: String, Encodable { + case gps + case router + case bluetooth + case bluetoothLe = "bluetooth_le" + } + } +} diff --git a/scripts/format.sh b/scripts/format.sh index 3b2ed24..53248ab 100755 --- a/scripts/format.sh +++ b/scripts/format.sh @@ -31,7 +31,13 @@ elif which docker > /dev/null 2>&1; then --user "$(id -u):$(id -g)" \ swift:6.0.2 \ /workspace/scripts/format.sh +elif which podman > /dev/null 2>&1; then + podman run --rm \ + --volume .:/workspace \ + --userns=keep-id \ + docker.io/swift:6.0.2 \ + /workspace/scripts/format.sh else - echo "Either 'swift' or 'docker' has to be installed to run swiftformat as an SPM plugin." + echo "Either 'swift', 'docker' or 'podman' has to be installed to run swiftformat." exit 1 fi