v0.14.4
This commit is contained in:
+35
-10
@@ -26,11 +26,7 @@ pub fn resolve_entity_id(
|
||||
) -> Option<String> {
|
||||
let requested = entity_override
|
||||
.filter(|value| !value.trim().is_empty())
|
||||
.map(str::trim)
|
||||
.unwrap_or_else(|| settings.default_entity_id.trim());
|
||||
if requested.is_empty() {
|
||||
return None;
|
||||
}
|
||||
.map(str::trim)?;
|
||||
|
||||
// Aliases are presentation-only. Accepting an alias here is a defensive
|
||||
// compatibility path for settings saved by older UI revisions or manual edits;
|
||||
@@ -48,6 +44,39 @@ pub fn resolve_entity_id(
|
||||
Some(requested.to_string())
|
||||
}
|
||||
|
||||
pub async fn test_connection(
|
||||
default_client: &reqwest::Client,
|
||||
settings: &HomeAssistantSettings,
|
||||
) -> Result<()> {
|
||||
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")
|
||||
}
|
||||
let mut base = Url::parse(settings.url.trim()).context("invalid Home Assistant URL")?;
|
||||
if !matches!(base.scheme(), "http" | "https") {
|
||||
bail!("Home Assistant URL must use http or https")
|
||||
}
|
||||
base = base.join("api/").context("cannot build Home Assistant API URL")?;
|
||||
let response = request_client(default_client, settings)?
|
||||
.get(base)
|
||||
.bearer_auth(settings.token.trim())
|
||||
.header("Accept", "application/json")
|
||||
.send()
|
||||
.await
|
||||
.context("Home Assistant request failed")?;
|
||||
if !response.status().is_success() {
|
||||
let status = response.status();
|
||||
let body = response.text().await.unwrap_or_default();
|
||||
bail!(
|
||||
"Home Assistant returned {status}: {}",
|
||||
body.chars().take(200).collect::<String>()
|
||||
)
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn read_temperature(
|
||||
default_client: &reqwest::Client,
|
||||
settings: &HomeAssistantSettings,
|
||||
@@ -136,7 +165,6 @@ mod tests {
|
||||
HomeAssistantSettings {
|
||||
url: "http://homeassistant.local:8123".into(),
|
||||
token: "token".into(),
|
||||
default_entity_id: "sensor.salon_temperature".into(),
|
||||
outdoor_entity_id: "sensor.zewnatrz_temperature".into(),
|
||||
sensor_stale_after_seconds: 300,
|
||||
allow_invalid_tls: false,
|
||||
@@ -156,10 +184,7 @@ mod tests {
|
||||
resolve_entity_id(&settings, Some("Gabinet")).as_deref(),
|
||||
Some("sensor.gabinet_temperature")
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_entity_id(&settings, None).as_deref(),
|
||||
Some("sensor.salon_temperature")
|
||||
);
|
||||
assert_eq!(resolve_entity_id(&settings, None), None);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user