Skip to content

Commit

Permalink
Add default calibrations for platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrMachowski committed May 31, 2023
1 parent 2e26f7d commit 3e654dd
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 51 deletions.
8 changes: 0 additions & 8 deletions docs/templates/neato.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@

This platform can be used to control vacuums connected to Home Assistant using built-in Neato integration.

## Calibration

You can calibrate the map using following config:
```yaml
calibration_source:
identity: true
```
## Available templates

* ### Room cleaning (`vacuum_clean_segment`)
Expand Down
8 changes: 0 additions & 8 deletions docs/templates/romedtinoSimpleWyze.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ This platform can be used to control vacuums connected to Home Assistant using c

To reset a value for a given consumable press and hold a matching tile.

## Calibration

You can calibrate the map using following config:
```yaml
calibration_source:
identity: true
```
## Available templates

* ### Room cleaning (`vacuum_clean_segment`)
Expand Down
8 changes: 0 additions & 8 deletions docs/templates/roomba.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@

This platform can be used to control vacuums connected to Home Assistant using built-in Roomba integration.

## Calibration

You can calibrate the map using following config:
```yaml
calibration_source:
identity: true
```
## Available templates

* ### Room cleaning (`vacuum_clean_segment`)
Expand Down
14 changes: 11 additions & 3 deletions src/config-validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function validateIconConfig(config: IconActionConfig): TranslatableString[] {
return ["validation.preset.icons.invalid"];
}
const errors: TranslatableString[] = [];
if (!config.icon && config.type !== "menu") {
if (!config.icon && config.type !== "menu" && !config.replace_config) {
errors.push("validation.preset.icons.icon.missing");
}
return errors;
Expand Down Expand Up @@ -239,11 +239,20 @@ function validateMapModeConfig(

function validatePreset(config: CardPresetConfig, nameRequired: boolean, language: Language): TranslatableString[] {
const errors: TranslatableString[] = [];
const platformsWithDefaultCalibration = [
PlatformGenerator.DEEBOTUNIVERSE_DEEBOT_4_HOME_ASSISTANT_PLATFORM,
PlatformGenerator.NEATO_PLATFORM,
PlatformGenerator.ROMEDTINO_SIMPLE_WAZE_PLATFORM,
PlatformGenerator.ROOMBA_PLATFORM,
];
const mandatoryFields = new Map<string, string>([
["entity", "validation.preset.entity.missing"],
["map_source", "validation.preset.map_source.missing"],
["calibration_source", "validation.preset.calibration_source.missing"],
]);
const vacuumPlatform = PlatformGenerator.getPlatformName(config.vacuum_platform);
if (!platformsWithDefaultCalibration.includes(vacuumPlatform)) {
mandatoryFields.set("calibration_source", "validation.preset.calibration_source.missing");
}
const params = Object.keys(config);
mandatoryFields.forEach((v: string, k: string) => {
if (!params.includes(k)) {
Expand All @@ -254,7 +263,6 @@ function validatePreset(config: CardPresetConfig, nameRequired: boolean, languag
if (config.calibration_source) validateCalibrationSource(config.calibration_source).forEach(e => errors.push(e));
if (config.vacuum_platform && !PlatformGenerator.getPlatforms().includes(config.vacuum_platform))
errors.push(["validation.preset.platform.invalid", "{0}", config.vacuum_platform]);
const vacuumPlatform = config.vacuum_platform ?? "default";
(config.icons ?? []).flatMap(i => validateIconConfig(i)).forEach(e => errors.push(e));
(config.tiles ?? []).flatMap(i => validateTileConfig(i)).forEach(e => errors.push(e));
(config.map_modes ?? [])
Expand Down
9 changes: 9 additions & 0 deletions src/model/generators/platform-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as simpleWyzeTemplate from "./platform_templates/romedtino_simple-wyze-
import * as setupDecimalTemplate from "./platform_templates/setup_decimal.json";
import * as setupIntegerTemplate from "./platform_templates/setup_integer.json";
import {
CalibrationPoint,
IconTemplate,
MapModeConfig,
PlatformTemplate,
Expand Down Expand Up @@ -86,6 +87,10 @@ export class PlatformGenerator {
return Array.from(PlatformGenerator.TEMPLATES.keys());
}

public static getPlatformName(platform: string | undefined): string {
return platform ?? PlatformGenerator.XIAOMI_MIIO_PLATFORM;
}

public static getPlatformsDocumentationUrl(platform: string): string {
const file =
PlatformGenerator.TEMPLATE_DOCUMENTATIONS_URLS.get(platform) ??
Expand Down Expand Up @@ -132,6 +137,10 @@ export class PlatformGenerator {
return undefined;
}

public static getCalibration(platform: string | undefined): CalibrationPoint[] | undefined {
return this.getPlatformTemplate(PlatformGenerator.getPlatformName(platform)).calibration_points;
}

private static getPlatformTemplate(platform: string): PlatformTemplate {
return (
this.TEMPLATES.get(platform) ??
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"icon": "mdi:select-drag",
"selection_type": "MANUAL_RECTANGLE",
"coordinates_rounding": true,
"coordinates_to_meters_divider": 1000,
"coordinates_to_meters_divider": -1,
"repeats_type": "NONE",
"max_selections": 1,
"service_call_schema": {
Expand All @@ -47,7 +47,7 @@
"selection_type": "PREDEFINED_RECTANGLE",
"max_selections": 1,
"coordinates_rounding": true,
"coordinates_to_meters_divider": 1000,
"coordinates_to_meters_divider": -1,
"repeats_type": "NONE",
"service_call_schema": {
"service": "xiaomi_miio.vacuum_clean_zone",
Expand Down Expand Up @@ -141,5 +141,37 @@
}
}
}
],
"calibration_points": [
{
"vacuum": {
"x": 0,
"y": 0
},
"map": {
"x": 400,
"y": 400
}
},
{
"vacuum": {
"x": 6400,
"y": 0
},
"map": {
"x": 528,
"y": 400
}
},
{
"vacuum": {
"x": 0,
"y": 6400
},
"map": {
"x": 400,
"y": 528
}
}
]
}
34 changes: 33 additions & 1 deletion src/model/generators/platform_templates/neato.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,37 @@
"unit": "unit.meter_squared_shortcut"
}
]
}
},
"calibration_points": [
{
"vacuum": {
"x": 0,
"y": 0
},
"map": {
"x": 0,
"y": 0
}
},
{
"vacuum": {
"x": 1,
"y": 0
},
"map": {
"x": 1,
"y": 0
}
},
{
"vacuum": {
"x": 0,
"y": 1
},
"map": {
"x": 0,
"y": 1
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,37 @@
"unit": "unit.hour_shortcut"
}
]
}
},
"calibration_points": [
{
"vacuum": {
"x": 0,
"y": 0
},
"map": {
"x": 0,
"y": 0
}
},
{
"vacuum": {
"x": 1,
"y": 0
},
"map": {
"x": 1,
"y": 0
}
},
{
"vacuum": {
"x": 0,
"y": 1
},
"map": {
"x": 0,
"y": 1
}
}
]
}
34 changes: 33 additions & 1 deletion src/model/generators/platform_templates/roomba.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,37 @@
]
}
]
}
},
"calibration_points": [
{
"vacuum": {
"x": 0,
"y": 0
},
"map": {
"x": 0,
"y": 0
}
},
{
"vacuum": {
"x": 1,
"y": 0
},
"map": {
"x": 1,
"y": 0
}
},
{
"vacuum": {
"x": 0,
"y": 1
},
"map": {
"x": 0,
"y": 1
}
}
]
}
2 changes: 2 additions & 0 deletions src/model/map_objects/manual-rectangle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ export class ManualRectangle extends MapObject {
const width = Math.abs(x2 - x1);
const height = Math.abs(y2 - y1);
const divider = this._context.coordinatesToMetersDivider();
if (divider === -1)
return "";
const rounder = (v: number): string => (v / divider).toFixed(1);
return `${rounder(width)}${this.localize("unit.meter_shortcut")} x ${rounder(height)}${this.localize(
"unit.meter_shortcut",
Expand Down
3 changes: 2 additions & 1 deletion src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export interface CardPresetConfig extends ConditionalObjectConfig {
readonly map_source: MapSourceConfig;
readonly map_locked?: boolean;
readonly two_finger_pan?: boolean;
readonly calibration_source: CalibrationSourceConfig;
readonly calibration_source?: CalibrationSourceConfig;
readonly icons?: IconActionConfig[];
readonly append_icons?: boolean;
readonly tiles?: TileConfig[];
Expand Down Expand Up @@ -112,6 +112,7 @@ export interface PlatformTemplate {
readonly from_sensors?: TileFromSensorTemplate[];
};
readonly icons?: IconTemplate[];
readonly calibration_points?: CalibrationPoint[];
}

export interface TileTemplate extends TileConfig {
Expand Down
5 changes: 3 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Modifier } from "./model/map_mode/modifier";
import { HomeAssistantFixed } from "./types/fixes";
import { ServiceCallSchema } from "./model/map_mode/service-call-schema";
import { TemplatableItemValue } from "./model/map_mode/templatable-value";
import { PlatformGenerator } from "./model/generators/platform-generator";

export function stopEvent(event: MouseEvent | TouchEvent): void {
event.preventDefault();
Expand Down Expand Up @@ -66,7 +67,7 @@ export function getWatchedEntitiesForPreset(config: CardPresetConfig, language:
if (config.map_source.camera) {
watchedEntities.add(config.map_source.camera);
}
if (config.calibration_source.entity) {
if (config.calibration_source?.entity) {
watchedEntities.add(config.calibration_source.entity);
}
(config.conditions ?? [])
Expand Down Expand Up @@ -98,7 +99,7 @@ export function getWatchedEntitiesForPreset(config: CardPresetConfig, language:
if (e) watchedEntities.add(e);
});
(config.map_modes ?? [])
.map(m => new MapMode(config.vacuum_platform ?? "default", m, language))
.map(m => new MapMode(PlatformGenerator.getPlatformName(config.vacuum_platform), m, language))
.forEach(m => getWatchedEntitiesForMapMode(m).forEach(e => watchedEntities.add(e)));
return watchedEntities;
}
Expand Down
Loading

0 comments on commit 3e654dd

Please sign in to comment.