mirror of
https://github.com/home-assistant/frontend.git
synced 2025-12-20 02:38:53 +00:00
39 lines
1.0 KiB
JavaScript
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;
|
|
});
|
|
}
|