-
-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mqtt): Optionally expose KeyLockCapability
- Loading branch information
Showing
5 changed files
with
99 additions
and
2 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
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,8 @@ | ||
const SimpleToggleCapabilityMqttHandle = require("./SimpleToggleCapabilityMqttHandle"); | ||
|
||
|
||
class KeyLockCapabilityMqttHandle extends SimpleToggleCapabilityMqttHandle {} | ||
|
||
KeyLockCapabilityMqttHandle.OPTIONAL = true; | ||
|
||
module.exports = KeyLockCapabilityMqttHandle; |
86 changes: 86 additions & 0 deletions
86
backend/lib/mqtt/capabilities/SimpleToggleCapabilityMqttHandle.js
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,86 @@ | ||
const CapabilityMqttHandle = require("./CapabilityMqttHandle"); | ||
|
||
const capabilities = require("../../core/capabilities"); | ||
const Commands = require("../common/Commands"); | ||
const ComponentType = require("../homeassistant/ComponentType"); | ||
const DataType = require("../homie/DataType"); | ||
const EntityCategory = require("../homeassistant/EntityCategory"); | ||
const InLineHassComponent = require("../homeassistant/components/InLineHassComponent"); | ||
const PropertyMqttHandle = require("../handles/PropertyMqttHandle"); | ||
|
||
class SimpleToggleCapabilityMqttHandle extends CapabilityMqttHandle { | ||
/** | ||
* @param {object} options | ||
* @param {import("../handles/RobotMqttHandle")} options.parent | ||
* @param {import("../MqttController")} options.controller MqttController instance | ||
* @param {import("../../core/ValetudoRobot")} options.robot | ||
* @param {import("../../core/capabilities/SimpleToggleCapability")} options.capability | ||
*/ | ||
constructor(options) { | ||
super(Object.assign(options, { | ||
friendlyName: CAPABILITIES_TO_FRIENDLY_NAME_MAPPING[options.capability.getType()] | ||
})); | ||
this.capability = options.capability; | ||
|
||
this.registerChild( | ||
new PropertyMqttHandle({ | ||
parent: this, | ||
controller: this.controller, | ||
topicName: "enabled", | ||
friendlyName: CAPABILITIES_TO_FRIENDLY_NAME_MAPPING[options.capability.getType()], | ||
datatype: DataType.ENUM, | ||
format: Object.values(Commands.SWITCH).join(","), | ||
setter: async (value) => { | ||
if (value === Commands.SWITCH.ON) { | ||
await this.capability.enable(); | ||
} else if (value === Commands.SWITCH.OFF) { | ||
await this.capability.disable(); | ||
} else { | ||
throw new Error("Invalid value"); | ||
} | ||
}, | ||
getter: async () => { | ||
const isEnabled = await this.capability.isEnabled(); | ||
|
||
if (isEnabled) { | ||
return Commands.SWITCH.ON; | ||
} else { | ||
return Commands.SWITCH.OFF; | ||
} | ||
}, | ||
}).also((prop) => { | ||
const capabilityType = options.capability.getType(); | ||
|
||
this.controller.withHass((hass) => { | ||
prop.attachHomeAssistantComponent( | ||
new InLineHassComponent({ | ||
hass: hass, | ||
robot: this.robot, | ||
name: capabilityType, | ||
friendlyName: CAPABILITIES_TO_FRIENDLY_NAME_MAPPING[capabilityType], | ||
componentType: ComponentType.SWITCH, | ||
autoconf: { | ||
state_topic: prop.getBaseTopic(), | ||
value_template: "{{ value }}", | ||
command_topic: prop.getBaseTopic() + "/set", | ||
icon: CAPABILITIES_TO_ICON_MAPPING[capabilityType], | ||
entity_category: EntityCategory.CONFIG, | ||
} | ||
}) | ||
); | ||
}); | ||
}) | ||
); | ||
} | ||
|
||
} | ||
|
||
const CAPABILITIES_TO_FRIENDLY_NAME_MAPPING = { | ||
[capabilities.KeyLockCapability.TYPE]: "Lock keys", | ||
}; | ||
|
||
const CAPABILITIES_TO_ICON_MAPPING = { | ||
[capabilities.KeyLockCapability.TYPE]: "mdi:lock", | ||
}; | ||
|
||
module.exports = SimpleToggleCapabilityMqttHandle; |
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
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