Exemplo n.º 1
0
        public static async Task <HassEntity> GetHttpSensorRequest(string entityId)
        {
            try
            {
                using (HttpClient)
                {
                    var bearerValue = $"Bearer {HassUtil.GetApiKey()}";
                    using (HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("GET"), $"http://192.168.1.2:8123/api/states/{entityId}"))
                    {
                        request.Headers.TryAddWithoutValidation("Authorization", bearerValue);
                        var response = await HttpClient.SendAsync(request);

                        if (!response.IsSuccessStatusCode)
                        {
                            Console.WriteLine($"Request: {response.RequestMessage} \n Result: {response}", "error");
                            return(null);
                        }
                        else
                        {
                            Console.WriteLine($"Request: {response.RequestMessage} Result: {response.StatusCode}", "info");
                            HassEntity state = JsonConvert.DeserializeObject <HassEntity>(await response.Content.ReadAsStringAsync());
                            return(state);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"{e}", "error");
                Console.WriteLine($"{e.Message}", "error");
                Console.WriteLine($"{e.StackTrace}", "error");
                Console.WriteLine($"{e.InnerException}", "error");
                return(null);
            }
        }
Exemplo n.º 2
0
        private void SaveEntityState(string topic, string message)
        {
            var hub        = _hubContext.Clients.All;
            var topicSplit = topic.Split("/");
            var entity     = new HassEntity();

            if (topicSplit[4] == "state")
            {
                entity.EntityId = topicSplit[3];
                entity.State    = message;
                if (entity.EntityId == "dark_sky_summary" ||
                    entity.EntityId.Contains("dark_sky_precip") && !entity.EntityId.Contains("probability"))
                {
                    hub.InvokeAsync("OnIconUpdate", entity);
                }
                else if (entity.EntityId.StartsWith("dark_sky"))
                {
                    hub.InvokeAsync("OnWeatherUpdate", entity);
                }
                else if (entity.EntityId.EndsWith("door"))
                {
                    hub.InvokeAsync("OnDoorUpdate", entity);
                }
            }
            //else if (topicSplit[4] == "friendly_name")
            //{
            //    entity.EntityId = topicSplit[3];
            //    entity.Attributes.FriendlyName = message;
            //}
            //else if (topicSplit[4] == "entity_picture")
            //{
            //    entity.EntityId = topicSplit[3];
            //    entity.Message = message;
            //    hub.InvokeAsync("OnIconUpdate", entity);

            //}
        }
Exemplo n.º 3
0
 public async Task SendHassEntitiesToView(HassEntity entity)
 {
     await Clients.All.InvokeAsync("OnEntityUpdate", entity);
 }