Ejemplo n.º 1
0
        private async Task InitializeAsync(UserModel userModel)
        {
            if (userModel == null)
            {
                userModel = await HomeAssistantHttpClient.GetUserData();
            }

            var connectedDevices = await HomeAssistantHttpClient.GetConnectedDevices();

            foreach (var roomEntry in userModel.Rooms)
            {
                var roomModel = new RoomModel()
                {
                    RoomType = (RoomType)Enum.Parse(typeof(RoomType), roomEntry.Type.RemoveWhitespaces()),
                    Name     = roomEntry.Name,
                    Devices  = new ObservableCollection <IDeviceModel>(connectedDevices.Where((IDeviceModel deviceModel) =>
                    {
                        foreach (var deviceEntry in roomEntry.Devices)
                        {
                            if (deviceEntry.Id == deviceModel.Id)
                            {
                                return(true);
                            }
                        }

                        return(false);
                    }))
                };

                Device.BeginInvokeOnMainThread(() =>
                {
                    RoomViewModels.Add(new RoomViewModel(roomModel));
                });
            }
        }
Ejemplo n.º 2
0
        public override void UpdateData()
        {
            Task.Run(async() => {
                dynamic deviceData = await HomeAssistantHttpClient.GetAsync(string.Format("action/{0}/*", Id));

                if (deviceData == null)
                {
                    return;
                }

                // Avoid setting values via properties which would cause PUT requests sent to server

                ((MiKettleModel)DeviceModel).TemperatureCurrent = deviceData.temperature.current;
                ((MiKettleModel)DeviceModel).TemperatureSet     = deviceData.temperature.set;
                ((MiKettleModel)DeviceModel).KeepWarmTimeLimit  = deviceData.keep_warm.time_limit;
                ((MiKettleModel)DeviceModel).KeepWarmType       = (KeepWarmType)deviceData.keep_warm.type;
                ((MiKettleModel)DeviceModel).Action             = (Model.Devices.Action)deviceData.action;
                ((MiKettleModel)DeviceModel).Mode     = (Mode)deviceData.mode;
                ((MiKettleModel)DeviceModel).BoilMode = (BoilMode)deviceData.boil_mode;

                NotifyPropertyChanged(nameof(TemperatureCurrent));
                NotifyPropertyChanged(nameof(TemperatureSet));
                NotifyPropertyChanged(nameof(KeepWarmTimeLimit));
                NotifyPropertyChanged(nameof(KeepWarmType));
                NotifyPropertyChanged(nameof(Action));
                NotifyPropertyChanged(nameof(Mode));
                NotifyPropertyChanged(nameof(BoilMode));
            });
        }
Ejemplo n.º 3
0
        public App()
        {
            InitializeComponent();

            Task.Run(async() => {
                var userData = await HomeAssistantHttpClient.GetUserData();

                if (userData == null)
                {
                    await NavigationService.Navigation.NavigateToAsync <LoginViewModel>();
                    return;
                }

                await NavigationService.Navigation.NavigateToAsync <HomeViewModel>(userData);
            }).Wait();
        }
        public CreateRoomActionViewModel()
        {
            roomModel        = new RoomModel();
            AvailableDevices = new ObservableCollection <IDeviceModel>();

            Task.Run(async() => {
                var availableDevices = await HomeAssistantHttpClient.GetConnectedDevices();

                if (availableDevices == null && availableDevices.Count == 0)
                {
                    return;
                }

                AvailableDevices = new ObservableCollection <IDeviceModel>(availableDevices);
            });

            CreateRoomCommand = new Command(() => {
                return;
            });
        }
Ejemplo n.º 5
0
        public LoginViewModel()
        {
            LoginRequestCommand = new Command(async() => {
                if (string.IsNullOrEmpty(Username))
                {
                    return;
                }

                if (string.IsNullOrEmpty(Password))
                {
                    return;
                }

                HttpStatusCode status = await HomeAssistantHttpClient.RequestLogin(Username, Password);

                switch (status)
                {
                case HttpStatusCode.OK:
                    await NavigationService.Navigation.NavigateToAsync <HomeViewModel>();
                    return;

                case HttpStatusCode.RequestTimeout:
                    await Application.Current.MainPage.DisplayAlert("Connection error.", "Connection timed out.", "OK");
                    return;

                case HttpStatusCode.Unauthorized:
                    await Application.Current.MainPage.DisplayAlert("Authorization error.", "Provided credentials are invalid.", "OK");
                    return;

                case HttpStatusCode.ServiceUnavailable:
                    await Application.Current.MainPage.DisplayAlert("Service unavailable.", "Server is temporarily down. Try again later.", "OK");
                    return;

                default:
                    await Application.Current.MainPage.DisplayAlert("Unknown error.", "Unknown error occured.", "OK");
                    return;
                }
            });
        }