Compare commits

..

25 Commits
0.3.0 ... 0.3.3

Author SHA1 Message Date
988cd4a72f Version 0.3.3 2018-10-21 19:19:55 +03:00
d1ea916781 Fix assumed state switch 2018-10-21 19:18:33 +03:00
ce9f25b86c Light color button 2018-10-21 19:12:37 +03:00
f29762c931 Fix hidden group issue 2018-10-21 18:52:29 +03:00
30e4496ef1 Resolves #148 assumed_state support 2018-10-21 17:13:11 +03:00
7f9dc5dd3a Set Light britness to 0 if light is turned off 2018-10-21 16:18:27 +03:00
0f6babc243 Resolves #151 Group visibility support 2018-10-21 16:11:47 +03:00
6a43e04b31 Just small method rename 2018-10-21 15:26:14 +03:00
36fa5a50c4 Remove cancelling null subscription 2018-10-21 14:48:25 +03:00
9ad6d92ccd View entities in entityCollection. Child entities in parse 2018-10-21 14:43:52 +03:00
fafa8f43f4 Minor light fixes 2018-10-21 13:55:18 +03:00
9b490d33d5 Reverting views refactoring 2018-10-21 02:39:51 +03:00
33f9a1075e Remove ViewWrapper widget 2018-10-21 01:09:07 +03:00
b83006e2c3 View as widget refactoring 2018-10-21 00:30:58 +03:00
ba09c36bd2 Resloves #133 Light support 2018-10-18 23:47:55 +03:00
c71ee568b0 Merge pull request #152 from estevez-dev/release/0.3.2
Fix empty cards on default_view
2018-10-18 22:03:51 +03:00
75041f5c23 Fix empty cards on default_view 2018-10-18 21:57:10 +03:00
14da471774 Merge pull request #150 from estevez-dev/release/0.3.1
Resolves #136 cover state
2018-10-17 21:34:36 +03:00
369b44f1c8 Merge branch 'master' into release/0.3.1 2018-10-17 21:34:27 +03:00
8284bb6e76 Resolves #136 cover state 2018-10-17 21:21:00 +03:00
9b3b4dfbbc WIP #133 Lights 2018-10-17 02:19:46 +03:00
5ca4424933 Fix dropdown width 2018-10-16 23:30:17 +03:00
a308aa29a4 Add mode switch stateless widget 2018-10-16 23:20:27 +03:00
9e80b0eaaf Add temperature control stateless widget 2018-10-16 22:35:17 +03:00
85379cf491 Resolves #132 2018-10-16 21:10:59 +03:00
12 changed files with 1011 additions and 616 deletions

View File

@ -1,11 +1,11 @@
part of 'main.dart';
class HACard extends StatelessWidget {
class CardWidget extends StatelessWidget {
final List<Entity> entities;
final String friendlyName;
const HACard({
const CardWidget({
Key key,
this.entities,
this.friendlyName
@ -13,6 +13,13 @@ class HACard extends StatelessWidget {
@override
Widget build(BuildContext context) {
final entityModel = EntityModel.of(context);
if (entityModel != null) {
final groupEntity = entityModel.entity;
if ((groupEntity!= null) && (groupEntity.isHidden)) {
return Container(width: 0.0, height: 0.0,);
}
}
List<Widget> body = [];
body.add(_buildCardHeader());
body.addAll(_buildCardBody(context));

View File

@ -17,7 +17,6 @@ class _EntityViewPageState extends State<EntityViewPage> {
@override
void initState() {
super.initState();
if (_stateSubscription != null) _stateSubscription.cancel();
_stateSubscription = eventBus.on<StateChangedEvent>().listen((event) {
if (event.entityId == widget.entity.entityId) {
setState(() {});

View File

@ -23,17 +23,18 @@ class Entity {
"sensor"
];
double rightWidgetPadding = 14.0;
double leftWidgetPadding = 8.0;
double extendedWidgetHeight = 50.0;
static const rightWidgetPadding = 14.0;
static const leftWidgetPadding = 8.0;
static const extendedWidgetHeight = 50.0;
static const iconSize = 28.0;
static const stateFontSize = 16.0;
static const nameFontSize = 16.0;
static const smallFontSize = 14.0;
static const largeFontSize = 24.0;
static const inputWidth = 160.0;
static const rowPadding = 10.0;
double widgetHeight = 34.0;
double iconSize = 28.0;
double stateFontSize = 16.0;
double nameFontSize = 16.0;
double smallFontSize = 14.0;
double largeFontSize = 24.0;
double inputWidth = 160.0;
double rowPadding = 10.0;
Map attributes;
String domain;
@ -61,6 +62,7 @@ class Entity {
String get unitOfMeasurement => attributes["unit_of_measurement"] ?? "";
List get childEntityIds => attributes["entity_id"] ?? [];
String get lastUpdated => _getLastUpdatedFormatted();
bool get isHidden => attributes["hidden"] ?? false;
Entity(Map rawData) {
update(rawData);
@ -82,14 +84,28 @@ class Entity {
} else if (temp1 is double) {
return temp1;
} else {
return null;
return double.tryParse("$temp1");
}
}
int _getIntAttributeValue(String attributeName) {
var temp1 = attributes["$attributeName"];
if (temp1 is int) {
return temp1;
} else if (temp1 is double) {
return temp1.round();
} else {
return int.tryParse("$temp1");
}
}
Widget buildDefaultWidget(BuildContext context) {
return EntityModel(
entity: this,
child: DefaultEntityContainer(state: _buildStatePart(context)),
child: DefaultEntityContainer(
state: _buildStatePart(context),
height: widgetHeight,
),
handleTap: true,
);
}
@ -113,7 +129,7 @@ class Entity {
return EntityModel(
entity: this,
child: EntityPageContainer(children: <Widget>[
DefaultEntityContainer(state: _buildStatePartForPage(context)),
DefaultEntityContainer(state: _buildStatePartForPage(context), height: widgetHeight),
LastUpdatedWidget(),
Divider(),
_buildAdditionalControlsForPage(context),
@ -174,7 +190,7 @@ class SwitchEntity extends Entity {
@override
Widget _buildStatePart(BuildContext context) {
return SwitchControlWidget();
return SwitchStateWidget();
}
}
@ -183,7 +199,7 @@ class ButtonEntity extends Entity {
@override
Widget _buildStatePart(BuildContext context) {
return ButtonControlWidget();
return ButtonStateWidget();
}
}
@ -198,7 +214,7 @@ class TextEntity extends Entity {
@override
Widget _buildStatePart(BuildContext context) {
return TextControlWidget();
return TextInputStateWidget();
}
}
@ -220,7 +236,7 @@ class SliderEntity extends Entity {
//width: 200.0,
child: Row(
children: <Widget>[
SliderControlWidget(
SliderStateWidget(
expanded: true,
),
SimpleEntityState(),
@ -236,7 +252,7 @@ class SliderEntity extends Entity {
@override
Widget _buildAdditionalControlsForPage(BuildContext context) {
return SliderControlWidget(
return SliderStateWidget(
expanded: false,
);
}
@ -446,8 +462,8 @@ class CoverEntity extends Entity {
double get currentPosition => _getDoubleAttributeValue('current_position');
double get currentTiltPosition => _getDoubleAttributeValue('current_tilt_position');
bool get canBeOpened => ((state == "closed") || (state == "closing") || (state == "opening"));
bool get canBeClosed => ((state == "open") || (state == "opening")|| (state == "closing"));
bool get canBeOpened => ((state != "opening") && (state != "open"));
bool get canBeClosed => ((state != "closing") && (state != "closed"));
bool get canTiltBeOpened => currentPosition < 100;
bool get canTiltBeClosed => currentPosition > 0;
@ -464,3 +480,83 @@ class CoverEntity extends Entity {
}
}
class LightEntity extends Entity {
static const SUPPORT_BRIGHTNESS = 1;
static const SUPPORT_COLOR_TEMP = 2;
static const SUPPORT_EFFECT = 4;
static const SUPPORT_FLASH = 8;
static const SUPPORT_COLOR = 16;
static const SUPPORT_TRANSITION = 32;
static const SUPPORT_WHITE_VALUE = 128;
bool get supportBrightness => ((attributes["supported_features"] &
LightEntity.SUPPORT_BRIGHTNESS) ==
LightEntity.SUPPORT_BRIGHTNESS);
bool get supportColorTemp => ((attributes["supported_features"] &
LightEntity.SUPPORT_COLOR_TEMP) ==
LightEntity.SUPPORT_COLOR_TEMP);
bool get supportEffect => ((attributes["supported_features"] &
LightEntity.SUPPORT_EFFECT) ==
LightEntity.SUPPORT_EFFECT);
bool get supportFlash => ((attributes["supported_features"] &
LightEntity.SUPPORT_FLASH) ==
LightEntity.SUPPORT_FLASH);
bool get supportColor => ((attributes["supported_features"] &
LightEntity.SUPPORT_COLOR) ==
LightEntity.SUPPORT_COLOR);
bool get supportTransition => ((attributes["supported_features"] &
LightEntity.SUPPORT_TRANSITION) ==
LightEntity.SUPPORT_TRANSITION);
bool get supportWhiteValue => ((attributes["supported_features"] &
LightEntity.SUPPORT_WHITE_VALUE) ==
LightEntity.SUPPORT_WHITE_VALUE);
int get brightness => _getIntAttributeValue("brightness");
int get colorTemp => _getIntAttributeValue("color_temp");
double get maxMireds => _getDoubleAttributeValue("max_mireds");
double get minMireds => _getDoubleAttributeValue("min_mireds");
Color get color => _getColor();
bool get isAdditionalControls => ((attributes["supported_features"] != null) && (attributes["supported_features"] != 0));
List<String> get effectList => _getEffectList();
LightEntity(Map rawData) : super(rawData);
Color _getColor() {
List rgb = attributes["rgb_color"];
try {
if ((rgb != null) && (rgb.length > 0)) {
return Color.fromARGB(255, rgb[0], rgb[1], rgb[2]);
} else {
return null;
}
} catch (e) {
return null;
}
}
List<String> _getEffectList() {
if (attributes["effect_list"] != null) {
List<String> result = (attributes["effect_list"] as List).cast<String>();
return result;
} else {
return null;
}
}
@override
Widget _buildStatePart(BuildContext context) {
return SwitchStateWidget();
}
@override
Widget _buildAdditionalControlsForPage(BuildContext context) {
if (!isAdditionalControls) {
return Container(height: 0.0, width: 0.0);
} else {
return LightControlsWidget();
}
}
}

View File

@ -1,11 +1,11 @@
part of '../main.dart';
class SwitchControlWidget extends StatefulWidget {
class SwitchStateWidget extends StatefulWidget {
@override
_SwitchControlWidgetState createState() => _SwitchControlWidgetState();
_SwitchStateWidgetState createState() => _SwitchStateWidgetState();
}
class _SwitchControlWidgetState extends State<SwitchControlWidget> {
class _SwitchStateWidgetState extends State<SwitchStateWidget> {
@override
void initState() {
@ -28,57 +28,46 @@ class _SwitchControlWidgetState extends State<SwitchControlWidget> {
@override
Widget build(BuildContext context) {
final entityModel = EntityModel.of(context);
return Switch(
value: entityModel.entity.assumedState == 'on',
onChanged: ((switchState) {
_setNewState(switchState, entityModel.entity);
}),
);
final entity = entityModel.entity;
if ((entity.attributes["assumed_state"] == null) || (entity.attributes["assumed_state"] == false)) {
return Switch(
value: entity.assumedState == 'on',
onChanged: ((switchState) {
_setNewState(switchState, entity);
}),
);
} else {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
IconButton(
onPressed: () => _setNewState(false, entity),
icon: Icon(MaterialDesignIcons.createIconDataFromIconName("mdi:flash-off")),
color: entity.assumedState == 'on' ? Colors.black : Colors.blue,
iconSize: Entity.iconSize,
),
IconButton(
onPressed: () => _setNewState(true, entity),
icon: Icon(MaterialDesignIcons.createIconDataFromIconName("mdi:flash")),
color: entity.assumedState == 'on' ? Colors.blue : Colors.black,
iconSize: Entity.iconSize
)
],
);
}
}
}
class ButtonControlWidget extends StatefulWidget {
class TextInputStateWidget extends StatefulWidget {
TextInputStateWidget({Key key}) : super(key: key);
@override
_ButtonControlWidgetState createState() => _ButtonControlWidgetState();
_TextInputStateWidgetState createState() => _TextInputStateWidgetState();
}
class _ButtonControlWidgetState extends State<ButtonControlWidget> {
@override
void initState() {
super.initState();
}
void _setNewState(Entity entity) {
eventBus.fire(new ServiceCallEvent(entity.domain, "turn_on", entity.entityId, null));
}
@override
Widget build(BuildContext context) {
final entityModel = EntityModel.of(context);
return FlatButton(
onPressed: (() {
_setNewState(entityModel.entity);
}),
child: Text(
"EXECUTE",
textAlign: TextAlign.right,
style:
new TextStyle(fontSize: entityModel.entity.stateFontSize, color: Colors.blue),
),
);
}
}
class TextControlWidget extends StatefulWidget {
TextControlWidget({Key key}) : super(key: key);
@override
_TextControlWidgetState createState() => _TextControlWidgetState();
}
class _TextControlWidgetState extends State<TextControlWidget> {
class _TextInputStateWidgetState extends State<TextInputStateWidget> {
String _tmpValue;
String _entityState;
String _entityDomain;
@ -167,17 +156,17 @@ class _TextControlWidgetState extends State<TextControlWidget> {
}
class SliderControlWidget extends StatefulWidget {
class SliderStateWidget extends StatefulWidget {
final bool expanded;
SliderControlWidget({Key key, @required this.expanded}) : super(key: key);
SliderStateWidget({Key key, @required this.expanded}) : super(key: key);
@override
_SliderControlWidgetState createState() => _SliderControlWidgetState();
_SliderStateWidgetState createState() => _SliderStateWidgetState();
}
class _SliderControlWidgetState extends State<SliderControlWidget> {
class _SliderStateWidgetState extends State<SliderStateWidget> {
int _multiplier = 1;
void setNewState(newValue, domain, entityId) {
@ -398,12 +387,13 @@ class _ClimateControlWidgetState extends State<ClimateControlWidget> {
_resetVars(entity);
}
return Padding(
padding: EdgeInsets.fromLTRB(entity.leftWidgetPadding, entity.rowPadding, entity.rightWidgetPadding, 0.0),
padding: EdgeInsets.fromLTRB(Entity.leftWidgetPadding, Entity.rowPadding, Entity.rightWidgetPadding, 0.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
_buildOnOffControl(entity),
_buildTemperatureControls(entity),
_buildTargetTemperatureControls(entity),
_buildHumidityControls(entity),
_buildOperationControl(entity),
_buildFanControl(entity),
@ -417,21 +407,10 @@ class _ClimateControlWidgetState extends State<ClimateControlWidget> {
Widget _buildAwayModeControl(ClimateEntity entity) {
if (entity.supportAwayMode) {
return Row(
children: <Widget>[
Expanded(
child: Text(
"Away mode",
style: TextStyle(
fontSize: entity.stateFontSize
),
),
),
Switch(
onChanged: (value) => _setAwayMode(entity, value),
value: _tmpAwayMode,
)
],
return ModeSwitchWidget(
caption: "Away mode",
onChange: (value) => _setAwayMode(entity, value),
value: _tmpAwayMode,
);
} else {
return Container(height: 0.0, width: 0.0,);
@ -440,21 +419,10 @@ class _ClimateControlWidgetState extends State<ClimateControlWidget> {
Widget _buildOnOffControl(ClimateEntity entity) {
if (entity.supportOnOff) {
return Row(
children: <Widget>[
Expanded(
child: Text(
"On / Off",
style: TextStyle(
fontSize: entity.stateFontSize
),
),
),
Switch(
onChanged: (value) => _setOnOf(entity, value),
value: !_tmpIsOff,
)
],
return ModeSwitchWidget(
onChange: (value) => _setOnOf(entity, value),
caption: "On / Off",
value: !_tmpIsOff
);
} else {
return Container(height: 0.0, width: 0.0,);
@ -463,21 +431,10 @@ class _ClimateControlWidgetState extends State<ClimateControlWidget> {
Widget _buildAuxHeatControl(ClimateEntity entity) {
if (entity.supportAuxHeat ) {
return Row(
children: <Widget>[
Expanded(
child: Text(
"Aux heat",
style: TextStyle(
fontSize: entity.stateFontSize
),
),
),
Switch(
onChanged: (value) => _setAuxHeat(entity, value),
value: _tmpAuxHeat,
)
],
return ModeSwitchWidget(
caption: "Aux heat",
onChange: (value) => _setAuxHeat(entity, value),
value: _tmpAuxHeat
);
} else {
return Container(height: 0.0, width: 0.0,);
@ -486,29 +443,11 @@ class _ClimateControlWidgetState extends State<ClimateControlWidget> {
Widget _buildOperationControl(ClimateEntity entity) {
if (entity.supportOperationMode) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Operation", style: TextStyle(
fontSize: entity.stateFontSize
)),
DropdownButton<String>(
value: "$_tmpOperationMode",
iconSize: 30.0,
style: TextStyle(
fontSize: entity.largeFontSize,
color: Colors.black,
),
items: entity.operationList.map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (mode) => _setOperationMode(entity, mode),
),
Container(height: entity.rowPadding,)
],
return ModeSelectorWidget(
onChange: (mode) => _setOperationMode(entity, mode),
options: entity.operationList,
caption: "Operation",
value: _tmpOperationMode,
);
} else {
return Container(height: 0.0, width: 0.0);
@ -517,29 +456,11 @@ class _ClimateControlWidgetState extends State<ClimateControlWidget> {
Widget _buildFanControl(ClimateEntity entity) {
if (entity.supportFanMode) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Fan mode", style: TextStyle(
fontSize: entity.stateFontSize
)),
DropdownButton<String>(
value: "$_tmpFanMode",
iconSize: 30.0,
style: TextStyle(
fontSize: entity.largeFontSize,
color: Colors.black,
),
items: entity.fanList.map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (mode) => _setFanMode(entity, mode),
),
Container(height: entity.rowPadding,)
],
return ModeSelectorWidget(
options: entity.fanList,
onChange: (mode) => _setFanMode(entity, mode),
caption: "Fan mode",
value: _tmpFanMode,
);
} else {
return Container(height: 0.0, width: 0.0);
@ -548,29 +469,11 @@ class _ClimateControlWidgetState extends State<ClimateControlWidget> {
Widget _buildSwingControl(ClimateEntity entity) {
if (entity.supportSwingMode) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Swing mode", style: TextStyle(
fontSize: entity.stateFontSize
)),
DropdownButton<String>(
value: "$_tmpSwingMode",
iconSize: 30.0,
style: TextStyle(
fontSize: entity.largeFontSize,
color: Colors.black,
),
items: entity.swingList.map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (mode) => _setSwingMode(entity, mode),
),
Container(height: entity.rowPadding,)
],
return ModeSelectorWidget(
onChange: (mode) => _setSwingMode(entity, mode),
options: entity.swingList,
value: _tmpSwingMode,
caption: "Swing mode"
);
} else {
return Container(height: 0.0, width: 0.0);
@ -578,139 +481,71 @@ class _ClimateControlWidgetState extends State<ClimateControlWidget> {
}
Widget _buildTemperatureControls(ClimateEntity entity) {
List<Widget> result = [];
if (entity.supportTargetTemperature) {
result.addAll(<Widget>[
Text(
"$_tmpTemperature",
style: TextStyle(
fontSize: entity.largeFontSize,
color: _showPending ? Colors.red : Colors.black
),
),
Column(
children: <Widget>[
IconButton(
icon: Icon(MaterialDesignIcons.createIconDataFromIconName('mdi:chevron-up')),
iconSize: 30.0,
onPressed: () => _temperatureUp(entity, 0.1),
),
IconButton(
icon: Icon(MaterialDesignIcons.createIconDataFromIconName('mdi:chevron-down')),
iconSize: 30.0,
onPressed: () => _temperatureDown(entity, 0.1),
)
],
),
Column(
children: <Widget>[
IconButton(
icon: Icon(MaterialDesignIcons.createIconDataFromIconName('mdi:chevron-double-up')),
iconSize: 30.0,
onPressed: () => _temperatureUp(entity, 0.5),
),
IconButton(
icon: Icon(MaterialDesignIcons.createIconDataFromIconName('mdi:chevron-double-down')),
iconSize: 30.0,
onPressed: () => _temperatureDown(entity, 0.5),
)
],
)
]);
} else if (entity.supportTargetTemperatureHigh && entity.supportTargetTemperatureLow) {
result.addAll(<Widget>[
Text(
"$_tmpTargetLow",
style: TextStyle(
fontSize: entity.largeFontSize,
color: _showPending ? Colors.red : Colors.black
),
),
Column(
children: <Widget>[
IconButton(
icon: Icon(MaterialDesignIcons.createIconDataFromIconName('mdi:chevron-up')),
iconSize: 30.0,
onPressed: () => _targetLowUp(entity, 0.1),
),
IconButton(
icon: Icon(MaterialDesignIcons.createIconDataFromIconName('mdi:chevron-down')),
iconSize: 30.0,
onPressed: () => _targetLowDown(entity, 0.1),
)
],
),
Column(
children: <Widget>[
IconButton(
icon: Icon(MaterialDesignIcons.createIconDataFromIconName('mdi:chevron-double-up')),
iconSize: 30.0,
onPressed: () => _targetLowUp(entity, 0.5),
),
IconButton(
icon: Icon(MaterialDesignIcons.createIconDataFromIconName('mdi:chevron-double-down')),
iconSize: 30.0,
onPressed: () => _targetLowDown(entity, 0.5),
)
],
),
Expanded(
child: Container(height: 10.0),
),
Text(
"$_tmpTargetHigh",
style: TextStyle(
fontSize: entity.largeFontSize,
color: _showPending ? Colors.red : Colors.black
),
),
Column(
children: <Widget>[
IconButton(
icon: Icon(MaterialDesignIcons.createIconDataFromIconName('mdi:chevron-up')),
iconSize: 30.0,
onPressed: () => _targetHighUp(entity, 0.1),
),
IconButton(
icon: Icon(MaterialDesignIcons.createIconDataFromIconName('mdi:chevron-down')),
iconSize: 30.0,
onPressed: () => _targetHighDown(entity, 0.1),
)
],
),
Column(
children: <Widget>[
IconButton(
icon: Icon(MaterialDesignIcons.createIconDataFromIconName('mdi:chevron-double-up')),
iconSize: 30.0,
onPressed: () => _targetHighUp(entity, 0.5),
),
IconButton(
icon: Icon(MaterialDesignIcons.createIconDataFromIconName('mdi:chevron-double-down')),
iconSize: 30.0,
onPressed: () => _targetHighDown(entity, 0.5),
)
],
)
]);
} else if (entity.supportTargetTemperatureHigh || entity.supportTargetTemperatureLow) {
result.add(Text("Unsupported temperature control. Please, report an issue."));
}
if (result.isNotEmpty) {
if ((entity.supportTargetTemperature) && (entity.temperature != null)) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Target temperature", style: TextStyle(
fontSize: entity.stateFontSize
fontSize: Entity.stateFontSize
)),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: result,
TemperatureControlWidget(
value: _tmpTemperature,
fontColor: _showPending ? Colors.red : Colors.black,
onLargeDec: () => _temperatureDown(entity, 0.5),
onLargeInc: () => _temperatureUp(entity, 0.5),
onSmallDec: () => _temperatureDown(entity, 0.1),
onSmallInc: () => _temperatureUp(entity, 0.1),
)
],
);
} else {
return Container(height: 0.0, width: 0.0,);
return Container(width: 0.0, height: 0.0,);
}
}
Widget _buildTargetTemperatureControls(ClimateEntity entity) {
List<Widget> controls = [];
if ((entity.supportTargetTemperatureLow) && (entity.targetLow != null)) {
controls.addAll(<Widget>[
TemperatureControlWidget(
value: _tmpTargetLow,
fontColor: _showPending ? Colors.red : Colors.black,
onLargeDec: () => _targetLowDown(entity, 0.5),
onLargeInc: () => _targetLowUp(entity, 0.5),
onSmallDec: () => _targetLowDown(entity, 0.1),
onSmallInc: () => _targetLowUp(entity, 0.1),
),
Expanded(
child: Container(height: 10.0),
)
]);
}
if ((entity.supportTargetTemperatureHigh) && (entity.targetHigh != null)) {
controls.add(
TemperatureControlWidget(
value: _tmpTargetHigh,
fontColor: _showPending ? Colors.red : Colors.black,
onLargeDec: () => _targetHighDown(entity, 0.5),
onLargeInc: () => _targetHighUp(entity, 0.5),
onSmallDec: () => _targetHighDown(entity, 0.1),
onSmallInc: () => _targetHighUp(entity, 0.1),
)
);
}
if (controls.isNotEmpty) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Target temperature range", style: TextStyle(
fontSize: Entity.stateFontSize
)),
Row(
children: controls,
)
],
);
} else {
return Container(width: 0.0, height: 0.0);
}
}
@ -720,7 +555,7 @@ class _ClimateControlWidgetState extends State<ClimateControlWidget> {
result.addAll(<Widget>[
Text(
"$_tmpTargetHumidity%",
style: TextStyle(fontSize: entity.largeFontSize),
style: TextStyle(fontSize: Entity.largeFontSize),
),
Expanded(
child: Slider(
@ -744,9 +579,9 @@ class _ClimateControlWidgetState extends State<ClimateControlWidget> {
children: <Widget>[
Padding(
padding: EdgeInsets.fromLTRB(
0.0, entity.rowPadding, 0.0, entity.rowPadding),
0.0, Entity.rowPadding, 0.0, Entity.rowPadding),
child: Text("Target humidity", style: TextStyle(
fontSize: entity.stateFontSize
fontSize: Entity.stateFontSize
)),
),
Row(
@ -754,7 +589,7 @@ class _ClimateControlWidgetState extends State<ClimateControlWidget> {
children: result,
),
Container(
height: entity.rowPadding,
height: Entity.rowPadding,
)
],
);
@ -859,13 +694,14 @@ class _CoverControlWidgetState extends State<CoverControlWidget> {
Widget build(BuildContext context) {
final entityModel = EntityModel.of(context);
final CoverEntity entity = entityModel.entity;
TheLogger.log("debug", "${entity.state}");
if (_changedHere) {
_changedHere = false;
} else {
_resetVars(entity);
}
return Padding(
padding: EdgeInsets.fromLTRB(entity.leftWidgetPadding, entity.rowPadding, entity.rightWidgetPadding, 0.0),
padding: EdgeInsets.fromLTRB(Entity.leftWidgetPadding, Entity.rowPadding, Entity.rightWidgetPadding, 0.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
@ -883,9 +719,9 @@ class _CoverControlWidgetState extends State<CoverControlWidget> {
children: <Widget>[
Padding(
padding: EdgeInsets.fromLTRB(
0.0, entity.rowPadding, 0.0, entity.rowPadding),
0.0, Entity.rowPadding, 0.0, Entity.rowPadding),
child: Text("Position", style: TextStyle(
fontSize: entity.stateFontSize
fontSize: Entity.stateFontSize
)),
),
Slider(
@ -901,7 +737,7 @@ class _CoverControlWidgetState extends State<CoverControlWidget> {
},
onChangeEnd: (double value) => _setNewPosition(entity, value),
),
Container(height: entity.rowPadding,)
Container(height: Entity.rowPadding,)
],
);
} else {
@ -913,7 +749,7 @@ class _CoverControlWidgetState extends State<CoverControlWidget> {
List<Widget> controls = [];
if (entity.supportCloseTilt || entity.supportOpenTilt || entity.supportStopTilt) {
controls.add(
CoverEntityTiltControlState()
CoverEntityTiltControlButtons()
);
}
if (entity.supportSetTiltPosition) {
@ -931,15 +767,15 @@ class _CoverControlWidgetState extends State<CoverControlWidget> {
},
onChangeEnd: (double value) => _setNewTiltPosition(entity, value),
),
Container(height: entity.rowPadding,)
Container(height: Entity.rowPadding,)
]);
}
if (controls.isNotEmpty) {
controls.insert(0, Padding(
padding: EdgeInsets.fromLTRB(
0.0, entity.rowPadding, 0.0, entity.rowPadding),
0.0, Entity.rowPadding, 0.0, Entity.rowPadding),
child: Text("Tilt position", style: TextStyle(
fontSize: entity.stateFontSize
fontSize: Entity.stateFontSize
)),
));
return Column(
@ -951,4 +787,243 @@ class _CoverControlWidgetState extends State<CoverControlWidget> {
}
}
}
class LightControlsWidget extends StatefulWidget {
@override
_LightControlsWidgetState createState() => _LightControlsWidgetState();
}
class _LightControlsWidgetState extends State<LightControlsWidget> {
int _tmpBrightness;
int _tmpColorTemp;
Color _tmpColor;
bool _changedHere = false;
String _tmpEffect;
void _resetState(LightEntity entity) {
_tmpBrightness = entity.brightness ?? 0;
_tmpColorTemp = entity.colorTemp;
_tmpColor = entity.color;
_tmpEffect = null;
}
void _setBrightness(LightEntity entity, double value) {
setState(() {
_tmpBrightness = value.round();
_changedHere = true;
if (_tmpBrightness > 0) {
eventBus.fire(new ServiceCallEvent(
entity.domain, "turn_on", entity.entityId,
{"brightness": _tmpBrightness}));
} else {
eventBus.fire(new ServiceCallEvent(
entity.domain, "turn_off", entity.entityId,
null));
}
});
}
void _setColorTemp(LightEntity entity, double value) {
setState(() {
_tmpColorTemp = value.round();
_changedHere = true;
eventBus.fire(new ServiceCallEvent(
entity.domain, "turn_on", entity.entityId,
{"color_temp": _tmpColorTemp}));
});
}
void _setColor(LightEntity entity, Color color) {
setState(() {
_tmpColor = color;
_changedHere = true;
TheLogger.log("Debug", "Color: [${color.red}, ${color.green}, ${color.blue}]");
if ((color == Colors.black) || ((color.red == color.green) && (color.green == color.blue))) {
eventBus.fire(new ServiceCallEvent(
entity.domain, "turn_off", entity.entityId,
null));
} else {
eventBus.fire(new ServiceCallEvent(
entity.domain, "turn_on", entity.entityId,
{"rgb_color": [color.red, color.green, color.blue]}));
}
});
}
void _setEffect(LightEntity entity, String value) {
setState(() {
_tmpEffect = value;
_changedHere = true;
if (_tmpEffect != null) {
eventBus.fire(new ServiceCallEvent(
entity.domain, "turn_on", entity.entityId,
{"effect": "$value"}));
}
});
}
@override
Widget build(BuildContext context) {
final entityModel = EntityModel.of(context);
final LightEntity entity = entityModel.entity;
if (!_changedHere) {
_resetState(entity);
} else {
_changedHere = false;
}
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
_buildBrightnessControl(entity),
_buildColorTempControl(entity),
_buildColorControl(entity),
_buildEffectControl(entity)
],
);
}
Widget _buildBrightnessControl(LightEntity entity) {
if ((entity.supportBrightness) && (_tmpBrightness != null)) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(height: Entity.rowPadding,),
Text(
"Brightness",
style: TextStyle(fontSize: Entity.stateFontSize),
),
Container(height: Entity.rowPadding,),
Row(
children: <Widget>[
Icon(Icons.brightness_5),
Expanded(
child: Slider(
value: _tmpBrightness.toDouble(),
min: 0.0,
max: 255.0,
onChanged: (value) {
setState(() {
_changedHere = true;
_tmpBrightness = value.round();
});
},
onChangeEnd: (value) => _setBrightness(entity, value),
),
)
],
),
Container(height: Entity.rowPadding,)
],
);
} else {
return Container(width: 0.0, height: 0.0);
}
}
Widget _buildColorTempControl(LightEntity entity) {
if ((entity.supportColorTemp) && (_tmpColorTemp != null)) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(height: Entity.rowPadding,),
Text(
"Color temperature",
style: TextStyle(fontSize: Entity.stateFontSize),
),
Container(height: Entity.rowPadding,),
Row(
children: <Widget>[
Text("Cold", style: TextStyle(color: Colors.lightBlue),),
Expanded(
child: Slider(
value: _tmpColorTemp.toDouble(),
min: entity.minMireds,
max: entity.maxMireds,
onChanged: (value) {
setState(() {
_changedHere = true;
_tmpColorTemp = value.round();
});
},
onChangeEnd: (value) => _setColorTemp(entity, value),
),
),
Text("Warm", style: TextStyle(color: Colors.amberAccent),),
],
),
Container(height: Entity.rowPadding,)
],
);
} else {
return Container(width: 0.0, height: 0.0);
}
}
Widget _buildColorControl(LightEntity entity) {
if ((entity.supportColor) && (entity.color != null)) {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(height: Entity.rowPadding,),
RaisedButton(
onPressed: () => _showColorPicker(entity),
color: _tmpColor ?? Colors.black45,
child: Text(
"COLOR",
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 50.0,
fontWeight: FontWeight.bold,
color: Colors.black12,
),
),
),
Container(height: 2*Entity.rowPadding,),
],
);
} else {
return Container(width: 0.0, height: 0.0);
}
}
void _showColorPicker(LightEntity entity) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
titlePadding: EdgeInsets.all(0.0),
contentPadding: EdgeInsets.all(0.0),
content: SingleChildScrollView(
child: MaterialPicker(
pickerColor: _tmpColor,
onColorChanged: (color) {
_setColor(entity, color);
Navigator.of(context).pop();
},
enableLabel: true,
),
),
);
},
);
}
Widget _buildEffectControl(LightEntity entity) {
if ((entity.supportEffect) && (entity.effectList != null)) {
return ModeSelectorWidget(
onChange: (effect) => _setEffect(entity, effect),
caption: "Effect",
options: entity.effectList,
value: _tmpEffect
);
} else {
return Container(width: 0.0, height: 0.0);
}
}
}

View File

@ -1,11 +1,8 @@
part of '../main.dart';
class EntityWidgetsSizes {
}
class EntityWidgetsSizes {}
class EntityModel extends InheritedWidget {
const EntityModel({
Key key,
@required this.entity,
@ -24,23 +21,22 @@ class EntityModel extends InheritedWidget {
bool updateShouldNotify(InheritedWidget oldWidget) {
return true;
}
}
class DefaultEntityContainer extends StatelessWidget {
DefaultEntityContainer({
Key key,
@required this.state,
@required this.height
}) : super(key: key);
final Widget state;
final double height;
@override
Widget build(BuildContext context) {
final entityModel = EntityModel.of(context);
return SizedBox(
height: entityModel.entity.widgetHeight,
height: height,
child: Row(
children: <Widget>[
EntityIcon(),
@ -52,11 +48,9 @@ class DefaultEntityContainer extends StatelessWidget {
),
);
}
}
class EntityPageContainer extends StatelessWidget {
EntityPageContainer({Key key, @required this.children}) : super(key: key);
final List<Widget> children;
@ -67,33 +61,30 @@ class EntityPageContainer extends StatelessWidget {
children: children,
);
}
}
class SimpleEntityState extends StatelessWidget {
@override
Widget build(BuildContext context) {
final entityModel = EntityModel.of(context);
return Padding(
padding:
EdgeInsets.fromLTRB(0.0, 0.0, entityModel.entity.rightWidgetPadding, 0.0),
padding: EdgeInsets.fromLTRB(
0.0, 0.0, Entity.rightWidgetPadding, 0.0),
child: GestureDetector(
child: Text(
"${entityModel.entity.state}${entityModel.entity.unitOfMeasurement}",
textAlign: TextAlign.right,
style: new TextStyle(
fontSize: entityModel.entity.stateFontSize,
fontSize: Entity.stateFontSize,
)),
onTap: () => entityModel.handleTap ? eventBus.fire(new ShowEntityPageEvent(entityModel.entity)) : null,
)
);
onTap: () => entityModel.handleTap
? eventBus.fire(new ShowEntityPageEvent(entityModel.entity))
: null,
));
}
}
class EntityName extends StatelessWidget {
@override
Widget build(BuildContext context) {
final entityModel = EntityModel.of(context);
@ -104,75 +95,73 @@ class EntityName extends StatelessWidget {
"${entityModel.entity.displayName}",
overflow: TextOverflow.ellipsis,
softWrap: false,
style: TextStyle(fontSize: entityModel.entity.nameFontSize),
style: TextStyle(fontSize: Entity.nameFontSize),
),
),
onTap: () => entityModel.handleTap ? eventBus.fire(new ShowEntityPageEvent(entityModel.entity)) : null,
onTap: () => entityModel.handleTap
? eventBus.fire(new ShowEntityPageEvent(entityModel.entity))
: null,
);
}
}
class EntityIcon extends StatelessWidget {
@override
Widget build(BuildContext context) {
final entityModel = EntityModel.of(context);
return GestureDetector(
child: Padding(
padding: EdgeInsets.fromLTRB(entityModel.entity.leftWidgetPadding, 0.0, 12.0, 0.0),
padding: EdgeInsets.fromLTRB(
Entity.leftWidgetPadding, 0.0, 12.0, 0.0),
//TODO: move createIconWidgetFromEntityData into this widget
child: MaterialDesignIcons.createIconWidgetFromEntityData(
entityModel.entity,
entityModel.entity.iconSize,
Entity.STATE_ICONS_COLORS[entityModel.entity.state] ?? Entity.STATE_ICONS_COLORS["default"]),
Entity.iconSize,
Entity.STATE_ICONS_COLORS[entityModel.entity.state] ??
Entity.STATE_ICONS_COLORS["default"]),
),
onTap: () => entityModel.handleTap ? eventBus.fire(new ShowEntityPageEvent(entityModel.entity)) : null,
onTap: () => entityModel.handleTap
? eventBus.fire(new ShowEntityPageEvent(entityModel.entity))
: null,
);
}
}
class LastUpdatedWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final entityModel = EntityModel.of(context);
return Padding(
padding: EdgeInsets.fromLTRB(
entityModel.entity.leftWidgetPadding, 0.0, 0.0, 0.0),
Entity.leftWidgetPadding, 0.0, 0.0, 0.0),
child: Text(
'${entityModel.entity.lastUpdated}',
textAlign: TextAlign.left,
style:
TextStyle(fontSize: entityModel.entity.smallFontSize, color: Colors.black26),
style: TextStyle(
fontSize: Entity.smallFontSize, color: Colors.black26),
),
);
}
}
class EntityAttributesList extends StatelessWidget {
EntityAttributesList({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
final entityModel = EntityModel.of(context);
List<Widget> attrs = [];
if ((entityModel.entity.attributesToShow == null) || (entityModel.entity.attributesToShow.contains("all"))) {
entityModel.entity.attributes.forEach((name, value){
attrs.add(
_buildSingleAttribute(entityModel.entity, "$name", "$value")
);
if ((entityModel.entity.attributesToShow == null) ||
(entityModel.entity.attributesToShow.contains("all"))) {
entityModel.entity.attributes.forEach((name, value) {
attrs.add(_buildSingleAttribute("$name", "$value"));
});
} else {
entityModel.entity.attributesToShow.forEach((String attr) {
String attrValue = entityModel.entity.getAttribute("$attr");
if (attrValue != null) {
attrs.add(
_buildSingleAttribute(entityModel.entity, "$attr", "$attrValue")
);
_buildSingleAttribute("$attr", "$attrValue"));
}
});
}
@ -183,12 +172,13 @@ class EntityAttributesList extends StatelessWidget {
);
}
Widget _buildSingleAttribute(Entity entity, String name, String value) {
Widget _buildSingleAttribute(String name, String value) {
return Row(
children: <Widget>[
Expanded(
child: Padding(
padding: EdgeInsets.fromLTRB(entity.leftWidgetPadding, entity.rowPadding, 0.0, 0.0),
padding: EdgeInsets.fromLTRB(
Entity.leftWidgetPadding, Entity.rowPadding, 0.0, 0.0),
child: Text(
"$name",
textAlign: TextAlign.left,
@ -197,7 +187,8 @@ class EntityAttributesList extends StatelessWidget {
),
Expanded(
child: Padding(
padding: EdgeInsets.fromLTRB(0.0, entity.rowPadding, entity.rightWidgetPadding, 0.0),
padding: EdgeInsets.fromLTRB(
0.0, Entity.rowPadding, Entity.rightWidgetPadding, 0.0),
child: Text(
"$value",
textAlign: TextAlign.right,
@ -210,48 +201,54 @@ class EntityAttributesList extends StatelessWidget {
}
class Badge extends StatelessWidget {
@override
Widget build(BuildContext context) {
final entityModel = EntityModel.of(context);
double iconSize = 26.0;
Widget badgeIcon;
String onBadgeTextValue;
Color iconColor = Entity.badgeColors[entityModel.entity.domain] ?? Entity.badgeColors["default"];
Color iconColor = Entity.badgeColors[entityModel.entity.domain] ??
Entity.badgeColors["default"];
switch (entityModel.entity.domain) {
case "sun": {
badgeIcon = entityModel.entity.state == "below_horizon" ?
Icon(
MaterialDesignIcons.createIconDataFromIconCode(0xf0dc),
size: iconSize,
) :
Icon(
MaterialDesignIcons.createIconDataFromIconCode(0xf5a8),
size: iconSize,
);
break;
}
case "sensor": {
onBadgeTextValue = entityModel.entity.unitOfMeasurement;
badgeIcon = Center(
child: Text(
"${entityModel.entity.state}",
overflow: TextOverflow.fade,
softWrap: false,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 17.0),
),
);
break;
}
case "device_tracker": {
badgeIcon = MaterialDesignIcons.createIconWidgetFromEntityData(entityModel.entity, iconSize,Colors.black);
onBadgeTextValue = entityModel.entity.state;
break;
}
default: {
badgeIcon = MaterialDesignIcons.createIconWidgetFromEntityData(entityModel.entity, iconSize,Colors.black);
}
case "sun":
{
badgeIcon = entityModel.entity.state == "below_horizon"
? Icon(
MaterialDesignIcons.createIconDataFromIconCode(0xf0dc),
size: iconSize,
)
: Icon(
MaterialDesignIcons.createIconDataFromIconCode(0xf5a8),
size: iconSize,
);
break;
}
case "sensor":
{
onBadgeTextValue = entityModel.entity.unitOfMeasurement;
badgeIcon = Center(
child: Text(
"${entityModel.entity.state}",
overflow: TextOverflow.fade,
softWrap: false,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 17.0),
),
);
break;
}
case "device_tracker":
{
badgeIcon = MaterialDesignIcons.createIconWidgetFromEntityData(
entityModel.entity, iconSize, Colors.black);
onBadgeTextValue = entityModel.entity.state;
break;
}
default:
{
badgeIcon = MaterialDesignIcons.createIconWidgetFromEntityData(
entityModel.entity, iconSize, Colors.black);
}
}
Widget onBadgeText;
if (onBadgeTextValue == null || onBadgeTextValue.length == 0) {
@ -261,71 +258,70 @@ class Badge extends StatelessWidget {
padding: EdgeInsets.fromLTRB(6.0, 2.0, 6.0, 2.0),
child: Text("$onBadgeTextValue",
style: TextStyle(fontSize: 12.0, color: Colors.white),
textAlign: TextAlign.center, softWrap: false, overflow: TextOverflow.fade),
textAlign: TextAlign.center,
softWrap: false,
overflow: TextOverflow.fade),
decoration: new BoxDecoration(
// Circle shape
//shape: BoxShape.circle,
color: iconColor,
borderRadius: BorderRadius.circular(9.0),
)
);
));
}
return GestureDetector(
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
width: 50.0,
height: 50.0,
decoration: new BoxDecoration(
// Circle shape
shape: BoxShape.circle,
color: Colors.white,
// The border you want
border: new Border.all(
width: 2.0,
color: iconColor,
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
width: 50.0,
height: 50.0,
decoration: new BoxDecoration(
// Circle shape
shape: BoxShape.circle,
color: Colors.white,
// The border you want
border: new Border.all(
width: 2.0,
color: iconColor,
),
),
child: Stack(
overflow: Overflow.visible,
children: <Widget>[
Positioned(
width: 46.0,
height: 46.0,
top: 0.0,
left: 0.0,
child: badgeIcon,
),
Positioned(
//width: 50.0,
bottom: -9.0,
left: -10.0,
right: -10.0,
child: Center(
child: onBadgeText,
))
],
),
),
child: Stack(
overflow: Overflow.visible,
children: <Widget>[
Positioned(
width: 46.0,
height: 46.0,
top: 0.0,
left: 0.0,
child: badgeIcon,
),
Positioned(
//width: 50.0,
bottom: -9.0,
left: -10.0,
right: -10.0,
child: Center(
child: onBadgeText,
)
)
],
Container(
width: 60.0,
child: Text(
"${entityModel.entity.displayName}",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 12.0),
softWrap: true,
maxLines: 3,
overflow: TextOverflow.ellipsis,
),
),
),
Container(
width: 60.0,
child: Text(
"${entityModel.entity.displayName}",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 12.0),
softWrap: true,
maxLines: 3,
overflow: TextOverflow.ellipsis,
),
),
],
),
onTap: () => eventBus.fire(new ShowEntityPageEvent(entityModel.entity))
);
],
),
onTap: () =>
eventBus.fire(new ShowEntityPageEvent(entityModel.entity)));
}
}
class ClimateStateWidget extends StatelessWidget {
@ -333,65 +329,141 @@ class ClimateStateWidget extends StatelessWidget {
Widget build(BuildContext context) {
final entityModel = EntityModel.of(context);
final ClimateEntity entity = entityModel.entity;
String targetTemp = "-";
if ((entity.supportTargetTemperature) && (entity.temperature != null)) {
targetTemp = "${entity.temperature}";
} else if ((entity.supportTargetTemperatureLow) &&
(entity.targetLow != null)) {
targetTemp = "${entity.targetLow}";
if ((entity.supportTargetTemperatureHigh) &&
(entity.targetHigh != null)) {
targetTemp += " - ${entity.targetHigh}";
}
}
return Padding(
padding:
EdgeInsets.fromLTRB(0.0, 0.0, entityModel.entity.rightWidgetPadding, 0.0),
padding: EdgeInsets.fromLTRB(
0.0, 0.0, Entity.rightWidgetPadding, 0.0),
child: GestureDetector(
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Row(
children: <Widget>[
Text(
"${entity.state}",
Text("${entity.state}",
textAlign: TextAlign.right,
style: new TextStyle(
fontWeight: FontWeight.bold,
fontSize: entityModel.entity.stateFontSize,
fontSize: Entity.stateFontSize,
)),
Text(
entity.supportTargetTemperature ? " ${entity.temperature}" : " ${entity.targetLow} - ${entity.targetHigh}",
Text(" $targetTemp",
textAlign: TextAlign.right,
style: new TextStyle(
fontSize: entityModel.entity.stateFontSize,
fontSize: Entity.stateFontSize,
))
],
),
Text(
"Currently: ${entity.attributes["current_temperature"]}",
entity.attributes["current_temperature"] != null ?
Text("Currently: ${entity.attributes["current_temperature"]}",
textAlign: TextAlign.right,
style: new TextStyle(
fontSize: entityModel.entity.stateFontSize,
color: Colors.black45
))
fontSize: Entity.stateFontSize,
color: Colors.black45)
) :
Container(height: 0.0,)
],
),
onTap: () => entityModel.handleTap ? eventBus.fire(new ShowEntityPageEvent(entity)) : null,
onTap: () => entityModel.handleTap
? eventBus.fire(new ShowEntityPageEvent(entity))
: null,
));
}
}
class TemperatureControlWidget extends StatelessWidget {
final double value;
final double fontSize;
final Color fontColor;
final onSmallInc;
final onLargeInc;
final onSmallDec;
final onLargeDec;
TemperatureControlWidget(
{Key key,
@required this.value,
@required this.onSmallInc,
@required this.onSmallDec,
@required this.onLargeInc,
@required this.onLargeDec,
this.fontSize,
this.fontColor})
: super(key: key);
@override
Widget build(BuildContext context) {
return Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"$value",
style: TextStyle(
fontSize: fontSize ?? 24.0,
color: fontColor ?? Colors.black
),
),
Column(
children: <Widget>[
IconButton(
icon: Icon(MaterialDesignIcons.createIconDataFromIconName(
'mdi:chevron-up')),
iconSize: 30.0,
onPressed: () => onSmallInc(),
),
IconButton(
icon: Icon(MaterialDesignIcons.createIconDataFromIconName(
'mdi:chevron-down')),
iconSize: 30.0,
onPressed: () => onSmallDec(),
)
],
),
Column(
children: <Widget>[
IconButton(
icon: Icon(MaterialDesignIcons.createIconDataFromIconName(
'mdi:chevron-double-up')),
iconSize: 30.0,
onPressed: () => onLargeInc(),
),
IconButton(
icon: Icon(MaterialDesignIcons.createIconDataFromIconName(
'mdi:chevron-double-down')),
iconSize: 30.0,
onPressed: () => onLargeDec(),
)
],
)
],
);
}
}
class DateTimeStateWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final entityModel = EntityModel.of(context);
final DateTimeEntity entity = entityModel.entity;
return Padding(
padding:
EdgeInsets.fromLTRB(0.0, 0.0, entity.rightWidgetPadding, 0.0),
padding: EdgeInsets.fromLTRB(0.0, 0.0, Entity.rightWidgetPadding, 0.0),
child: GestureDetector(
child: Text(
"${entity.formattedState}",
child: Text("${entity.formattedState}",
textAlign: TextAlign.right,
style: new TextStyle(
fontSize: entity.stateFontSize,
fontSize: Entity.stateFontSize,
)),
onTap: () => _handleStateTap(context, entity),
)
);
));
}
void _handleStateTap(BuildContext context, DateTimeEntity entity) {
@ -399,18 +471,35 @@ class DateTimeStateWidget extends StatelessWidget {
_showDatePicker(context, entity).then((date) {
if (date != null) {
if (entity.hasTime) {
_showTimePicker(context, entity).then((time){
entity.setNewState({"date": "${formatDate(date, [yyyy, '-', mm, '-', dd])}", "time": "${formatDate(DateTime(1970, 1, 1, time.hour, time.minute), [HH, ':', nn])}"});
_showTimePicker(context, entity).then((time) {
entity.setNewState({
"date": "${formatDate(date, [yyyy, '-', mm, '-', dd])}",
"time":
"${formatDate(DateTime(1970, 1, 1, time.hour, time.minute), [
HH,
':',
nn
])}"
});
});
} else {
entity.setNewState({"date": "${formatDate(date, [yyyy, '-', mm, '-', dd])}"});
entity.setNewState({
"date": "${formatDate(date, [yyyy, '-', mm, '-', dd])}"
});
}
}
});
} else if (entity.hasTime) {
_showTimePicker(context, entity).then((time){
_showTimePicker(context, entity).then((time) {
if (time != null) {
entity.setNewState({"time": "${formatDate(DateTime(1970, 1, 1, time.hour, time.minute), [HH, ':', nn])}"});
entity.setNewState({
"time":
"${formatDate(DateTime(1970, 1, 1, time.hour, time.minute), [
HH,
':',
nn
])}"
});
}
});
} else {
@ -424,30 +513,30 @@ class DateTimeStateWidget extends StatelessWidget {
initialDate: entity.dateTimeState,
firstDate: DateTime(1970),
lastDate: DateTime(2037) //Unix timestamp will finish at Jan 19, 2038
);
);
}
Future _showTimePicker(BuildContext context, DateTimeEntity entity) {
return showTimePicker(
context: context,
initialTime: TimeOfDay.fromDateTime(entity.dateTimeState)
);
initialTime: TimeOfDay.fromDateTime(entity.dateTimeState));
}
}
class CoverEntityControlState extends StatelessWidget {
void _open(CoverEntity entity) {
eventBus.fire(new ServiceCallEvent(entity.domain, "open_cover", entity.entityId, null));
eventBus.fire(new ServiceCallEvent(
entity.domain, "open_cover", entity.entityId, null));
}
void _close(CoverEntity entity) {
eventBus.fire(new ServiceCallEvent(entity.domain, "close_cover", entity.entityId, null));
eventBus.fire(new ServiceCallEvent(
entity.domain, "close_cover", entity.entityId, null));
}
void _stop(CoverEntity entity) {
eventBus.fire(new ServiceCallEvent(entity.domain, "stop_cover", entity.entityId, null));
eventBus.fire(new ServiceCallEvent(
entity.domain, "stop_cover", entity.entityId, null));
}
@override
@ -456,64 +545,62 @@ class CoverEntityControlState extends StatelessWidget {
final CoverEntity entity = entityModel.entity;
List<Widget> buttons = [];
if (entity.supportOpen) {
buttons.add(
IconButton(
icon: Icon(
MaterialDesignIcons.createIconDataFromIconName("mdi:arrow-up"),
size: entity.iconSize,
),
onPressed: entity.canBeOpened ? () =>_open(entity) : null
)
);
buttons.add(IconButton(
icon: Icon(
MaterialDesignIcons.createIconDataFromIconName("mdi:arrow-up"),
size: Entity.iconSize,
),
onPressed: entity.canBeOpened ? () => _open(entity) : null));
} else {
buttons.add(Container(width: entity.iconSize+20.0,));
buttons.add(Container(
width: Entity.iconSize + 20.0,
));
}
if (entity.supportStop) {
buttons.add(
IconButton(
icon: Icon(
MaterialDesignIcons.createIconDataFromIconName("mdi:stop"),
size: entity.iconSize,
),
onPressed: () => _stop(entity)
)
);
buttons.add(IconButton(
icon: Icon(
MaterialDesignIcons.createIconDataFromIconName("mdi:stop"),
size: Entity.iconSize,
),
onPressed: () => _stop(entity)));
} else {
buttons.add(Container(width: entity.iconSize+20.0,));
buttons.add(Container(
width: Entity.iconSize + 20.0,
));
}
if (entity.supportClose) {
buttons.add(
IconButton(
icon: Icon(
MaterialDesignIcons.createIconDataFromIconName("mdi:arrow-down"),
size: entity.iconSize,
),
onPressed: entity.canBeClosed ? () => _close(entity) : null
)
);
buttons.add(IconButton(
icon: Icon(
MaterialDesignIcons.createIconDataFromIconName("mdi:arrow-down"),
size: Entity.iconSize,
),
onPressed: entity.canBeClosed ? () => _close(entity) : null));
} else {
buttons.add(Container(width: entity.iconSize+20.0,));
buttons.add(Container(
width: Entity.iconSize + 20.0,
));
}
return Row(
children: buttons,
);
}
}
class CoverEntityTiltControlState extends StatelessWidget {
class CoverEntityTiltControlButtons extends StatelessWidget {
void _open(CoverEntity entity) {
eventBus.fire(new ServiceCallEvent(entity.domain, "open_cover_tilt", entity.entityId, null));
eventBus.fire(new ServiceCallEvent(
entity.domain, "open_cover_tilt", entity.entityId, null));
}
void _close(CoverEntity entity) {
eventBus.fire(new ServiceCallEvent(entity.domain, "close_cover_tilt", entity.entityId, null));
eventBus.fire(new ServiceCallEvent(
entity.domain, "close_cover_tilt", entity.entityId, null));
}
void _stop(CoverEntity entity) {
eventBus.fire(new ServiceCallEvent(entity.domain, "stop_cover_tilt", entity.entityId, null));
eventBus.fire(new ServiceCallEvent(
entity.domain, "stop_cover_tilt", entity.entityId, null));
}
@override
@ -522,48 +609,167 @@ class CoverEntityTiltControlState extends StatelessWidget {
final CoverEntity entity = entityModel.entity;
List<Widget> buttons = [];
if (entity.supportOpenTilt) {
buttons.add(
IconButton(
icon: Icon(
MaterialDesignIcons.createIconDataFromIconName("mdi:arrow-top-right"),
size: entity.iconSize,
),
onPressed: entity.canTiltBeOpened ? () =>_open(entity) : null
)
);
buttons.add(IconButton(
icon: Icon(
MaterialDesignIcons.createIconDataFromIconName(
"mdi:arrow-top-right"),
size: Entity.iconSize,
),
onPressed: entity.canTiltBeOpened ? () => _open(entity) : null));
} else {
buttons.add(Container(width: entity.iconSize+20.0,));
buttons.add(Container(
width: Entity.iconSize + 20.0,
));
}
if (entity.supportStopTilt) {
buttons.add(
IconButton(
icon: Icon(
MaterialDesignIcons.createIconDataFromIconName("mdi:stop"),
size: entity.iconSize,
),
onPressed: () => _stop(entity)
)
);
buttons.add(IconButton(
icon: Icon(
MaterialDesignIcons.createIconDataFromIconName("mdi:stop"),
size: Entity.iconSize,
),
onPressed: () => _stop(entity)));
} else {
buttons.add(Container(width: entity.iconSize+20.0,));
buttons.add(Container(
width: Entity.iconSize + 20.0,
));
}
if (entity.supportCloseTilt) {
buttons.add(
IconButton(
icon: Icon(
MaterialDesignIcons.createIconDataFromIconName("mdi:arrow-bottom-left"),
size: entity.iconSize,
),
onPressed: entity.canTiltBeClosed ? () => _close(entity) : null
)
);
buttons.add(IconButton(
icon: Icon(
MaterialDesignIcons.createIconDataFromIconName(
"mdi:arrow-bottom-left"),
size: Entity.iconSize,
),
onPressed: entity.canTiltBeClosed ? () => _close(entity) : null));
} else {
buttons.add(Container(width: entity.iconSize+20.0,));
buttons.add(Container(
width: Entity.iconSize + 20.0,
));
}
return Row(
children: buttons,
);
}
}
class ButtonStateWidget extends StatelessWidget {
void _setNewState(Entity entity) {
eventBus.fire(new ServiceCallEvent(entity.domain, "turn_on", entity.entityId, null));
}
@override
Widget build(BuildContext context) {
final entityModel = EntityModel.of(context);
return FlatButton(
onPressed: (() {
_setNewState(entityModel.entity);
}),
child: Text(
"EXECUTE",
textAlign: TextAlign.right,
style:
new TextStyle(fontSize: Entity.stateFontSize, color: Colors.blue),
),
);
}
}
class ModeSelectorWidget extends StatelessWidget {
final String caption;
final List<String> options;
final String value;
final double captionFontSize;
final double valueFontSize;
final double bottomPadding;
final onChange;
ModeSelectorWidget({
Key key,
this.caption,
@required this.options,
this.value,
@required this.onChange,
this.captionFontSize,
this.valueFontSize,
this.bottomPadding
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("$caption", style: TextStyle(
fontSize: captionFontSize ?? Entity.stateFontSize
)),
Row(
children: <Widget>[
Expanded(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton<String>(
value: value,
iconSize: 30.0,
isExpanded: true,
style: TextStyle(
fontSize: valueFontSize ?? Entity.largeFontSize,
color: Colors.black,
),
hint: Text("Select ${caption.toLowerCase()}"),
items: options.map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
onChanged: (mode) => onChange(mode),
),
),
)
],
),
Container(height: bottomPadding ?? Entity.rowPadding,)
],
);
}
}
class ModeSwitchWidget extends StatelessWidget {
final String caption;
final onChange;
final double captionFontSize;
final bool value;
ModeSwitchWidget({
Key key,
@required this.caption,
@required this.onChange,
this.captionFontSize,
this.value
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Row(
children: <Widget>[
Expanded(
child: Text(
"$caption",
style: TextStyle(
fontSize: captionFontSize ?? Entity.stateFontSize
),
),
),
Switch(
onChanged: (value) => onChange(value),
value: value ?? false,
)
],
);
}
}

View File

@ -2,28 +2,32 @@ part of 'main.dart';
class EntityCollection {
Map<String, Entity> _entities;
List<String> viewList;
Map<String, Entity> _allEntities;
Map<String, Entity> views;
bool get isEmpty => _entities.isEmpty;
bool get isEmpty => _allEntities.isEmpty;
EntityCollection() {
_entities = {};
viewList = [];
_allEntities = {};
views = {};
}
bool get hasDefaultView => _entities["group.default_view"] != null;
bool get hasDefaultView => _allEntities["group.default_view"] != null;
void parse(List rawData) {
_entities.clear();
viewList.clear();
_allEntities.clear();
views.clear();
TheLogger.log("Debug","Parsing ${rawData.length} Home Assistant entities");
rawData.forEach((rawEntityData) {
Entity newEntity = addFromRaw(rawEntityData);
if (newEntity.isView) {
viewList.add(newEntity.entityId);
addFromRaw(rawEntityData);
});
_allEntities.forEach((entityId, entity){
if ((entity.isGroup) && (entity.childEntityIds != null)) {
entity.childEntities = getAll(entity.childEntityIds);
}
if (entity.isView) {
views[entityId] = entity;
}
});
}
@ -35,13 +39,15 @@ class EntityCollection {
}
case "automation":
case "input_boolean":
case "switch":
case "switch": {
return SwitchEntity(rawEntityData);
}
case "light": {
return SwitchEntity(rawEntityData);
return LightEntity(rawEntityData);
}
case "script":
case "scene": {
return ButtonEntity(rawEntityData);
return ButtonEntity(rawEntityData);
}
case "input_datetime": {
return DateTimeEntity(rawEntityData);
@ -76,12 +82,12 @@ class EntityCollection {
}
void add(Entity entity) {
_entities[entity.entityId] = entity;
_allEntities[entity.entityId] = entity;
}
Entity addFromRaw(Map rawEntityData) {
Entity entity = _createEntityInstance(rawEntityData);
_entities[entity.entityId] = entity;
_allEntities[entity.entityId] = entity;
return entity;
}
@ -90,7 +96,7 @@ class EntityCollection {
}
Entity get(String entityId) {
return _entities[entityId];
return _allEntities[entityId];
}
List<Entity> getAll(List ids) {
@ -105,13 +111,13 @@ class EntityCollection {
}
bool isExist(String entityId) {
return _entities[entityId] != null;
return _allEntities[entityId] != null;
}
Map<String,List<String>> getDefaultViewTopLevelEntities() {
Map<String,List<String>> result = {"userGroups": [], "notGroupedEntities": []};
List<String> entities = [];
_entities.forEach((id, entity){
_allEntities.forEach((id, entity){
if ((id.indexOf("group.") == 0) && (id.indexOf(".all_") == -1) && (!entity.isView)) {
result["userGroups"].add(id);
}
@ -123,7 +129,7 @@ class EntityCollection {
entities.forEach((entiyId) {
bool foundInGroup = false;
result["userGroups"].forEach((userGroupId) {
if (_entities[userGroupId].childEntityIds.contains(entiyId)) {
if (_allEntities[userGroupId].childEntityIds.contains(entiyId)) {
foundInGroup = true;
}
});

View File

@ -38,7 +38,7 @@ class HomeAssistant {
String get locationName => _instanceConfig["location_name"] ?? "";
String get userName => _userName ?? locationName;
String get userAvatarText => userName.length > 0 ? userName[0] : "";
int get viewsCount => _entities.viewList.length ?? 0;
int get viewsCount => _entities.views.length ?? 0;
EntityCollection get entities => _entities;
@ -192,7 +192,6 @@ class HomeAssistant {
_handleMessage(String message) {
var data = json.decode(message);
TheLogger.log("Debug","[Received] => ${data['type']}");
if (data["type"] == "auth_required") {
_sendAuthMessageRaw('{"type": "auth","$_authType": "$_password"}');
} else if (data["type"] == "auth_ok") {
@ -210,10 +209,11 @@ class HomeAssistant {
} else if (data["id"] == _userInfoMessageId) {
_parseUserInfo(data);
} else if (data["id"] == _currentMessageId) {
TheLogger.log("Debug","Request id:$_currentMessageId was successful");
TheLogger.log("Debug","[Received] => Request id:$_currentMessageId was successful");
}
} else if (data["type"] == "event") {
if ((data["event"] != null) && (data["event"]["event_type"] == "state_changed")) {
TheLogger.log("Debug","[Received] => ${data['type']}.${data["event"]["event_type"]}: ${data["event"]["data"]["entity_id"]}");
_handleEntityStateChange(data["event"]["data"]);
} else if (data["event"] != null) {
TheLogger.log("Warning","Unhandled event type: ${data["event"]["event_type"]}");
@ -300,7 +300,7 @@ class HomeAssistant {
String message = '{"id": $_currentMessageId, "type": "call_service", "domain": "$domain", "service": "$service", "service_data": {"entity_id": "$entityId"';
if (additionalParams != null) {
additionalParams.forEach((name, value){
if ((value is double) || (value is int)) {
if ((value is double) || (value is int) || (value is List)) {
message += ', "$name" : $value';
} else {
message += ', "$name" : "$value"';

View File

@ -12,6 +12,7 @@ import 'package:url_launcher/url_launcher.dart';
import 'package:flutter/services.dart';
import 'package:date_format/date_format.dart';
import 'package:http/http.dart' as http;
import 'package:flutter_colorpicker/material_picker.dart';
part 'entity_class/entity.class.dart';
part 'entity_class/stateless_widgets.dart';
@ -30,7 +31,7 @@ part 'card_class.dart';
EventBus eventBus = new EventBus();
const String appName = "HA Client";
const appVersion = "0.3.0.38";
const appVersion = "0.3.3";
String homeAssistantWebHost;
@ -206,6 +207,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
//_instanceConfig = _homeAssistant.instanceConfig;
_entities = _homeAssistant.entities;
_uiViewsCount = _homeAssistant.viewsCount;
TheLogger.log("Debug","_uiViewsCount=$_uiViewsCount");
_isLoading = 0;
});
}).catchError((e) {
@ -238,7 +240,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
}
List<Tab> buildUIViewTabs() {
//TODO move somewhere to ViewBuilder
List<Tab> result = [];
if (!_entities.isEmpty) {
if (!_entities.hasDefaultView) {
@ -252,10 +253,10 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
)
);
}
_entities.viewList.forEach((viewId) {
_entities.views.forEach((viewId, groupEntity) {
result.add(
Tab(
icon: MaterialDesignIcons.createIconWidgetFromEntityData(_entities.get(viewId), 24.0, null) ??
icon: MaterialDesignIcons.createIconWidgetFromEntityData(groupEntity, 24.0, null) ??
Icon(
MaterialDesignIcons.createIconDataFromIconName("mdi:home-assistant"),
size: 24.0,

View File

@ -31,7 +31,8 @@ class ViewBuilder{
Map<String, List<String>> userGroupsList = entityCollection.getDefaultViewTopLevelEntities();
List<Entity> entitiesForView = [];
userGroupsList["userGroups"].forEach((groupId){
entitiesForView.add(entityCollection.get(groupId));
Entity en = entityCollection.get(groupId);
entitiesForView.add(en);
});
userGroupsList["notGroupedEntities"].forEach((entityId){
entitiesForView.add(entityCollection.get(entityId));
@ -45,26 +46,12 @@ class ViewBuilder{
List<View> _composeViews() {
List<View> result = [];
int counter = 0;
entityCollection.viewList.forEach((viewId) {
entityCollection.views.forEach((viewId, viewGroupEntity) {
counter += 1;
//try {
Entity viewGroupEntity = entityCollection.get(viewId);
List<Entity> entitiesForView = [];
viewGroupEntity.childEntityIds.forEach((
entityId) { //Each entity or group in view
if (entityCollection.isExist(entityId)) {
Entity en = entityCollection.get(entityId);
if (en.isGroup) {
en.childEntities = entityCollection.getAll(en.childEntityIds);
}
entitiesForView.add(en);
} else {
TheLogger.log("Warning", "Unknown entity inside view: $entityId");
}
});
result.add(View(
count: counter,
entities: entitiesForView
entities: viewGroupEntity.childEntities
));
/*} catch (error) {
TheLogger.log("Error","Error parsing view: $viewId");

View File

@ -14,7 +14,7 @@ class View {
}) {
childEntitiesAsBadges = [];
childEntitiesAsCards = {};
_composeEntities();
_filterEntities();
}
Widget buildWidget(BuildContext context) {
@ -24,7 +24,7 @@ class View {
);
}
void _composeEntities() {
void _filterEntities() {
entities.forEach((Entity entity){
if (!entity.isGroup) {
if (entity.isBadge) {
@ -41,6 +41,7 @@ class View {
} else {
childEntitiesAsCards[entity.entityId] = CardSkeleton(
displayName: entity.displayName,
groupEntity: entity
);
childEntitiesAsCards[entity.entityId].childEntities = entity.childEntities;
}
@ -111,10 +112,14 @@ class ViewWidgetState extends State<ViewWidget> {
widget.cards.forEach((String id, CardSkeleton skeleton){
result.add(
HACard(
entities: skeleton.childEntities,
friendlyName: skeleton.displayName,
)
EntityModel(
entity: skeleton.groupEntity,
handleTap: false,
child: CardWidget(
entities: skeleton.childEntities,
friendlyName: skeleton.displayName,
)
)
);
});
@ -151,8 +156,13 @@ class ViewWidgetState extends State<ViewWidget> {
class CardSkeleton {
String displayName;
List<Entity> childEntities;
Entity groupEntity;
CardSkeleton({Key key, this.displayName, this.childEntities}) {
CardSkeleton({
Key key,
this.displayName,
this.childEntities,
this.groupEntity}) {
childEntities = [];
}
}

View File

@ -113,6 +113,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.2"
flutter_colorpicker:
dependency: "direct main"
description:
name: flutter_colorpicker
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.0"
flutter_launcher_icons:
dependency: "direct dev"
description:

View File

@ -1,7 +1,7 @@
name: hass_client
description: Home Assistant Android Client
version: 0.3.0+38
version: 0.3.3+43
environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
@ -15,6 +15,7 @@ dependencies:
cached_network_image: ^0.4.1
url_launcher: ^3.0.3
date_format: ^1.0.5
flutter_colorpicker: ^0.1.0
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.