import { FC } from "react"; import { useHAEntities } from "haService/HAEntities"; import { InputAutoComplete, InputAutoCompletePropsBase, } from "./InputAutoComplete"; import { prettyName } from "utils/formatting"; export type InputEntityProps = InputAutoCompletePropsBase & { restrictToDomain?: string[]; restrictedIntegrations?: string[]; preSelectedEntityIds?: string[]; restrictedDeviceClass?: string[]; restrictMode?: "and" | "or"; }; export const InputEntity: FC = (props) => { // state const entities = useHAEntities(); let options = entities.getOptions( props.restrictToDomain, props.restrictedIntegrations, props.restrictedDeviceClass, props.restrictMode ?? "and" ); 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, props.restrictedIntegrations ) ?? [] ); if (errors.length > 0) { return errors; } return null; }; return ( (typeof opt !== "string" ? prettyName(opt.domain) : "")} /> ); };