-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3e6f386
Showing
15 changed files
with
1,101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules/ | ||
config/accessories/ | ||
config/persist/ | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
package-lock.json | ||
config/accessories/ | ||
config/persist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Julio Benavides | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
Petkit Homebridge Plugin | ||
======================== | ||
|
||
<p align="center"> | ||
<img src="https://github.com/jubepue/homebridge-petkit-platform/blob/master/images/homebridge-petkit-platform.png"><br> | ||
</p> | ||
|
||
<span align="center"> | ||
|
||
[![npm](https://img.shields.io/npm/v/homebridge-petkit-platform.svg)](https://www.npmjs.com/package/homebridge-petkit-platform) | ||
[![npm](https://img.shields.io/npm/dt/homebridge-petkit-platform.svg)](https://www.npmjs.com/package/homebridge-petkit-platform) | ||
[![verified-by-homebridge](https://badgen.net/badge/homebridge/verified/purple)](https://github.com/homebridge/homebridge/wiki/Verified-Plugins) | ||
|
||
[![GitHub watchers](https://img.shields.io/github/watchers/jubepue/homebridge-petkit-platform.svg?style=social&label=Watch)](https://GitHub.com/jubepue/homebridge-petkit-platform/watchers/) | ||
[![GitHub stars](https://img.shields.io/github/stars/jubepue/homebridge-petkit-platform.svg?style=social&label=Star)](https://GitHub.com/jubepue/homebridge-petkit-platform/stargazers/) | ||
[![GitHub forks](https://img.shields.io/github/forks/jubepue/homebridge-petkit-platform.svg?style=social&label=Fork)](https://GitHub.com/jubepue/homebridge-petkit-platform/network/) | ||
|
||
If you like Petkit Homebridge Plugin - give it a star, or fork it and contribute! | ||
|
||
</span> | ||
|
||
Homebridge custom plugin for controlling all your Petkit devices in HomeKit. | ||
|
||
## Preparation | ||
1. [Use the Petkit Homebridge Plugin](https://github.com/jubepue/homebridge-petkit-platform/wiki/How-to-Use-Petkit-Homebridge-Plugin) | ||
2. [How to Get Logs](https://github.com/jubepue/homebridge-petkit-platform/wiki/How-To-Get-Logs) | ||
|
||
## Set up the development environment | ||
|
||
``` | ||
—-VSCode | ||
—-engines | ||
"node": “>=0.12.0” | ||
"homebridge": ">=0.2.0" | ||
—-dependencies | ||
"axios": “^0.21.1", | ||
"crypto-js": “^4.0.0”, | ||
"uuid": "^8.3.2", | ||
"qs": "^6.11.0" | ||
``` | ||
|
||
## Feedback | ||
|
||
You can use the **GitHub Issue** to provide feedback on any problems you encounter. | ||
|
||
## LICENSE | ||
|
||
For more information, see the [LICENSE](LICENSE) file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
{ | ||
"pluginAlias": "PetkitPlatform", | ||
"pluginType": "platform", | ||
"singular": true, | ||
"headerDisplay": "For documentation please see https://github.com/jubepue/petkit-homebridge .", | ||
"footerDisplay": "", | ||
"schema": { | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"title": "Name", | ||
"type": "string", | ||
"required": true, | ||
"default": "PetkitPlatform", | ||
"description": "You shouldn't need to change this." | ||
}, | ||
"options": { | ||
"title": "", | ||
"type": "object", | ||
"required": true, | ||
"properties": { | ||
"username": { | ||
"title": "Username", | ||
"type": "string", | ||
"required": true | ||
}, | ||
"password": { | ||
"title": "Password", | ||
"type": "string", | ||
"required": true | ||
}, | ||
"lang": { | ||
"title": "Language Code", | ||
"type": "string", | ||
"default": "en", | ||
"description": "Your two letter language code", | ||
"required": true | ||
}, | ||
"countryCode": { | ||
"title": "Country Code", | ||
"type": "number", | ||
"default": "", | ||
"description": "Your two integer country code", | ||
"required": true | ||
}, | ||
"debug": { | ||
"title": "Enable Debug Logging", | ||
"type": "boolean", | ||
"default": false, | ||
"required": true | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"bridge": { | ||
"name": "homebridge-petkit-platform", | ||
"username": "0E:78:B3:D4:BC:B7", | ||
"port": 37876, | ||
"pin": "034-85-687" | ||
}, | ||
|
||
"description": "This is an Homebridge Plugin of Petkit.", | ||
|
||
"platforms": [ | ||
{ | ||
"platform": "PetkitPlatform", | ||
"name": "PetkitPlatform", | ||
"options": | ||
{ | ||
"username": "", | ||
"password": "", | ||
"lang": "es", | ||
"countryCode": 34, | ||
"debug":false | ||
} | ||
} | ||
] | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
const PetkitOpenAPI = require("./lib/petkitopenapi"); | ||
const FeederAccessory = require('./lib/feederaccessory'); | ||
const PetkitListener = require("./lib/petkitlistener"); | ||
|
||
const LogUtil = require('./util/logutil'); | ||
|
||
var Accessory, Service, Characteristic; | ||
|
||
const pluginName = 'homebridge-petkit-platform'; | ||
const platformName = 'PetkitPlatform'; | ||
|
||
module.exports = function (homebridge) { | ||
Accessory = homebridge.platformAccessory; | ||
Service = homebridge.hap.Service; | ||
Characteristic = homebridge.hap.Characteristic; | ||
// registerAccessory' three parameters is plugin-name, accessory-name, constructor-name | ||
homebridge.registerPlatform(pluginName, platformName, PetkitPlatform, true); | ||
} | ||
|
||
class PetkitPlatform { | ||
constructor(log, config, api) { | ||
this.log = new LogUtil( | ||
config.options.debug, | ||
); | ||
this.config = config; | ||
if (!config || !config.options) { | ||
this.log.log('The config configuration is incorrect, disabling plugin.') | ||
return; | ||
} | ||
this.deviceAccessories = new Map(); | ||
this.accessories = new Map(); | ||
|
||
if (api) { | ||
// Save the API object as plugin needs to register new accessory via this object | ||
this.api = api; | ||
// Listen to event "didFinishLaunching", this means homebridge already finished loading cached accessories. | ||
// Platform Plugin should only register new accessory that doesn't exist in homebridge after this event. | ||
// Or start discover new accessories. | ||
this.api.on('didFinishLaunching', function () { | ||
this.log.log("Initializing PetkitPlatform..."); | ||
this.initPetkitSDK(config); | ||
}.bind(this)); | ||
} | ||
} | ||
|
||
async initPetkitSDK(config) { | ||
let devices; | ||
let api; | ||
|
||
api = new PetkitOpenAPI( | ||
config.options.username, | ||
config.options.password, | ||
config.options.countryCode, | ||
this.log, | ||
); | ||
this.petkitOpenApi = api; | ||
|
||
await api.login(config.options.username, config.options.password); | ||
|
||
try { | ||
devices = await api.getDeviceList(); | ||
} catch (e) { | ||
// this.log.log(JSON.stringify(e.message)); | ||
this.log.log('Failed to get device information. Please check if the config.json is correct.') | ||
return; | ||
} | ||
|
||
for (const device of devices) { | ||
//this.log.log(JSON.stringify(device)); | ||
this.addAccessory(device); | ||
} | ||
|
||
let listener = new PetkitListener(api, this.log); | ||
this.petkitListener = listener; | ||
this.petkitListener.start(); | ||
this.petkitListener.registerListener(this.onDevicesListener.bind(this)); | ||
} | ||
|
||
addAccessory(device) { | ||
var deviceType = device.type; | ||
this.log.log(`Adding: ${device.name || 'unnamed'} (${deviceType} / ${device.id})`); | ||
// Get UUID | ||
const uuid = this.api.hap.uuid.generate(device.id); | ||
const homebridgeAccessory = this.accessories.get(uuid); | ||
|
||
// Construct new accessory | ||
let deviceAccessory; | ||
switch (deviceType) { | ||
case 'Feeder': | ||
case 'FeederMini': | ||
case 'D3': | ||
case 'D4': | ||
deviceAccessory = new FeederAccessory(this, homebridgeAccessory, device); | ||
this.accessories.set(uuid, deviceAccessory.homebridgeAccessory); | ||
this.deviceAccessories.set(uuid, deviceAccessory); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
//Handle device deletion, addition, status update | ||
async onDevicesListener(listener) { | ||
for (const device of listener) { | ||
let uuid = this.api.hap.uuid.generate(device.id); | ||
if (this.accessories.has(uuid)) { | ||
this.refreshDeviceStates(device); | ||
} else { | ||
this.addAccessory(device); | ||
} | ||
} | ||
for (const uuid of this.accessories.keys()) { | ||
let found = listener.find(x => this.api.hap.uuid.generate(x.id) == uuid); | ||
if (!found) { | ||
let homebridgeAccessory = this.accessories.get(uuid); | ||
this.removeAccessory(homebridgeAccessory); | ||
} | ||
} | ||
} | ||
|
||
//refresh Accessorie status | ||
async refreshDeviceStates(message) { | ||
const uuid = this.api.hap.uuid.generate(message.id); | ||
const deviceAccessorie = this.deviceAccessories.get(uuid); | ||
if (deviceAccessorie) { | ||
deviceAccessorie.updateState(message); | ||
} | ||
} | ||
|
||
// Called from device classes | ||
registerPlatformAccessory(platformAccessory) { | ||
this.log.log(`Register Platform Accessory ${platformAccessory.displayName}`); | ||
this.api.registerPlatformAccessories(pluginName, platformName, [platformAccessory]); | ||
} | ||
|
||
// Function invoked when homebridge tries to restore cached accessory. | ||
// Developer can configure accessory at here (like setup event handler). | ||
// Update current value. | ||
configureAccessory(accessory) { | ||
// this.log("Configuring cached accessory [%s]", accessory.displayName, accessory.context.deviceId, accessory.UUID); | ||
// Set the accessory to reachable if plugin can currently process the accessory, | ||
// otherwise set to false and update the reachability later by invoking | ||
// accessory.updateReachability() | ||
accessory.reachable = true; | ||
accessory.on('identify', function (paired, callback) { | ||
// this.log.debug('[IDENTIFY][%s]', accessory.displayName); | ||
callback(); | ||
}); | ||
this.accessories.set(accessory.UUID, accessory); | ||
} | ||
|
||
// Sample function to show how developer can remove accessory dynamically from outside event | ||
removeAccessory(accessory) { | ||
if (accessory) { | ||
this.log.log(`Remove Accessory ${accessory.displayName}`); | ||
this.api.unregisterPlatformAccessories(pluginName, platformName, [accessory]); | ||
this.accessories.delete(accessory.UUID); | ||
this.deviceAccessories.delete(accessory.UUID); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.