1
0
mirror of https://github.com/home-assistant/frontend.git synced 2025-12-20 02:38:53 +00:00
Files
frontend/src/panels/lovelace/common/process-config-entities.js
2018-11-02 16:00:25 +01:00

39 lines
1.0 KiB
JavaScript

// Parse array of entity objects from config
import isValidEntityId from "../../../common/entity/valid_entity_id";
export default function processConfigEntities(entities) {
if (!entities || !Array.isArray(entities)) {
throw new Error("Entities need to be an array");
}
return entities.map((entityConf, index) => {
if (
typeof entityConf === "object" &&
!Array.isArray(entityConf) &&
entityConf.type
) {
return entityConf;
}
if (typeof entityConf === "string") {
entityConf = { entity: entityConf };
} else if (typeof entityConf === "object" && !Array.isArray(entityConf)) {
if (!entityConf.entity) {
throw new Error(
`Entity object at position ${index} is missing entity field.`
);
}
} else {
throw new Error(`Invalid entity specified at position ${index}.`);
}
if (!isValidEntityId(entityConf.entity)) {
throw new Error(
`Invalid entity ID at position ${index}: ${entityConf.entity}`
);
}
return entityConf;
});
}