{"id":18165583,"url":"https://github.com/homewizard/swift-homewizard-energy","last_synced_at":"2026-01-31T23:41:05.604Z","repository":{"id":259018715,"uuid":"874705896","full_name":"homewizard/swift-homewizard-energy","owner":"homewizard","description":"HomeWizard Energy local API package for Swift","archived":false,"fork":false,"pushed_at":"2024-10-21T10:33:46.000Z","size":586,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-11-02T12:07:47.855Z","etag":null,"topics":["dsmr","dsmr5","homewizard-energy","local-api","p1","swift"],"latest_commit_sha":null,"homepage":"https://api-documentation.homewizard.com","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/homewizard.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-18T10:08:45.000Z","updated_at":"2024-10-31T11:43:21.000Z","dependencies_parsed_at":"2024-10-22T14:47:00.834Z","dependency_job_id":null,"html_url":"https://github.com/homewizard/swift-homewizard-energy","commit_stats":null,"previous_names":["homewizard/swift-homewizard-energy"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/homewizard%2Fswift-homewizard-energy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/homewizard%2Fswift-homewizard-energy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/homewizard%2Fswift-homewizard-energy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/homewizard%2Fswift-homewizard-energy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/homewizard","download_url":"https://codeload.github.com/homewizard/swift-homewizard-energy/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230627762,"owners_count":18255881,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dsmr","dsmr5","homewizard-energy","local-api","p1","swift"],"created_at":"2024-11-02T12:07:53.561Z","updated_at":"2026-01-31T23:41:00.581Z","avatar_url":"https://github.com/homewizard.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# HomeWizard Energy: `HWLocalAPI`\n\nSwift library to communicate with HomeWizard Energy devices using their local API.\n\nThis package is aimed at basic control of the device. Initial setup and configuration is assumed\nto be done with the official HomeWizard Energy app.\n\n[![Swift 5.10](https://img.shields.io/badge/Swift-5.10-orange.svg?style=flat)](https://swift.org)\n[![iOS](https://img.shields.io/badge/iOS-14+-green.svg?style=flat)](http://developer.apple.com)\n[![macOS](https://img.shields.io/badge/macOS-13+-green.svg?style=flat)](http://developer.apple.com)\n[![watchOS](https://img.shields.io/badge/watchOS-7.0+-green.svg?style=flat)](http://developer.apple.com)\n[![tvOS](https://img.shields.io/badge/tvOS-14.0+-green.svg?style=flat)](http://developer.apple.com)\n[![Linux](https://img.shields.io/badge/Linux-Supported-green.svg?style=flat)](https://swift.org)\n[![Release](https://img.shields.io/github/v/release/homewizard/swift-homewizard-energy)](https://github.com/homewizard/swift-homewizard-energy/releases)\n\n- [HomeWizard Energy: `HWLocalAPI`](#homewizard-energy-hwlocalapi)\n  - [Features](#features)\n  - [Usage](#usage)\n    - [Discovery](#discovery)\n      - [Monitor](#monitor)\n      - [Quick Lookup](#quick-lookup)\n    - [Devices](#devices)\n      - [Simple Data and State](#simple-data-and-state)\n      - [Monitoring](#monitoring)\n  - [Documentation](#documentation)\n  - [Installation](#installation)\n    - [Swift Package Manager (SPM)](#swift-package-manager-spm)\n      - [Package File](#package-file)\n      - [Xcode](#xcode)\n  - [Development and contribution](#development-and-contribution)\n  - [Release Notes](#release-notes)\n  - [License](#license)\n\n## Features\n\n- [x] Discover HomeWizard Energy devices on your local network (not supported for Linux)\n- [x] Connect to a device and fetch it's latest measurement readings\n- [x] Control the power state of Energy Sockets\n- [x] A `DeviceMonitor` that allows you to fetch all measurement data of one or more devices repetitively using a specified update interval\n\n## Usage\n\nThis will briefly describe the usage.\nA more detailed [documentation](#documentation) is added in the package.\n\n### Discovery\n\nYou can use the `DeviceDiscoveryHandler` to discover HomeWizard Energy devices on your local network through Bonjour.\n\n#### Monitor\n\nTo continuously monitor your network, you can implement its delegate:\n\n```swift\nfinal class Monitor: DeviceDiscoveryDelegate {\n    private(set) var discoveredDevices: [DiscoveredDevice] = []\n    private var handler: DeviceDiscoveryHandler!\n\n    init() {\n        handler = DeviceDiscoveryHandler(delegate: self)\n        handler.start()\n    }\n\n    deinit {\n        handler.stop()\n    }\n\n    func deviceDiscoveryHandler(_ handler: DeviceDiscoveryHandler, didDiscover device: DiscoveredDevice) {\n        discoveredDevices.append(device)\n    }\n\n    func deviceDiscoveryHandler(_ handler: DeviceDiscoveryHandler, didLoose device: DiscoveredDevice) {\n        discoveredDevices\n            .removeAll(where: {\n                $0.serial == device.serial\n            })\n    }\n\n    func deviceDiscoveryHandler(_ handler: DeviceDiscoveryHandler, didUpdate device: DiscoveredDevice, from oldDevice: DiscoveredDevice) {\n        discoveredDevices\n            .removeAll(where: {\n                $0.serial == oldDevice.serial\n            })\n        discoveredDevices.append(device)\n    }\n}\n```\n\n#### Quick Lookup\n\nAlternatively you can use a quick lookup to scan the network for just a couple of seconds and get the list with discovered devices:\n\n```swift\nfinal class Preferences {\n    func usedDevice() async throws -\u003e (any Device)? {\n        guard let serial = UserDefaults\n            .standard\n            .string(forKey: \"usedDevice\") else {\n            return nil\n        }\n\n        let discovered = await DeviceDiscoveryHandler.quickLookup()\n        if let device = discovered.first(where: { $0.serial == serial }) {\n            return try await device.load()\n        } else {\n            return nil\n        }\n    }\n\n    func setUsedDevice(_ device: (any Device)?) {\n        UserDefaults.standard.set(device?.serial, forKey: \"usedDevice\")\n    }\n}\n```\n\n### Devices\n\n#### Simple Data and State\n\nOnce you have discovered a device, or know its IP address, you can load basic information of the device,\nas well as getting its latest measurement data:\n\n```swift\nif let device = try await DeviceLoader.load(\"192.168.0.10\") as? P1Meter {\n    let measurement = try await device.fetchData()\n    print(\"Current power usage: \\(measurement.activePower) watt\")\n}\n```\n\nFor Energy Sockets you can also get and set their state (power on/off, LED brightness, switch lock):\n\n```swift\nif let socket = try await DeviceLoader.load(\"192.168.0.10\") as? EnergySocket {\n    if try await socket.isPoweredOn {\n        print(\"The socket was powered on. I will turn it off now...\")\n        try await socket.setPoweredOn(false)\n    }\n}\n```\n\n#### Monitoring\n\nBesides getting measurement data manually as described above, you may also use the `DeviceMonitor` to continuously update the data\nof one or more devices (with a specified update interval).\n\n```swift\nfinal class MyMonitor: Sendable {\n    /// Devices to monitor\n    private let devices: [any Device]\n\n    init(devices: [any Device]) {\n        self.devices = devices\n\n        // Register the observer\n        NotificationCenter\n            .default\n            .addObserver(\n                self,\n                selector: #selector(dataReceived(_:)),\n                name: DeviceMonitor.didUpdate,\n                object: nil\n            )\n\n        // Add the devices to monitor\n        DeviceMonitor.default.add(devices)\n        // Start the monitor if needed\n        if !DeviceMonitor.default.isRunning {\n            DeviceMonitor.default.start()\n        }\n    }\n\n    deinit {\n        DeviceMonitor.default.remove(self.devices)\n        NotificationCenter.default.removeObserver(self)\n    }\n\n    @objc private func dataReceived(_ notification: Notification) {\n        if let data = notification.userInfo?[DeviceMonitor.UserInfo.data] as? P1MeterData,\n           let power = data.activePower {\n\n            if power \u003c 0 {\n                print(\"Your main connection is now delivering \\(power) watt back into the grid\")\n            } else {\n                print(\"Your main connection is now using \\(power) watt from the grid\")\n            }\n\n        }\n    }\n}\n```\n\n## Documentation\n\nThe package is documented using [DocC](https://www.swift.org/documentation/docc/).\n\nWhen added as a dependency to your project, use *Product \u003e Build Documentation* (or ⌃⇧⌘D) once to\nadd it to your Developer Documentation.\n\nFor more details about the Local API itself, you may take a look at [our API documentation](https://api-documentation.homewizard.com).\n\n## Installation\n\n### Swift Package Manager (SPM)\n\n[Swift Package Manager](https://swift.org/package-manager/) is a tool for managing Swift packages, both executables as libraries.\nIt’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.\n\n#### Package File\n\nAdd HWLocalAPI as a package to your `Package.swift` file and then specify it as a dependency of the Target in which you wish to use it.\n\n```swift\n// swift-tools-version: 5.10\nimport PackageDescription\n\nlet package = Package(\n    name: \"MyProject\",\n    platforms: [\n       .macOS(.v13)\n    ],\n    dependencies: [\n        .package(url: \"https://github.com/HomeWizard/swift-homewizard-energy\", from: \"1.0.0\")\n    ],\n    targets: [\n        .target(\n            name: \"MyProject\",\n            dependencies: [\n                .product(name: \"HWLocalAPI\", package: \"swift-homewizard-energy\")\n            ]\n        ),\n        .testTarget(\n            name: \"MyProjectTests\",\n            dependencies: [\"MyProject\"]\n        ),\n    ]\n)\n```\n\n#### Xcode\n\nTo add HWLocalAPI as a [dependency](https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app) to your Xcode project, \nselect *File \u003e Add Package Dependency* and enter the repository URL in the field top right.\n\n## Development and contribution\n\nPull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.\n\n## Release Notes\n\nSee [CHANGELOG.md](https://github.com/HomeWizard/swift-homewizard-energy/blob/main/CHANGELOG.md) for a list of changes.\n\n## License\n\nHWLocalAPI is available under the MIT license. See the LICENSE file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhomewizard%2Fswift-homewizard-energy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhomewizard%2Fswift-homewizard-energy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhomewizard%2Fswift-homewizard-energy/lists"}