Last active
December 13, 2024 15:28
-
-
Save W-Floyd/8b6132f5e75b840c52e2f16e08f9aff8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StrategyDashboardDemo { | |
static async generate(config, hass) { | |
// Query all data we need. We will make it available to views by storing it in strategy options. | |
const [areas, devices, entities] = await Promise.all([ | |
hass.callWS({ type: "config/area_registry/list" }), | |
hass.callWS({ type: "config/device_registry/list" }), | |
hass.callWS({ type: "config/entity_registry/list" }), | |
]); | |
// Each view is based on a strategy so we delay rendering until it's opened | |
return { | |
views: areas.map((area) => ({ | |
strategy: { | |
type: "custom:my-demo", | |
area, | |
devices, | |
entities, | |
}, | |
title: area.name, | |
path: area.area_id, | |
})), | |
}; | |
} | |
} | |
class StrategyViewDemo { | |
static async generate(config, hass) { | |
const { area, devices, entities } = config; | |
const areaDevices = new Set(); | |
// Find all devices linked to this area | |
for (const device of devices) { | |
if (device.area_id === area.area_id) { | |
areaDevices.add(device.id); | |
} | |
} | |
const cards = []; | |
// Find all entities directly linked to this area | |
// or linked to a device linked to this area. | |
for (const entity of entities) { | |
if ( | |
entity.area_id | |
? entity.area_id === area.area_id | |
: areaDevices.has(entity.device_id) | |
) { | |
cards.push({ | |
type: "button", | |
entity: entity.entity_id, | |
}); | |
} | |
} | |
return { | |
cards: [ | |
{ | |
type: "grid", | |
cards, | |
}, | |
], | |
}; | |
} | |
} | |
customElements.define("ll-strategy-dashboard-my-demo", StrategyDashboardDemo); | |
customElements.define("ll-strategy-view-my-demo", StrategyViewDemo); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment