Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/data/scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,17 @@ export interface SceneConfig {
name: string;
icon?: string;
entities: SceneEntities;
metadata?: SceneMetaData;
}

export interface SceneEntities {
[entityId: string]: string | { state: string; [key: string]: any };
}

export interface SceneMetaData {
[entityId: string]: { entity_only?: boolean | undefined };
}

export const activateScene = (
hass: HomeAssistant,
entityId: string
Expand Down
55 changes: 39 additions & 16 deletions src/panels/config/scene/ha-scene-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
SceneConfig,
SceneEntities,
SceneEntity,
SceneMetaData,
SCENE_IGNORED_DOMAINS,
showSceneEditor,
} from "../../../data/scene";
Expand Down Expand Up @@ -628,16 +629,22 @@ export class HaSceneEditor extends SubscribeMixin(
const filteredEntityReg = this._entityRegistryEntries.filter((entityReg) =>
this._entities.includes(entityReg.entity_id)
);
this._devices = [];
const newDevices: string[] = [];

for (const entityReg of filteredEntityReg) {
if (!entityReg.device_id) {
continue;
}
if (!this._devices.includes(entityReg.device_id)) {
this._devices = [...this._devices, entityReg.device_id];
const entityMetaData = config.metadata?.[entityReg.entity_id];
if (
!newDevices.includes(entityReg.device_id) &&
!entityMetaData?.entity_only
) {
newDevices.push(entityReg.device_id);
}
}

this._devices = newDevices;
}

private _entityPicked(ev: CustomEvent) {
Expand All @@ -646,18 +653,8 @@ export class HaSceneEditor extends SubscribeMixin(
if (this._entities.includes(entityId)) {
return;
}
const entityRegistry = this._entityRegistryEntries.find(
(entityReg) => entityReg.entity_id === entityId
);
if (
entityRegistry?.device_id &&
!this._devices.includes(entityRegistry.device_id)
) {
this._pickDevice(entityRegistry.device_id);
} else {
this._entities = [...this._entities, entityId];
this._storeState(entityId);
}
this._entities = [...this._entities, entityId];
this._storeState(entityId);
this._dirty = true;
}

Expand Down Expand Up @@ -815,6 +812,28 @@ export class HaSceneEditor extends SubscribeMixin(
);
}

private _calculateMetaData(): SceneMetaData {
const output: SceneMetaData = {};

for (const entityReg of this._entityRegistryEntries) {
if (!this._entities.includes(entityReg.entity_id)) {
continue;
}

const entityState = this._getCurrentState(entityReg.entity_id);

if (!entityState) {
continue;
}

output[entityReg.entity_id] = {
entity_only: !this._devices.includes(entityReg.device_id!),
};
}

return output;
}

private _calculateStates(): SceneEntities {
const output: SceneEntities = {};
this._entities.forEach((entityId) => {
Expand Down Expand Up @@ -847,7 +866,11 @@ export class HaSceneEditor extends SubscribeMixin(

private async _saveScene(): Promise<void> {
const id = !this.sceneId ? "" + Date.now() : this.sceneId!;
this._config = { ...this._config!, entities: this._calculateStates() };
this._config = {
...this._config!,
entities: this._calculateStates(),
metadata: this._calculateMetaData(),
};
try {
this._saving = true;
await saveScene(this.hass, id, this._config);
Expand Down
7 changes: 3 additions & 4 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2252,15 +2252,14 @@
"area": "Area",
"devices": {
"header": "Devices",
"introduction": "Add the devices that you want to be included in your scene. Set all the devices to the state you want for this scene.",
"introduction": "Add the devices that you want to be included in your scene. Set all entities in each device to the state you want for this scene.",
"add": "Add a device",
"delete": "Delete device"
},
"entities": {
"header": "Entities",
"introduction": "Entities that do not belong to a device can be set here.",
"without_device": "Entities without device",
"device_entities": "If you add an entity that belongs to a device, the device will be added.",
"introduction": "Individual entities can be added here.",
"without_device": "Entities",
"add": "Add an entity",
"delete": "Delete entity"
}
Expand Down