Add input_boolean for water heater preparation logic
Refreshed logic to allow users to toggle hot water preparation. Introduced new `input_boolean.boiler_prepare_hot_water` and updated config, services, and tests to ensure water readiness only when required.
This commit is contained in:
@@ -14,8 +14,10 @@ public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoo
|
||||
private const string CurrentPriceEntityId = "sensor.nordpool_current_price";
|
||||
private const string TodayPricesEntityId = "sensor.nordpool_today_prices";
|
||||
private const string TomorrowPricesEntityId = "sensor.nordpool_tomorrow_prices";
|
||||
private const string TodayTomorrowPricesEntityId = "sensor.nordpool_today_tomorrow_prices";
|
||||
private const string CheapestIntervalsEntityId = "sensor.nordpool_cheapest_intervals";
|
||||
private const string BoilerWaterReadyEntityId = "sensor.nordpool_boiler_water_ready";
|
||||
private const string BoilerSkipPriceCapEntityId = "switch.nordpool_boiler_skip_price_cap";
|
||||
|
||||
private static readonly string[] AllEntityIds =
|
||||
[
|
||||
@@ -23,8 +25,10 @@ public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoo
|
||||
CurrentPriceEntityId,
|
||||
TodayPricesEntityId,
|
||||
TomorrowPricesEntityId,
|
||||
TodayTomorrowPricesEntityId,
|
||||
CheapestIntervalsEntityId,
|
||||
BoilerWaterReadyEntityId
|
||||
BoilerWaterReadyEntityId,
|
||||
BoilerSkipPriceCapEntityId
|
||||
];
|
||||
|
||||
public async Task CreateEntitiesAsync(string? unitOfMeasurement)
|
||||
@@ -54,8 +58,10 @@ public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoo
|
||||
ResetCurrentPriceAsync(),
|
||||
ResetTodayPricesAsync(),
|
||||
ResetTomorrowPricesAsync(),
|
||||
ResetTodayTomorrowPricesAsync(),
|
||||
ResetCheapestIntervalsAsync(),
|
||||
ResetBoilerWaterReadyAsync()
|
||||
ResetBoilerWaterReadyAsync(),
|
||||
ResetBoilerSkipPriceCapAsync()
|
||||
);
|
||||
|
||||
logger.LogInformation("All NordPool entity states reset to defaults");
|
||||
@@ -108,6 +114,18 @@ public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoo
|
||||
});
|
||||
}
|
||||
|
||||
private async Task ResetTodayTomorrowPricesAsync()
|
||||
{
|
||||
await SetEntityStateSafe(TodayTomorrowPricesEntityId, "unknown");
|
||||
await SetEntityAttributesSafe(TodayTomorrowPricesEntityId, new
|
||||
{
|
||||
prices = Array.Empty<NordPoolDayPrice>(),
|
||||
prices_count = 0,
|
||||
friendly_name = "NordPool today and tomorrow prices",
|
||||
last_updated = (string?)null
|
||||
});
|
||||
}
|
||||
|
||||
private async Task ResetCheapestIntervalsAsync()
|
||||
{
|
||||
await SetEntityStateSafe(CheapestIntervalsEntityId, "0");
|
||||
@@ -139,6 +157,11 @@ public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoo
|
||||
});
|
||||
}
|
||||
|
||||
private async Task ResetBoilerSkipPriceCapAsync()
|
||||
{
|
||||
await SetEntityStateSafe(BoilerSkipPriceCapEntityId, "OFF");
|
||||
}
|
||||
|
||||
public async Task UpdateEntitiesAsync(EntityUpdateContext ctx)
|
||||
{
|
||||
var utcNow = DateTime.UtcNow;
|
||||
@@ -185,6 +208,19 @@ public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoo
|
||||
last_updated = timestamp
|
||||
});
|
||||
|
||||
var mergedPrices = MergeTodayTomorrowPrices(ctx.TodayNordPoolPrice, ctx.TomorrowNordPoolPrice);
|
||||
var updateTimeState = utcNow.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss");
|
||||
logger.LogInformation("Updating sensor.nordpool_today_tomorrow_prices with {Count} merged prices", mergedPrices.Count);
|
||||
|
||||
await SetEntityStateSafe(TodayTomorrowPricesEntityId, updateTimeState);
|
||||
await SetEntityAttributesSafe(TodayTomorrowPricesEntityId, new
|
||||
{
|
||||
prices = mergedPrices,
|
||||
prices_count = mergedPrices.Count,
|
||||
friendly_name = "NordPool today and tomorrow prices",
|
||||
last_updated = timestamp
|
||||
});
|
||||
|
||||
var cheapestCount = ctx.CheapestIntervals?.Count ?? 0;
|
||||
logger.LogInformation("Updating sensor.nordpool_cheapest_intervals with {Count} intervals, is_now_cheap={IsNow}", cheapestCount, ctx.IsNowCheap);
|
||||
|
||||
@@ -195,6 +231,7 @@ public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoo
|
||||
configured_hours = ctx.ConfiguredHours,
|
||||
configured_interval_count = ctx.ConfiguredIntervalCount,
|
||||
configured_price_cap = ctx.ConfiguredPriceCap,
|
||||
skip_price_cap = ctx.SkipPriceCap,
|
||||
configured_ready_by = ctx.ConfiguredReadyBy,
|
||||
total_heating_minutes = cheapestCount * 15,
|
||||
is_now_cheap = ctx.IsNowCheap,
|
||||
@@ -257,13 +294,34 @@ public class NordPoolEntityService(IEntityManager entityManager, ILogger<NordPoo
|
||||
},
|
||||
new NordPoolEntity("sensor", "nordpool_today_prices", "NordPool today prices"),
|
||||
new NordPoolEntity("sensor", "nordpool_tomorrow_prices", "NordPool tomorrow prices"),
|
||||
new NordPoolEntity("sensor", "nordpool_today_tomorrow_prices", "NordPool today and tomorrow prices"),
|
||||
new NordPoolEntity("sensor", "nordpool_cheapest_intervals", "NordPool cheapest boiler intervals"),
|
||||
new NordPoolEntity("sensor", "nordpool_boiler_water_ready", "Boiler water ready")
|
||||
new NordPoolEntity("sensor", "nordpool_boiler_water_ready", "Boiler water ready"),
|
||||
new NordPoolEntity("switch", "nordpool_boiler_skip_price_cap", "Skip price cap, heat until ready")
|
||||
{
|
||||
AdditionalConfig = new
|
||||
{
|
||||
optimistic = true,
|
||||
icon = "mdi:water-thermometer-outline"
|
||||
}
|
||||
}
|
||||
]);
|
||||
|
||||
return entries;
|
||||
}
|
||||
|
||||
private static IReadOnlyList<NordPoolDayPrice> MergeTodayTomorrowPrices(
|
||||
NordPoolPrice? today, NordPoolPrice? tomorrow)
|
||||
{
|
||||
var todayList = today?.Prices ?? [];
|
||||
var tomorrowList = tomorrow?.Prices ?? [];
|
||||
return todayList
|
||||
.Concat(tomorrowList)
|
||||
.Where(p => p?.Start != null)
|
||||
.OrderBy(p => p!.Start!.Value)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
internal async Task SetEntityStateSafe(string entityId, string state)
|
||||
{
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user