コード例 #1
0
        public static async void PostHttpSensorRequest(string entityId, string state, string deviceClass, string uom, string friendlyName)
        {
            try
            {
                using (HttpClient)
                {
                    var bearerValue = $"Bearer {HassUtil.GetApiKey()}";

                    using (HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("POST"), $"http://192.168.1.2:8123/api/states/{entityId}"))
                    {
                        request.Headers.TryAddWithoutValidation("Authorization", bearerValue);

                        request.Content = new StringContent("{\"state\": \"" + state + "\", \"attributes\": {\"unit_of_measurement\": \"" + uom + "\",\"device_class\": \"" + deviceClass + "\", \"friendly_name\": \"" + friendlyName + "\"}}");
                        request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
                        var response = await HttpClient.SendAsync(request);

                        if (!response.IsSuccessStatusCode)
                        {
                            Console.WriteLine($"Request: {response.RequestMessage} Result: {response}", "error");
                        }
                        else
                        {
                            { Console.WriteLine($"Request: {response.RequestMessage} Result: {response.StatusCode}", "info"); }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"{e}", "error");
                Console.WriteLine($"{e.Message}", "error");
                Console.WriteLine($"{e.StackTrace}", "error");
                Console.WriteLine($"{e.InnerException}", "error");
            }
        }
コード例 #2
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);
            }
        }
コード例 #3
0
        public static async Task <bool> PostHttpServiceRequest(string entityId, string domain, string service)
        {
            try
            {
                using (HttpClient)
                {
                    var bearerValue = $"Bearer {HassUtil.GetApiKey()}";

                    using (HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("POST"), $"http://192.168.1.2:8123/api/services/{domain}/{service}"))
                    {
                        request.Headers.TryAddWithoutValidation("Authorization", bearerValue);

                        request.Content = new StringContent("{\"entity_id\":\"" + domain + "." + entityId + "\"}");
                        request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
                        var response = await HttpClient.SendAsync(request);

                        if (!response.IsSuccessStatusCode)
                        {
                            Console.WriteLine($"Request: {response.RequestMessage} Result: {response}", "error");
                        }
                        else
                        {
                            Console.WriteLine($"Request: {response.RequestMessage} Result: {response.StatusCode}", "info");
                        }

                        return(response.IsSuccessStatusCode);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"{e}", "error");
                Console.WriteLine($"{e.Message}", "error");
                Console.WriteLine($"{e.StackTrace}", "error");
                Console.WriteLine($"{e.InnerException}", "error");

                return(false);
            }
        }