aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: b09bda2b6c8ddadf256f34400e1b2ae9df0a7c1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
//! MQTT Home Assistant integration library for the [Embassy](https://embassy.dev/) async runtime.
//!
//! # Features
//!
//! - Support for multiple entity types: sensors, buttons, switches, binary sensors, numbers, device trackers
//! - Built on top of Embassy's async runtime for embedded systems
//! - No-std compatible
//! - Automatic MQTT discovery for Home Assistant
//! - No runtime allocation
//!
//! # Installation
//!
//! ```bash
//! cargo add embassy-ha
//! ```
//!
//! # Quick Start
//!
//! This example does not compile as-is because it requires device-specific setup, but it should
//! be easy to adapt if you already have Embassy running on your microcontroller.
//!
//! ```no_run
//! use embassy_executor::Spawner;
//! use embassy_ha::{DeviceConfig, SensorConfig, SensorClass, StateClass};
//! use embassy_time::Timer;
//! use static_cell::StaticCell;
//!
//! static HA_RESOURCES: StaticCell<embassy_ha::DeviceResources> = StaticCell::new();
//!
//! #[embassy_executor::main]
//! async fn main(spawner: Spawner) {
//!     // Initialize your network stack
//!     // This is device specific
//!     let stack: embassy_net::Stack<'static>;
//! #   let stack = unsafe { core::mem::zeroed() };
//!
//!     // Create a Home Assistant device
//!     let device = embassy_ha::new(
//!         HA_RESOURCES.init(Default::default()),
//!         DeviceConfig {
//!             device_id: "my-device",
//!             device_name: "My Device",
//!             manufacturer: "ACME Corp",
//!             model: "Model X",
//!         },
//!     );
//!
//!     // Create a temperature sensor
//!     let sensor_config = SensorConfig {
//!         class: SensorClass::Temperature,
//!         state_class: StateClass::Measurement,
//!         unit: Some(embassy_ha::constants::HA_UNIT_TEMPERATURE_CELSIUS),
//!         ..Default::default()
//!     };
//!     let mut sensor = embassy_ha::create_sensor(&device, "temp-sensor", sensor_config);
//!
//!     // Spawn the Home Assistant communication task
//!     spawner.spawn(ha_task(stack, device)).unwrap();
//!
//!     // Main loop - read and publish temperature
//!     loop {
//! #       let temperature = 0.0;
//!         // let temperature = read_temperature().await;
//!         sensor.publish(temperature);
//!         Timer::after_secs(60).await;
//!     }
//! }
//!
//! #[embassy_executor::task]
//! async fn ha_task(stack: embassy_net::Stack<'static>, device: embassy_ha::Device<'static>) {
//!     embassy_ha::connect_and_run(stack, device, "mqtt-broker-address").await;
//! }
//! ```
//!
//! # Examples
//!
//! The repository includes several examples demonstrating different entity types. To run an example:
//!
//! ```bash
//! export MQTT_ADDRESS="mqtt://your-mqtt-broker:1883"
//! cargo run --example sensor
//! ```
//!
//! Available examples:
//! - `sensor` - Temperature and humidity sensors
//! - `button` - Triggerable button entity
//! - `switch` - On/off switch control
//! - `binary_sensor` - Binary state sensor
//! - `number` - Numeric input entity
//! - `device_tracker` - Location tracking entity

#![no_std]

use core::{
    cell::RefCell,
    net::{Ipv4Addr, SocketAddrV4},
    task::Waker,
};

use embassy_net::tcp::TcpSocket;
use embassy_sync::waitqueue::AtomicWaker;
use embassy_time::{Duration, Timer};
use heapless::{
    Vec, VecView,
    string::{String, StringView},
};
use serde::Serialize;

mod mqtt;

mod log;
#[allow(unused)]
use log::Format;

pub mod constants;

mod binary_state;
pub use binary_state::*;

mod command_policy;
pub use command_policy::*;

mod entity;
pub use entity::*;

mod entity_binary_sensor;
pub use entity_binary_sensor::*;

mod entity_button;
pub use entity_button::*;

mod entity_category;
pub use entity_category::*;

mod entity_device_tracker;
pub use entity_device_tracker::*;

mod entity_number;
pub use entity_number::*;

mod entity_sensor;
pub use entity_sensor::*;

mod entity_switch;
pub use entity_switch::*;

mod transport;
pub use transport::Transport;

mod unit;
pub use unit::*;

const AVAILABLE_PAYLOAD: &str = "online";
const NOT_AVAILABLE_PAYLOAD: &str = "offline";
const DEFAULT_KEEPALIVE_TIME: u16 = 30;
const MQTT_TIMEOUT: Duration = Duration::from_secs(30);

#[derive(Debug)]
pub struct Error(&'static str);

impl core::fmt::Display for Error {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str(self.0)
    }
}

impl core::error::Error for Error {}

impl Error {
    pub(crate) fn new(message: &'static str) -> Self {
        Self(message)
    }
}

#[derive(Debug, Clone, Copy, Serialize)]
#[cfg_attr(feature = "defmt", derive(Format))]
struct DeviceDiscovery<'a> {
    identifiers: &'a [&'a str],
    name: &'a str,
    manufacturer: &'a str,
    model: &'a str,
}

#[derive(Debug, Serialize)]
#[cfg_attr(feature = "defmt", derive(Format))]
struct EntityDiscovery<'a> {
    #[serde(rename = "unique_id")]
    id: &'a str,

    #[serde(skip_serializing_if = "Option::is_none")]
    name: Option<&'a str>,

    #[serde(skip_serializing_if = "Option::is_none")]
    device_class: Option<&'a str>,

    #[serde(skip_serializing_if = "Option::is_none")]
    state_topic: Option<&'a str>,

    #[serde(skip_serializing_if = "Option::is_none")]
    command_topic: Option<&'a str>,

    #[serde(skip_serializing_if = "Option::is_none")]
    json_attributes_topic: Option<&'a str>,

    #[serde(skip_serializing_if = "Option::is_none")]
    unit_of_measurement: Option<&'a str>,

    #[serde(skip_serializing_if = "Option::is_none")]
    schema: Option<&'a str>,

    #[serde(skip_serializing_if = "Option::is_none")]
    platform: Option<&'a str>,

    #[serde(skip_serializing_if = "Option::is_none")]
    state_class: Option<&'a str>,

    #[serde(skip_serializing_if = "Option::is_none")]
    icon: Option<&'a str>,

    #[serde(skip_serializing_if = "Option::is_none")]
    entity_category: Option<&'a str>,

    #[serde(skip_serializing_if = "Option::is_none")]
    entity_picture: Option<&'a str>,

    #[serde(skip_serializing_if = "Option::is_none")]
    min: Option<f32>,

    #[serde(skip_serializing_if = "Option::is_none")]
    max: Option<f32>,

    #[serde(skip_serializing_if = "Option::is_none")]
    step: Option<f32>,

    #[serde(skip_serializing_if = "Option::is_none")]
    mode: Option<&'a str>,

    #[serde(skip_serializing_if = "Option::is_none")]
    suggested_display_precision: Option<u8>,

    #[serde(skip_serializing_if = "Option::is_none")]
    availability_topic: Option<&'a str>,

    #[serde(skip_serializing_if = "Option::is_none")]
    payload_available: Option<&'a str>,

    #[serde(skip_serializing_if = "Option::is_none")]
    payload_not_available: Option<&'a str>,

    device: &'a DeviceDiscovery<'a>,
}

struct DiscoveryTopicDisplay<'a> {
    domain: &'a str,
    device_id: &'a str,
    entity_id: &'a str,
}

impl<'a> core::fmt::Display for DiscoveryTopicDisplay<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(
            f,
            "homeassistant/{}/{}_{}/config",
            self.domain, self.device_id, self.entity_id
        )
    }
}

struct StateTopicDisplay<'a> {
    device_id: &'a str,
    entity_id: &'a str,
}

impl<'a> core::fmt::Display for StateTopicDisplay<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "embassy-ha/{}/{}/state", self.device_id, self.entity_id)
    }
}

struct CommandTopicDisplay<'a> {
    device_id: &'a str,
    entity_id: &'a str,
}

impl<'a> core::fmt::Display for CommandTopicDisplay<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(
            f,
            "embassy-ha/{}/{}/command",
            self.device_id, self.entity_id
        )
    }
}

struct AttributesTopicDisplay<'a> {
    device_id: &'a str,
    entity_id: &'a str,
}

impl<'a> core::fmt::Display for AttributesTopicDisplay<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(
            f,
            "embassy-ha/{}/{}/attributes",
            self.device_id, self.entity_id
        )
    }
}

struct DeviceAvailabilityTopic<'a> {
    device_id: &'a str,
}

impl<'a> core::fmt::Display for DeviceAvailabilityTopic<'a> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "embassy-ha/{}/availability", self.device_id)
    }
}

pub struct DeviceConfig {
    pub device_id: &'static str,
    pub device_name: &'static str,
    pub manufacturer: &'static str,
    pub model: &'static str,
}

pub struct DeviceBuffersOwned {
    pub publish: Vec<u8, 2048>,
    pub subscribe: Vec<u8, 128>,
    pub discovery: Vec<u8, 2048>,
    pub availability_topic: String<128>,
    pub discovery_topic: String<128>,
    pub state_topic: String<128>,
    pub command_topic: String<128>,
    pub attributes_topic: String<128>,
}

impl Default for DeviceBuffersOwned {
    fn default() -> Self {
        Self {
            publish: Default::default(),
            subscribe: Default::default(),
            discovery: Default::default(),
            availability_topic: Default::default(),
            discovery_topic: Default::default(),
            state_topic: Default::default(),
            command_topic: Default::default(),
            attributes_topic: Default::default(),
        }
    }
}

impl DeviceBuffersOwned {
    pub fn as_buffers_mut(&mut self) -> DeviceBuffers<'_> {
        DeviceBuffers {
            publish: &mut self.publish,
            subscribe: &mut self.subscribe,
            discovery: &mut self.discovery,
            availability_topic: &mut self.availability_topic,
            discovery_topic: &mut self.discovery_topic,
            state_topic: &mut self.state_topic,
            command_topic: &mut self.command_topic,
            attributes_topic: &mut self.attributes_topic,
        }
    }
}

pub struct DeviceBuffers<'a> {
    pub publish: &'a mut VecView<u8>,
    pub subscribe: &'a mut VecView<u8>,
    pub discovery: &'a mut VecView<u8>,
    pub availability_topic: &'a mut StringView,
    pub discovery_topic: &'a mut StringView,
    pub state_topic: &'a mut StringView,
    pub command_topic: &'a mut StringView,
    pub attributes_topic: &'a mut StringView,
}

impl<'a> DeviceBuffers<'a> {
    pub fn clear(&mut self) {
        self.publish.clear();
        self.subscribe.clear();
        self.discovery.clear();
        self.availability_topic.clear();
        self.discovery_topic.clear();
        self.state_topic.clear();
        self.command_topic.clear();
        self.attributes_topic.clear();
    }
}

pub struct DeviceResources {
    waker: AtomicWaker,
    entities: [RefCell<Option<EntityData>>; Self::ENTITY_LIMIT],

    mqtt_resources: mqtt::ClientResources,
    buffers: DeviceBuffersOwned,
}

impl DeviceResources {
    const ENTITY_LIMIT: usize = 16;
}

impl Default for DeviceResources {
    fn default() -> Self {
        Self {
            waker: AtomicWaker::new(),
            entities: [const { RefCell::new(None) }; Self::ENTITY_LIMIT],

            mqtt_resources: Default::default(),
            buffers: Default::default(),
        }
    }
}

#[derive(Debug, Default)]
pub(crate) struct ButtonStorage {
    pub timestamp: Option<embassy_time::Instant>,
    pub consumed: bool,
}

#[derive(Debug)]
pub(crate) struct SwitchCommand {
    pub value: BinaryState,
    #[allow(unused)]
    pub timestamp: embassy_time::Instant,
}

#[derive(Debug)]
pub(crate) struct SwitchState {
    pub value: BinaryState,
    #[allow(unused)]
    pub timestamp: embassy_time::Instant,
}

#[derive(Debug, Default)]
pub(crate) struct SwitchStorage {
    pub state: Option<SwitchState>,
    pub command: Option<SwitchCommand>,
    pub command_policy: CommandPolicy,
}

#[derive(Debug)]
pub(crate) struct BinarySensorState {
    pub value: BinaryState,
    #[allow(unused)]
    pub timestamp: embassy_time::Instant,
}

#[derive(Debug, Default)]
pub(crate) struct BinarySensorStorage {
    pub state: Option<BinarySensorState>,
}

#[derive(Debug)]
pub(crate) struct NumericSensorState {
    pub value: f32,
    #[allow(unused)]
    pub timestamp: embassy_time::Instant,
}

#[derive(Debug, Default)]
pub(crate) struct NumericSensorStorage {
    pub state: Option<NumericSensorState>,
}

#[derive(Debug)]
pub(crate) struct NumberState {
    pub value: f32,
    #[allow(unused)]
    pub timestamp: embassy_time::Instant,
}

#[derive(Debug)]
pub(crate) struct NumberCommand {
    pub value: f32,
    #[allow(unused)]
    pub timestamp: embassy_time::Instant,
}

#[derive(Debug, Default)]
pub(crate) struct NumberStorage {
    pub state: Option<NumberState>,
    pub command: Option<NumberCommand>,
    pub command_policy: CommandPolicy,
}

#[derive(Debug, Serialize)]
pub(crate) struct DeviceTrackerState {
    pub latitude: f32,
    pub longitude: f32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gps_accuracy: Option<f32>,
}

#[derive(Debug, Default)]
pub(crate) struct DeviceTrackerStorage {
    pub state: Option<DeviceTrackerState>,
}

#[derive(Debug)]
pub(crate) enum EntityStorage {
    Button(ButtonStorage),
    Switch(SwitchStorage),
    BinarySensor(BinarySensorStorage),
    NumericSensor(NumericSensorStorage),
    Number(NumberStorage),
    DeviceTracker(DeviceTrackerStorage),
}

impl EntityStorage {
    pub fn as_button_mut(&mut self) -> &mut ButtonStorage {
        match self {
            EntityStorage::Button(storage) => storage,
            _ => panic!("expected storage type to be button"),
        }
    }

    pub fn as_switch_mut(&mut self) -> &mut SwitchStorage {
        match self {
            EntityStorage::Switch(storage) => storage,
            _ => panic!("expected storage type to be switch"),
        }
    }

    pub fn as_binary_sensor_mut(&mut self) -> &mut BinarySensorStorage {
        match self {
            EntityStorage::BinarySensor(storage) => storage,
            _ => panic!("expected storage type to be binary_sensor"),
        }
    }

    pub fn as_numeric_sensor_mut(&mut self) -> &mut NumericSensorStorage {
        match self {
            EntityStorage::NumericSensor(storage) => storage,
            _ => panic!("expected storage type to be numeric_sensor"),
        }
    }

    pub fn as_number_mut(&mut self) -> &mut NumberStorage {
        match self {
            EntityStorage::Number(storage) => storage,
            _ => panic!("expected storage type to be number"),
        }
    }

    pub fn as_device_tracker_mut(&mut self) -> &mut DeviceTrackerStorage {
        match self {
            EntityStorage::DeviceTracker(storage) => storage,
            _ => panic!("expected storage type to be device tracker"),
        }
    }
}

struct EntityData {
    config: EntityConfig,
    storage: EntityStorage,
    publish: bool,
    command: bool,
    command_waker: Option<Waker>,
}

pub(crate) struct Entity<'a> {
    pub(crate) data: &'a RefCell<Option<EntityData>>,
    pub(crate) waker: &'a AtomicWaker,
}

impl<'a> Entity<'a> {
    pub fn queue_publish(&mut self) {
        self.with_data(|data| data.publish = true);
        self.waker.wake();
    }

    pub async fn wait_command(&mut self) {
        struct Fut<'a, 'b>(&'a mut Entity<'b>);

        impl<'a, 'b> core::future::Future for Fut<'a, 'b> {
            type Output = ();

            fn poll(
                mut self: core::pin::Pin<&mut Self>,
                cx: &mut core::task::Context<'_>,
            ) -> core::task::Poll<Self::Output> {
                let this = &mut self.as_mut().0;
                this.with_data(|data| {
                    let dirty = data.command;
                    if dirty {
                        data.command = false;
                        data.command_waker = None;
                        core::task::Poll::Ready(())
                    } else {
                        // TODO: avoid clone if waker would wake
                        data.command_waker = Some(cx.waker().clone());
                        core::task::Poll::Pending
                    }
                })
            }
        }

        Fut(self).await
    }

    fn with_data<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&mut EntityData) -> R,
    {
        f(self.data.borrow_mut().as_mut().unwrap())
    }
}

pub struct Device<'a> {
    config: DeviceConfig,

    // resources
    waker: &'a AtomicWaker,
    entities: &'a [RefCell<Option<EntityData>>],

    mqtt_resources: &'a mut mqtt::ClientResources,
    buffers: DeviceBuffers<'a>,
}

pub fn new<'a>(resources: &'a mut DeviceResources, config: DeviceConfig) -> Device<'a> {
    Device {
        config,
        waker: &resources.waker,
        entities: &resources.entities,

        mqtt_resources: &mut resources.mqtt_resources,
        buffers: resources.buffers.as_buffers_mut(),
    }
}

fn create_entity<'a>(
    device: &Device<'a>,
    config: EntityConfig,
    storage: EntityStorage,
) -> Entity<'a> {
    let index = 'outer: {
        for idx in 0..device.entities.len() {
            if device.entities[idx].borrow().is_none() {
                break 'outer idx;
            }
        }
        panic!("device entity limit reached");
    };

    let data = EntityData {
        config,
        storage,
        publish: false,
        command: false,
        command_waker: None,
    };
    device.entities[index].replace(Some(data));

    Entity {
        data: &device.entities[index],
        waker: device.waker,
    }
}

pub fn create_sensor<'a>(
    device: &Device<'a>,
    id: &'static str,
    config: SensorConfig,
) -> Sensor<'a> {
    let mut entity_config = EntityConfig {
        id,
        ..Default::default()
    };
    config.populate(&mut entity_config);

    let entity = create_entity(
        device,
        entity_config,
        EntityStorage::NumericSensor(Default::default()),
    );
    Sensor::new(entity)
}

pub fn create_button<'a>(
    device: &Device<'a>,
    id: &'static str,
    config: ButtonConfig,
) -> Button<'a> {
    let mut entity_config = EntityConfig {
        id,
        ..Default::default()
    };
    config.populate(&mut entity_config);

    let entity = create_entity(
        device,
        entity_config,
        EntityStorage::Button(Default::default()),
    );
    Button::new(entity)
}

pub fn create_number<'a>(
    device: &Device<'a>,
    id: &'static str,
    config: NumberConfig,
) -> Number<'a> {
    let mut entity_config = EntityConfig {
        id,
        ..Default::default()
    };
    config.populate(&mut entity_config);

    let entity = create_entity(
        device,
        entity_config,
        EntityStorage::Number(NumberStorage {
            command_policy: config.command_policy,
            ..Default::default()
        }),
    );
    Number::new(entity)
}

pub fn create_switch<'a>(
    device: &Device<'a>,
    id: &'static str,
    config: SwitchConfig,
) -> Switch<'a> {
    let mut entity_config = EntityConfig {
        id,
        ..Default::default()
    };
    config.populate(&mut entity_config);

    let entity = create_entity(
        device,
        entity_config,
        EntityStorage::Switch(SwitchStorage {
            command_policy: config.command_policy,
            ..Default::default()
        }),
    );
    Switch::new(entity)
}

pub fn create_binary_sensor<'a>(
    device: &Device<'a>,
    id: &'static str,
    config: BinarySensorConfig,
) -> BinarySensor<'a> {
    let mut entity_config = EntityConfig {
        id,
        ..Default::default()
    };
    config.populate(&mut entity_config);

    let entity = create_entity(
        device,
        entity_config,
        EntityStorage::BinarySensor(Default::default()),
    );
    BinarySensor::new(entity)
}

pub fn create_device_tracker<'a>(
    device: &Device<'a>,
    id: &'static str,
    config: DeviceTrackerConfig,
) -> DeviceTracker<'a> {
    let mut entity_config = EntityConfig {
        id,
        ..Default::default()
    };
    config.populate(&mut entity_config);

    let entity = create_entity(
        device,
        entity_config,
        EntityStorage::DeviceTracker(Default::default()),
    );
    DeviceTracker::new(entity)
}

async fn device_mqtt_subscribe<T: Transport>(
    client: &mut mqtt::Client<'_, T>,
    topic: impl core::fmt::Display,
) -> Result<(), Error> {
    use core::fmt::Write;

    // Format topic to string for both subscribe call and logging
    let mut topic_buffer = heapless::String::<128>::new();
    write!(&mut topic_buffer, "{}", topic).expect("topic buffer too small");
    let topic_str = topic_buffer.as_str();

    match embassy_time::with_timeout(MQTT_TIMEOUT, client.subscribe(topic_str)).await {
        Ok(Ok(_)) => Ok(()),
        Ok(Err(err)) => {
            crate::log::error!(
                "mqtt subscribe to '{}' failed with: {:?}",
                topic_str,
                crate::log::Debug2Format(&err)
            );
            Err(Error::new("mqtt subscribe failed"))
        }
        Err(_) => {
            crate::log::error!("mqtt subscribe to '{}' timed out", topic_str);
            Err(Error::new("mqtt subscribe timed out"))
        }
    }
}

async fn device_mqtt_publish<T: Transport>(
    client: &mut mqtt::Client<'_, T>,
    topic: impl core::fmt::Display,
    data: &[u8],
    retain: bool,
) -> Result<(), Error> {
    use core::fmt::Write;

    // Format topic to string for both publish call and logging
    let mut topic_buffer = heapless::String::<128>::new();
    write!(&mut topic_buffer, "{}", topic).expect("topic buffer too small");
    let topic_str = topic_buffer.as_str();

    let result = if retain {
        embassy_time::with_timeout(
            MQTT_TIMEOUT,
            client.publish_with(
                topic_str,
                data,
                mqtt::PublishParams {
                    retain: true,
                    ..Default::default()
                },
            ),
        )
        .await
    } else {
        embassy_time::with_timeout(MQTT_TIMEOUT, client.publish(topic_str, data)).await
    };

    match result {
        Ok(Ok(_)) => Ok(()),
        Ok(Err(err)) => {
            crate::log::error!(
                "mqtt publish to '{}' failed with: {:?}",
                topic_str,
                crate::log::Debug2Format(&err)
            );
            Err(Error::new("mqtt publish failed"))
        }
        Err(_) => {
            crate::log::error!("mqtt publish to '{}' timed out", topic_str);
            Err(Error::new("mqtt publish timed out"))
        }
    }
}

/// Receives MQTT publish data with timeout and proper error handling.
///
/// This helper function handles the common pattern of receiving MQTT data with:
/// - Automatic timeout handling using MQTT_TIMEOUT
/// - Consistent error logging
/// - Size validation against buffer capacity
///
/// # Arguments
/// * `client` - MQTT client for receiving data
/// * `data_len` - Expected length of data to receive
/// * `buffer` - Buffer to receive the data into
///
/// # Returns
/// * `Ok(&[u8])` - Slice of the buffer containing the received data
/// * `Err(Error)` - If operation fails, times out, or data exceeds buffer size
///
/// # Errors
/// Returns error if:
/// - `data_len` is greater than `buffer.len()` (buffer too small)
/// - The receive operation times out after MQTT_TIMEOUT seconds
/// - The underlying MQTT receive operation fails
async fn mqtt_receive_data<'a, T: Transport>(
    client: &mut mqtt::Client<'_, T>,
    data_len: usize,
    buffer: &'a mut [u8],
) -> Result<&'a [u8], Error> {
    // Validate buffer size - reject if too small (per user requirement)
    if data_len > buffer.len() {
        crate::log::warn!(
            "mqtt publish payload is too large ({} bytes, buffer size {} bytes), rejecting",
            data_len,
            buffer.len()
        );
        return Err(Error::new("mqtt payload too large for buffer"));
    }

    crate::log::debug!("mqtt receiving {} bytes of data", data_len);

    match embassy_time::with_timeout(MQTT_TIMEOUT, client.receive_data(&mut buffer[..data_len]))
        .await
    {
        Ok(Ok(())) => Ok(&buffer[..data_len]),
        Ok(Err(err)) => {
            crate::log::error!(
                "mqtt receive data failed with: {:?}",
                crate::log::Debug2Format(&err)
            );
            Err(Error::new("mqtt receive data failed"))
        }
        Err(_) => {
            crate::log::error!("mqtt receive data timed out");
            Err(Error::new("mqtt receive data timed out"))
        }
    }
}

/// Publishes discovery messages for all entities in the device.
///
/// This function iterates over all entities, generates their discovery messages,
/// publishes them to MQTT, and optionally subscribes to their command topics.
///
/// # Arguments
/// * `client` - MQTT client for publishing and subscribing
/// * `entities` - Slice of entities to publish discoveries for
/// * `buffers` - Device buffers for generating discovery messages
/// * `device_config` - Device configuration
/// * `availability_topic` - The device availability topic string
/// * `subscribe_to_commands` - Whether to subscribe to command topics (true for initial discovery, false for rediscovery)
///
/// # Returns
/// `Ok(())` if all discoveries were published successfully, or an error if any operation fails
async fn publish_entity_discoveries<T: Transport>(
    client: &mut mqtt::Client<'_, T>,
    entities: &[RefCell<Option<EntityData>>],
    buffers: &mut DeviceBuffers<'_>,
    device_config: &DeviceConfig,
    availability_topic: &str,
    subscribe_to_commands: bool,
) -> Result<(), Error> {
    crate::log::debug!("publishing entity discovery messages");

    for entity in entities {
        buffers.clear();

        // Borrow the entity and fill out the buffers to be sent
        // This should be done inside a block so that we do not hold the RefMut across an await
        {
            let mut entity = entity.borrow_mut();
            let entity = match entity.as_mut() {
                Some(entity) => entity,
                None => break,
            };

            generate_entity_discovery(buffers, device_config, &entity.config, availability_topic);
        }

        let discovery_topic = buffers.discovery_topic.as_str();
        crate::log::debug!("sending discovery to topic '{}'", discovery_topic);
        device_mqtt_publish(client, discovery_topic, buffers.discovery, false).await?;

        if subscribe_to_commands {
            let command_topic = buffers.command_topic.as_str();
            crate::log::debug!("subscribing to command topic '{}'", command_topic);
            device_mqtt_subscribe(client, command_topic).await?;
        }
    }

    Ok(())
}

fn generate_entity_discovery(
    buffers: &mut DeviceBuffers<'_>,
    device_config: &DeviceConfig,
    entity_config: &EntityConfig,
    availability_topic: &str,
) {
    use core::fmt::Write;

    let discovery_topic_display = DiscoveryTopicDisplay {
        domain: entity_config.domain,
        device_id: device_config.device_id,
        entity_id: entity_config.id,
    };
    let state_topic_display = StateTopicDisplay {
        device_id: device_config.device_id,
        entity_id: entity_config.id,
    };
    let command_topic_display = CommandTopicDisplay {
        device_id: device_config.device_id,
        entity_id: entity_config.id,
    };
    let attributes_topic_display = AttributesTopicDisplay {
        device_id: device_config.device_id,
        entity_id: entity_config.id,
    };

    write!(buffers.discovery_topic, "{discovery_topic_display}")
        .expect("discovery topic buffer too small");
    write!(buffers.state_topic, "{state_topic_display}").expect("state topic buffer too small");
    write!(buffers.command_topic, "{command_topic_display}")
        .expect("command topic buffer too small");
    write!(buffers.attributes_topic, "{attributes_topic_display}")
        .expect("attributes topic buffer too small");

    let device_discovery = DeviceDiscovery {
        identifiers: &[device_config.device_id],
        name: device_config.device_name,
        manufacturer: device_config.manufacturer,
        model: device_config.model,
    };

    let discovery = EntityDiscovery {
        id: entity_config.id,
        name: entity_config.name,
        device_class: entity_config.device_class,
        state_topic: Some(buffers.state_topic.as_str()),
        command_topic: Some(buffers.command_topic.as_str()),
        json_attributes_topic: Some(buffers.attributes_topic.as_str()),
        unit_of_measurement: entity_config.measurement_unit,
        schema: entity_config.schema,
        platform: entity_config.platform,
        state_class: entity_config.state_class,
        icon: entity_config.icon,
        entity_category: entity_config.category,
        entity_picture: entity_config.picture,
        min: entity_config.min,
        max: entity_config.max,
        step: entity_config.step,
        mode: entity_config.mode,
        suggested_display_precision: entity_config.suggested_display_precision,
        availability_topic: Some(availability_topic),
        payload_available: Some(AVAILABLE_PAYLOAD),
        payload_not_available: Some(NOT_AVAILABLE_PAYLOAD),
        device: &device_discovery,
    };

    crate::log::debug!(
        "discovery for entity '{}': {:?}",
        entity_config.id,
        discovery
    );

    buffers
        .discovery
        .resize(buffers.discovery.capacity(), 0)
        .unwrap();
    let n = serde_json_core::to_slice(&discovery, buffers.discovery)
        .expect("discovery buffer too small");
    buffers.discovery.truncate(n);
}

/// Runs the main Home Assistant device event loop.
///
/// This function handles MQTT communication, entity discovery, and state updates. It will run
/// until the first error is encountered, at which point it returns immediately.
///
/// # Behavior
///
/// - Connects to the MQTT broker using the provided transport
/// - Publishes discovery messages for all entities
/// - Subscribes to command topics for controllable entities
/// - Enters the main event loop to handle state updates and commands
/// - Returns on the first error (connection loss, timeout, protocol error, etc.)
///
/// # Error Handling
///
/// This function should be called inside a retry loop, as any network error will cause this
/// function to fail. When an error occurs, the transport may be in an invalid state and should
/// be re-established before calling `run` again.
///
/// # Example
///
/// ```no_run
/// # use embassy_ha::{Device, Transport};
/// # async fn example(mut device: Device<'_>, create_transport: impl Fn() -> impl Transport) {
/// loop {
///     let mut transport = create_transport();
///
///     match embassy_ha::run(&mut device, &mut transport).await {
///         Ok(()) => {
///             // Normal exit (this shouldn't happen in practice)
///             break;
///         }
///         Err(err) => {
///             // Log error and retry after delay
///             // The transport connection should be re-established
///             embassy_time::Timer::after_secs(5).await;
///         }
///     }
/// }
/// # }
/// ```
///
/// For a higher-level alternative that handles retries automatically, see [`connect_and_run`].
pub async fn run<T: Transport>(device: &mut Device<'_>, transport: &mut T) -> Result<(), Error> {
    use core::fmt::Write;

    device.buffers.availability_topic.clear();
    write!(
        device.buffers.availability_topic,
        "{}",
        DeviceAvailabilityTopic {
            device_id: device.config.device_id
        }
    )
    .expect("device availability buffer too small");

    // Store availability_topic in a separate buffer to avoid borrow conflicts
    let mut availability_topic_copy = heapless::String::<128>::new();
    availability_topic_copy
        .push_str(device.buffers.availability_topic.as_str())
        .expect("availability topic too large");
    let availability_topic = availability_topic_copy.as_str();

    let mut ping_ticker =
        embassy_time::Ticker::every(Duration::from_secs(u64::from(DEFAULT_KEEPALIVE_TIME)));
    let mut client = mqtt::Client::new(device.mqtt_resources, transport);
    let connect_params = mqtt::ConnectParams {
        will_topic: Some(availability_topic),
        will_payload: Some(NOT_AVAILABLE_PAYLOAD.as_bytes()),
        will_retain: true,
        keepalive: Some(DEFAULT_KEEPALIVE_TIME),
        ..Default::default()
    };
    match embassy_time::with_timeout(
        MQTT_TIMEOUT,
        client.connect_with(device.config.device_id, connect_params),
    )
    .await
    {
        Ok(Ok(())) => {}
        Ok(Err(err)) => {
            crate::log::error!(
                "mqtt connect failed with: {:?}",
                crate::log::Debug2Format(&err)
            );
            return Err(Error::new("mqtt connection failed"));
        }
        Err(_) => {
            crate::log::error!("mqtt connect timed out");
            return Err(Error::new("mqtt connect timed out"));
        }
    }

    device_mqtt_subscribe(&mut client, constants::HA_STATUS_TOPIC).await?;

    publish_entity_discoveries(
        &mut client,
        device.entities,
        &mut device.buffers,
        &device.config,
        availability_topic,
        true,
    )
    .await?;

    device_mqtt_publish(
        &mut client,
        availability_topic,
        AVAILABLE_PAYLOAD.as_bytes(),
        true,
    )
    .await?;

    let mut first_iteration_push = true;
    'outer_loop: loop {
        use core::fmt::Write;

        for entity in device.entities {
            let publish_topic = {
                let mut entity = entity.borrow_mut();
                let entity = match entity.as_mut() {
                    Some(entity) => entity,
                    None => break,
                };

                if !entity.publish && !first_iteration_push {
                    continue;
                }

                entity.publish = false;
                device.buffers.publish.clear();

                let mut publish_to_attributes = false;
                match &entity.storage {
                    EntityStorage::Switch(SwitchStorage {
                        state: Some(SwitchState { value, .. }),
                        ..
                    }) => device
                        .buffers
                        .publish
                        .extend_from_slice(value.as_str().as_bytes())
                        .expect("publish buffer too small for switch state payload"),
                    EntityStorage::BinarySensor(BinarySensorStorage {
                        state: Some(BinarySensorState { value, .. }),
                    }) => device
                        .buffers
                        .publish
                        .extend_from_slice(value.as_str().as_bytes())
                        .expect("publish buffer too small for binary sensor state payload"),
                    EntityStorage::NumericSensor(NumericSensorStorage {
                        state: Some(NumericSensorState { value, .. }),
                        ..
                    }) => write!(device.buffers.publish, "{}", value)
                        .expect("publish buffer too small for numeric sensor payload"),
                    EntityStorage::Number(NumberStorage {
                        state: Some(NumberState { value, .. }),
                        ..
                    }) => write!(device.buffers.publish, "{}", value)
                        .expect("publish buffer too small for number state payload"),
                    EntityStorage::DeviceTracker(DeviceTrackerStorage {
                        state: Some(tracker_state),
                    }) => {
                        publish_to_attributes = true;
                        device
                            .buffers
                            .publish
                            .resize(device.buffers.publish.capacity(), 0)
                            .expect("resize to capacity should never fail");
                        let n = serde_json_core::to_slice(&tracker_state, device.buffers.publish)
                            .expect("publish buffer too small for tracker state payload");
                        device.buffers.publish.truncate(n);
                    }
                    _ => {
                        if !first_iteration_push {
                            crate::log::warn!(
                                "entity '{}' requested state publish but its storage does not support it",
                                entity.config.id
                            );
                        }
                        continue;
                    }
                }

                if publish_to_attributes {
                    let attributes_topic_display = AttributesTopicDisplay {
                        device_id: device.config.device_id,
                        entity_id: entity.config.id,
                    };
                    device.buffers.attributes_topic.clear();
                    write!(
                        device.buffers.attributes_topic,
                        "{attributes_topic_display}"
                    )
                    .expect("attributes topic buffer too small");
                    device.buffers.attributes_topic.as_str()
                } else {
                    let state_topic_display = StateTopicDisplay {
                        device_id: device.config.device_id,
                        entity_id: entity.config.id,
                    };
                    device.buffers.state_topic.clear();
                    write!(device.buffers.state_topic, "{state_topic_display}")
                        .expect("state topic buffer too small");
                    device.buffers.state_topic.as_str()
                }
            };

            device_mqtt_publish(&mut client, publish_topic, device.buffers.publish, false).await?;
        }
        first_iteration_push = false;

        let receive = client.receive();
        let waker = wait_on_atomic_waker(device.waker);
        let publish =
            match embassy_futures::select::select3(receive, waker, ping_ticker.next()).await {
                embassy_futures::select::Either3::First(packet) => match packet {
                    Ok(mqtt::Packet::Publish(publish)) => publish,
                    Err(err) => {
                        crate::log::error!(
                            "mqtt receive failed with: {:?}",
                            crate::log::Debug2Format(&err)
                        );
                        return Err(Error::new("mqtt receive failed"));
                    }
                    _ => continue,
                },
                embassy_futures::select::Either3::Second(_) => continue,
                embassy_futures::select::Either3::Third(_) => {
                    if let Err(err) = client.ping().await {
                        crate::log::error!(
                            "mqtt ping failed with: {:?}",
                            crate::log::Debug2Format(&err)
                        );
                        return Err(Error::new("mqtt ping failed"));
                    }
                    continue;
                }
            };

        if publish.topic == constants::HA_STATUS_TOPIC {
            let mut receive_buffer = [0u8; 64];
            let receive_data_len = publish.data_len;
            let receive_data =
                mqtt_receive_data(&mut client, receive_data_len, &mut receive_buffer).await?;

            if receive_data == constants::HA_STATUS_PAYLOAD_ONLINE.as_bytes() {
                first_iteration_push = true;

                crate::log::debug!("home assistant came online, republishing discoveries");
                publish_entity_discoveries(
                    &mut client,
                    device.entities,
                    &mut device.buffers,
                    &device.config,
                    availability_topic,
                    false,
                )
                .await?;
            }
            continue;
        }

        let entity = 'entity_search_block: {
            for entity in device.entities {
                let mut data = entity.borrow_mut();
                let data = match data.as_mut() {
                    Some(data) => data,
                    None => break,
                };

                let command_topic_display = CommandTopicDisplay {
                    device_id: device.config.device_id,
                    entity_id: data.config.id,
                };
                device.buffers.command_topic.clear();
                write!(device.buffers.command_topic, "{command_topic_display}")
                    .expect("command topic buffer too small");

                if device.buffers.command_topic.as_bytes() == publish.topic.as_bytes() {
                    break 'entity_search_block entity;
                }
            }
            continue 'outer_loop;
        };

        let mut read_buffer = [0u8; 128];
        let data_len = publish.data_len;
        let receive_data =
            match mqtt_receive_data(&mut client, data_len, &mut read_buffer).await {
                Ok(data) => data,
                Err(_) => continue 'outer_loop,
            };

        let command = match str::from_utf8(receive_data) {
            Ok(command) => command,
            Err(_) => {
                crate::log::warn!("mqtt message contained invalid utf-8, ignoring it");
                continue;
            }
        };

        let mut entity = entity.borrow_mut();
        let data = entity.as_mut().unwrap();

        match &mut data.storage {
            EntityStorage::Button(button_storage) => {
                if command != constants::HA_BUTTON_PAYLOAD_PRESS {
                    crate::log::warn!(
                        "button '{}' received unexpected command '{}', expected '{}', ignoring it",
                        data.config.id,
                        command,
                        constants::HA_BUTTON_PAYLOAD_PRESS
                    );
                    continue;
                }
                button_storage.consumed = false;
                button_storage.timestamp = Some(embassy_time::Instant::now());
            }
            EntityStorage::Switch(switch_storage) => {
                let command = match command.parse::<BinaryState>() {
                    Ok(command) => command,
                    Err(_) => {
                        crate::log::warn!(
                            "switch '{}' received invalid command '{}', expected 'ON' or 'OFF', ignoring it",
                            data.config.id,
                            command
                        );
                        continue;
                    }
                };
                let timestamp = embassy_time::Instant::now();
                if switch_storage.command_policy == CommandPolicy::PublishState {
                    data.publish = true;
                    switch_storage.state = Some(SwitchState {
                        value: command,
                        timestamp,
                    });
                }
                switch_storage.command = Some(SwitchCommand {
                    value: command,
                    timestamp,
                });
            }
            EntityStorage::Number(number_storage) => {
                let command = match command.parse::<f32>() {
                    Ok(command) => command,
                    Err(_) => {
                        crate::log::warn!(
                            "number '{}' received invalid command '{}', expected a valid number, ignoring it",
                            data.config.id,
                            command
                        );
                        continue;
                    }
                };
                let timestamp = embassy_time::Instant::now();
                if number_storage.command_policy == CommandPolicy::PublishState {
                    data.publish = true;
                    number_storage.state = Some(NumberState {
                        value: command,
                        timestamp,
                    });
                }
                number_storage.command = Some(NumberCommand {
                    value: command,
                    timestamp,
                });
            }
            _ => continue 'outer_loop,
        }

        data.command = true;
        if let Some(waker) = data.command_waker.take() {
            waker.wake();
        }
    }
}

/// High-level function that manages TCP connections and runs the device event loop with automatic retries.
///
/// This is a convenience wrapper around [`run`] that handles:
/// - DNS resolution (if hostname is provided)
/// - TCP connection establishment
/// - Automatic reconnection on failure with 5-second delay
/// - Infinite retry loop
///
/// # Arguments
///
/// * `stack` - The Embassy network stack for TCP connections
/// * `device` - The Home Assistant device to run
/// * `address` - MQTT broker address in one of these formats:
///   - `"192.168.1.100"` - IPv4 address (uses default port 1883)
///   - `"192.168.1.100:1883"` - IPv4 address with explicit port
///   - `"mqtt.example.com"` - Hostname (uses default port 1883)
///   - `"mqtt.example.com:1883"` - Hostname with explicit port
///
/// # Returns
///
/// This function never returns normally (returns `!`). It runs indefinitely, automatically
/// reconnecting on any error.
///
/// # Example
///
/// ```no_run
/// # use embassy_executor::Spawner;
/// # use embassy_ha::{Device, DeviceConfig};
/// # use static_cell::StaticCell;
/// # static HA_RESOURCES: StaticCell<embassy_ha::DeviceResources> = StaticCell::new();
/// #[embassy_executor::task]
/// async fn ha_task(stack: embassy_net::Stack<'static>) {
///     let device = embassy_ha::new(
///         HA_RESOURCES.init(Default::default()),
///         DeviceConfig {
///             device_id: "my-device",
///             device_name: "My Device",
///             manufacturer: "ACME",
///             model: "X",
///         },
///     );
///
///     // This function never returns
///     embassy_ha::connect_and_run(stack, device, "mqtt.example.com:1883").await;
/// }
/// ```
pub async fn connect_and_run(
    stack: embassy_net::Stack<'_>,
    mut device: Device<'_>,
    address: &str,
) -> ! {
    const DEFAULT_MQTT_PORT: u16 = 1883;

    let mut rx_buffer = [0u8; 1024];
    let mut tx_buffer = [0u8; 1024];
    let mut delay = false;

    loop {
        if !delay {
            delay = true;
        } else {
            crate::log::info!("Retrying connection in 5 seconds...");
            Timer::after_secs(5).await;
        }

        let addr = {
            // Try to parse as complete SocketAddrV4 first (e.g., "192.168.1.1:1883")
            if let Ok(sock_addr) = address.parse::<SocketAddrV4>() {
                sock_addr
            }
            // Try to parse as Ipv4Addr with default port (e.g., "192.168.1.1")
            else if let Ok(ip_addr) = address.parse::<Ipv4Addr>() {
                SocketAddrV4::new(ip_addr, DEFAULT_MQTT_PORT)
            }
            // Otherwise, parse as hostname:port or hostname
            else {
                let (addr_str, port) = match address.split_once(':') {
                    Some((addr_str, port_str)) => {
                        let port = port_str
                            .parse::<u16>()
                            .expect("Invalid port number in address");
                        (addr_str, port)
                    }
                    None => (address, DEFAULT_MQTT_PORT),
                };

                let addrs = match stack
                    .dns_query(addr_str, embassy_net::dns::DnsQueryType::A)
                    .await
                {
                    Ok(addrs) => addrs,
                    Err(err) => {
                        crate::log::error!(
                            "DNS query for '{}' failed with: {:?}",
                            addr_str,
                            crate::log::Debug2Format(&err)
                        );
                        continue;
                    }
                };

                #[allow(unreachable_patterns)]
                let ipv4_addr = match addrs
                    .iter()
                    .filter_map(|addr| match addr {
                        embassy_net::IpAddress::Ipv4(ipv4) => Some(*ipv4),
                        _ => None,
                    })
                    .next()
                {
                    Some(addr) => addr,
                    None => {
                        crate::log::error!(
                            "DNS query for '{}' returned no IPv4 addresses",
                            addr_str
                        );
                        continue;
                    }
                };

                SocketAddrV4::new(ipv4_addr, port)
            }
        };

        crate::log::info!("Connecting to MQTT broker at {}", addr);

        let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
        socket.set_timeout(Some(embassy_time::Duration::from_secs(10)));

        let connect_fut = embassy_time::with_timeout(Duration::from_secs(10), socket.connect(addr));
        match connect_fut.await {
            Ok(Err(err)) => {
                crate::log::error!(
                    "TCP connect to {} failed with: {:?}",
                    addr,
                    crate::log::Debug2Format(&err)
                );
                continue;
            }
            Err(_) => {
                crate::log::error!("TCP connect to {} timed out", addr);
                continue;
            }
            _ => {}
        }

        socket.set_timeout(None);

        if let Err(err) = run(&mut device, &mut socket).await {
            crate::log::error!(
                "Device run failed with: {:?}",
                crate::log::Debug2Format(&err)
            );
        }
    }
}

async fn wait_on_atomic_waker(waker: &AtomicWaker) {
    struct F<'a>(&'a AtomicWaker, bool);
    impl<'a> core::future::Future for F<'a> {
        type Output = ();

        fn poll(
            self: core::pin::Pin<&mut Self>,
            cx: &mut core::task::Context<'_>,
        ) -> core::task::Poll<Self::Output> {
            if !self.1 {
                self.0.register(cx.waker());
                self.get_mut().1 = true;
                core::task::Poll::Pending
            } else {
                core::task::Poll::Ready(())
            }
        }
    }
    F(waker, false).await
}