diff options
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 105 |
1 files changed, 95 insertions, 10 deletions
| @@ -1,17 +1,102 @@ | |||
| 1 | # embassy-ha | 1 | # embassy-ha |
| 2 | 2 | ||
| 3 | Home Assistant MQTT device library for embassy. | 3 | [](https://crates.io/crates/embassy-ha) |
| 4 | [](https://docs.rs/embassy-ha) | ||
| 4 | 5 | ||
| 5 | To create a device use the [`new`] function. | 6 | MQTT Home Assistant integration library for the [Embassy](https://embassy.dev/) async runtime. |
| 6 | 7 | ||
| 7 | After the device is created you should create one or more entities using functions such as | 8 | ## Features |
| 8 | [`create_button`]/[`create_sensor`]/... | ||
| 9 | 9 | ||
| 10 | Once the entities have been created either [`run`] or [`connect_and_run`] should be called in a | 10 | - Support for multiple entity types: sensors, buttons, switches, binary sensors, numbers, device trackers |
| 11 | seperate task. | 11 | - Built on top of Embassy's async runtime for embedded systems |
| 12 | - No-std compatible | ||
| 13 | - Automatic MQTT discovery for Home Assistant | ||
| 14 | - No runtime allocation | ||
| 12 | 15 | ||
| 13 | There are various examples you can run locally (ex: `cargo run --features tracing --example | 16 | ## Installation |
| 14 | button`) assuming you have a home assistant instance running. To run the examples the | ||
| 15 | environment variable `MQTT_ADDRESS` should be set to the mqtt server used by home assistant. | ||
| 16 | 17 | ||
| 17 | License: MIT OR Apache-2.0 | 18 | ```bash |
| 19 | cargo add embassy-ha | ||
| 20 | ``` | ||
| 21 | |||
| 22 | ## Quick Start | ||
| 23 | |||
| 24 | This example does not compile as-is because it requires device-specific setup, but it should | ||
| 25 | be easy to adapt if you already have Embassy running on your microcontroller. | ||
| 26 | |||
| 27 | ```rust | ||
| 28 | use embassy_executor::Spawner; | ||
| 29 | use embassy_ha::{DeviceConfig, SensorConfig, SensorClass, StateClass}; | ||
| 30 | use embassy_time::Timer; | ||
| 31 | use static_cell::StaticCell; | ||
| 32 | |||
| 33 | static HA_RESOURCES: StaticCell<embassy_ha::DeviceResources> = StaticCell::new(); | ||
| 34 | |||
| 35 | #[embassy_executor::main] | ||
| 36 | async fn main(spawner: Spawner) { | ||
| 37 | // Initialize your network stack | ||
| 38 | // This is device specific | ||
| 39 | let stack: embassy_net::Stack<'static>; | ||
| 40 | |||
| 41 | // Create a Home Assistant device | ||
| 42 | let device = embassy_ha::new( | ||
| 43 | HA_RESOURCES.init(Default::default()), | ||
| 44 | DeviceConfig { | ||
| 45 | device_id: "my-device", | ||
| 46 | device_name: "My Device", | ||
| 47 | manufacturer: "ACME Corp", | ||
| 48 | model: "Model X", | ||
| 49 | }, | ||
| 50 | ); | ||
| 51 | |||
| 52 | // Create a temperature sensor | ||
| 53 | let sensor_config = SensorConfig { | ||
| 54 | class: SensorClass::Temperature, | ||
| 55 | state_class: StateClass::Measurement, | ||
| 56 | unit: Some(embassy_ha::constants::HA_UNIT_TEMPERATURE_CELSIUS), | ||
| 57 | ..Default::default() | ||
| 58 | }; | ||
| 59 | let mut sensor = embassy_ha::create_sensor(&device, "temp-sensor", sensor_config); | ||
| 60 | |||
| 61 | // Spawn the Home Assistant communication task | ||
| 62 | spawner.spawn(ha_task(stack, device)).unwrap(); | ||
| 63 | |||
| 64 | // Main loop - read and publish temperature | ||
| 65 | loop { | ||
| 66 | // let temperature = read_temperature().await; | ||
| 67 | sensor.publish(temperature); | ||
| 68 | Timer::after_secs(60).await; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | #[embassy_executor::task] | ||
| 73 | async fn ha_task(stack: embassy_net::Stack<'static>, device: embassy_ha::Device<'static>) { | ||
| 74 | embassy_ha::connect_and_run(stack, device, "mqtt-broker-address").await; | ||
| 75 | } | ||
| 76 | ``` | ||
| 77 | |||
| 78 | ## Examples | ||
| 79 | |||
| 80 | The repository includes several examples demonstrating different entity types. To run an example: | ||
| 81 | |||
| 82 | ```bash | ||
| 83 | export MQTT_ADDRESS="mqtt://your-mqtt-broker:1883" | ||
| 84 | cargo run --example sensor | ||
| 85 | ``` | ||
| 86 | |||
| 87 | Available examples: | ||
| 88 | - `sensor` - Temperature and humidity sensors | ||
| 89 | - `button` - Triggerable button entity | ||
| 90 | - `switch` - On/off switch control | ||
| 91 | - `binary_sensor` - Binary state sensor | ||
| 92 | - `number` - Numeric input entity | ||
| 93 | - `device_tracker` - Location tracking entity | ||
| 94 | |||
| 95 | ## License | ||
| 96 | |||
| 97 | Licensed under either of: | ||
| 98 | |||
| 99 | - Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) | ||
| 100 | - MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) | ||
| 101 | |||
| 102 | at your option. | ||
