public async Task Update()
        {
            var automationType = await InputSelect.LoadFromEntityId(connector, InputSelect.Name.AirPurifier2sAutomationType);

            if (automationType.State != AutomationType)
            {
                ClearConditions();
                return;
            }

            var airPurifier = await XiaomiAirPurifier.LoadFromEntityId(connector, XiaomiAirPurifier.Name.AirPurifier2S);

            if (turnOffCondition.CheckFulfilled(airPurifier.Aqi <= turningOffValue))
            {
                if (airPurifier.State == "on")
                {
                    logger.LogInformation("Turning off {EntityId}, because aqi is lower or equal than {TurningOffValue} for {ForTime} minutes",
                                          airPurifier.EntityId, turningOffValue, forTurnOnTime);
                    await airPurifier.TurnOff();
                }
            }

            if (turnOnCondition.CheckFulfilled(airPurifier.Aqi > turningOffValue))
            {
                if (airPurifier.State == "off")
                {
                    logger.LogInformation("Turning on {EntityId}, because aqi is lower or equal than {TurningOffValue} for {ForTime} minutes",
                                          airPurifier.EntityId, turningOffValue, forTurnOnTime);
                    await airPurifier.TurnOn();
                }
            }

            if (airPurifier.State == "on")
            {
                var level = Math.Min((airPurifier.Aqi / 10) + 3, 16);

                if (airPurifier.PresetMode != AirPurifierPresetMode.Favorite ||
                    airPurifier.FavoriteLevel != level)
                {
                    logger.LogInformation("Changing speed of {EntityId} to {Level}",
                                          airPurifier.EntityId, level);

                    await airPurifier.SetFavoriteLevel(level);

                    await airPurifier.SetPresetMode(AirPurifierPresetMode.Favorite);
                }
            }
        }
        public async Task Update()
        {
            var automationType = await InputSelect.LoadFromEntityId(connector, InputSelect.Name.AirPurifier2sAutomationType);

            if (automationType.State != AutomationType)
            {
                ClearConditions();
                return;
            }

            var airPurifier = await XiaomiAirPurifier.LoadFromEntityId(connector, XiaomiAirPurifier.Name.AirPurifier2S);

            if (dateTimeHelper.Now.Between(new TimeSpan(9, 30, 0), new TimeSpan(12, 0, 0)))
            {
                if (airPurifier.State == "on")
                {
                    logger.LogInformation("Turning off {EntityId}, because of no one in the room", airPurifier.EntityId);
                    await airPurifier.TurnOff();
                }

                ClearConditions();
            }
            else
            {
                if (turnOffCondition.CheckFulfilled(airPurifier.Aqi <= turningOffValue))
                {
                    if (airPurifier.State == "on")
                    {
                        logger.LogInformation("Turning off {EntityId}, because aqi is lower or equal than {TurningOffValue} for {ForTime} minutes",
                                              airPurifier.EntityId, turningOffValue, forTurnOnTime);
                        await airPurifier.TurnOff();
                    }
                }

                if (turnOnCondition.CheckFulfilled(airPurifier.Aqi > turningOffValue))
                {
                    if (airPurifier.State == "off")
                    {
                        logger.LogInformation("Turning on {EntityId}, because aqi is greater or equal than {TurningOffValue} for {ForTime} minutes",
                                              airPurifier.EntityId, turningOffValue, forTurnOnTime);
                        await airPurifier.TurnOn();

                        if (airPurifier.PresetMode != AirPurifierPresetMode.Silent)
                        {
                            logger.LogInformation("Changing speed of {EntityId} to {Speed} after turning on",
                                                  airPurifier.EntityId, AirPurifierPresetMode.Silent);

                            await airPurifier.SetPresetMode(AirPurifierPresetMode.Silent);
                        }
                    }
                }

                if (airPurifier.State == "on")
                {
                    if (dateTimeHelper.Now.Between(new TimeSpan(18, 0, 0), new TimeSpan(20, 0, 0)))
                    {
                        var level = Math.Min(airPurifier.Aqi / 10 + 2, 16);

                        if (airPurifier.PresetMode != AirPurifierPresetMode.Favorite ||
                            airPurifier.FavoriteLevel != level)
                        {
                            logger.LogInformation("Changing speed of {EntityId} to {Level}",
                                                  airPurifier.EntityId, level);

                            await airPurifier.SetFavoriteLevel(level);

                            await airPurifier.SetPresetMode(AirPurifierPresetMode.Favorite);
                        }
                    }
                    else
                    {
                        if (silentSpeedCondition.CheckFulfilled(airPurifier.Aqi <= 20))
                        {
                            if (airPurifier.PresetMode != AirPurifierPresetMode.Silent)
                            {
                                logger.LogInformation("Changing speed of {EntityId} to {Speed}",
                                                      airPurifier.EntityId, AirPurifierPresetMode.Silent);

                                await airPurifier.SetPresetMode(AirPurifierPresetMode.Silent);
                            }
                        }

                        if (autoSpeedCondition.CheckFulfilled(airPurifier.Aqi > 20))
                        {
                            var level = Math.Min(airPurifier.Aqi / 10, 16);

                            if (airPurifier.PresetMode != AirPurifierPresetMode.Favorite ||
                                airPurifier.FavoriteLevel != level)
                            {
                                logger.LogInformation("Changing speed of {EntityId} to {Level}",
                                                      airPurifier.EntityId, level);

                                await airPurifier.SetFavoriteLevel(level);

                                await airPurifier.SetPresetMode(AirPurifierPresetMode.Favorite);
                            }
                        }
                    }
                }
            }
        }
Пример #3
0
        public async Task Update()
        {
            var airPurifier = await XiaomiAirPurifier.LoadFromEntityId(connector, XiaomiAirPurifier.Name.AirPurifierPro);

            if (dateTimeHelper.Now.Between(new TimeSpan(23, 0, 0), new TimeSpan(6, 0, 0)))
            {
                if (airPurifier.State == "on")
                {
                    logger.LogInformation("Turning off {EntityId}, because of night", airPurifier.EntityId);
                    await airPurifier.TurnOff();
                }

                turnOnCondition.Reset();
                turnOffCondition.Reset();

                return;
            }

            if (turnOffCondition.CheckFulfilled(airPurifier.Aqi <= turningOffValue))
            {
                if (airPurifier.State == "on")
                {
                    logger.LogInformation("Turning off {EntityId}, because aqi is lower or equal than {TurningOffValue} for {ForTime} minutes",
                                          airPurifier.EntityId, turningOffValue, forTime);
                    await airPurifier.TurnOff();
                }
            }

            if (turnOnCondition.CheckFulfilled(airPurifier.Aqi > turningOffValue))
            {
                if (airPurifier.State == "off")
                {
                    logger.LogInformation("Turning on {EntityId}, because aqi is lower or equal than {TurningOffValue} for {ForTime} minutes",
                                          airPurifier.EntityId, turningOffValue, forTime);
                    await airPurifier.TurnOn();
                }
            }

            if (airPurifier.State == "on")
            {
                if (airPurifier.Aqi < 20)
                {
                    if (airPurifier.PresetMode != AirPurifierPresetMode.Auto)
                    {
                        logger.LogInformation("Changing speed of {EntityId} to {Speed}",
                                              airPurifier.EntityId, AirPurifierPresetMode.Auto);
                        await airPurifier.SetPresetMode(AirPurifierPresetMode.Auto);
                    }
                }
                else
                {
                    var level = Math.Min((airPurifier.Aqi / 10) + 1, 16);

                    if (airPurifier.PresetMode != AirPurifierPresetMode.Favorite ||
                        airPurifier.FavoriteLevel != level)
                    {
                        logger.LogInformation("Changing speed of {EntityId} to {Level}",
                                              airPurifier.EntityId, level);

                        await airPurifier.SetFavoriteLevel(level);

                        await airPurifier.SetPresetMode(AirPurifierPresetMode.Favorite);
                    }
                }
            }
        }