WIP #308
This commit is contained in:
parent
a4736bfb5a
commit
8c9804e16f
@ -67,6 +67,7 @@ class Entity {
|
||||
String state;
|
||||
String displayState;
|
||||
DateTime _lastUpdated;
|
||||
String thumbnailBase64;
|
||||
|
||||
List<Entity> childEntities = [];
|
||||
List<String> attributesToShow = ["all"];
|
||||
|
@ -1,13 +1,19 @@
|
||||
part of '../main.dart';
|
||||
|
||||
class EntityIcon extends StatelessWidget {
|
||||
class EntityIcon extends StatefulWidget {
|
||||
|
||||
final EdgeInsetsGeometry padding;
|
||||
final double size;
|
||||
final Color color;
|
||||
|
||||
const EntityIcon({Key key, this.color, this.size: Sizes.iconSize, this.padding: const EdgeInsets.fromLTRB(
|
||||
Sizes.leftWidgetPadding, 0.0, 12.0, 0.0)}) : super(key: key);
|
||||
EntityIcon({Key key, this.padding: const EdgeInsets.fromLTRB(
|
||||
Sizes.leftWidgetPadding, 0.0, 12.0, 0.0), this.size: Sizes.iconSize, this.color}) : super(key: key);
|
||||
|
||||
@override
|
||||
_EntityIconState createState() => _EntityIconState();
|
||||
}
|
||||
|
||||
class _EntityIconState extends State<EntityIcon> {
|
||||
|
||||
int getDefaultIconByEntityId(String entityId, String deviceClass, String state) {
|
||||
String domain = entityId.split(".")[0];
|
||||
@ -24,13 +30,24 @@ class EntityIcon extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
Widget buildIcon(EntityWrapper data, Color color) {
|
||||
Widget buildIcon(HomeAssistantModel homeAssistantModel, EntityWrapper data, Color color) {
|
||||
if (data == null) {
|
||||
return null;
|
||||
return Container(width: widget.size, height: widget.size,);
|
||||
}
|
||||
if (data.entity.entityPicture != null) {
|
||||
if ((data.entity.domain == "camera" || data.entity.domain == "media_player") && data.entity.thumbnailBase64 == null) {
|
||||
homeAssistantModel.homeAssistant.updateEntityThumbnail(data.entity);
|
||||
}
|
||||
if (data.entity.thumbnailBase64 != null) {
|
||||
return CircleAvatar(
|
||||
radius: size/2,
|
||||
radius: widget.size/2,
|
||||
backgroundColor: Colors.white,
|
||||
backgroundImage: MemoryImage(
|
||||
Base64Codec().decode(data.entity.thumbnailBase64),
|
||||
)
|
||||
);
|
||||
} else if (data.entity.entityPicture != null && data.entity.domain != "camera" && data.entity.domain != "media_player") {
|
||||
return CircleAvatar(
|
||||
radius: widget.size/2,
|
||||
backgroundColor: Colors.white,
|
||||
backgroundImage: CachedNetworkImageProvider(
|
||||
"$homeAssistantWebHost${data.entity.entityPicture}",
|
||||
@ -47,7 +64,7 @@ class EntityIcon extends StatelessWidget {
|
||||
}
|
||||
return Icon(
|
||||
IconData(iconCode, fontFamily: 'Material Design Icons'),
|
||||
size: size,
|
||||
size: widget.size,
|
||||
color: color,
|
||||
);
|
||||
}
|
||||
@ -56,11 +73,13 @@ class EntityIcon extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final EntityWrapper entityWrapper = EntityModel.of(context).entityWrapper;
|
||||
final HomeAssistantModel homeAssistantModel = HomeAssistantModel.of(context);
|
||||
return Padding(
|
||||
padding: padding,
|
||||
padding: widget.padding,
|
||||
child: buildIcon(
|
||||
homeAssistantModel,
|
||||
entityWrapper,
|
||||
color ?? EntityColor.stateColor(entityWrapper.entity.state)
|
||||
widget.color ?? EntityColor.stateColor(entityWrapper.entity.state)
|
||||
),
|
||||
);
|
||||
}
|
||||
|
@ -210,11 +210,7 @@ class HomeAssistant {
|
||||
_completeConnecting({"errorCode": 6, "errorMessage": "${data["message"]}"});
|
||||
} else if (data["type"] == "result") {
|
||||
Logger.d("[Received] <== id:${data["id"]}, ${data['success'] ? 'success' : 'error'}");
|
||||
if (data["success"]) {
|
||||
_messageResolver[data["id"]]?.complete(data);
|
||||
} else {
|
||||
_messageResolver[data["id"]]?.completeError(data["error"]["message"]);
|
||||
}
|
||||
_messageResolver.remove(data["id"]);
|
||||
} else if (data["type"] == "event") {
|
||||
if ((data["event"] != null) && (data["event"]["event_type"] == "state_changed")) {
|
||||
@ -257,6 +253,30 @@ class HomeAssistant {
|
||||
await _sendInitialMessage("get_services").then((data) => Logger.d("We actually don`t need the list of servcies for now"));
|
||||
}
|
||||
|
||||
Future updateEntityThumbnail(Entity entity) async {
|
||||
if (entity.thumbnailBase64 == null) {
|
||||
_incrementMessageId();
|
||||
_messageResolver[_currentMessageId] = Completer();
|
||||
String type;
|
||||
if (entity.domain == "camera") {
|
||||
type = "camera_thumbnail";
|
||||
} else if (entity.domain == "media_player") {
|
||||
type = "media_player_thumbnail";
|
||||
}
|
||||
_send('{"id": $_currentMessageId, "type": "$type", "entity_id": "${entity.entityId}"}', false);
|
||||
await _messageResolver[_currentMessageId].future.then((data){
|
||||
if (data['success']) {
|
||||
Logger.d("Got entity thumbnail for ${entity
|
||||
.entityId}. Content-type: ${data['result']['content_type']}");
|
||||
if (!data['result']['content_type'].contains('xml')) {
|
||||
entity.thumbnailBase64 = data['result']['content'];
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
_incrementMessageId() {
|
||||
_currentMessageId += 1;
|
||||
}
|
||||
|
@ -649,10 +649,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
||||
primary: false,
|
||||
drawer: _buildAppDrawer(),
|
||||
bottomNavigationBar: bottomBar,
|
||||
body: HomeAssistantModel(
|
||||
child: _buildScaffoldBody(true),
|
||||
homeAssistant: _homeAssistant,
|
||||
)
|
||||
body: _buildScaffoldBody(true)
|
||||
);
|
||||
} else {
|
||||
return Scaffold(
|
||||
@ -660,7 +657,10 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
|
||||
drawer: _buildAppDrawer(),
|
||||
primary: false,
|
||||
bottomNavigationBar: bottomBar,
|
||||
body: _buildScaffoldBody(false),
|
||||
body: HomeAssistantModel(
|
||||
child: _buildScaffoldBody(false),
|
||||
homeAssistant: _homeAssistant
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ class MaterialDesignIcons {
|
||||
"cover.closed": "mdi:window-closed",
|
||||
"cover.closing": "mdi:window-open",
|
||||
"cover.opening": "mdi:window-open",
|
||||
"camera": "mdi:cctv",
|
||||
"lock.locked": "mdi:lock",
|
||||
"lock.unlocked": "mdi:lock-open",
|
||||
"fan": "mdi:fan",
|
||||
|
Reference in New Issue
Block a user