v0.8.0
This commit is contained in:
@@ -40,6 +40,7 @@ pub async fn read_temperature(
|
||||
default_client: &reqwest::Client,
|
||||
settings: &HomeAssistantSettings,
|
||||
entity_override: Option<&str>,
|
||||
stale_after_seconds: Option<u64>,
|
||||
) -> Result<f64> {
|
||||
if settings.url.trim().is_empty() { bail!("Home Assistant URL is not configured") }
|
||||
if settings.token.trim().is_empty() { bail!("Home Assistant token is not configured") }
|
||||
@@ -62,6 +63,13 @@ pub async fn read_temperature(
|
||||
bail!("Home Assistant returned {status}: {}", body.chars().take(200).collect::<String>())
|
||||
}
|
||||
let payload: Value = response.json().await.context("invalid Home Assistant JSON")?;
|
||||
if let Some(limit) = stale_after_seconds.filter(|value| *value > 0) {
|
||||
let updated = payload.get("last_updated").and_then(Value::as_str)
|
||||
.ok_or_else(|| anyhow!("Home Assistant last_updated is missing"))?;
|
||||
let updated = chrono::DateTime::parse_from_rfc3339(updated).context("invalid Home Assistant last_updated")?.with_timezone(&chrono::Utc);
|
||||
let age = chrono::Utc::now().signed_duration_since(updated).num_seconds().max(0) as u64;
|
||||
if age > limit { bail!("Home Assistant sensor is stale: {age}s old (limit {limit}s)") }
|
||||
}
|
||||
let state = payload.get("state").and_then(Value::as_str)
|
||||
.ok_or_else(|| anyhow!("Home Assistant state is missing"))?;
|
||||
let mut temperature: f64 = state.parse().context("Home Assistant state is not a number")?;
|
||||
|
||||
Reference in New Issue
Block a user