Skip to content

Commit

Permalink
feat(mqtt): Allow resetting consumables via MQTT and set consumables …
Browse files Browse the repository at this point in the history
…to be optionally exposed in new deployments
  • Loading branch information
Hypfer committed May 28, 2023
1 parent 5f85637 commit ac06012
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
3 changes: 3 additions & 0 deletions backend/lib/Configuration.js
Expand Up @@ -82,6 +82,9 @@ class Configuration {
if (!parsedConfig.mqtt.optionalExposedCapabilities.includes("CurrentStatisticsCapability")) {
parsedConfig.mqtt.optionalExposedCapabilities.push("CurrentStatisticsCapability");
}
if (!parsedConfig.mqtt.optionalExposedCapabilities.includes("ConsumableMonitoringCapability")) {
parsedConfig.mqtt.optionalExposedCapabilities.push("ConsumableMonitoringCapability");
}
// END migration code to be removed with the next version

parsedConfig._version = Tools.GET_VALETUDO_VERSION();
Expand Down
5 changes: 3 additions & 2 deletions backend/lib/doc/Configuration.openapi.json
Expand Up @@ -280,9 +280,10 @@
"items": {
"type": "string",
"enum": [
"SpeakerVolumeControlCapability",
"ConsumableMonitoringCapability",
"CurrentStatisticsCapability",
"TotalStatisticsCapability"
"TotalStatisticsCapability",
"SpeakerVolumeControlCapability"
]
}
}
Expand Down
@@ -1,5 +1,6 @@
const CapabilityMqttHandle = require("./CapabilityMqttHandle");

const Commands = require("../common/Commands");
const ComponentType = require("../homeassistant/ComponentType");
const DataType = require("../homie/DataType");
const EntityCategory = require("../homeassistant/EntityCategory");
Expand Down Expand Up @@ -114,7 +115,7 @@ class ConsumableMonitoringCapabilityMqttHandle extends CapabilityMqttHandle {
new InLineHassComponent({
hass: hass,
robot: this.robot,
name: this.capability.getType() + "_" + topicId.replace("-", "_"),
name: `${this.capability.getType()}_${topicId.replace("-", "_")}`,
friendlyName: this.genConsumableFriendlyName(type, subType),
componentType: ComponentType.SENSOR,
baseTopicReference: this.controller.hassAnchorProvider.getTopicReference(
Expand All @@ -138,6 +139,38 @@ class ConsumableMonitoringCapabilityMqttHandle extends CapabilityMqttHandle {
});
})
);

this.registerChild(
new PropertyMqttHandle({
parent: this,
controller: this.controller,
topicName: `${topicId}/reset`,
friendlyName: "Reset the consumable",
datatype: DataType.ENUM,
format: Commands.BASIC.PERFORM,
setter: async (value) => {
await this.capability.resetConsumable(type, subType);
}
}).also((prop) => {
this.controller.withHass((hass) => {
prop.attachHomeAssistantComponent(
new InLineHassComponent({
hass: hass,
robot: this.robot,
name: `${this.capability.getType()}_${topicId.replace("-", "_")}_reset`,
friendlyName: `Reset ${this.genConsumableFriendlyName(type, subType)} Consumable`,
componentType: ComponentType.BUTTON,
autoconf: {
command_topic: `${prop.getBaseTopic()}/set`,
payload_press: Commands.BASIC.PERFORM,
icon: "mdi:restore",
entity_category: EntityCategory.DIAGNOSTIC
}
})
);
});
})
);
}

async refresh() {
Expand Down Expand Up @@ -180,6 +213,6 @@ const SUBTYPE_MAPPING = Object.freeze({
[stateAttrs.ConsumableStateAttribute.SUB_TYPE.DOCK]: "Dock",
});

ConsumableMonitoringCapabilityMqttHandle.OPTIONAL = false;
ConsumableMonitoringCapabilityMqttHandle.OPTIONAL = true;

module.exports = ConsumableMonitoringCapabilityMqttHandle;
1 change: 1 addition & 0 deletions backend/lib/mqtt/homeassistant/ComponentType.js
Expand Up @@ -6,6 +6,7 @@
const ComponentType = Object.freeze({
ALARM_CONTROL_PANEL: "alarm_control_panel",
BINARY_SENSOR: "binary_sensor",
BUTTON: "button",
CAMERA: "camera",
COVER: "cover",
DEVICE_TRACKER: "device_tracker",
Expand Down

0 comments on commit ac06012

Please sign in to comment.