From 87f89b63e1e0f2eb7fd65c06883bb4eef3c1adb0 Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Wed, 14 Nov 2018 15:14:46 +0200 Subject: [PATCH] State const --- lib/entity_class/climate_entity.class.dart | 2 +- lib/entity_class/const.dart | 31 +++++++++++ lib/entity_class/cover_entity.class.dart | 4 +- lib/entity_class/entity.class.dart | 6 +-- lib/entity_widgets/badge.dart | 4 +- .../controls/light_controls.dart | 2 +- .../controls/media_player_widget.dart | 54 ++++++++++++++++--- lib/entity_widgets/entity_colors.class.dart | 28 ++++++---- lib/entity_widgets/entity_icon.dart | 2 +- .../history_chart/combined_history_chart.dart | 4 +- .../history_chart/history_control_widget.dart | 2 +- .../numeric_state_history_chart.dart | 2 +- .../simple_state_history_chart.dart | 6 +-- lib/entity_widgets/state/switch_state.dart | 10 ++-- lib/main.dart | 1 + 15 files changed, 116 insertions(+), 42 deletions(-) create mode 100644 lib/entity_class/const.dart diff --git a/lib/entity_class/climate_entity.class.dart b/lib/entity_class/climate_entity.class.dart index 6b8837a..929b7f1 100644 --- a/lib/entity_class/climate_entity.class.dart +++ b/lib/entity_class/climate_entity.class.dart @@ -84,7 +84,7 @@ class ClimateEntity extends Entity { String get fanMode => attributes['fan_mode']; String get swingMode => attributes['swing_mode']; bool get awayMode => attributes['away_mode'] == "on"; - bool get isOff => state == "off"; + bool get isOff => state == EntityState.off; bool get auxHeat => attributes['aux_heat'] == "on"; ClimateEntity(Map rawData) : super(rawData); diff --git a/lib/entity_class/const.dart b/lib/entity_class/const.dart new file mode 100644 index 0000000..5edc18e --- /dev/null +++ b/lib/entity_class/const.dart @@ -0,0 +1,31 @@ +part of '../main.dart'; + +class EntityState { + static const on = 'on'; + static const off = 'off'; + static const home = 'home'; + static const not_home = 'not_home'; + static const unknown = 'unknown'; + static const open = 'open'; + static const opening = 'opening'; + static const closed = 'closed'; + static const closing = 'closing'; + static const playing = 'playing'; + static const paused = 'paused'; + static const idle = 'idle'; + static const standby = 'standby'; + static const alarm_disarmed = 'disarmed'; + static const alarm_armed_home = 'armed_home'; + static const alarm_armed_away = 'armed_away'; + static const alarm_armed_night = 'armed_night'; + static const alarm_armed_custom_bypass = 'armed_custom_bypass'; + static const alarm_pending = 'pending'; + static const alarm_arming = 'arming'; + static const alarm_disarming = 'disarming'; + static const alarm_triggered = 'triggered'; + static const locked = 'locked'; + static const unlocked = 'unlocked'; + static const unavailable = 'unavailable'; + static const ok = 'ok'; + static const problem = 'problem'; +} \ No newline at end of file diff --git a/lib/entity_class/cover_entity.class.dart b/lib/entity_class/cover_entity.class.dart index b791f2c..6b6b544 100644 --- a/lib/entity_class/cover_entity.class.dart +++ b/lib/entity_class/cover_entity.class.dart @@ -40,8 +40,8 @@ class CoverEntity extends Entity { double get currentPosition => _getDoubleAttributeValue('current_position'); double get currentTiltPosition => _getDoubleAttributeValue('current_tilt_position'); - bool get canBeOpened => ((state != "opening") && (state != "open")) || (state == "open" && currentPosition != null && currentPosition > 0.0 && currentPosition < 100.0); - bool get canBeClosed => ((state != "closing") && (state != "closed")); + bool get canBeOpened => ((state != EntityState.opening) && (state != EntityState.open)) || (state == EntityState.open && currentPosition != null && currentPosition > 0.0 && currentPosition < 100.0); + bool get canBeClosed => ((state != EntityState.closing) && (state != EntityState.closed)); bool get canTiltBeOpened => currentTiltPosition < 100; bool get canTiltBeClosed => currentTiltPosition > 0; diff --git a/lib/entity_class/entity.class.dart b/lib/entity_class/entity.class.dart index 845c71e..dc78763 100644 --- a/lib/entity_class/entity.class.dart +++ b/lib/entity_class/entity.class.dart @@ -2,10 +2,6 @@ part of '../main.dart'; class Entity { - static const badgeColors = { - "default": Color.fromRGBO(223, 76, 30, 1.0), - "binary_sensor": Color.fromRGBO(3, 155, 229, 1.0) - }; static List badgeDomains = [ "alarm_control_panel", "binary_sensor", @@ -39,7 +35,7 @@ class Entity { bool get isGroup => domain == "group"; bool get isBadge => Entity.badgeDomains.contains(domain); String get icon => attributes["icon"] ?? ""; - bool get isOn => state == "on"; + bool get isOn => state == EntityState.on; String get entityPicture => attributes["entity_picture"]; String get unitOfMeasurement => attributes["unit_of_measurement"] ?? ""; List get childEntityIds => attributes["entity_id"] ?? []; diff --git a/lib/entity_widgets/badge.dart b/lib/entity_widgets/badge.dart index 3d7dcf2..d4bfd89 100644 --- a/lib/entity_widgets/badge.dart +++ b/lib/entity_widgets/badge.dart @@ -7,8 +7,8 @@ class BadgeWidget extends StatelessWidget { double iconSize = 26.0; Widget badgeIcon; String onBadgeTextValue; - Color iconColor = Entity.badgeColors[entityModel.entity.domain] ?? - Entity.badgeColors["default"]; + Color iconColor = EntityColor.badgeColors[entityModel.entity.domain] ?? + EntityColor.badgeColors["default"]; switch (entityModel.entity.domain) { case "sun": { diff --git a/lib/entity_widgets/controls/light_controls.dart b/lib/entity_widgets/controls/light_controls.dart index 753ae08..c73b52c 100644 --- a/lib/entity_widgets/controls/light_controls.dart +++ b/lib/entity_widgets/controls/light_controls.dart @@ -98,7 +98,7 @@ class _LightControlsWidgetState extends State { } Widget _buildBrightnessControl(LightEntity entity) { - if ((entity.supportBrightness) && (_tmpBrightness != null) && (entity.state != "unavailable")) { + if ((entity.supportBrightness) && (_tmpBrightness != null) && (entity.state != EntityState.unavailable)) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ diff --git a/lib/entity_widgets/controls/media_player_widget.dart b/lib/entity_widgets/controls/media_player_widget.dart index 12e93f8..cc03ac6 100644 --- a/lib/entity_widgets/controls/media_player_widget.dart +++ b/lib/entity_widgets/controls/media_player_widget.dart @@ -3,7 +3,17 @@ part of '../../main.dart'; class MediaPlayerWidget extends StatelessWidget { void _setPower(MediaPlayerEntity entity) { - TheLogger.debug('WAT?'); + if (entity.state != EntityState.unavailable && entity.state != EntityState.unknown) { + if (entity.state == EntityState.off) { + TheLogger.debug("${entity.entityId} turn_on"); + } else { + TheLogger.debug("${entity.entityId} turn_off"); + } + } + } + + void _callAction(MediaPlayerEntity entity, String action) { + TheLogger.debug("${entity.entityId} $action"); } @override @@ -40,7 +50,7 @@ class MediaPlayerWidget extends StatelessWidget { Widget _buildControls(MediaPlayerEntity entity) { List result = []; - if (entity.supportTurnOn) { + if (entity.supportTurnOn || entity.supportTurnOff) { result.add( IconButton( icon: Icon(Icons.power_settings_new), @@ -49,6 +59,36 @@ class MediaPlayerWidget extends StatelessWidget { ) ); } + if (entity.supportPreviousTrack) { + result.add( + IconButton( + icon: Icon(Icons.skip_previous), + onPressed: () => _callAction(entity, "media_previous_track"), + iconSize: Sizes.iconSize, + ) + ); + } + if (entity.supportPlay || entity.supportPause) { + if (entity.state == EntityState.playing) { + result.add( + IconButton( + icon: Icon(Icons.pause_circle_outline), + onPressed: () => _callAction(entity, "media_pause"), + iconSize: Sizes.iconSize*1.5, + ) + ); + } //else if (entity.state == '') + + } + if (entity.supportNextTrack) { + result.add( + IconButton( + icon: Icon(Icons.skip_next), + onPressed: () => _callAction(entity, "media_next_track"), + iconSize: Sizes.iconSize, + ) + ); + } return Row( children: result, mainAxisAlignment: MainAxisAlignment.center, @@ -65,7 +105,7 @@ class MediaPlayerWidget extends StatelessWidget { List states = []; states.add(Text("${entity.displayName}", style: style)); String state = entity.state; - if (state == null || state == "off" || state == "unavailable" || state == "idle") { + if (state == null || state == EntityState.off || state == EntityState.unavailable || state == EntityState.idle) { states.add(Text("${entity.state}", style: style.apply(fontSizeDelta: 4.0),)); } if (entity.attributes['media_title'] != null) { @@ -87,7 +127,7 @@ class MediaPlayerWidget extends StatelessWidget { Widget _buildImage(MediaPlayerEntity entity) { String state = entity.state; - if (homeAssistantWebHost != null && entity.entityPicture != null && state != "off" && state != "unavailable" && state != "idle") { + if (homeAssistantWebHost != null && entity.entityPicture != null && state != EntityState.off && state != EntityState.unavailable && state != EntityState.idle) { return Container( color: Colors.black, child: Row( @@ -109,7 +149,7 @@ class MediaPlayerWidget extends StatelessWidget { Icon( MaterialDesignIcons.createIconDataFromIconName("mdi:movie"), size: 150.0, - color: EntityColors.stateColor("$state"), + color: EntityColor.stateColor("$state"), ) ], ); @@ -141,7 +181,7 @@ class _MediaPlayerProgressWidgetState extends State { Duration duration = Duration(seconds: entity._getIntAttributeValue("media_duration") ?? 1); Duration position = Duration(seconds: entity._getIntAttributeValue("media_position") ?? 0); int currentPosition = position.inSeconds; - if (entity.state == "playing") { + if (entity.state == EntityState.playing) { _timer?.cancel(); _timer = Timer(Duration(seconds: 1), () { setState(() { @@ -159,7 +199,7 @@ class _MediaPlayerProgressWidgetState extends State { return LinearProgressIndicator( value: progress, backgroundColor: Colors.black45, - valueColor: AlwaysStoppedAnimation(EntityColors.stateColor("on")), + valueColor: AlwaysStoppedAnimation(EntityColor.stateColor(EntityState.on)), ); } catch (e) { _timer?.cancel(); diff --git a/lib/entity_widgets/entity_colors.class.dart b/lib/entity_widgets/entity_colors.class.dart index 0bd727d..ce085af 100644 --- a/lib/entity_widgets/entity_colors.class.dart +++ b/lib/entity_widgets/entity_colors.class.dart @@ -1,22 +1,28 @@ part of '../main.dart'; -class EntityColors { +class EntityColor { + + static const badgeColors = { + "default": Color.fromRGBO(223, 76, 30, 1.0), + "binary_sensor": Color.fromRGBO(3, 155, 229, 1.0) + }; + static const _stateColors = { - "on": Colors.amber, + EntityState.on: Colors.amber, "auto": Colors.amber, - "idle": Colors.amber, - "playing": Colors.amber, + EntityState.idle: Colors.amber, + EntityState.playing: Colors.amber, "above_horizon": Colors.amber, - "home": Colors.amber, - "open": Colors.amber, - "off": Color.fromRGBO(68, 115, 158, 1.0), - "closed": Color.fromRGBO(68, 115, 158, 1.0), + EntityState.home: Colors.amber, + EntityState.open: Colors.amber, + EntityState.off: Color.fromRGBO(68, 115, 158, 1.0), + EntityState.closed: Color.fromRGBO(68, 115, 158, 1.0), "below_horizon": Color.fromRGBO(68, 115, 158, 1.0), "default": Color.fromRGBO(68, 115, 158, 1.0), "heat": Colors.redAccent, "cool": Colors.lightBlue, - "unavailable": Colors.black26, - "unknown": Colors.black26, + EntityState.unavailable: Colors.black26, + EntityState.unknown: Colors.black26, }; static Color stateColor(String state) { @@ -48,7 +54,7 @@ class EntityColors { charts.Color c1 = charts.MaterialPalette.getOrderedPalettes(10)[r.round()].shadeDefault; return Color.fromARGB(c1.a, c1.r, c1.g, c1.b); } else { - return _stateColors["on"]; + return _stateColors[EntityState.on]; } } } diff --git a/lib/entity_widgets/entity_icon.dart b/lib/entity_widgets/entity_icon.dart index dba3c7c..991ef70 100644 --- a/lib/entity_widgets/entity_icon.dart +++ b/lib/entity_widgets/entity_icon.dart @@ -11,7 +11,7 @@ class EntityIcon extends StatelessWidget { child: MaterialDesignIcons.createIconWidgetFromEntityData( entityModel.entity, Sizes.iconSize, - EntityColors.stateColor(entityModel.entity.state) + EntityColor.stateColor(entityModel.entity.state) ), ), onTap: () => entityModel.handleTap diff --git a/lib/entity_widgets/history_chart/combined_history_chart.dart b/lib/entity_widgets/history_chart/combined_history_chart.dart index b199442..628cf98 100644 --- a/lib/entity_widgets/history_chart/combined_history_chart.dart +++ b/lib/entity_widgets/history_chart/combined_history_chart.dart @@ -156,7 +156,7 @@ class _CombinedHistoryChartWidgetState extends State result.add( new charts.Series( id: "value", - colorFn: (EntityHistoryMoment historyMoment, __) => EntityColors.chartHistoryStateColor("_", historyMoment.colorId), + colorFn: (EntityHistoryMoment historyMoment, __) => EntityColor.chartHistoryStateColor("_", historyMoment.colorId), radiusPxFn: (EntityHistoryMoment historyMoment, __) { if (historyMoment.hiddenDot) { return 0.0; @@ -179,7 +179,7 @@ class _CombinedHistoryChartWidgetState extends State new charts.Series( id: 'state', radiusPxFn: (EntityHistoryMoment historyMoment, __) => (historyMoment.id == _selectedId) ? 5.0 : 4.0, - colorFn: (EntityHistoryMoment historyMoment, __) => EntityColors.chartHistoryStateColor(historyMoment.state, historyMoment.colorId), + colorFn: (EntityHistoryMoment historyMoment, __) => EntityColor.chartHistoryStateColor(historyMoment.state, historyMoment.colorId), domainFn: (EntityHistoryMoment historyMoment, _) => historyMoment.startTime, domainLowerBoundFn: (EntityHistoryMoment historyMoment, _) => historyMoment.startTime, domainUpperBoundFn: (EntityHistoryMoment historyMoment, _) => historyMoment.endTime ?? DateTime.now(), diff --git a/lib/entity_widgets/history_chart/history_control_widget.dart b/lib/entity_widgets/history_chart/history_control_widget.dart index e79984d..c8fbb18 100644 --- a/lib/entity_widgets/history_chart/history_control_widget.dart +++ b/lib/entity_widgets/history_chart/history_control_widget.dart @@ -55,7 +55,7 @@ class HistoryControlWidget extends StatelessWidget { textAlign: TextAlign.right, style: TextStyle( fontWeight: FontWeight.bold, - color: EntityColors.historyStateColor(selectedStates[i], colorIndexes[i]), + color: EntityColor.historyStateColor(selectedStates[i], colorIndexes[i]), fontSize: 22.0 ), ) diff --git a/lib/entity_widgets/history_chart/numeric_state_history_chart.dart b/lib/entity_widgets/history_chart/numeric_state_history_chart.dart index 3ab4549..8e6aaab 100644 --- a/lib/entity_widgets/history_chart/numeric_state_history_chart.dart +++ b/lib/entity_widgets/history_chart/numeric_state_history_chart.dart @@ -108,7 +108,7 @@ class _NumericStateHistoryChartWidgetState extends State( id: 'State', - colorFn: (EntityHistoryMoment historyMoment, __) => EntityColors.chartHistoryStateColor("on", -1), + colorFn: (EntityHistoryMoment historyMoment, __) => EntityColor.chartHistoryStateColor(EntityState.on, -1), domainFn: (EntityHistoryMoment historyMoment, _) => historyMoment.startTime, measureFn: (EntityHistoryMoment historyMoment, _) => historyMoment.value ?? historyMoment.previousValue, data: data, diff --git a/lib/entity_widgets/history_chart/simple_state_history_chart.dart b/lib/entity_widgets/history_chart/simple_state_history_chart.dart index 4152430..3868fb7 100644 --- a/lib/entity_widgets/history_chart/simple_state_history_chart.dart +++ b/lib/entity_widgets/history_chart/simple_state_history_chart.dart @@ -107,7 +107,7 @@ class _SimpleStateHistoryChartWidgetState extends State( id: 'State', strokeWidthPxFn: (EntityHistoryMoment historyMoment, __) => (historyMoment.id == _selectedId) ? 6.0 : 3.0, - colorFn: (EntityHistoryMoment historyMoment, __) => EntityColors.chartHistoryStateColor(historyMoment.state, historyMoment.colorId), + colorFn: (EntityHistoryMoment historyMoment, __) => EntityColor.chartHistoryStateColor(historyMoment.state, historyMoment.colorId), domainFn: (EntityHistoryMoment historyMoment, _) => historyMoment.startTime, measureFn: (EntityHistoryMoment historyMoment, _) => 10, data: data, @@ -115,7 +115,7 @@ class _SimpleStateHistoryChartWidgetState extends State( id: 'State', radiusPxFn: (EntityHistoryMoment historyMoment, __) => (historyMoment.id == _selectedId) ? 5.0 : 3.0, - colorFn: (EntityHistoryMoment historyMoment, __) => EntityColors.chartHistoryStateColor(historyMoment.state, historyMoment.colorId), + colorFn: (EntityHistoryMoment historyMoment, __) => EntityColor.chartHistoryStateColor(historyMoment.state, historyMoment.colorId), domainFn: (EntityHistoryMoment historyMoment, _) => historyMoment.startTime, measureFn: (EntityHistoryMoment historyMoment, _) => 10, data: data, @@ -123,7 +123,7 @@ class _SimpleStateHistoryChartWidgetState extends State( id: 'State', radiusPxFn: (EntityHistoryMoment historyMoment, __) => (historyMoment.id == _selectedId) ? 5.0 : 3.0, - colorFn: (EntityHistoryMoment historyMoment, __) => EntityColors.chartHistoryStateColor(historyMoment.state, historyMoment.colorId), + colorFn: (EntityHistoryMoment historyMoment, __) => EntityColor.chartHistoryStateColor(historyMoment.state, historyMoment.colorId), domainFn: (EntityHistoryMoment historyMoment, _) => historyMoment.endTime ?? DateTime.now(), measureFn: (EntityHistoryMoment historyMoment, _) => 10, data: data, diff --git a/lib/entity_widgets/state/switch_state.dart b/lib/entity_widgets/state/switch_state.dart index 3423c25..d7bd227 100644 --- a/lib/entity_widgets/state/switch_state.dart +++ b/lib/entity_widgets/state/switch_state.dart @@ -14,7 +14,7 @@ class _SwitchStateWidgetState extends State { void _setNewState(newValue, Entity entity) { setState(() { - entity.assumedState = newValue ? 'on' : 'off'; + entity.assumedState = newValue ? EntityState.on : EntityState.off; }); Timer(Duration(seconds: 2), (){ setState(() { @@ -30,13 +30,13 @@ class _SwitchStateWidgetState extends State { final entityModel = EntityModel.of(context); final entity = entityModel.entity; Widget result; - if (entity.state == "unavailable") { + if (entity.state == EntityState.unavailable || entity.state == EntityState.unknown) { return SimpleEntityState(); } else if ((entity.attributes["assumed_state"] == null) || (entity.attributes["assumed_state"] == false)) { return SizedBox( height: 32.0, child: Switch( - value: entity.assumedState == 'on', + value: entity.assumedState == EntityState.on, onChanged: ((switchState) { _setNewState(switchState, entity); }), @@ -51,13 +51,13 @@ class _SwitchStateWidgetState extends State { IconButton( onPressed: () => _setNewState(false, entity), icon: Icon(MaterialDesignIcons.createIconDataFromIconName("mdi:flash-off")), - color: entity.assumedState == 'on' ? Colors.black : Colors.blue, + color: entity.assumedState == EntityState.on ? Colors.black : Colors.blue, iconSize: Sizes.iconSize, ), IconButton( onPressed: () => _setNewState(true, entity), icon: Icon(MaterialDesignIcons.createIconDataFromIconName("mdi:flash")), - color: entity.assumedState == 'on' ? Colors.blue : Colors.black, + color: entity.assumedState == EntityState.on ? Colors.blue : Colors.black, iconSize: Sizes.iconSize ) ], diff --git a/lib/main.dart b/lib/main.dart index 2a8024a..e2ee1d8 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -15,6 +15,7 @@ import 'package:http/http.dart' as http; import 'package:flutter_colorpicker/material_picker.dart'; import 'package:charts_flutter/flutter.dart' as charts; +part 'entity_class/const.dart'; part 'entity_class/entity.class.dart'; part 'entity_class/switch_entity.class.dart'; part 'entity_class/button_entity.class.dart';