Skip to content

Commit

Permalink
feat(mqtt): Provide segment information
Browse files Browse the repository at this point in the history
* chore: Move getSegments() impl from capability to map class

* feat(mqtt): Add map segments retrieval to map handle

* feat(mqtt): Add map segments HAss component
  • Loading branch information
depau authored Apr 23, 2021
1 parent 1df1caf commit f104189
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 17 deletions.
18 changes: 1 addition & 17 deletions lib/core/capabilities/MapSegmentationCapability.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const Capability = require("./Capability");
const MapLayer = require("../../entities/map/MapLayer");
const NotImplementedError = require("../NotImplementedError");
const ValetudoMapSegment = require("../../entities/core/ValetudoMapSegment");

/**
* @template {import("../ValetudoRobot")} T
Expand All @@ -12,21 +10,7 @@ class MapSegmentationCapability extends Capability {
* @returns {Promise<Array<import("../../entities/core/ValetudoMapSegment")>>}
*/
async getSegments() {
return this.robot.state.map.layers
.filter(e => e.type === MapLayer.TYPE.SEGMENT)
.map(e => {
let id = e.metaData.segmentId;

if (typeof id === "number") {
id = id.toString();
}

return new ValetudoMapSegment({
id: id,
name: e.metaData.name
});
}
);
return this.robot.state.map.getSegments();
}

/**
Expand Down
23 changes: 23 additions & 0 deletions lib/entities/map/ValetudoMap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const MapLayer = require("./MapLayer");
const SerializableEntity = require("../SerializableEntity");
const ValetudoMapSegment = require("../core/ValetudoMapSegment");

/**
* Represents a Valetudo standard issue map
Expand Down Expand Up @@ -77,6 +79,27 @@ class ValetudoMap extends SerializableEntity { //TODO: Current, Historic, Etc.
entities.forEach(e => this.addEntity(e));
}

/**
* @public
* @return {Array<ValetudoMapSegment>}
*/
getSegments() {
return this.layers
.filter(e => e.type === MapLayer.TYPE.SEGMENT)
.map(e => {
let id = e.metaData.segmentId;

if (typeof id === "number") {
id = id.toString();
}

return new ValetudoMapSegment({
id: id,
name: e.metaData.name
});
});
}

getIntersectingLayers(point) {
//TODO
}
Expand Down
46 changes: 46 additions & 0 deletions lib/mqtt/handles/MapNodeMqttHandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const ComponentType = require("../homeassistant/ComponentType");
const crc = require("crc");
const DataType = require("../homie/DataType");
const fs = require("fs");
const HassAnchor = require("../homeassistant/HassAnchor");
const InLineHassComponent = require("../homeassistant/components/InLineHassComponent");
const Logger = require("../../Logger");
const NodeMqttHandle = require("./NodeMqttHandle");
Expand Down Expand Up @@ -59,6 +60,51 @@ class MapNodeMqttHandle extends NodeMqttHandle {
);
}

this.registerChild(
new PropertyMqttHandle({
parent: this,
controller: this.controller,
topicName: "segments",
friendlyName: "Map segments",
datatype: DataType.STRING,
format: "json",
getter: async () => {
if (this.robot.state.map === null || !this.controller.provideMapData || !this.controller.isInitialized()) {
return {};
}
const res = {};
for (const segment of this.robot.state.map.getSegments()) {
res[segment.id] = segment.name;
}
await HassAnchor.getAnchor(HassAnchor.ANCHOR.MAP_SEGMENTS_LEN).post(Object.keys(res).length);
return res;
},
helpText: "This property contains a JSON mapping of segment IDs to segment names."
}).also((prop) => {
this.controller.withHass((hass) => {
prop.attachHomeAssistantComponent(
new InLineHassComponent({
hass: hass,
robot: this.robot,
name: "MapSegments",
friendlyName: "Map segments",
componentType: ComponentType.SENSOR,
baseTopicReference: HassAnchor.getTopicReference(HassAnchor.REFERENCE.HASS_MAP_SEGMENTS_STATE),
autoconf: {
state_topic: HassAnchor.getTopicReference(HassAnchor.REFERENCE.HASS_MAP_SEGMENTS_STATE),
icon: "mdi:vector-selection",
json_attributes_topic: prop.getBaseTopic(),
json_attributes_template: "{{ value }}"
},
topics: {
"": HassAnchor.getAnchor(HassAnchor.ANCHOR.MAP_SEGMENTS_LEN)
}
})
);
});
})
);

this.controller.withHass((hass) => {
this.registerChild(
new PropertyMqttHandle({
Expand Down
2 changes: 2 additions & 0 deletions lib/mqtt/homeassistant/HassAnchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ HassAnchor.ANCHOR = Object.freeze({
CONSUMABLE_VALUE: "consumable_value_",
FAN_SPEED: "fan_speed",
GOTO_PRESETS_LEN: "goto_presets_len",
MAP_SEGMENTS_LEN: "map_segments_len",
VACUUM_STATE: "vacuum_state",
WIFI_IPS: "wifi_ips",
WIFI_FREQUENCY: "wifi_freq",
Expand All @@ -211,6 +212,7 @@ HassAnchor.REFERENCE = Object.freeze({
ZONE_PRESETS: "zone_presets",
HASS_CONSUMABLE_STATE: "hass_consumable_state_",
HASS_GOTO_LOCATION_STATE: "hass_goto_location_state",
HASS_MAP_SEGMENTS_STATE: "hass_map_segments_state",
HASS_WATER_GRADE_PRESETS: "hass_water_grade_presets",
HASS_WIFI_CONFIG_ATTRS: "hass_wifi_config_attrs",
HAZZ_ZONE_CLEANING_STATE: "hass_zone_cleaning_state",
Expand Down

0 comments on commit f104189

Please sign in to comment.