Last active
December 18, 2015 18:45
-
-
Save sweetpi/9157350 to your computer and use it in GitHub Desktop.
pimatic: providing devices in a plugin
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
class MyDevice extends env.devices.Device | |
actions: | |
turnOn: | |
description: "turns the switch on" | |
turnOff: | |
description: "turns the switch off" | |
changeStateTo: | |
description: "changes the switch to on or off" | |
params: | |
state: | |
type: Boolean | |
# Returns a promise | |
turnOn: -> @changeStateTo on | |
# Retuns a promise | |
turnOff: -> @changeStateTo off | |
# Retuns a promise that is fulfilled when done. | |
changeStateTo: (state) -> #... |
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
inSomeFunctionOfMyPlugin: => | |
# Got a new value, so emit it to the framework: | |
@emit "temperature", 20 |
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
class MyDevice extends env.devices.Device | |
attributes: | |
temperature: | |
description: "the messured temperature" | |
type: Number | |
unit: '°C' | |
getTemperature: -> Promise.resolve(30) | |
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
class MyDevice extends env.devices.Device | |
# ####constructor() | |
# Your constructor function must assign a name and id to the device. | |
constructor: (@config) -> | |
@name = @config.name | |
@id = @config.id | |
# ... | |
super() |
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
module.exports = (env) -> | |
class MyPlugin extends env.plugins.Plugin | |
init: (app, @framework, config) => | |
# ... | |
deviceConfigDef = require("./device-config-schema") | |
@framework.deviceManager.registerDeviceClass("MySwitch", { | |
configDef: deviceConfigDef.MySwitch, | |
createCallback: (config) => new MySwitch(config) | |
}) | |
class MySwitch extends env.devices.PowerSwitch | |
# ... | |
myPlugin = new MyPlugin | |
return myPlugin |
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
class MySwitch extends env.devices.PowerSwitch | |
# ####constructor() | |
# Your constructor function must assign a name and id to the device. | |
constructor: (@config) -> | |
@name = @config.name | |
@id = @config.id | |
# ... |
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
class YourTemperatureSensor extends env.devices.TemperatureSensor | |
temperature: null | |
constructor: (@config) -> | |
@name = "some name or from config" | |
@id = "some-id" | |
# update the temperature every 5 seconds | |
setInterval( ( => | |
@doYourStuff() | |
), 5000) | |
doYourStuff: () -> | |
# temperature = ... | |
@temperature = temperature | |
@emit "temperature", temperature | |
getTemperature: -> Promise.resolve(@temperature) |
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
class MyCustomDevice extends env.devices.Device | |
# [...] | |
getTemplateName: -> "customdevice" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is a typo in the template.coffe.
getTemplateName: -> "customdevice"
needs to begetTemplate: -> "customdevice"
.