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