From 3e9e388dddf7debcffd0e43121919bb92e178f68 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 18 Nov 2025 10:55:51 +0100 Subject: [PATCH] new endpoints 5 --- apps/ui/static/api-client.js | 8 ++++++++ apps/ui/templates/device.html | 25 ++++++------------------- apps/ui/templates/room.html | 7 +++++++ apps/ui/templates/rooms.html | 4 ++++ 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/apps/ui/static/api-client.js b/apps/ui/static/api-client.js index 2efca82..26a0a52 100644 --- a/apps/ui/static/api-client.js +++ b/apps/ui/static/api-client.js @@ -6,6 +6,14 @@ */ class HomeAutomationClient { + /** + * Get layout info for a specific device + * @param {string} deviceId - Device ID + * @returns {Promise} Layout info + */ + async getDeviceLayout(deviceId) { + return await this.fetch(this.api(`/devices/${deviceId}/layout`)); + } constructor() { this.eventSource = null; this.eventListeners = []; diff --git a/apps/ui/templates/device.html b/apps/ui/templates/device.html index be53790..6907325 100644 --- a/apps/ui/templates/device.html +++ b/apps/ui/templates/device.html @@ -323,25 +323,12 @@ try { // Load device info using API client - const devicesData = await window.apiClient.getDevices(); - deviceData = window.apiClient.findDevice(devicesData, deviceId); - - if (!deviceData) { - throw new Error(`Gerät "${deviceId}" nicht gefunden`); - } - - // Load layout to get room - const layoutData = await window.apiClient.getLayout(); - for (const room of layoutData.rooms) { - if (room.devices.includes(deviceId)) { - roomName = room.name; - break; - } - } - - // Load all device states at once - const allStates = await window.apiClient.getAllStates(); - deviceState = allStates[deviceId] || {}; + // NEW: Use new endpoints for device info and layout + deviceData = await window.apiClient.getDeviceState(deviceId); + const layoutInfo = await window.apiClient.getDeviceLayout(deviceId); + roomName = layoutInfo.room; + // deviceState is now the result of getDeviceState + deviceState = deviceData; // Update header document.getElementById('device-icon').textContent = deviceIcons[deviceData.type] || '📱'; diff --git a/apps/ui/templates/room.html b/apps/ui/templates/room.html index eaef772..b225285 100644 --- a/apps/ui/templates/room.html +++ b/apps/ui/templates/room.html @@ -241,8 +241,11 @@ try { // Load layout and devices using API client + // NEW: Use device layout endpoint for each device in this room const layoutData = await window.apiClient.getLayout(); const devicesData = await window.apiClient.getDevices(); + // Example: For each device in room.devices, you could fetch layout info via + // await window.apiClient.fetch(window.apiClient.api(`/devices/${device_id}/layout`)); console.log('Room name from URL:', roomName); console.log('Available rooms:', layoutData.rooms.map(r => r.name)); @@ -287,7 +290,11 @@ } // Load all device states at once + // NEW: Use device state endpoint for each device + // Fallback: load all states as before const allStates = await window.apiClient.getAllStates(); + // Example: For each device in room.devices, you could fetch state via + // await window.apiClient.getDeviceState(device_id); roomDevices.forEach(device => { deviceStates[device.device_id] = allStates[device.device_id] || null; }); diff --git a/apps/ui/templates/rooms.html b/apps/ui/templates/rooms.html index d38f4fa..46426d2 100644 --- a/apps/ui/templates/rooms.html +++ b/apps/ui/templates/rooms.html @@ -195,8 +195,12 @@ try { // Load layout and devices using API client + // NEW: Use device layout endpoint for each device + // Fallback: load all rooms as before const layoutData = await window.apiClient.getLayout(); const devicesData = await window.apiClient.getDevices(); + // Example: For each device, you could also fetch layout info via + // await window.apiClient.fetch(window.apiClient.api(`/devices/${device_id}/layout`)); // Create device lookup const deviceMap = {};