State const

This commit is contained in:
Yegor Vialov 2018-11-14 15:14:46 +02:00
parent 3190b45db3
commit 87f89b63e1
15 changed files with 116 additions and 42 deletions

View File

@ -84,7 +84,7 @@ class ClimateEntity extends Entity {
String get fanMode => attributes['fan_mode']; String get fanMode => attributes['fan_mode'];
String get swingMode => attributes['swing_mode']; String get swingMode => attributes['swing_mode'];
bool get awayMode => attributes['away_mode'] == "on"; 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"; bool get auxHeat => attributes['aux_heat'] == "on";
ClimateEntity(Map rawData) : super(rawData); ClimateEntity(Map rawData) : super(rawData);

View File

@ -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';
}

View File

@ -40,8 +40,8 @@ class CoverEntity extends Entity {
double get currentPosition => _getDoubleAttributeValue('current_position'); double get currentPosition => _getDoubleAttributeValue('current_position');
double get currentTiltPosition => _getDoubleAttributeValue('current_tilt_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 canBeOpened => ((state != EntityState.opening) && (state != EntityState.open)) || (state == EntityState.open && currentPosition != null && currentPosition > 0.0 && currentPosition < 100.0);
bool get canBeClosed => ((state != "closing") && (state != "closed")); bool get canBeClosed => ((state != EntityState.closing) && (state != EntityState.closed));
bool get canTiltBeOpened => currentTiltPosition < 100; bool get canTiltBeOpened => currentTiltPosition < 100;
bool get canTiltBeClosed => currentTiltPosition > 0; bool get canTiltBeClosed => currentTiltPosition > 0;

View File

@ -2,10 +2,6 @@ part of '../main.dart';
class Entity { 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 = [ static List badgeDomains = [
"alarm_control_panel", "alarm_control_panel",
"binary_sensor", "binary_sensor",
@ -39,7 +35,7 @@ class Entity {
bool get isGroup => domain == "group"; bool get isGroup => domain == "group";
bool get isBadge => Entity.badgeDomains.contains(domain); bool get isBadge => Entity.badgeDomains.contains(domain);
String get icon => attributes["icon"] ?? ""; String get icon => attributes["icon"] ?? "";
bool get isOn => state == "on"; bool get isOn => state == EntityState.on;
String get entityPicture => attributes["entity_picture"]; String get entityPicture => attributes["entity_picture"];
String get unitOfMeasurement => attributes["unit_of_measurement"] ?? ""; String get unitOfMeasurement => attributes["unit_of_measurement"] ?? "";
List get childEntityIds => attributes["entity_id"] ?? []; List get childEntityIds => attributes["entity_id"] ?? [];

View File

@ -7,8 +7,8 @@ class BadgeWidget extends StatelessWidget {
double iconSize = 26.0; double iconSize = 26.0;
Widget badgeIcon; Widget badgeIcon;
String onBadgeTextValue; String onBadgeTextValue;
Color iconColor = Entity.badgeColors[entityModel.entity.domain] ?? Color iconColor = EntityColor.badgeColors[entityModel.entity.domain] ??
Entity.badgeColors["default"]; EntityColor.badgeColors["default"];
switch (entityModel.entity.domain) { switch (entityModel.entity.domain) {
case "sun": case "sun":
{ {

View File

@ -98,7 +98,7 @@ class _LightControlsWidgetState extends State<LightControlsWidget> {
} }
Widget _buildBrightnessControl(LightEntity entity) { Widget _buildBrightnessControl(LightEntity entity) {
if ((entity.supportBrightness) && (_tmpBrightness != null) && (entity.state != "unavailable")) { if ((entity.supportBrightness) && (_tmpBrightness != null) && (entity.state != EntityState.unavailable)) {
return Column( return Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[ children: <Widget>[

View File

@ -3,7 +3,17 @@ part of '../../main.dart';
class MediaPlayerWidget extends StatelessWidget { class MediaPlayerWidget extends StatelessWidget {
void _setPower(MediaPlayerEntity entity) { 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 @override
@ -40,7 +50,7 @@ class MediaPlayerWidget extends StatelessWidget {
Widget _buildControls(MediaPlayerEntity entity) { Widget _buildControls(MediaPlayerEntity entity) {
List<Widget> result = []; List<Widget> result = [];
if (entity.supportTurnOn) { if (entity.supportTurnOn || entity.supportTurnOff) {
result.add( result.add(
IconButton( IconButton(
icon: Icon(Icons.power_settings_new), 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( return Row(
children: result, children: result,
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
@ -65,7 +105,7 @@ class MediaPlayerWidget extends StatelessWidget {
List<Widget> states = []; List<Widget> states = [];
states.add(Text("${entity.displayName}", style: style)); states.add(Text("${entity.displayName}", style: style));
String state = entity.state; 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),)); states.add(Text("${entity.state}", style: style.apply(fontSizeDelta: 4.0),));
} }
if (entity.attributes['media_title'] != null) { if (entity.attributes['media_title'] != null) {
@ -87,7 +127,7 @@ class MediaPlayerWidget extends StatelessWidget {
Widget _buildImage(MediaPlayerEntity entity) { Widget _buildImage(MediaPlayerEntity entity) {
String state = entity.state; 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( return Container(
color: Colors.black, color: Colors.black,
child: Row( child: Row(
@ -109,7 +149,7 @@ class MediaPlayerWidget extends StatelessWidget {
Icon( Icon(
MaterialDesignIcons.createIconDataFromIconName("mdi:movie"), MaterialDesignIcons.createIconDataFromIconName("mdi:movie"),
size: 150.0, size: 150.0,
color: EntityColors.stateColor("$state"), color: EntityColor.stateColor("$state"),
) )
], ],
); );
@ -141,7 +181,7 @@ class _MediaPlayerProgressWidgetState extends State<MediaPlayerProgressWidget> {
Duration duration = Duration(seconds: entity._getIntAttributeValue("media_duration") ?? 1); Duration duration = Duration(seconds: entity._getIntAttributeValue("media_duration") ?? 1);
Duration position = Duration(seconds: entity._getIntAttributeValue("media_position") ?? 0); Duration position = Duration(seconds: entity._getIntAttributeValue("media_position") ?? 0);
int currentPosition = position.inSeconds; int currentPosition = position.inSeconds;
if (entity.state == "playing") { if (entity.state == EntityState.playing) {
_timer?.cancel(); _timer?.cancel();
_timer = Timer(Duration(seconds: 1), () { _timer = Timer(Duration(seconds: 1), () {
setState(() { setState(() {
@ -159,7 +199,7 @@ class _MediaPlayerProgressWidgetState extends State<MediaPlayerProgressWidget> {
return LinearProgressIndicator( return LinearProgressIndicator(
value: progress, value: progress,
backgroundColor: Colors.black45, backgroundColor: Colors.black45,
valueColor: AlwaysStoppedAnimation<Color>(EntityColors.stateColor("on")), valueColor: AlwaysStoppedAnimation<Color>(EntityColor.stateColor(EntityState.on)),
); );
} catch (e) { } catch (e) {
_timer?.cancel(); _timer?.cancel();

View File

@ -1,22 +1,28 @@
part of '../main.dart'; 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 = { static const _stateColors = {
"on": Colors.amber, EntityState.on: Colors.amber,
"auto": Colors.amber, "auto": Colors.amber,
"idle": Colors.amber, EntityState.idle: Colors.amber,
"playing": Colors.amber, EntityState.playing: Colors.amber,
"above_horizon": Colors.amber, "above_horizon": Colors.amber,
"home": Colors.amber, EntityState.home: Colors.amber,
"open": Colors.amber, EntityState.open: Colors.amber,
"off": Color.fromRGBO(68, 115, 158, 1.0), EntityState.off: Color.fromRGBO(68, 115, 158, 1.0),
"closed": 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), "below_horizon": Color.fromRGBO(68, 115, 158, 1.0),
"default": Color.fromRGBO(68, 115, 158, 1.0), "default": Color.fromRGBO(68, 115, 158, 1.0),
"heat": Colors.redAccent, "heat": Colors.redAccent,
"cool": Colors.lightBlue, "cool": Colors.lightBlue,
"unavailable": Colors.black26, EntityState.unavailable: Colors.black26,
"unknown": Colors.black26, EntityState.unknown: Colors.black26,
}; };
static Color stateColor(String state) { static Color stateColor(String state) {
@ -48,7 +54,7 @@ class EntityColors {
charts.Color c1 = charts.MaterialPalette.getOrderedPalettes(10)[r.round()].shadeDefault; charts.Color c1 = charts.MaterialPalette.getOrderedPalettes(10)[r.round()].shadeDefault;
return Color.fromARGB(c1.a, c1.r, c1.g, c1.b); return Color.fromARGB(c1.a, c1.r, c1.g, c1.b);
} else { } else {
return _stateColors["on"]; return _stateColors[EntityState.on];
} }
} }
} }

View File

@ -11,7 +11,7 @@ class EntityIcon extends StatelessWidget {
child: MaterialDesignIcons.createIconWidgetFromEntityData( child: MaterialDesignIcons.createIconWidgetFromEntityData(
entityModel.entity, entityModel.entity,
Sizes.iconSize, Sizes.iconSize,
EntityColors.stateColor(entityModel.entity.state) EntityColor.stateColor(entityModel.entity.state)
), ),
), ),
onTap: () => entityModel.handleTap onTap: () => entityModel.handleTap

View File

@ -156,7 +156,7 @@ class _CombinedHistoryChartWidgetState extends State<CombinedHistoryChartWidget>
result.add( result.add(
new charts.Series<EntityHistoryMoment, DateTime>( new charts.Series<EntityHistoryMoment, DateTime>(
id: "value", id: "value",
colorFn: (EntityHistoryMoment historyMoment, __) => EntityColors.chartHistoryStateColor("_", historyMoment.colorId), colorFn: (EntityHistoryMoment historyMoment, __) => EntityColor.chartHistoryStateColor("_", historyMoment.colorId),
radiusPxFn: (EntityHistoryMoment historyMoment, __) { radiusPxFn: (EntityHistoryMoment historyMoment, __) {
if (historyMoment.hiddenDot) { if (historyMoment.hiddenDot) {
return 0.0; return 0.0;
@ -179,7 +179,7 @@ class _CombinedHistoryChartWidgetState extends State<CombinedHistoryChartWidget>
new charts.Series<EntityHistoryMoment, DateTime>( new charts.Series<EntityHistoryMoment, DateTime>(
id: 'state', id: 'state',
radiusPxFn: (EntityHistoryMoment historyMoment, __) => (historyMoment.id == _selectedId) ? 5.0 : 4.0, 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, domainFn: (EntityHistoryMoment historyMoment, _) => historyMoment.startTime,
domainLowerBoundFn: (EntityHistoryMoment historyMoment, _) => historyMoment.startTime, domainLowerBoundFn: (EntityHistoryMoment historyMoment, _) => historyMoment.startTime,
domainUpperBoundFn: (EntityHistoryMoment historyMoment, _) => historyMoment.endTime ?? DateTime.now(), domainUpperBoundFn: (EntityHistoryMoment historyMoment, _) => historyMoment.endTime ?? DateTime.now(),

View File

@ -55,7 +55,7 @@ class HistoryControlWidget extends StatelessWidget {
textAlign: TextAlign.right, textAlign: TextAlign.right,
style: TextStyle( style: TextStyle(
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
color: EntityColors.historyStateColor(selectedStates[i], colorIndexes[i]), color: EntityColor.historyStateColor(selectedStates[i], colorIndexes[i]),
fontSize: 22.0 fontSize: 22.0
), ),
) )

View File

@ -108,7 +108,7 @@ class _NumericStateHistoryChartWidgetState extends State<NumericStateHistoryChar
return [ return [
new charts.Series<EntityHistoryMoment, DateTime>( new charts.Series<EntityHistoryMoment, DateTime>(
id: 'State', id: 'State',
colorFn: (EntityHistoryMoment historyMoment, __) => EntityColors.chartHistoryStateColor("on", -1), colorFn: (EntityHistoryMoment historyMoment, __) => EntityColor.chartHistoryStateColor(EntityState.on, -1),
domainFn: (EntityHistoryMoment historyMoment, _) => historyMoment.startTime, domainFn: (EntityHistoryMoment historyMoment, _) => historyMoment.startTime,
measureFn: (EntityHistoryMoment historyMoment, _) => historyMoment.value ?? historyMoment.previousValue, measureFn: (EntityHistoryMoment historyMoment, _) => historyMoment.value ?? historyMoment.previousValue,
data: data, data: data,

View File

@ -107,7 +107,7 @@ class _SimpleStateHistoryChartWidgetState extends State<SimpleStateHistoryChartW
new charts.Series<EntityHistoryMoment, DateTime>( new charts.Series<EntityHistoryMoment, DateTime>(
id: 'State', id: 'State',
strokeWidthPxFn: (EntityHistoryMoment historyMoment, __) => (historyMoment.id == _selectedId) ? 6.0 : 3.0, 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, domainFn: (EntityHistoryMoment historyMoment, _) => historyMoment.startTime,
measureFn: (EntityHistoryMoment historyMoment, _) => 10, measureFn: (EntityHistoryMoment historyMoment, _) => 10,
data: data, data: data,
@ -115,7 +115,7 @@ class _SimpleStateHistoryChartWidgetState extends State<SimpleStateHistoryChartW
new charts.Series<EntityHistoryMoment, DateTime>( new charts.Series<EntityHistoryMoment, DateTime>(
id: 'State', id: 'State',
radiusPxFn: (EntityHistoryMoment historyMoment, __) => (historyMoment.id == _selectedId) ? 5.0 : 3.0, 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, domainFn: (EntityHistoryMoment historyMoment, _) => historyMoment.startTime,
measureFn: (EntityHistoryMoment historyMoment, _) => 10, measureFn: (EntityHistoryMoment historyMoment, _) => 10,
data: data, data: data,
@ -123,7 +123,7 @@ class _SimpleStateHistoryChartWidgetState extends State<SimpleStateHistoryChartW
new charts.Series<EntityHistoryMoment, DateTime>( new charts.Series<EntityHistoryMoment, DateTime>(
id: 'State', id: 'State',
radiusPxFn: (EntityHistoryMoment historyMoment, __) => (historyMoment.id == _selectedId) ? 5.0 : 3.0, 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(), domainFn: (EntityHistoryMoment historyMoment, _) => historyMoment.endTime ?? DateTime.now(),
measureFn: (EntityHistoryMoment historyMoment, _) => 10, measureFn: (EntityHistoryMoment historyMoment, _) => 10,
data: data, data: data,

View File

@ -14,7 +14,7 @@ class _SwitchStateWidgetState extends State<SwitchStateWidget> {
void _setNewState(newValue, Entity entity) { void _setNewState(newValue, Entity entity) {
setState(() { setState(() {
entity.assumedState = newValue ? 'on' : 'off'; entity.assumedState = newValue ? EntityState.on : EntityState.off;
}); });
Timer(Duration(seconds: 2), (){ Timer(Duration(seconds: 2), (){
setState(() { setState(() {
@ -30,13 +30,13 @@ class _SwitchStateWidgetState extends State<SwitchStateWidget> {
final entityModel = EntityModel.of(context); final entityModel = EntityModel.of(context);
final entity = entityModel.entity; final entity = entityModel.entity;
Widget result; Widget result;
if (entity.state == "unavailable") { if (entity.state == EntityState.unavailable || entity.state == EntityState.unknown) {
return SimpleEntityState(); return SimpleEntityState();
} else if ((entity.attributes["assumed_state"] == null) || (entity.attributes["assumed_state"] == false)) { } else if ((entity.attributes["assumed_state"] == null) || (entity.attributes["assumed_state"] == false)) {
return SizedBox( return SizedBox(
height: 32.0, height: 32.0,
child: Switch( child: Switch(
value: entity.assumedState == 'on', value: entity.assumedState == EntityState.on,
onChanged: ((switchState) { onChanged: ((switchState) {
_setNewState(switchState, entity); _setNewState(switchState, entity);
}), }),
@ -51,13 +51,13 @@ class _SwitchStateWidgetState extends State<SwitchStateWidget> {
IconButton( IconButton(
onPressed: () => _setNewState(false, entity), onPressed: () => _setNewState(false, entity),
icon: Icon(MaterialDesignIcons.createIconDataFromIconName("mdi:flash-off")), 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, iconSize: Sizes.iconSize,
), ),
IconButton( IconButton(
onPressed: () => _setNewState(true, entity), onPressed: () => _setNewState(true, entity),
icon: Icon(MaterialDesignIcons.createIconDataFromIconName("mdi:flash")), 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 iconSize: Sizes.iconSize
) )
], ],

View File

@ -15,6 +15,7 @@ import 'package:http/http.dart' as http;
import 'package:flutter_colorpicker/material_picker.dart'; import 'package:flutter_colorpicker/material_picker.dart';
import 'package:charts_flutter/flutter.dart' as charts; import 'package:charts_flutter/flutter.dart' as charts;
part 'entity_class/const.dart';
part 'entity_class/entity.class.dart'; part 'entity_class/entity.class.dart';
part 'entity_class/switch_entity.class.dart'; part 'entity_class/switch_entity.class.dart';
part 'entity_class/button_entity.class.dart'; part 'entity_class/button_entity.class.dart';