Exemple #1
0
        /// <summary>
        /// Refreshes all panels which have updated after the provided time.
        /// </summary>
        /// <param name="lastUpdatedTime"></param>
        /// <returns></returns>
        private async Task <DateTime> UpdateEntitiesSinceLastUpdate(DateTime lastUpdatedTime)
        {
            // Get all entities not just the entities we think we want
            IEnumerable <Entity> allEntities = await WebRequests.GetData();

            if (null != allEntities)
            {
                // Get all entities which have been updated since the last time we've checked
                IEnumerable <Entity> entitiesToUpdate = allEntities.Where(x => x.LastUpdated > lastUpdatedTime).ToList();

                if (entitiesToUpdate.Any())
                {
                    // For all entities which need to be updated, get all group entities and check their children to see if the group entity needs to be updated as well
                    lastUpdatedTime = entitiesToUpdate.Select(x => x.LastUpdated).OrderByDescending(x => x).First();

                    foreach (Entity group in allEntities.Where(x => x.Attributes.ContainsKey("entity_id")))
                    {
                        IEnumerable <string> childrenEntityIds = ParseEntityIdAttribute(group);

                        if (childrenEntityIds.Any(x => entitiesToUpdate.Any(y => y.EntityId.Equals(x, StringComparison.InvariantCultureIgnoreCase))))
                        {
                            if (!entitiesToUpdate.Any(x => x.EntityId == group.EntityId))
                            {
                                // Add this group entity which is missing from the update requested list
                                Telemetry.TrackTrace($"{nameof(UpdateEntitiesSinceLastUpdate)} manually adding group {group.EntityId} to update list.");
                                entitiesToUpdate = entitiesToUpdate.Union(new List <Entity>()
                                {
                                    group
                                });
                            }
                        }
                    }

                    Telemetry.TrackTrace($"{nameof(UpdateEntitiesSinceLastUpdate)} has updated Entities: {string.Join(", ", entitiesToUpdate.Select(x => x.EntityId).ToList())}");

                    // Perform the update on the UI thread; don't block
                    UpdateEntityPanels(entitiesToUpdate, allEntities);
                }
            }

            return(lastUpdatedTime);
        }
Exemple #2
0
        /// <summary>
        /// Queries Home Assistant for state information and then populates the main view with panels to
        /// show the state information.
        /// </summary>
        /// <returns></returns>
        private async Task LoadFrame()
        {
            ScrollViewer scrollViewer = this.FindName("MainScrollView") as ScrollViewer;

            scrollViewer.Background = ThemeControl.BackgroundBrush;

            IEnumerable <Entity> entities = null;

            if (!string.IsNullOrEmpty(SettingsControl.HomeAssistantHostname))
            {
                entities = await WebRequests.GetData();
            }

            scrollViewer.Content = CreateViews(entities ?? new List <Entity>());

            // By setting a center horizontal aligntment we trim off the edges so that the dead space around
            // the left and right edges of the screen will render black. This only works well when there's
            // content which wraps the screen else we'll trim the background image as well, so guard against
            // null content before applying this setting.
            if (null != entities)
            {
                scrollViewer.HorizontalAlignment = HorizontalAlignment.Center;
            }
        }