1+ using System . Text . Json . Serialization ;
2+
3+ namespace Spoke . Data . Models ;
4+
5+ /// <summary>
6+ /// Configuration for Home Assistant integration
7+ /// </summary>
8+ public class HomeAssistantConfig
9+ {
10+ /// <summary>
11+ /// Base URL of the Home Assistant instance
12+ /// </summary>
13+ public string BaseUrl { get ; set ; } = string . Empty ;
14+
15+ /// <summary>
16+ /// Encrypted long-lived access token
17+ /// </summary>
18+ public byte [ ] EncryptedAccessToken { get ; set ; } = Array . Empty < byte > ( ) ;
19+
20+ /// <summary>
21+ /// WebSocket URL for real-time updates (optional, derived from BaseUrl if not set)
22+ /// </summary>
23+ public string ? WebSocketUrl { get ; set ; }
24+
25+ /// <summary>
26+ /// Home Assistant version
27+ /// </summary>
28+ public string ? Version { get ; set ; }
29+
30+ /// <summary>
31+ /// List of supported features
32+ /// </summary>
33+ public List < string > Features { get ; set ; } = new ( ) ;
34+
35+ /// <summary>
36+ /// Validate that the configuration is valid
37+ /// </summary>
38+ public bool IsValid ( )
39+ {
40+ if ( string . IsNullOrWhiteSpace ( BaseUrl ) )
41+ return false ;
42+
43+ if ( ! Uri . TryCreate ( BaseUrl , UriKind . Absolute , out var baseUri ) )
44+ return false ;
45+
46+ if ( baseUri . Scheme != Uri . UriSchemeHttp && baseUri . Scheme != Uri . UriSchemeHttps )
47+ return false ;
48+
49+ if ( EncryptedAccessToken . Length == 0 )
50+ return false ;
51+
52+ return true ;
53+ }
54+
55+ /// <summary>
56+ /// Get the WebSocket URL, deriving from BaseUrl if not explicitly set
57+ /// </summary>
58+ public string GetWebSocketUrl ( )
59+ {
60+ if ( ! string . IsNullOrWhiteSpace ( WebSocketUrl ) )
61+ return WebSocketUrl ;
62+
63+ if ( Uri . TryCreate ( BaseUrl , UriKind . Absolute , out var baseUri ) )
64+ {
65+ var wsScheme = baseUri . Scheme == Uri . UriSchemeHttps ? "wss" : "ws" ;
66+ return $ "{ wsScheme } ://{ baseUri . Host } :{ baseUri . Port } /api/websocket";
67+ }
68+
69+ return string . Empty ;
70+ }
71+
72+ /// <summary>
73+ /// Check if the Home Assistant version is compatible
74+ /// </summary>
75+ public bool IsVersionCompatible ( )
76+ {
77+ if ( string . IsNullOrWhiteSpace ( Version ) )
78+ return false ;
79+
80+ // TODO: Implement version compatibility check
81+ // For now, assume versions starting with 2023+ are compatible
82+ return Version . StartsWith ( "2023" ) || Version . StartsWith ( "2024" ) || Version . StartsWith ( "2025" ) ;
83+ }
84+
85+ /// <summary>
86+ /// Check if a specific feature is supported
87+ /// </summary>
88+ public bool SupportsFeature ( string feature ) =>
89+ Features . Contains ( feature , StringComparer . OrdinalIgnoreCase ) ;
90+
91+ public override string ToString ( ) => $ "{ BaseUrl } (v{ Version ?? "unknown" } )";
92+ }
0 commit comments