[#38] Add access token support
This commit is contained in:
parent
ea855b96dd
commit
8eac41b633
@ -15,6 +15,7 @@ class SettingsChangedEvent {
|
|||||||
class HassioDataModel {
|
class HassioDataModel {
|
||||||
String _hassioAPIEndpoint;
|
String _hassioAPIEndpoint;
|
||||||
String _hassioPassword;
|
String _hassioPassword;
|
||||||
|
String _hassioAuthType;
|
||||||
IOWebSocketChannel _hassioChannel;
|
IOWebSocketChannel _hassioChannel;
|
||||||
int _currentMssageId = 0;
|
int _currentMssageId = 0;
|
||||||
int _statesMessageId = 0;
|
int _statesMessageId = 0;
|
||||||
@ -36,9 +37,10 @@ class HassioDataModel {
|
|||||||
Map get uiStructure => _uiStructure;
|
Map get uiStructure => _uiStructure;
|
||||||
Map get instanceConfig => _instanceConfig;
|
Map get instanceConfig => _instanceConfig;
|
||||||
|
|
||||||
HassioDataModel(String url, String password) {
|
HassioDataModel(String url, String password, String authType) {
|
||||||
_hassioAPIEndpoint = url;
|
_hassioAPIEndpoint = url;
|
||||||
_hassioPassword = password;
|
_hassioPassword = password;
|
||||||
|
_hassioAuthType = authType;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future fetch() {
|
Future fetch() {
|
||||||
@ -112,7 +114,7 @@ class HassioDataModel {
|
|||||||
var data = json.decode(message);
|
var data = json.decode(message);
|
||||||
debugPrint("[Received]Message type: ${data['type']}");
|
debugPrint("[Received]Message type: ${data['type']}");
|
||||||
if (data["type"] == "auth_required") {
|
if (data["type"] == "auth_required") {
|
||||||
_sendMessageRaw('{"type": "auth","api_password": "$_hassioPassword"}');
|
_sendMessageRaw('{"type": "auth","$_hassioAuthType": "$_hassioPassword"}');
|
||||||
} else if (data["type"] == "auth_ok") {
|
} else if (data["type"] == "auth_ok") {
|
||||||
_sendSubscribe();
|
_sendSubscribe();
|
||||||
connectionCompleter.complete();
|
connectionCompleter.complete();
|
||||||
|
@ -54,6 +54,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
int _errorCodeToBeShown = 0;
|
int _errorCodeToBeShown = 0;
|
||||||
String _lastErrorMessage = "";
|
String _lastErrorMessage = "";
|
||||||
StreamSubscription _stateSubscription;
|
StreamSubscription _stateSubscription;
|
||||||
|
StreamSubscription _settingsSubscription;
|
||||||
bool _isLoading = true;
|
bool _isLoading = true;
|
||||||
Map _stateIconColors = {
|
Map _stateIconColors = {
|
||||||
"on": Colors.amber,
|
"on": Colors.amber,
|
||||||
@ -67,7 +68,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
WidgetsBinding.instance.addObserver(this);
|
WidgetsBinding.instance.addObserver(this);
|
||||||
eventBus.on<SettingsChangedEvent>().listen((event) {
|
_settingsSubscription = eventBus.on<SettingsChangedEvent>().listen((event) {
|
||||||
debugPrint("Settings change event: reconnect=${event.reconnect}");
|
debugPrint("Settings change event: reconnect=${event.reconnect}");
|
||||||
setState(() {
|
setState(() {
|
||||||
_errorCodeToBeShown = 0;
|
_errorCodeToBeShown = 0;
|
||||||
@ -92,19 +93,20 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
_instanceHost = "$domain:$port";
|
_instanceHost = "$domain:$port";
|
||||||
String apiEndpoint = "${prefs.getString('hassio-protocol')}://$domain:$port/api/websocket";
|
String apiEndpoint = "${prefs.getString('hassio-protocol')}://$domain:$port/api/websocket";
|
||||||
String apiPassword = prefs.getString('hassio-password');
|
String apiPassword = prefs.getString('hassio-password');
|
||||||
if ((domain == null) || (port == null) || (apiEndpoint == null) || (apiPassword == null) ||
|
String authType = prefs.getString('hassio-auth-type');
|
||||||
(domain.length == 0) || (port.length == 0) || (apiEndpoint.length == 0) || (apiPassword.length == 0)) {
|
if ((domain == null) || (port == null) || (apiPassword == null) ||
|
||||||
|
(domain.length == 0) || (port.length == 0) || (apiPassword.length == 0)) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_errorCodeToBeShown = 5;
|
_errorCodeToBeShown = 5;
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
if (_dataModel != null) _dataModel.closeConnection();
|
if (_dataModel != null) _dataModel.closeConnection();
|
||||||
_createConnection(apiEndpoint, apiPassword);
|
_createConnection(apiEndpoint, apiPassword, authType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_createConnection(String apiEndpoint, String apiPassword) {
|
_createConnection(String apiEndpoint, String apiPassword, String authType) {
|
||||||
_dataModel = HassioDataModel(apiEndpoint, apiPassword);
|
_dataModel = HassioDataModel(apiEndpoint, apiPassword, authType);
|
||||||
_refreshData();
|
_refreshData();
|
||||||
if (_stateSubscription != null) _stateSubscription.cancel();
|
if (_stateSubscription != null) _stateSubscription.cancel();
|
||||||
_stateSubscription = eventBus.on<StateChangedEvent>().listen((event) {
|
_stateSubscription = eventBus.on<StateChangedEvent>().listen((event) {
|
||||||
@ -406,6 +408,8 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
_scaffoldKey?.currentState?.hideCurrentSnackBar();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -479,6 +483,8 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
WidgetsBinding.instance.removeObserver(this);
|
WidgetsBinding.instance.removeObserver(this);
|
||||||
|
if (_stateSubscription != null) _stateSubscription.cancel();
|
||||||
|
if (_settingsSubscription != null) _settingsSubscription.cancel();
|
||||||
_dataModel.closeConnection();
|
_dataModel.closeConnection();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
|
|||||||
String _hassioPort = "8123";
|
String _hassioPort = "8123";
|
||||||
String _hassioPassword = "";
|
String _hassioPassword = "";
|
||||||
String _socketProtocol = "wss";
|
String _socketProtocol = "wss";
|
||||||
|
String _authType = "access_token";
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@ -29,6 +30,7 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
|
|||||||
_hassioPort = prefs.getString("hassio-port") ?? '8123';
|
_hassioPort = prefs.getString("hassio-port") ?? '8123';
|
||||||
_hassioPassword = prefs.getString("hassio-password");
|
_hassioPassword = prefs.getString("hassio-password");
|
||||||
_socketProtocol = prefs.getString("hassio-protocol") ?? 'wss';
|
_socketProtocol = prefs.getString("hassio-protocol") ?? 'wss';
|
||||||
|
_authType = prefs.getString("hassio-auth-type") ?? 'access_token';
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,6 +40,7 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
|
|||||||
prefs.setString("hassio-port", _hassioPort);
|
prefs.setString("hassio-port", _hassioPort);
|
||||||
prefs.setString("hassio-password", _hassioPassword);
|
prefs.setString("hassio-password", _hassioPassword);
|
||||||
prefs.setString("hassio-protocol", _socketProtocol);
|
prefs.setString("hassio-protocol", _socketProtocol);
|
||||||
|
prefs.setString("hassio-auth-type", _authType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -95,9 +98,23 @@ class _ConnectionSettingsPageState extends State<ConnectionSettingsPage> {
|
|||||||
_saveSettings();
|
_saveSettings();
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
new Row(
|
||||||
|
children: [
|
||||||
|
Text("Login with access token (HA >= 0.78.0)"),
|
||||||
|
Switch(
|
||||||
|
value: (_authType == "access_token"),
|
||||||
|
onChanged: (value) {
|
||||||
|
setState(() {
|
||||||
|
_authType = value ? "access_token" : "api_password";
|
||||||
|
});
|
||||||
|
_saveSettings();
|
||||||
|
},
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
new TextField(
|
new TextField(
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
labelText: "Home Assistant password"
|
labelText: _authType == "access_token" ? "Access token" : "API password"
|
||||||
),
|
),
|
||||||
controller: TextEditingController(
|
controller: TextEditingController(
|
||||||
text: _hassioPassword
|
text: _hassioPassword
|
||||||
|
Reference in New Issue
Block a user