Skip to content

Commit

Permalink
Merge pull request #17 from Cyberbeni/device_tracker
Browse files Browse the repository at this point in the history
Also add device tracker for presence detection
  • Loading branch information
Cyberbeni authored Jan 23, 2025
2 parents 565adfd + ef49721 commit bbd6c1a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
23 changes: 21 additions & 2 deletions Sources/Wiring/App/App+Presence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
)
}
Expand Down
25 changes: 25 additions & 0 deletions Sources/Wiring/HomeAssistantMqtt/MqttDeviceTracker.swift
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
8 changes: 7 additions & 1 deletion scripts/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bbd6c1a

Please sign in to comment.