Compare commits

..

8 Commits

28 changed files with 343 additions and 225 deletions

View File

@ -3,18 +3,13 @@
<uses-feature android:name="android.hardware.touchscreen"
android:required="false" />
<!-- The INTERNET permission is required for development. Specifically,
flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
@ -24,7 +19,8 @@
android:name=".Application"
android:label="HA Client"
android:icon="@mipmap/ic_launcher"
android:usesCleartextTraffic="true">
android:usesCleartextTraffic="true"
android:networkSecurityConfig="@xml/network_security_config">
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config>
<trust-anchors>
<certificates src="system"/>
<certificates src="user"/>
</trust-anchors>
</base-config>
</network-security-config>

View File

@ -26,10 +26,10 @@ class _AlarmControlPanelControlsWidgetWidgetState extends State<AlarmControlPane
void _callService(AlarmControlPanelEntity entity, String service) {
ConnectionManager().callService(
entity.domain,
service,
entity.entityId,
{"code": "$code"}
domain: entity.domain,
service: service,
entityId: entity.entityId,
data: {"code": "$code"}
);
setState(() {
code = "";
@ -62,10 +62,9 @@ class _AlarmControlPanelControlsWidgetWidgetState extends State<AlarmControlPane
child: new Text("Yes"),
onPressed: () {
ConnectionManager().callService(
entity.domain,
"alarm_trigger",
entity.entityId,
null
domain: entity.domain,
service: "alarm_trigger",
entityId: entity.entityId
);
Navigator.of(context).pop();
},

View File

@ -84,10 +84,10 @@ class _ClimateControlWidgetState extends State<ClimateControlWidget> {
setState(() {
_changedHere = true;
ConnectionManager().callService(
entity.domain,
"set_temperature",
entity.entityId,
{"temperature": "${_tmpTemperature.toStringAsFixed(1)}"}
domain: entity.domain,
service: "set_temperature",
entityId: entity.entityId,
data: {"temperature": "${_tmpTemperature.toStringAsFixed(1)}"}
);
_resetStateTimer(entity);
});
@ -107,10 +107,10 @@ class _ClimateControlWidgetState extends State<ClimateControlWidget> {
setState(() {
_changedHere = true;
ConnectionManager().callService(
entity.domain,
"set_temperature",
entity.entityId,
{"target_temp_high": "${_tmpTargetHigh.toStringAsFixed(1)}", "target_temp_low": "${_tmpTargetLow.toStringAsFixed(1)}"}
domain: entity.domain,
service: "set_temperature",
entityId: entity.entityId,
data: {"target_temp_high": "${_tmpTargetHigh.toStringAsFixed(1)}", "target_temp_low": "${_tmpTargetLow.toStringAsFixed(1)}"}
);
_resetStateTimer(entity);
});
@ -121,7 +121,12 @@ class _ClimateControlWidgetState extends State<ClimateControlWidget> {
setState(() {
_tmpTargetHumidity = value.roundToDouble();
_changedHere = true;
ConnectionManager().callService(entity.domain, "set_humidity", entity.entityId, {"humidity": "$_tmpTargetHumidity"});
ConnectionManager().callService(
domain: entity.domain,
service: "set_humidity",
entityId: entity.entityId,
data: {"humidity": "$_tmpTargetHumidity"}
);
_resetStateTimer(entity);
});
}
@ -130,7 +135,12 @@ class _ClimateControlWidgetState extends State<ClimateControlWidget> {
setState(() {
_tmpHVACMode = value;
_changedHere = true;
ConnectionManager().callService(entity.domain, "set_hvac_mode", entity.entityId, {"hvac_mode": "$_tmpHVACMode"});
ConnectionManager().callService(
domain: entity.domain,
service: "set_hvac_mode",
entityId: entity.entityId,
data: {"hvac_mode": "$_tmpHVACMode"}
);
_resetStateTimer(entity);
});
}
@ -139,7 +149,12 @@ class _ClimateControlWidgetState extends State<ClimateControlWidget> {
setState(() {
_tmpSwingMode = value;
_changedHere = true;
ConnectionManager().callService(entity.domain, "set_swing_mode", entity.entityId, {"swing_mode": "$_tmpSwingMode"});
ConnectionManager().callService(
domain: entity.domain,
service: "set_swing_mode",
entityId: entity.entityId,
data: {"swing_mode": "$_tmpSwingMode"}
);
_resetStateTimer(entity);
});
}
@ -148,7 +163,7 @@ class _ClimateControlWidgetState extends State<ClimateControlWidget> {
setState(() {
_tmpFanMode = value;
_changedHere = true;
ConnectionManager().callService(entity.domain, "set_fan_mode", entity.entityId, {"fan_mode": "$_tmpFanMode"});
ConnectionManager().callService(domain: entity.domain, service: "set_fan_mode", entityId: entity.entityId, data: {"fan_mode": "$_tmpFanMode"});
_resetStateTimer(entity);
});
}
@ -157,7 +172,7 @@ class _ClimateControlWidgetState extends State<ClimateControlWidget> {
setState(() {
_tmpPresetMode = value;
_changedHere = true;
ConnectionManager().callService(entity.domain, "set_preset_mode", entity.entityId, {"preset_mode": "$_tmpPresetMode"});
ConnectionManager().callService(domain: entity.domain, service: "set_preset_mode", entityId: entity.entityId, data: {"preset_mode": "$_tmpPresetMode"});
_resetStateTimer(entity);
});
}
@ -175,7 +190,7 @@ class _ClimateControlWidgetState extends State<ClimateControlWidget> {
setState(() {
_tmpAuxHeat = value;
_changedHere = true;
ConnectionManager().callService(entity.domain, "set_aux_heat", entity.entityId, {"aux_heat": "$_tmpAuxHeat"});
ConnectionManager().callService(domain: entity.domain, service: "set_aux_heat", entityId: entity.entityId, data: {"aux_heat": "$_tmpAuxHeat"});
_resetStateTimer(entity);
});
}

View File

@ -18,7 +18,7 @@ class _CoverControlWidgetState extends State<CoverControlWidget> {
setState(() {
_tmpPosition = position.roundToDouble();
_changedHere = true;
ConnectionManager().callService(entity.domain, "set_cover_position", entity.entityId,{"position": _tmpPosition.round()});
ConnectionManager().callService(domain: entity.domain, service: "set_cover_position", entityId: entity.entityId, data: {"position": _tmpPosition.round()});
});
}
@ -26,7 +26,7 @@ class _CoverControlWidgetState extends State<CoverControlWidget> {
setState(() {
_tmpTiltPosition = position.roundToDouble();
_changedHere = true;
ConnectionManager().callService(entity.domain, "set_cover_tilt_position", entity.entityId,{"tilt_position": _tmpTiltPosition.round()});
ConnectionManager().callService(domain: entity.domain, service: "set_cover_tilt_position", entityId: entity.entityId, data: {"tilt_position": _tmpTiltPosition.round()});
});
}
@ -136,17 +136,17 @@ class _CoverControlWidgetState extends State<CoverControlWidget> {
class CoverTiltControlsWidget extends StatelessWidget {
void _open(CoverEntity entity) {
ConnectionManager().callService(
entity.domain, "open_cover_tilt", entity.entityId, null);
domain: entity.domain, service: "open_cover_tilt", entityId: entity.entityId);
}
void _close(CoverEntity entity) {
ConnectionManager().callService(
entity.domain, "close_cover_tilt", entity.entityId, null);
domain: entity.domain, service: "close_cover_tilt", entityId: entity.entityId);
}
void _stop(CoverEntity entity) {
ConnectionManager().callService(
entity.domain, "stop_cover_tilt", entity.entityId, null);
domain: entity.domain, service: "stop_cover_tilt", entityId: entity.entityId);
}
@override

View File

@ -3,17 +3,26 @@ part of '../../../main.dart';
class CoverStateWidget extends StatelessWidget {
void _open(CoverEntity entity) {
ConnectionManager().callService(
entity.domain, "open_cover", entity.entityId, null);
domain: entity.domain,
service: "open_cover",
entityId: entity.entityId
);
}
void _close(CoverEntity entity) {
ConnectionManager().callService(
entity.domain, "close_cover", entity.entityId, null);
domain: entity.domain,
service: "close_cover",
entityId: entity.entityId
);
}
void _stop(CoverEntity entity) {
ConnectionManager().callService(
entity.domain, "stop_cover", entity.entityId, null);
domain: entity.domain,
service: "stop_cover",
entityId: entity.entityId
);
}
@override

View File

@ -36,6 +36,6 @@ class DateTimeEntity extends Entity {
}
void setNewState(Map newValue) {
ConnectionManager().callService(domain, "set_datetime", entityId, newValue);
ConnectionManager().callService(domain: domain, service: "set_datetime", entityId: entityId, data: newValue);
}
}

View File

@ -153,7 +153,7 @@ class Entity {
domain = rawData["entity_id"].split(".")[0];
entityId = rawData["entity_id"];
deviceClass = attributes["device_class"];
state = rawData["state"];
state = rawData["state"] is bool ? (rawData["state"] ? EntityState.on : EntityState.off) : rawData["state"];
displayState = Entity.StateByDeviceClass["$deviceClass.$state"] ?? (state.toLowerCase() == 'unknown' ? '-' : state);
_lastUpdated = DateTime.tryParse(rawData["last_updated"]);
entityPicture = _getEntityPictureUrl(webHost);

View File

@ -32,15 +32,17 @@ class EntityWrapper {
void handleTap() {
switch (uiAction.tapAction) {
case EntityUIAction.toggle: {
ConnectionManager().callService("homeassistant", "toggle", entity.entityId, null);
ConnectionManager().callService(domain: "homeassistant", service: "toggle", entityId: entity.entityId);
break;
}
case EntityUIAction.callService: {
if (uiAction.tapService != null) {
ConnectionManager().callService(uiAction.tapService.split(".")[0],
uiAction.tapService.split(".")[1], null,
uiAction.tapServiceData);
ConnectionManager().callService(
domain: uiAction.tapService.split(".")[0],
service: uiAction.tapService.split(".")[1],
data: uiAction.tapServiceData
);
}
break;
}
@ -74,15 +76,17 @@ class EntityWrapper {
void handleHold() {
switch (uiAction.holdAction) {
case EntityUIAction.toggle: {
ConnectionManager().callService("homeassistant", "toggle", entity.entityId, null);
ConnectionManager().callService(domain: "homeassistant", service: "toggle", entityId: entity.entityId);
break;
}
case EntityUIAction.callService: {
if (uiAction.holdService != null) {
ConnectionManager().callService(uiAction.holdService.split(".")[0],
uiAction.holdService.split(".")[1], null,
uiAction.holdServiceData);
ConnectionManager().callService(
domain: uiAction.holdService.split(".")[0],
service: uiAction.holdService.split(".")[1],
data: uiAction.holdServiceData
);
}
break;
}

View File

@ -25,8 +25,11 @@ class _FanControlsWidgetState extends State<FanControlsWidget> {
_tmpOscillate = oscillate;
_changedHere = true;
ConnectionManager().callService(
"fan", "oscillate", entity.entityId,
{"oscillating": oscillate});
domain: "fan",
service: "oscillate",
entityId: entity.entityId,
data: {"oscillating": oscillate}
);
});
}
@ -35,8 +38,11 @@ class _FanControlsWidgetState extends State<FanControlsWidget> {
_tmpDirectionForward = forward;
_changedHere = true;
ConnectionManager().callService(
"fan", "set_direction", entity.entityId,
{"direction": forward ? "forward" : "reverse"});
domain: "fan",
service: "set_direction",
entityId: entity.entityId,
data: {"direction": forward ? "forward" : "reverse"}
);
});
}
@ -45,8 +51,11 @@ class _FanControlsWidgetState extends State<FanControlsWidget> {
_tmpSpeed = value;
_changedHere = true;
ConnectionManager().callService(
"fan", "set_speed", entity.entityId,
{"speed": value});
domain: "fan",
service: "set_speed",
entityId: entity.entityId,
data: {"speed": value}
);
});
}

View File

@ -18,7 +18,7 @@ class FlatServiceButton extends StatelessWidget {
}) : super(key: key);
void _setNewState() {
ConnectionManager().callService(serviceDomain, serviceName, entityId, null);
ConnectionManager().callService(domain: serviceDomain, service: serviceName, entityId: entityId);
}
@override

View File

@ -29,8 +29,11 @@ class _LightControlsWidgetState extends State<LightControlsWidget> {
_tmpBrightness = value.round();
_changedHere = true;
ConnectionManager().callService(
entity.domain, "turn_on", entity.entityId,
{"brightness": _tmpBrightness});
domain: entity.domain,
service: "turn_on",
entityId: entity.entityId,
data: {"brightness": _tmpBrightness}
);
});
}
@ -39,8 +42,11 @@ class _LightControlsWidgetState extends State<LightControlsWidget> {
_tmpWhiteValue = value.round();
_changedHere = true;
ConnectionManager().callService(
entity.domain, "turn_on", entity.entityId,
{"white_value": _tmpWhiteValue});
domain: entity.domain,
service: "turn_on",
entityId: entity.entityId,
data: {"white_value": _tmpWhiteValue}
);
});
}
@ -50,8 +56,11 @@ class _LightControlsWidgetState extends State<LightControlsWidget> {
_tmpColorTemp = value.round();
_changedHere = true;
ConnectionManager().callService(
entity.domain, "turn_on", entity.entityId,
{"color_temp": _tmpColorTemp});
domain: entity.domain,
service: "turn_on",
entityId: entity.entityId,
data: {"color_temp": _tmpColorTemp}
);
});
}
@ -59,10 +68,12 @@ class _LightControlsWidgetState extends State<LightControlsWidget> {
setState(() {
_tmpColor = color;
_changedHere = true;
Logger.d( "HS Color: [${color.hue}, ${color.saturation}]");
ConnectionManager().callService(
entity.domain, "turn_on", entity.entityId,
{"hs_color": [color.hue, color.saturation*100]});
domain: entity.domain,
service: "turn_on",
entityId: entity.entityId,
data: {"hs_color": [color.hue, color.saturation*100]}
);
});
}
@ -72,8 +83,11 @@ class _LightControlsWidgetState extends State<LightControlsWidget> {
_changedHere = true;
if (_tmpEffect != null) {
ConnectionManager().callService(
entity.domain, "turn_on", entity.entityId,
{"effect": "$value"});
domain: entity.domain,
service: "turn_on",
entityId: entity.entityId,
data: {"effect": "$value"}
);
}
});
}

View File

@ -7,11 +7,11 @@ class LockStateWidget extends StatelessWidget {
const LockStateWidget({Key key, this.assumedState: false}) : super(key: key);
void _lock(Entity entity) {
ConnectionManager().callService("lock", "lock", entity.entityId, null);
ConnectionManager().callService(domain: "lock", service: "lock", entityId: entity.entityId);
}
void _unlock(Entity entity) {
ConnectionManager().callService("lock", "unlock", entity.entityId, null);
ConnectionManager().callService(domain: "lock", service: "unlock", entityId: entity.entityId);
}
@override

View File

@ -57,10 +57,10 @@ class _MediaPlayerSeekBarState extends State<MediaPlayerSeekBar> {
focusColor: Colors.white,
onPressed: () {
ConnectionManager().callService(
"media_player",
"media_seek",
"${entity.entityId}",
{"seek_position": _savedPosition}
domain: "media_player",
service: "media_seek",
entityId: entity.entityId,
data: {"seek_position": _savedPosition}
);
setState(() {
_savedPosition = 0;
@ -104,10 +104,10 @@ class _MediaPlayerSeekBarState extends State<MediaPlayerSeekBar> {
Timer(Duration(milliseconds: 500), () {
if (!_seekStarted) {
ConnectionManager().callService(
"media_player",
"media_seek",
"${entity.entityId}",
{"seek_position": val}
domain: "media_player",
service: "media_seek",
entityId: entity.entityId,
data: {"seek_position": val}
);
setState(() {
_changedHere = true;

View File

@ -118,26 +118,28 @@ class MediaPlayerPlaybackControls extends StatelessWidget {
void _setPower(MediaPlayerEntity entity) {
if (entity.state != EntityState.unavailable && entity.state != EntityState.unknown) {
if (entity.state == EntityState.off) {
Logger.d("${entity.entityId} turn_on");
ConnectionManager().callService(
entity.domain, "turn_on", entity.entityId,
null);
domain: entity.domain,
service: "turn_on",
entityId: entity.entityId
);
} else {
Logger.d("${entity.entityId} turn_off");
ConnectionManager().callService(
entity.domain, "turn_off", entity.entityId,
null);
domain: entity.domain,
service: "turn_off",
entityId: entity.entityId
);
}
}
}
void _callAction(MediaPlayerEntity entity, String action) {
Logger.d("${entity.entityId} $action");
ConnectionManager().callService(
entity.domain, "$action", entity.entityId,
null);
domain: entity.domain,
service: "$action",
entityId: entity.entityId
);
}
@override
@ -264,27 +266,50 @@ class _MediaPlayerControlsState extends State<MediaPlayerControls> {
setState(() {
_changedHere = true;
_newVolumeLevel = value;
ConnectionManager().callService("media_player", "volume_set", entityId, {"volume_level": value});
ConnectionManager().callService(
domain: "media_player",
service: "volume_set",
entityId: entityId,
data: {"volume_level": value}
);
});
}
void _setVolumeMute(bool isMuted, String entityId) {
ConnectionManager().callService("media_player", "volume_mute", entityId, {"is_volume_muted": isMuted});
ConnectionManager().callService(
domain: "media_player",
service: "volume_mute",
entityId: entityId,
data: {"is_volume_muted": isMuted}
);
}
void _setVolumeUp(String entityId) {
ConnectionManager().callService("media_player", "volume_up", entityId, null);
ConnectionManager().callService(
domain: "media_player",
service: "volume_up",
entityId: entityId
);
}
void _setVolumeDown(String entityId) {
ConnectionManager().callService("media_player", "volume_down", entityId, null);
ConnectionManager().callService(
domain: "media_player",
service: "volume_down",
entityId: entityId
);
}
void _setSoundMode(String value, String entityId) {
setState(() {
_newSoundMode = value;
_changedHere = true;
ConnectionManager().callService("media_player", "select_sound_mode", entityId, {"sound_mode": "$value"});
ConnectionManager().callService(
domain: "media_player",
service: "select_sound_mode",
entityId: entityId,
data: {"sound_mode": "$value"}
);
});
}
@ -292,7 +317,12 @@ class _MediaPlayerControlsState extends State<MediaPlayerControls> {
setState(() {
_newSource = source;
_changedHere = true;
ConnectionManager().callService("media_player", "select_source", entityId, {"source": "$source"});
ConnectionManager().callService(
domain: "media_player",
service: "select_source",
entityId: entityId,
data: {"source": "$source"}
);
});
}

View File

@ -11,8 +11,12 @@ class SelectStateWidget extends StatefulWidget {
class _SelectStateWidgetState extends State<SelectStateWidget> {
void setNewState(domain, entityId, newValue) {
ConnectionManager().callService(domain, "select_option", entityId,
{"option": "$newValue"});
ConnectionManager().callService(
domain: domain,
service: "select_option",
entityId: entityId,
data: {"option": "$newValue"}
);
}
@override

View File

@ -18,8 +18,12 @@ class _SliderControlsWidgetState extends State<SliderControlsWidget> {
_newValue = newValue;
_changedHere = true;
});
ConnectionManager().callService(domain, "set_value", entityId,
{"value": "${newValue.toString()}"});
ConnectionManager().callService(
domain: domain,
service: "set_value",
entityId: entityId,
data: {"value": "${newValue.toString()}"}
);
}
@override

View File

@ -39,7 +39,10 @@ class _SwitchStateWidgetState extends State<SwitchStateWidget> {
domain = entity.domain;
}
ConnectionManager().callService(
domain, (newValue as bool) ? "turn_on" : "turn_off", entity.entityId, null);
domain: domain,
service: (newValue as bool) ? "turn_on" : "turn_off",
entityId: entity.entityId
);
}
@override

View File

@ -26,8 +26,12 @@ class _TextInputStateWidgetState extends State<TextInputStateWidget> {
void setNewState(newValue, domain, entityId) {
if (validate(newValue, _minLength, _maxLength)) {
ConnectionManager().callService(domain, "set_value", entityId,
{"value": "$newValue"});
ConnectionManager().callService(
domain: domain,
service: "set_value",
entityId: entityId,
data: {"value": "$newValue"}
);
} else {
setState(() {
_tmpValue = _entityState;

View File

@ -77,10 +77,9 @@ class VacuumControls extends StatelessWidget {
icon: Icon(MaterialDesignIcons.getIconDataFromIconName("mdi:play")),
iconSize: iconSize,
onPressed: () => ConnectionManager().callService(
"vacuum",
entity.entityId,
"start",
null
domain: "vacuum",
entityId: entity.entityId,
service: "start"
),
)
);
@ -91,10 +90,9 @@ class VacuumControls extends StatelessWidget {
icon: Icon(MaterialDesignIcons.getIconDataFromIconName("mdi:play-pause")),
iconSize: iconSize,
onPressed: () => ConnectionManager().callService(
"vacuum",
entity.entityId,
"start_pause",
null
domain: "vacuum",
entityId: entity.entityId,
service: "start_pause"
),
)
);
@ -104,10 +102,9 @@ class VacuumControls extends StatelessWidget {
icon: Icon(MaterialDesignIcons.getIconDataFromIconName("mdi:pause")),
iconSize: iconSize,
onPressed: () => ConnectionManager().callService(
"vacuum",
entity.entityId,
"pause",
null
domain: "vacuum",
entityId: entity.entityId,
service: "pause"
),
)
);
@ -118,10 +115,9 @@ class VacuumControls extends StatelessWidget {
icon: Icon(MaterialDesignIcons.getIconDataFromIconName("mdi:stop")),
iconSize: iconSize,
onPressed: () => ConnectionManager().callService(
"vacuum",
entity.entityId,
"stop",
null
domain: "vacuum",
entityId: entity.entityId,
service: "stop"
),
)
);
@ -132,10 +128,9 @@ class VacuumControls extends StatelessWidget {
icon: Icon(MaterialDesignIcons.getIconDataFromIconName("mdi:broom")),
iconSize: iconSize,
onPressed: () => ConnectionManager().callService(
"vacuum",
entity.entityId,
"clean_spot",
null
domain: "vacuum",
entityId: entity.entityId,
service: "clean_spot"
),
)
);
@ -146,10 +141,9 @@ class VacuumControls extends StatelessWidget {
icon: Icon(MaterialDesignIcons.getIconDataFromIconName("mdi:map-marker")),
iconSize: iconSize,
onPressed: () => ConnectionManager().callService(
"vacuum",
entity.entityId,
"locate",
null
domain: "vacuum",
entityId: entity.entityId,
service: "locate"
),
)
);
@ -160,10 +154,9 @@ class VacuumControls extends StatelessWidget {
icon: Icon(MaterialDesignIcons.getIconDataFromIconName("mdi:home-map-marker")),
iconSize: iconSize,
onPressed: () => ConnectionManager().callService(
"vacuum",
entity.entityId,
"return_to_base",
null
domain: "vacuum",
entityId: entity.entityId,
service: "return_to_base"
),
)
);
@ -201,10 +194,10 @@ class VacuumControls extends StatelessWidget {
options: entity.fanSpeedList,
value: entity.fanSpeed,
onChange: (val) => ConnectionManager().callService(
"vacuum",
entity.entityId,
"set_fan_speed",
{"fan_speed": val}
domain: "vacuum",
entityId: entity.entityId,
service: "set_fan_speed",
data: {"fan_speed": val}
)
),
);

View File

@ -106,10 +106,20 @@ class HomeAssistant {
});
}
Future _getLovelace() async {
await ConnectionManager().sendSocketMessage(type: "lovelace/config").then((data) => _rawLovelaceData = data).catchError((e) {
throw HAError("Error getting lovelace config: $e");
Future _getLovelace() {
Completer completer = Completer();
ConnectionManager().sendSocketMessage(type: "lovelace/config").then((data) {
_rawLovelaceData = data;
completer.complete();
}).catchError((e) {
if ("$e" == "config_not_found") {
ConnectionManager().useLovelace = false;
completer.complete();
} else {
completer.completeError(HAError("Error getting lovelace config: $e"));
}
});
return completer.future;
}
Future _getUserInfo() async {
@ -125,7 +135,6 @@ class HomeAssistant {
Future _getServices() async {
await ConnectionManager().sendSocketMessage(type: "get_services").then((data) {
Logger.d("Got ${data.length} services");
Logger.d("Media extractor: ${data["media_extractor"]}");
services = data;
}).catchError((e) {
Logger.w("Can't get services: $e");
@ -209,6 +218,9 @@ class HomeAssistant {
try {
//bool isThereCardOptionsInside = rawCard["card"] != null;
var rawCardInfo = rawCard["card"] ?? rawCard;
if (rawCardInfo['state_filter'] != null) {
Logger.d("Hey!!!!!! We found a card with state filter: ${rawCardInfo['state_filter']}");
}
HACard card = HACard(
id: "card",
name: rawCardInfo["title"] ?? rawCardInfo["name"],
@ -217,7 +229,7 @@ class HomeAssistant {
showName: rawCardInfo['show_name'] ?? true,
showState: rawCardInfo['show_state'] ?? true,
showEmpty: rawCardInfo['show_empty'] ?? true,
stateFilter: rawCardInfo['state_filter'] ?? [],
stateFilter: (rawCard['state_filter'] ?? rawCardInfo['state_filter']) ?? [],
states: rawCardInfo['states'],
conditions: rawCard['conditions'] ?? [],
content: rawCardInfo['content'],

View File

@ -189,8 +189,8 @@ class ConnectionManager {
//Logger.d("[Received] <== Request id ${data['id']} was successful");
_messageResolver["${data["id"]}"]?.complete(data["result"]);
} else if (data["id"] != null) {
//Logger.e("[Received] <== Error received on request id ${data['id']}: ${data['error']}");
_messageResolver["${data["id"]}"]?.completeError("${data['error']["message"]}");
Logger.e("[Received] <== Error received on request id ${data['id']}: ${data['error']}");
_messageResolver["${data["id"]}"]?.completeError("${data["error"]["code"]}");
}
_messageResolver.remove("${data["id"]}");
} else if (data["type"] == "event") {
@ -348,16 +348,16 @@ class ConnectionManager {
_currentMessageId += 1;
}
Future callService(String domain, String service, String entityId, Map additionalServiceData) {
Future callService({@required String domain, @required String service, String entityId, Map data}) {
eventBus.fire(NotifyServiceCallEvent(domain, service, entityId));
Logger.d("Service call: $domain.$service, $entityId, $additionalServiceData");
Logger.d("Service call: $domain.$service, $entityId, $data");
Completer completer = Completer();
Map serviceData = {};
if (entityId != null) {
serviceData["entity_id"] = entityId;
}
if (additionalServiceData != null && additionalServiceData.isNotEmpty) {
serviceData.addAll(additionalServiceData);
if (data != null && data.isNotEmpty) {
serviceData.addAll(data);
}
if (serviceData.isNotEmpty)
sendHTTPPost(

View File

@ -14,7 +14,7 @@ class LocationManager {
}
final int defaultUpdateIntervalMinutes = 20;
final String backgroundTaskId = "haclocationtask4352";
final String backgroundTaskId = "haclocationtask0";
final String backgroundTaskTag = "haclocation";
Duration _updateInterval;
bool _isRunning;
@ -27,6 +27,7 @@ class LocationManager {
_isRunning = prefs.getBool("location-enabled") ?? false;
if (_isRunning) {
await _startLocationService();
updateDeviceLocation(false);
}
}
@ -57,27 +58,48 @@ class LocationManager {
}
_startLocationService() async {
Logger.d("Scheduling location update for every ${_updateInterval
.inMinutes} minutes...");
String webhookId = ConnectionManager().webhookId;
String httpWebHost = ConnectionManager().httpWebHost;
if (webhookId != null && webhookId.isNotEmpty) {
await workManager.Workmanager.registerPeriodicTask(
backgroundTaskId,
"haClientLocationTracking",
tag: backgroundTaskTag,
inputData: {
"webhookId": webhookId,
"httpWebHost": httpWebHost
},
frequency: _updateInterval,
existingWorkPolicy: workManager.ExistingWorkPolicy.keep,
backoffPolicy: workManager.BackoffPolicy.linear,
backoffPolicyDelay: _updateInterval,
constraints: workManager.Constraints(
networkType: workManager.NetworkType.connected
)
);
Duration interval;
int delayFactor;
int taskCount;
Logger.d("Starting location update for every ${_updateInterval
.inMinutes} minutes...");
if (_updateInterval.inMinutes == 10) {
interval = Duration(minutes: 20);
taskCount = 2;
delayFactor = 10;
} else if (_updateInterval.inMinutes == 5) {
interval = Duration(minutes: 15);
taskCount = 3;
delayFactor = 5;
} else {
interval = _updateInterval;
taskCount = 1;
delayFactor = 0;
}
for (int i = 1; i <= taskCount; i++) {
int delay = i*delayFactor;
Logger.d("Scheduling location update task #$i for every ${interval.inMinutes} minutes in $delay minutes...");
await workManager.Workmanager.registerPeriodicTask(
"$backgroundTaskId$n",
"haClientLocationTracking",
tag: backgroundTaskTag,
inputData: {
"webhookId": webhookId,
"httpWebHost": httpWebHost
},
frequency: interval,
initialDelay: Duration(minutes: delay),
existingWorkPolicy: workManager.ExistingWorkPolicy.keep,
backoffPolicy: workManager.BackoffPolicy.linear,
backoffPolicyDelay: interval,
constraints: workManager.Constraints(
networkType: workManager.NetworkType.connected
)
);
}
}
}
@ -86,58 +108,46 @@ class LocationManager {
await workManager.Workmanager.cancelByTag(backgroundTaskTag);
}
updateDeviceLocation() async {
Logger.d("[Test location] Started");
Logger.d("[Test location] Forcing Android location manager...");
updateDeviceLocation(bool force) async {
if (!force && !_isRunning) {
Logger.d("[Foreground location] Not enabled. Aborting.");
return;
}
Logger.d("[Foreground location] Started");
//Logger.d("[Foreground location] Forcing Android location manager...");
Geolocator geolocator = Geolocator()..forceAndroidLocationManager = true;
var battery = Battery();
int batteryLevel = 100;
String webhookId = ConnectionManager().webhookId;
String httpWebHost = ConnectionManager().httpWebHost;
if (webhookId != null && webhookId.isNotEmpty) {
String url = "$httpWebHost/api/webhook/$webhookId";
Map<String, String> headers = {};
headers["Content-Type"] = "application/json";
Map data = {
"type": "update_location",
"data": {
"gps": [],
"gps_accuracy": 0,
"battery": batteryLevel
}
};
Logger.d("[Test location] Getting battery level...");
battery.batteryLevel.then((val) => data["data"]["battery"] = val).whenComplete((){
Logger.d("[Test location] Getting device location...");
geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high, locationPermissionLevel: GeolocationPermission.locationAlways).then((location) {
Logger.d("[Test location] Got location: ${location.latitude} ${location.longitude} with accuracy of ${location.accuracy}");
if (location != null) {
data["data"]["gps"] = [location.latitude, location.longitude];
data["data"]["gps_accuracy"] = location.accuracy;
Logger.d("[Test location] Sending data home...");
http.post(
url,
headers: headers,
body: json.encode(data)
);
Logger.d("[Foreground location] Getting battery level...");
int batteryLevel = await battery.batteryLevel;
Logger.d("[Foreground location] Getting device location...");
Position position = await geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high,
locationPermissionLevel: GeolocationPermission.locationAlways
);
if (position != null) {
Logger.d("[Foreground location] Location: ${position.latitude} ${position.longitude}. Accuracy: ${position.accuracy}. (${position.timestamp})");
String url = "$httpWebHost/api/webhook/$webhookId";
Map data = {
"type": "update_location",
"data": {
"gps": [position.latitude, position.longitude],
"gps_accuracy": position.accuracy,
"battery": batteryLevel ?? 100
}
}).catchError((e) {
Logger.d("[Test location] Error getting current location: ${e.toString()}. Trying last known...");
geolocator.getLastKnownPosition(desiredAccuracy: LocationAccuracy.medium).then((location){
Logger.d("[Test location] Got last known location: ${location.latitude} ${location.longitude} with accuracy of ${location.accuracy}");
if (location != null) {
data["data"]["gps"] = [location.latitude, location.longitude];
data["data"]["gps_accuracy"] = location.accuracy;
Logger.d("[Test location] Sending data home...");
http.post(
url,
headers: headers,
body: json.encode(data)
);
}
});
});
});
};
Logger.d("[Foreground location] Sending data home...");
var response = await http.post(
url,
headers: {"Content-Type": "application/json"},
body: json.encode(data)
);
Logger.d("[Foreground location] Got HTTP ${response.statusCode}");
} else {
Logger.d("[Foreground location] No location. Aborting.");
}
}
}

View File

@ -45,7 +45,7 @@ class MobileAppIntegrationManager {
positiveText: "Restart now",
negativeText: "Later",
onPositive: () {
ConnectionManager().callService("homeassistant","restart", null, null);
ConnectionManager().callService(domain: "homeassistant", service: "restart");
},
));
});

View File

@ -29,6 +29,9 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
setState(() {
_locationTrackingEnabled = prefs.getBool("location-enabled") ?? false;
_locationInterval = prefs.getInt("location-interval") ?? LocationManager().defaultUpdateIntervalMinutes;
if (_locationInterval % 5 != 0) {
_locationInterval = 5 * (_locationInterval ~/ 5);
}
});
});
}
@ -36,15 +39,15 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
void incLocationInterval() {
if (_locationInterval < 720) {
setState(() {
_locationInterval = _locationInterval + 1;
_locationInterval = _locationInterval + 5;
});
}
}
void decLocationInterval() {
if (_locationInterval > 15) {
if (_locationInterval > 5) {
setState(() {
_locationInterval = _locationInterval - 1;
_locationInterval = _locationInterval - 5;
});
}
}
@ -56,7 +59,7 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
positiveText: "Sure. Make it so",
negativeText: "What?? No!",
onPositive: () {
ConnectionManager().callService("homeassistant", "restart", null, null);
ConnectionManager().callService(domain: "homeassistant", service: "restart");
},
));
}
@ -68,7 +71,7 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
positiveText: "Sure. Make it so",
negativeText: "What?? No!",
onPositive: () {
ConnectionManager().callService("homeassistant","stop", null, null);
ConnectionManager().callService(domain: "homeassistant", service: "stop");
},
));
}
@ -91,7 +94,7 @@ class _IntegrationSettingsPageState extends State<IntegrationSettingsPage> {
_switchLocationTrackingState(bool state) async {
if (state) {
await LocationManager().updateDeviceLocation();
await LocationManager().updateDeviceLocation(true);
}
await LocationManager().setSettings(_locationTrackingEnabled, _locationInterval);
setState(() {

View File

@ -122,6 +122,7 @@ class _MainPageState extends ReceiveShareState<MainPage> with WidgetsBindingObse
_showInfoBottomBar(progress: true,);
ConnectionManager().init(loadSettings: false, forceReconnect: false).then((_){
_fetchData();
LocationManager().updateDeviceLocation(false);
//StartupUserMessagesManager().checkMessagesToShow();
}, onError: (e) {
_setErrorState(e);

View File

@ -89,21 +89,20 @@ class _PlayMediaPageState extends State<PlayMediaPage> {
}
Navigator.pop(context);
ConnectionManager().callService(
serviceDomain,
"play_media",
entity.entityId,
{
"media_content_id": _mediaUrl,
"media_content_type": _contentType
}
domain: serviceDomain,
service: "play_media",
entityId: entity.entityId,
data: {
"media_content_id": _mediaUrl,
"media_content_type": _contentType
}
);
HomeAssistant().sendToPlayerId = entity.entityId;
if (HomeAssistant().sendFromPlayerId != null && HomeAssistant().sendFromPlayerId != HomeAssistant().sendToPlayerId) {
ConnectionManager().callService(
HomeAssistant().sendFromPlayerId.split(".")[0],
"turn_off",
HomeAssistant().sendFromPlayerId,
null
domain: HomeAssistant().sendFromPlayerId.split(".")[0],
service: "turn_off",
entityId: HomeAssistant().sendFromPlayerId
);
HomeAssistant().sendFromPlayerId = null;
}

View File

@ -26,7 +26,7 @@ dependencies:
flutter_secure_storage: ^3.3.1+1
device_info: ^0.4.0+3
flutter_local_notifications: ^0.8.4
geolocator: ^5.1.4+2
geolocator: ^5.1.5
workmanager: ^0.1.3
battery: ^0.3.1+1
share: