use core::{future::Future, ops::Deref};
use mqttrs::QoS;
use serde::{
ser::{Error as _, SerializeStruct},
Serialize, Serializer,
};
use crate::{
device_id, device_type, homeassistant::ser::DiscoverySerializer, io::publish, Error,
McutieTask, MqttMessage, Payload, Publishable, Topic, TopicString, DATA_CHANNEL,
};
pub mod binary_sensor;
pub mod button;
pub mod light;
pub mod sensor;
mod ser;
const HA_STATUS_TOPIC: Topic<&'static str> = Topic::General("homeassistant/status");
const STATE_ONLINE: &str = "online";
const STATE_OFFLINE: &str = "offline";
pub trait Component: Serialize {
type State;
fn platform() -> &'static str;
fn publish_state<T: Deref<Target = str>>(
&self,
topic: &Topic<T>,
state: Self::State,
) -> impl Future<Output = Result<(), Error>>;
}
impl<'t, T, L, const S: usize> McutieTask<'t, T, L, S>
where
T: Deref<Target = str> + 't,
L: Publishable + 't,
{
pub(super) async fn ha_after_connected(&self) {
let _ = HA_STATUS_TOPIC.subscribe(false).await;
}
pub(super) async fn ha_handle_update(
&self,
topic: &Topic<TopicString>,
payload: &Payload,
) -> bool {
if topic == &HA_STATUS_TOPIC {
if payload.as_ref() == STATE_ONLINE.as_bytes() {
DATA_CHANNEL.send(MqttMessage::HomeAssistantOnline).await;
}
true
} else {
false
}
}
}
impl<T: Deref<Target = str>> Serialize for Topic<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut topic = TopicString::new();
self.to_string(&mut topic)
.map_err(|_| S::Error::custom("topic was too large to serialize"))?;
serializer.serialize_str(&topic)
}
}
fn name_or_device<S>(name: &Option<&str>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(name.unwrap_or_else(|| device_type()))
}
#[derive(Clone, Copy, Default)]
pub struct Device<'a> {
pub name: Option<&'a str>,
pub configuration_url: Option<&'a str>,
}
impl Device<'_> {
pub const fn new() -> Self {
Self {
name: None,
configuration_url: None,
}
}
}
impl Serialize for Device<'_> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut len = 2;
if self.configuration_url.is_some() {
len += 1;
}
let mut serializer = serializer.serialize_struct("Device", len)?;
serializer.serialize_field("name", self.name.unwrap_or_else(|| device_type()))?;
serializer.serialize_field("ids", device_id())?;
if let Some(cu) = self.configuration_url {
serializer.serialize_field("cu", cu)?;
} else {
serializer.skip_field("cu")?;
}
serializer.end()
}
}
#[derive(Clone, Copy, Default, Serialize)]
pub struct Origin<'a> {
#[serde(serialize_with = "name_or_device")]
pub name: Option<&'a str>,
}
impl Origin<'_> {
pub const fn new() -> Self {
Self { name: None }
}
}
pub struct Entity<'a, const A: usize, C: Component> {
pub device: Device<'a>,
pub origin: Origin<'a>,
pub object_id: &'a str,
pub unique_id: Option<&'a str>,
pub name: &'a str,
pub availability: AvailabilityTopics<'a, A>,
pub state_topic: Option<Topic<&'a str>>,
pub command_topic: Option<Topic<&'a str>>,
pub component: C,
}
impl<const A: usize, C: Component> Entity<'_, A, C> {
pub async fn publish_discovery(&self) -> Result<(), Error> {
let mut topic = TopicString::new();
topic
.push_str(option_env!("HA_DISCOVERY_PREFIX").unwrap_or("homeassistant"))
.map_err(|_| Error::TooLarge)?;
topic.push('/').map_err(|_| Error::TooLarge)?;
topic.push_str(C::platform()).map_err(|_| Error::TooLarge)?;
topic.push('/').map_err(|_| Error::TooLarge)?;
topic
.push_str(self.object_id)
.map_err(|_| Error::TooLarge)?;
topic.push_str("/config").map_err(|_| Error::TooLarge)?;
let mut payload = Payload::new();
payload.serialize_json(self).map_err(|_| Error::TooLarge)?;
publish(&topic, &payload, QoS::AtMostOnce, false).await
}
pub async fn publish_state(&self, state: C::State) -> Result<(), Error> {
if let Some(topic) = self.state_topic {
self.component.publish_state(&topic, state).await
} else {
Err(Error::Invalid)
}
}
}
#[allow(missing_docs)]
pub enum AvailabilityState {
Online,
Offline,
}
impl AsRef<[u8]> for AvailabilityState {
fn as_ref(&self) -> &'static [u8] {
match self {
Self::Online => STATE_ONLINE.as_bytes(),
Self::Offline => STATE_OFFLINE.as_bytes(),
}
}
}
pub enum AvailabilityTopics<'a, const A: usize> {
None,
All([Topic<&'a str>; A]),
Any([Topic<&'a str>; A]),
Latest([Topic<&'a str>; A]),
}
impl<const A: usize, C: Component> Serialize for Entity<'_, A, C> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let outer = DiscoverySerializer {
discovery: self,
inner: serializer,
};
self.component.serialize(outer)
}
}