1786299019
This commit is contained in:
@@ -183,30 +183,30 @@ fn toggle_action(current_state: &str, on_state: &str) -> &'static str {
|
|||||||
const REQUEST_TIMEOUT: Duration = Duration::from_secs(12);
|
const REQUEST_TIMEOUT: Duration = Duration::from_secs(12);
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct HaClient {
|
struct HaClient {
|
||||||
pub agent: ureq::Agent,
|
agent: ureq::Agent,
|
||||||
pub base_url: String,
|
base_url: String,
|
||||||
pub token: String,
|
token: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct HaCalendar {
|
struct HaCalendar {
|
||||||
pub entity_id: String,
|
entity_id: String,
|
||||||
pub name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct HaCalendarEvent {
|
struct HaCalendarEvent {
|
||||||
pub uid: String,
|
uid: String,
|
||||||
pub summary: String,
|
summary: String,
|
||||||
pub start: HaCalendarTime,
|
start: HaCalendarTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct HaCalendarTime {
|
struct HaCalendarTime {
|
||||||
#[serde(rename = "dateTime")]
|
#[serde(rename = "dateTime")]
|
||||||
pub date_time: Option<String>,
|
date_time: Option<String>,
|
||||||
pub date: Option<String>,
|
date: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
@@ -220,7 +220,7 @@ pub struct CalendarEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl HaClient {
|
impl HaClient {
|
||||||
pub fn from_environment() -> Result<Self, String> {
|
fn from_environment() -> Result<Self, String> {
|
||||||
let base_url = std::env::var("HASS_URL")
|
let base_url = std::env::var("HASS_URL")
|
||||||
.map_err(|_| "HASS_URL is not configured".to_string())?
|
.map_err(|_| "HASS_URL is not configured".to_string())?
|
||||||
.trim_end_matches('/')
|
.trim_end_matches('/')
|
||||||
@@ -272,7 +272,7 @@ impl HaClient {
|
|||||||
.map_err(|error| format!("{entity_id}: invalid response: {error}"))
|
.map_err(|error| format!("{entity_id}: invalid response: {error}"))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fetch_calendar_events(&self) -> Result<Vec<CalendarEvent>, String> {
|
fn fetch_calendar_events(&self) -> Result<Vec<CalendarEvent>, String> {
|
||||||
let now = Local::now();
|
let now = Local::now();
|
||||||
let start = now.to_rfc3339();
|
let start = now.to_rfc3339();
|
||||||
let end = (now + chrono::Duration::days(30)).to_rfc3339();
|
let end = (now + chrono::Duration::days(30)).to_rfc3339();
|
||||||
@@ -302,7 +302,7 @@ impl HaClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn format_calendar_time(value: &HaCalendarTime) -> Option<(i64, String, String)> {
|
fn format_calendar_time(value: &HaCalendarTime) -> Option<(i64, String, String)> {
|
||||||
if let Some(date_time) = value.date_time.as_deref() {
|
if let Some(date_time) = value.date_time.as_deref() {
|
||||||
let parsed = DateTime::parse_from_rfc3339(date_time).ok()?;
|
let parsed = DateTime::parse_from_rfc3339(date_time).ok()?;
|
||||||
return Some((
|
return Some((
|
||||||
|
|||||||
Reference in New Issue
Block a user