This commit is contained in:
estevez-dev 2019-03-02 20:13:24 +02:00
parent a4736bfb5a
commit 8c9804e16f
5 changed files with 61 additions and 20 deletions

View File

@ -67,6 +67,7 @@ class Entity {
String state; String state;
String displayState; String displayState;
DateTime _lastUpdated; DateTime _lastUpdated;
String thumbnailBase64;
List<Entity> childEntities = []; List<Entity> childEntities = [];
List<String> attributesToShow = ["all"]; List<String> attributesToShow = ["all"];

View File

@ -1,13 +1,19 @@
part of '../main.dart'; part of '../main.dart';
class EntityIcon extends StatelessWidget { class EntityIcon extends StatefulWidget {
final EdgeInsetsGeometry padding; final EdgeInsetsGeometry padding;
final double size; final double size;
final Color color; final Color color;
const EntityIcon({Key key, this.color, this.size: Sizes.iconSize, this.padding: const EdgeInsets.fromLTRB( EntityIcon({Key key, this.padding: const EdgeInsets.fromLTRB(
Sizes.leftWidgetPadding, 0.0, 12.0, 0.0)}) : super(key: key); 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) { int getDefaultIconByEntityId(String entityId, String deviceClass, String state) {
String domain = entityId.split(".")[0]; 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) { 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( 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, backgroundColor: Colors.white,
backgroundImage: CachedNetworkImageProvider( backgroundImage: CachedNetworkImageProvider(
"$homeAssistantWebHost${data.entity.entityPicture}", "$homeAssistantWebHost${data.entity.entityPicture}",
@ -47,7 +64,7 @@ class EntityIcon extends StatelessWidget {
} }
return Icon( return Icon(
IconData(iconCode, fontFamily: 'Material Design Icons'), IconData(iconCode, fontFamily: 'Material Design Icons'),
size: size, size: widget.size,
color: color, color: color,
); );
} }
@ -56,11 +73,13 @@ class EntityIcon extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final EntityWrapper entityWrapper = EntityModel.of(context).entityWrapper; final EntityWrapper entityWrapper = EntityModel.of(context).entityWrapper;
final HomeAssistantModel homeAssistantModel = HomeAssistantModel.of(context);
return Padding( return Padding(
padding: padding, padding: widget.padding,
child: buildIcon( child: buildIcon(
homeAssistantModel,
entityWrapper, entityWrapper,
color ?? EntityColor.stateColor(entityWrapper.entity.state) widget.color ?? EntityColor.stateColor(entityWrapper.entity.state)
), ),
); );
} }

View File

@ -210,11 +210,7 @@ class HomeAssistant {
_completeConnecting({"errorCode": 6, "errorMessage": "${data["message"]}"}); _completeConnecting({"errorCode": 6, "errorMessage": "${data["message"]}"});
} else if (data["type"] == "result") { } else if (data["type"] == "result") {
Logger.d("[Received] <== id:${data["id"]}, ${data['success'] ? 'success' : 'error'}"); Logger.d("[Received] <== id:${data["id"]}, ${data['success'] ? 'success' : 'error'}");
if (data["success"]) {
_messageResolver[data["id"]]?.complete(data); _messageResolver[data["id"]]?.complete(data);
} else {
_messageResolver[data["id"]]?.completeError(data["error"]["message"]);
}
_messageResolver.remove(data["id"]); _messageResolver.remove(data["id"]);
} else if (data["type"] == "event") { } else if (data["type"] == "event") {
if ((data["event"] != null) && (data["event"]["event_type"] == "state_changed")) { 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")); 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() { _incrementMessageId() {
_currentMessageId += 1; _currentMessageId += 1;
} }

View File

@ -649,10 +649,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
primary: false, primary: false,
drawer: _buildAppDrawer(), drawer: _buildAppDrawer(),
bottomNavigationBar: bottomBar, bottomNavigationBar: bottomBar,
body: HomeAssistantModel( body: _buildScaffoldBody(true)
child: _buildScaffoldBody(true),
homeAssistant: _homeAssistant,
)
); );
} else { } else {
return Scaffold( return Scaffold(
@ -660,7 +657,10 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver, Ticker
drawer: _buildAppDrawer(), drawer: _buildAppDrawer(),
primary: false, primary: false,
bottomNavigationBar: bottomBar, bottomNavigationBar: bottomBar,
body: _buildScaffoldBody(false), body: HomeAssistantModel(
child: _buildScaffoldBody(false),
homeAssistant: _homeAssistant
),
); );
} }
} }

View File

@ -22,6 +22,7 @@ class MaterialDesignIcons {
"cover.closed": "mdi:window-closed", "cover.closed": "mdi:window-closed",
"cover.closing": "mdi:window-open", "cover.closing": "mdi:window-open",
"cover.opening": "mdi:window-open", "cover.opening": "mdi:window-open",
"camera": "mdi:cctv",
"lock.locked": "mdi:lock", "lock.locked": "mdi:lock",
"lock.unlocked": "mdi:lock-open", "lock.unlocked": "mdi:lock-open",
"fan": "mdi:fan", "fan": "mdi:fan",