State const
This commit is contained in:
parent
3190b45db3
commit
87f89b63e1
@ -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);
|
||||
|
31
lib/entity_class/const.dart
Normal file
31
lib/entity_class/const.dart
Normal 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';
|
||||
}
|
@ -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;
|
||||
|
||||
|
@ -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"] ?? [];
|
||||
|
@ -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":
|
||||
{
|
||||
|
@ -98,7 +98,7 @@ class _LightControlsWidgetState extends State<LightControlsWidget> {
|
||||
}
|
||||
|
||||
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: <Widget>[
|
||||
|
@ -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<Widget> 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<Widget> 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<MediaPlayerProgressWidget> {
|
||||
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<MediaPlayerProgressWidget> {
|
||||
return LinearProgressIndicator(
|
||||
value: progress,
|
||||
backgroundColor: Colors.black45,
|
||||
valueColor: AlwaysStoppedAnimation<Color>(EntityColors.stateColor("on")),
|
||||
valueColor: AlwaysStoppedAnimation<Color>(EntityColor.stateColor(EntityState.on)),
|
||||
);
|
||||
} catch (e) {
|
||||
_timer?.cancel();
|
||||
|
@ -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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -156,7 +156,7 @@ class _CombinedHistoryChartWidgetState extends State<CombinedHistoryChartWidget>
|
||||
result.add(
|
||||
new charts.Series<EntityHistoryMoment, DateTime>(
|
||||
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<CombinedHistoryChartWidget>
|
||||
new charts.Series<EntityHistoryMoment, DateTime>(
|
||||
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(),
|
||||
|
@ -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
|
||||
),
|
||||
)
|
||||
|
@ -108,7 +108,7 @@ class _NumericStateHistoryChartWidgetState extends State<NumericStateHistoryChar
|
||||
return [
|
||||
new charts.Series<EntityHistoryMoment, DateTime>(
|
||||
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,
|
||||
|
@ -107,7 +107,7 @@ class _SimpleStateHistoryChartWidgetState extends State<SimpleStateHistoryChartW
|
||||
new charts.Series<EntityHistoryMoment, DateTime>(
|
||||
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<SimpleStateHistoryChartW
|
||||
new charts.Series<EntityHistoryMoment, DateTime>(
|
||||
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<SimpleStateHistoryChartW
|
||||
new charts.Series<EntityHistoryMoment, DateTime>(
|
||||
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,
|
||||
|
@ -14,7 +14,7 @@ class _SwitchStateWidgetState extends State<SwitchStateWidget> {
|
||||
|
||||
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<SwitchStateWidget> {
|
||||
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<SwitchStateWidget> {
|
||||
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
|
||||
)
|
||||
],
|
||||
|
@ -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';
|
||||
|
Reference in New Issue
Block a user