Skip to content

Commit

Permalink
Merge pull request #6 from Cyberbeni/unique-id-fix
Browse files Browse the repository at this point in the history
Fix unique_id format, fix conflicting clientId between test and prod MQTT client, set object_id
  • Loading branch information
Cyberbeni authored Nov 2, 2024
2 parents f08c143 + 259cd57 commit 18a5730
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
9 changes: 5 additions & 4 deletions Sources/Wiring/App/App+Presence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ extension App {
let config = Mqtt.BinarySensor(
availabilityTopic: mqttClient.stateTopic,
device: .init(
name: name,
identifiers: stateTopic
identifiers: stateTopic,
name: name
),
deviceClass: .presence,
name: name,
name: nil,
objectId: name,
stateTopic: stateTopic,
uniqueId: stateTopic
uniqueId: stateTopic.replacingOccurrences(of: "/", with: "_")
)
await mqttClient.setOnConnectMessage(
topic: "\(mqttConfig.homeAssistantBaseTopic)/binary_sensor/\(mqttConfig.baseTopic)-presence/\(person)/config",
Expand Down
8 changes: 2 additions & 6 deletions Sources/Wiring/HomeAssistantMqtt/MqttBinarySensor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ extension Mqtt {
let availabilityTopic: String
let device: Device
let deviceClass: DeviceClass?
let name: String
let name: String?
let objectId: String?
let stateTopic: String
let uniqueId: String

Expand All @@ -15,11 +16,6 @@ extension Mqtt {
case off = "OFF"
}

struct Device: Encodable {
let name: String
let identifiers: String
}

/// https://www.home-assistant.io/integrations/binary_sensor/#device-class
enum DeviceClass: String, Encodable {
case battery
Expand Down
8 changes: 8 additions & 0 deletions Sources/Wiring/HomeAssistantMqtt/MqttDevice.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Foundation

extension Mqtt {
struct Device: Encodable {
let identifiers: String
let name: String?
}
}
10 changes: 6 additions & 4 deletions Sources/Wiring/ThirdPartyWrappers/MQTTClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import NIOFoundationCompat

actor MQTTClient {
private static let reconnectDelay: Double = 5
private static let clientId = "Wiring"

private let mqttClient: MQTTNIO.MQTTClient

Expand All @@ -15,16 +14,19 @@ actor MQTTClient {
private var onConnectMessages: [String: ByteBuffer] = [:]

private let baseTopic: String
private let clientId: String
nonisolated var stateTopic: String { "\(baseTopic)/server/state" }

private let messageEncoder = Mqtt.jsonEncoder()

init(config: Config.Mqtt) {
baseTopic = config.baseTopic
let clientId = "Wiring - \(config.baseTopic)"
self.clientId = clientId
mqttClient = MQTTNIO.MQTTClient(
host: config.host,
port: config.port,
identifier: Self.clientId,
identifier: clientId,
eventLoopGroupProvider: .createNew,
configuration: MQTTNIO.MQTTClient.Configuration(
version: .v5_0,
Expand All @@ -43,11 +45,11 @@ actor MQTTClient {
setOnConnectMessage(topic: stateTopic, rawMessage: Mqtt.Availability.online)
isStarted = true

mqttClient.addPublishListener(named: Self.clientId) { result in
mqttClient.addPublishListener(named: clientId) { result in
guard case let .failure(error) = result else { return }
Log.error("Publish listener error: \(error)")
}
mqttClient.addCloseListener(named: Self.clientId) { [weak self] _ in
mqttClient.addCloseListener(named: clientId) { [weak self] _ in
Log.error("Connection closed...")
Task { [weak self] in
await self?.connect(isReconnect: true)
Expand Down

0 comments on commit 18a5730

Please sign in to comment.