Skip to content

Commit

Permalink
fix issue that no devices have been detected (#39)
Browse files Browse the repository at this point in the history
* fix issue that no devices have been detected

* fix devices too

* use clone on device dialog

* add dummy for qr code action
  • Loading branch information
foxriver76 authored Jul 17, 2024
1 parent c9fb631 commit 6782131
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 30 deletions.
12 changes: 6 additions & 6 deletions src-admin/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ interface AppState extends GenericAppState {
matter: MatterConfig;
commissioning: CommissioningInfo | null;
nodeStates: { [uuid: string]: NodeStateResponse };
detectedDevices: DetectedRoom[] | null;
/** Undefined if no detection ran yet */
detectedDevices?: DetectedRoom[];
ready: boolean;
}

Expand Down Expand Up @@ -130,7 +131,6 @@ class App extends GenericApp<GenericAppProps, AppState> {
devices: {},
},
ready: false,
detectedDevices: null,
});

this.alert = window.alert;
Expand Down Expand Up @@ -349,8 +349,8 @@ class App extends GenericApp<GenericAppProps, AppState> {
nodeStates={this.state.nodeStates}
themeType={this.state.themeType}
theme={this.state.theme}
detectedDevices={this.state.detectedDevices || []}
setDetectedDevices={(detectedDevices: DetectedRoom[]) =>
detectedDevices={this.state.detectedDevices}
setDetectedDevices={detectedDevices =>
this.setState({ detectedDevices })}
productIDs={productIDs}
matter={this.state.matter}
Expand Down Expand Up @@ -381,8 +381,8 @@ class App extends GenericApp<GenericAppProps, AppState> {
socket={this.socket}
themeType={this.state.themeType}
theme={this.state.theme}
detectedDevices={this.state.detectedDevices || []}
setDetectedDevices={(detectedDevices: DetectedRoom[]) =>
detectedDevices={this.state.detectedDevices}
setDetectedDevices={detectedDevices =>
this.setState({ detectedDevices })}
productIDs={productIDs}
instance={this.instance}
Expand Down
3 changes: 2 additions & 1 deletion src-admin/src/Tabs/BridgesAndDevices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export const STYLES: Record<string, React.CSSProperties> = {
export interface BridgesAndDevicesProps {
alive: boolean;
commissioning: Record<string, boolean>;
detectedDevices: DetectedRoom[];
/** Undefined if no detection ran yet */
detectedDevices?: DetectedRoom[];
instance: number;
matter: MatterConfig;
nodeStates: { [uuid: string]: NodeStateResponse };
Expand Down
2 changes: 1 addition & 1 deletion src-admin/src/Tabs/Devices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ class Devices extends BridgesAndDevices<DevicesProps, DevicesState> {
matter={this.props.matter}
socket={this.props.socket}
detectedDevices={this.props.detectedDevices}
setDetectedDevices={(detectedDevices: DetectedRoom[]) =>
setDetectedDevices={detectedDevices =>
this.props.setDetectedDevices(detectedDevices)}
type="device"
/>
Expand Down
2 changes: 1 addition & 1 deletion src-admin/src/Utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export async function detectDevices(
}
const members: string[] = devicesObject[id].common.members;

if (members && members.length) {
if (members?.length) {
members.forEach(member => {
// if an object really exists
if (devicesObject[member]) {
Expand Down
34 changes: 13 additions & 21 deletions src-admin/src/components/DeviceDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import {
} from '@iobroker/adapter-react-v5';
import { Types } from '@iobroker/type-detector';

import { detectDevices, getText } from '../Utils';
import { clone, detectDevices, getText } from '../Utils';
import type { DetectedRoom, DetectedDevice, MatterConfig } from '../types';

export const DEVICE_ICONS: Record<Types, React.JSX.Element> = {
Expand Down Expand Up @@ -172,7 +172,8 @@ const styles: Record<string, any> = {
};

interface DeviceDialogProps {
detectedDevices: DetectedRoom[];
/** Undefined if no detection ran yet */
detectedDevices?: DetectedRoom[];
matter: MatterConfig;
socket: AdminConnection;
setDetectedDevices: (detectedDevices: DetectedRoom[]) => void;
Expand Down Expand Up @@ -225,11 +226,12 @@ class DeviceDialog extends Component<DeviceDialogProps, DeviceDialogState> {

async componentDidMount() {
const detectedDevices = this.props.detectedDevices || (await detectDevices(this.props.socket));

if (!this.props.detectedDevices) {
setTimeout(() => this.props.setDetectedDevices(detectedDevices), 100);
}

let rooms: DetectedRoom[] = JSON.parse(JSON.stringify(detectedDevices));
let rooms = clone(detectedDevices);

// ignore buttons
rooms.forEach(
Expand Down Expand Up @@ -341,9 +343,7 @@ class DeviceDialog extends Component<DeviceDialogProps, DeviceDialogState> {
checked={!!this.state.devicesChecked[device._id]}
disabled={!supported}
onChange={e => {
const devicesChecked = JSON.parse(
JSON.stringify(this.state.devicesChecked),
);
const devicesChecked = clone(this.state.devicesChecked);
devicesChecked[device._id] = e.target.checked;
this.setState({ devicesChecked });
}}
Expand All @@ -364,7 +364,7 @@ class DeviceDialog extends Component<DeviceDialogProps, DeviceDialogState> {
}
value={device.common.name}
onChange={e => {
const rooms = JSON.parse(JSON.stringify(this.state.rooms));
const rooms = clone(this.state.rooms);
rooms[roomIndex].devices[deviceIndex].common.name =
e.target.value;
this.setState({ rooms });
Expand All @@ -376,7 +376,7 @@ class DeviceDialog extends Component<DeviceDialogProps, DeviceDialogState> {
style={{ minWidth: 'initial' }}
value={device.vendorID}
onChange={e => {
const rooms = JSON.parse(JSON.stringify(this.state.rooms));
const rooms = clone(this.state.rooms);
rooms[roomIndex].devices[deviceIndex].vendorID = e.target.value;
this.setState({ rooms });
}}
Expand All @@ -394,7 +394,7 @@ class DeviceDialog extends Component<DeviceDialogProps, DeviceDialogState> {
style={{ minWidth: 'initial' }}
value={device.productID}
onChange={e => {
const rooms = JSON.parse(JSON.stringify(this.state.rooms));
const rooms = clone(this.state.rooms);
rooms[roomIndex].devices[deviceIndex].productID = e.target.value;
this.setState({ rooms });
}}
Expand Down Expand Up @@ -446,9 +446,7 @@ class DeviceDialog extends Component<DeviceDialogProps, DeviceDialogState> {
<Switch
checked={this.state.ignoreUsedDevices || false}
onChange={e => {
const devicesChecked = JSON.parse(
JSON.stringify(this.state.devicesChecked),
);
const devicesChecked = clone(this.state.devicesChecked);
Object.keys(devicesChecked).forEach(deviceId => {
if (e.target.checked && this.state.usedDevices[deviceId]) {
devicesChecked[deviceId] = false;
Expand Down Expand Up @@ -480,9 +478,7 @@ class DeviceDialog extends Component<DeviceDialogProps, DeviceDialogState> {
e.target.checked ? 'true' : 'false',
);

const rooms: DetectedRoom[] = JSON.parse(
JSON.stringify(this.state.rooms),
);
const rooms = clone(this.state.rooms);
if (e.target.checked) {
rooms.forEach(room => {
room.devices.forEach(device => {
Expand Down Expand Up @@ -551,9 +547,7 @@ class DeviceDialog extends Component<DeviceDialogProps, DeviceDialogState> {
<Accordion
expanded={this.state.expanded.includes(room._id)}
onChange={() => {
const expanded: string[] = JSON.parse(
JSON.stringify(this.state.expanded),
);
const expanded = clone(this.state.expanded);
const pos = expanded.indexOf(room._id);
if (pos === -1) {
expanded.push(room._id);
Expand Down Expand Up @@ -594,9 +588,7 @@ class DeviceDialog extends Component<DeviceDialogProps, DeviceDialogState> {
onClick={e => {
e.stopPropagation();
e.preventDefault();
const devicesChecked = JSON.parse(
JSON.stringify(this.state.devicesChecked),
);
const devicesChecked = clone(this.state.devicesChecked);
if (
counters[roomIndex] === room.devices.length
) {
Expand Down
1 change: 1 addition & 0 deletions src-admin/src/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"Enable": "Aktivieren",
"Enable/disable bridge": "Bridge aktivieren/deaktivieren",
"Expand all": "Alle erweitern",
"Generate new pairing code": "Neuen Pairing-Code generieren",
"Hide unsupported devices": "Nicht unterstützte Geräte ausblenden",
"Interface": "Schnittstelle",
"Manual pairing code": "Manueller Kopplungscode",
Expand Down
1 change: 1 addition & 0 deletions src-admin/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"Enable": "Enable",
"Enable/disable bridge": "Enable/disable bridge",
"Expand all": "Expand all",
"Generate new pairing code": "Generate new pairing code",
"Hide unsupported devices": "Hide unsupported devices",
"Interface": "Interface",
"Manual pairing code": "Manual pairing code",
Expand Down
1 change: 1 addition & 0 deletions src-admin/src/i18n/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"Enable": "Permitir",
"Enable/disable bridge": "Activar/desactivar puente",
"Expand all": "Expandir todo",
"Generate new pairing code": "Generar nuevo código de emparejamiento",
"Hide unsupported devices": "Ocultar dispositivos no compatibles",
"Interface": "Interfaz",
"Manual pairing code": "Código de emparejamiento manual",
Expand Down
1 change: 1 addition & 0 deletions src-admin/src/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"Enable": "Activer",
"Enable/disable bridge": "Activer/désactiver le pont",
"Expand all": "Développer tout",
"Generate new pairing code": "Générer un nouveau code d'appairage",
"Hide unsupported devices": "Masquer les appareils non pris en charge",
"Interface": "Interface",
"Manual pairing code": "Code d'appairage manuel",
Expand Down
1 change: 1 addition & 0 deletions src-admin/src/i18n/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"Enable": "Abilitare",
"Enable/disable bridge": "Abilita/disabilita il bridge",
"Expand all": "Espandi tutto",
"Generate new pairing code": "Genera un nuovo codice di accoppiamento",
"Hide unsupported devices": "Nascondi i dispositivi non supportati",
"Interface": "Interfaccia",
"Manual pairing code": "Codice di abbinamento manuale",
Expand Down
1 change: 1 addition & 0 deletions src-admin/src/i18n/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"Enable": "Inschakelen",
"Enable/disable bridge": "Brug in-/uitschakelen",
"Expand all": "Alles uitvouwen",
"Generate new pairing code": "Genereer een nieuwe koppelingscode",
"Hide unsupported devices": "Verberg niet-ondersteunde apparaten",
"Interface": "Koppel",
"Manual pairing code": "Handmatige koppelingscode",
Expand Down
1 change: 1 addition & 0 deletions src-admin/src/i18n/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"Enable": "Włączać",
"Enable/disable bridge": "Włącz/wyłącz most",
"Expand all": "Rozwiń wszystkie",
"Generate new pairing code": "Wygeneruj nowy kod parowania",
"Hide unsupported devices": "Ukryj nieobsługiwane urządzenia",
"Interface": "Interfejs",
"Manual pairing code": "Ręczny kod parowania",
Expand Down
1 change: 1 addition & 0 deletions src-admin/src/i18n/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"Enable": "Habilitar",
"Enable/disable bridge": "Habilitar/desabilitar ponte",
"Expand all": "Expandir todos",
"Generate new pairing code": "Gerar novo código de pareamento",
"Hide unsupported devices": "Ocultar dispositivos não suportados",
"Interface": "Interface",
"Manual pairing code": "Código de emparelhamento manual",
Expand Down
1 change: 1 addition & 0 deletions src-admin/src/i18n/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"Enable": "Давать возможность",
"Enable/disable bridge": "Включить/выключить мост",
"Expand all": "Расширить все",
"Generate new pairing code": "Создать новый код сопряжения",
"Hide unsupported devices": "Скрыть неподдерживаемые устройства",
"Interface": "Интерфейс",
"Manual pairing code": "Код сопряжения вручную",
Expand Down
1 change: 1 addition & 0 deletions src-admin/src/i18n/uk.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"Enable": "Увімкнути",
"Enable/disable bridge": "Увімкнути/вимкнути міст",
"Expand all": "Розгорнути все",
"Generate new pairing code": "Створіть новий код сполучення",
"Hide unsupported devices": "Приховати непідтримувані пристрої",
"Interface": "Інтерфейс",
"Manual pairing code": "Код підключення вручну",
Expand Down
1 change: 1 addition & 0 deletions src-admin/src/i18n/zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"Enable": "使能够",
"Enable/disable bridge": "启用/禁用桥接",
"Expand all": "展开全部",
"Generate new pairing code": "生成新的配对码",
"Hide unsupported devices": "隐藏不支持的设备",
"Interface": "界面",
"Manual pairing code": "手动配对码",
Expand Down
14 changes: 14 additions & 0 deletions src/lib/DeviceManagement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ class MatterAdapterDeviceManagement extends DeviceManagement<MatterAdapter> {
icon: 'fa-solid fa-pen',
description: t('Rename this device'),
handler: this.handleRenameDevice.bind(this)
},
{
id: 'pairingCode',
icon: 'fa-solid fa-qrcode',
description: t('Generate new pairing code'),
handler: this.handlePairingCode.bind(this)
}
]
};
Expand Down Expand Up @@ -136,6 +142,14 @@ class MatterAdapterDeviceManagement extends DeviceManagement<MatterAdapter> {
return { error: { code: 404, message: 'State not found' } };
}

/**
* Handle new pairing code request
*/
handlePairingCode(): Promise<{ refresh: DeviceRefresh }> {
// TODO: return form as soon as QR code implemented
return Promise.resolve({ refresh: false });
}

async handleRenameDevice(id: string, context: ActionContext): Promise<{ refresh: DeviceRefresh }> {
this.adapter.log.info(`Rename device ${id}`);
const devices = await this.adapter.getDevicesAsync();
Expand Down

0 comments on commit 6782131

Please sign in to comment.