Skip to content

Commit

Permalink
Implement light control and support the Pico
Browse files Browse the repository at this point in the history
  • Loading branch information
Mossop committed Dec 5, 2024
1 parent 078d90d commit 1ac3098
Show file tree
Hide file tree
Showing 12 changed files with 532 additions and 114 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ jobs:
run: cargo check --no-default-features --features serde

- name: Cargo check homeassistant
run: cargo check --no-default-features --features homeassistant
run: cargo check --no-default-features --features homeassistant,defmt

- name: Cargo check homeassistant
run: cargo check --no-default-features --features homeassistant,log

- name: Run tests
run: cargo test
Expand Down
20 changes: 8 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,30 @@ description = "A no-std, no-alloc, opinionated MQTT client for IoT devices"
default = ["homeassistant"]
homeassistant = ["serde"]
serde = ["dep:serde", "dep:serde-json-core"]
defmt = ["dep:defmt"]
log = ["dep:log"]

[dependencies]
defmt = "0.3.10"
defmt = { version = "0.3.10", optional = true }
log = { version = "0.4.22", optional = true }
embassy-net = { version = "0.5.0", features = [
"dns",
"medium-ip",
"medium-ethernet",
"proto-ipv4",
"tcp",
"defmt",
] }
embassy-time = "0.3.2"
serde = { version = "1.0.215", default-features = false, features = [
"derive",
], optional = true }
serde-json-core = { version = "0.6.0", optional = true }
embassy-futures = "0.1.1"
embassy-sync = { version = "0.6.1", features = ["defmt"] }
embedded-io-async = { version = "0.6.1", features = ["defmt-03"] }
embassy-sync = "0.6.1"
embedded-io-async = "0.6.1"
mqttrs = { version = "0.4.1", default-features = false }
heapless = "0.7.0"
embedded-io = { version = "0.6.1", features = ["defmt-03"] }
embedded-io = "0.6.1"
once_cell = { version = "1.20.2", default-features = false, features = [
"critical-section",
] }
Expand Down
9 changes: 9 additions & 0 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ impl<const N: usize> Buffer<N> {
Ok(())
}

#[cfg(feature = "serde")]
pub fn deserialize_json<'a, T: serde::Deserialize<'a>>(
&'a self,
) -> Result<T, serde_json_core::de::Error> {
let (result, _) = serde_json_core::from_slice(self)?;

Ok(result)
}

/// The number of bytes available for writing into this buffer.
pub fn available(&self) -> usize {
N - self.cursor
Expand Down
77 changes: 77 additions & 0 deletions src/fmt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#![macro_use]

#[cfg(not(feature = "defmt"))]
use core::fmt;

#[cfg(feature = "defmt")]
pub(crate) use ::defmt::Debug2Format;

#[cfg(not(feature = "defmt"))]
pub(crate) struct Debug2Format<D: fmt::Debug>(pub(crate) D);

#[cfg(feature = "log")]
impl<D: fmt::Debug> fmt::Debug for Debug2Format<D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

#[collapse_debuginfo(yes)]
macro_rules! trace {
($s:literal $(, $x:expr)* $(,)?) => {
#[cfg(feature = "defmt")]
::defmt::trace!($s $(, $x)*);
#[cfg(feature = "log")]
::log::trace!($s $(, $x)*);
#[cfg(not(any(feature="defmt", feature="log")))]
let _ = ($( & $x ),*);
};
}

#[collapse_debuginfo(yes)]
macro_rules! debug {
($s:literal $(, $x:expr)* $(,)?) => {
#[cfg(feature = "defmt")]
::defmt::debug!($s $(, $x)*);
#[cfg(feature = "log")]
::log::debug!($s $(, $x)*);
#[cfg(not(any(feature="defmt", feature="log")))]
let _ = ($( & $x ),*);
};
}

#[collapse_debuginfo(yes)]
macro_rules! info {
($s:literal $(, $x:expr)* $(,)?) => {
#[cfg(feature = "defmt")]
::defmt::info!($s $(, $x)*);
#[cfg(feature = "log")]
::log::info!($s $(, $x)*);
#[cfg(not(any(feature="defmt", feature="log")))]
let _ = ($( & $x ),*);
};
}

#[collapse_debuginfo(yes)]
macro_rules! warn {
($s:literal $(, $x:expr)* $(,)?) => {
#[cfg(feature = "defmt")]
::defmt::warn!($s $(, $x)*);
#[cfg(feature = "log")]
::log::warn!($s $(, $x)*);
#[cfg(not(any(feature="defmt", feature="log")))]
let _ = ($( & $x ),*);
};
}

#[collapse_debuginfo(yes)]
macro_rules! error {
($s:literal $(, $x:expr)* $(,)?) => {
#[cfg(feature = "defmt")]
::defmt::error!($s $(, $x)*);
#[cfg(feature = "log")]
::log::error!($s $(, $x)*);
#[cfg(not(any(feature="defmt", feature="log")))]
let _ = ($( & $x ),*);
};
}
24 changes: 22 additions & 2 deletions src/homeassistant/binary_sensor.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
//! Tools for publishing a [Home Assistant binary sensor](https://www.home-assistant.io/integrations/binary_sensor.mqtt/).
use core::ops::Deref;

use serde::Serialize;
use serde::{Deserialize, Serialize};

use crate::{homeassistant::Component, Error, Publishable, Topic};

/// The state of the sensor. Can be easily converted to or from a [`bool`].
#[derive(Clone, Copy, PartialEq, Eq)]
#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(from = "&str", into = "&'static str")]
pub enum BinarySensorState {
On,
Off,
}

impl From<BinarySensorState> for &'static str {
fn from(state: BinarySensorState) -> Self {
match state {
BinarySensorState::On => "ON",
BinarySensorState::Off => "OFF",
}
}
}

impl<'a> From<&'a str> for BinarySensorState {
fn from(st: &'a str) -> Self {
if st == "ON" {
Self::On
} else {
Self::Off
}
}
}

impl From<bool> for BinarySensorState {
fn from(val: bool) -> Self {
if val {
Expand Down
Loading

0 comments on commit 1ac3098

Please sign in to comment.