Update
This commit is contained in:
@@ -19,7 +19,6 @@ public sealed class ShowerMonitorEntityService(
|
||||
StateEntityId,
|
||||
DifferentialEntityId,
|
||||
HumidityRateEntityId,
|
||||
TodaySessionCountEntityId,
|
||||
LastShowerDurationEntityId,
|
||||
LastShowerTimeEntityId,
|
||||
CleanupButtonEntityId
|
||||
@@ -42,7 +41,6 @@ public sealed class ShowerMonitorEntityService(
|
||||
{
|
||||
try
|
||||
{
|
||||
logger.LogInformation("Creating MQTT entity: {EntityId}", entity.EntityId);
|
||||
await entityManager.CreateEntityAsync(entity).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -67,37 +65,19 @@ public sealed class ShowerMonitorEntityService(
|
||||
SetEntityStateSafe(StateEntityId, ctx.State.ToString().ToLowerInvariant()),
|
||||
UpdateDifferentialAsync(ctx.Differential, timestamp),
|
||||
UpdateHumidityRateAsync(ctx.HumidityRate, timestamp),
|
||||
UpdateTodaySessionCountAsync(ctx.TodaySessionCount, timestamp),
|
||||
UpdateLastShowerAsync(ctx.LastCompletedSession, timestamp)
|
||||
};
|
||||
|
||||
await Task.WhenAll(updateTasks).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task ResetEntitiesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
logger.LogInformation("Resetting ShowerMonitor entity states to defaults");
|
||||
|
||||
await Task.WhenAll(
|
||||
SetEntityStateSafe(ActiveEntityId, "off"),
|
||||
SetEntityStateSafe(StateEntityId, ShowerState.Idle.ToString().ToLowerInvariant()),
|
||||
SetEntityStateSafe(DifferentialEntityId, "0"),
|
||||
SetEntityStateSafe(HumidityRateEntityId, "0"),
|
||||
SetEntityStateSafe(TodaySessionCountEntityId, "0"),
|
||||
SetEntityStateSafe(LastShowerDurationEntityId, "0"),
|
||||
SetEntityStateSafe(LastShowerTimeEntityId, "unknown")
|
||||
).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async Task CleanupEntitiesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
try
|
||||
{
|
||||
var removeTasks = CleanableEntityIds.Select(entityId => entityManager.RemoveEntity(entityId));
|
||||
var removeTasks = CleanableEntityIds.Select(entityManager.RemoveEntity);
|
||||
await Task.WhenAll(removeTasks).ConfigureAwait(false);
|
||||
|
||||
logger.LogInformation("ShowerMonitor entities cleaned up successfully");
|
||||
@@ -172,16 +152,6 @@ public sealed class ShowerMonitorEntityService(
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task UpdateTodaySessionCountAsync(int count, string timestamp)
|
||||
{
|
||||
await SetEntityStateSafe(TodaySessionCountEntityId, count.ToString(CultureInfo.InvariantCulture)).ConfigureAwait(false);
|
||||
await SetEntityAttributesSafe(TodaySessionCountEntityId, new
|
||||
{
|
||||
friendly_name = "Showers today",
|
||||
last_updated = timestamp
|
||||
}).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task UpdateLastShowerAsync(ShowerSession? session, string timestamp)
|
||||
{
|
||||
if (session is null)
|
||||
@@ -235,6 +205,7 @@ public sealed class ShowerMonitorEntityService(
|
||||
{
|
||||
var totalMinutes = (int)ts.TotalMinutes;
|
||||
var seconds = ts.Seconds;
|
||||
|
||||
return $"{totalMinutes}m {seconds}s";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user