WIP #523 Change device name for integration

This commit is contained in:
Yegor Vialov 2020-05-01 22:39:32 +00:00
parent f28e5493dc
commit 163338ea75
3 changed files with 28 additions and 23 deletions

View File

@ -19,6 +19,7 @@ class ConnectionManager {
String _tempToken; String _tempToken;
String oauthUrl; String oauthUrl;
String webhookId; String webhookId;
String mobileAppDeviceName;
bool settingsLoaded = false; bool settingsLoaded = false;
int appIntegrationVersion; int appIntegrationVersion;
bool get isAuthenticated => _token != null; bool get isAuthenticated => _token != null;
@ -45,6 +46,7 @@ class ConnectionManager {
_port = prefs.getString('hassio-port'); _port = prefs.getString('hassio-port');
webhookId = prefs.getString('app-webhook-id'); webhookId = prefs.getString('app-webhook-id');
appIntegrationVersion = prefs.getInt('app-integration-version') ?? 0; appIntegrationVersion = prefs.getInt('app-integration-version') ?? 0;
mobileAppDeviceName = prefs.getString('app-integration-device-name');
displayHostname = "$_domain:$_port"; displayHostname = "$_domain:$_port";
_webSocketAPIEndpoint = _webSocketAPIEndpoint =
"${prefs.getString('hassio-protocol')}://$_domain:$_port/api/websocket"; "${prefs.getString('hassio-protocol')}://$_domain:$_port/api/websocket";

View File

@ -16,9 +16,13 @@ class MobileAppIntegrationManager {
} }
}; };
static String getDefaultDeviceName() {
return '${HomeAssistant().userName}\'s ${DeviceInfoManager().model}';
}
static Future checkAppRegistration({bool forceRegister: false, bool showOkDialog: false}) { static Future checkAppRegistration({bool forceRegister: false, bool showOkDialog: false}) {
Completer completer = Completer(); Completer completer = Completer();
_appRegistrationData["device_name"] = "${HomeAssistant().userName}'s ${DeviceInfoManager().model}"; _appRegistrationData["device_name"] = ConnectionManager().mobileAppDeviceName ?? getDefaultDeviceName();
(_appRegistrationData["app_data"] as Map)["push_token"] = "${HomeAssistant().fcmToken}"; (_appRegistrationData["app_data"] as Map)["push_token"] = "${HomeAssistant().fcmToken}";
if (ConnectionManager().webhookId == null || forceRegister) { if (ConnectionManager().webhookId == null || forceRegister) {
Logger.d("Mobile app was not registered yet or need to be reseted. Registering..."); Logger.d("Mobile app was not registered yet or need to be reseted. Registering...");

View File

@ -14,6 +14,9 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
int _locationInterval = LocationManager().defaultUpdateIntervalMinutes; int _locationInterval = LocationManager().defaultUpdateIntervalMinutes;
bool _locationTrackingEnabled = false; bool _locationTrackingEnabled = false;
bool _wait = false; bool _wait = false;
String _deviceName = '';
bool _applyNameEnabled = false;
String _newDeviceName = '';
@override @override
void initState() { void initState() {
@ -27,6 +30,7 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
await prefs.reload(); await prefs.reload();
SharedPreferences.getInstance().then((prefs) { SharedPreferences.getInstance().then((prefs) {
setState(() { setState(() {
_deviceName = _newDeviceName = ConnectionManager().mobileAppDeviceName ?? MobileAppIntegrationManager.getDefaultDeviceName();
_locationTrackingEnabled = prefs.getBool("location-enabled") ?? false; _locationTrackingEnabled = prefs.getBool("location-enabled") ?? false;
_locationInterval = prefs.getInt("location-interval") ?? LocationManager().defaultUpdateIntervalMinutes; _locationInterval = prefs.getInt("location-interval") ?? LocationManager().defaultUpdateIntervalMinutes;
if (_locationInterval % 5 != 0) { if (_locationInterval % 5 != 0) {
@ -157,30 +161,25 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
], ],
), ),
Divider(), Divider(),
Text("Integration status", style: Theme.of(context).textTheme.title), Text("Device name", style: Theme.of(context).textTheme.title),
Container(height: Sizes.rowPadding,), Container(height: Sizes.rowPadding,),
Text( TextField(
"${HomeAssistant().userName}'s ${DeviceInfoManager().model}, ${DeviceInfoManager().osName} ${DeviceInfoManager().osVersion}", /*decoration: InputDecoration(
style: Theme.of(context).textTheme.subtitle, labelText: "Long-lived token"
),*/
controller: TextEditingController.fromValue(TextEditingValue(text: _newDeviceName)),
onChanged: (value) {
setState(() {
_newDeviceName = value;
_applyNameEnabled = _newDeviceName != _deviceName;
});
}
), ),
Container(height: 6.0,), Container(height: 6.0,),
Text("Here you can manually check if HA Client integration with your Home Assistant works fine. As mobileApp integration in Home Assistant is still in development, this is not 100% correct check."),
//Divider(),
Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
RaisedButton( RaisedButton(
color: Colors.blue, color: Colors.blue,
onPressed: () => updateRegistration(), onPressed: () => _applyNameEnabled ? updateRegistration() : null,
child: Text("Check integration", style: Theme.of(context).textTheme.button) child: Text("Update device name", style: Theme.of(context).textTheme.button)
),
Container(width: 10.0,),
RaisedButton(
color: Colors.redAccent,
onPressed: () => resetRegistration(),
child: Text("Reset integration", style: Theme.of(context).textTheme.button)
)
],
), ),
] ]
); );