From b34cc9708026b4dbf714484562e7d84c146b85ee Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Mon, 28 Oct 2019 17:59:47 +0000 Subject: [PATCH] Show entity page on main page --- .../alarm_control_panel_controls.widget.dart | 16 +++-- .../climate/widgets/climate_controls.dart | 26 ++++++--- .../cover/widgets/cover_controls.widget.dart | 16 ++--- lib/entities/cover/widgets/cover_state.dart | 12 ++-- .../date_time/date_time_entity.class.dart | 5 +- lib/entities/entity_page_layout.widget.dart | 4 +- lib/entities/entity_wrapper.class.dart | 16 ++--- lib/entities/fan/widgets/fan_controls.dart | 12 ++-- lib/entities/flat_service_button.widget.dart | 2 +- .../light/widgets/light_controls.dart | 20 +++---- lib/entities/lock/widgets/lock_state.dart | 4 +- .../widgets/media_player_seek_bar.widget.dart | 8 +-- .../widgets/media_player_widgets.dart | 26 ++++----- lib/entities/select/widgets/select_state.dart | 4 +- .../slider/widgets/slider_controls.dart | 4 +- lib/entities/switch/widget/switch_state.dart | 4 +- .../text/widgets/text_input_state.dart | 4 +- .../vacuum/widgets/vacuum_controls.dart | 57 ++++++++++-------- lib/managers/connection_manager.class.dart | 7 ++- .../mobile_app_integration_manager.class.dart | 2 +- lib/pages/integration_settings.page.dart | 4 +- lib/pages/main.page.dart | 58 ++++++++++++------- lib/pages/play_media.page.dart | 17 ++++-- lib/types/event_bus_events.dart | 5 +- 24 files changed, 186 insertions(+), 147 deletions(-) diff --git a/lib/entities/alarm_control_panel/widgets/alarm_control_panel_controls.widget.dart b/lib/entities/alarm_control_panel/widgets/alarm_control_panel_controls.widget.dart index c1a91a3..0d2bb92 100644 --- a/lib/entities/alarm_control_panel/widgets/alarm_control_panel_controls.widget.dart +++ b/lib/entities/alarm_control_panel/widgets/alarm_control_panel_controls.widget.dart @@ -25,9 +25,12 @@ class _AlarmControlPanelControlsWidgetWidgetState extends State { _tempThrottleTimer = Timer(Duration(seconds: 2), () { setState(() { _changedHere = true; - eventBus.fire(new ServiceCallEvent(entity.domain, "set_temperature", entity.entityId,{"temperature": "${_tmpTemperature.toStringAsFixed(1)}"})); + ConnectionManager().callService( + entity.domain, + "set_temperature", + entity.entityId, + {"temperature": "${_tmpTemperature.toStringAsFixed(1)}"} + ); _resetStateTimer(entity); }); }); @@ -101,7 +106,12 @@ class _ClimateControlWidgetState extends State { _targetTempThrottleTimer = Timer(Duration(seconds: 2), () { setState(() { _changedHere = true; - eventBus.fire(new ServiceCallEvent(entity.domain, "set_temperature", entity.entityId,{"target_temp_high": "${_tmpTargetHigh.toStringAsFixed(1)}", "target_temp_low": "${_tmpTargetLow.toStringAsFixed(1)}"})); + ConnectionManager().callService( + entity.domain, + "set_temperature", + entity.entityId, + {"target_temp_high": "${_tmpTargetHigh.toStringAsFixed(1)}", "target_temp_low": "${_tmpTargetLow.toStringAsFixed(1)}"} + ); _resetStateTimer(entity); }); }); @@ -111,7 +121,7 @@ class _ClimateControlWidgetState extends State { setState(() { _tmpTargetHumidity = value.roundToDouble(); _changedHere = true; - eventBus.fire(new ServiceCallEvent(entity.domain, "set_humidity", entity.entityId,{"humidity": "$_tmpTargetHumidity"})); + ConnectionManager().callService(entity.domain, "set_humidity", entity.entityId, {"humidity": "$_tmpTargetHumidity"}); _resetStateTimer(entity); }); } @@ -120,7 +130,7 @@ class _ClimateControlWidgetState extends State { setState(() { _tmpHVACMode = value; _changedHere = true; - eventBus.fire(new ServiceCallEvent(entity.domain, "set_hvac_mode", entity.entityId,{"hvac_mode": "$_tmpHVACMode"})); + ConnectionManager().callService(entity.domain, "set_hvac_mode", entity.entityId, {"hvac_mode": "$_tmpHVACMode"}); _resetStateTimer(entity); }); } @@ -129,7 +139,7 @@ class _ClimateControlWidgetState extends State { setState(() { _tmpSwingMode = value; _changedHere = true; - eventBus.fire(new ServiceCallEvent(entity.domain, "set_swing_mode", entity.entityId,{"swing_mode": "$_tmpSwingMode"})); + ConnectionManager().callService(entity.domain, "set_swing_mode", entity.entityId, {"swing_mode": "$_tmpSwingMode"}); _resetStateTimer(entity); }); } @@ -138,7 +148,7 @@ class _ClimateControlWidgetState extends State { setState(() { _tmpFanMode = value; _changedHere = true; - eventBus.fire(new ServiceCallEvent(entity.domain, "set_fan_mode", entity.entityId,{"fan_mode": "$_tmpFanMode"})); + ConnectionManager().callService(entity.domain, "set_fan_mode", entity.entityId, {"fan_mode": "$_tmpFanMode"}); _resetStateTimer(entity); }); } @@ -147,7 +157,7 @@ class _ClimateControlWidgetState extends State { setState(() { _tmpPresetMode = value; _changedHere = true; - eventBus.fire(new ServiceCallEvent(entity.domain, "set_preset_mode", entity.entityId,{"preset_mode": "$_tmpPresetMode"})); + ConnectionManager().callService(entity.domain, "set_preset_mode", entity.entityId, {"preset_mode": "$_tmpPresetMode"}); _resetStateTimer(entity); }); } @@ -165,7 +175,7 @@ class _ClimateControlWidgetState extends State { setState(() { _tmpAuxHeat = value; _changedHere = true; - eventBus.fire(new ServiceCallEvent(entity.domain, "set_aux_heat", entity.entityId, {"aux_heat": "$_tmpAuxHeat"})); + ConnectionManager().callService(entity.domain, "set_aux_heat", entity.entityId, {"aux_heat": "$_tmpAuxHeat"}); _resetStateTimer(entity); }); } diff --git a/lib/entities/cover/widgets/cover_controls.widget.dart b/lib/entities/cover/widgets/cover_controls.widget.dart index 40ab1b2..0510c47 100644 --- a/lib/entities/cover/widgets/cover_controls.widget.dart +++ b/lib/entities/cover/widgets/cover_controls.widget.dart @@ -18,7 +18,7 @@ class _CoverControlWidgetState extends State { setState(() { _tmpPosition = position.roundToDouble(); _changedHere = true; - eventBus.fire(new ServiceCallEvent(entity.domain, "set_cover_position", entity.entityId,{"position": _tmpPosition.round()})); + ConnectionManager().callService(entity.domain, "set_cover_position", entity.entityId,{"position": _tmpPosition.round()}); }); } @@ -26,7 +26,7 @@ class _CoverControlWidgetState extends State { setState(() { _tmpTiltPosition = position.roundToDouble(); _changedHere = true; - eventBus.fire(new ServiceCallEvent(entity.domain, "set_cover_tilt_position", entity.entityId,{"tilt_position": _tmpTiltPosition.round()})); + ConnectionManager().callService(entity.domain, "set_cover_tilt_position", entity.entityId,{"tilt_position": _tmpTiltPosition.round()}); }); } @@ -135,18 +135,18 @@ class _CoverControlWidgetState extends State { class CoverTiltControlsWidget extends StatelessWidget { void _open(CoverEntity entity) { - eventBus.fire(new ServiceCallEvent( - entity.domain, "open_cover_tilt", entity.entityId, null)); + ConnectionManager().callService( + entity.domain, "open_cover_tilt", entity.entityId, null); } void _close(CoverEntity entity) { - eventBus.fire(new ServiceCallEvent( - entity.domain, "close_cover_tilt", entity.entityId, null)); + ConnectionManager().callService( + entity.domain, "close_cover_tilt", entity.entityId, null); } void _stop(CoverEntity entity) { - eventBus.fire(new ServiceCallEvent( - entity.domain, "stop_cover_tilt", entity.entityId, null)); + ConnectionManager().callService( + entity.domain, "stop_cover_tilt", entity.entityId, null); } @override diff --git a/lib/entities/cover/widgets/cover_state.dart b/lib/entities/cover/widgets/cover_state.dart index a115557..77bed97 100644 --- a/lib/entities/cover/widgets/cover_state.dart +++ b/lib/entities/cover/widgets/cover_state.dart @@ -2,18 +2,18 @@ part of '../../../main.dart'; class CoverStateWidget extends StatelessWidget { void _open(CoverEntity entity) { - eventBus.fire(new ServiceCallEvent( - entity.domain, "open_cover", entity.entityId, null)); + ConnectionManager().callService( + entity.domain, "open_cover", entity.entityId, null); } void _close(CoverEntity entity) { - eventBus.fire(new ServiceCallEvent( - entity.domain, "close_cover", entity.entityId, null)); + ConnectionManager().callService( + entity.domain, "close_cover", entity.entityId, null); } void _stop(CoverEntity entity) { - eventBus.fire(new ServiceCallEvent( - entity.domain, "stop_cover", entity.entityId, null)); + ConnectionManager().callService( + entity.domain, "stop_cover", entity.entityId, null); } @override diff --git a/lib/entities/date_time/date_time_entity.class.dart b/lib/entities/date_time/date_time_entity.class.dart index fdca0b1..2db63bd 100644 --- a/lib/entities/date_time/date_time_entity.class.dart +++ b/lib/entities/date_time/date_time_entity.class.dart @@ -35,8 +35,7 @@ class DateTimeEntity extends Entity { return formattedState; } - void setNewState(newValue) { - eventBus - .fire(new ServiceCallEvent(domain, "set_datetime", entityId, newValue)); + void setNewState(Map newValue) { + ConnectionManager().callService(domain, "set_datetime", entityId, newValue); } } \ No newline at end of file diff --git a/lib/entities/entity_page_layout.widget.dart b/lib/entities/entity_page_layout.widget.dart index 7d7325b..209445d 100644 --- a/lib/entities/entity_page_layout.widget.dart +++ b/lib/entities/entity_page_layout.widget.dart @@ -17,7 +17,7 @@ class EntityPageLayout extends StatelessWidget { showClose ? Container( color: Colors.blue[300], - height: 36, + height: 40, child: Row( children: [ Expanded( @@ -37,7 +37,7 @@ class EntityPageLayout extends StatelessWidget { padding: EdgeInsets.all(0), icon: Icon(Icons.close), color: Colors.white, - iconSize: 30.0, + iconSize: 36.0, onPressed: () { eventBus.fire(ShowEntityPageEvent()); }, diff --git a/lib/entities/entity_wrapper.class.dart b/lib/entities/entity_wrapper.class.dart index d1fe9bc..016d004 100644 --- a/lib/entities/entity_wrapper.class.dart +++ b/lib/entities/entity_wrapper.class.dart @@ -32,17 +32,15 @@ class EntityWrapper { void handleTap() { switch (uiAction.tapAction) { case EntityUIAction.toggle: { - eventBus.fire( - ServiceCallEvent("homeassistant", "toggle", entity.entityId, null)); + ConnectionManager().callService("homeassistant", "toggle", entity.entityId, null); break; } case EntityUIAction.callService: { if (uiAction.tapService != null) { - eventBus.fire( - ServiceCallEvent(uiAction.tapService.split(".")[0], + ConnectionManager().callService(uiAction.tapService.split(".")[0], uiAction.tapService.split(".")[1], null, - uiAction.tapServiceData)); + uiAction.tapServiceData); } break; } @@ -76,17 +74,15 @@ class EntityWrapper { void handleHold() { switch (uiAction.holdAction) { case EntityUIAction.toggle: { - eventBus.fire( - ServiceCallEvent("homeassistant", "toggle", entity.entityId, null)); + ConnectionManager().callService("homeassistant", "toggle", entity.entityId, null); break; } case EntityUIAction.callService: { if (uiAction.holdService != null) { - eventBus.fire( - ServiceCallEvent(uiAction.holdService.split(".")[0], + ConnectionManager().callService(uiAction.holdService.split(".")[0], uiAction.holdService.split(".")[1], null, - uiAction.holdServiceData)); + uiAction.holdServiceData); } break; } diff --git a/lib/entities/fan/widgets/fan_controls.dart b/lib/entities/fan/widgets/fan_controls.dart index a630896..dae4376 100644 --- a/lib/entities/fan/widgets/fan_controls.dart +++ b/lib/entities/fan/widgets/fan_controls.dart @@ -24,9 +24,9 @@ class _FanControlsWidgetState extends State { setState(() { _tmpOscillate = oscillate; _changedHere = true; - eventBus.fire(new ServiceCallEvent( + ConnectionManager().callService( "fan", "oscillate", entity.entityId, - {"oscillating": oscillate})); + {"oscillating": oscillate}); }); } @@ -34,9 +34,9 @@ class _FanControlsWidgetState extends State { setState(() { _tmpDirectionForward = forward; _changedHere = true; - eventBus.fire(new ServiceCallEvent( + ConnectionManager().callService( "fan", "set_direction", entity.entityId, - {"direction": forward ? "forward" : "reverse"})); + {"direction": forward ? "forward" : "reverse"}); }); } @@ -44,9 +44,9 @@ class _FanControlsWidgetState extends State { setState(() { _tmpSpeed = value; _changedHere = true; - eventBus.fire(new ServiceCallEvent( + ConnectionManager().callService( "fan", "set_speed", entity.entityId, - {"speed": value})); + {"speed": value}); }); } diff --git a/lib/entities/flat_service_button.widget.dart b/lib/entities/flat_service_button.widget.dart index 6dc4612..df1adcc 100644 --- a/lib/entities/flat_service_button.widget.dart +++ b/lib/entities/flat_service_button.widget.dart @@ -18,7 +18,7 @@ class FlatServiceButton extends StatelessWidget { }) : super(key: key); void _setNewState() { - eventBus.fire(new ServiceCallEvent(serviceDomain, serviceName, entityId, null)); + ConnectionManager().callService(serviceDomain, serviceName, entityId, null); } @override diff --git a/lib/entities/light/widgets/light_controls.dart b/lib/entities/light/widgets/light_controls.dart index 740955e..6be905f 100644 --- a/lib/entities/light/widgets/light_controls.dart +++ b/lib/entities/light/widgets/light_controls.dart @@ -28,9 +28,9 @@ class _LightControlsWidgetState extends State { setState(() { _tmpBrightness = value.round(); _changedHere = true; - eventBus.fire(new ServiceCallEvent( + ConnectionManager().callService( entity.domain, "turn_on", entity.entityId, - {"brightness": _tmpBrightness})); + {"brightness": _tmpBrightness}); }); } @@ -38,9 +38,9 @@ class _LightControlsWidgetState extends State { setState(() { _tmpWhiteValue = value.round(); _changedHere = true; - eventBus.fire(new ServiceCallEvent( + ConnectionManager().callService( entity.domain, "turn_on", entity.entityId, - {"white_value": _tmpWhiteValue})); + {"white_value": _tmpWhiteValue}); }); } @@ -49,9 +49,9 @@ class _LightControlsWidgetState extends State { setState(() { _tmpColorTemp = value.round(); _changedHere = true; - eventBus.fire(new ServiceCallEvent( + ConnectionManager().callService( entity.domain, "turn_on", entity.entityId, - {"color_temp": _tmpColorTemp})); + {"color_temp": _tmpColorTemp}); }); } @@ -60,9 +60,9 @@ class _LightControlsWidgetState extends State { _tmpColor = color; _changedHere = true; Logger.d( "HS Color: [${color.hue}, ${color.saturation}]"); - eventBus.fire(new ServiceCallEvent( + ConnectionManager().callService( entity.domain, "turn_on", entity.entityId, - {"hs_color": [color.hue, color.saturation*100]})); + {"hs_color": [color.hue, color.saturation*100]}); }); } @@ -71,9 +71,9 @@ class _LightControlsWidgetState extends State { _tmpEffect = value; _changedHere = true; if (_tmpEffect != null) { - eventBus.fire(new ServiceCallEvent( + ConnectionManager().callService( entity.domain, "turn_on", entity.entityId, - {"effect": "$value"})); + {"effect": "$value"}); } }); } diff --git a/lib/entities/lock/widgets/lock_state.dart b/lib/entities/lock/widgets/lock_state.dart index 0890b47..fe98dbc 100644 --- a/lib/entities/lock/widgets/lock_state.dart +++ b/lib/entities/lock/widgets/lock_state.dart @@ -7,11 +7,11 @@ class LockStateWidget extends StatelessWidget { const LockStateWidget({Key key, this.assumedState: false}) : super(key: key); void _lock(Entity entity) { - eventBus.fire(new ServiceCallEvent("lock", "lock", entity.entityId, null)); + ConnectionManager().callService("lock", "lock", entity.entityId, null); } void _unlock(Entity entity) { - eventBus.fire(new ServiceCallEvent("lock", "unlock", entity.entityId, null)); + ConnectionManager().callService("lock", "unlock", entity.entityId, null); } @override diff --git a/lib/entities/media_player/widgets/media_player_seek_bar.widget.dart b/lib/entities/media_player/widgets/media_player_seek_bar.widget.dart index 98e59bb..e3cf446 100644 --- a/lib/entities/media_player/widgets/media_player_seek_bar.widget.dart +++ b/lib/entities/media_player/widgets/media_player_seek_bar.widget.dart @@ -56,12 +56,12 @@ class _MediaPlayerSeekBarState extends State { color: Colors.orange, focusColor: Colors.white, onPressed: () { - eventBus.fire(ServiceCallEvent( + ConnectionManager().callService( "media_player", "media_seek", "${entity.entityId}", {"seek_position": _savedPosition} - )); + ); setState(() { _savedPosition = 0; }); @@ -103,12 +103,12 @@ class _MediaPlayerSeekBarState extends State { _seekStarted = false; Timer(Duration(milliseconds: 500), () { if (!_seekStarted) { - eventBus.fire(ServiceCallEvent( + ConnectionManager().callService( "media_player", "media_seek", "${entity.entityId}", {"seek_position": val} - )); + ); setState(() { _changedHere = true; _currentPosition = val; diff --git a/lib/entities/media_player/widgets/media_player_widgets.dart b/lib/entities/media_player/widgets/media_player_widgets.dart index 988093e..4fb5019 100644 --- a/lib/entities/media_player/widgets/media_player_widgets.dart +++ b/lib/entities/media_player/widgets/media_player_widgets.dart @@ -1,7 +1,7 @@ part of '../../../main.dart'; class MediaPlayerWidget extends StatelessWidget { - + @override Widget build(BuildContext context) { final EntityModel entityModel = EntityModel.of(context); @@ -121,23 +121,23 @@ class MediaPlayerPlaybackControls extends StatelessWidget { if (entity.state != EntityState.unavailable && entity.state != EntityState.unknown) { if (entity.state == EntityState.off) { Logger.d("${entity.entityId} turn_on"); - eventBus.fire(new ServiceCallEvent( + ConnectionManager().callService( entity.domain, "turn_on", entity.entityId, - null)); + null); } else { Logger.d("${entity.entityId} turn_off"); - eventBus.fire(new ServiceCallEvent( + ConnectionManager().callService( entity.domain, "turn_off", entity.entityId, - null)); + null); } } } void _callAction(MediaPlayerEntity entity, String action) { Logger.d("${entity.entityId} $action"); - eventBus.fire(new ServiceCallEvent( + ConnectionManager().callService( entity.domain, "$action", entity.entityId, - null)); + null); } @override @@ -264,27 +264,27 @@ class _MediaPlayerControlsState extends State { setState(() { _changedHere = true; _newVolumeLevel = value; - eventBus.fire(ServiceCallEvent("media_player", "volume_set", entityId, {"volume_level": value})); + ConnectionManager().callService("media_player", "volume_set", entityId, {"volume_level": value}); }); } void _setVolumeMute(bool isMuted, String entityId) { - eventBus.fire(ServiceCallEvent("media_player", "volume_mute", entityId, {"is_volume_muted": isMuted})); + ConnectionManager().callService("media_player", "volume_mute", entityId, {"is_volume_muted": isMuted}); } void _setVolumeUp(String entityId) { - eventBus.fire(ServiceCallEvent("media_player", "volume_up", entityId, null)); + ConnectionManager().callService("media_player", "volume_up", entityId, null); } void _setVolumeDown(String entityId) { - eventBus.fire(ServiceCallEvent("media_player", "volume_down", entityId, null)); + ConnectionManager().callService("media_player", "volume_down", entityId, null); } void _setSoundMode(String value, String entityId) { setState(() { _newSoundMode = value; _changedHere = true; - eventBus.fire(ServiceCallEvent("media_player", "select_sound_mode", entityId, {"sound_mode": "$value"})); + ConnectionManager().callService("media_player", "select_sound_mode", entityId, {"sound_mode": "$value"}); }); } @@ -292,7 +292,7 @@ class _MediaPlayerControlsState extends State { setState(() { _newSource = source; _changedHere = true; - eventBus.fire(ServiceCallEvent("media_player", "select_source", entityId, {"source": "$source"})); + ConnectionManager().callService("media_player", "select_source", entityId, {"source": "$source"}); }); } diff --git a/lib/entities/select/widgets/select_state.dart b/lib/entities/select/widgets/select_state.dart index aa482f0..8c6f40d 100644 --- a/lib/entities/select/widgets/select_state.dart +++ b/lib/entities/select/widgets/select_state.dart @@ -11,8 +11,8 @@ class SelectStateWidget extends StatefulWidget { class _SelectStateWidgetState extends State { void setNewState(domain, entityId, newValue) { - eventBus.fire(new ServiceCallEvent(domain, "select_option", entityId, - {"option": "$newValue"})); + ConnectionManager().callService(domain, "select_option", entityId, + {"option": "$newValue"}); } @override diff --git a/lib/entities/slider/widgets/slider_controls.dart b/lib/entities/slider/widgets/slider_controls.dart index 226c418..74faff9 100644 --- a/lib/entities/slider/widgets/slider_controls.dart +++ b/lib/entities/slider/widgets/slider_controls.dart @@ -18,8 +18,8 @@ class _SliderControlsWidgetState extends State { _newValue = newValue; _changedHere = true; }); - eventBus.fire(new ServiceCallEvent(domain, "set_value", entityId, - {"value": "${newValue.toString()}"})); + ConnectionManager().callService(domain, "set_value", entityId, + {"value": "${newValue.toString()}"}); } @override diff --git a/lib/entities/switch/widget/switch_state.dart b/lib/entities/switch/widget/switch_state.dart index 278e85a..2002e3f 100644 --- a/lib/entities/switch/widget/switch_state.dart +++ b/lib/entities/switch/widget/switch_state.dart @@ -38,8 +38,8 @@ class _SwitchStateWidgetState extends State { } else { domain = entity.domain; } - eventBus.fire(new ServiceCallEvent( - domain, (newValue as bool) ? "turn_on" : "turn_off", entity.entityId, null)); + ConnectionManager().callService( + domain, (newValue as bool) ? "turn_on" : "turn_off", entity.entityId, null); } @override diff --git a/lib/entities/text/widgets/text_input_state.dart b/lib/entities/text/widgets/text_input_state.dart index ee96377..9463f45 100644 --- a/lib/entities/text/widgets/text_input_state.dart +++ b/lib/entities/text/widgets/text_input_state.dart @@ -26,8 +26,8 @@ class _TextInputStateWidgetState extends State { void setNewState(newValue, domain, entityId) { if (validate(newValue, _minLength, _maxLength)) { - eventBus.fire(new ServiceCallEvent(domain, "set_value", entityId, - {"value": "$newValue"})); + ConnectionManager().callService(domain, "set_value", entityId, + {"value": "$newValue"}); } else { setState(() { _tmpValue = _entityState; diff --git a/lib/entities/vacuum/widgets/vacuum_controls.dart b/lib/entities/vacuum/widgets/vacuum_controls.dart index 1762b48..14212e4 100644 --- a/lib/entities/vacuum/widgets/vacuum_controls.dart +++ b/lib/entities/vacuum/widgets/vacuum_controls.dart @@ -77,9 +77,10 @@ class VacuumControls extends StatelessWidget { icon: Icon(MaterialDesignIcons.getIconDataFromIconName("mdi:play")), iconSize: iconSize, onPressed: () => ConnectionManager().callService( - domain: "vacuum", - entityId: entity.entityId, - service: "start" + "vacuum", + entity.entityId, + "start", + null ), ) ); @@ -90,9 +91,10 @@ class VacuumControls extends StatelessWidget { icon: Icon(MaterialDesignIcons.getIconDataFromIconName("mdi:play-pause")), iconSize: iconSize, onPressed: () => ConnectionManager().callService( - domain: "vacuum", - entityId: entity.entityId, - service: "start_pause" + "vacuum", + entity.entityId, + "start_pause", + null ), ) ); @@ -102,9 +104,10 @@ class VacuumControls extends StatelessWidget { icon: Icon(MaterialDesignIcons.getIconDataFromIconName("mdi:pause")), iconSize: iconSize, onPressed: () => ConnectionManager().callService( - domain: "vacuum", - entityId: entity.entityId, - service: "pause" + "vacuum", + entity.entityId, + "pause", + null ), ) ); @@ -115,9 +118,10 @@ class VacuumControls extends StatelessWidget { icon: Icon(MaterialDesignIcons.getIconDataFromIconName("mdi:stop")), iconSize: iconSize, onPressed: () => ConnectionManager().callService( - domain: "vacuum", - entityId: entity.entityId, - service: "stop" + "vacuum", + entity.entityId, + "stop", + null ), ) ); @@ -128,9 +132,10 @@ class VacuumControls extends StatelessWidget { icon: Icon(MaterialDesignIcons.getIconDataFromIconName("mdi:broom")), iconSize: iconSize, onPressed: () => ConnectionManager().callService( - domain: "vacuum", - entityId: entity.entityId, - service: "clean_spot" + "vacuum", + entity.entityId, + "clean_spot", + null ), ) ); @@ -141,9 +146,10 @@ class VacuumControls extends StatelessWidget { icon: Icon(MaterialDesignIcons.getIconDataFromIconName("mdi:map-marker")), iconSize: iconSize, onPressed: () => ConnectionManager().callService( - domain: "vacuum", - entityId: entity.entityId, - service: "locate" + "vacuum", + entity.entityId, + "locate", + null ), ) ); @@ -154,9 +160,10 @@ class VacuumControls extends StatelessWidget { icon: Icon(MaterialDesignIcons.getIconDataFromIconName("mdi:home-map-marker")), iconSize: iconSize, onPressed: () => ConnectionManager().callService( - domain: "vacuum", - entityId: entity.entityId, - service: "return_to_base" + "vacuum", + entity.entityId, + "return_to_base", + null ), ) ); @@ -194,10 +201,10 @@ class VacuumControls extends StatelessWidget { options: entity.fanSpeedList, value: entity.fanSpeed, onChange: (val) => ConnectionManager().callService( - domain: "vacuum", - entityId: entity.entityId, - service: "set_fan_speed", - additionalServiceData: {"fan_speed": val} + "vacuum", + entity.entityId, + "set_fan_speed", + {"fan_speed": val} ) ), ); diff --git a/lib/managers/connection_manager.class.dart b/lib/managers/connection_manager.class.dart index b09efdf..62e19c5 100644 --- a/lib/managers/connection_manager.class.dart +++ b/lib/managers/connection_manager.class.dart @@ -348,14 +348,15 @@ class ConnectionManager { _currentMessageId += 1; } - Future callService({String domain, String service, String entityId, Map additionalServiceData}) { + Future callService(String domain, String service, String entityId, Map serviceData) { + eventBus.fire(NotifyServiceCallEvent(domain, service, entityId)); Completer completer = Completer(); Map serviceData = {}; if (entityId != null) { serviceData["entity_id"] = entityId; } - if (additionalServiceData != null && additionalServiceData.isNotEmpty) { - serviceData.addAll(additionalServiceData); + if (serviceData != null && serviceData.isNotEmpty) { + serviceData.addAll(serviceData); } if (serviceData.isNotEmpty) sendHTTPPost( diff --git a/lib/managers/mobile_app_integration_manager.class.dart b/lib/managers/mobile_app_integration_manager.class.dart index e0b5e08..6b654e2 100644 --- a/lib/managers/mobile_app_integration_manager.class.dart +++ b/lib/managers/mobile_app_integration_manager.class.dart @@ -45,7 +45,7 @@ class MobileAppIntegrationManager { positiveText: "Restart now", negativeText: "Later", onPositive: () { - ConnectionManager().callService(domain: "homeassistant", service: "restart", entityId: null); + ConnectionManager().callService("homeassistant","restart", null, null); }, )); }); diff --git a/lib/pages/integration_settings.page.dart b/lib/pages/integration_settings.page.dart index e6656b0..c3c3ba4 100644 --- a/lib/pages/integration_settings.page.dart +++ b/lib/pages/integration_settings.page.dart @@ -56,7 +56,7 @@ class _IntegrationSettingsPageState extends State { positiveText: "Sure. Make it so", negativeText: "What?? No!", onPositive: () { - ConnectionManager().callService(domain: "homeassistant", service: "restart", entityId: null); + ConnectionManager().callService("homeassistant", "restart", null, null); }, )); } @@ -68,7 +68,7 @@ class _IntegrationSettingsPageState extends State { positiveText: "Sure. Make it so", negativeText: "What?? No!", onPositive: () { - ConnectionManager().callService(domain: "homeassistant", service: "stop", entityId: null); + ConnectionManager().callService("homeassistant","stop", null, null); }, )); } diff --git a/lib/pages/main.page.dart b/lib/pages/main.page.dart index e9c7f8d..8448e58 100644 --- a/lib/pages/main.page.dart +++ b/lib/pages/main.page.dart @@ -26,7 +26,7 @@ class _MainPageState extends ReceiveShareState with WidgetsBindingObse bool _showLoginButton = false; bool _preventAppRefresh = false; String _savedSharedText; - String _entityToShow; + Entity _entityToShow; @override void initState() { @@ -218,9 +218,8 @@ class _MainPageState extends ReceiveShareState with WidgetsBindingObse } if (_serviceCallSubscription == null) { _serviceCallSubscription = - eventBus.on().listen((event) { - _callService(event.domain, event.service, event.entityId, - event.additionalParams); + eventBus.on().listen((event) { + _notifyServiceCalled(event.domain, event.service, event.entityId); }); } @@ -318,27 +317,28 @@ class _MainPageState extends ReceiveShareState with WidgetsBindingObse ); } - //TODO remove this shit.... maybe - void _callService(String domain, String service, String entityId, Map additionalParams) { + void _notifyServiceCalled(String domain, String service, String entityId) { _showInfoBottomBar( message: "Calling $domain.$service", - duration: Duration(seconds: 3) + duration: Duration(seconds: 4) ); - ConnectionManager().callService(domain: domain, service: service, entityId: entityId, additionalServiceData: additionalParams).catchError((e) => _setErrorState(e)); } void _showEntityPage(String entityId) { setState(() { - _entityToShow = entityId; + _entityToShow = HomeAssistant().entities.get(entityId); + if (_entityToShow != null) { + _mainScrollController?.jumpTo(0); + } }); - if (_entityToShow!= null && MediaQuery.of(context).size.width < Sizes.tabletMinWidth) { + /*if (_entityToShow!= null && MediaQuery.of(context).size.width < Sizes.tabletMinWidth) { Navigator.push( context, MaterialPageRoute( builder: (context) => EntityViewPage(entityId: entityId), ) ); - } + }*/ } void _showPage(String path, bool goBackFirst) { @@ -631,6 +631,7 @@ class _MainPageState extends ReceiveShareState with WidgetsBindingObse } final GlobalKey _scaffoldKey = new GlobalKey(); + final ScrollController _mainScrollController = ScrollController(); Widget _buildScaffoldBody(bool empty) { List> serviceMenuItems = []; @@ -725,7 +726,6 @@ class _MainPageState extends ReceiveShareState with WidgetsBindingObse } } else { if (_entityToShow != null && MediaQuery.of(context).size.width >= Sizes.tabletMinWidth) { - Entity entity = HomeAssistant().entities.get(_entityToShow); mainScrollBody = Flex( direction: Axis.horizontal, children: [ @@ -738,12 +738,13 @@ class _MainPageState extends ReceiveShareState with WidgetsBindingObse ), ConstrainedBox( constraints: BoxConstraints.tightFor(width: Sizes.entityPageMaxWidth), - child: EntityPageLayout(entity: entity, showClose: true,), + child: EntityPageLayout(entity: _entityToShow, showClose: true,), ) ], ); + } else if (_entityToShow != null) { + mainScrollBody = EntityPageLayout(entity: _entityToShow, showClose: true,); } else { - _entityToShow = null; mainScrollBody = HomeAssistant().buildViews(context, _viewsTabController); } } @@ -799,7 +800,7 @@ class _MainPageState extends ReceiveShareState with WidgetsBindingObse _scaffoldKey.currentState.openDrawer(); }, ), - bottom: empty ? null : TabBar( + bottom: (empty || _entityToShow != null) ? null : TabBar( controller: _viewsTabController, tabs: buildUIViewTabs(), isScrollable: true, @@ -808,7 +809,8 @@ class _MainPageState extends ReceiveShareState with WidgetsBindingObse ]; }, - body: mainScrollBody + body: mainScrollBody, + controller: _mainScrollController, ); } @@ -874,12 +876,24 @@ class _MainPageState extends ReceiveShareState with WidgetsBindingObse body: _buildScaffoldBody(true) ); } else { - return Scaffold( - key: _scaffoldKey, - drawer: _buildAppDrawer(), - primary: false, - bottomNavigationBar: bottomBar, - body: _buildScaffoldBody(false) + return WillPopScope( + child: Scaffold( + key: _scaffoldKey, + drawer: _buildAppDrawer(), + primary: false, + bottomNavigationBar: bottomBar, + body: _buildScaffoldBody(false) + ), + onWillPop: () { + if (_entityToShow != null) { + setState(() { + _entityToShow = null; + }); + return Future.value(false); + } else { + return Future.value(true); + } + }, ); } } diff --git a/lib/pages/play_media.page.dart b/lib/pages/play_media.page.dart index d53bb65..1349f68 100644 --- a/lib/pages/play_media.page.dart +++ b/lib/pages/play_media.page.dart @@ -89,17 +89,22 @@ class _PlayMediaPageState extends State { } Navigator.pop(context); ConnectionManager().callService( - domain: serviceDomain, - entityId: entity.entityId, - service: "play_media", - additionalServiceData: { + serviceDomain, + entity.entityId, + "play_media", + { "media_content_id": _mediaUrl, "media_content_type": _contentType } ); HomeAssistant().sendToPlayerId = entity.entityId; if (HomeAssistant().sendFromPlayerId != null && HomeAssistant().sendFromPlayerId != HomeAssistant().sendToPlayerId) { - eventBus.fire(ServiceCallEvent(HomeAssistant().sendFromPlayerId.split(".")[0], "turn_off", HomeAssistant().sendFromPlayerId, null)); + ConnectionManager().callService( + HomeAssistant().sendFromPlayerId.split(".")[0], + "turn_off", + HomeAssistant().sendFromPlayerId, + null + ); HomeAssistant().sendFromPlayerId = null; } eventBus.fire(ShowEntityPageEvent(entity: entity)); @@ -241,5 +246,5 @@ class _PlayMediaPageState extends State { _refreshDataSubscription?.cancel(); super.dispose(); } - + } \ No newline at end of file diff --git a/lib/types/event_bus_events.dart b/lib/types/event_bus_events.dart index 85f3044..1181967 100644 --- a/lib/types/event_bus_events.dart +++ b/lib/types/event_bus_events.dart @@ -33,13 +33,12 @@ class StartAuthEvent { StartAuthEvent(this.oauthUrl, this.showButton); } -class ServiceCallEvent { +class NotifyServiceCallEvent { String domain; String service; String entityId; - Map additionalParams; - ServiceCallEvent(this.domain, this.service, this.entityId, this.additionalParams); + NotifyServiceCallEvent(this.domain, this.service, this.entityId); } class ShowPopupDialogEvent {