device action builder

This commit is contained in:
ari committed 2022-06-17 13:37:25 -04:00
1 parent 6be6005531
commit 029df7932e
16 files changed
+725 -434

No files matched your search

@@ -11,3 +11,13 @@ test("similarity score", () => {
].map((x) => computeSimilarity(x, "light.balcony", "Balcony Light"));
expect(searchTerms).toStrictEqual(searchTerms.sort());
});
test("similarity score", () => {
expect(
computeSimilarity(
"li",
"6592b3d6d05d3de6fa26c3dbd2edecc2",
"Balcony Lights"
)
).toBeGreaterThan(0);
});
@@ -51,7 +51,7 @@ export function computeSimilarity(
if (lowCaseId.includes(segment)) {
similarityScore += lowCaseId.length / lowCaseId.length;
}
if (lowCaseId.includes(segment)) {
if (lowCaseLabel.includes(segment)) {
similarityScore += lowCaseLabel.length / lowCaseLabel.length;
}
}
+42 -16
View File
@@ -1,22 +1,48 @@
import { FC } from "react";
import { useHAEntities } from 'haService';
import { InputAutoComplete, InputAutoCompletePropsBase } from './InputAutoComplete';
import { useHAEntities } from "haService";
import {
InputAutoComplete,
InputAutoCompletePropsBase,
} from "./InputAutoComplete";
import { prettyName } from "utils/formatting";
export type InputEntityProps = InputAutoCompletePropsBase & {
restrictToDomain?: string[],
}
restrictToDomain?: string[];
preSelectedEntityIds?: string[];
};
export const InputEntity: FC<InputEntityProps> = props => {
// state
const entities = useHAEntities();
return <InputAutoComplete
{...props}
validateOption={v => entities.validateOptions(v, props.restrictToDomain)}
options={entities.getOptions(props.restrictToDomain)}
getID={entities.getID}
getLabel={entities.getLabel}
groupBy={opt => (typeof opt !== 'string') ? prettyName(opt.domain) : ''}
export const InputEntity: FC<InputEntityProps> = (props) => {
// state
const entities = useHAEntities();
let options = entities.getOptions(props.restrictToDomain);
if (props.preSelectedEntityIds) {
options = options.filter((opt) =>
props.preSelectedEntityIds?.includes(entities.getID(opt))
);
}
const validateOptions = (v: string[]): null | string[] => {
const errors: string[] = [];
if (props.preSelectedEntityIds) {
v.forEach((eid) => {
if (!props.preSelectedEntityIds?.includes(eid)) {
errors.push(`"${eid}" is not a valid selection`);
}
});
}
errors.concat(entities.validateOptions(v, props.restrictToDomain) ?? []);
if (errors.length > 0) {
return errors;
}
return null;
};
return (
<InputAutoComplete
{...props}
validateOption={validateOptions}
options={options}
getID={entities.getID}
getLabel={entities.getLabel}
groupBy={(opt) => (typeof opt !== "string" ? prettyName(opt.domain) : "")}
/>
}
);
};
@@ -19,6 +19,7 @@ export type Props<T extends string | Object> = {
title?: string;
helperText?: string;
endAdornment?: ReactNode;
disabled?: boolean;
};
export function InputList<T extends string | Object>({
current,
@@ -33,6 +34,7 @@ export function InputList<T extends string | Object>({
title,
helperText,
endAdornment,
disabled,
}: Props<T>) {
const invalidCurrent =
current && !options.map(getKey).includes(getKey(current));
@@ -42,6 +44,7 @@ export function InputList<T extends string | Object>({
variant="filled"
sx={{ marginRight: "0.25em", minWidth: `${label.length * 0.75}em` }}
fullWidth
disabled={disabled}
>
<InputLabel>{label}</InputLabel>
<Select
@@ -60,6 +63,7 @@ export function InputList<T extends string | Object>({
variant="filled"
error={invalidCurrent}
fullWidth
disabled={disabled}
>
{options.map((t, i) => (
<MenuItem key={i} value={getKey(t)}>