diff --git a/frontend/src/components/AddSmartPlugModal.tsx b/frontend/src/components/AddSmartPlugModal.tsx index 41130b981..2ec046070 100644 --- a/frontend/src/components/AddSmartPlugModal.tsx +++ b/frontend/src/components/AddSmartPlugModal.tsx @@ -60,12 +60,22 @@ export function AddSmartPlugModal({ plug, onClose }: AddSmartPlugModalProps) { queryFn: api.getSmartPlugs, }); - // Fetch Home Assistant entities when in HA mode - const { data: haEntities, isLoading: haEntitiesLoading, error: haEntitiesError } = useQuery({ + // Fetch settings to check if HA is configured + const { data: settings } = useQuery({ + queryKey: ['settings'], + queryFn: api.getSettings, + }); + + // Check if HA is properly configured + const haConfigured = !!(settings?.ha_enabled && settings?.ha_url && settings?.ha_token); + + // Fetch Home Assistant entities when in HA mode AND HA is configured + const { data: haEntities, isLoading: haEntitiesLoading } = useQuery({ queryKey: ['ha-entities'], queryFn: api.getHAEntities, - enabled: plugType === 'homeassistant', + enabled: plugType === 'homeassistant' && haConfigured, retry: false, + staleTime: 0, }); // Close on Escape key and cleanup scan polling @@ -383,74 +393,92 @@ export function AddSmartPlugModal({ plug, onClose }: AddSmartPlugModalProps) { {/* Home Assistant Entity Selector - only show when HA is selected */} {plugType === 'homeassistant' && (
- {haEntitiesLoading && ( -
- - Loading entities... -
- )} - - {haEntitiesError && ( -
- {haEntitiesError instanceof Error ? haEntitiesError.message : 'Failed to load Home Assistant entities. Check Settings → Network → Home Assistant.'} -
- )} - - {haEntities && haEntities.length === 0 && ( -
- No switch/light entities found in Home Assistant -
- )} - - {haEntities && haEntities.length > 0 && (() => { - // Filter out entities already configured (except current plug when editing) - const configuredEntityIds = existingPlugs - ?.filter(p => p.ha_entity_id && p.id !== plug?.id) - .map(p => p.ha_entity_id) || []; - const availableEntities = haEntities.filter(e => !configuredEntityIds.includes(e.entity_id)); - - return ( + {/* HA not configured */} + {!haConfigured && ( +
+
+ Home Assistant is not configured. Set it up in{' '} + Settings → Network → Home Assistant +
- + - {configuredEntityIds.length > 0 && ( -

- {configuredEntityIds.length} entity(s) already configured -

- )} -
- ); - })()} - - {haEntityId && haEntities && ( -
- -
-

Entity selected

-

- {haEntities.find(e => e.entity_id === haEntityId)?.friendly_name} - {haEntities.find(e => e.entity_id === haEntityId)?.state} -

)} + + {/* HA configured - show loading/entities */} + {haConfigured && ( + <> + {haEntitiesLoading && ( +
+ + Loading entities... +
+ )} + + {haEntities && haEntities.length === 0 && ( +
+ No switch/light entities found in Home Assistant +
+ )} + + {haEntities && haEntities.length > 0 && (() => { + // Filter out entities already configured (except current plug when editing) + const configuredEntityIds = existingPlugs + ?.filter(p => p.ha_entity_id && p.id !== plug?.id) + .map(p => p.ha_entity_id) || []; + const availableEntities = haEntities.filter(e => !configuredEntityIds.includes(e.entity_id)); + + return ( +
+ + + {configuredEntityIds.length > 0 && ( +

+ {configuredEntityIds.length} entity(s) already configured +

+ )} +
+ ); + })()} + + {haEntityId && haEntities && ( +
+ +
+

Entity selected

+

+ {haEntities.find(e => e.entity_id === haEntityId)?.friendly_name} - {haEntities.find(e => e.entity_id === haEntityId)?.state} +

+
+
+ )} + + )}
)}