This commit is contained in:
Mateusz Gruszczyński
2026-09-16 14:24:40 +02:00
parent d0ae3d0f6a
commit 3f9acb1675
28 changed files with 726 additions and 127 deletions
+56 -12
View File
@@ -9,14 +9,25 @@ const SUPERVISOR_AUTH_MODE: &str = "supervisor";
const SUPERVISOR_TOKEN_ENV: &str = "SUPERVISOR_TOKEN";
const SUPERVISOR_BASE_URL: &str = "http://supervisor/core/";
pub fn uses_supervisor_auth() -> bool {
pub fn supervisor_detected() -> bool {
env::var(SUPERVISOR_AUTH_ENV)
.map(|value| value.trim().eq_ignore_ascii_case(SUPERVISOR_AUTH_MODE))
.unwrap_or(false)
}
pub fn auth_mode() -> &'static str {
if uses_supervisor_auth() {
pub fn supervisor_token_detected() -> bool {
supervisor_detected()
&& env::var(SUPERVISOR_TOKEN_ENV)
.map(|token| !token.trim().is_empty())
.unwrap_or(false)
}
pub fn uses_supervisor_auth(settings: &HomeAssistantSettings) -> bool {
supervisor_detected() && !settings.manual_auth_override
}
pub fn auth_mode(settings: &HomeAssistantSettings) -> &'static str {
if uses_supervisor_auth(settings) {
SUPERVISOR_AUTH_MODE
} else {
"manual"
@@ -24,7 +35,7 @@ pub fn auth_mode() -> &'static str {
}
pub fn effective_url(settings: &HomeAssistantSettings) -> String {
if uses_supervisor_auth() {
if uses_supervisor_auth(settings) {
SUPERVISOR_BASE_URL.to_string()
} else {
settings.url.clone()
@@ -32,17 +43,15 @@ pub fn effective_url(settings: &HomeAssistantSettings) -> String {
}
pub fn token_configured(settings: &HomeAssistantSettings) -> bool {
if uses_supervisor_auth() {
env::var(SUPERVISOR_TOKEN_ENV)
.map(|token| !token.trim().is_empty())
.unwrap_or(false)
if uses_supervisor_auth(settings) {
supervisor_token_detected()
} else {
!settings.token.trim().is_empty()
}
}
fn connection(settings: &HomeAssistantSettings) -> Result<(Url, String)> {
let (raw_url, token) = if uses_supervisor_auth() {
let (raw_url, token) = if uses_supervisor_auth(settings) {
let token = env::var(SUPERVISOR_TOKEN_ENV)
.context("Home Assistant Supervisor token is not available")?;
(SUPERVISOR_BASE_URL.to_string(), token)
@@ -54,7 +63,7 @@ fn connection(settings: &HomeAssistantSettings) -> Result<(Url, String)> {
bail!("Home Assistant URL is not configured")
}
if token.trim().is_empty() {
if uses_supervisor_auth() {
if uses_supervisor_auth(settings) {
bail!("Home Assistant Supervisor token is not available")
}
bail!("Home Assistant token is not configured")
@@ -110,7 +119,7 @@ pub fn resolve_entity_id(
pub async fn test_connection(
default_client: &reqwest::Client,
settings: &HomeAssistantSettings,
) -> Result<()> {
) -> Result<Option<Value>> {
let (mut base, token) = connection(settings)?;
base = base.join("api/").context("cannot build Home Assistant API URL")?;
let response = request_client(default_client, settings)?
@@ -128,7 +137,41 @@ pub async fn test_connection(
body.chars().take(200).collect::<String>()
)
}
Ok(())
let entities = list_entities(default_client, settings).await?;
let sample = entities
.iter()
.find(|entity| {
entity
.get("entity_id")
.and_then(Value::as_str)
.map(|id| id.starts_with("sensor."))
.unwrap_or(false)
&& entity
.get("state")
.and_then(Value::as_str)
.map(|state| !matches!(state, "" | "unknown" | "unavailable"))
.unwrap_or(false)
})
.or_else(|| {
entities.iter().find(|entity| {
entity
.get("state")
.and_then(Value::as_str)
.map(|state| !matches!(state, "" | "unknown" | "unavailable"))
.unwrap_or(false)
})
})
.map(|entity| {
let attributes = entity.get("attributes").and_then(Value::as_object);
serde_json::json!({
"entity_id": entity.get("entity_id").and_then(Value::as_str).unwrap_or_default(),
"name": attributes.and_then(|value| value.get("friendly_name")).and_then(Value::as_str).unwrap_or_default(),
"state": entity.get("state").and_then(Value::as_str).unwrap_or_default(),
"unit": attributes.and_then(|value| value.get("unit_of_measurement")).and_then(Value::as_str).unwrap_or_default(),
})
});
Ok(sample)
}
pub async fn read_temperature(
@@ -213,6 +256,7 @@ mod tests {
outdoor_entity_id: "sensor.zewnatrz_temperature".into(),
sensor_stale_after_seconds: 300,
allow_invalid_tls: false,
manual_auth_override: false,
sensor_aliases,
flow_inputs: Vec::new(),
}