Improve Home Assistant connection reliability

This commit is contained in:
2026-07-27 16:23:40 -07:00
parent b7ec774efd
commit f3341ee064
16 changed files with 1696 additions and 697 deletions
+16 -14
View File
@@ -42,9 +42,8 @@ class WemoServer : Server
self.connectionStatus = .connecting
let op = ConnectOperation(baseURL: self.baseURL, session: _urlSession)
weak var weakOp = op
op.completionBlock = {
guard let strongOp = weakOp else { completion(nil) ; return }
op.completionBlock = { [weak op] in
guard let strongOp = op else { completion(nil) ; return }
if let error = strongOp.error {
self._logError("Error connecting to server", error: error)
self.connectionStatus = .disconnected
@@ -69,14 +68,18 @@ class WemoServer : Server
func fetchDevices(_ completion: @escaping (Result<[AnyDevice], Error>) -> Void)
{
let op = FetchDevicesOperation(baseURL: self.baseURL, session: _urlSession)
op.completionBlock = { [unowned op] in
op.completionBlock = { [weak op] in
guard let op else {
completion(.failure(ConnectionError.unknown))
return
}
if let error = op.error {
self._logError("Error fetching devices", error: error)
completion(.failure(error))
} else {
self.devices = op.devices
return
}
self.devices = op.devices
completion(.success(self.devices.map { AnyDevice($0) }))
}
@@ -98,9 +101,8 @@ class WemoServer : Server
{
if connectionStatus == .connected, let device = findDevice(device) {
let op = ToggleDeviceOperation(baseURL: self.baseURL, session: _urlSession, device: device, state: state)
weak var weakOp = op
op.completionBlock = {
guard let strongOp = weakOp else { completion(nil) ; return }
op.completionBlock = { [weak op] in
guard let strongOp = op else { completion(nil) ; return }
if let error = strongOp.error {
self._logError("Error toggling device", error: error)
}
@@ -132,7 +134,7 @@ class WemoServer : Server
}
}
internal class WemoOperation : Operation
internal class WemoOperation : Operation, @unchecked Sendable
{
var baseURL: URL
var session: URLSession
@@ -146,7 +148,7 @@ internal class WemoOperation : Operation
}
}
internal class ConnectOperation : WemoOperation
internal class ConnectOperation : WemoOperation, @unchecked Sendable
{
override func main()
{
@@ -164,7 +166,7 @@ internal class ConnectOperation : WemoOperation
}
}
internal class FetchDevicesOperation : WemoOperation
internal class FetchDevicesOperation : WemoOperation, @unchecked Sendable
{
private(set) var devices: [WemoDevice] = []
@@ -201,7 +203,7 @@ internal class FetchDevicesOperation : WemoOperation
}
}
internal class ToggleDeviceOperation : WemoOperation
internal class ToggleDeviceOperation : WemoOperation, @unchecked Sendable
{
var device: WemoDevice
var state: DeviceState