Skip to content

Commit

Permalink
feat!: Move the sensor consumable type as a subtype of the new type c…
Browse files Browse the repository at this point in the history
…leaning (master branch)
  • Loading branch information
DGAlexandru committed Jan 8, 2025
1 parent 0fc8ad9 commit 0a9e144
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class ConsumableStateAttribute extends StateAttribute {
ConsumableStateAttribute.TYPE = Object.freeze({
FILTER: "filter",
BRUSH: "brush",
SENSOR: "sensor",
MOP: "mop",
DETERGENT: "detergent",
BIN: "bin"
BIN: "bin",
CLEANING: "cleaning"
});

/**
Expand All @@ -46,7 +46,8 @@ ConsumableStateAttribute.SUB_TYPE = Object.freeze({
SECONDARY: "secondary",
SIDE_LEFT: "side_left",
SIDE_RIGHT: "side_right",
DOCK: "dock"
DOCK: "dock",
SENSOR: "sensor"
});

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class ConsumableMonitoringCapabilityMqttHandle extends CapabilityMqttHandle {
const TYPE_MAPPING = Object.freeze({
[stateAttrs.ConsumableStateAttribute.TYPE.BRUSH]: "Brush",
[stateAttrs.ConsumableStateAttribute.TYPE.FILTER]: "Filter",
[stateAttrs.ConsumableStateAttribute.TYPE.SENSOR]: "Sensor cleaning",
[stateAttrs.ConsumableStateAttribute.TYPE.CLEANING]: "Cleaning",
[stateAttrs.ConsumableStateAttribute.TYPE.MOP]: "Mop",
[stateAttrs.ConsumableStateAttribute.TYPE.DETERGENT]: "Detergent",
[stateAttrs.ConsumableStateAttribute.TYPE.BIN]: "Bin",
Expand All @@ -211,6 +211,7 @@ const SUBTYPE_MAPPING = Object.freeze({
[stateAttrs.ConsumableStateAttribute.SUB_TYPE.ALL]: "",
[stateAttrs.ConsumableStateAttribute.SUB_TYPE.NONE]: "",
[stateAttrs.ConsumableStateAttribute.SUB_TYPE.DOCK]: "Dock",
[stateAttrs.ConsumableStateAttribute.SUB_TYPE.SENSOR]: "Sensor"
});

ConsumableMonitoringCapabilityMqttHandle.OPTIONAL = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,14 @@ class DreameConsumableMonitoringCapability extends ConsumableMonitoringCapabilit
break;
}
break;
case ConsumableStateAttribute.TYPE.SENSOR:
if (this.miot_actions.reset_sensor) {
switch (subType) {
case ConsumableStateAttribute.SUB_TYPE.ALL:
case ConsumableStateAttribute.TYPE.CLEANING:
switch (subType) {
case ConsumableStateAttribute.SUB_TYPE.SENSOR:
if (this.miot_actions.reset_sensor) {
payload = this.miot_actions.reset_sensor;
break;
}
}
break;

}
break;
case ConsumableStateAttribute.TYPE.MOP:
Expand Down Expand Up @@ -264,8 +265,8 @@ class DreameConsumableMonitoringCapability extends ConsumableMonitoringCapabilit
) {
if (msg.piid === this.miot_properties.sensor.piid) {
consumable = new ConsumableStateAttribute({
type: ConsumableStateAttribute.TYPE.SENSOR,
subType: ConsumableStateAttribute.SUB_TYPE.ALL,
type: ConsumableStateAttribute.TYPE.CLEANING,
subType: ConsumableStateAttribute.SUB_TYPE.SENSOR,
remaining: {
value: Math.round(Math.max(0, msg.value * 60)),
unit: ConsumableStateAttribute.UNITS.MINUTES
Expand Down Expand Up @@ -350,8 +351,8 @@ class DreameConsumableMonitoringCapability extends ConsumableMonitoringCapabilit
if (this.miot_properties.sensor) {
availableConsumables.push(
{
type: ConsumableStateAttribute.TYPE.SENSOR,
subType: ConsumableStateAttribute.SUB_TYPE.ALL,
type: ConsumableStateAttribute.TYPE.CLEANING,
subType: ConsumableStateAttribute.SUB_TYPE.SENSOR,
unit: ConsumableStateAttribute.UNITS.MINUTES,
maxValue: 30 * 60
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const MOCKED_CONSUMABLES = Object.freeze([
serviceLife: 10,
},
{
type: ConsumableStateAttribute.TYPE.SENSOR,
subType: ConsumableStateAttribute.SUB_TYPE.ALL,
type: ConsumableStateAttribute.TYPE.CLEANING,
subType: ConsumableStateAttribute.SUB_TYPE.SENSOR,
serviceLife: 5,
},
{
Expand Down Expand Up @@ -127,8 +127,8 @@ class MockConsumableMonitoringCapability extends ConsumableMonitoringCapability
unit: ConsumableStateAttribute.UNITS.MINUTES
},
{
type: ConsumableStateAttribute.TYPE.SENSOR,
subType: ConsumableStateAttribute.SUB_TYPE.ALL,
type: ConsumableStateAttribute.TYPE.CLEANING,
subType: ConsumableStateAttribute.SUB_TYPE.SENSOR,
unit: ConsumableStateAttribute.UNITS.MINUTES
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class RoborockConsumableMonitoringCapability extends ConsumableMonitoringCapabil
}
}),
new ConsumableStateAttribute({
type: ConsumableStateAttribute.TYPE.SENSOR,
subType: ConsumableStateAttribute.SUB_TYPE.ALL,
type: ConsumableStateAttribute.TYPE.CLEANING,
subType: ConsumableStateAttribute.SUB_TYPE.SENSOR,
remaining: {
value: CONVERT_TO_MINUTES_REMAINING(data[0].sensor_dirty_time, 30),
unit: ConsumableStateAttribute.UNITS.MINUTES
Expand Down Expand Up @@ -155,8 +155,8 @@ class RoborockConsumableMonitoringCapability extends ConsumableMonitoringCapabil
maxValue: 150 * 60
},
{
type: ConsumableStateAttribute.TYPE.SENSOR,
subType: ConsumableStateAttribute.SUB_TYPE.ALL,
type: ConsumableStateAttribute.TYPE.CLEANING,
subType: ConsumableStateAttribute.SUB_TYPE.SENSOR,
unit: ConsumableStateAttribute.UNITS.MINUTES,
maxValue: 30 * 60
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
{
"__class": "ConsumableStateAttribute",
"metaData": {},
"type": "sensor",
"subType": "all",
"type": "cleaning",
"subType": "sensor",
"remaining": {
"value": 123,
"unit": "minutes"
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export interface MapSegmentRenameRequestParameters {
name: string;
}

export type ConsumableType = "filter" | "brush" | "sensor" | "mop" | "detergent";
export type ConsumableType = "filter" | "brush" | "mop" | "detergent" | "bin" | "cleaning";
export type ConsumableSubType = "none" | "all" | "main" | "secondary" | "side_left" | "side_right";
export type ConsumableUnit = "minutes" | "percent";

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/NoCloudEventControls.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {FunctionComponent} from "react";
import {NoCloudEvent, NoCloudEventInteraction} from "../api";
import {ConsumableSubType, ConsumableType, NoCloudEvent, NoCloudEventInteraction} from "../api";
import {Button, ButtonGroup, Stack, styled, Typography} from "@mui/material";
import {getConsumableName} from "../utils";
import {formatRelative} from "date-fns";
Expand Down Expand Up @@ -47,7 +47,7 @@ const ConsumableDepletedEventControl: FunctionComponent<NoCloudEventRenderProps>
<Stack>
<EventTimestamp timestamp={event.timestamp}/>
<Typography color={color} style={textStyle} sx={{mr: 1}}>
The consumable <em>{getConsumableName(event.type, event.subType)}</em> is depleted
The consumable <em>{getConsumableName(event.type as ConsumableType, event.subType as ConsumableSubType)}</em> is depleted
</Typography>
</Stack>
<Button
Expand Down
13 changes: 7 additions & 6 deletions frontend/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//Adapted from https://stackoverflow.com/a/34270811/10951033
import {NoCloudDataPoint} from "./api";
import {ConsumableSubType, ConsumableType, NoCloudDataPoint} from "./api";
import {useCallback, useLayoutEffect, useRef} from "react";

export function convertSecondsToHumans(seconds: number, showSeconds = true, showDays = true): string {
Expand Down Expand Up @@ -126,26 +126,27 @@ export const deepCopy = <T>(target: T): T => {
return target;
};

const consumableTypeMapping: Record<string, string> = {
const consumableTypeMapping: Record<ConsumableType, string> = {
"brush": "Brush",
"filter": "Filter",
"sensor": "Sensor cleaning",
"cleaning": "Cleaning",
"mop": "Mop",
"detergent": "Detergent",
"bin": "Bin"
};

const consumableSubtypeMapping: Record<string, string> = {
const consumableSubtypeMapping: Record<ConsumableType, string> = {
"main": "Main",
"secondary": "Secondary",
"side_right": "Right",
"side_left": "Left",
"all": "",
"none": "",
"dock": "Dock"
"dock": "Dock",
"sensor": "Sensor"
};

export const getConsumableName = (type: string, subType?: string): string => {
export const getConsumableName = (type: ConsumableType, subType?: ConsumableType): string => {
let ret = "";
if (subType && subType in consumableSubtypeMapping) {
ret += consumableSubtypeMapping[subType] + " ";
Expand Down

0 comments on commit 0a9e144

Please sign in to comment.