mirror of
				https://github.com/esphome/esphome.git
				synced 2025-11-03 16:41:50 +00:00 
			
		
		
		
	Compare commits
	
		
			422 Commits
		
	
	
		
			copilot/fi
			...
			2025.9.3
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					93e18e850e | ||
| 
						 | 
					59c0ffb98b | ||
| 
						 | 
					29658b79bc | ||
| 
						 | 
					158a59aa83 | ||
| 
						 | 
					c95180504a | ||
| 
						 | 
					a96c013eb1 | ||
| 
						 | 
					58166b3e71 | ||
| 
						 | 
					345fc0b6ca | ||
| 
						 | 
					127058e700 | ||
| 
						 | 
					57f7a709cf | ||
| 
						 | 
					f2a9e9265e | ||
| 
						 | 
					1ecd26adb5 | ||
| 
						 | 
					6d9fc672d5 | ||
| 
						 | 
					b9361b0868 | ||
| 
						 | 
					e47f4ef602 | ||
| 
						 | 
					961be7fd12 | ||
| 
						 | 
					a5a21f47d1 | ||
| 
						 | 
					a06cd84974 | ||
| 
						 | 
					e3703b43c1 | ||
| 
						 | 
					f6dc25c0ce | ||
| 
						 | 
					d2df232706 | ||
| 
						 | 
					404e679e66 | ||
| 
						 | 
					8d401ad05a | ||
| 
						 | 
					e542816f7d | ||
| 
						 | 
					12cadf0a04 | ||
| 
						 | 
					adc3d3127d | ||
| 
						 | 
					61ab682099 | ||
| 
						 | 
					c05b7cca5e | ||
| 
						 | 
					6ac395da6d | ||
| 
						 | 
					54616ae1b4 | ||
| 
						 | 
					e33dcda907 | ||
| 
						 | 
					04c1b90e57 | ||
| 
						 | 
					ddb8fedef7 | ||
| 
						 | 
					04f4f79cb4 | ||
| 
						 | 
					8890071360 | ||
| 
						 | 
					4b3a997a8e | ||
| 
						 | 
					2a4ab6a811 | ||
| 
						 | 
					971de64494 | ||
| 
						 | 
					926fdcbecd | ||
| 
						 | 
					6b147312cd | ||
| 
						 | 
					2d9152d9b9 | ||
| 
						 | 
					24f9550ce5 | ||
| 
						 | 
					3427aaab8c | ||
| 
						 | 
					4e17d14acc | ||
| 
						 | 
					1750f02ef3 | ||
| 
						 | 
					ae158179bd | ||
| 
						 | 
					c601494779 | ||
| 
						 | 
					646f4e66be | ||
| 
						 | 
					5b5e5c213c | ||
| 
						 | 
					46235684b1 | ||
| 
						 | 
					5b702a1efa | ||
| 
						 | 
					56e9fd2e38 | ||
| 
						 | 
					65f15a706f | ||
| 
						 | 
					eee64cc3a6 | ||
| 
						 | 
					f43fb3c3a3 | ||
| 
						 | 
					79b0025fe6 | ||
| 
						 | 
					c6a039a72f | ||
| 
						 | 
					6f1fa094c2 | ||
| 
						 | 
					7a869a33f0 | ||
| 
						 | 
					56e85b3ef9 | ||
| 
						 | 
					2401f81be3 | ||
| 
						 | 
					52a7e26c6d | ||
| 
						 | 
					d729dc20a8 | ||
| 
						 | 
					d3b7a9687b | ||
| 
						 | 
					9d7fc11108 | ||
| 
						 | 
					7969627d3e | ||
| 
						 | 
					82d2e367d4 | ||
| 
						 | 
					972aa691e4 | ||
| 
						 | 
					ac61b8f893 | ||
| 
						 | 
					d9f625e5c8 | ||
| 
						 | 
					e218f16f0f | ||
| 
						 | 
					422d209786 | ||
| 
						 | 
					e972e1f8c2 | ||
| 
						 | 
					cfb90b7b18 | ||
| 
						 | 
					8976ea2436 | ||
| 
						 | 
					01ff09064d | ||
| 
						 | 
					39212f0d7f | ||
| 
						 | 
					8993f4e6b4 | ||
| 
						 | 
					7adad0ee49 | ||
| 
						 | 
					dd8815ec9d | ||
| 
						 | 
					59e62a1f44 | ||
| 
						 | 
					f5f84fe825 | ||
| 
						 | 
					90c2fdd565 | ||
| 
						 | 
					f6d69231e8 | ||
| 
						 | 
					5cc0e21bc7 | ||
| 
						 | 
					703b592793 | ||
| 
						 | 
					75c9430d91 | ||
| 
						 | 
					e5bba00deb | ||
| 
						 | 
					8d90f13e97 | ||
| 
						 | 
					666e33e70b | ||
| 
						 | 
					7eaaa4e426 | ||
| 
						 | 
					166ad942ef | ||
| 
						 | 
					0ff08bbc09 | ||
| 
						 | 
					6e2bcabbc9 | ||
| 
						 | 
					afa191ae41 | ||
| 
						 | 
					93da52c4d2 | ||
| 
						 | 
					0cc0979674 | ||
| 
						 | 
					629f1e94f1 | ||
| 
						 | 
					8c28f346c7 | ||
| 
						 | 
					3cf36e2f94 | ||
| 
						 | 
					1ac07c96b1 | ||
| 
						 | 
					91228c82e6 | ||
| 
						 | 
					28d16728d3 | ||
| 
						 | 
					f24a182ba2 | ||
| 
						 | 
					0065fe1516 | ||
| 
						 | 
					148fa698cc | ||
| 
						 | 
					b25506b045 | ||
| 
						 | 
					0c737fc4df | ||
| 
						 | 
					a8b8507ffc | ||
| 
						 | 
					c33bb3a8a9 | ||
| 
						 | 
					4d09932320 | ||
| 
						 | 
					e018b15641 | ||
| 
						 | 
					3fd469cfe8 | ||
| 
						 | 
					1359142106 | ||
| 
						 | 
					487ba4dad0 | ||
| 
						 | 
					694c590eb6 | ||
| 
						 | 
					b74463c3e6 | ||
| 
						 | 
					98e8a0c201 | ||
| 
						 | 
					91b2f75d04 | ||
| 
						 | 
					f1806046a9 | ||
| 
						 | 
					5b283d6d38 | ||
| 
						 | 
					1340665ac7 | ||
| 
						 | 
					1510db277c | ||
| 
						 | 
					a49669ee58 | ||
| 
						 | 
					09b40b882e | ||
| 
						 | 
					0069163d31 | ||
| 
						 | 
					86c2af4882 | ||
| 
						 | 
					b4b795dcaf | ||
| 
						 | 
					b8ed7ec145 | ||
| 
						 | 
					365a427b57 | ||
| 
						 | 
					e327ae8c95 | ||
| 
						 | 
					4c2f356b35 | ||
| 
						 | 
					e55bce83e3 | ||
| 
						 | 
					ba2433197e | ||
| 
						 | 
					c471bdb446 | ||
| 
						 | 
					cbac9caa52 | ||
| 
						 | 
					edf7094662 | ||
| 
						 | 
					25489b6009 | ||
| 
						 | 
					dc45a613f3 | ||
| 
						 | 
					e0617e01e0 | ||
| 
						 | 
					c7ee727af4 | ||
| 
						 | 
					c5b2a9e24b | ||
| 
						 | 
					101d553df9 | ||
| 
						 | 
					8fb6420b1c | ||
| 
						 | 
					c03d978b46 | ||
| 
						 | 
					2d3cdf60ba | ||
| 
						 | 
					a29fef166b | ||
| 
						 | 
					9fe94f1201 | ||
| 
						 | 
					1b8978a89a | ||
| 
						 | 
					6f188d1284 | ||
| 
						 | 
					a1a336783e | ||
| 
						 | 
					c55bc93f70 | ||
| 
						 | 
					de998f2f39 | ||
| 
						 | 
					950299e52b | ||
| 
						 | 
					23c6650902 | ||
| 
						 | 
					5759692627 | ||
| 
						 | 
					0ab65c225e | ||
| 
						 | 
					8aeb6d3ba2 | ||
| 
						 | 
					c3359edb33 | ||
| 
						 | 
					4d681ffe3d | ||
| 
						 | 
					68628a85b1 | ||
| 
						 | 
					086f1982fa | ||
| 
						 | 
					5ba1c32242 | ||
| 
						 | 
					d2b23ba3a7 | ||
| 
						 | 
					83fbd77c4a | ||
| 
						 | 
					1a054299d4 | ||
| 
						 | 
					e3fb9c2a78 | ||
| 
						 | 
					d1276dc6df | ||
| 
						 | 
					f286bc57f3 | ||
| 
						 | 
					ed48282d09 | ||
| 
						 | 
					2ddd8c72d6 | ||
| 
						 | 
					d0b4bc48e4 | ||
| 
						 | 
					77dbe77117 | ||
| 
						 | 
					6daeffcefd | ||
| 
						 | 
					6d834c019d | ||
| 
						 | 
					905e2906fe | ||
| 
						 | 
					a25b544c3b | ||
| 
						 | 
					da21174c6d | ||
| 
						 | 
					e29f0ee7f8 | ||
| 
						 | 
					983b3cb879 | ||
| 
						 | 
					fd568d9af3 | ||
| 
						 | 
					ca72286386 | ||
| 
						 | 
					dea68bebd8 | ||
| 
						 | 
					ef98f67b41 | ||
| 
						 | 
					6a92b691a0 | ||
| 
						 | 
					bc960cf6d2 | ||
| 
						 | 
					461ce69296 | ||
| 
						 | 
					6a20e6f9ad | ||
| 
						 | 
					cde00a1f4c | ||
| 
						 | 
					5dc691874b | ||
| 
						 | 
					c526ab9a3f | ||
| 
						 | 
					07875a8b1e | ||
| 
						 | 
					ba4789970c | ||
| 
						 | 
					015977cfdf | ||
| 
						 | 
					e513c0f004 | ||
| 
						 | 
					a11970aee0 | ||
| 
						 | 
					4ab37b069b | ||
| 
						 | 
					b6bb6699d1 | ||
| 
						 | 
					078eaff9a8 | ||
| 
						 | 
					a7786b75a0 | ||
| 
						 | 
					d4c11dac8c | ||
| 
						 | 
					2f2f2f7d15 | ||
| 
						 | 
					a92a08c2de | ||
| 
						 | 
					75595b08be | ||
| 
						 | 
					3c7aba0681 | ||
| 
						 | 
					e5d1c30797 | ||
| 
						 | 
					c171d13c8c | ||
| 
						 | 
					65d63de9b6 | ||
| 
						 | 
					9e712e4127 | ||
| 
						 | 
					9007621fd7 | ||
| 
						 | 
					c01a26607e | ||
| 
						 | 
					f6ca70970f | ||
| 
						 | 
					4dc11f05a7 | ||
| 
						 | 
					5e508f7461 | ||
| 
						 | 
					2aceb56606 | ||
| 
						 | 
					d071a074ef | ||
| 
						 | 
					7a459c8c20 | ||
| 
						 | 
					aebd21958a | ||
| 
						 | 
					c542db8bfe | ||
| 
						 | 
					d9dcfe66ec | ||
| 
						 | 
					8517c2e903 | ||
| 
						 | 
					684384892a | ||
| 
						 | 
					d560831d79 | ||
| 
						 | 
					fcc3c8e1b6 | ||
| 
						 | 
					959ffde60e | ||
| 
						 | 
					07715dd50f | ||
| 
						 | 
					03836ee2d2 | ||
| 
						 | 
					50408d9abb | ||
| 
						 | 
					0de7259428 | ||
| 
						 | 
					d054709c2d | ||
| 
						 | 
					da16887915 | ||
| 
						 | 
					6da8ec8d55 | ||
| 
						 | 
					d2752b38c9 | ||
| 
						 | 
					6004367ee2 | ||
| 
						 | 
					ecfeb8e4d3 | ||
| 
						 | 
					456c31262d | ||
| 
						 | 
					9f02575287 | ||
| 
						 | 
					07bca6103f | ||
| 
						 | 
					a58c3950bc | ||
| 
						 | 
					8fe582309e | ||
| 
						 | 
					b41a61c76e | ||
| 
						 | 
					61a5023888 | ||
| 
						 | 
					4396bc0d1a | ||
| 
						 | 
					acfce581fa | ||
| 
						 | 
					88303f39fa | ||
| 
						 | 
					ca19959d7c | ||
| 
						 | 
					9737b35579 | ||
| 
						 | 
					be9c20c357 | ||
| 
						 | 
					12ba4b142e | ||
| 
						 | 
					c096c6934d | ||
| 
						 | 
					17f787fc36 | ||
| 
						 | 
					5cd9a86dcb | ||
| 
						 | 
					83fe4b4ff3 | ||
| 
						 | 
					94accd5abe | ||
| 
						 | 
					3ca0015284 | ||
| 
						 | 
					33eddb6035 | ||
| 
						 | 
					72c58ae36d | ||
| 
						 | 
					35411d199f | ||
| 
						 | 
					d45944a9e2 | ||
| 
						 | 
					86f306ba9e | ||
| 
						 | 
					1b3b2f6e6f | ||
| 
						 | 
					2adb993242 | ||
| 
						 | 
					3ff5b4773b | ||
| 
						 | 
					2cbf4f30f9 | ||
| 
						 | 
					56b6dd31f1 | ||
| 
						 | 
					fc1b49e87d | ||
| 
						 | 
					0089619518 | ||
| 
						 | 
					5a6db28f1d | ||
| 
						 | 
					6819bbd8f8 | ||
| 
						 | 
					634f687c3e | ||
| 
						 | 
					e2a9b85924 | ||
| 
						 | 
					4ccc6aee09 | ||
| 
						 | 
					0eab908b0e | ||
| 
						 | 
					3964f9794b | ||
| 
						 | 
					a45137434b | ||
| 
						 | 
					9b1ebdb6da | ||
| 
						 | 
					5a1533bea9 | ||
| 
						 | 
					0b50ef227b | ||
| 
						 | 
					0e31bc1a67 | ||
| 
						 | 
					8e67df8059 | ||
| 
						 | 
					e1a0949ddb | ||
| 
						 | 
					c5b2c8d971 | ||
| 
						 | 
					a8775ba60b | ||
| 
						 | 
					104906ca11 | ||
| 
						 | 
					ad5f6f0cfe | ||
| 
						 | 
					8356f7fcd3 | ||
| 
						 | 
					225de226b0 | ||
| 
						 | 
					2aaf951357 | ||
| 
						 | 
					82718e62e7 | ||
| 
						 | 
					fd07e1d979 | ||
| 
						 | 
					4dab9c4400 | ||
| 
						 | 
					7e23d865e6 | ||
| 
						 | 
					8f118232e4 | ||
| 
						 | 
					23554cda06 | ||
| 
						 | 
					064385eac6 | ||
| 
						 | 
					6502ed70de | ||
| 
						 | 
					bb894c3e32 | ||
| 
						 | 
					c5858b7032 | ||
| 
						 | 
					99f57ecb73 | ||
| 
						 | 
					cc6c892678 | ||
| 
						 | 
					07a98d2525 | ||
| 
						 | 
					e80f616366 | ||
| 
						 | 
					46be877594 | ||
| 
						 | 
					ac8b48a53c | ||
| 
						 | 
					7fdbd8528a | ||
| 
						 | 
					80970f972b | ||
| 
						 | 
					3c7865cd6f | ||
| 
						 | 
					3a6a66537c | ||
| 
						 | 
					7118bea031 | ||
| 
						 | 
					44bd8e5b54 | ||
| 
						 | 
					efaeb91803 | ||
| 
						 | 
					761c6c6685 | ||
| 
						 | 
					1f55486896 | ||
| 
						 | 
					6818439109 | ||
| 
						 | 
					0a77423073 | ||
| 
						 | 
					c29f8d0187 | ||
| 
						 | 
					2a3f80a82c | ||
| 
						 | 
					75f3adcd95 | ||
| 
						 | 
					daf8ec36ab | ||
| 
						 | 
					6c5632a0b3 | ||
| 
						 | 
					abecc0e8d8 | ||
| 
						 | 
					af9ecf3429 | ||
| 
						 | 
					5fa84439c2 | ||
| 
						 | 
					5d18afcd99 | ||
| 
						 | 
					117cffd2b0 | ||
| 
						 | 
					8ea1a3ed64 | ||
| 
						 | 
					4f29b3c7aa | ||
| 
						 | 
					3325592d67 | ||
| 
						 | 
					0a3ee7d84e | ||
| 
						 | 
					882237120e | ||
| 
						 | 
					71efaf097b | ||
| 
						 | 
					bd60dbb746 | ||
| 
						 | 
					6b5e43ca72 | ||
| 
						 | 
					8d61b1e8df | ||
| 
						 | 
					9c897993bb | ||
| 
						 | 
					93f9475105 | ||
| 
						 | 
					95cd224e3e | ||
| 
						 | 
					b7afeafda9 | ||
| 
						 | 
					7922462bcf | ||
| 
						 | 
					46d433775b | ||
| 
						 | 
					7c4a54de90 | ||
| 
						 | 
					c3f1596498 | ||
| 
						 | 
					0d1949a61b | ||
| 
						 | 
					6a8722f33e | ||
| 
						 | 
					fff66072d4 | ||
| 
						 | 
					1c2e1ab3e5 | ||
| 
						 | 
					68ddd98f5f | ||
| 
						 | 
					0dda3faed5 | ||
| 
						 | 
					40c0c36179 | ||
| 
						 | 
					6b7ced1970 | ||
| 
						 | 
					ed2b76050b | ||
| 
						 | 
					113813617d | ||
| 
						 | 
					c3a209d3f4 | ||
| 
						 | 
					7ffdaa1f06 | ||
| 
						 | 
					3a857950bf | ||
| 
						 | 
					0256e0005e | ||
| 
						 | 
					c65af68e63 | ||
| 
						 | 
					ef2121a215 | ||
| 
						 | 
					bb40b7702d | ||
| 
						 | 
					6c48f3d719 | ||
| 
						 | 
					ff52869b4c | ||
| 
						 | 
					82b7c1224c | ||
| 
						 | 
					c14c4fb658 | ||
| 
						 | 
					42aee53dde | ||
| 
						 | 
					9aa21956c8 | ||
| 
						 | 
					4c2874a32b | ||
| 
						 | 
					45b88f2da9 | ||
| 
						 | 
					8f53961496 | ||
| 
						 | 
					5cf0e4d9dd | ||
| 
						 | 
					b70983ed09 | ||
| 
						 | 
					ffa89eb2d3 | ||
| 
						 | 
					8b67d6dfec | ||
| 
						 | 
					581b4ef5a1 | ||
| 
						 | 
					da02f970d4 | ||
| 
						 | 
					2fc0a11596 | ||
| 
						 | 
					5a8f722316 | ||
| 
						 | 
					279f56141e | ||
| 
						 | 
					6bfe281d18 | ||
| 
						 | 
					a1371aea37 | ||
| 
						 | 
					d5c9c10b3b | ||
| 
						 | 
					cef39e7c59 | ||
| 
						 | 
					2b9e1ce315 | ||
| 
						 | 
					ff9ddb9d68 | ||
| 
						 | 
					676c51ffa0 | ||
| 
						 | 
					7e4d09dbd8 | ||
| 
						 | 
					58504662d8 | ||
| 
						 | 
					83b69519dd | ||
| 
						 | 
					d4d1a96f9b | ||
| 
						 | 
					76fd104fb6 | ||
| 
						 | 
					c4d1b1317a | ||
| 
						 | 
					14bc83342f | ||
| 
						 | 
					a1461c5293 | ||
| 
						 | 
					73b2db8af5 | ||
| 
						 | 
					a7a119f576 | ||
| 
						 | 
					1ba76f5f2e | ||
| 
						 | 
					37a9ad6a0d | ||
| 
						 | 
					c0a62c0be1 | ||
| 
						 | 
					bfb14e1cf9 | ||
| 
						 | 
					1415e02e40 | ||
| 
						 | 
					81f907e994 | ||
| 
						 | 
					61008bc8a9 | ||
| 
						 | 
					6d66ddd68d | ||
| 
						 | 
					fc180251be | ||
| 
						 | 
					ee1d4f27ef | ||
| 
						 | 
					325ec0a0ae | ||
| 
						 | 
					6071f4b02c | ||
| 
						 | 
					083ac8ce8e | ||
| 
						 | 
					4ceda31f32 | ||
| 
						 | 
					5021cc6d5f | ||
| 
						 | 
					2b3e546203 | ||
| 
						 | 
					1642d34d29 | ||
| 
						 | 
					8ceb1b9d60 | ||
| 
						 | 
					d872c8a999 | ||
| 
						 | 
					99125c045f | ||
| 
						 | 
					860a5ef5c0 | ||
| 
						 | 
					b01f03cc24 | ||
| 
						 | 
					cfb22e33c9 | ||
| 
						 | 
					96bbb58f34 | ||
| 
						 | 
					3edd746c6c | ||
| 
						 | 
					c308e03e92 | ||
| 
						 | 
					bd2b3b9da5 | ||
| 
						 | 
					d443a97dd8 | 
@@ -9,7 +9,7 @@ This document provides essential context for AI models interacting with this pro
 | 
			
		||||
 | 
			
		||||
## 2. Core Technologies & Stack
 | 
			
		||||
 | 
			
		||||
*   **Languages:** Python (>=3.10), C++ (gnu++20)
 | 
			
		||||
*   **Languages:** Python (>=3.11), C++ (gnu++20)
 | 
			
		||||
*   **Frameworks & Runtimes:** PlatformIO, Arduino, ESP-IDF.
 | 
			
		||||
*   **Build Systems:** PlatformIO is the primary build system. CMake is used as an alternative.
 | 
			
		||||
*   **Configuration:** YAML.
 | 
			
		||||
@@ -38,7 +38,7 @@ This document provides essential context for AI models interacting with this pro
 | 
			
		||||
    5.  **Dashboard** (`esphome/dashboard/`): A web-based interface for device configuration, management, and OTA updates.
 | 
			
		||||
 | 
			
		||||
*   **Platform Support:**
 | 
			
		||||
    1.  **ESP32** (`components/esp32/`): Espressif ESP32 family. Supports multiple variants (S2, S3, C3, etc.) and both IDF and Arduino frameworks.
 | 
			
		||||
    1.  **ESP32** (`components/esp32/`): Espressif ESP32 family. Supports multiple variants (Original, C2, C3, C5, C6, H2, P4, S2, S3) with ESP-IDF framework. Arduino framework supports only a subset of the variants (Original, C3, S2, S3).
 | 
			
		||||
    2.  **ESP8266** (`components/esp8266/`): Espressif ESP8266. Arduino framework only, with memory constraints.
 | 
			
		||||
    3.  **RP2040** (`components/rp2040/`): Raspberry Pi Pico/RP2040. Arduino framework with PIO (Programmable I/O) support.
 | 
			
		||||
    4.  **LibreTiny** (`components/libretiny/`): Realtek and Beken chips. Supports multiple chip families and auto-generated components.
 | 
			
		||||
@@ -60,7 +60,7 @@ This document provides essential context for AI models interacting with this pro
 | 
			
		||||
        ├── __init__.py          # Component configuration schema and code generation
 | 
			
		||||
        ├── [component].h        # C++ header file (if needed)
 | 
			
		||||
        ├── [component].cpp      # C++ implementation (if needed)
 | 
			
		||||
        └── [platform]/         # Platform-specific implementations
 | 
			
		||||
        └── [platform]/          # Platform-specific implementations
 | 
			
		||||
            ├── __init__.py      # Platform-specific configuration
 | 
			
		||||
            ├── [platform].h     # Platform C++ header
 | 
			
		||||
            └── [platform].cpp   # Platform C++ implementation
 | 
			
		||||
@@ -150,7 +150,8 @@ This document provides essential context for AI models interacting with this pro
 | 
			
		||||
*   **Configuration Validation:**
 | 
			
		||||
    *   **Common Validators:** `cv.int_`, `cv.float_`, `cv.string`, `cv.boolean`, `cv.int_range(min=0, max=100)`, `cv.positive_int`, `cv.percentage`.
 | 
			
		||||
    *   **Complex Validation:** `cv.All(cv.string, cv.Length(min=1, max=50))`, `cv.Any(cv.int_, cv.string)`.
 | 
			
		||||
    *   **Platform-Specific:** `cv.only_on(["esp32", "esp8266"])`, `cv.only_with_arduino`.
 | 
			
		||||
    *   **Platform-Specific:** `cv.only_on(["esp32", "esp8266"])`, `esp32.only_on_variant(...)`, `cv.only_on_esp32`, `cv.only_on_esp8266`, `cv.only_on_rp2040`.
 | 
			
		||||
    *   **Framework-Specific:** `cv.only_with_framework(...)`, `cv.only_with_arduino`, `cv.only_with_esp_idf`.
 | 
			
		||||
    *   **Schema Extensions:**
 | 
			
		||||
        ```python
 | 
			
		||||
        CONFIG_SCHEMA = cv.Schema({ ... })
 | 
			
		||||
 
 | 
			
		||||
@@ -1 +1 @@
 | 
			
		||||
6af8b429b94191fe8e239fcb3b73f7982d0266cb5b05ffbc81edaeac1bc8c273
 | 
			
		||||
4368db58e8f884aff245996b1e8b644cc0796c0bb2fa706d5740d40b823d3ac9
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										4
									
								
								.github/actions/restore-python/action.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.github/actions/restore-python/action.yml
									
									
									
									
										vendored
									
									
								
							@@ -17,12 +17,12 @@ runs:
 | 
			
		||||
  steps:
 | 
			
		||||
    - name: Set up Python ${{ inputs.python-version }}
 | 
			
		||||
      id: python
 | 
			
		||||
      uses: actions/setup-python@v5.6.0
 | 
			
		||||
      uses: actions/setup-python@v6.0.0
 | 
			
		||||
      with:
 | 
			
		||||
        python-version: ${{ inputs.python-version }}
 | 
			
		||||
    - name: Restore Python virtual environment
 | 
			
		||||
      id: cache-venv
 | 
			
		||||
      uses: actions/cache/restore@v4.2.3
 | 
			
		||||
      uses: actions/cache/restore@v4.2.4
 | 
			
		||||
      with:
 | 
			
		||||
        path: venv
 | 
			
		||||
        # yamllint disable-line rule:line-length
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										34
									
								
								.github/workflows/auto-label-pr.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										34
									
								
								.github/workflows/auto-label-pr.yml
									
									
									
									
										vendored
									
									
								
							@@ -22,7 +22,7 @@ jobs:
 | 
			
		||||
    if: github.event.action != 'labeled' || github.event.sender.type != 'Bot'
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Checkout
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
 | 
			
		||||
      - name: Generate a token
 | 
			
		||||
        id: generate-token
 | 
			
		||||
@@ -32,7 +32,7 @@ jobs:
 | 
			
		||||
          private-key: ${{ secrets.ESPHOME_GITHUB_APP_PRIVATE_KEY }}
 | 
			
		||||
 | 
			
		||||
      - name: Auto Label PR
 | 
			
		||||
        uses: actions/github-script@v7.0.1
 | 
			
		||||
        uses: actions/github-script@v8.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          github-token: ${{ steps.generate-token.outputs.token }}
 | 
			
		||||
          script: |
 | 
			
		||||
@@ -105,7 +105,9 @@ jobs:
 | 
			
		||||
 | 
			
		||||
            // Calculate data from PR files
 | 
			
		||||
            const changedFiles = prFiles.map(file => file.filename);
 | 
			
		||||
            const totalChanges = prFiles.reduce((sum, file) => sum + (file.additions || 0) + (file.deletions || 0), 0);
 | 
			
		||||
            const totalAdditions = prFiles.reduce((sum, file) => sum + (file.additions || 0), 0);
 | 
			
		||||
            const totalDeletions = prFiles.reduce((sum, file) => sum + (file.deletions || 0), 0);
 | 
			
		||||
            const totalChanges = totalAdditions + totalDeletions;
 | 
			
		||||
 | 
			
		||||
            console.log('Current labels:', currentLabels.join(', '));
 | 
			
		||||
            console.log('Changed files:', changedFiles.length);
 | 
			
		||||
@@ -231,16 +233,21 @@ jobs:
 | 
			
		||||
            // Strategy: PR size detection
 | 
			
		||||
            async function detectPRSize() {
 | 
			
		||||
              const labels = new Set();
 | 
			
		||||
              const testChanges = prFiles
 | 
			
		||||
                .filter(file => file.filename.startsWith('tests/'))
 | 
			
		||||
                .reduce((sum, file) => sum + (file.additions || 0) + (file.deletions || 0), 0);
 | 
			
		||||
 | 
			
		||||
              const nonTestChanges = totalChanges - testChanges;
 | 
			
		||||
 | 
			
		||||
              if (totalChanges <= SMALL_PR_THRESHOLD) {
 | 
			
		||||
                labels.add('small-pr');
 | 
			
		||||
                return labels;
 | 
			
		||||
              }
 | 
			
		||||
 | 
			
		||||
              const testAdditions = prFiles
 | 
			
		||||
                .filter(file => file.filename.startsWith('tests/'))
 | 
			
		||||
                .reduce((sum, file) => sum + (file.additions || 0), 0);
 | 
			
		||||
              const testDeletions = prFiles
 | 
			
		||||
                .filter(file => file.filename.startsWith('tests/'))
 | 
			
		||||
                .reduce((sum, file) => sum + (file.deletions || 0), 0);
 | 
			
		||||
 | 
			
		||||
              const nonTestChanges = (totalAdditions - testAdditions) - (totalDeletions - testDeletions);
 | 
			
		||||
 | 
			
		||||
              // Don't add too-big if mega-pr label is already present
 | 
			
		||||
              if (nonTestChanges > TOO_BIG_THRESHOLD && !isMegaPR) {
 | 
			
		||||
                labels.add('too-big');
 | 
			
		||||
@@ -375,7 +382,7 @@ jobs:
 | 
			
		||||
              const labels = new Set();
 | 
			
		||||
 | 
			
		||||
              // Check for missing tests
 | 
			
		||||
              if ((allLabels.has('new-component') || allLabels.has('new-platform')) && !allLabels.has('has-tests')) {
 | 
			
		||||
              if ((allLabels.has('new-component') || allLabels.has('new-platform') || allLabels.has('new-feature')) && !allLabels.has('has-tests')) {
 | 
			
		||||
                labels.add('needs-tests');
 | 
			
		||||
              }
 | 
			
		||||
 | 
			
		||||
@@ -412,10 +419,13 @@ jobs:
 | 
			
		||||
 | 
			
		||||
              // Too big message
 | 
			
		||||
              if (finalLabels.includes('too-big')) {
 | 
			
		||||
                const testChanges = prFiles
 | 
			
		||||
                const testAdditions = prFiles
 | 
			
		||||
                  .filter(file => file.filename.startsWith('tests/'))
 | 
			
		||||
                  .reduce((sum, file) => sum + (file.additions || 0) + (file.deletions || 0), 0);
 | 
			
		||||
                const nonTestChanges = totalChanges - testChanges;
 | 
			
		||||
                  .reduce((sum, file) => sum + (file.additions || 0), 0);
 | 
			
		||||
                const testDeletions = prFiles
 | 
			
		||||
                  .filter(file => file.filename.startsWith('tests/'))
 | 
			
		||||
                  .reduce((sum, file) => sum + (file.deletions || 0), 0);
 | 
			
		||||
                const nonTestChanges = (totalAdditions - testAdditions) - (totalDeletions - testDeletions);
 | 
			
		||||
 | 
			
		||||
                const tooManyLabels = finalLabels.length > MAX_LABELS;
 | 
			
		||||
                const tooManyChanges = nonTestChanges > TOO_BIG_THRESHOLD;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										8
									
								
								.github/workflows/ci-api-proto.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										8
									
								
								.github/workflows/ci-api-proto.yml
									
									
									
									
										vendored
									
									
								
							@@ -21,9 +21,9 @@ jobs:
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Checkout
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
      - name: Set up Python
 | 
			
		||||
        uses: actions/setup-python@v5.6.0
 | 
			
		||||
        uses: actions/setup-python@v6.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          python-version: "3.11"
 | 
			
		||||
 | 
			
		||||
@@ -47,7 +47,7 @@ jobs:
 | 
			
		||||
          fi
 | 
			
		||||
      - if: failure()
 | 
			
		||||
        name: Review PR
 | 
			
		||||
        uses: actions/github-script@v7.0.1
 | 
			
		||||
        uses: actions/github-script@v8.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          script: |
 | 
			
		||||
            await github.rest.pulls.createReview({
 | 
			
		||||
@@ -70,7 +70,7 @@ jobs:
 | 
			
		||||
            esphome/components/api/api_pb2_service.*
 | 
			
		||||
      - if: success()
 | 
			
		||||
        name: Dismiss review
 | 
			
		||||
        uses: actions/github-script@v7.0.1
 | 
			
		||||
        uses: actions/github-script@v8.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          script: |
 | 
			
		||||
            let reviews = await github.rest.pulls.listReviews({
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										8
									
								
								.github/workflows/ci-clang-tidy-hash.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										8
									
								
								.github/workflows/ci-clang-tidy-hash.yml
									
									
									
									
										vendored
									
									
								
							@@ -20,10 +20,10 @@ jobs:
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Checkout
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
 | 
			
		||||
      - name: Set up Python
 | 
			
		||||
        uses: actions/setup-python@v5.6.0
 | 
			
		||||
        uses: actions/setup-python@v6.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          python-version: "3.11"
 | 
			
		||||
 | 
			
		||||
@@ -41,7 +41,7 @@ jobs:
 | 
			
		||||
 | 
			
		||||
      - if: failure()
 | 
			
		||||
        name: Request changes
 | 
			
		||||
        uses: actions/github-script@v7.0.1
 | 
			
		||||
        uses: actions/github-script@v8.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          script: |
 | 
			
		||||
            await github.rest.pulls.createReview({
 | 
			
		||||
@@ -54,7 +54,7 @@ jobs:
 | 
			
		||||
 | 
			
		||||
      - if: success()
 | 
			
		||||
        name: Dismiss review
 | 
			
		||||
        uses: actions/github-script@v7.0.1
 | 
			
		||||
        uses: actions/github-script@v8.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          script: |
 | 
			
		||||
            let reviews = await github.rest.pulls.listReviews({
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										4
									
								
								.github/workflows/ci-docker.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.github/workflows/ci-docker.yml
									
									
									
									
										vendored
									
									
								
							@@ -43,9 +43,9 @@ jobs:
 | 
			
		||||
          - "docker"
 | 
			
		||||
          # - "lint"
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: actions/checkout@v4.2.2
 | 
			
		||||
      - uses: actions/checkout@v5.0.0
 | 
			
		||||
      - name: Set up Python
 | 
			
		||||
        uses: actions/setup-python@v5.6.0
 | 
			
		||||
        uses: actions/setup-python@v6.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          python-version: "3.11"
 | 
			
		||||
      - name: Set up Docker Buildx
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										38
									
								
								.github/workflows/ci.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										38
									
								
								.github/workflows/ci.yml
									
									
									
									
										vendored
									
									
								
							@@ -36,18 +36,18 @@ jobs:
 | 
			
		||||
      cache-key: ${{ steps.cache-key.outputs.key }}
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
      - name: Generate cache-key
 | 
			
		||||
        id: cache-key
 | 
			
		||||
        run: echo key="${{ hashFiles('requirements.txt', 'requirements_test.txt', '.pre-commit-config.yaml') }}" >> $GITHUB_OUTPUT
 | 
			
		||||
      - name: Set up Python ${{ env.DEFAULT_PYTHON }}
 | 
			
		||||
        id: python
 | 
			
		||||
        uses: actions/setup-python@v5.6.0
 | 
			
		||||
        uses: actions/setup-python@v6.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          python-version: ${{ env.DEFAULT_PYTHON }}
 | 
			
		||||
      - name: Restore Python virtual environment
 | 
			
		||||
        id: cache-venv
 | 
			
		||||
        uses: actions/cache@v4.2.3
 | 
			
		||||
        uses: actions/cache@v4.2.4
 | 
			
		||||
        with:
 | 
			
		||||
          path: venv
 | 
			
		||||
          # yamllint disable-line rule:line-length
 | 
			
		||||
@@ -70,7 +70,7 @@ jobs:
 | 
			
		||||
    if: needs.determine-jobs.outputs.python-linters == 'true'
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
      - name: Restore Python
 | 
			
		||||
        uses: ./.github/actions/restore-python
 | 
			
		||||
        with:
 | 
			
		||||
@@ -91,7 +91,7 @@ jobs:
 | 
			
		||||
      - common
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
      - name: Restore Python
 | 
			
		||||
        uses: ./.github/actions/restore-python
 | 
			
		||||
        with:
 | 
			
		||||
@@ -136,7 +136,7 @@ jobs:
 | 
			
		||||
      - common
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
      - name: Restore Python
 | 
			
		||||
        id: restore-python
 | 
			
		||||
        uses: ./.github/actions/restore-python
 | 
			
		||||
@@ -156,12 +156,12 @@ jobs:
 | 
			
		||||
          . venv/bin/activate
 | 
			
		||||
          pytest -vv --cov-report=xml --tb=native -n auto tests --ignore=tests/integration/
 | 
			
		||||
      - name: Upload coverage to Codecov
 | 
			
		||||
        uses: codecov/codecov-action@v5.4.3
 | 
			
		||||
        uses: codecov/codecov-action@v5.5.1
 | 
			
		||||
        with:
 | 
			
		||||
          token: ${{ secrets.CODECOV_TOKEN }}
 | 
			
		||||
      - name: Save Python virtual environment cache
 | 
			
		||||
        if: github.ref == 'refs/heads/dev'
 | 
			
		||||
        uses: actions/cache/save@v4.2.3
 | 
			
		||||
        uses: actions/cache/save@v4.2.4
 | 
			
		||||
        with:
 | 
			
		||||
          path: venv
 | 
			
		||||
          key: ${{ runner.os }}-${{ steps.restore-python.outputs.python-version }}-venv-${{ needs.common.outputs.cache-key }}
 | 
			
		||||
@@ -179,7 +179,7 @@ jobs:
 | 
			
		||||
      component-test-count: ${{ steps.determine.outputs.component-test-count }}
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          # Fetch enough history to find the merge base
 | 
			
		||||
          fetch-depth: 2
 | 
			
		||||
@@ -214,15 +214,15 @@ jobs:
 | 
			
		||||
    if: needs.determine-jobs.outputs.integration-tests == 'true'
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
      - name: Set up Python 3.13
 | 
			
		||||
        id: python
 | 
			
		||||
        uses: actions/setup-python@v5.6.0
 | 
			
		||||
        uses: actions/setup-python@v6.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          python-version: "3.13"
 | 
			
		||||
      - name: Restore Python virtual environment
 | 
			
		||||
        id: cache-venv
 | 
			
		||||
        uses: actions/cache@v4.2.3
 | 
			
		||||
        uses: actions/cache@v4.2.4
 | 
			
		||||
        with:
 | 
			
		||||
          path: venv
 | 
			
		||||
          key: ${{ runner.os }}-${{ steps.python.outputs.python-version }}-venv-${{ needs.common.outputs.cache-key }}
 | 
			
		||||
@@ -287,7 +287,7 @@ jobs:
 | 
			
		||||
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          # Need history for HEAD~1 to work for checking changed files
 | 
			
		||||
          fetch-depth: 2
 | 
			
		||||
@@ -300,14 +300,14 @@ jobs:
 | 
			
		||||
 | 
			
		||||
      - name: Cache platformio
 | 
			
		||||
        if: github.ref == 'refs/heads/dev'
 | 
			
		||||
        uses: actions/cache@v4.2.3
 | 
			
		||||
        uses: actions/cache@v4.2.4
 | 
			
		||||
        with:
 | 
			
		||||
          path: ~/.platformio
 | 
			
		||||
          key: platformio-${{ matrix.pio_cache_key }}-${{ hashFiles('platformio.ini') }}
 | 
			
		||||
 | 
			
		||||
      - name: Cache platformio
 | 
			
		||||
        if: github.ref != 'refs/heads/dev'
 | 
			
		||||
        uses: actions/cache/restore@v4.2.3
 | 
			
		||||
        uses: actions/cache/restore@v4.2.4
 | 
			
		||||
        with:
 | 
			
		||||
          path: ~/.platformio
 | 
			
		||||
          key: platformio-${{ matrix.pio_cache_key }}-${{ hashFiles('platformio.ini') }}
 | 
			
		||||
@@ -374,7 +374,7 @@ jobs:
 | 
			
		||||
          sudo apt-get install libsdl2-dev
 | 
			
		||||
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
      - name: Restore Python
 | 
			
		||||
        uses: ./.github/actions/restore-python
 | 
			
		||||
        with:
 | 
			
		||||
@@ -400,7 +400,7 @@ jobs:
 | 
			
		||||
      matrix: ${{ steps.split.outputs.components }}
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
      - name: Split components into 20 groups
 | 
			
		||||
        id: split
 | 
			
		||||
        run: |
 | 
			
		||||
@@ -430,7 +430,7 @@ jobs:
 | 
			
		||||
          sudo apt-get install libsdl2-dev
 | 
			
		||||
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
      - name: Restore Python
 | 
			
		||||
        uses: ./.github/actions/restore-python
 | 
			
		||||
        with:
 | 
			
		||||
@@ -459,7 +459,7 @@ jobs:
 | 
			
		||||
    if: github.event_name == 'pull_request' && github.base_ref != 'beta' && github.base_ref != 'release'
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
      - name: Restore Python
 | 
			
		||||
        uses: ./.github/actions/restore-python
 | 
			
		||||
        with:
 | 
			
		||||
 
 | 
			
		||||
@@ -25,7 +25,7 @@ jobs:
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Request reviews from component codeowners
 | 
			
		||||
        uses: actions/github-script@v7.0.1
 | 
			
		||||
        uses: actions/github-script@v8.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          script: |
 | 
			
		||||
            const owner = context.repo.owner;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								.github/workflows/codeql.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/codeql.yml
									
									
									
									
										vendored
									
									
								
							@@ -54,7 +54,7 @@ jobs:
 | 
			
		||||
            # your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Checkout repository
 | 
			
		||||
        uses: actions/checkout@v4
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
 | 
			
		||||
      # Initializes the CodeQL tools for scanning.
 | 
			
		||||
      - name: Initialize CodeQL
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								.github/workflows/external-component-bot.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/external-component-bot.yml
									
									
									
									
										vendored
									
									
								
							@@ -15,7 +15,7 @@ jobs:
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Add external component comment
 | 
			
		||||
        uses: actions/github-script@v7.0.1
 | 
			
		||||
        uses: actions/github-script@v8.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          github-token: ${{ secrets.GITHUB_TOKEN }}
 | 
			
		||||
          script: |
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								.github/workflows/issue-codeowner-notify.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/issue-codeowner-notify.yml
									
									
									
									
										vendored
									
									
								
							@@ -19,7 +19,7 @@ jobs:
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Notify codeowners for component issues
 | 
			
		||||
        uses: actions/github-script@v7.0.1
 | 
			
		||||
        uses: actions/github-script@v8.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          script: |
 | 
			
		||||
            const owner = context.repo.owner;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										24
									
								
								.github/workflows/needs-docs.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										24
									
								
								.github/workflows/needs-docs.yml
									
									
									
									
										vendored
									
									
								
							@@ -1,24 +0,0 @@
 | 
			
		||||
name: Needs Docs
 | 
			
		||||
 | 
			
		||||
on:
 | 
			
		||||
  pull_request:
 | 
			
		||||
    types: [labeled, unlabeled]
 | 
			
		||||
 | 
			
		||||
jobs:
 | 
			
		||||
  check:
 | 
			
		||||
    name: Check
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check for needs-docs label
 | 
			
		||||
        uses: actions/github-script@v7.0.1
 | 
			
		||||
        with:
 | 
			
		||||
          script: |
 | 
			
		||||
            const { data: labels } = await github.rest.issues.listLabelsOnIssue({
 | 
			
		||||
              owner: context.repo.owner,
 | 
			
		||||
              repo: context.repo.repo,
 | 
			
		||||
              issue_number: context.issue.number
 | 
			
		||||
            });
 | 
			
		||||
            const needsDocs = labels.find(label => label.name === 'needs-docs');
 | 
			
		||||
            if (needsDocs) {
 | 
			
		||||
              core.setFailed('Pull request needs docs');
 | 
			
		||||
            }
 | 
			
		||||
							
								
								
									
										20
									
								
								.github/workflows/release.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										20
									
								
								.github/workflows/release.yml
									
									
									
									
										vendored
									
									
								
							@@ -20,7 +20,7 @@ jobs:
 | 
			
		||||
      branch_build: ${{ steps.tag.outputs.branch_build }}
 | 
			
		||||
      deploy_env: ${{ steps.tag.outputs.deploy_env }}
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: actions/checkout@v4.2.2
 | 
			
		||||
      - uses: actions/checkout@v5.0.0
 | 
			
		||||
      - name: Get tag
 | 
			
		||||
        id: tag
 | 
			
		||||
        # yamllint disable rule:line-length
 | 
			
		||||
@@ -60,9 +60,9 @@ jobs:
 | 
			
		||||
      contents: read
 | 
			
		||||
      id-token: write
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: actions/checkout@v4.2.2
 | 
			
		||||
      - uses: actions/checkout@v5.0.0
 | 
			
		||||
      - name: Set up Python
 | 
			
		||||
        uses: actions/setup-python@v5.6.0
 | 
			
		||||
        uses: actions/setup-python@v6.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          python-version: "3.x"
 | 
			
		||||
      - name: Build
 | 
			
		||||
@@ -70,7 +70,7 @@ jobs:
 | 
			
		||||
          pip3 install build
 | 
			
		||||
          python3 -m build
 | 
			
		||||
      - name: Publish
 | 
			
		||||
        uses: pypa/gh-action-pypi-publish@v1.12.4
 | 
			
		||||
        uses: pypa/gh-action-pypi-publish@v1.13.0
 | 
			
		||||
        with:
 | 
			
		||||
          skip-existing: true
 | 
			
		||||
 | 
			
		||||
@@ -92,9 +92,9 @@ jobs:
 | 
			
		||||
            os: "ubuntu-24.04-arm"
 | 
			
		||||
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: actions/checkout@v4.2.2
 | 
			
		||||
      - uses: actions/checkout@v5.0.0
 | 
			
		||||
      - name: Set up Python
 | 
			
		||||
        uses: actions/setup-python@v5.6.0
 | 
			
		||||
        uses: actions/setup-python@v6.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          python-version: "3.11"
 | 
			
		||||
 | 
			
		||||
@@ -168,10 +168,10 @@ jobs:
 | 
			
		||||
          - ghcr
 | 
			
		||||
          - dockerhub
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: actions/checkout@v4.2.2
 | 
			
		||||
      - uses: actions/checkout@v5.0.0
 | 
			
		||||
 | 
			
		||||
      - name: Download digests
 | 
			
		||||
        uses: actions/download-artifact@v4.3.0
 | 
			
		||||
        uses: actions/download-artifact@v5.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          pattern: digests-*
 | 
			
		||||
          path: /tmp/digests
 | 
			
		||||
@@ -220,7 +220,7 @@ jobs:
 | 
			
		||||
      - deploy-manifest
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Trigger Workflow
 | 
			
		||||
        uses: actions/github-script@v7.0.1
 | 
			
		||||
        uses: actions/github-script@v8.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          github-token: ${{ secrets.DEPLOY_HA_ADDON_REPO_TOKEN }}
 | 
			
		||||
          script: |
 | 
			
		||||
@@ -246,7 +246,7 @@ jobs:
 | 
			
		||||
    environment: ${{ needs.init.outputs.deploy_env }}
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Trigger Workflow
 | 
			
		||||
        uses: actions/github-script@v7.0.1
 | 
			
		||||
        uses: actions/github-script@v8.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          github-token: ${{ secrets.DEPLOY_ESPHOME_SCHEMA_REPO_TOKEN }}
 | 
			
		||||
          script: |
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										4
									
								
								.github/workflows/stale.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.github/workflows/stale.yml
									
									
									
									
										vendored
									
									
								
							@@ -17,7 +17,7 @@ jobs:
 | 
			
		||||
  stale:
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: actions/stale@v9.1.0
 | 
			
		||||
      - uses: actions/stale@v10.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          days-before-pr-stale: 90
 | 
			
		||||
          days-before-pr-close: 7
 | 
			
		||||
@@ -37,7 +37,7 @@ jobs:
 | 
			
		||||
  close-issues:
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: actions/stale@v9.1.0
 | 
			
		||||
      - uses: actions/stale@v10.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          days-before-pr-stale: -1
 | 
			
		||||
          days-before-pr-close: -1
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										30
									
								
								.github/workflows/status-check-labels.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								.github/workflows/status-check-labels.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
name: Status check labels
 | 
			
		||||
 | 
			
		||||
on:
 | 
			
		||||
  pull_request:
 | 
			
		||||
    types: [labeled, unlabeled]
 | 
			
		||||
 | 
			
		||||
jobs:
 | 
			
		||||
  check:
 | 
			
		||||
    name: Check ${{ matrix.label }}
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    strategy:
 | 
			
		||||
      fail-fast: false
 | 
			
		||||
      matrix:
 | 
			
		||||
        label:
 | 
			
		||||
          - needs-docs
 | 
			
		||||
          - merge-after-release
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check for ${{ matrix.label }} label
 | 
			
		||||
        uses: actions/github-script@v8.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          script: |
 | 
			
		||||
            const { data: labels } = await github.rest.issues.listLabelsOnIssue({
 | 
			
		||||
              owner: context.repo.owner,
 | 
			
		||||
              repo: context.repo.repo,
 | 
			
		||||
              issue_number: context.issue.number
 | 
			
		||||
            });
 | 
			
		||||
            const hasLabel = labels.find(label => label.name === '${{ matrix.label }}');
 | 
			
		||||
            if (hasLabel) {
 | 
			
		||||
              core.setFailed('Pull request cannot be merged, it is labeled as ${{ matrix.label }}');
 | 
			
		||||
            }
 | 
			
		||||
							
								
								
									
										6
									
								
								.github/workflows/sync-device-classes.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										6
									
								
								.github/workflows/sync-device-classes.yml
									
									
									
									
										vendored
									
									
								
							@@ -13,16 +13,16 @@ jobs:
 | 
			
		||||
    if: github.repository == 'esphome/esphome'
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Checkout
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
 | 
			
		||||
      - name: Checkout Home Assistant
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          repository: home-assistant/core
 | 
			
		||||
          path: lib/home-assistant
 | 
			
		||||
 | 
			
		||||
      - name: Setup Python
 | 
			
		||||
        uses: actions/setup-python@v5.6.0
 | 
			
		||||
        uses: actions/setup-python@v6.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          python-version: 3.13
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -11,7 +11,7 @@ ci:
 | 
			
		||||
repos:
 | 
			
		||||
  - repo: https://github.com/astral-sh/ruff-pre-commit
 | 
			
		||||
    # Ruff version.
 | 
			
		||||
    rev: v0.12.7
 | 
			
		||||
    rev: v0.12.12
 | 
			
		||||
    hooks:
 | 
			
		||||
      # Run the linter.
 | 
			
		||||
      - id: ruff
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										35
									
								
								CODEOWNERS
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								CODEOWNERS
									
									
									
									
									
								
							@@ -66,7 +66,7 @@ esphome/components/binary_sensor/* @esphome/core
 | 
			
		||||
esphome/components/bk72xx/* @kuba2k2
 | 
			
		||||
esphome/components/bl0906/* @athom-tech @jesserockz @tarontop
 | 
			
		||||
esphome/components/bl0939/* @ziceva
 | 
			
		||||
esphome/components/bl0940/* @tobias-
 | 
			
		||||
esphome/components/bl0940/* @dan-s-github @tobias-
 | 
			
		||||
esphome/components/bl0942/* @dbuezas @dwmw2
 | 
			
		||||
esphome/components/ble_client/* @buxtronix @clydebarrow
 | 
			
		||||
esphome/components/bluetooth_proxy/* @bdraco @jesserockz
 | 
			
		||||
@@ -88,7 +88,8 @@ esphome/components/bp1658cj/* @Cossid
 | 
			
		||||
esphome/components/bp5758d/* @Cossid
 | 
			
		||||
esphome/components/button/* @esphome/core
 | 
			
		||||
esphome/components/bytebuffer/* @clydebarrow
 | 
			
		||||
esphome/components/camera/* @DT-art1 @bdraco
 | 
			
		||||
esphome/components/camera/* @bdraco @DT-art1
 | 
			
		||||
esphome/components/camera_encoder/* @DT-art1
 | 
			
		||||
esphome/components/canbus/* @danielschramm @mvturnho
 | 
			
		||||
esphome/components/cap1188/* @mreditor97
 | 
			
		||||
esphome/components/captive_portal/* @esphome/core
 | 
			
		||||
@@ -144,9 +145,9 @@ esphome/components/es8156/* @kbx81
 | 
			
		||||
esphome/components/es8311/* @kahrendt @kroimon
 | 
			
		||||
esphome/components/es8388/* @P4uLT
 | 
			
		||||
esphome/components/esp32/* @esphome/core
 | 
			
		||||
esphome/components/esp32_ble/* @Rapsssito @bdraco @jesserockz
 | 
			
		||||
esphome/components/esp32_ble/* @bdraco @jesserockz @Rapsssito
 | 
			
		||||
esphome/components/esp32_ble_client/* @bdraco @jesserockz
 | 
			
		||||
esphome/components/esp32_ble_server/* @Rapsssito @clydebarrow @jesserockz
 | 
			
		||||
esphome/components/esp32_ble_server/* @clydebarrow @jesserockz @Rapsssito
 | 
			
		||||
esphome/components/esp32_ble_tracker/* @bdraco
 | 
			
		||||
esphome/components/esp32_camera_web_server/* @ayufan
 | 
			
		||||
esphome/components/esp32_can/* @Sympatron
 | 
			
		||||
@@ -166,7 +167,7 @@ esphome/components/ezo_pmp/* @carlos-sarmiento
 | 
			
		||||
esphome/components/factory_reset/* @anatoly-savchenkov
 | 
			
		||||
esphome/components/fastled_base/* @OttoWinter
 | 
			
		||||
esphome/components/feedback/* @ianchi
 | 
			
		||||
esphome/components/fingerprint_grow/* @OnFreund @alexborro @loongyh
 | 
			
		||||
esphome/components/fingerprint_grow/* @alexborro @loongyh @OnFreund
 | 
			
		||||
esphome/components/font/* @clydebarrow @esphome/core
 | 
			
		||||
esphome/components/fs3000/* @kahrendt
 | 
			
		||||
esphome/components/ft5x06/* @clydebarrow
 | 
			
		||||
@@ -202,7 +203,7 @@ esphome/components/heatpumpir/* @rob-deutsch
 | 
			
		||||
esphome/components/hitachi_ac424/* @sourabhjaiswal
 | 
			
		||||
esphome/components/hm3301/* @freekode
 | 
			
		||||
esphome/components/hmac_md5/* @dwmw2
 | 
			
		||||
esphome/components/homeassistant/* @OttoWinter @esphome/core
 | 
			
		||||
esphome/components/homeassistant/* @esphome/core @OttoWinter
 | 
			
		||||
esphome/components/homeassistant/number/* @landonr
 | 
			
		||||
esphome/components/homeassistant/switch/* @Links2004
 | 
			
		||||
esphome/components/honeywell_hih_i2c/* @Benichou34
 | 
			
		||||
@@ -227,13 +228,13 @@ esphome/components/iaqcore/* @yozik04
 | 
			
		||||
esphome/components/ili9xxx/* @clydebarrow @nielsnl68
 | 
			
		||||
esphome/components/improv_base/* @esphome/core
 | 
			
		||||
esphome/components/improv_serial/* @esphome/core
 | 
			
		||||
esphome/components/ina226/* @Sergio303 @latonita
 | 
			
		||||
esphome/components/ina226/* @latonita @Sergio303
 | 
			
		||||
esphome/components/ina260/* @mreditor97
 | 
			
		||||
esphome/components/ina2xx_base/* @latonita
 | 
			
		||||
esphome/components/ina2xx_i2c/* @latonita
 | 
			
		||||
esphome/components/ina2xx_spi/* @latonita
 | 
			
		||||
esphome/components/inkbird_ibsth1_mini/* @fkirill
 | 
			
		||||
esphome/components/inkplate6/* @jesserockz
 | 
			
		||||
esphome/components/inkplate/* @jesserockz @JosipKuci
 | 
			
		||||
esphome/components/integration/* @OttoWinter
 | 
			
		||||
esphome/components/internal_temperature/* @Mat931
 | 
			
		||||
esphome/components/interval/* @esphome/core
 | 
			
		||||
@@ -246,6 +247,7 @@ esphome/components/kuntze/* @ssieb
 | 
			
		||||
esphome/components/lc709203f/* @ilikecake
 | 
			
		||||
esphome/components/lcd_menu/* @numo68
 | 
			
		||||
esphome/components/ld2410/* @regevbr @sebcaps
 | 
			
		||||
esphome/components/ld2412/* @Rihan9
 | 
			
		||||
esphome/components/ld2420/* @descipher
 | 
			
		||||
esphome/components/ld2450/* @hareeshmu
 | 
			
		||||
esphome/components/ld24xx/* @kbx81
 | 
			
		||||
@@ -275,8 +277,8 @@ esphome/components/max7219digit/* @rspaargaren
 | 
			
		||||
esphome/components/max9611/* @mckaymatthew
 | 
			
		||||
esphome/components/mcp23008/* @jesserockz
 | 
			
		||||
esphome/components/mcp23017/* @jesserockz
 | 
			
		||||
esphome/components/mcp23s08/* @SenexCrenshaw @jesserockz
 | 
			
		||||
esphome/components/mcp23s17/* @SenexCrenshaw @jesserockz
 | 
			
		||||
esphome/components/mcp23s08/* @jesserockz @SenexCrenshaw
 | 
			
		||||
esphome/components/mcp23s17/* @jesserockz @SenexCrenshaw
 | 
			
		||||
esphome/components/mcp23x08_base/* @jesserockz
 | 
			
		||||
esphome/components/mcp23x17_base/* @jesserockz
 | 
			
		||||
esphome/components/mcp23xxx_base/* @jesserockz
 | 
			
		||||
@@ -297,6 +299,7 @@ esphome/components/mics_4514/* @jesserockz
 | 
			
		||||
esphome/components/midea/* @dudanov
 | 
			
		||||
esphome/components/midea_ir/* @dudanov
 | 
			
		||||
esphome/components/mipi_dsi/* @clydebarrow
 | 
			
		||||
esphome/components/mipi_rgb/* @clydebarrow
 | 
			
		||||
esphome/components/mipi_spi/* @clydebarrow
 | 
			
		||||
esphome/components/mitsubishi/* @RubyBailey
 | 
			
		||||
esphome/components/mixer/speaker/* @kahrendt
 | 
			
		||||
@@ -340,7 +343,7 @@ esphome/components/ota/* @esphome/core
 | 
			
		||||
esphome/components/output/* @esphome/core
 | 
			
		||||
esphome/components/packet_transport/* @clydebarrow
 | 
			
		||||
esphome/components/pca6416a/* @Mat931
 | 
			
		||||
esphome/components/pca9554/* @clydebarrow @hwstar
 | 
			
		||||
esphome/components/pca9554/* @bdraco @clydebarrow @hwstar
 | 
			
		||||
esphome/components/pcf85063/* @brogon
 | 
			
		||||
esphome/components/pcf8563/* @KoenBreeman
 | 
			
		||||
esphome/components/pi4ioe5v6408/* @jesserockz
 | 
			
		||||
@@ -351,9 +354,9 @@ esphome/components/pm2005/* @andrewjswan
 | 
			
		||||
esphome/components/pmsa003i/* @sjtrny
 | 
			
		||||
esphome/components/pmsx003/* @ximex
 | 
			
		||||
esphome/components/pmwcs3/* @SeByDocKy
 | 
			
		||||
esphome/components/pn532/* @OttoWinter @jesserockz
 | 
			
		||||
esphome/components/pn532_i2c/* @OttoWinter @jesserockz
 | 
			
		||||
esphome/components/pn532_spi/* @OttoWinter @jesserockz
 | 
			
		||||
esphome/components/pn532/* @jesserockz @OttoWinter
 | 
			
		||||
esphome/components/pn532_i2c/* @jesserockz @OttoWinter
 | 
			
		||||
esphome/components/pn532_spi/* @jesserockz @OttoWinter
 | 
			
		||||
esphome/components/pn7150/* @jesserockz @kbx81
 | 
			
		||||
esphome/components/pn7150_i2c/* @jesserockz @kbx81
 | 
			
		||||
esphome/components/pn7160/* @jesserockz @kbx81
 | 
			
		||||
@@ -362,7 +365,7 @@ esphome/components/pn7160_spi/* @jesserockz @kbx81
 | 
			
		||||
esphome/components/power_supply/* @esphome/core
 | 
			
		||||
esphome/components/preferences/* @esphome/core
 | 
			
		||||
esphome/components/psram/* @esphome/core
 | 
			
		||||
esphome/components/pulse_meter/* @TrentHouliston @cstaahl @stevebaxter
 | 
			
		||||
esphome/components/pulse_meter/* @cstaahl @stevebaxter @TrentHouliston
 | 
			
		||||
esphome/components/pvvx_mithermometer/* @pasiz
 | 
			
		||||
esphome/components/pylontech/* @functionpointer
 | 
			
		||||
esphome/components/qmp6988/* @andrewpc
 | 
			
		||||
@@ -403,7 +406,7 @@ esphome/components/sensirion_common/* @martgras
 | 
			
		||||
esphome/components/sensor/* @esphome/core
 | 
			
		||||
esphome/components/sfa30/* @ghsensdev
 | 
			
		||||
esphome/components/sgp40/* @SenexCrenshaw
 | 
			
		||||
esphome/components/sgp4x/* @SenexCrenshaw @martgras
 | 
			
		||||
esphome/components/sgp4x/* @martgras @SenexCrenshaw
 | 
			
		||||
esphome/components/shelly_dimmer/* @edge90 @rnauber
 | 
			
		||||
esphome/components/sht3xd/* @mrtoy-me
 | 
			
		||||
esphome/components/sht4x/* @sjtrny
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								Doxyfile
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								Doxyfile
									
									
									
									
									
								
							@@ -48,7 +48,7 @@ PROJECT_NAME           = ESPHome
 | 
			
		||||
# could be handy for archiving the generated documentation or if some version
 | 
			
		||||
# control system is used.
 | 
			
		||||
 | 
			
		||||
PROJECT_NUMBER         = 2025.8.0-dev
 | 
			
		||||
PROJECT_NUMBER         = 2025.9.3
 | 
			
		||||
 | 
			
		||||
# Using the PROJECT_BRIEF tag one can provide an optional one line description
 | 
			
		||||
# for a project that appears at the top of each page and should give viewer a
 | 
			
		||||
 
 | 
			
		||||
@@ -90,7 +90,7 @@ def main():
 | 
			
		||||
    def run_command(*cmd, ignore_error: bool = False):
 | 
			
		||||
        print(f"$ {shlex.join(list(cmd))}")
 | 
			
		||||
        if not args.dry_run:
 | 
			
		||||
            rc = subprocess.call(list(cmd))
 | 
			
		||||
            rc = subprocess.call(list(cmd), close_fds=False)
 | 
			
		||||
            if rc != 0 and not ignore_error:
 | 
			
		||||
                print("Command failed")
 | 
			
		||||
                sys.exit(1)
 | 
			
		||||
 
 | 
			
		||||
@@ -15,9 +15,11 @@ import argcomplete
 | 
			
		||||
 | 
			
		||||
from esphome import const, writer, yaml_util
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
from esphome.components.mqtt import CONF_DISCOVER_IP
 | 
			
		||||
from esphome.config import iter_component_configs, read_config, strip_default_ids
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    ALLOWED_NAME_CHARS,
 | 
			
		||||
    CONF_API,
 | 
			
		||||
    CONF_BAUD_RATE,
 | 
			
		||||
    CONF_BROKER,
 | 
			
		||||
    CONF_DEASSERT_RTS_DTR,
 | 
			
		||||
@@ -43,6 +45,7 @@ from esphome.const import (
 | 
			
		||||
    SECRETS_FILES,
 | 
			
		||||
)
 | 
			
		||||
from esphome.core import CORE, EsphomeError, coroutine
 | 
			
		||||
from esphome.enum import StrEnum
 | 
			
		||||
from esphome.helpers import get_bool_env, indent, is_ip_address
 | 
			
		||||
from esphome.log import AnsiFore, color, setup_log
 | 
			
		||||
from esphome.types import ConfigType
 | 
			
		||||
@@ -106,13 +109,15 @@ def choose_prompt(options, purpose: str = None):
 | 
			
		||||
    return options[opt - 1][1]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Purpose(StrEnum):
 | 
			
		||||
    UPLOADING = "uploading"
 | 
			
		||||
    LOGGING = "logging"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def choose_upload_log_host(
 | 
			
		||||
    default: list[str] | str | None,
 | 
			
		||||
    check_default: str | None,
 | 
			
		||||
    show_ota: bool,
 | 
			
		||||
    show_mqtt: bool,
 | 
			
		||||
    show_api: bool,
 | 
			
		||||
    purpose: str | None = None,
 | 
			
		||||
    purpose: Purpose,
 | 
			
		||||
) -> list[str]:
 | 
			
		||||
    # Convert to list for uniform handling
 | 
			
		||||
    defaults = [default] if isinstance(default, str) else default or []
 | 
			
		||||
@@ -132,53 +137,145 @@ def choose_upload_log_host(
 | 
			
		||||
                ]
 | 
			
		||||
                resolved.append(choose_prompt(options, purpose=purpose))
 | 
			
		||||
            elif device == "OTA":
 | 
			
		||||
                if (show_ota and "ota" in CORE.config) or (
 | 
			
		||||
                    show_api and "api" in CORE.config
 | 
			
		||||
                # ensure IP adresses are used first
 | 
			
		||||
                if is_ip_address(CORE.address) and (
 | 
			
		||||
                    (purpose == Purpose.LOGGING and has_api())
 | 
			
		||||
                    or (purpose == Purpose.UPLOADING and has_ota())
 | 
			
		||||
                ):
 | 
			
		||||
                    resolved.append(CORE.address)
 | 
			
		||||
                elif show_mqtt and has_mqtt_logging():
 | 
			
		||||
                    resolved.append("MQTT")
 | 
			
		||||
 | 
			
		||||
                if purpose == Purpose.LOGGING:
 | 
			
		||||
                    if has_api() and has_mqtt_ip_lookup():
 | 
			
		||||
                        resolved.append("MQTTIP")
 | 
			
		||||
 | 
			
		||||
                    if has_mqtt_logging():
 | 
			
		||||
                        resolved.append("MQTT")
 | 
			
		||||
 | 
			
		||||
                    if has_api() and has_non_ip_address():
 | 
			
		||||
                        resolved.append(CORE.address)
 | 
			
		||||
 | 
			
		||||
                elif purpose == Purpose.UPLOADING:
 | 
			
		||||
                    if has_ota() and has_mqtt_ip_lookup():
 | 
			
		||||
                        resolved.append("MQTTIP")
 | 
			
		||||
 | 
			
		||||
                    if has_ota() and has_non_ip_address():
 | 
			
		||||
                        resolved.append(CORE.address)
 | 
			
		||||
 | 
			
		||||
            else:
 | 
			
		||||
                resolved.append(device)
 | 
			
		||||
        if not resolved:
 | 
			
		||||
            _LOGGER.error("All specified devices: %s could not be resolved.", defaults)
 | 
			
		||||
        return resolved
 | 
			
		||||
 | 
			
		||||
    # No devices specified, show interactive chooser
 | 
			
		||||
    options = [
 | 
			
		||||
        (f"{port.path} ({port.description})", port.path) for port in get_serial_ports()
 | 
			
		||||
    ]
 | 
			
		||||
    if (show_ota and "ota" in CORE.config) or (show_api and "api" in CORE.config):
 | 
			
		||||
        options.append((f"Over The Air ({CORE.address})", CORE.address))
 | 
			
		||||
    if show_mqtt and has_mqtt_logging():
 | 
			
		||||
        mqtt_config = CORE.config[CONF_MQTT]
 | 
			
		||||
        options.append((f"MQTT ({mqtt_config[CONF_BROKER]})", "MQTT"))
 | 
			
		||||
 | 
			
		||||
    if purpose == Purpose.LOGGING:
 | 
			
		||||
        if has_mqtt_logging():
 | 
			
		||||
            mqtt_config = CORE.config[CONF_MQTT]
 | 
			
		||||
            options.append((f"MQTT ({mqtt_config[CONF_BROKER]})", "MQTT"))
 | 
			
		||||
 | 
			
		||||
        if has_api():
 | 
			
		||||
            if has_resolvable_address():
 | 
			
		||||
                options.append((f"Over The Air ({CORE.address})", CORE.address))
 | 
			
		||||
            if has_mqtt_ip_lookup():
 | 
			
		||||
                options.append(("Over The Air (MQTT IP lookup)", "MQTTIP"))
 | 
			
		||||
 | 
			
		||||
    elif purpose == Purpose.UPLOADING and has_ota():
 | 
			
		||||
        if has_resolvable_address():
 | 
			
		||||
            options.append((f"Over The Air ({CORE.address})", CORE.address))
 | 
			
		||||
        if has_mqtt_ip_lookup():
 | 
			
		||||
            options.append(("Over The Air (MQTT IP lookup)", "MQTTIP"))
 | 
			
		||||
 | 
			
		||||
    if check_default is not None and check_default in [opt[1] for opt in options]:
 | 
			
		||||
        return [check_default]
 | 
			
		||||
    return [choose_prompt(options, purpose=purpose)]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def mqtt_logging_enabled(mqtt_config):
 | 
			
		||||
def has_mqtt_logging() -> bool:
 | 
			
		||||
    """Check if MQTT logging is available."""
 | 
			
		||||
    if CONF_MQTT not in CORE.config:
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
    mqtt_config = CORE.config[CONF_MQTT]
 | 
			
		||||
 | 
			
		||||
    # enabled by default
 | 
			
		||||
    if CONF_LOG_TOPIC not in mqtt_config:
 | 
			
		||||
        return True
 | 
			
		||||
 | 
			
		||||
    log_topic = mqtt_config[CONF_LOG_TOPIC]
 | 
			
		||||
    if log_topic is None:
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
    if CONF_TOPIC not in log_topic:
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
    return log_topic.get(CONF_LEVEL, None) != "NONE"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def has_mqtt_logging() -> bool:
 | 
			
		||||
    """Check if MQTT logging is available."""
 | 
			
		||||
    return (mqtt_config := CORE.config.get(CONF_MQTT)) and mqtt_logging_enabled(
 | 
			
		||||
        mqtt_config
 | 
			
		||||
    )
 | 
			
		||||
def has_mqtt() -> bool:
 | 
			
		||||
    """Check if MQTT is available."""
 | 
			
		||||
    return CONF_MQTT in CORE.config
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def has_api() -> bool:
 | 
			
		||||
    """Check if API is available."""
 | 
			
		||||
    return CONF_API in CORE.config
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def has_ota() -> bool:
 | 
			
		||||
    """Check if OTA is available."""
 | 
			
		||||
    return CONF_OTA in CORE.config
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def has_mqtt_ip_lookup() -> bool:
 | 
			
		||||
    """Check if MQTT is available and IP lookup is supported."""
 | 
			
		||||
    if CONF_MQTT not in CORE.config:
 | 
			
		||||
        return False
 | 
			
		||||
    # Default Enabled
 | 
			
		||||
    if CONF_DISCOVER_IP not in CORE.config[CONF_MQTT]:
 | 
			
		||||
        return True
 | 
			
		||||
    return CORE.config[CONF_MQTT][CONF_DISCOVER_IP]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def has_mdns() -> bool:
 | 
			
		||||
    """Check if MDNS is available."""
 | 
			
		||||
    return CONF_MDNS not in CORE.config or not CORE.config[CONF_MDNS][CONF_DISABLED]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def has_non_ip_address() -> bool:
 | 
			
		||||
    """Check if CORE.address is set and is not an IP address."""
 | 
			
		||||
    return CORE.address is not None and not is_ip_address(CORE.address)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def has_ip_address() -> bool:
 | 
			
		||||
    """Check if CORE.address is a valid IP address."""
 | 
			
		||||
    return CORE.address is not None and is_ip_address(CORE.address)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def has_resolvable_address() -> bool:
 | 
			
		||||
    """Check if CORE.address is resolvable (via mDNS or is an IP address)."""
 | 
			
		||||
    return has_mdns() or has_ip_address()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def mqtt_get_ip(config: ConfigType, username: str, password: str, client_id: str):
 | 
			
		||||
    from esphome import mqtt
 | 
			
		||||
 | 
			
		||||
    return mqtt.get_esphome_device_ip(config, username, password, client_id)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
_PORT_TO_PORT_TYPE = {
 | 
			
		||||
    "MQTT": "MQTT",
 | 
			
		||||
    "MQTTIP": "MQTTIP",
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_port_type(port: str) -> str:
 | 
			
		||||
    if port.startswith("/") or port.startswith("COM"):
 | 
			
		||||
        return "SERIAL"
 | 
			
		||||
    if port == "MQTT":
 | 
			
		||||
        return "MQTT"
 | 
			
		||||
    return "NETWORK"
 | 
			
		||||
    return _PORT_TO_PORT_TYPE.get(port, "NETWORK")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def run_miniterm(config: ConfigType, port: str, args) -> int:
 | 
			
		||||
@@ -223,7 +320,9 @@ def run_miniterm(config: ConfigType, port: str, args) -> int:
 | 
			
		||||
                        .replace(b"\n", b"")
 | 
			
		||||
                        .decode("utf8", "backslashreplace")
 | 
			
		||||
                    )
 | 
			
		||||
                    time_str = datetime.now().time().strftime("[%H:%M:%S]")
 | 
			
		||||
                    time_ = datetime.now()
 | 
			
		||||
                    nanoseconds = time_.microsecond // 1000
 | 
			
		||||
                    time_str = f"[{time_.hour:02}:{time_.minute:02}:{time_.second:02}.{nanoseconds:03}]"
 | 
			
		||||
                    safe_print(parser.parse_line(line, time_str))
 | 
			
		||||
 | 
			
		||||
                    backtrace_state = platformio_api.process_stacktrace(
 | 
			
		||||
@@ -393,27 +492,29 @@ def check_permissions(port: str):
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def upload_program(config: ConfigType, args: ArgsProtocol, host: str) -> int | str:
 | 
			
		||||
def upload_program(
 | 
			
		||||
    config: ConfigType, args: ArgsProtocol, devices: list[str]
 | 
			
		||||
) -> tuple[int, str | None]:
 | 
			
		||||
    host = devices[0]
 | 
			
		||||
    try:
 | 
			
		||||
        module = importlib.import_module("esphome.components." + CORE.target_platform)
 | 
			
		||||
        if getattr(module, "upload_program")(config, args, host):
 | 
			
		||||
            return 0
 | 
			
		||||
            return 0, host
 | 
			
		||||
    except AttributeError:
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    if get_port_type(host) == "SERIAL":
 | 
			
		||||
        check_permissions(host)
 | 
			
		||||
 | 
			
		||||
        exit_code = 1
 | 
			
		||||
        if CORE.target_platform in (PLATFORM_ESP32, PLATFORM_ESP8266):
 | 
			
		||||
            file = getattr(args, "file", None)
 | 
			
		||||
            return upload_using_esptool(config, host, file, args.upload_speed)
 | 
			
		||||
            exit_code = upload_using_esptool(config, host, file, args.upload_speed)
 | 
			
		||||
        elif CORE.target_platform == PLATFORM_RP2040 or CORE.is_libretiny:
 | 
			
		||||
            exit_code = upload_using_platformio(config, host)
 | 
			
		||||
        # else: Unknown target platform, exit_code remains 1
 | 
			
		||||
 | 
			
		||||
        if CORE.target_platform in (PLATFORM_RP2040):
 | 
			
		||||
            return upload_using_platformio(config, host)
 | 
			
		||||
 | 
			
		||||
        if CORE.is_libretiny:
 | 
			
		||||
            return upload_using_platformio(config, host)
 | 
			
		||||
 | 
			
		||||
        return 1  # Unknown target platform
 | 
			
		||||
        return exit_code, host if exit_code == 0 else None
 | 
			
		||||
 | 
			
		||||
    ota_conf = {}
 | 
			
		||||
    for ota_item in config.get(CONF_OTA, []):
 | 
			
		||||
@@ -430,31 +531,23 @@ def upload_program(config: ConfigType, args: ArgsProtocol, host: str) -> int | s
 | 
			
		||||
 | 
			
		||||
    remote_port = int(ota_conf[CONF_PORT])
 | 
			
		||||
    password = ota_conf.get(CONF_PASSWORD, "")
 | 
			
		||||
    binary = args.file if getattr(args, "file", None) is not None else CORE.firmware_bin
 | 
			
		||||
 | 
			
		||||
    # Check if we should use MQTT for address resolution
 | 
			
		||||
    # This happens when no device was specified, or the current host is "MQTT"/"OTA"
 | 
			
		||||
    devices: list[str] = args.device or []
 | 
			
		||||
    if (
 | 
			
		||||
        CONF_MQTT in config  # pylint: disable=too-many-boolean-expressions
 | 
			
		||||
        and (not devices or host in ("MQTT", "OTA"))
 | 
			
		||||
        and (
 | 
			
		||||
            ((config[CONF_MDNS][CONF_DISABLED]) and not is_ip_address(CORE.address))
 | 
			
		||||
            or get_port_type(host) == "MQTT"
 | 
			
		||||
        )
 | 
			
		||||
    ):
 | 
			
		||||
        from esphome import mqtt
 | 
			
		||||
    # MQTT address resolution
 | 
			
		||||
    if get_port_type(host) in ("MQTT", "MQTTIP"):
 | 
			
		||||
        devices = mqtt_get_ip(config, args.username, args.password, args.client_id)
 | 
			
		||||
 | 
			
		||||
        host = mqtt.get_esphome_device_ip(
 | 
			
		||||
            config, args.username, args.password, args.client_id
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    if getattr(args, "file", None) is not None:
 | 
			
		||||
        return espota2.run_ota(host, remote_port, password, args.file)
 | 
			
		||||
 | 
			
		||||
    return espota2.run_ota(host, remote_port, password, CORE.firmware_bin)
 | 
			
		||||
    return espota2.run_ota(devices, remote_port, password, binary)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def show_logs(config: ConfigType, args: ArgsProtocol, devices: list[str]) -> int | None:
 | 
			
		||||
    try:
 | 
			
		||||
        module = importlib.import_module("esphome.components." + CORE.target_platform)
 | 
			
		||||
        if getattr(module, "show_logs")(config, args, devices):
 | 
			
		||||
            return 0
 | 
			
		||||
    except AttributeError:
 | 
			
		||||
        pass
 | 
			
		||||
 | 
			
		||||
    if "logger" not in config:
 | 
			
		||||
        raise EsphomeError("Logger is not configured!")
 | 
			
		||||
 | 
			
		||||
@@ -463,20 +556,28 @@ def show_logs(config: ConfigType, args: ArgsProtocol, devices: list[str]) -> int
 | 
			
		||||
    if get_port_type(port) == "SERIAL":
 | 
			
		||||
        check_permissions(port)
 | 
			
		||||
        return run_miniterm(config, port, args)
 | 
			
		||||
    if get_port_type(port) == "NETWORK" and "api" in config:
 | 
			
		||||
        addresses_to_use = devices
 | 
			
		||||
        if config[CONF_MDNS][CONF_DISABLED] and CONF_MQTT in config:
 | 
			
		||||
            from esphome import mqtt
 | 
			
		||||
 | 
			
		||||
            mqtt_address = mqtt.get_esphome_device_ip(
 | 
			
		||||
    port_type = get_port_type(port)
 | 
			
		||||
 | 
			
		||||
    # Check if we should use API for logging
 | 
			
		||||
    if has_api():
 | 
			
		||||
        addresses_to_use: list[str] | None = None
 | 
			
		||||
 | 
			
		||||
        if port_type == "NETWORK" and (has_mdns() or is_ip_address(port)):
 | 
			
		||||
            addresses_to_use = devices
 | 
			
		||||
        elif port_type in ("NETWORK", "MQTT", "MQTTIP") and has_mqtt_ip_lookup():
 | 
			
		||||
            # Only use MQTT IP lookup if the first condition didn't match
 | 
			
		||||
            # (for MQTT/MQTTIP types, or for NETWORK when mdns/ip check fails)
 | 
			
		||||
            addresses_to_use = mqtt_get_ip(
 | 
			
		||||
                config, args.username, args.password, args.client_id
 | 
			
		||||
            )[0]
 | 
			
		||||
            addresses_to_use = [mqtt_address]
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
        from esphome.components.api.client import run_logs
 | 
			
		||||
        if addresses_to_use is not None:
 | 
			
		||||
            from esphome.components.api.client import run_logs
 | 
			
		||||
 | 
			
		||||
        return run_logs(config, addresses_to_use)
 | 
			
		||||
    if get_port_type(port) == "MQTT" and "mqtt" in config:
 | 
			
		||||
            return run_logs(config, addresses_to_use)
 | 
			
		||||
 | 
			
		||||
    if port_type in ("NETWORK", "MQTT") and has_mqtt_logging():
 | 
			
		||||
        from esphome import mqtt
 | 
			
		||||
 | 
			
		||||
        return mqtt.show_logs(
 | 
			
		||||
@@ -542,23 +643,14 @@ def command_upload(args: ArgsProtocol, config: ConfigType) -> int | None:
 | 
			
		||||
    devices = choose_upload_log_host(
 | 
			
		||||
        default=args.device,
 | 
			
		||||
        check_default=None,
 | 
			
		||||
        show_ota=True,
 | 
			
		||||
        show_mqtt=False,
 | 
			
		||||
        show_api=False,
 | 
			
		||||
        purpose="uploading",
 | 
			
		||||
        purpose=Purpose.UPLOADING,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    # Try each device until one succeeds
 | 
			
		||||
    exit_code = 1
 | 
			
		||||
    for device in devices:
 | 
			
		||||
        _LOGGER.info("Uploading to %s", device)
 | 
			
		||||
        exit_code = upload_program(config, args, device)
 | 
			
		||||
        if exit_code == 0:
 | 
			
		||||
            _LOGGER.info("Successfully uploaded program.")
 | 
			
		||||
            return 0
 | 
			
		||||
        if len(devices) > 1:
 | 
			
		||||
            _LOGGER.warning("Failed to upload to %s", device)
 | 
			
		||||
 | 
			
		||||
    exit_code, _ = upload_program(config, args, devices)
 | 
			
		||||
    if exit_code == 0:
 | 
			
		||||
        _LOGGER.info("Successfully uploaded program.")
 | 
			
		||||
    else:
 | 
			
		||||
        _LOGGER.warning("Failed to upload to %s", devices)
 | 
			
		||||
    return exit_code
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -576,10 +668,7 @@ def command_logs(args: ArgsProtocol, config: ConfigType) -> int | None:
 | 
			
		||||
    devices = choose_upload_log_host(
 | 
			
		||||
        default=args.device,
 | 
			
		||||
        check_default=None,
 | 
			
		||||
        show_ota=False,
 | 
			
		||||
        show_mqtt=True,
 | 
			
		||||
        show_api=True,
 | 
			
		||||
        purpose="logging",
 | 
			
		||||
        purpose=Purpose.LOGGING,
 | 
			
		||||
    )
 | 
			
		||||
    return show_logs(config, args, devices)
 | 
			
		||||
 | 
			
		||||
@@ -605,25 +694,14 @@ def command_run(args: ArgsProtocol, config: ConfigType) -> int | None:
 | 
			
		||||
    devices = choose_upload_log_host(
 | 
			
		||||
        default=args.device,
 | 
			
		||||
        check_default=None,
 | 
			
		||||
        show_ota=True,
 | 
			
		||||
        show_mqtt=False,
 | 
			
		||||
        show_api=True,
 | 
			
		||||
        purpose="uploading",
 | 
			
		||||
        purpose=Purpose.UPLOADING,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    # Try each device for upload until one succeeds
 | 
			
		||||
    successful_device: str | None = None
 | 
			
		||||
    for device in devices:
 | 
			
		||||
        _LOGGER.info("Uploading to %s", device)
 | 
			
		||||
        exit_code = upload_program(config, args, device)
 | 
			
		||||
        if exit_code == 0:
 | 
			
		||||
            _LOGGER.info("Successfully uploaded program.")
 | 
			
		||||
            successful_device = device
 | 
			
		||||
            break
 | 
			
		||||
        if len(devices) > 1:
 | 
			
		||||
            _LOGGER.warning("Failed to upload to %s", device)
 | 
			
		||||
 | 
			
		||||
    if successful_device is None:
 | 
			
		||||
    exit_code, successful_device = upload_program(config, args, devices)
 | 
			
		||||
    if exit_code == 0:
 | 
			
		||||
        _LOGGER.info("Successfully uploaded program.")
 | 
			
		||||
    else:
 | 
			
		||||
        _LOGGER.warning("Failed to upload to %s", devices)
 | 
			
		||||
        return exit_code
 | 
			
		||||
 | 
			
		||||
    if args.no_logs:
 | 
			
		||||
@@ -633,10 +711,7 @@ def command_run(args: ArgsProtocol, config: ConfigType) -> int | None:
 | 
			
		||||
    devices = choose_upload_log_host(
 | 
			
		||||
        default=successful_device,
 | 
			
		||||
        check_default=successful_device,
 | 
			
		||||
        show_ota=False,
 | 
			
		||||
        show_mqtt=True,
 | 
			
		||||
        show_api=True,
 | 
			
		||||
        purpose="logging",
 | 
			
		||||
        purpose=Purpose.LOGGING,
 | 
			
		||||
    )
 | 
			
		||||
    return show_logs(config, args, devices)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -61,11 +61,10 @@ void AbsoluteHumidityComponent::loop() {
 | 
			
		||||
      ESP_LOGW(TAG, "No valid state from temperature sensor!");
 | 
			
		||||
    }
 | 
			
		||||
    if (no_humidity) {
 | 
			
		||||
      ESP_LOGW(TAG, "No valid state from temperature sensor!");
 | 
			
		||||
      ESP_LOGW(TAG, "No valid state from humidity sensor!");
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGW(TAG, "Unable to calculate absolute humidity.");
 | 
			
		||||
    this->publish_state(NAN);
 | 
			
		||||
    this->status_set_warning();
 | 
			
		||||
    this->status_set_warning(LOG_STR("Unable to calculate absolute humidity."));
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -87,9 +86,8 @@ void AbsoluteHumidityComponent::loop() {
 | 
			
		||||
      es = es_wobus(temperature_c);
 | 
			
		||||
      break;
 | 
			
		||||
    default:
 | 
			
		||||
      ESP_LOGE(TAG, "Invalid saturation vapor pressure equation selection!");
 | 
			
		||||
      this->publish_state(NAN);
 | 
			
		||||
      this->status_set_error();
 | 
			
		||||
      this->status_set_error("Invalid saturation vapor pressure equation selection!");
 | 
			
		||||
      return;
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGD(TAG, "Saturation vapor pressure %f kPa", es);
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,7 @@ from esphome.const import (
 | 
			
		||||
    CONF_EQUATION,
 | 
			
		||||
    CONF_HUMIDITY,
 | 
			
		||||
    CONF_TEMPERATURE,
 | 
			
		||||
    ICON_WATER,
 | 
			
		||||
    DEVICE_CLASS_ABSOLUTE_HUMIDITY,
 | 
			
		||||
    STATE_CLASS_MEASUREMENT,
 | 
			
		||||
    UNIT_GRAMS_PER_CUBIC_METER,
 | 
			
		||||
)
 | 
			
		||||
@@ -27,8 +27,8 @@ EQUATION = {
 | 
			
		||||
CONFIG_SCHEMA = (
 | 
			
		||||
    sensor.sensor_schema(
 | 
			
		||||
        unit_of_measurement=UNIT_GRAMS_PER_CUBIC_METER,
 | 
			
		||||
        icon=ICON_WATER,
 | 
			
		||||
        accuracy_decimals=2,
 | 
			
		||||
        device_class=DEVICE_CLASS_ABSOLUTE_HUMIDITY,
 | 
			
		||||
        state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
    )
 | 
			
		||||
    .extend(
 | 
			
		||||
 
 | 
			
		||||
@@ -11,15 +11,8 @@ from esphome.components.esp32.const import (
 | 
			
		||||
    VARIANT_ESP32S2,
 | 
			
		||||
    VARIANT_ESP32S3,
 | 
			
		||||
)
 | 
			
		||||
from esphome.config_helpers import filter_source_files_from_platform
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_ANALOG,
 | 
			
		||||
    CONF_INPUT,
 | 
			
		||||
    CONF_NUMBER,
 | 
			
		||||
    PLATFORM_ESP8266,
 | 
			
		||||
    PlatformFramework,
 | 
			
		||||
)
 | 
			
		||||
from esphome.const import CONF_ANALOG, CONF_INPUT, CONF_NUMBER, PLATFORM_ESP8266
 | 
			
		||||
from esphome.core import CORE
 | 
			
		||||
 | 
			
		||||
CODEOWNERS = ["@esphome/core"]
 | 
			
		||||
@@ -273,21 +266,3 @@ def validate_adc_pin(value):
 | 
			
		||||
        )(value)
 | 
			
		||||
 | 
			
		||||
    raise NotImplementedError
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
FILTER_SOURCE_FILES = filter_source_files_from_platform(
 | 
			
		||||
    {
 | 
			
		||||
        "adc_sensor_esp32.cpp": {
 | 
			
		||||
            PlatformFramework.ESP32_ARDUINO,
 | 
			
		||||
            PlatformFramework.ESP32_IDF,
 | 
			
		||||
        },
 | 
			
		||||
        "adc_sensor_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO},
 | 
			
		||||
        "adc_sensor_rp2040.cpp": {PlatformFramework.RP2040_ARDUINO},
 | 
			
		||||
        "adc_sensor_libretiny.cpp": {
 | 
			
		||||
            PlatformFramework.BK72XX_ARDUINO,
 | 
			
		||||
            PlatformFramework.RTL87XX_ARDUINO,
 | 
			
		||||
            PlatformFramework.LN882X_ARDUINO,
 | 
			
		||||
        },
 | 
			
		||||
        "adc_sensor_zephyr.cpp": {PlatformFramework.NRF52_ZEPHYR},
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
@@ -241,6 +241,8 @@ float ADCSensor::sample_autorange_() {
 | 
			
		||||
    cali_config.bitwidth = ADC_BITWIDTH_DEFAULT;
 | 
			
		||||
 | 
			
		||||
    err = adc_cali_create_scheme_curve_fitting(&cali_config, &handle);
 | 
			
		||||
    ESP_LOGVV(TAG, "Autorange atten=%d: Calibration handle creation %s (err=%d)", atten,
 | 
			
		||||
              (err == ESP_OK) ? "SUCCESS" : "FAILED", err);
 | 
			
		||||
#else
 | 
			
		||||
    adc_cali_line_fitting_config_t cali_config = {
 | 
			
		||||
      .unit_id = this->adc_unit_,
 | 
			
		||||
@@ -251,10 +253,14 @@ float ADCSensor::sample_autorange_() {
 | 
			
		||||
#endif
 | 
			
		||||
    };
 | 
			
		||||
    err = adc_cali_create_scheme_line_fitting(&cali_config, &handle);
 | 
			
		||||
    ESP_LOGVV(TAG, "Autorange atten=%d: Calibration handle creation %s (err=%d)", atten,
 | 
			
		||||
              (err == ESP_OK) ? "SUCCESS" : "FAILED", err);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    int raw;
 | 
			
		||||
    err = adc_oneshot_read(this->adc_handle_, this->channel_, &raw);
 | 
			
		||||
    ESP_LOGVV(TAG, "Autorange atten=%d: Raw ADC read %s, value=%d (err=%d)", atten,
 | 
			
		||||
              (err == ESP_OK) ? "SUCCESS" : "FAILED", raw, err);
 | 
			
		||||
 | 
			
		||||
    if (err != ESP_OK) {
 | 
			
		||||
      ESP_LOGW(TAG, "ADC read failed in autorange with error %d", err);
 | 
			
		||||
@@ -275,8 +281,10 @@ float ADCSensor::sample_autorange_() {
 | 
			
		||||
      err = adc_cali_raw_to_voltage(handle, raw, &voltage_mv);
 | 
			
		||||
      if (err == ESP_OK) {
 | 
			
		||||
        voltage = voltage_mv / 1000.0f;
 | 
			
		||||
        ESP_LOGVV(TAG, "Autorange atten=%d: CALIBRATED - raw=%d -> %dmV -> %.6fV", atten, raw, voltage_mv, voltage);
 | 
			
		||||
      } else {
 | 
			
		||||
        voltage = raw * 3.3f / 4095.0f;
 | 
			
		||||
        ESP_LOGVV(TAG, "Autorange atten=%d: UNCALIBRATED FALLBACK - raw=%d -> %.6fV (3.3V ref)", atten, raw, voltage);
 | 
			
		||||
      }
 | 
			
		||||
      // Clean up calibration handle
 | 
			
		||||
#if USE_ESP32_VARIANT_ESP32C3 || USE_ESP32_VARIANT_ESP32C5 || USE_ESP32_VARIANT_ESP32C6 || \
 | 
			
		||||
@@ -287,6 +295,7 @@ float ADCSensor::sample_autorange_() {
 | 
			
		||||
#endif
 | 
			
		||||
    } else {
 | 
			
		||||
      voltage = raw * 3.3f / 4095.0f;
 | 
			
		||||
      ESP_LOGVV(TAG, "Autorange atten=%d: NO CALIBRATION - raw=%d -> %.6fV (3.3V ref)", atten, raw, voltage);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return {raw, voltage};
 | 
			
		||||
@@ -324,18 +333,32 @@ float ADCSensor::sample_autorange_() {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  const int adc_half = 2048;
 | 
			
		||||
  uint32_t c12 = std::min(raw12, adc_half);
 | 
			
		||||
  uint32_t c6 = adc_half - std::abs(raw6 - adc_half);
 | 
			
		||||
  uint32_t c2 = adc_half - std::abs(raw2 - adc_half);
 | 
			
		||||
  uint32_t c0 = std::min(4095 - raw0, adc_half);
 | 
			
		||||
  uint32_t csum = c12 + c6 + c2 + c0;
 | 
			
		||||
  const uint32_t c12 = std::min(raw12, adc_half);
 | 
			
		||||
 | 
			
		||||
  const int32_t c6_signed = adc_half - std::abs(raw6 - adc_half);
 | 
			
		||||
  const uint32_t c6 = (c6_signed > 0) ? c6_signed : 0;  // Clamp to prevent underflow
 | 
			
		||||
 | 
			
		||||
  const int32_t c2_signed = adc_half - std::abs(raw2 - adc_half);
 | 
			
		||||
  const uint32_t c2 = (c2_signed > 0) ? c2_signed : 0;  // Clamp to prevent underflow
 | 
			
		||||
 | 
			
		||||
  const uint32_t c0 = std::min(4095 - raw0, adc_half);
 | 
			
		||||
  const uint32_t csum = c12 + c6 + c2 + c0;
 | 
			
		||||
 | 
			
		||||
  ESP_LOGVV(TAG, "Autorange summary:");
 | 
			
		||||
  ESP_LOGVV(TAG, "  Raw readings: 12db=%d, 6db=%d, 2.5db=%d, 0db=%d", raw12, raw6, raw2, raw0);
 | 
			
		||||
  ESP_LOGVV(TAG, "  Voltages: 12db=%.6f, 6db=%.6f, 2.5db=%.6f, 0db=%.6f", mv12, mv6, mv2, mv0);
 | 
			
		||||
  ESP_LOGVV(TAG, "  Coefficients: c12=%u, c6=%u, c2=%u, c0=%u, sum=%u", c12, c6, c2, c0, csum);
 | 
			
		||||
 | 
			
		||||
  if (csum == 0) {
 | 
			
		||||
    ESP_LOGE(TAG, "Invalid weight sum in autorange calculation");
 | 
			
		||||
    return NAN;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return (mv12 * c12 + mv6 * c6 + mv2 * c2 + mv0 * c0) / csum;
 | 
			
		||||
  const float final_result = (mv12 * c12 + mv6 * c6 + mv2 * c2 + mv0 * c0) / csum;
 | 
			
		||||
  ESP_LOGV(TAG, "Autorange final: (%.6f*%u + %.6f*%u + %.6f*%u + %.6f*%u)/%u = %.6fV", mv12, c12, mv6, c6, mv2, c2, mv0,
 | 
			
		||||
           c0, csum, final_result);
 | 
			
		||||
 | 
			
		||||
  return final_result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace adc
 | 
			
		||||
 
 | 
			
		||||
@@ -9,6 +9,7 @@ from esphome.components.zephyr import (
 | 
			
		||||
    zephyr_add_prj_conf,
 | 
			
		||||
    zephyr_add_user,
 | 
			
		||||
)
 | 
			
		||||
from esphome.config_helpers import filter_source_files_from_platform
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_ATTENUATION,
 | 
			
		||||
@@ -20,6 +21,7 @@ from esphome.const import (
 | 
			
		||||
    PLATFORM_NRF52,
 | 
			
		||||
    STATE_CLASS_MEASUREMENT,
 | 
			
		||||
    UNIT_VOLT,
 | 
			
		||||
    PlatformFramework,
 | 
			
		||||
)
 | 
			
		||||
from esphome.core import CORE
 | 
			
		||||
 | 
			
		||||
@@ -174,3 +176,21 @@ async def to_code(config):
 | 
			
		||||
}};
 | 
			
		||||
"""
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
FILTER_SOURCE_FILES = filter_source_files_from_platform(
 | 
			
		||||
    {
 | 
			
		||||
        "adc_sensor_esp32.cpp": {
 | 
			
		||||
            PlatformFramework.ESP32_ARDUINO,
 | 
			
		||||
            PlatformFramework.ESP32_IDF,
 | 
			
		||||
        },
 | 
			
		||||
        "adc_sensor_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO},
 | 
			
		||||
        "adc_sensor_rp2040.cpp": {PlatformFramework.RP2040_ARDUINO},
 | 
			
		||||
        "adc_sensor_libretiny.cpp": {
 | 
			
		||||
            PlatformFramework.BK72XX_ARDUINO,
 | 
			
		||||
            PlatformFramework.RTL87XX_ARDUINO,
 | 
			
		||||
            PlatformFramework.LN882X_ARDUINO,
 | 
			
		||||
        },
 | 
			
		||||
        "adc_sensor_zephyr.cpp": {PlatformFramework.NRF52_ZEPHYR},
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
@@ -113,7 +113,7 @@ void ADE7880::update() {
 | 
			
		||||
  if (this->channel_a_ != nullptr) {
 | 
			
		||||
    auto *chan = this->channel_a_;
 | 
			
		||||
    this->update_sensor_from_s24zp_register16_(chan->current, AIRMS, [](float val) { return val / 100000.0f; });
 | 
			
		||||
    this->update_sensor_from_s24zp_register16_(chan->voltage, BVRMS, [](float val) { return val / 10000.0f; });
 | 
			
		||||
    this->update_sensor_from_s24zp_register16_(chan->voltage, AVRMS, [](float val) { return val / 10000.0f; });
 | 
			
		||||
    this->update_sensor_from_s24zp_register16_(chan->active_power, AWATT, [](float val) { return val / 100.0f; });
 | 
			
		||||
    this->update_sensor_from_s24zp_register16_(chan->apparent_power, AVA, [](float val) { return val / 100.0f; });
 | 
			
		||||
    this->update_sensor_from_s16_register16_(chan->power_factor, APF,
 | 
			
		||||
 
 | 
			
		||||
@@ -36,6 +36,7 @@ from esphome.const import (
 | 
			
		||||
    UNIT_WATT,
 | 
			
		||||
    UNIT_WATT_HOURS,
 | 
			
		||||
)
 | 
			
		||||
from esphome.types import ConfigType
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["i2c"]
 | 
			
		||||
 | 
			
		||||
@@ -51,6 +52,20 @@ CONF_POWER_GAIN = "power_gain"
 | 
			
		||||
 | 
			
		||||
CONF_NEUTRAL = "neutral"
 | 
			
		||||
 | 
			
		||||
# Tuple of power channel phases
 | 
			
		||||
POWER_PHASES = (CONF_PHASE_A, CONF_PHASE_B, CONF_PHASE_C)
 | 
			
		||||
 | 
			
		||||
# Tuple of sensor types that can be configured for power channels
 | 
			
		||||
POWER_SENSOR_TYPES = (
 | 
			
		||||
    CONF_CURRENT,
 | 
			
		||||
    CONF_VOLTAGE,
 | 
			
		||||
    CONF_ACTIVE_POWER,
 | 
			
		||||
    CONF_APPARENT_POWER,
 | 
			
		||||
    CONF_POWER_FACTOR,
 | 
			
		||||
    CONF_FORWARD_ACTIVE_ENERGY,
 | 
			
		||||
    CONF_REVERSE_ACTIVE_ENERGY,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
NEUTRAL_CHANNEL_SCHEMA = cv.Schema(
 | 
			
		||||
    {
 | 
			
		||||
        cv.GenerateID(): cv.declare_id(NeutralChannel),
 | 
			
		||||
@@ -150,7 +165,64 @@ POWER_CHANNEL_SCHEMA = cv.Schema(
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = (
 | 
			
		||||
 | 
			
		||||
def prefix_sensor_name(
 | 
			
		||||
    sensor_conf: ConfigType,
 | 
			
		||||
    channel_name: str,
 | 
			
		||||
    channel_config: ConfigType,
 | 
			
		||||
    sensor_type: str,
 | 
			
		||||
) -> None:
 | 
			
		||||
    """Helper to prefix sensor name with channel name.
 | 
			
		||||
 | 
			
		||||
    Args:
 | 
			
		||||
        sensor_conf: The sensor configuration (dict or string)
 | 
			
		||||
        channel_name: The channel name to prefix with
 | 
			
		||||
        channel_config: The channel configuration to update
 | 
			
		||||
        sensor_type: The sensor type key in the channel config
 | 
			
		||||
    """
 | 
			
		||||
    if isinstance(sensor_conf, dict) and CONF_NAME in sensor_conf:
 | 
			
		||||
        sensor_name = sensor_conf[CONF_NAME]
 | 
			
		||||
        if sensor_name and not sensor_name.startswith(channel_name):
 | 
			
		||||
            sensor_conf[CONF_NAME] = f"{channel_name} {sensor_name}"
 | 
			
		||||
    elif isinstance(sensor_conf, str):
 | 
			
		||||
        # Simple value case - convert to dict with prefixed name
 | 
			
		||||
        channel_config[sensor_type] = {CONF_NAME: f"{channel_name} {sensor_conf}"}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def process_channel_sensors(
 | 
			
		||||
    config: ConfigType, channel_key: str, sensor_types: tuple
 | 
			
		||||
) -> None:
 | 
			
		||||
    """Process sensors for a channel and prefix their names.
 | 
			
		||||
 | 
			
		||||
    Args:
 | 
			
		||||
        config: The main configuration
 | 
			
		||||
        channel_key: The channel key (e.g., CONF_PHASE_A, CONF_NEUTRAL)
 | 
			
		||||
        sensor_types: Tuple of sensor types to process for this channel
 | 
			
		||||
    """
 | 
			
		||||
    if not (channel_config := config.get(channel_key)) or not (
 | 
			
		||||
        channel_name := channel_config.get(CONF_NAME)
 | 
			
		||||
    ):
 | 
			
		||||
        return
 | 
			
		||||
 | 
			
		||||
    for sensor_type in sensor_types:
 | 
			
		||||
        if sensor_conf := channel_config.get(sensor_type):
 | 
			
		||||
            prefix_sensor_name(sensor_conf, channel_name, channel_config, sensor_type)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def preprocess_channels(config: ConfigType) -> ConfigType:
 | 
			
		||||
    """Preprocess channel configurations to add channel name prefix to sensor names."""
 | 
			
		||||
    # Process power channels
 | 
			
		||||
    for channel in POWER_PHASES:
 | 
			
		||||
        process_channel_sensors(config, channel, POWER_SENSOR_TYPES)
 | 
			
		||||
 | 
			
		||||
    # Process neutral channel
 | 
			
		||||
    process_channel_sensors(config, CONF_NEUTRAL, (CONF_CURRENT,))
 | 
			
		||||
 | 
			
		||||
    return config
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = cv.All(
 | 
			
		||||
    preprocess_channels,
 | 
			
		||||
    cv.Schema(
 | 
			
		||||
        {
 | 
			
		||||
            cv.GenerateID(): cv.declare_id(ADE7880),
 | 
			
		||||
@@ -167,7 +239,7 @@ CONFIG_SCHEMA = (
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
    .extend(cv.polling_component_schema("60s"))
 | 
			
		||||
    .extend(i2c.i2c_device_schema(0x38))
 | 
			
		||||
    .extend(i2c.i2c_device_schema(0x38)),
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -188,15 +260,7 @@ async def neutral_channel(config):
 | 
			
		||||
async def power_channel(config):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
 | 
			
		||||
    for sensor_type in [
 | 
			
		||||
        CONF_CURRENT,
 | 
			
		||||
        CONF_VOLTAGE,
 | 
			
		||||
        CONF_ACTIVE_POWER,
 | 
			
		||||
        CONF_APPARENT_POWER,
 | 
			
		||||
        CONF_POWER_FACTOR,
 | 
			
		||||
        CONF_FORWARD_ACTIVE_ENERGY,
 | 
			
		||||
        CONF_REVERSE_ACTIVE_ENERGY,
 | 
			
		||||
    ]:
 | 
			
		||||
    for sensor_type in POWER_SENSOR_TYPES:
 | 
			
		||||
        if conf := config.get(sensor_type):
 | 
			
		||||
            sens = await sensor.new_sensor(conf)
 | 
			
		||||
            cg.add(getattr(var, f"set_{sensor_type}")(sens))
 | 
			
		||||
@@ -216,44 +280,6 @@ async def power_channel(config):
 | 
			
		||||
    return var
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def final_validate(config):
 | 
			
		||||
    for channel in [CONF_PHASE_A, CONF_PHASE_B, CONF_PHASE_C]:
 | 
			
		||||
        if channel := config.get(channel):
 | 
			
		||||
            channel_name = channel.get(CONF_NAME)
 | 
			
		||||
 | 
			
		||||
            for sensor_type in [
 | 
			
		||||
                CONF_CURRENT,
 | 
			
		||||
                CONF_VOLTAGE,
 | 
			
		||||
                CONF_ACTIVE_POWER,
 | 
			
		||||
                CONF_APPARENT_POWER,
 | 
			
		||||
                CONF_POWER_FACTOR,
 | 
			
		||||
                CONF_FORWARD_ACTIVE_ENERGY,
 | 
			
		||||
                CONF_REVERSE_ACTIVE_ENERGY,
 | 
			
		||||
            ]:
 | 
			
		||||
                if conf := channel.get(sensor_type):
 | 
			
		||||
                    sensor_name = conf.get(CONF_NAME)
 | 
			
		||||
                    if (
 | 
			
		||||
                        sensor_name
 | 
			
		||||
                        and channel_name
 | 
			
		||||
                        and not sensor_name.startswith(channel_name)
 | 
			
		||||
                    ):
 | 
			
		||||
                        conf[CONF_NAME] = f"{channel_name} {sensor_name}"
 | 
			
		||||
 | 
			
		||||
    if channel := config.get(CONF_NEUTRAL):
 | 
			
		||||
        channel_name = channel.get(CONF_NAME)
 | 
			
		||||
        if conf := channel.get(CONF_CURRENT):
 | 
			
		||||
            sensor_name = conf.get(CONF_NAME)
 | 
			
		||||
            if (
 | 
			
		||||
                sensor_name
 | 
			
		||||
                and channel_name
 | 
			
		||||
                and not sensor_name.startswith(channel_name)
 | 
			
		||||
            ):
 | 
			
		||||
                conf[CONF_NAME] = f"{channel_name} {sensor_name}"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
FINAL_VALIDATE_SCHEMA = final_validate
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
 
 | 
			
		||||
@@ -89,7 +89,7 @@ void AGS10Component::dump_config() {
 | 
			
		||||
bool AGS10Component::new_i2c_address(uint8_t newaddress) {
 | 
			
		||||
  uint8_t rev_newaddress = ~newaddress;
 | 
			
		||||
  std::array<uint8_t, 5> data{newaddress, rev_newaddress, newaddress, rev_newaddress, 0};
 | 
			
		||||
  data[4] = calc_crc8_(data, 4);
 | 
			
		||||
  data[4] = crc8(data.data(), 4, 0xFF, 0x31, true);
 | 
			
		||||
  if (!this->write_bytes(REG_ADDRESS, data)) {
 | 
			
		||||
    this->error_code_ = COMMUNICATION_FAILED;
 | 
			
		||||
    this->status_set_warning();
 | 
			
		||||
@@ -109,7 +109,7 @@ bool AGS10Component::set_zero_point_with_current_resistance() { return this->set
 | 
			
		||||
 | 
			
		||||
bool AGS10Component::set_zero_point_with(uint16_t value) {
 | 
			
		||||
  std::array<uint8_t, 5> data{0x00, 0x0C, (uint8_t) ((value >> 8) & 0xFF), (uint8_t) (value & 0xFF), 0};
 | 
			
		||||
  data[4] = calc_crc8_(data, 4);
 | 
			
		||||
  data[4] = crc8(data.data(), 4, 0xFF, 0x31, true);
 | 
			
		||||
  if (!this->write_bytes(REG_CALIBRATION, data)) {
 | 
			
		||||
    this->error_code_ = COMMUNICATION_FAILED;
 | 
			
		||||
    this->status_set_warning();
 | 
			
		||||
@@ -184,7 +184,7 @@ template<size_t N> optional<std::array<uint8_t, N>> AGS10Component::read_and_che
 | 
			
		||||
  auto res = *data;
 | 
			
		||||
  auto crc_byte = res[len];
 | 
			
		||||
 | 
			
		||||
  if (crc_byte != calc_crc8_(res, len)) {
 | 
			
		||||
  if (crc_byte != crc8(res.data(), len, 0xFF, 0x31, true)) {
 | 
			
		||||
    this->error_code_ = CRC_CHECK_FAILED;
 | 
			
		||||
    ESP_LOGE(TAG, "Reading AGS10 version failed: crc error!");
 | 
			
		||||
    return optional<std::array<uint8_t, N>>();
 | 
			
		||||
@@ -192,20 +192,5 @@ template<size_t N> optional<std::array<uint8_t, N>> AGS10Component::read_and_che
 | 
			
		||||
 | 
			
		||||
  return data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template<size_t N> uint8_t AGS10Component::calc_crc8_(std::array<uint8_t, N> dat, uint8_t num) {
 | 
			
		||||
  uint8_t i, byte1, crc = 0xFF;
 | 
			
		||||
  for (byte1 = 0; byte1 < num; byte1++) {
 | 
			
		||||
    crc ^= (dat[byte1]);
 | 
			
		||||
    for (i = 0; i < 8; i++) {
 | 
			
		||||
      if (crc & 0x80) {
 | 
			
		||||
        crc = (crc << 1) ^ 0x31;
 | 
			
		||||
      } else {
 | 
			
		||||
        crc = (crc << 1);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return crc;
 | 
			
		||||
}
 | 
			
		||||
}  // namespace ags10
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,9 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/i2c/i2c.h"
 | 
			
		||||
#include "esphome/components/sensor/sensor.h"
 | 
			
		||||
#include "esphome/core/automation.h"
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/components/sensor/sensor.h"
 | 
			
		||||
#include "esphome/components/i2c/i2c.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ags10 {
 | 
			
		||||
@@ -99,16 +99,6 @@ class AGS10Component : public PollingComponent, public i2c::I2CDevice {
 | 
			
		||||
   * Read, checks and returns data from the sensor.
 | 
			
		||||
   */
 | 
			
		||||
  template<size_t N> optional<std::array<uint8_t, N>> read_and_check_(uint8_t a_register);
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Calculates CRC8 value.
 | 
			
		||||
   *
 | 
			
		||||
   * CRC8 calculation, initial value: 0xFF, polynomial: 0x31 (x8+ x5+ x4+1)
 | 
			
		||||
   *
 | 
			
		||||
   * @param[in] dat the data buffer
 | 
			
		||||
   * @param num number of bytes in the buffer
 | 
			
		||||
   */
 | 
			
		||||
  template<size_t N> uint8_t calc_crc8_(std::array<uint8_t, N> dat, uint8_t num);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
template<typename... Ts> class AGS10NewI2cAddressAction : public Action<Ts...>, public Parented<AGS10Component> {
 | 
			
		||||
 
 | 
			
		||||
@@ -96,7 +96,7 @@ void AHT10Component::read_data_() {
 | 
			
		||||
    ESP_LOGD(TAG, "Read attempt %d at %ums", this->read_count_, (unsigned) (millis() - this->start_time_));
 | 
			
		||||
  }
 | 
			
		||||
  if (this->read(data, 6) != i2c::ERROR_OK) {
 | 
			
		||||
    this->status_set_warning("Read failed, will retry");
 | 
			
		||||
    this->status_set_warning(LOG_STR("Read failed, will retry"));
 | 
			
		||||
    this->restart_read_();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
@@ -113,7 +113,7 @@ void AHT10Component::read_data_() {
 | 
			
		||||
    } else {
 | 
			
		||||
      ESP_LOGD(TAG, "Invalid humidity, retrying");
 | 
			
		||||
      if (this->write(AHT10_MEASURE_CMD, sizeof(AHT10_MEASURE_CMD)) != i2c::ERROR_OK) {
 | 
			
		||||
        this->status_set_warning(ESP_LOG_MSG_COMM_FAIL);
 | 
			
		||||
        this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
 | 
			
		||||
      }
 | 
			
		||||
      this->restart_read_();
 | 
			
		||||
      return;
 | 
			
		||||
@@ -144,7 +144,7 @@ void AHT10Component::update() {
 | 
			
		||||
    return;
 | 
			
		||||
  this->start_time_ = millis();
 | 
			
		||||
  if (this->write(AHT10_MEASURE_CMD, sizeof(AHT10_MEASURE_CMD)) != i2c::ERROR_OK) {
 | 
			
		||||
    this->status_set_warning(ESP_LOG_MSG_COMM_FAIL);
 | 
			
		||||
    this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  this->restart_read_();
 | 
			
		||||
 
 | 
			
		||||
@@ -18,6 +18,6 @@ CONFIG_SCHEMA = cv.Schema(
 | 
			
		||||
).extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def to_code(config):
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    yield esp32_ble_tracker.register_ble_device(var, config)
 | 
			
		||||
    await esp32_ble_tracker.register_ble_device(var, config)
 | 
			
		||||
 
 | 
			
		||||
@@ -13,7 +13,7 @@ from esphome.const import (
 | 
			
		||||
    CONF_TRIGGER_ID,
 | 
			
		||||
    CONF_WEB_SERVER,
 | 
			
		||||
)
 | 
			
		||||
from esphome.core import CORE, coroutine_with_priority
 | 
			
		||||
from esphome.core import CORE, CoroPriority, coroutine_with_priority
 | 
			
		||||
from esphome.core.entity_helpers import entity_duplicate_validator, setup_entity
 | 
			
		||||
from esphome.cpp_generator import MockObjClass
 | 
			
		||||
 | 
			
		||||
@@ -345,6 +345,6 @@ async def alarm_control_panel_is_armed_to_code(
 | 
			
		||||
    return cg.new_Pvariable(condition_id, template_arg, paren)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@coroutine_with_priority(100.0)
 | 
			
		||||
@coroutine_with_priority(CoroPriority.CORE)
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    cg.add_global(alarm_control_panel_ns.using)
 | 
			
		||||
 
 | 
			
		||||
@@ -29,22 +29,6 @@ namespace am2315c {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "am2315c";
 | 
			
		||||
 | 
			
		||||
uint8_t AM2315C::crc8_(uint8_t *data, uint8_t len) {
 | 
			
		||||
  uint8_t crc = 0xFF;
 | 
			
		||||
  while (len--) {
 | 
			
		||||
    crc ^= *data++;
 | 
			
		||||
    for (uint8_t i = 0; i < 8; i++) {
 | 
			
		||||
      if (crc & 0x80) {
 | 
			
		||||
        crc <<= 1;
 | 
			
		||||
        crc ^= 0x31;
 | 
			
		||||
      } else {
 | 
			
		||||
        crc <<= 1;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return crc;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool AM2315C::reset_register_(uint8_t reg) {
 | 
			
		||||
  //  code based on demo code sent by www.aosong.com
 | 
			
		||||
  //  no further documentation.
 | 
			
		||||
@@ -86,7 +70,7 @@ bool AM2315C::convert_(uint8_t *data, float &humidity, float &temperature) {
 | 
			
		||||
  humidity = raw * 9.5367431640625e-5;
 | 
			
		||||
  raw = ((data[3] & 0x0F) << 16) | (data[4] << 8) | data[5];
 | 
			
		||||
  temperature = raw * 1.9073486328125e-4 - 50;
 | 
			
		||||
  return this->crc8_(data, 6) == data[6];
 | 
			
		||||
  return crc8(data, 6, 0xFF, 0x31, true) == data[6];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void AM2315C::setup() {
 | 
			
		||||
 
 | 
			
		||||
@@ -21,9 +21,9 @@
 | 
			
		||||
// SOFTWARE.
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/components/sensor/sensor.h"
 | 
			
		||||
#include "esphome/components/i2c/i2c.h"
 | 
			
		||||
#include "esphome/components/sensor/sensor.h"
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace am2315c {
 | 
			
		||||
@@ -39,7 +39,6 @@ class AM2315C : public PollingComponent, public i2c::I2CDevice {
 | 
			
		||||
  void set_humidity_sensor(sensor::Sensor *humidity_sensor) { this->humidity_sensor_ = humidity_sensor; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  uint8_t crc8_(uint8_t *data, uint8_t len);
 | 
			
		||||
  bool convert_(uint8_t *data, float &humidity, float &temperature);
 | 
			
		||||
  bool reset_register_(uint8_t reg);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -24,7 +24,7 @@ from esphome.const import (
 | 
			
		||||
    CONF_TRIGGER_ID,
 | 
			
		||||
    CONF_VARIABLES,
 | 
			
		||||
)
 | 
			
		||||
from esphome.core import CORE, coroutine_with_priority
 | 
			
		||||
from esphome.core import CORE, CoroPriority, coroutine_with_priority
 | 
			
		||||
 | 
			
		||||
DOMAIN = "api"
 | 
			
		||||
DEPENDENCIES = ["network"]
 | 
			
		||||
@@ -134,7 +134,7 @@ CONFIG_SCHEMA = cv.All(
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@coroutine_with_priority(40.0)
 | 
			
		||||
@coroutine_with_priority(CoroPriority.WEB)
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
@@ -193,6 +193,7 @@ async def to_code(config):
 | 
			
		||||
        if key := encryption_config.get(CONF_KEY):
 | 
			
		||||
            decoded = base64.b64decode(key)
 | 
			
		||||
            cg.add(var.set_noise_psk(list(decoded)))
 | 
			
		||||
            cg.add_define("USE_API_NOISE_PSK_FROM_YAML")
 | 
			
		||||
        else:
 | 
			
		||||
            # No key provided, but encryption desired
 | 
			
		||||
            # This will allow a plaintext client to provide a noise key,
 | 
			
		||||
@@ -321,6 +322,7 @@ HOMEASSISTANT_TAG_SCANNED_ACTION_SCHEMA = cv.maybe_simple_value(
 | 
			
		||||
    HOMEASSISTANT_TAG_SCANNED_ACTION_SCHEMA,
 | 
			
		||||
)
 | 
			
		||||
async def homeassistant_tag_scanned_to_code(config, action_id, template_arg, args):
 | 
			
		||||
    cg.add_define("USE_API_HOMEASSISTANT_SERVICES")
 | 
			
		||||
    serv = await cg.get_variable(config[CONF_ID])
 | 
			
		||||
    var = cg.new_Pvariable(action_id, template_arg, serv, True)
 | 
			
		||||
    cg.add(var.set_service("esphome.tag_scanned"))
 | 
			
		||||
 
 | 
			
		||||
@@ -27,9 +27,6 @@ service APIConnection {
 | 
			
		||||
  rpc subscribe_logs (SubscribeLogsRequest) returns (void) {}
 | 
			
		||||
  rpc subscribe_homeassistant_services (SubscribeHomeassistantServicesRequest) returns (void) {}
 | 
			
		||||
  rpc subscribe_home_assistant_states (SubscribeHomeAssistantStatesRequest) returns (void) {}
 | 
			
		||||
  rpc get_time (GetTimeRequest) returns (GetTimeResponse) {
 | 
			
		||||
    option (needs_authentication) = false;
 | 
			
		||||
  }
 | 
			
		||||
  rpc execute_service (ExecuteServiceRequest) returns (void) {}
 | 
			
		||||
  rpc noise_encryption_set_key (NoiseEncryptionSetKeyRequest) returns (NoiseEncryptionSetKeyResponse) {}
 | 
			
		||||
 | 
			
		||||
@@ -809,15 +806,16 @@ message HomeAssistantStateResponse {
 | 
			
		||||
// ==================== IMPORT TIME ====================
 | 
			
		||||
message GetTimeRequest {
 | 
			
		||||
  option (id) = 36;
 | 
			
		||||
  option (source) = SOURCE_BOTH;
 | 
			
		||||
  option (source) = SOURCE_SERVER;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
message GetTimeResponse {
 | 
			
		||||
  option (id) = 37;
 | 
			
		||||
  option (source) = SOURCE_BOTH;
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
 | 
			
		||||
  fixed32 epoch_seconds = 1;
 | 
			
		||||
  string timezone = 2;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==================== USER-DEFINES SERVICES ====================
 | 
			
		||||
@@ -1438,11 +1436,11 @@ message BluetoothLERawAdvertisementsResponse {
 | 
			
		||||
  option (ifdef) = "USE_BLUETOOTH_PROXY";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
 | 
			
		||||
  repeated BluetoothLERawAdvertisement advertisements = 1;
 | 
			
		||||
  repeated BluetoothLERawAdvertisement advertisements = 1 [(fixed_array_with_length_define) = "BLUETOOTH_PROXY_ADVERTISEMENT_BATCH_SIZE"];
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
enum BluetoothDeviceRequestType {
 | 
			
		||||
  BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT = 0;
 | 
			
		||||
  BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT = 0 [deprecated = true]; // V1 removed, use V3 variants
 | 
			
		||||
  BLUETOOTH_DEVICE_REQUEST_TYPE_DISCONNECT = 1;
 | 
			
		||||
  BLUETOOTH_DEVICE_REQUEST_TYPE_PAIR = 2;
 | 
			
		||||
  BLUETOOTH_DEVICE_REQUEST_TYPE_UNPAIR = 3;
 | 
			
		||||
@@ -1712,6 +1710,7 @@ message BluetoothScannerStateResponse {
 | 
			
		||||
 | 
			
		||||
  BluetoothScannerState state = 1;
 | 
			
		||||
  BluetoothScannerMode mode = 2;
 | 
			
		||||
  BluetoothScannerMode configured_mode = 3;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
message BluetoothScannerSetModeRequest {
 | 
			
		||||
 
 | 
			
		||||
@@ -42,6 +42,8 @@ static constexpr uint8_t MAX_PING_RETRIES = 60;
 | 
			
		||||
static constexpr uint16_t PING_RETRY_INTERVAL = 1000;
 | 
			
		||||
static constexpr uint32_t KEEPALIVE_DISCONNECT_TIMEOUT = (KEEPALIVE_TIMEOUT_MS * 5) / 2;
 | 
			
		||||
 | 
			
		||||
static constexpr auto ESPHOME_VERSION_REF = StringRef::from_lit(ESPHOME_VERSION);
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "api.connection";
 | 
			
		||||
#ifdef USE_CAMERA
 | 
			
		||||
static const int CAMERA_STOP_STREAM = 5000;
 | 
			
		||||
@@ -112,7 +114,7 @@ void APIConnection::start() {
 | 
			
		||||
  APIError err = this->helper_->init();
 | 
			
		||||
  if (err != APIError::OK) {
 | 
			
		||||
    on_fatal_error();
 | 
			
		||||
    this->log_warning_("Helper init failed", err);
 | 
			
		||||
    this->log_warning_(LOG_STR("Helper init failed"), err);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  this->client_info_.peername = helper_->getpeername();
 | 
			
		||||
@@ -159,7 +161,7 @@ void APIConnection::loop() {
 | 
			
		||||
        break;
 | 
			
		||||
      } else if (err != APIError::OK) {
 | 
			
		||||
        on_fatal_error();
 | 
			
		||||
        this->log_warning_("Reading failed", err);
 | 
			
		||||
        this->log_warning_(LOG_STR("Reading failed"), err);
 | 
			
		||||
        return;
 | 
			
		||||
      } else {
 | 
			
		||||
        this->last_traffic_ = now;
 | 
			
		||||
@@ -289,16 +291,26 @@ uint16_t APIConnection::encode_message_to_buffer(ProtoMessage &msg, uint8_t mess
 | 
			
		||||
    return 0;  // Doesn't fit
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Allocate buffer space - pass payload size, allocation functions add header/footer space
 | 
			
		||||
  ProtoWriteBuffer buffer = is_single ? conn->allocate_single_message_buffer(calculated_size)
 | 
			
		||||
                                      : conn->allocate_batch_message_buffer(calculated_size);
 | 
			
		||||
 | 
			
		||||
  // Get buffer size after allocation (which includes header padding)
 | 
			
		||||
  std::vector<uint8_t> &shared_buf = conn->parent_->get_shared_buffer_ref();
 | 
			
		||||
  size_t size_before_encode = shared_buf.size();
 | 
			
		||||
 | 
			
		||||
  if (is_single || conn->flags_.batch_first_message) {
 | 
			
		||||
    // Single message or first batch message
 | 
			
		||||
    conn->prepare_first_message_buffer(shared_buf, header_padding, total_calculated_size);
 | 
			
		||||
    if (conn->flags_.batch_first_message) {
 | 
			
		||||
      conn->flags_.batch_first_message = false;
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    // Batch message second or later
 | 
			
		||||
    // Add padding for previous message footer + this message header
 | 
			
		||||
    size_t current_size = shared_buf.size();
 | 
			
		||||
    shared_buf.reserve(current_size + total_calculated_size);
 | 
			
		||||
    shared_buf.resize(current_size + footer_size + header_padding);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Encode directly into buffer
 | 
			
		||||
  msg.encode(buffer);
 | 
			
		||||
  size_t size_before_encode = shared_buf.size();
 | 
			
		||||
  msg.encode({&shared_buf});
 | 
			
		||||
 | 
			
		||||
  // Calculate actual encoded size (not including header that was already added)
 | 
			
		||||
  size_t actual_payload_size = shared_buf.size() - size_before_encode;
 | 
			
		||||
@@ -455,9 +467,7 @@ uint16_t APIConnection::try_send_light_state(EntityBase *entity, APIConnection *
 | 
			
		||||
  resp.cold_white = values.get_cold_white();
 | 
			
		||||
  resp.warm_white = values.get_warm_white();
 | 
			
		||||
  if (light->supports_effects()) {
 | 
			
		||||
    // get_effect_name() returns temporary std::string - must store it
 | 
			
		||||
    std::string effect_name = light->get_effect_name();
 | 
			
		||||
    resp.set_effect(StringRef(effect_name));
 | 
			
		||||
    resp.set_effect(light->get_effect_name_ref());
 | 
			
		||||
  }
 | 
			
		||||
  return fill_and_encode_entity_state(light, resp, LightStateResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
 | 
			
		||||
}
 | 
			
		||||
@@ -1062,17 +1072,17 @@ void APIConnection::camera_image(const CameraImageRequest &msg) {
 | 
			
		||||
 | 
			
		||||
#ifdef USE_HOMEASSISTANT_TIME
 | 
			
		||||
void APIConnection::on_get_time_response(const GetTimeResponse &value) {
 | 
			
		||||
  if (homeassistant::global_homeassistant_time != nullptr)
 | 
			
		||||
  if (homeassistant::global_homeassistant_time != nullptr) {
 | 
			
		||||
    homeassistant::global_homeassistant_time->set_epoch_time(value.epoch_seconds);
 | 
			
		||||
#ifdef USE_TIME_TIMEZONE
 | 
			
		||||
    if (!value.timezone.empty() && value.timezone != homeassistant::global_homeassistant_time->get_timezone()) {
 | 
			
		||||
      homeassistant::global_homeassistant_time->set_timezone(value.timezone);
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
bool APIConnection::send_get_time_response(const GetTimeRequest &msg) {
 | 
			
		||||
  GetTimeResponse resp;
 | 
			
		||||
  resp.epoch_seconds = ::time(nullptr);
 | 
			
		||||
  return this->send_message(resp, GetTimeResponse::MESSAGE_TYPE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifdef USE_BLUETOOTH_PROXY
 | 
			
		||||
void APIConnection::subscribe_bluetooth_le_advertisements(const SubscribeBluetoothLEAdvertisementsRequest &msg) {
 | 
			
		||||
  bluetooth_proxy::global_bluetooth_proxy->subscribe_api_connection(this, msg.flags);
 | 
			
		||||
@@ -1362,9 +1372,8 @@ bool APIConnection::send_hello_response(const HelloRequest &msg) {
 | 
			
		||||
  HelloResponse resp;
 | 
			
		||||
  resp.api_version_major = 1;
 | 
			
		||||
  resp.api_version_minor = 12;
 | 
			
		||||
  // Temporary string for concatenation - will be valid during send_message call
 | 
			
		||||
  std::string server_info = App.get_name() + " (esphome v" ESPHOME_VERSION ")";
 | 
			
		||||
  resp.set_server_info(StringRef(server_info));
 | 
			
		||||
  // Send only the version string - the client only logs this for debugging and doesn't use it otherwise
 | 
			
		||||
  resp.set_server_info(ESPHOME_VERSION_REF);
 | 
			
		||||
  resp.set_name(StringRef(App.get_name()));
 | 
			
		||||
 | 
			
		||||
#ifdef USE_API_PASSWORD
 | 
			
		||||
@@ -1411,13 +1420,9 @@ bool APIConnection::send_device_info_response(const DeviceInfoRequest &msg) {
 | 
			
		||||
  std::string mac_address = get_mac_address_pretty();
 | 
			
		||||
  resp.set_mac_address(StringRef(mac_address));
 | 
			
		||||
 | 
			
		||||
  // Compile-time StringRef constants
 | 
			
		||||
  static constexpr auto ESPHOME_VERSION_REF = StringRef::from_lit(ESPHOME_VERSION);
 | 
			
		||||
  resp.set_esphome_version(ESPHOME_VERSION_REF);
 | 
			
		||||
 | 
			
		||||
  // get_compilation_time() returns temporary std::string - must store it
 | 
			
		||||
  std::string compilation_time = App.get_compilation_time();
 | 
			
		||||
  resp.set_compilation_time(StringRef(compilation_time));
 | 
			
		||||
  resp.set_compilation_time(App.get_compilation_time_ref());
 | 
			
		||||
 | 
			
		||||
  // Compile-time StringRef constants for manufacturers
 | 
			
		||||
#if defined(USE_ESP8266) || defined(USE_ESP32)
 | 
			
		||||
@@ -1559,7 +1564,7 @@ bool APIConnection::send_buffer(ProtoWriteBuffer buffer, uint8_t message_type) {
 | 
			
		||||
    return false;
 | 
			
		||||
  if (err != APIError::OK) {
 | 
			
		||||
    on_fatal_error();
 | 
			
		||||
    this->log_warning_("Packet write failed", err);
 | 
			
		||||
    this->log_warning_(LOG_STR("Packet write failed"), err);
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
  // Do not set last_traffic_ on send
 | 
			
		||||
@@ -1620,14 +1625,6 @@ bool APIConnection::schedule_batch_() {
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ProtoWriteBuffer APIConnection::allocate_single_message_buffer(uint16_t size) { return this->create_buffer(size); }
 | 
			
		||||
 | 
			
		||||
ProtoWriteBuffer APIConnection::allocate_batch_message_buffer(uint16_t size) {
 | 
			
		||||
  ProtoWriteBuffer result = this->prepare_message_buffer(size, this->flags_.batch_first_message);
 | 
			
		||||
  this->flags_.batch_first_message = false;
 | 
			
		||||
  return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void APIConnection::process_batch_() {
 | 
			
		||||
  // Ensure PacketInfo remains trivially destructible for our placement new approach
 | 
			
		||||
  static_assert(std::is_trivially_destructible<PacketInfo>::value,
 | 
			
		||||
@@ -1735,7 +1732,7 @@ void APIConnection::process_batch_() {
 | 
			
		||||
    }
 | 
			
		||||
    remaining_size -= payload_size;
 | 
			
		||||
    // Calculate where the next message's header padding will start
 | 
			
		||||
    // Current buffer size + footer space (that prepare_message_buffer will add for this message)
 | 
			
		||||
    // Current buffer size + footer space for this message
 | 
			
		||||
    current_offset = shared_buf.size() + footer_size;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -1754,7 +1751,7 @@ void APIConnection::process_batch_() {
 | 
			
		||||
                                                       std::span<const PacketInfo>(packet_info, packet_count));
 | 
			
		||||
  if (err != APIError::OK && err != APIError::WOULD_BLOCK) {
 | 
			
		||||
    on_fatal_error();
 | 
			
		||||
    this->log_warning_("Batch write failed", err);
 | 
			
		||||
    this->log_warning_(LOG_STR("Batch write failed"), err);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
@@ -1832,11 +1829,14 @@ void APIConnection::process_state_subscriptions_() {
 | 
			
		||||
}
 | 
			
		||||
#endif  // USE_API_HOMEASSISTANT_STATES
 | 
			
		||||
 | 
			
		||||
void APIConnection::log_warning_(const char *message, APIError err) {
 | 
			
		||||
  ESP_LOGW(TAG, "%s: %s %s errno=%d", this->get_client_combined_info().c_str(), message, api_error_to_str(err), errno);
 | 
			
		||||
void APIConnection::log_warning_(const LogString *message, APIError err) {
 | 
			
		||||
  ESP_LOGW(TAG, "%s: %s %s errno=%d", this->get_client_combined_info().c_str(), LOG_STR_ARG(message),
 | 
			
		||||
           LOG_STR_ARG(api_error_to_logstr(err)), errno);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void APIConnection::log_socket_operation_failed_(APIError err) { this->log_warning_("Socket operation failed", err); }
 | 
			
		||||
void APIConnection::log_socket_operation_failed_(APIError err) {
 | 
			
		||||
  this->log_warning_(LOG_STR("Socket operation failed"), err);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace esphome::api
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -44,7 +44,7 @@ static constexpr size_t MAX_PACKETS_PER_BATCH = 64;  // ESP32 has 8KB+ stack, HO
 | 
			
		||||
static constexpr size_t MAX_PACKETS_PER_BATCH = 32;  // ESP8266/RP2040/etc have smaller stacks
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
class APIConnection : public APIServerConnection {
 | 
			
		||||
class APIConnection final : public APIServerConnection {
 | 
			
		||||
 public:
 | 
			
		||||
  friend class APIServer;
 | 
			
		||||
  friend class ListEntitiesIterator;
 | 
			
		||||
@@ -219,7 +219,6 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
#ifdef USE_API_HOMEASSISTANT_STATES
 | 
			
		||||
  void subscribe_home_assistant_states(const SubscribeHomeAssistantStatesRequest &msg) override;
 | 
			
		||||
#endif
 | 
			
		||||
  bool send_get_time_response(const GetTimeRequest &msg) override;
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
  void execute_service(const ExecuteServiceRequest &msg) override;
 | 
			
		||||
#endif
 | 
			
		||||
@@ -252,44 +251,21 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
 | 
			
		||||
    // Get header padding size - used for both reserve and insert
 | 
			
		||||
    uint8_t header_padding = this->helper_->frame_header_padding();
 | 
			
		||||
 | 
			
		||||
    // Get shared buffer from parent server
 | 
			
		||||
    std::vector<uint8_t> &shared_buf = this->parent_->get_shared_buffer_ref();
 | 
			
		||||
    this->prepare_first_message_buffer(shared_buf, header_padding,
 | 
			
		||||
                                       reserve_size + header_padding + this->helper_->frame_footer_size());
 | 
			
		||||
    return {&shared_buf};
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  void prepare_first_message_buffer(std::vector<uint8_t> &shared_buf, size_t header_padding, size_t total_size) {
 | 
			
		||||
    shared_buf.clear();
 | 
			
		||||
    // Reserve space for header padding + message + footer
 | 
			
		||||
    // - Header padding: space for protocol headers (7 bytes for Noise, 6 for Plaintext)
 | 
			
		||||
    // - Footer: space for MAC (16 bytes for Noise, 0 for Plaintext)
 | 
			
		||||
    shared_buf.reserve(reserve_size + header_padding + this->helper_->frame_footer_size());
 | 
			
		||||
    shared_buf.reserve(total_size);
 | 
			
		||||
    // Resize to add header padding so message encoding starts at the correct position
 | 
			
		||||
    shared_buf.resize(header_padding);
 | 
			
		||||
    return {&shared_buf};
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Prepare buffer for next message in batch
 | 
			
		||||
  ProtoWriteBuffer prepare_message_buffer(uint16_t message_size, bool is_first_message) {
 | 
			
		||||
    // Get reference to shared buffer (it maintains state between batch messages)
 | 
			
		||||
    std::vector<uint8_t> &shared_buf = this->parent_->get_shared_buffer_ref();
 | 
			
		||||
 | 
			
		||||
    if (is_first_message) {
 | 
			
		||||
      shared_buf.clear();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    size_t current_size = shared_buf.size();
 | 
			
		||||
 | 
			
		||||
    // Calculate padding to add:
 | 
			
		||||
    // - First message: just header padding
 | 
			
		||||
    // - Subsequent messages: footer for previous message + header padding for this message
 | 
			
		||||
    size_t padding_to_add = is_first_message
 | 
			
		||||
                                ? this->helper_->frame_header_padding()
 | 
			
		||||
                                : this->helper_->frame_header_padding() + this->helper_->frame_footer_size();
 | 
			
		||||
 | 
			
		||||
    // Reserve space for padding + message
 | 
			
		||||
    shared_buf.reserve(current_size + padding_to_add + message_size);
 | 
			
		||||
 | 
			
		||||
    // Resize to add the padding bytes
 | 
			
		||||
    shared_buf.resize(current_size + padding_to_add);
 | 
			
		||||
 | 
			
		||||
    return {&shared_buf};
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  bool try_to_clear_buffer(bool log_out_of_space);
 | 
			
		||||
@@ -297,10 +273,6 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
 | 
			
		||||
  std::string get_client_combined_info() const { return this->client_info_.get_combined_info(); }
 | 
			
		||||
 | 
			
		||||
  // Buffer allocator methods for batch processing
 | 
			
		||||
  ProtoWriteBuffer allocate_single_message_buffer(uint16_t size);
 | 
			
		||||
  ProtoWriteBuffer allocate_batch_message_buffer(uint16_t size);
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  // Helper function to handle authentication completion
 | 
			
		||||
  void complete_authentication_();
 | 
			
		||||
@@ -328,9 +300,17 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
                                              APIConnection *conn, uint32_t remaining_size, bool is_single) {
 | 
			
		||||
    // Set common fields that are shared by all entity types
 | 
			
		||||
    msg.key = entity->get_object_id_hash();
 | 
			
		||||
    // IMPORTANT: get_object_id() may return a temporary std::string
 | 
			
		||||
    std::string object_id = entity->get_object_id();
 | 
			
		||||
    msg.set_object_id(StringRef(object_id));
 | 
			
		||||
    // Try to use static reference first to avoid allocation
 | 
			
		||||
    StringRef static_ref = entity->get_object_id_ref_for_api_();
 | 
			
		||||
    // Store dynamic string outside the if-else to maintain lifetime
 | 
			
		||||
    std::string object_id;
 | 
			
		||||
    if (!static_ref.empty()) {
 | 
			
		||||
      msg.set_object_id(static_ref);
 | 
			
		||||
    } else {
 | 
			
		||||
      // Dynamic case - need to allocate
 | 
			
		||||
      object_id = entity->get_object_id();
 | 
			
		||||
      msg.set_object_id(StringRef(object_id));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (entity->has_own_name()) {
 | 
			
		||||
      msg.set_name(entity->get_name());
 | 
			
		||||
@@ -751,7 +731,7 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Helper function to log API errors with errno
 | 
			
		||||
  void log_warning_(const char *message, APIError err);
 | 
			
		||||
  void log_warning_(const LogString *message, APIError err);
 | 
			
		||||
  // Specific helper for duplicated error message
 | 
			
		||||
  void log_socket_operation_failed_(APIError err);
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
@@ -23,59 +23,59 @@ static const char *const TAG = "api.frame_helper";
 | 
			
		||||
#define LOG_PACKET_SENDING(data, len) ((void) 0)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
const char *api_error_to_str(APIError err) {
 | 
			
		||||
const LogString *api_error_to_logstr(APIError err) {
 | 
			
		||||
  // not using switch to ensure compiler doesn't try to build a big table out of it
 | 
			
		||||
  if (err == APIError::OK) {
 | 
			
		||||
    return "OK";
 | 
			
		||||
    return LOG_STR("OK");
 | 
			
		||||
  } else if (err == APIError::WOULD_BLOCK) {
 | 
			
		||||
    return "WOULD_BLOCK";
 | 
			
		||||
    return LOG_STR("WOULD_BLOCK");
 | 
			
		||||
  } else if (err == APIError::BAD_INDICATOR) {
 | 
			
		||||
    return "BAD_INDICATOR";
 | 
			
		||||
    return LOG_STR("BAD_INDICATOR");
 | 
			
		||||
  } else if (err == APIError::BAD_DATA_PACKET) {
 | 
			
		||||
    return "BAD_DATA_PACKET";
 | 
			
		||||
    return LOG_STR("BAD_DATA_PACKET");
 | 
			
		||||
  } else if (err == APIError::TCP_NODELAY_FAILED) {
 | 
			
		||||
    return "TCP_NODELAY_FAILED";
 | 
			
		||||
    return LOG_STR("TCP_NODELAY_FAILED");
 | 
			
		||||
  } else if (err == APIError::TCP_NONBLOCKING_FAILED) {
 | 
			
		||||
    return "TCP_NONBLOCKING_FAILED";
 | 
			
		||||
    return LOG_STR("TCP_NONBLOCKING_FAILED");
 | 
			
		||||
  } else if (err == APIError::CLOSE_FAILED) {
 | 
			
		||||
    return "CLOSE_FAILED";
 | 
			
		||||
    return LOG_STR("CLOSE_FAILED");
 | 
			
		||||
  } else if (err == APIError::SHUTDOWN_FAILED) {
 | 
			
		||||
    return "SHUTDOWN_FAILED";
 | 
			
		||||
    return LOG_STR("SHUTDOWN_FAILED");
 | 
			
		||||
  } else if (err == APIError::BAD_STATE) {
 | 
			
		||||
    return "BAD_STATE";
 | 
			
		||||
    return LOG_STR("BAD_STATE");
 | 
			
		||||
  } else if (err == APIError::BAD_ARG) {
 | 
			
		||||
    return "BAD_ARG";
 | 
			
		||||
    return LOG_STR("BAD_ARG");
 | 
			
		||||
  } else if (err == APIError::SOCKET_READ_FAILED) {
 | 
			
		||||
    return "SOCKET_READ_FAILED";
 | 
			
		||||
    return LOG_STR("SOCKET_READ_FAILED");
 | 
			
		||||
  } else if (err == APIError::SOCKET_WRITE_FAILED) {
 | 
			
		||||
    return "SOCKET_WRITE_FAILED";
 | 
			
		||||
    return LOG_STR("SOCKET_WRITE_FAILED");
 | 
			
		||||
  } else if (err == APIError::OUT_OF_MEMORY) {
 | 
			
		||||
    return "OUT_OF_MEMORY";
 | 
			
		||||
    return LOG_STR("OUT_OF_MEMORY");
 | 
			
		||||
  } else if (err == APIError::CONNECTION_CLOSED) {
 | 
			
		||||
    return "CONNECTION_CLOSED";
 | 
			
		||||
    return LOG_STR("CONNECTION_CLOSED");
 | 
			
		||||
  }
 | 
			
		||||
#ifdef USE_API_NOISE
 | 
			
		||||
  else if (err == APIError::BAD_HANDSHAKE_PACKET_LEN) {
 | 
			
		||||
    return "BAD_HANDSHAKE_PACKET_LEN";
 | 
			
		||||
    return LOG_STR("BAD_HANDSHAKE_PACKET_LEN");
 | 
			
		||||
  } else if (err == APIError::HANDSHAKESTATE_READ_FAILED) {
 | 
			
		||||
    return "HANDSHAKESTATE_READ_FAILED";
 | 
			
		||||
    return LOG_STR("HANDSHAKESTATE_READ_FAILED");
 | 
			
		||||
  } else if (err == APIError::HANDSHAKESTATE_WRITE_FAILED) {
 | 
			
		||||
    return "HANDSHAKESTATE_WRITE_FAILED";
 | 
			
		||||
    return LOG_STR("HANDSHAKESTATE_WRITE_FAILED");
 | 
			
		||||
  } else if (err == APIError::HANDSHAKESTATE_BAD_STATE) {
 | 
			
		||||
    return "HANDSHAKESTATE_BAD_STATE";
 | 
			
		||||
    return LOG_STR("HANDSHAKESTATE_BAD_STATE");
 | 
			
		||||
  } else if (err == APIError::CIPHERSTATE_DECRYPT_FAILED) {
 | 
			
		||||
    return "CIPHERSTATE_DECRYPT_FAILED";
 | 
			
		||||
    return LOG_STR("CIPHERSTATE_DECRYPT_FAILED");
 | 
			
		||||
  } else if (err == APIError::CIPHERSTATE_ENCRYPT_FAILED) {
 | 
			
		||||
    return "CIPHERSTATE_ENCRYPT_FAILED";
 | 
			
		||||
    return LOG_STR("CIPHERSTATE_ENCRYPT_FAILED");
 | 
			
		||||
  } else if (err == APIError::HANDSHAKESTATE_SETUP_FAILED) {
 | 
			
		||||
    return "HANDSHAKESTATE_SETUP_FAILED";
 | 
			
		||||
    return LOG_STR("HANDSHAKESTATE_SETUP_FAILED");
 | 
			
		||||
  } else if (err == APIError::HANDSHAKESTATE_SPLIT_FAILED) {
 | 
			
		||||
    return "HANDSHAKESTATE_SPLIT_FAILED";
 | 
			
		||||
    return LOG_STR("HANDSHAKESTATE_SPLIT_FAILED");
 | 
			
		||||
  } else if (err == APIError::BAD_HANDSHAKE_ERROR_BYTE) {
 | 
			
		||||
    return "BAD_HANDSHAKE_ERROR_BYTE";
 | 
			
		||||
    return LOG_STR("BAD_HANDSHAKE_ERROR_BYTE");
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
  return "UNKNOWN";
 | 
			
		||||
  return LOG_STR("UNKNOWN");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Default implementation for loop - handles sending buffered data
 | 
			
		||||
@@ -156,7 +156,9 @@ APIError APIFrameHelper::write_raw_(const struct iovec *iov, int iovcnt, uint16_
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Try to send directly if no buffered data
 | 
			
		||||
  ssize_t sent = this->socket_->writev(iov, iovcnt);
 | 
			
		||||
  // Optimize for single iovec case (common for plaintext API)
 | 
			
		||||
  ssize_t sent =
 | 
			
		||||
      (iovcnt == 1) ? this->socket_->write(iov[0].iov_base, iov[0].iov_len) : this->socket_->writev(iov, iovcnt);
 | 
			
		||||
 | 
			
		||||
  if (sent == -1) {
 | 
			
		||||
    APIError err = this->handle_socket_write_error_();
 | 
			
		||||
 
 | 
			
		||||
@@ -66,7 +66,7 @@ enum class APIError : uint16_t {
 | 
			
		||||
#endif
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
const char *api_error_to_str(APIError err);
 | 
			
		||||
const LogString *api_error_to_logstr(APIError err);
 | 
			
		||||
 | 
			
		||||
class APIFrameHelper {
 | 
			
		||||
 public:
 | 
			
		||||
@@ -104,9 +104,9 @@ class APIFrameHelper {
 | 
			
		||||
  // The buffer contains all messages with appropriate padding before each
 | 
			
		||||
  virtual APIError write_protobuf_packets(ProtoWriteBuffer buffer, std::span<const PacketInfo> packets) = 0;
 | 
			
		||||
  // Get the frame header padding required by this protocol
 | 
			
		||||
  virtual uint8_t frame_header_padding() = 0;
 | 
			
		||||
  uint8_t frame_header_padding() const { return frame_header_padding_; }
 | 
			
		||||
  // Get the frame footer size required by this protocol
 | 
			
		||||
  virtual uint8_t frame_footer_size() = 0;
 | 
			
		||||
  uint8_t frame_footer_size() const { return frame_footer_size_; }
 | 
			
		||||
  // Check if socket has data ready to read
 | 
			
		||||
  bool is_socket_ready() const { return socket_ != nullptr && socket_->ready(); }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -10,10 +10,18 @@
 | 
			
		||||
#include <cstring>
 | 
			
		||||
#include <cinttypes>
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP8266
 | 
			
		||||
#include <pgmspace.h>
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
namespace esphome::api {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "api.noise";
 | 
			
		||||
#ifdef USE_ESP8266
 | 
			
		||||
static const char PROLOGUE_INIT[] PROGMEM = "NoiseAPIInit";
 | 
			
		||||
#else
 | 
			
		||||
static const char *const PROLOGUE_INIT = "NoiseAPIInit";
 | 
			
		||||
#endif
 | 
			
		||||
static constexpr size_t PROLOGUE_INIT_LEN = 12;  // strlen("NoiseAPIInit")
 | 
			
		||||
 | 
			
		||||
#define HELPER_LOG(msg, ...) ESP_LOGVV(TAG, "%s: " msg, this->client_info_->get_combined_info().c_str(), ##__VA_ARGS__)
 | 
			
		||||
@@ -27,42 +35,42 @@ static constexpr size_t PROLOGUE_INIT_LEN = 12;  // strlen("NoiseAPIInit")
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/// Convert a noise error code to a readable error
 | 
			
		||||
std::string noise_err_to_str(int err) {
 | 
			
		||||
const LogString *noise_err_to_logstr(int err) {
 | 
			
		||||
  if (err == NOISE_ERROR_NO_MEMORY)
 | 
			
		||||
    return "NO_MEMORY";
 | 
			
		||||
    return LOG_STR("NO_MEMORY");
 | 
			
		||||
  if (err == NOISE_ERROR_UNKNOWN_ID)
 | 
			
		||||
    return "UNKNOWN_ID";
 | 
			
		||||
    return LOG_STR("UNKNOWN_ID");
 | 
			
		||||
  if (err == NOISE_ERROR_UNKNOWN_NAME)
 | 
			
		||||
    return "UNKNOWN_NAME";
 | 
			
		||||
    return LOG_STR("UNKNOWN_NAME");
 | 
			
		||||
  if (err == NOISE_ERROR_MAC_FAILURE)
 | 
			
		||||
    return "MAC_FAILURE";
 | 
			
		||||
    return LOG_STR("MAC_FAILURE");
 | 
			
		||||
  if (err == NOISE_ERROR_NOT_APPLICABLE)
 | 
			
		||||
    return "NOT_APPLICABLE";
 | 
			
		||||
    return LOG_STR("NOT_APPLICABLE");
 | 
			
		||||
  if (err == NOISE_ERROR_SYSTEM)
 | 
			
		||||
    return "SYSTEM";
 | 
			
		||||
    return LOG_STR("SYSTEM");
 | 
			
		||||
  if (err == NOISE_ERROR_REMOTE_KEY_REQUIRED)
 | 
			
		||||
    return "REMOTE_KEY_REQUIRED";
 | 
			
		||||
    return LOG_STR("REMOTE_KEY_REQUIRED");
 | 
			
		||||
  if (err == NOISE_ERROR_LOCAL_KEY_REQUIRED)
 | 
			
		||||
    return "LOCAL_KEY_REQUIRED";
 | 
			
		||||
    return LOG_STR("LOCAL_KEY_REQUIRED");
 | 
			
		||||
  if (err == NOISE_ERROR_PSK_REQUIRED)
 | 
			
		||||
    return "PSK_REQUIRED";
 | 
			
		||||
    return LOG_STR("PSK_REQUIRED");
 | 
			
		||||
  if (err == NOISE_ERROR_INVALID_LENGTH)
 | 
			
		||||
    return "INVALID_LENGTH";
 | 
			
		||||
    return LOG_STR("INVALID_LENGTH");
 | 
			
		||||
  if (err == NOISE_ERROR_INVALID_PARAM)
 | 
			
		||||
    return "INVALID_PARAM";
 | 
			
		||||
    return LOG_STR("INVALID_PARAM");
 | 
			
		||||
  if (err == NOISE_ERROR_INVALID_STATE)
 | 
			
		||||
    return "INVALID_STATE";
 | 
			
		||||
    return LOG_STR("INVALID_STATE");
 | 
			
		||||
  if (err == NOISE_ERROR_INVALID_NONCE)
 | 
			
		||||
    return "INVALID_NONCE";
 | 
			
		||||
    return LOG_STR("INVALID_NONCE");
 | 
			
		||||
  if (err == NOISE_ERROR_INVALID_PRIVATE_KEY)
 | 
			
		||||
    return "INVALID_PRIVATE_KEY";
 | 
			
		||||
    return LOG_STR("INVALID_PRIVATE_KEY");
 | 
			
		||||
  if (err == NOISE_ERROR_INVALID_PUBLIC_KEY)
 | 
			
		||||
    return "INVALID_PUBLIC_KEY";
 | 
			
		||||
    return LOG_STR("INVALID_PUBLIC_KEY");
 | 
			
		||||
  if (err == NOISE_ERROR_INVALID_FORMAT)
 | 
			
		||||
    return "INVALID_FORMAT";
 | 
			
		||||
    return LOG_STR("INVALID_FORMAT");
 | 
			
		||||
  if (err == NOISE_ERROR_INVALID_SIGNATURE)
 | 
			
		||||
    return "INVALID_SIGNATURE";
 | 
			
		||||
  return to_string(err);
 | 
			
		||||
    return LOG_STR("INVALID_SIGNATURE");
 | 
			
		||||
  return LOG_STR("UNKNOWN");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// Initialize the frame helper, returns OK if successful.
 | 
			
		||||
@@ -75,7 +83,11 @@ APIError APINoiseFrameHelper::init() {
 | 
			
		||||
  // init prologue
 | 
			
		||||
  size_t old_size = prologue_.size();
 | 
			
		||||
  prologue_.resize(old_size + PROLOGUE_INIT_LEN);
 | 
			
		||||
#ifdef USE_ESP8266
 | 
			
		||||
  memcpy_P(prologue_.data() + old_size, PROLOGUE_INIT, PROLOGUE_INIT_LEN);
 | 
			
		||||
#else
 | 
			
		||||
  std::memcpy(prologue_.data() + old_size, PROLOGUE_INIT, PROLOGUE_INIT_LEN);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  state_ = State::CLIENT_HELLO;
 | 
			
		||||
  return APIError::OK;
 | 
			
		||||
@@ -83,18 +95,18 @@ APIError APINoiseFrameHelper::init() {
 | 
			
		||||
// Helper for handling handshake frame errors
 | 
			
		||||
APIError APINoiseFrameHelper::handle_handshake_frame_error_(APIError aerr) {
 | 
			
		||||
  if (aerr == APIError::BAD_INDICATOR) {
 | 
			
		||||
    send_explicit_handshake_reject_("Bad indicator byte");
 | 
			
		||||
    send_explicit_handshake_reject_(LOG_STR("Bad indicator byte"));
 | 
			
		||||
  } else if (aerr == APIError::BAD_HANDSHAKE_PACKET_LEN) {
 | 
			
		||||
    send_explicit_handshake_reject_("Bad handshake packet len");
 | 
			
		||||
    send_explicit_handshake_reject_(LOG_STR("Bad handshake packet len"));
 | 
			
		||||
  }
 | 
			
		||||
  return aerr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Helper for handling noise library errors
 | 
			
		||||
APIError APINoiseFrameHelper::handle_noise_error_(int err, const char *func_name, APIError api_err) {
 | 
			
		||||
APIError APINoiseFrameHelper::handle_noise_error_(int err, const LogString *func_name, APIError api_err) {
 | 
			
		||||
  if (err != 0) {
 | 
			
		||||
    state_ = State::FAILED;
 | 
			
		||||
    HELPER_LOG("%s failed: %s", func_name, noise_err_to_str(err).c_str());
 | 
			
		||||
    HELPER_LOG("%s failed: %s", LOG_STR_ARG(func_name), LOG_STR_ARG(noise_err_to_logstr(err)));
 | 
			
		||||
    return api_err;
 | 
			
		||||
  }
 | 
			
		||||
  return APIError::OK;
 | 
			
		||||
@@ -279,11 +291,11 @@ APIError APINoiseFrameHelper::state_action_() {
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      if (frame.empty()) {
 | 
			
		||||
        send_explicit_handshake_reject_("Empty handshake message");
 | 
			
		||||
        send_explicit_handshake_reject_(LOG_STR("Empty handshake message"));
 | 
			
		||||
        return APIError::BAD_HANDSHAKE_ERROR_BYTE;
 | 
			
		||||
      } else if (frame[0] != 0x00) {
 | 
			
		||||
        HELPER_LOG("Bad handshake error byte: %u", frame[0]);
 | 
			
		||||
        send_explicit_handshake_reject_("Bad handshake error byte");
 | 
			
		||||
        send_explicit_handshake_reject_(LOG_STR("Bad handshake error byte"));
 | 
			
		||||
        return APIError::BAD_HANDSHAKE_ERROR_BYTE;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
@@ -293,8 +305,10 @@ APIError APINoiseFrameHelper::state_action_() {
 | 
			
		||||
      err = noise_handshakestate_read_message(handshake_, &mbuf, nullptr);
 | 
			
		||||
      if (err != 0) {
 | 
			
		||||
        // Special handling for MAC failure
 | 
			
		||||
        send_explicit_handshake_reject_(err == NOISE_ERROR_MAC_FAILURE ? "Handshake MAC failure" : "Handshake error");
 | 
			
		||||
        return handle_noise_error_(err, "noise_handshakestate_read_message", APIError::HANDSHAKESTATE_READ_FAILED);
 | 
			
		||||
        send_explicit_handshake_reject_(err == NOISE_ERROR_MAC_FAILURE ? LOG_STR("Handshake MAC failure")
 | 
			
		||||
                                                                       : LOG_STR("Handshake error"));
 | 
			
		||||
        return handle_noise_error_(err, LOG_STR("noise_handshakestate_read_message"),
 | 
			
		||||
                                   APIError::HANDSHAKESTATE_READ_FAILED);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      aerr = check_handshake_finished_();
 | 
			
		||||
@@ -307,8 +321,8 @@ APIError APINoiseFrameHelper::state_action_() {
 | 
			
		||||
      noise_buffer_set_output(mbuf, buffer + 1, sizeof(buffer) - 1);
 | 
			
		||||
 | 
			
		||||
      err = noise_handshakestate_write_message(handshake_, &mbuf, nullptr);
 | 
			
		||||
      APIError aerr_write =
 | 
			
		||||
          handle_noise_error_(err, "noise_handshakestate_write_message", APIError::HANDSHAKESTATE_WRITE_FAILED);
 | 
			
		||||
      APIError aerr_write = handle_noise_error_(err, LOG_STR("noise_handshakestate_write_message"),
 | 
			
		||||
                                                APIError::HANDSHAKESTATE_WRITE_FAILED);
 | 
			
		||||
      if (aerr_write != APIError::OK)
 | 
			
		||||
        return aerr_write;
 | 
			
		||||
      buffer[0] = 0x00;  // success
 | 
			
		||||
@@ -331,15 +345,31 @@ APIError APINoiseFrameHelper::state_action_() {
 | 
			
		||||
  }
 | 
			
		||||
  return APIError::OK;
 | 
			
		||||
}
 | 
			
		||||
void APINoiseFrameHelper::send_explicit_handshake_reject_(const std::string &reason) {
 | 
			
		||||
void APINoiseFrameHelper::send_explicit_handshake_reject_(const LogString *reason) {
 | 
			
		||||
#ifdef USE_STORE_LOG_STR_IN_FLASH
 | 
			
		||||
  // On ESP8266 with flash strings, we need to use PROGMEM-aware functions
 | 
			
		||||
  size_t reason_len = strlen_P(reinterpret_cast<PGM_P>(reason));
 | 
			
		||||
  std::vector<uint8_t> data;
 | 
			
		||||
  data.resize(reason.length() + 1);
 | 
			
		||||
  data.resize(reason_len + 1);
 | 
			
		||||
  data[0] = 0x01;  // failure
 | 
			
		||||
 | 
			
		||||
  // Copy error message from PROGMEM
 | 
			
		||||
  if (reason_len > 0) {
 | 
			
		||||
    memcpy_P(data.data() + 1, reinterpret_cast<PGM_P>(reason), reason_len);
 | 
			
		||||
  }
 | 
			
		||||
#else
 | 
			
		||||
  // Normal memory access
 | 
			
		||||
  const char *reason_str = LOG_STR_ARG(reason);
 | 
			
		||||
  size_t reason_len = strlen(reason_str);
 | 
			
		||||
  std::vector<uint8_t> data;
 | 
			
		||||
  data.resize(reason_len + 1);
 | 
			
		||||
  data[0] = 0x01;  // failure
 | 
			
		||||
 | 
			
		||||
  // Copy error message in bulk
 | 
			
		||||
  if (!reason.empty()) {
 | 
			
		||||
    std::memcpy(data.data() + 1, reason.c_str(), reason.length());
 | 
			
		||||
  if (reason_len > 0) {
 | 
			
		||||
    std::memcpy(data.data() + 1, reason_str, reason_len);
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  // temporarily remove failed state
 | 
			
		||||
  auto orig_state = state_;
 | 
			
		||||
@@ -368,7 +398,8 @@ APIError APINoiseFrameHelper::read_packet(ReadPacketBuffer *buffer) {
 | 
			
		||||
  noise_buffer_init(mbuf);
 | 
			
		||||
  noise_buffer_set_inout(mbuf, frame.data(), frame.size(), frame.size());
 | 
			
		||||
  err = noise_cipherstate_decrypt(recv_cipher_, &mbuf);
 | 
			
		||||
  APIError decrypt_err = handle_noise_error_(err, "noise_cipherstate_decrypt", APIError::CIPHERSTATE_DECRYPT_FAILED);
 | 
			
		||||
  APIError decrypt_err =
 | 
			
		||||
      handle_noise_error_(err, LOG_STR("noise_cipherstate_decrypt"), APIError::CIPHERSTATE_DECRYPT_FAILED);
 | 
			
		||||
  if (decrypt_err != APIError::OK)
 | 
			
		||||
    return decrypt_err;
 | 
			
		||||
 | 
			
		||||
@@ -450,7 +481,8 @@ APIError APINoiseFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer, st
 | 
			
		||||
                           4 + packet.payload_size + frame_footer_size_);
 | 
			
		||||
 | 
			
		||||
    int err = noise_cipherstate_encrypt(send_cipher_, &mbuf);
 | 
			
		||||
    APIError aerr = handle_noise_error_(err, "noise_cipherstate_encrypt", APIError::CIPHERSTATE_ENCRYPT_FAILED);
 | 
			
		||||
    APIError aerr =
 | 
			
		||||
        handle_noise_error_(err, LOG_STR("noise_cipherstate_encrypt"), APIError::CIPHERSTATE_ENCRYPT_FAILED);
 | 
			
		||||
    if (aerr != APIError::OK)
 | 
			
		||||
      return aerr;
 | 
			
		||||
 | 
			
		||||
@@ -504,25 +536,27 @@ APIError APINoiseFrameHelper::init_handshake_() {
 | 
			
		||||
  nid_.modifier_ids[0] = NOISE_MODIFIER_PSK0;
 | 
			
		||||
 | 
			
		||||
  err = noise_handshakestate_new_by_id(&handshake_, &nid_, NOISE_ROLE_RESPONDER);
 | 
			
		||||
  APIError aerr = handle_noise_error_(err, "noise_handshakestate_new_by_id", APIError::HANDSHAKESTATE_SETUP_FAILED);
 | 
			
		||||
  APIError aerr =
 | 
			
		||||
      handle_noise_error_(err, LOG_STR("noise_handshakestate_new_by_id"), APIError::HANDSHAKESTATE_SETUP_FAILED);
 | 
			
		||||
  if (aerr != APIError::OK)
 | 
			
		||||
    return aerr;
 | 
			
		||||
 | 
			
		||||
  const auto &psk = ctx_->get_psk();
 | 
			
		||||
  err = noise_handshakestate_set_pre_shared_key(handshake_, psk.data(), psk.size());
 | 
			
		||||
  aerr = handle_noise_error_(err, "noise_handshakestate_set_pre_shared_key", APIError::HANDSHAKESTATE_SETUP_FAILED);
 | 
			
		||||
  aerr = handle_noise_error_(err, LOG_STR("noise_handshakestate_set_pre_shared_key"),
 | 
			
		||||
                             APIError::HANDSHAKESTATE_SETUP_FAILED);
 | 
			
		||||
  if (aerr != APIError::OK)
 | 
			
		||||
    return aerr;
 | 
			
		||||
 | 
			
		||||
  err = noise_handshakestate_set_prologue(handshake_, prologue_.data(), prologue_.size());
 | 
			
		||||
  aerr = handle_noise_error_(err, "noise_handshakestate_set_prologue", APIError::HANDSHAKESTATE_SETUP_FAILED);
 | 
			
		||||
  aerr = handle_noise_error_(err, LOG_STR("noise_handshakestate_set_prologue"), APIError::HANDSHAKESTATE_SETUP_FAILED);
 | 
			
		||||
  if (aerr != APIError::OK)
 | 
			
		||||
    return aerr;
 | 
			
		||||
  // set_prologue copies it into handshakestate, so we can get rid of it now
 | 
			
		||||
  prologue_ = {};
 | 
			
		||||
 | 
			
		||||
  err = noise_handshakestate_start(handshake_);
 | 
			
		||||
  aerr = handle_noise_error_(err, "noise_handshakestate_start", APIError::HANDSHAKESTATE_SETUP_FAILED);
 | 
			
		||||
  aerr = handle_noise_error_(err, LOG_STR("noise_handshakestate_start"), APIError::HANDSHAKESTATE_SETUP_FAILED);
 | 
			
		||||
  if (aerr != APIError::OK)
 | 
			
		||||
    return aerr;
 | 
			
		||||
  return APIError::OK;
 | 
			
		||||
@@ -540,7 +574,8 @@ APIError APINoiseFrameHelper::check_handshake_finished_() {
 | 
			
		||||
    return APIError::HANDSHAKESTATE_BAD_STATE;
 | 
			
		||||
  }
 | 
			
		||||
  int err = noise_handshakestate_split(handshake_, &send_cipher_, &recv_cipher_);
 | 
			
		||||
  APIError aerr = handle_noise_error_(err, "noise_handshakestate_split", APIError::HANDSHAKESTATE_SPLIT_FAILED);
 | 
			
		||||
  APIError aerr =
 | 
			
		||||
      handle_noise_error_(err, LOG_STR("noise_handshakestate_split"), APIError::HANDSHAKESTATE_SPLIT_FAILED);
 | 
			
		||||
  if (aerr != APIError::OK)
 | 
			
		||||
    return aerr;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -7,7 +7,7 @@
 | 
			
		||||
 | 
			
		||||
namespace esphome::api {
 | 
			
		||||
 | 
			
		||||
class APINoiseFrameHelper : public APIFrameHelper {
 | 
			
		||||
class APINoiseFrameHelper final : public APIFrameHelper {
 | 
			
		||||
 public:
 | 
			
		||||
  APINoiseFrameHelper(std::unique_ptr<socket::Socket> socket, std::shared_ptr<APINoiseContext> ctx,
 | 
			
		||||
                      const ClientInfo *client_info)
 | 
			
		||||
@@ -25,10 +25,6 @@ class APINoiseFrameHelper : public APIFrameHelper {
 | 
			
		||||
  APIError read_packet(ReadPacketBuffer *buffer) override;
 | 
			
		||||
  APIError write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) override;
 | 
			
		||||
  APIError write_protobuf_packets(ProtoWriteBuffer buffer, std::span<const PacketInfo> packets) override;
 | 
			
		||||
  // Get the frame header padding required by this protocol
 | 
			
		||||
  uint8_t frame_header_padding() override { return frame_header_padding_; }
 | 
			
		||||
  // Get the frame footer size required by this protocol
 | 
			
		||||
  uint8_t frame_footer_size() override { return frame_footer_size_; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  APIError state_action_();
 | 
			
		||||
@@ -36,9 +32,9 @@ class APINoiseFrameHelper : public APIFrameHelper {
 | 
			
		||||
  APIError write_frame_(const uint8_t *data, uint16_t len);
 | 
			
		||||
  APIError init_handshake_();
 | 
			
		||||
  APIError check_handshake_finished_();
 | 
			
		||||
  void send_explicit_handshake_reject_(const std::string &reason);
 | 
			
		||||
  void send_explicit_handshake_reject_(const LogString *reason);
 | 
			
		||||
  APIError handle_handshake_frame_error_(APIError aerr);
 | 
			
		||||
  APIError handle_noise_error_(int err, const char *func_name, APIError api_err);
 | 
			
		||||
  APIError handle_noise_error_(int err, const LogString *func_name, APIError api_err);
 | 
			
		||||
 | 
			
		||||
  // Pointers first (4 bytes each)
 | 
			
		||||
  NoiseHandshakeState *handshake_{nullptr};
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,10 @@
 | 
			
		||||
#include <cstring>
 | 
			
		||||
#include <cinttypes>
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP8266
 | 
			
		||||
#include <pgmspace.h>
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
namespace esphome::api {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "api.plaintext";
 | 
			
		||||
@@ -197,11 +201,20 @@ APIError APIPlaintextFrameHelper::read_packet(ReadPacketBuffer *buffer) {
 | 
			
		||||
      // We must send at least 3 bytes to be read, so we add
 | 
			
		||||
      // a message after the indicator byte to ensures its long
 | 
			
		||||
      // enough and can aid in debugging.
 | 
			
		||||
      const char msg[] = "\x00"
 | 
			
		||||
                         "Bad indicator byte";
 | 
			
		||||
      static constexpr uint8_t INDICATOR_MSG_SIZE = 19;
 | 
			
		||||
#ifdef USE_ESP8266
 | 
			
		||||
      static const char MSG_PROGMEM[] PROGMEM = "\x00"
 | 
			
		||||
                                                "Bad indicator byte";
 | 
			
		||||
      char msg[INDICATOR_MSG_SIZE];
 | 
			
		||||
      memcpy_P(msg, MSG_PROGMEM, INDICATOR_MSG_SIZE);
 | 
			
		||||
      iov[0].iov_base = (void *) msg;
 | 
			
		||||
      iov[0].iov_len = 19;
 | 
			
		||||
      this->write_raw_(iov, 1, 19);
 | 
			
		||||
#else
 | 
			
		||||
      static const char MSG[] = "\x00"
 | 
			
		||||
                                "Bad indicator byte";
 | 
			
		||||
      iov[0].iov_base = (void *) MSG;
 | 
			
		||||
#endif
 | 
			
		||||
      iov[0].iov_len = INDICATOR_MSG_SIZE;
 | 
			
		||||
      this->write_raw_(iov, 1, INDICATOR_MSG_SIZE);
 | 
			
		||||
    }
 | 
			
		||||
    return aerr;
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,7 @@
 | 
			
		||||
 | 
			
		||||
namespace esphome::api {
 | 
			
		||||
 | 
			
		||||
class APIPlaintextFrameHelper : public APIFrameHelper {
 | 
			
		||||
class APIPlaintextFrameHelper final : public APIFrameHelper {
 | 
			
		||||
 public:
 | 
			
		||||
  APIPlaintextFrameHelper(std::unique_ptr<socket::Socket> socket, const ClientInfo *client_info)
 | 
			
		||||
      : APIFrameHelper(std::move(socket), client_info) {
 | 
			
		||||
@@ -22,9 +22,6 @@ class APIPlaintextFrameHelper : public APIFrameHelper {
 | 
			
		||||
  APIError read_packet(ReadPacketBuffer *buffer) override;
 | 
			
		||||
  APIError write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) override;
 | 
			
		||||
  APIError write_protobuf_packets(ProtoWriteBuffer buffer, std::span<const PacketInfo> packets) override;
 | 
			
		||||
  uint8_t frame_header_padding() override { return frame_header_padding_; }
 | 
			
		||||
  // Get the frame footer size required by this protocol
 | 
			
		||||
  uint8_t frame_footer_size() override { return frame_footer_size_; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  APIError try_read_frame_(std::vector<uint8_t> *frame);
 | 
			
		||||
 
 | 
			
		||||
@@ -30,6 +30,7 @@ extend google.protobuf.FieldOptions {
 | 
			
		||||
    optional bool no_zero_copy = 50008 [default=false];
 | 
			
		||||
    optional bool fixed_array_skip_zero = 50009 [default=false];
 | 
			
		||||
    optional string fixed_array_size_define = 50010;
 | 
			
		||||
    optional string fixed_array_with_length_define = 50011;
 | 
			
		||||
 | 
			
		||||
    // container_pointer: Zero-copy optimization for repeated fields.
 | 
			
		||||
    //
 | 
			
		||||
 
 | 
			
		||||
@@ -901,6 +901,16 @@ bool HomeAssistantStateResponse::decode_length(uint32_t field_id, ProtoLengthDel
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
bool GetTimeResponse::decode_length(uint32_t field_id, ProtoLengthDelimited value) {
 | 
			
		||||
  switch (field_id) {
 | 
			
		||||
    case 2:
 | 
			
		||||
      this->timezone = value.as_string();
 | 
			
		||||
      break;
 | 
			
		||||
    default:
 | 
			
		||||
      return false;
 | 
			
		||||
  }
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
bool GetTimeResponse::decode_32bit(uint32_t field_id, Proto32Bit value) {
 | 
			
		||||
  switch (field_id) {
 | 
			
		||||
    case 1:
 | 
			
		||||
@@ -911,8 +921,6 @@ bool GetTimeResponse::decode_32bit(uint32_t field_id, Proto32Bit value) {
 | 
			
		||||
  }
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
void GetTimeResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_fixed32(1, this->epoch_seconds); }
 | 
			
		||||
void GetTimeResponse::calculate_size(ProtoSize &size) const { size.add_fixed32(1, this->epoch_seconds); }
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
void ListEntitiesServicesArgument::encode(ProtoWriteBuffer buffer) const {
 | 
			
		||||
  buffer.encode_string(1, this->name_ref_);
 | 
			
		||||
@@ -1843,12 +1851,14 @@ void BluetoothLERawAdvertisement::calculate_size(ProtoSize &size) const {
 | 
			
		||||
  size.add_length(1, this->data_len);
 | 
			
		||||
}
 | 
			
		||||
void BluetoothLERawAdvertisementsResponse::encode(ProtoWriteBuffer buffer) const {
 | 
			
		||||
  for (auto &it : this->advertisements) {
 | 
			
		||||
    buffer.encode_message(1, it, true);
 | 
			
		||||
  for (uint16_t i = 0; i < this->advertisements_len; i++) {
 | 
			
		||||
    buffer.encode_message(1, this->advertisements[i], true);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
void BluetoothLERawAdvertisementsResponse::calculate_size(ProtoSize &size) const {
 | 
			
		||||
  size.add_repeated_message(1, this->advertisements);
 | 
			
		||||
  for (uint16_t i = 0; i < this->advertisements_len; i++) {
 | 
			
		||||
    size.add_message_object_force(1, this->advertisements[i]);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
bool BluetoothDeviceRequest::decode_varint(uint32_t field_id, ProtoVarInt value) {
 | 
			
		||||
  switch (field_id) {
 | 
			
		||||
@@ -2151,10 +2161,12 @@ void BluetoothDeviceClearCacheResponse::calculate_size(ProtoSize &size) const {
 | 
			
		||||
void BluetoothScannerStateResponse::encode(ProtoWriteBuffer buffer) const {
 | 
			
		||||
  buffer.encode_uint32(1, static_cast<uint32_t>(this->state));
 | 
			
		||||
  buffer.encode_uint32(2, static_cast<uint32_t>(this->mode));
 | 
			
		||||
  buffer.encode_uint32(3, static_cast<uint32_t>(this->configured_mode));
 | 
			
		||||
}
 | 
			
		||||
void BluetoothScannerStateResponse::calculate_size(ProtoSize &size) const {
 | 
			
		||||
  size.add_uint32(1, static_cast<uint32_t>(this->state));
 | 
			
		||||
  size.add_uint32(1, static_cast<uint32_t>(this->mode));
 | 
			
		||||
  size.add_uint32(1, static_cast<uint32_t>(this->configured_mode));
 | 
			
		||||
}
 | 
			
		||||
bool BluetoothScannerSetModeRequest::decode_varint(uint32_t field_id, ProtoVarInt value) {
 | 
			
		||||
  switch (field_id) {
 | 
			
		||||
 
 | 
			
		||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@@ -1110,7 +1110,11 @@ void HomeAssistantStateResponse::dump_to(std::string &out) const {
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
void GetTimeRequest::dump_to(std::string &out) const { out.append("GetTimeRequest {}"); }
 | 
			
		||||
void GetTimeResponse::dump_to(std::string &out) const { dump_field(out, "epoch_seconds", this->epoch_seconds); }
 | 
			
		||||
void GetTimeResponse::dump_to(std::string &out) const {
 | 
			
		||||
  MessageDumpHelper helper(out, "GetTimeResponse");
 | 
			
		||||
  dump_field(out, "epoch_seconds", this->epoch_seconds);
 | 
			
		||||
  dump_field(out, "timezone", this->timezone);
 | 
			
		||||
}
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
void ListEntitiesServicesArgument::dump_to(std::string &out) const {
 | 
			
		||||
  MessageDumpHelper helper(out, "ListEntitiesServicesArgument");
 | 
			
		||||
@@ -1135,7 +1139,7 @@ void ExecuteServiceArgument::dump_to(std::string &out) const {
 | 
			
		||||
  dump_field(out, "string_", this->string_);
 | 
			
		||||
  dump_field(out, "int_", this->int_);
 | 
			
		||||
  for (const auto it : this->bool_array) {
 | 
			
		||||
    dump_field(out, "bool_array", it, 4);
 | 
			
		||||
    dump_field(out, "bool_array", static_cast<bool>(it), 4);
 | 
			
		||||
  }
 | 
			
		||||
  for (const auto &it : this->int_array) {
 | 
			
		||||
    dump_field(out, "int_array", it, 4);
 | 
			
		||||
@@ -1534,9 +1538,9 @@ void BluetoothLERawAdvertisement::dump_to(std::string &out) const {
 | 
			
		||||
}
 | 
			
		||||
void BluetoothLERawAdvertisementsResponse::dump_to(std::string &out) const {
 | 
			
		||||
  MessageDumpHelper helper(out, "BluetoothLERawAdvertisementsResponse");
 | 
			
		||||
  for (const auto &it : this->advertisements) {
 | 
			
		||||
  for (uint16_t i = 0; i < this->advertisements_len; i++) {
 | 
			
		||||
    out.append("  advertisements: ");
 | 
			
		||||
    it.dump_to(out);
 | 
			
		||||
    this->advertisements[i].dump_to(out);
 | 
			
		||||
    out.append("\n");
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -1704,6 +1708,7 @@ void BluetoothScannerStateResponse::dump_to(std::string &out) const {
 | 
			
		||||
  MessageDumpHelper helper(out, "BluetoothScannerStateResponse");
 | 
			
		||||
  dump_field(out, "state", static_cast<enums::BluetoothScannerState>(this->state));
 | 
			
		||||
  dump_field(out, "mode", static_cast<enums::BluetoothScannerMode>(this->mode));
 | 
			
		||||
  dump_field(out, "configured_mode", static_cast<enums::BluetoothScannerMode>(this->configured_mode));
 | 
			
		||||
}
 | 
			
		||||
void BluetoothScannerSetModeRequest::dump_to(std::string &out) const {
 | 
			
		||||
  MessageDumpHelper helper(out, "BluetoothScannerSetModeRequest");
 | 
			
		||||
 
 | 
			
		||||
@@ -160,15 +160,6 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
    case GetTimeRequest::MESSAGE_TYPE: {
 | 
			
		||||
      GetTimeRequest msg;
 | 
			
		||||
      // Empty message: no decode needed
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
      ESP_LOGVV(TAG, "on_get_time_request: %s", msg.dump().c_str());
 | 
			
		||||
#endif
 | 
			
		||||
      this->on_get_time_request(msg);
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case GetTimeResponse::MESSAGE_TYPE: {
 | 
			
		||||
      GetTimeResponse msg;
 | 
			
		||||
      msg.decode(msg_data, msg_size);
 | 
			
		||||
@@ -656,11 +647,6 @@ void APIServerConnection::on_subscribe_home_assistant_states_request(const Subsc
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
void APIServerConnection::on_get_time_request(const GetTimeRequest &msg) {
 | 
			
		||||
  if (this->check_connection_setup_() && !this->send_get_time_response(msg)) {
 | 
			
		||||
    this->on_fatal_error();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
void APIServerConnection::on_execute_service_request(const ExecuteServiceRequest &msg) {
 | 
			
		||||
  if (this->check_authenticated_()) {
 | 
			
		||||
 
 | 
			
		||||
@@ -71,7 +71,7 @@ class APIServerConnectionBase : public ProtoService {
 | 
			
		||||
#ifdef USE_API_HOMEASSISTANT_STATES
 | 
			
		||||
  virtual void on_home_assistant_state_response(const HomeAssistantStateResponse &value){};
 | 
			
		||||
#endif
 | 
			
		||||
  virtual void on_get_time_request(const GetTimeRequest &value){};
 | 
			
		||||
 | 
			
		||||
  virtual void on_get_time_response(const GetTimeResponse &value){};
 | 
			
		||||
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
@@ -226,7 +226,6 @@ class APIServerConnection : public APIServerConnectionBase {
 | 
			
		||||
#ifdef USE_API_HOMEASSISTANT_STATES
 | 
			
		||||
  virtual void subscribe_home_assistant_states(const SubscribeHomeAssistantStatesRequest &msg) = 0;
 | 
			
		||||
#endif
 | 
			
		||||
  virtual bool send_get_time_response(const GetTimeRequest &msg) = 0;
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
  virtual void execute_service(const ExecuteServiceRequest &msg) = 0;
 | 
			
		||||
#endif
 | 
			
		||||
@@ -348,7 +347,6 @@ class APIServerConnection : public APIServerConnectionBase {
 | 
			
		||||
#ifdef USE_API_HOMEASSISTANT_STATES
 | 
			
		||||
  void on_subscribe_home_assistant_states_request(const SubscribeHomeAssistantStatesRequest &msg) override;
 | 
			
		||||
#endif
 | 
			
		||||
  void on_get_time_request(const GetTimeRequest &msg) override;
 | 
			
		||||
#ifdef USE_API_SERVICES
 | 
			
		||||
  void on_execute_service_request(const ExecuteServiceRequest &msg) override;
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -37,12 +37,14 @@ void APIServer::setup() {
 | 
			
		||||
 | 
			
		||||
  this->noise_pref_ = global_preferences->make_preference<SavedNoisePsk>(hash, true);
 | 
			
		||||
 | 
			
		||||
#ifndef USE_API_NOISE_PSK_FROM_YAML
 | 
			
		||||
  // Only load saved PSK if not set from YAML
 | 
			
		||||
  SavedNoisePsk noise_pref_saved{};
 | 
			
		||||
  if (this->noise_pref_.load(&noise_pref_saved)) {
 | 
			
		||||
    ESP_LOGD(TAG, "Loaded saved Noise PSK");
 | 
			
		||||
 | 
			
		||||
    this->set_noise_psk(noise_pref_saved.psk);
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  // Schedule reboot if no clients connect within timeout
 | 
			
		||||
@@ -409,6 +411,12 @@ void APIServer::set_reboot_timeout(uint32_t reboot_timeout) { this->reboot_timeo
 | 
			
		||||
 | 
			
		||||
#ifdef USE_API_NOISE
 | 
			
		||||
bool APIServer::save_noise_psk(psk_t psk, bool make_active) {
 | 
			
		||||
#ifdef USE_API_NOISE_PSK_FROM_YAML
 | 
			
		||||
  // When PSK is set from YAML, this function should never be called
 | 
			
		||||
  // but if it is, reject the change
 | 
			
		||||
  ESP_LOGW(TAG, "Key set in YAML");
 | 
			
		||||
  return false;
 | 
			
		||||
#else
 | 
			
		||||
  auto &old_psk = this->noise_ctx_->get_psk();
 | 
			
		||||
  if (std::equal(old_psk.begin(), old_psk.end(), psk.begin())) {
 | 
			
		||||
    ESP_LOGW(TAG, "New PSK matches old");
 | 
			
		||||
@@ -437,6 +445,7 @@ bool APIServer::save_noise_psk(psk_t psk, bool make_active) {
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
  return true;
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -62,9 +62,11 @@ async def async_run_logs(config: dict[str, Any], addresses: list[str]) -> None:
 | 
			
		||||
        time_ = datetime.now()
 | 
			
		||||
        message: bytes = msg.message
 | 
			
		||||
        text = message.decode("utf8", "backslashreplace")
 | 
			
		||||
        for parsed_msg in parse_log_message(
 | 
			
		||||
            text, f"[{time_.hour:02}:{time_.minute:02}:{time_.second:02}]"
 | 
			
		||||
        ):
 | 
			
		||||
        nanoseconds = time_.microsecond // 1000
 | 
			
		||||
        timestamp = (
 | 
			
		||||
            f"[{time_.hour:02}:{time_.minute:02}:{time_.second:02}.{nanoseconds:03}]"
 | 
			
		||||
        )
 | 
			
		||||
        for parsed_msg in parse_log_message(text, timestamp):
 | 
			
		||||
            print(parsed_msg.replace("\033", "\\033") if dashboard else parsed_msg)
 | 
			
		||||
 | 
			
		||||
    stop = await async_run(cli, on_log, name=name)
 | 
			
		||||
 
 | 
			
		||||
@@ -8,74 +8,70 @@ namespace esphome::api {
 | 
			
		||||
static const char *const TAG = "api.proto";
 | 
			
		||||
 | 
			
		||||
void ProtoDecodableMessage::decode(const uint8_t *buffer, size_t length) {
 | 
			
		||||
  uint32_t i = 0;
 | 
			
		||||
  bool error = false;
 | 
			
		||||
  while (i < length) {
 | 
			
		||||
  const uint8_t *ptr = buffer;
 | 
			
		||||
  const uint8_t *end = buffer + length;
 | 
			
		||||
 | 
			
		||||
  while (ptr < end) {
 | 
			
		||||
    uint32_t consumed;
 | 
			
		||||
    auto res = ProtoVarInt::parse(&buffer[i], length - i, &consumed);
 | 
			
		||||
 | 
			
		||||
    // Parse field header
 | 
			
		||||
    auto res = ProtoVarInt::parse(ptr, end - ptr, &consumed);
 | 
			
		||||
    if (!res.has_value()) {
 | 
			
		||||
      ESP_LOGV(TAG, "Invalid field start at %" PRIu32, i);
 | 
			
		||||
      break;
 | 
			
		||||
      ESP_LOGV(TAG, "Invalid field start at offset %ld", (long) (ptr - buffer));
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    uint32_t field_type = (res->as_uint32()) & 0b111;
 | 
			
		||||
    uint32_t field_id = (res->as_uint32()) >> 3;
 | 
			
		||||
    i += consumed;
 | 
			
		||||
    uint32_t tag = res->as_uint32();
 | 
			
		||||
    uint32_t field_type = tag & 0b111;
 | 
			
		||||
    uint32_t field_id = tag >> 3;
 | 
			
		||||
    ptr += consumed;
 | 
			
		||||
 | 
			
		||||
    switch (field_type) {
 | 
			
		||||
      case 0: {  // VarInt
 | 
			
		||||
        res = ProtoVarInt::parse(&buffer[i], length - i, &consumed);
 | 
			
		||||
        res = ProtoVarInt::parse(ptr, end - ptr, &consumed);
 | 
			
		||||
        if (!res.has_value()) {
 | 
			
		||||
          ESP_LOGV(TAG, "Invalid VarInt at %" PRIu32, i);
 | 
			
		||||
          error = true;
 | 
			
		||||
          break;
 | 
			
		||||
          ESP_LOGV(TAG, "Invalid VarInt at offset %ld", (long) (ptr - buffer));
 | 
			
		||||
          return;
 | 
			
		||||
        }
 | 
			
		||||
        if (!this->decode_varint(field_id, *res)) {
 | 
			
		||||
          ESP_LOGV(TAG, "Cannot decode VarInt field %" PRIu32 " with value %" PRIu32 "!", field_id, res->as_uint32());
 | 
			
		||||
        }
 | 
			
		||||
        i += consumed;
 | 
			
		||||
        ptr += consumed;
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      case 2: {  // Length-delimited
 | 
			
		||||
        res = ProtoVarInt::parse(&buffer[i], length - i, &consumed);
 | 
			
		||||
        res = ProtoVarInt::parse(ptr, end - ptr, &consumed);
 | 
			
		||||
        if (!res.has_value()) {
 | 
			
		||||
          ESP_LOGV(TAG, "Invalid Length Delimited at %" PRIu32, i);
 | 
			
		||||
          error = true;
 | 
			
		||||
          break;
 | 
			
		||||
          ESP_LOGV(TAG, "Invalid Length Delimited at offset %ld", (long) (ptr - buffer));
 | 
			
		||||
          return;
 | 
			
		||||
        }
 | 
			
		||||
        uint32_t field_length = res->as_uint32();
 | 
			
		||||
        i += consumed;
 | 
			
		||||
        if (field_length > length - i) {
 | 
			
		||||
          ESP_LOGV(TAG, "Out-of-bounds Length Delimited at %" PRIu32, i);
 | 
			
		||||
          error = true;
 | 
			
		||||
          break;
 | 
			
		||||
        ptr += consumed;
 | 
			
		||||
        if (ptr + field_length > end) {
 | 
			
		||||
          ESP_LOGV(TAG, "Out-of-bounds Length Delimited at offset %ld", (long) (ptr - buffer));
 | 
			
		||||
          return;
 | 
			
		||||
        }
 | 
			
		||||
        if (!this->decode_length(field_id, ProtoLengthDelimited(&buffer[i], field_length))) {
 | 
			
		||||
        if (!this->decode_length(field_id, ProtoLengthDelimited(ptr, field_length))) {
 | 
			
		||||
          ESP_LOGV(TAG, "Cannot decode Length Delimited field %" PRIu32 "!", field_id);
 | 
			
		||||
        }
 | 
			
		||||
        i += field_length;
 | 
			
		||||
        ptr += field_length;
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      case 5: {  // 32-bit
 | 
			
		||||
        if (length - i < 4) {
 | 
			
		||||
          ESP_LOGV(TAG, "Out-of-bounds Fixed32-bit at %" PRIu32, i);
 | 
			
		||||
          error = true;
 | 
			
		||||
          break;
 | 
			
		||||
        if (ptr + 4 > end) {
 | 
			
		||||
          ESP_LOGV(TAG, "Out-of-bounds Fixed32-bit at offset %ld", (long) (ptr - buffer));
 | 
			
		||||
          return;
 | 
			
		||||
        }
 | 
			
		||||
        uint32_t val = encode_uint32(buffer[i + 3], buffer[i + 2], buffer[i + 1], buffer[i]);
 | 
			
		||||
        uint32_t val = encode_uint32(ptr[3], ptr[2], ptr[1], ptr[0]);
 | 
			
		||||
        if (!this->decode_32bit(field_id, Proto32Bit(val))) {
 | 
			
		||||
          ESP_LOGV(TAG, "Cannot decode 32-bit field %" PRIu32 " with value %" PRIu32 "!", field_id, val);
 | 
			
		||||
        }
 | 
			
		||||
        i += 4;
 | 
			
		||||
        ptr += 4;
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      default:
 | 
			
		||||
        ESP_LOGV(TAG, "Invalid field type at %" PRIu32, i);
 | 
			
		||||
        error = true;
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
    if (error) {
 | 
			
		||||
      break;
 | 
			
		||||
        ESP_LOGV(TAG, "Invalid field type %u at offset %ld", field_type, (long) (ptr - buffer));
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -15,6 +15,23 @@
 | 
			
		||||
 | 
			
		||||
namespace esphome::api {
 | 
			
		||||
 | 
			
		||||
// Helper functions for ZigZag encoding/decoding
 | 
			
		||||
inline constexpr uint32_t encode_zigzag32(int32_t value) {
 | 
			
		||||
  return (static_cast<uint32_t>(value) << 1) ^ (static_cast<uint32_t>(value >> 31));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline constexpr uint64_t encode_zigzag64(int64_t value) {
 | 
			
		||||
  return (static_cast<uint64_t>(value) << 1) ^ (static_cast<uint64_t>(value >> 63));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline constexpr int32_t decode_zigzag32(uint32_t value) {
 | 
			
		||||
  return (value & 1) ? static_cast<int32_t>(~(value >> 1)) : static_cast<int32_t>(value >> 1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline constexpr int64_t decode_zigzag64(uint64_t value) {
 | 
			
		||||
  return (value & 1) ? static_cast<int64_t>(~(value >> 1)) : static_cast<int64_t>(value >> 1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * StringRef Ownership Model for API Protocol Messages
 | 
			
		||||
 * ===================================================
 | 
			
		||||
@@ -87,33 +104,25 @@ class ProtoVarInt {
 | 
			
		||||
    return {};  // Incomplete or invalid varint
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  uint16_t as_uint16() const { return this->value_; }
 | 
			
		||||
  uint32_t as_uint32() const { return this->value_; }
 | 
			
		||||
  uint64_t as_uint64() const { return this->value_; }
 | 
			
		||||
  bool as_bool() const { return this->value_; }
 | 
			
		||||
  int32_t as_int32() const {
 | 
			
		||||
  constexpr uint16_t as_uint16() const { return this->value_; }
 | 
			
		||||
  constexpr uint32_t as_uint32() const { return this->value_; }
 | 
			
		||||
  constexpr uint64_t as_uint64() const { return this->value_; }
 | 
			
		||||
  constexpr bool as_bool() const { return this->value_; }
 | 
			
		||||
  constexpr int32_t as_int32() const {
 | 
			
		||||
    // Not ZigZag encoded
 | 
			
		||||
    return static_cast<int32_t>(this->as_int64());
 | 
			
		||||
  }
 | 
			
		||||
  int64_t as_int64() const {
 | 
			
		||||
  constexpr int64_t as_int64() const {
 | 
			
		||||
    // Not ZigZag encoded
 | 
			
		||||
    return static_cast<int64_t>(this->value_);
 | 
			
		||||
  }
 | 
			
		||||
  int32_t as_sint32() const {
 | 
			
		||||
  constexpr int32_t as_sint32() const {
 | 
			
		||||
    // with ZigZag encoding
 | 
			
		||||
    if (this->value_ & 1) {
 | 
			
		||||
      return static_cast<int32_t>(~(this->value_ >> 1));
 | 
			
		||||
    } else {
 | 
			
		||||
      return static_cast<int32_t>(this->value_ >> 1);
 | 
			
		||||
    }
 | 
			
		||||
    return decode_zigzag32(static_cast<uint32_t>(this->value_));
 | 
			
		||||
  }
 | 
			
		||||
  int64_t as_sint64() const {
 | 
			
		||||
  constexpr int64_t as_sint64() const {
 | 
			
		||||
    // with ZigZag encoding
 | 
			
		||||
    if (this->value_ & 1) {
 | 
			
		||||
      return static_cast<int64_t>(~(this->value_ >> 1));
 | 
			
		||||
    } else {
 | 
			
		||||
      return static_cast<int64_t>(this->value_ >> 1);
 | 
			
		||||
    }
 | 
			
		||||
    return decode_zigzag64(this->value_);
 | 
			
		||||
  }
 | 
			
		||||
  /**
 | 
			
		||||
   * Encode the varint value to a pre-allocated buffer without bounds checking.
 | 
			
		||||
@@ -309,22 +318,10 @@ class ProtoWriteBuffer {
 | 
			
		||||
    this->encode_uint64(field_id, static_cast<uint64_t>(value), force);
 | 
			
		||||
  }
 | 
			
		||||
  void encode_sint32(uint32_t field_id, int32_t value, bool force = false) {
 | 
			
		||||
    uint32_t uvalue;
 | 
			
		||||
    if (value < 0) {
 | 
			
		||||
      uvalue = ~(value << 1);
 | 
			
		||||
    } else {
 | 
			
		||||
      uvalue = value << 1;
 | 
			
		||||
    }
 | 
			
		||||
    this->encode_uint32(field_id, uvalue, force);
 | 
			
		||||
    this->encode_uint32(field_id, encode_zigzag32(value), force);
 | 
			
		||||
  }
 | 
			
		||||
  void encode_sint64(uint32_t field_id, int64_t value, bool force = false) {
 | 
			
		||||
    uint64_t uvalue;
 | 
			
		||||
    if (value < 0) {
 | 
			
		||||
      uvalue = ~(value << 1);
 | 
			
		||||
    } else {
 | 
			
		||||
      uvalue = value << 1;
 | 
			
		||||
    }
 | 
			
		||||
    this->encode_uint64(field_id, uvalue, force);
 | 
			
		||||
    this->encode_uint64(field_id, encode_zigzag64(value), force);
 | 
			
		||||
  }
 | 
			
		||||
  void encode_message(uint32_t field_id, const ProtoMessage &value, bool force = false);
 | 
			
		||||
  std::vector<uint8_t> *get_buffer() const { return buffer_; }
 | 
			
		||||
@@ -395,7 +392,7 @@ class ProtoSize {
 | 
			
		||||
   * @param value The uint32_t value to calculate size for
 | 
			
		||||
   * @return The number of bytes needed to encode the value
 | 
			
		||||
   */
 | 
			
		||||
  static inline uint32_t varint(uint32_t value) {
 | 
			
		||||
  static constexpr uint32_t varint(uint32_t value) {
 | 
			
		||||
    // Optimized varint size calculation using leading zeros
 | 
			
		||||
    // Each 7 bits requires one byte in the varint encoding
 | 
			
		||||
    if (value < 128)
 | 
			
		||||
@@ -419,7 +416,7 @@ class ProtoSize {
 | 
			
		||||
   * @param value The uint64_t value to calculate size for
 | 
			
		||||
   * @return The number of bytes needed to encode the value
 | 
			
		||||
   */
 | 
			
		||||
  static inline uint32_t varint(uint64_t value) {
 | 
			
		||||
  static constexpr uint32_t varint(uint64_t value) {
 | 
			
		||||
    // Handle common case of values fitting in uint32_t (vast majority of use cases)
 | 
			
		||||
    if (value <= UINT32_MAX) {
 | 
			
		||||
      return varint(static_cast<uint32_t>(value));
 | 
			
		||||
@@ -450,7 +447,7 @@ class ProtoSize {
 | 
			
		||||
   * @param value The int32_t value to calculate size for
 | 
			
		||||
   * @return The number of bytes needed to encode the value
 | 
			
		||||
   */
 | 
			
		||||
  static inline uint32_t varint(int32_t value) {
 | 
			
		||||
  static constexpr uint32_t varint(int32_t value) {
 | 
			
		||||
    // Negative values are sign-extended to 64 bits in protocol buffers,
 | 
			
		||||
    // which always results in a 10-byte varint for negative int32
 | 
			
		||||
    if (value < 0) {
 | 
			
		||||
@@ -466,7 +463,7 @@ class ProtoSize {
 | 
			
		||||
   * @param value The int64_t value to calculate size for
 | 
			
		||||
   * @return The number of bytes needed to encode the value
 | 
			
		||||
   */
 | 
			
		||||
  static inline uint32_t varint(int64_t value) {
 | 
			
		||||
  static constexpr uint32_t varint(int64_t value) {
 | 
			
		||||
    // For int64_t, we convert to uint64_t and calculate the size
 | 
			
		||||
    // This works because the bit pattern determines the encoding size,
 | 
			
		||||
    // and we've handled negative int32 values as a special case above
 | 
			
		||||
@@ -480,7 +477,7 @@ class ProtoSize {
 | 
			
		||||
   * @param type The wire type value (from the WireType enum in the protobuf spec)
 | 
			
		||||
   * @return The number of bytes needed to encode the field ID and wire type
 | 
			
		||||
   */
 | 
			
		||||
  static inline uint32_t field(uint32_t field_id, uint32_t type) {
 | 
			
		||||
  static constexpr uint32_t field(uint32_t field_id, uint32_t type) {
 | 
			
		||||
    uint32_t tag = (field_id << 3) | (type & 0b111);
 | 
			
		||||
    return varint(tag);
 | 
			
		||||
  }
 | 
			
		||||
@@ -607,9 +604,8 @@ class ProtoSize {
 | 
			
		||||
   */
 | 
			
		||||
  inline void add_sint32_force(uint32_t field_id_size, int32_t value) {
 | 
			
		||||
    // Always calculate size when force is true
 | 
			
		||||
    // ZigZag encoding for sint32: (n << 1) ^ (n >> 31)
 | 
			
		||||
    uint32_t zigzag = (static_cast<uint32_t>(value) << 1) ^ (static_cast<uint32_t>(value >> 31));
 | 
			
		||||
    total_size_ += field_id_size + varint(zigzag);
 | 
			
		||||
    // ZigZag encoding for sint32
 | 
			
		||||
    total_size_ += field_id_size + varint(encode_zigzag32(value));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,7 @@ from esphome.const import (
 | 
			
		||||
    CONF_DIRECTION,
 | 
			
		||||
    CONF_HYSTERESIS,
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    CONF_POWER_MODE,
 | 
			
		||||
    CONF_RANGE,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -57,7 +58,6 @@ FAST_FILTER = {
 | 
			
		||||
CONF_RAW_ANGLE = "raw_angle"
 | 
			
		||||
CONF_RAW_POSITION = "raw_position"
 | 
			
		||||
CONF_WATCHDOG = "watchdog"
 | 
			
		||||
CONF_POWER_MODE = "power_mode"
 | 
			
		||||
CONF_SLOW_FILTER = "slow_filter"
 | 
			
		||||
CONF_FAST_FILTER = "fast_filter"
 | 
			
		||||
CONF_START_POSITION = "start_position"
 | 
			
		||||
 
 | 
			
		||||
@@ -24,7 +24,6 @@ AS5600Sensor = as5600_ns.class_("AS5600Sensor", sensor.Sensor, cg.PollingCompone
 | 
			
		||||
CONF_RAW_ANGLE = "raw_angle"
 | 
			
		||||
CONF_RAW_POSITION = "raw_position"
 | 
			
		||||
CONF_WATCHDOG = "watchdog"
 | 
			
		||||
CONF_POWER_MODE = "power_mode"
 | 
			
		||||
CONF_SLOW_FILTER = "slow_filter"
 | 
			
		||||
CONF_FAST_FILTER = "fast_filter"
 | 
			
		||||
CONF_PWM_FREQUENCY = "pwm_frequency"
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,7 @@ from esphome.const import (
 | 
			
		||||
    PLATFORM_LN882X,
 | 
			
		||||
    PLATFORM_RTL87XX,
 | 
			
		||||
)
 | 
			
		||||
from esphome.core import CORE, coroutine_with_priority
 | 
			
		||||
from esphome.core import CORE, CoroPriority, coroutine_with_priority
 | 
			
		||||
 | 
			
		||||
CODEOWNERS = ["@esphome/core"]
 | 
			
		||||
 | 
			
		||||
@@ -27,7 +27,7 @@ CONFIG_SCHEMA = cv.All(
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@coroutine_with_priority(200.0)
 | 
			
		||||
@coroutine_with_priority(CoroPriority.NETWORK_TRANSPORT)
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    if CORE.is_esp32 or CORE.is_libretiny:
 | 
			
		||||
        # https://github.com/ESP32Async/AsyncTCP
 | 
			
		||||
 
 | 
			
		||||
@@ -16,6 +16,7 @@ from esphome.const import (
 | 
			
		||||
    DEVICE_CLASS_ENERGY,
 | 
			
		||||
    DEVICE_CLASS_POWER,
 | 
			
		||||
    DEVICE_CLASS_POWER_FACTOR,
 | 
			
		||||
    DEVICE_CLASS_REACTIVE_POWER,
 | 
			
		||||
    DEVICE_CLASS_VOLTAGE,
 | 
			
		||||
    ICON_CURRENT_AC,
 | 
			
		||||
    ICON_LIGHTBULB,
 | 
			
		||||
@@ -78,6 +79,7 @@ CONFIG_SCHEMA = (
 | 
			
		||||
                unit_of_measurement=UNIT_VOLT_AMPS_REACTIVE,
 | 
			
		||||
                icon=ICON_LIGHTBULB,
 | 
			
		||||
                accuracy_decimals=2,
 | 
			
		||||
                device_class=DEVICE_CLASS_REACTIVE_POWER,
 | 
			
		||||
                state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_POWER_FACTOR): sensor.sensor_schema(
 | 
			
		||||
 
 | 
			
		||||
@@ -110,6 +110,8 @@ void ATM90E32Component::update() {
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::setup() {
 | 
			
		||||
  this->spi_setup();
 | 
			
		||||
  this->cs_summary_ = this->cs_->dump_summary();
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
 | 
			
		||||
  uint16_t mmode0 = 0x87;  // 3P4W 50Hz
 | 
			
		||||
  uint16_t high_thresh = 0;
 | 
			
		||||
@@ -130,9 +132,9 @@ void ATM90E32Component::setup() {
 | 
			
		||||
    mmode0 |= 0 << 1;  // sets 1st bit to 0, phase b is not counted into the all-phase sum energy/power (P/Q/S)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->write16_(ATM90E32_REGISTER_SOFTRESET, 0x789A);    // Perform soft reset
 | 
			
		||||
  delay(6);                                               // Wait for the minimum 5ms + 1ms
 | 
			
		||||
  this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);  // enable register config access
 | 
			
		||||
  this->write16_(ATM90E32_REGISTER_SOFTRESET, 0x789A, false);  // Perform soft reset
 | 
			
		||||
  delay(6);                                                    // Wait for the minimum 5ms + 1ms
 | 
			
		||||
  this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);       // enable register config access
 | 
			
		||||
  if (!this->validate_spi_read_(0x55AA, "setup()")) {
 | 
			
		||||
    ESP_LOGW(TAG, "Could not initialize ATM90E32 IC, check SPI settings");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
@@ -156,16 +158,17 @@ void ATM90E32Component::setup() {
 | 
			
		||||
 | 
			
		||||
  if (this->enable_offset_calibration_) {
 | 
			
		||||
    // Initialize flash storage for offset calibrations
 | 
			
		||||
    uint32_t o_hash = fnv1_hash(std::string("_offset_calibration_") + this->cs_->dump_summary());
 | 
			
		||||
    uint32_t o_hash = fnv1_hash(std::string("_offset_calibration_") + this->cs_summary_);
 | 
			
		||||
    this->offset_pref_ = global_preferences->make_preference<OffsetCalibration[3]>(o_hash, true);
 | 
			
		||||
    this->restore_offset_calibrations_();
 | 
			
		||||
 | 
			
		||||
    // Initialize flash storage for power offset calibrations
 | 
			
		||||
    uint32_t po_hash = fnv1_hash(std::string("_power_offset_calibration_") + this->cs_->dump_summary());
 | 
			
		||||
    uint32_t po_hash = fnv1_hash(std::string("_power_offset_calibration_") + this->cs_summary_);
 | 
			
		||||
    this->power_offset_pref_ = global_preferences->make_preference<PowerOffsetCalibration[3]>(po_hash, true);
 | 
			
		||||
    this->restore_power_offset_calibrations_();
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION] Power & Voltage/Current offset calibration is disabled. Using config file values.");
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] Power & Voltage/Current offset calibration is disabled. Using config file values.",
 | 
			
		||||
             cs);
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
      this->write16_(this->voltage_offset_registers[phase],
 | 
			
		||||
                     static_cast<uint16_t>(this->offset_phase_[phase].voltage_offset_));
 | 
			
		||||
@@ -180,21 +183,18 @@ void ATM90E32Component::setup() {
 | 
			
		||||
 | 
			
		||||
  if (this->enable_gain_calibration_) {
 | 
			
		||||
    // Initialize flash storage for gain calibration
 | 
			
		||||
    uint32_t g_hash = fnv1_hash(std::string("_gain_calibration_") + this->cs_->dump_summary());
 | 
			
		||||
    uint32_t g_hash = fnv1_hash(std::string("_gain_calibration_") + this->cs_summary_);
 | 
			
		||||
    this->gain_calibration_pref_ = global_preferences->make_preference<GainCalibration[3]>(g_hash, true);
 | 
			
		||||
    this->restore_gain_calibrations_();
 | 
			
		||||
 | 
			
		||||
    if (this->using_saved_calibrations_) {
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION] Successfully restored gain calibration from memory.");
 | 
			
		||||
    } else {
 | 
			
		||||
    if (!this->using_saved_calibrations_) {
 | 
			
		||||
      for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
        this->write16_(voltage_gain_registers[phase], this->phase_[phase].voltage_gain_);
 | 
			
		||||
        this->write16_(current_gain_registers[phase], this->phase_[phase].ct_gain_);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION] Gain calibration is disabled. Using config file values.");
 | 
			
		||||
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] Gain calibration is disabled. Using config file values.", cs);
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
      this->write16_(voltage_gain_registers[phase], this->phase_[phase].voltage_gain_);
 | 
			
		||||
      this->write16_(current_gain_registers[phase], this->phase_[phase].ct_gain_);
 | 
			
		||||
@@ -213,6 +213,122 @@ void ATM90E32Component::setup() {
 | 
			
		||||
  this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000);  // end configuration
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::log_calibration_status_() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
 | 
			
		||||
  bool offset_mismatch = false;
 | 
			
		||||
  bool power_mismatch = false;
 | 
			
		||||
  bool gain_mismatch = false;
 | 
			
		||||
 | 
			
		||||
  for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
    offset_mismatch |= this->offset_calibration_mismatch_[phase];
 | 
			
		||||
    power_mismatch |= this->power_offset_calibration_mismatch_[phase];
 | 
			
		||||
    gain_mismatch |= this->gain_calibration_mismatch_[phase];
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (offset_mismatch) {
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] ", cs);
 | 
			
		||||
    ESP_LOGW(TAG,
 | 
			
		||||
             "[CALIBRATION][%s] ===================== Offset mismatch: using flash values =====================", cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------------------",
 | 
			
		||||
             cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] | Phase |   offset_voltage   |   offset_current   |", cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] |       |  config  |  flash  |  config  |  flash  |", cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------------------",
 | 
			
		||||
             cs);
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION][%s] |   %c   |  %6d  | %6d  |  %6d  | %6d  |", cs, 'A' + phase,
 | 
			
		||||
               this->config_offset_phase_[phase].voltage_offset_, this->offset_phase_[phase].voltage_offset_,
 | 
			
		||||
               this->config_offset_phase_[phase].current_offset_, this->offset_phase_[phase].current_offset_);
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGW(TAG,
 | 
			
		||||
             "[CALIBRATION][%s] ===============================================================================", cs);
 | 
			
		||||
  }
 | 
			
		||||
  if (power_mismatch) {
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] ", cs);
 | 
			
		||||
    ESP_LOGW(TAG,
 | 
			
		||||
             "[CALIBRATION][%s] ================= Power offset mismatch: using flash values =================", cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------------------",
 | 
			
		||||
             cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] | Phase | offset_active_power|offset_reactive_power|", cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] |       |  config  |  flash  |  config  |  flash  |", cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------------------",
 | 
			
		||||
             cs);
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION][%s] |   %c   |  %6d  | %6d  |  %6d  | %6d  |", cs, 'A' + phase,
 | 
			
		||||
               this->config_power_offset_phase_[phase].active_power_offset,
 | 
			
		||||
               this->power_offset_phase_[phase].active_power_offset,
 | 
			
		||||
               this->config_power_offset_phase_[phase].reactive_power_offset,
 | 
			
		||||
               this->power_offset_phase_[phase].reactive_power_offset);
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGW(TAG,
 | 
			
		||||
             "[CALIBRATION][%s] ===============================================================================", cs);
 | 
			
		||||
  }
 | 
			
		||||
  if (gain_mismatch) {
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] ", cs);
 | 
			
		||||
    ESP_LOGW(TAG,
 | 
			
		||||
             "[CALIBRATION][%s] ====================== Gain mismatch: using flash values =====================", cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------------------",
 | 
			
		||||
             cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] | Phase |    voltage_gain    |    current_gain    |", cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] |       |  config  |  flash  |  config  |  flash  |", cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------------------",
 | 
			
		||||
             cs);
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION][%s] |   %c   |  %6u  | %6u  |  %6u  | %6u  |", cs, 'A' + phase,
 | 
			
		||||
               this->config_gain_phase_[phase].voltage_gain, this->gain_phase_[phase].voltage_gain,
 | 
			
		||||
               this->config_gain_phase_[phase].current_gain, this->gain_phase_[phase].current_gain);
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGW(TAG,
 | 
			
		||||
             "[CALIBRATION][%s] ===============================================================================", cs);
 | 
			
		||||
  }
 | 
			
		||||
  if (!this->enable_offset_calibration_) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] Power & Voltage/Current offset calibration is disabled. Using config file values.",
 | 
			
		||||
             cs);
 | 
			
		||||
  } else if (this->restored_offset_calibration_ && !offset_mismatch) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ============== Restored offset calibration from memory ==============", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] --------------------------------------------------------------", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] --------------------------------------------------------------", cs);
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |     %6d      |     %6d      |", cs, 'A' + phase,
 | 
			
		||||
               this->offset_phase_[phase].voltage_offset_, this->offset_phase_[phase].current_offset_);
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ==============================================================\\n", cs);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (this->restored_power_offset_calibration_ && !power_mismatch) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ============ Restored power offset calibration from memory ============", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |       %6d        |        %6d        |", cs, 'A' + phase,
 | 
			
		||||
               this->power_offset_phase_[phase].active_power_offset,
 | 
			
		||||
               this->power_offset_phase_[phase].reactive_power_offset);
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] =====================================================================\n", cs);
 | 
			
		||||
  }
 | 
			
		||||
  if (!this->enable_gain_calibration_) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] Gain calibration is disabled. Using config file values.", cs);
 | 
			
		||||
  } else if (this->restored_gain_calibration_ && !gain_mismatch) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ============ Restoring saved gain calibrations to registers ============", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |    %6u    |    %6u    |", cs, 'A' + phase,
 | 
			
		||||
               this->gain_phase_[phase].voltage_gain, this->gain_phase_[phase].current_gain);
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] =====================================================================\\n", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] Gain calibration loaded and verified successfully.\n", cs);
 | 
			
		||||
  }
 | 
			
		||||
  this->calibration_message_printed_ = true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::dump_config() {
 | 
			
		||||
  ESP_LOGCONFIG("", "ATM90E32:");
 | 
			
		||||
  LOG_PIN("  CS Pin: ", this->cs_);
 | 
			
		||||
@@ -255,6 +371,10 @@ void ATM90E32Component::dump_config() {
 | 
			
		||||
  LOG_SENSOR("  ", "Peak Current C", this->phase_[PHASEC].peak_current_sensor_);
 | 
			
		||||
  LOG_SENSOR("  ", "Frequency", this->freq_sensor_);
 | 
			
		||||
  LOG_SENSOR("  ", "Chip Temp", this->chip_temperature_sensor_);
 | 
			
		||||
  if (this->restored_offset_calibration_ || this->restored_power_offset_calibration_ ||
 | 
			
		||||
      this->restored_gain_calibration_ || !this->enable_offset_calibration_ || !this->enable_gain_calibration_) {
 | 
			
		||||
    this->log_calibration_status_();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float ATM90E32Component::get_setup_priority() const { return setup_priority::IO; }
 | 
			
		||||
@@ -263,19 +383,17 @@ float ATM90E32Component::get_setup_priority() const { return setup_priority::IO;
 | 
			
		||||
// Peakdetect period: 05H. Bit 15:8 are PeakDet_period in ms. 7:0 are Sag_period
 | 
			
		||||
// Default is 143FH (20ms, 63ms)
 | 
			
		||||
uint16_t ATM90E32Component::read16_(uint16_t a_register) {
 | 
			
		||||
  this->enable();
 | 
			
		||||
  delay_microseconds_safe(1);  // min delay between CS low and first SCK is 200ns - 1us is plenty
 | 
			
		||||
  uint8_t addrh = (1 << 7) | ((a_register >> 8) & 0x03);
 | 
			
		||||
  uint8_t addrl = (a_register & 0xFF);
 | 
			
		||||
  uint8_t data[2];
 | 
			
		||||
  uint16_t output;
 | 
			
		||||
  this->enable();
 | 
			
		||||
  delay_microseconds_safe(1);  // min delay between CS low and first SCK is 200ns - 1ms is plenty
 | 
			
		||||
  this->write_byte(addrh);
 | 
			
		||||
  this->write_byte(addrl);
 | 
			
		||||
  this->read_array(data, 2);
 | 
			
		||||
  this->disable();
 | 
			
		||||
 | 
			
		||||
  output = (uint16_t(data[0] & 0xFF) << 8) | (data[1] & 0xFF);
 | 
			
		||||
  uint8_t data[4] = {addrh, addrl, 0x00, 0x00};
 | 
			
		||||
  this->transfer_array(data, 4);
 | 
			
		||||
  uint16_t output = encode_uint16(data[2], data[3]);
 | 
			
		||||
  ESP_LOGVV(TAG, "read16_ 0x%04" PRIX16 " output 0x%04" PRIX16, a_register, output);
 | 
			
		||||
  delay_microseconds_safe(1);  // allow the last clock to propagate before releasing CS
 | 
			
		||||
  this->disable();
 | 
			
		||||
  delay_microseconds_safe(1);  // meet minimum CS high time before next transaction
 | 
			
		||||
  return output;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -292,13 +410,19 @@ int ATM90E32Component::read32_(uint16_t addr_h, uint16_t addr_l) {
 | 
			
		||||
  return val;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::write16_(uint16_t a_register, uint16_t val) {
 | 
			
		||||
void ATM90E32Component::write16_(uint16_t a_register, uint16_t val, bool validate) {
 | 
			
		||||
  ESP_LOGVV(TAG, "write16_ 0x%04" PRIX16 " val 0x%04" PRIX16, a_register, val);
 | 
			
		||||
  uint8_t addrh = ((a_register >> 8) & 0x03);
 | 
			
		||||
  uint8_t addrl = (a_register & 0xFF);
 | 
			
		||||
  uint8_t data[4] = {addrh, addrl, uint8_t((val >> 8) & 0xFF), uint8_t(val & 0xFF)};
 | 
			
		||||
  this->enable();
 | 
			
		||||
  this->write_byte16(a_register);
 | 
			
		||||
  this->write_byte16(val);
 | 
			
		||||
  delay_microseconds_safe(1);  // ensure CS setup time
 | 
			
		||||
  this->write_array(data, 4);
 | 
			
		||||
  delay_microseconds_safe(1);  // allow clock to settle before raising CS
 | 
			
		||||
  this->disable();
 | 
			
		||||
  this->validate_spi_read_(val, "write16()");
 | 
			
		||||
  delay_microseconds_safe(1);  // ensure minimum CS high time
 | 
			
		||||
  if (validate)
 | 
			
		||||
    this->validate_spi_read_(val, "write16()");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float ATM90E32Component::get_local_phase_voltage_(uint8_t phase) { return this->phase_[phase].voltage_; }
 | 
			
		||||
@@ -441,8 +565,10 @@ float ATM90E32Component::get_chip_temperature_() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::run_gain_calibrations() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  if (!this->enable_gain_calibration_) {
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION] Gain calibration is disabled! Enable it first with enable_gain_calibration: true");
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] Gain calibration is disabled! Enable it first with enable_gain_calibration: true",
 | 
			
		||||
             cs);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -454,12 +580,14 @@ void ATM90E32Component::run_gain_calibrations() {
 | 
			
		||||
  float ref_currents[3] = {this->get_reference_current(0), this->get_reference_current(1),
 | 
			
		||||
                           this->get_reference_current(2)};
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION] ");
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION] ========================= Gain Calibration  =========================");
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION] ---------------------------------------------------------------------");
 | 
			
		||||
  ESP_LOGI(TAG,
 | 
			
		||||
           "[CALIBRATION] | Phase | V_meas (V) | I_meas (A) | V_ref | I_ref  | V_gain (old→new) | I_gain (old→new) |");
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION] ---------------------------------------------------------------------");
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ========================= Gain Calibration  =========================", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
  ESP_LOGI(
 | 
			
		||||
      TAG,
 | 
			
		||||
      "[CALIBRATION][%s] | Phase | V_meas (V) | I_meas (A) | V_ref | I_ref  | V_gain (old→new) | I_gain (old→new) |",
 | 
			
		||||
      cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
 | 
			
		||||
  for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
    float measured_voltage = this->get_phase_voltage_avg_(phase);
 | 
			
		||||
@@ -476,22 +604,22 @@ void ATM90E32Component::run_gain_calibrations() {
 | 
			
		||||
 | 
			
		||||
    // Voltage calibration
 | 
			
		||||
    if (ref_voltage <= 0.0f) {
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Skipping voltage calibration: reference voltage is 0.",
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Skipping voltage calibration: reference voltage is 0.", cs,
 | 
			
		||||
               phase_labels[phase]);
 | 
			
		||||
    } else if (measured_voltage == 0.0f) {
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Skipping voltage calibration: measured voltage is 0.",
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Skipping voltage calibration: measured voltage is 0.", cs,
 | 
			
		||||
               phase_labels[phase]);
 | 
			
		||||
    } else {
 | 
			
		||||
      uint32_t new_voltage_gain = static_cast<uint16_t>((ref_voltage / measured_voltage) * current_voltage_gain);
 | 
			
		||||
      if (new_voltage_gain == 0) {
 | 
			
		||||
        ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Voltage gain would be 0. Check reference and measured voltage.",
 | 
			
		||||
        ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Voltage gain would be 0. Check reference and measured voltage.", cs,
 | 
			
		||||
                 phase_labels[phase]);
 | 
			
		||||
      } else {
 | 
			
		||||
        if (new_voltage_gain >= 65535) {
 | 
			
		||||
          ESP_LOGW(
 | 
			
		||||
              TAG,
 | 
			
		||||
              "[CALIBRATION] Phase %s - Voltage gain exceeds 65535. You may need a higher output voltage transformer.",
 | 
			
		||||
              phase_labels[phase]);
 | 
			
		||||
          ESP_LOGW(TAG,
 | 
			
		||||
                   "[CALIBRATION][%s] Phase %s - Voltage gain exceeds 65535. You may need a higher output voltage "
 | 
			
		||||
                   "transformer.",
 | 
			
		||||
                   cs, phase_labels[phase]);
 | 
			
		||||
          new_voltage_gain = 65535;
 | 
			
		||||
        }
 | 
			
		||||
        this->gain_phase_[phase].voltage_gain = static_cast<uint16_t>(new_voltage_gain);
 | 
			
		||||
@@ -501,20 +629,20 @@ void ATM90E32Component::run_gain_calibrations() {
 | 
			
		||||
 | 
			
		||||
    // Current calibration
 | 
			
		||||
    if (ref_current == 0.0f) {
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Skipping current calibration: reference current is 0.",
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Skipping current calibration: reference current is 0.", cs,
 | 
			
		||||
               phase_labels[phase]);
 | 
			
		||||
    } else if (measured_current == 0.0f) {
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Skipping current calibration: measured current is 0.",
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Skipping current calibration: measured current is 0.", cs,
 | 
			
		||||
               phase_labels[phase]);
 | 
			
		||||
    } else {
 | 
			
		||||
      uint32_t new_current_gain = static_cast<uint16_t>((ref_current / measured_current) * current_current_gain);
 | 
			
		||||
      if (new_current_gain == 0) {
 | 
			
		||||
        ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Current gain would be 0. Check reference and measured current.",
 | 
			
		||||
        ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Current gain would be 0. Check reference and measured current.", cs,
 | 
			
		||||
                 phase_labels[phase]);
 | 
			
		||||
      } else {
 | 
			
		||||
        if (new_current_gain >= 65535) {
 | 
			
		||||
          ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Current gain exceeds 65535. You may need to turn up pga gain.",
 | 
			
		||||
                   phase_labels[phase]);
 | 
			
		||||
          ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Current gain exceeds 65535. You may need to turn up pga gain.",
 | 
			
		||||
                   cs, phase_labels[phase]);
 | 
			
		||||
          new_current_gain = 65535;
 | 
			
		||||
        }
 | 
			
		||||
        this->gain_phase_[phase].current_gain = static_cast<uint16_t>(new_current_gain);
 | 
			
		||||
@@ -523,13 +651,13 @@ void ATM90E32Component::run_gain_calibrations() {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Final row output
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION] |   %c   |  %9.2f |  %9.4f | %5.2f | %6.4f |  %5u → %-5u  |  %5u → %-5u  |",
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |  %9.2f |  %9.4f | %5.2f | %6.4f |  %5u → %-5u  |  %5u → %-5u  |", cs,
 | 
			
		||||
             'A' + phase, measured_voltage, measured_current, ref_voltage, ref_current, current_voltage_gain,
 | 
			
		||||
             did_voltage ? this->gain_phase_[phase].voltage_gain : current_voltage_gain, current_current_gain,
 | 
			
		||||
             did_current ? this->gain_phase_[phase].current_gain : current_current_gain);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION] =====================================================================\n");
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] =====================================================================\n", cs);
 | 
			
		||||
 | 
			
		||||
  this->save_gain_calibration_to_memory_();
 | 
			
		||||
  this->write_gains_to_registers_();
 | 
			
		||||
@@ -537,54 +665,108 @@ void ATM90E32Component::run_gain_calibrations() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::save_gain_calibration_to_memory_() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  bool success = this->gain_calibration_pref_.save(&this->gain_phase_);
 | 
			
		||||
  global_preferences->sync();
 | 
			
		||||
  if (success) {
 | 
			
		||||
    this->using_saved_calibrations_ = true;
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION] Gain calibration saved to memory.");
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] Gain calibration saved to memory.", cs);
 | 
			
		||||
  } else {
 | 
			
		||||
    this->using_saved_calibrations_ = false;
 | 
			
		||||
    ESP_LOGE(TAG, "[CALIBRATION] Failed to save gain calibration to memory!");
 | 
			
		||||
    ESP_LOGE(TAG, "[CALIBRATION][%s] Failed to save gain calibration to memory!", cs);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::save_offset_calibration_to_memory_() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  bool success = this->offset_pref_.save(&this->offset_phase_);
 | 
			
		||||
  global_preferences->sync();
 | 
			
		||||
  if (success) {
 | 
			
		||||
    this->using_saved_calibrations_ = true;
 | 
			
		||||
    this->restored_offset_calibration_ = true;
 | 
			
		||||
    for (bool &phase : this->offset_calibration_mismatch_)
 | 
			
		||||
      phase = false;
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] Offset calibration saved to memory.", cs);
 | 
			
		||||
  } else {
 | 
			
		||||
    this->using_saved_calibrations_ = false;
 | 
			
		||||
    ESP_LOGE(TAG, "[CALIBRATION][%s] Failed to save offset calibration to memory!", cs);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::save_power_offset_calibration_to_memory_() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  bool success = this->power_offset_pref_.save(&this->power_offset_phase_);
 | 
			
		||||
  global_preferences->sync();
 | 
			
		||||
  if (success) {
 | 
			
		||||
    this->using_saved_calibrations_ = true;
 | 
			
		||||
    this->restored_power_offset_calibration_ = true;
 | 
			
		||||
    for (bool &phase : this->power_offset_calibration_mismatch_)
 | 
			
		||||
      phase = false;
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] Power offset calibration saved to memory.", cs);
 | 
			
		||||
  } else {
 | 
			
		||||
    this->using_saved_calibrations_ = false;
 | 
			
		||||
    ESP_LOGE(TAG, "[CALIBRATION][%s] Failed to save power offset calibration to memory!", cs);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::run_offset_calibrations() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  if (!this->enable_offset_calibration_) {
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION] Offset calibration is disabled! Enable it first with enable_offset_calibration: true");
 | 
			
		||||
    ESP_LOGW(TAG,
 | 
			
		||||
             "[CALIBRATION][%s] Offset calibration is disabled! Enable it first with enable_offset_calibration: true",
 | 
			
		||||
             cs);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ======================== Offset Calibration ========================", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------", cs);
 | 
			
		||||
 | 
			
		||||
  for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
    int16_t voltage_offset = calibrate_offset(phase, true);
 | 
			
		||||
    int16_t current_offset = calibrate_offset(phase, false);
 | 
			
		||||
 | 
			
		||||
    this->write_offsets_to_registers_(phase, voltage_offset, current_offset);
 | 
			
		||||
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION] Phase %c - offset_voltage: %d, offset_current: %d", 'A' + phase, voltage_offset,
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |     %6d      |     %6d      |", cs, 'A' + phase, voltage_offset,
 | 
			
		||||
             current_offset);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->offset_pref_.save(&this->offset_phase_);  // Save to flash
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ==================================================================\n", cs);
 | 
			
		||||
 | 
			
		||||
  this->save_offset_calibration_to_memory_();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::run_power_offset_calibrations() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  if (!this->enable_offset_calibration_) {
 | 
			
		||||
    ESP_LOGW(
 | 
			
		||||
        TAG,
 | 
			
		||||
        "[CALIBRATION] Offset power calibration is disabled! Enable it first with enable_offset_calibration: true");
 | 
			
		||||
        "[CALIBRATION][%s] Offset power calibration is disabled! Enable it first with enable_offset_calibration: true",
 | 
			
		||||
        cs);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ===================== Power Offset Calibration =====================", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
 | 
			
		||||
  for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
    int16_t active_offset = calibrate_power_offset(phase, false);
 | 
			
		||||
    int16_t reactive_offset = calibrate_power_offset(phase, true);
 | 
			
		||||
 | 
			
		||||
    this->write_power_offsets_to_registers_(phase, active_offset, reactive_offset);
 | 
			
		||||
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION] Phase %c - offset_active_power: %d, offset_reactive_power: %d", 'A' + phase,
 | 
			
		||||
             active_offset, reactive_offset);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |       %6d        |        %6d        |", cs, 'A' + phase, active_offset,
 | 
			
		||||
             reactive_offset);
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] =====================================================================\n", cs);
 | 
			
		||||
 | 
			
		||||
  this->power_offset_pref_.save(&this->power_offset_phase_);  // Save to flash
 | 
			
		||||
  this->save_power_offset_calibration_to_memory_();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::write_gains_to_registers_() {
 | 
			
		||||
@@ -631,102 +813,276 @@ void ATM90E32Component::write_power_offsets_to_registers_(uint8_t phase, int16_t
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::restore_gain_calibrations_() {
 | 
			
		||||
  if (this->gain_calibration_pref_.load(&this->gain_phase_)) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION] Restoring saved gain calibrations to registers:");
 | 
			
		||||
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
      uint16_t v_gain = this->gain_phase_[phase].voltage_gain;
 | 
			
		||||
      uint16_t i_gain = this->gain_phase_[phase].current_gain;
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION]   Phase %c - Voltage Gain: %u, Current Gain: %u", 'A' + phase, v_gain, i_gain);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    this->write_gains_to_registers_();
 | 
			
		||||
 | 
			
		||||
    if (this->verify_gain_writes_()) {
 | 
			
		||||
      this->using_saved_calibrations_ = true;
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION] Gain calibration loaded and verified successfully.");
 | 
			
		||||
    } else {
 | 
			
		||||
      this->using_saved_calibrations_ = false;
 | 
			
		||||
      ESP_LOGE(TAG, "[CALIBRATION] Gain verification failed! Calibration may not be applied correctly.");
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    this->using_saved_calibrations_ = false;
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION] No stored gain calibrations found. Using config file values.");
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  for (uint8_t i = 0; i < 3; ++i) {
 | 
			
		||||
    this->config_gain_phase_[i].voltage_gain = this->phase_[i].voltage_gain_;
 | 
			
		||||
    this->config_gain_phase_[i].current_gain = this->phase_[i].ct_gain_;
 | 
			
		||||
    this->gain_phase_[i] = this->config_gain_phase_[i];
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (this->gain_calibration_pref_.load(&this->gain_phase_)) {
 | 
			
		||||
    bool all_zero = true;
 | 
			
		||||
    bool same_as_config = true;
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
      const auto &cfg = this->config_gain_phase_[phase];
 | 
			
		||||
      const auto &saved = this->gain_phase_[phase];
 | 
			
		||||
      if (saved.voltage_gain != 0 || saved.current_gain != 0)
 | 
			
		||||
        all_zero = false;
 | 
			
		||||
      if (saved.voltage_gain != cfg.voltage_gain || saved.current_gain != cfg.current_gain)
 | 
			
		||||
        same_as_config = false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!all_zero && !same_as_config) {
 | 
			
		||||
      for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
        bool mismatch = false;
 | 
			
		||||
        if (this->has_config_voltage_gain_[phase] &&
 | 
			
		||||
            this->gain_phase_[phase].voltage_gain != this->config_gain_phase_[phase].voltage_gain)
 | 
			
		||||
          mismatch = true;
 | 
			
		||||
        if (this->has_config_current_gain_[phase] &&
 | 
			
		||||
            this->gain_phase_[phase].current_gain != this->config_gain_phase_[phase].current_gain)
 | 
			
		||||
          mismatch = true;
 | 
			
		||||
        if (mismatch)
 | 
			
		||||
          this->gain_calibration_mismatch_[phase] = true;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      this->write_gains_to_registers_();
 | 
			
		||||
 | 
			
		||||
      if (this->verify_gain_writes_()) {
 | 
			
		||||
        this->using_saved_calibrations_ = true;
 | 
			
		||||
        this->restored_gain_calibration_ = true;
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      this->using_saved_calibrations_ = false;
 | 
			
		||||
      ESP_LOGE(TAG, "[CALIBRATION][%s] Gain verification failed! Calibration may not be applied correctly.", cs);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->using_saved_calibrations_ = false;
 | 
			
		||||
  for (uint8_t i = 0; i < 3; ++i)
 | 
			
		||||
    this->gain_phase_[i] = this->config_gain_phase_[i];
 | 
			
		||||
  this->write_gains_to_registers_();
 | 
			
		||||
 | 
			
		||||
  ESP_LOGW(TAG, "[CALIBRATION][%s] No stored gain calibrations found. Using config file values.", cs);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::restore_offset_calibrations_() {
 | 
			
		||||
  if (this->offset_pref_.load(&this->offset_phase_)) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION] Successfully restored offset calibration from memory.");
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  for (uint8_t i = 0; i < 3; ++i)
 | 
			
		||||
    this->config_offset_phase_[i] = this->offset_phase_[i];
 | 
			
		||||
 | 
			
		||||
  bool have_data = this->offset_pref_.load(&this->offset_phase_);
 | 
			
		||||
  bool all_zero = true;
 | 
			
		||||
  if (have_data) {
 | 
			
		||||
    for (auto &phase : this->offset_phase_) {
 | 
			
		||||
      if (phase.voltage_offset_ != 0 || phase.current_offset_ != 0) {
 | 
			
		||||
        all_zero = false;
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (have_data && !all_zero) {
 | 
			
		||||
    this->restored_offset_calibration_ = true;
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
      auto &offset = this->offset_phase_[phase];
 | 
			
		||||
      write_offsets_to_registers_(phase, offset.voltage_offset_, offset.current_offset_);
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION] Phase %c - offset_voltage:: %d, offset_current: %d", 'A' + phase,
 | 
			
		||||
               offset.voltage_offset_, offset.current_offset_);
 | 
			
		||||
      bool mismatch = false;
 | 
			
		||||
      if (this->has_config_voltage_offset_[phase] &&
 | 
			
		||||
          offset.voltage_offset_ != this->config_offset_phase_[phase].voltage_offset_)
 | 
			
		||||
        mismatch = true;
 | 
			
		||||
      if (this->has_config_current_offset_[phase] &&
 | 
			
		||||
          offset.current_offset_ != this->config_offset_phase_[phase].current_offset_)
 | 
			
		||||
        mismatch = true;
 | 
			
		||||
      if (mismatch)
 | 
			
		||||
        this->offset_calibration_mismatch_[phase] = true;
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION] No stored offset calibrations found. Using default values.");
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; phase++)
 | 
			
		||||
      this->offset_phase_[phase] = this->config_offset_phase_[phase];
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] No stored offset calibrations found. Using default values.", cs);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
    write_offsets_to_registers_(phase, this->offset_phase_[phase].voltage_offset_,
 | 
			
		||||
                                this->offset_phase_[phase].current_offset_);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::restore_power_offset_calibrations_() {
 | 
			
		||||
  if (this->power_offset_pref_.load(&this->power_offset_phase_)) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION] Successfully restored power offset calibration from memory.");
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  for (uint8_t i = 0; i < 3; ++i)
 | 
			
		||||
    this->config_power_offset_phase_[i] = this->power_offset_phase_[i];
 | 
			
		||||
 | 
			
		||||
  bool have_data = this->power_offset_pref_.load(&this->power_offset_phase_);
 | 
			
		||||
  bool all_zero = true;
 | 
			
		||||
  if (have_data) {
 | 
			
		||||
    for (auto &phase : this->power_offset_phase_) {
 | 
			
		||||
      if (phase.active_power_offset != 0 || phase.reactive_power_offset != 0) {
 | 
			
		||||
        all_zero = false;
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (have_data && !all_zero) {
 | 
			
		||||
    this->restored_power_offset_calibration_ = true;
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
      auto &offset = this->power_offset_phase_[phase];
 | 
			
		||||
      write_power_offsets_to_registers_(phase, offset.active_power_offset, offset.reactive_power_offset);
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION] Phase %c - offset_active_power: %d, offset_reactive_power: %d", 'A' + phase,
 | 
			
		||||
               offset.active_power_offset, offset.reactive_power_offset);
 | 
			
		||||
      bool mismatch = false;
 | 
			
		||||
      if (this->has_config_active_power_offset_[phase] &&
 | 
			
		||||
          offset.active_power_offset != this->config_power_offset_phase_[phase].active_power_offset)
 | 
			
		||||
        mismatch = true;
 | 
			
		||||
      if (this->has_config_reactive_power_offset_[phase] &&
 | 
			
		||||
          offset.reactive_power_offset != this->config_power_offset_phase_[phase].reactive_power_offset)
 | 
			
		||||
        mismatch = true;
 | 
			
		||||
      if (mismatch)
 | 
			
		||||
        this->power_offset_calibration_mismatch_[phase] = true;
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION] No stored power offsets found. Using default values.");
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; ++phase)
 | 
			
		||||
      this->power_offset_phase_[phase] = this->config_power_offset_phase_[phase];
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] No stored power offsets found. Using default values.", cs);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
    write_power_offsets_to_registers_(phase, this->power_offset_phase_[phase].active_power_offset,
 | 
			
		||||
                                      this->power_offset_phase_[phase].reactive_power_offset);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::clear_gain_calibrations() {
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION] Clearing stored gain calibrations and restoring config-defined values");
 | 
			
		||||
 | 
			
		||||
  for (int phase = 0; phase < 3; phase++) {
 | 
			
		||||
    gain_phase_[phase].voltage_gain = this->phase_[phase].voltage_gain_;
 | 
			
		||||
    gain_phase_[phase].current_gain = this->phase_[phase].ct_gain_;
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  if (!this->using_saved_calibrations_) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] No stored gain calibrations to clear. Current values:", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ----------------------------------------------------------", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ----------------------------------------------------------", cs);
 | 
			
		||||
    for (int phase = 0; phase < 3; phase++) {
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |    %6u    |    %6u    |", cs, 'A' + phase,
 | 
			
		||||
               this->gain_phase_[phase].voltage_gain, this->gain_phase_[phase].current_gain);
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ==========================================================\n", cs);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  bool success = this->gain_calibration_pref_.save(&this->gain_phase_);
 | 
			
		||||
  this->using_saved_calibrations_ = false;
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] Clearing stored gain calibrations and restoring config-defined values", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ----------------------------------------------------------", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ----------------------------------------------------------", cs);
 | 
			
		||||
 | 
			
		||||
  if (success) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION] Gain calibrations cleared. Config values restored:");
 | 
			
		||||
    for (int phase = 0; phase < 3; phase++) {
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION]   Phase %c - Voltage Gain: %u, Current Gain: %u", 'A' + phase,
 | 
			
		||||
               gain_phase_[phase].voltage_gain, gain_phase_[phase].current_gain);
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGE(TAG, "[CALIBRATION] Failed to clear gain calibrations!");
 | 
			
		||||
  for (int phase = 0; phase < 3; phase++) {
 | 
			
		||||
    uint16_t voltage_gain = this->phase_[phase].voltage_gain_;
 | 
			
		||||
    uint16_t current_gain = this->phase_[phase].ct_gain_;
 | 
			
		||||
 | 
			
		||||
    this->config_gain_phase_[phase].voltage_gain = voltage_gain;
 | 
			
		||||
    this->config_gain_phase_[phase].current_gain = current_gain;
 | 
			
		||||
    this->gain_phase_[phase].voltage_gain = voltage_gain;
 | 
			
		||||
    this->gain_phase_[phase].current_gain = current_gain;
 | 
			
		||||
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |    %6u    |    %6u    |", cs, 'A' + phase, voltage_gain, current_gain);
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ==========================================================\n", cs);
 | 
			
		||||
 | 
			
		||||
  GainCalibration zero_gains[3]{{0, 0}, {0, 0}, {0, 0}};
 | 
			
		||||
  bool success = this->gain_calibration_pref_.save(&zero_gains);
 | 
			
		||||
  global_preferences->sync();
 | 
			
		||||
 | 
			
		||||
  this->using_saved_calibrations_ = false;
 | 
			
		||||
  this->restored_gain_calibration_ = false;
 | 
			
		||||
  for (bool &phase : this->gain_calibration_mismatch_)
 | 
			
		||||
    phase = false;
 | 
			
		||||
 | 
			
		||||
  if (!success) {
 | 
			
		||||
    ESP_LOGE(TAG, "[CALIBRATION][%s] Failed to clear gain calibrations!", cs);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->write_gains_to_registers_();  // Apply them to the chip immediately
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::clear_offset_calibrations() {
 | 
			
		||||
  for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
    this->write_offsets_to_registers_(phase, 0, 0);
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  if (!this->restored_offset_calibration_) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] No stored offset calibrations to clear. Current values:", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] --------------------------------------------------------------", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] --------------------------------------------------------------", cs);
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |     %6d      |     %6d      |", cs, 'A' + phase,
 | 
			
		||||
               this->offset_phase_[phase].voltage_offset_, this->offset_phase_[phase].current_offset_);
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ==============================================================\n", cs);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->offset_pref_.save(&this->offset_phase_);  // Save cleared values to flash memory
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] Clearing stored offset calibrations and restoring config-defined values", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] --------------------------------------------------------------", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] --------------------------------------------------------------", cs);
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION] Offsets cleared.");
 | 
			
		||||
  for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
    int16_t voltage_offset =
 | 
			
		||||
        this->has_config_voltage_offset_[phase] ? this->config_offset_phase_[phase].voltage_offset_ : 0;
 | 
			
		||||
    int16_t current_offset =
 | 
			
		||||
        this->has_config_current_offset_[phase] ? this->config_offset_phase_[phase].current_offset_ : 0;
 | 
			
		||||
    this->write_offsets_to_registers_(phase, voltage_offset, current_offset);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |     %6d      |     %6d      |", cs, 'A' + phase, voltage_offset,
 | 
			
		||||
             current_offset);
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ==============================================================\n", cs);
 | 
			
		||||
 | 
			
		||||
  OffsetCalibration zero_offsets[3]{{0, 0}, {0, 0}, {0, 0}};
 | 
			
		||||
  this->offset_pref_.save(&zero_offsets);  // Clear stored values in flash
 | 
			
		||||
  global_preferences->sync();
 | 
			
		||||
 | 
			
		||||
  this->restored_offset_calibration_ = false;
 | 
			
		||||
  for (bool &phase : this->offset_calibration_mismatch_)
 | 
			
		||||
    phase = false;
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] Offsets cleared.", cs);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::clear_power_offset_calibrations() {
 | 
			
		||||
  for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
    this->write_power_offsets_to_registers_(phase, 0, 0);
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  if (!this->restored_power_offset_calibration_) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] No stored power offsets to clear. Current values:", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |       %6d        |        %6d        |", cs, 'A' + phase,
 | 
			
		||||
               this->power_offset_phase_[phase].active_power_offset,
 | 
			
		||||
               this->power_offset_phase_[phase].reactive_power_offset);
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] =====================================================================\n", cs);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->power_offset_pref_.save(&this->power_offset_phase_);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] Clearing stored power offsets and restoring config-defined values", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION] Power offsets cleared.");
 | 
			
		||||
  for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
    int16_t active_offset =
 | 
			
		||||
        this->has_config_active_power_offset_[phase] ? this->config_power_offset_phase_[phase].active_power_offset : 0;
 | 
			
		||||
    int16_t reactive_offset = this->has_config_reactive_power_offset_[phase]
 | 
			
		||||
                                  ? this->config_power_offset_phase_[phase].reactive_power_offset
 | 
			
		||||
                                  : 0;
 | 
			
		||||
    this->write_power_offsets_to_registers_(phase, active_offset, reactive_offset);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |       %6d        |        %6d        |", cs, 'A' + phase, active_offset,
 | 
			
		||||
             reactive_offset);
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] =====================================================================\n", cs);
 | 
			
		||||
 | 
			
		||||
  PowerOffsetCalibration zero_power_offsets[3]{{0, 0}, {0, 0}, {0, 0}};
 | 
			
		||||
  this->power_offset_pref_.save(&zero_power_offsets);
 | 
			
		||||
  global_preferences->sync();
 | 
			
		||||
 | 
			
		||||
  this->restored_power_offset_calibration_ = false;
 | 
			
		||||
  for (bool &phase : this->power_offset_calibration_mismatch_)
 | 
			
		||||
    phase = false;
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] Power offsets cleared.", cs);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int16_t ATM90E32Component::calibrate_offset(uint8_t phase, bool voltage) {
 | 
			
		||||
@@ -747,20 +1103,21 @@ int16_t ATM90E32Component::calibrate_offset(uint8_t phase, bool voltage) {
 | 
			
		||||
 | 
			
		||||
int16_t ATM90E32Component::calibrate_power_offset(uint8_t phase, bool reactive) {
 | 
			
		||||
  const uint8_t num_reads = 5;
 | 
			
		||||
  uint64_t total_value = 0;
 | 
			
		||||
  int64_t total_value = 0;
 | 
			
		||||
 | 
			
		||||
  for (uint8_t i = 0; i < num_reads; ++i) {
 | 
			
		||||
    uint32_t reading = reactive ? this->read32_(ATM90E32_REGISTER_QMEAN + phase, ATM90E32_REGISTER_QMEANLSB + phase)
 | 
			
		||||
                                : this->read32_(ATM90E32_REGISTER_PMEAN + phase, ATM90E32_REGISTER_PMEANLSB + phase);
 | 
			
		||||
    int32_t reading = reactive ? this->read32_(ATM90E32_REGISTER_QMEAN + phase, ATM90E32_REGISTER_QMEANLSB + phase)
 | 
			
		||||
                               : this->read32_(ATM90E32_REGISTER_PMEAN + phase, ATM90E32_REGISTER_PMEANLSB + phase);
 | 
			
		||||
    total_value += reading;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  const uint32_t average_value = total_value / num_reads;
 | 
			
		||||
  const uint32_t power_offset = ~average_value + 1;
 | 
			
		||||
  int32_t average_value = total_value / num_reads;
 | 
			
		||||
  int32_t power_offset = -average_value;
 | 
			
		||||
  return static_cast<int16_t>(power_offset);  // Takes the lower 16 bits
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool ATM90E32Component::verify_gain_writes_() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  bool success = true;
 | 
			
		||||
  for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
    uint16_t read_voltage = this->read16_(voltage_gain_registers[phase]);
 | 
			
		||||
@@ -768,7 +1125,7 @@ bool ATM90E32Component::verify_gain_writes_() {
 | 
			
		||||
 | 
			
		||||
    if (read_voltage != this->gain_phase_[phase].voltage_gain ||
 | 
			
		||||
        read_current != this->gain_phase_[phase].current_gain) {
 | 
			
		||||
      ESP_LOGE(TAG, "[CALIBRATION] Mismatch detected for Phase %s!", phase_labels[phase]);
 | 
			
		||||
      ESP_LOGE(TAG, "[CALIBRATION][%s] Mismatch detected for Phase %s!", cs, phase_labels[phase]);
 | 
			
		||||
      success = false;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
@@ -791,16 +1148,16 @@ void ATM90E32Component::check_phase_status() {
 | 
			
		||||
      status += "Phase Loss; ";
 | 
			
		||||
 | 
			
		||||
    auto *sensor = this->phase_status_text_sensor_[phase];
 | 
			
		||||
    const char *phase_name = sensor ? sensor->get_name().c_str() : "Unknown Phase";
 | 
			
		||||
    if (sensor == nullptr)
 | 
			
		||||
      continue;
 | 
			
		||||
 | 
			
		||||
    if (!status.empty()) {
 | 
			
		||||
      status.pop_back();  // remove space
 | 
			
		||||
      status.pop_back();  // remove semicolon
 | 
			
		||||
      ESP_LOGW(TAG, "%s: %s", phase_name, status.c_str());
 | 
			
		||||
      if (sensor != nullptr)
 | 
			
		||||
        sensor->publish_state(status);
 | 
			
		||||
      ESP_LOGW(TAG, "%s: %s", sensor->get_name().c_str(), status.c_str());
 | 
			
		||||
      sensor->publish_state(status);
 | 
			
		||||
    } else {
 | 
			
		||||
      if (sensor != nullptr)
 | 
			
		||||
        sensor->publish_state("Okay");
 | 
			
		||||
      sensor->publish_state("Okay");
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -817,9 +1174,12 @@ void ATM90E32Component::check_freq_status() {
 | 
			
		||||
  } else {
 | 
			
		||||
    freq_status = "Normal";
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGW(TAG, "Frequency status: %s", freq_status.c_str());
 | 
			
		||||
 | 
			
		||||
  if (this->freq_status_text_sensor_ != nullptr) {
 | 
			
		||||
    if (freq_status == "Normal") {
 | 
			
		||||
      ESP_LOGD(TAG, "Frequency status: %s", freq_status.c_str());
 | 
			
		||||
    } else {
 | 
			
		||||
      ESP_LOGW(TAG, "Frequency status: %s", freq_status.c_str());
 | 
			
		||||
    }
 | 
			
		||||
    this->freq_status_text_sensor_->publish_state(freq_status);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -61,15 +61,29 @@ class ATM90E32Component : public PollingComponent,
 | 
			
		||||
    this->phase_[phase].harmonic_active_power_sensor_ = obj;
 | 
			
		||||
  }
 | 
			
		||||
  void set_peak_current_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].peak_current_sensor_ = obj; }
 | 
			
		||||
  void set_volt_gain(int phase, uint16_t gain) { this->phase_[phase].voltage_gain_ = gain; }
 | 
			
		||||
  void set_ct_gain(int phase, uint16_t gain) { this->phase_[phase].ct_gain_ = gain; }
 | 
			
		||||
  void set_voltage_offset(uint8_t phase, int16_t offset) { this->offset_phase_[phase].voltage_offset_ = offset; }
 | 
			
		||||
  void set_current_offset(uint8_t phase, int16_t offset) { this->offset_phase_[phase].current_offset_ = offset; }
 | 
			
		||||
  void set_volt_gain(int phase, uint16_t gain) {
 | 
			
		||||
    this->phase_[phase].voltage_gain_ = gain;
 | 
			
		||||
    this->has_config_voltage_gain_[phase] = true;
 | 
			
		||||
  }
 | 
			
		||||
  void set_ct_gain(int phase, uint16_t gain) {
 | 
			
		||||
    this->phase_[phase].ct_gain_ = gain;
 | 
			
		||||
    this->has_config_current_gain_[phase] = true;
 | 
			
		||||
  }
 | 
			
		||||
  void set_voltage_offset(uint8_t phase, int16_t offset) {
 | 
			
		||||
    this->offset_phase_[phase].voltage_offset_ = offset;
 | 
			
		||||
    this->has_config_voltage_offset_[phase] = true;
 | 
			
		||||
  }
 | 
			
		||||
  void set_current_offset(uint8_t phase, int16_t offset) {
 | 
			
		||||
    this->offset_phase_[phase].current_offset_ = offset;
 | 
			
		||||
    this->has_config_current_offset_[phase] = true;
 | 
			
		||||
  }
 | 
			
		||||
  void set_active_power_offset(uint8_t phase, int16_t offset) {
 | 
			
		||||
    this->power_offset_phase_[phase].active_power_offset = offset;
 | 
			
		||||
    this->has_config_active_power_offset_[phase] = true;
 | 
			
		||||
  }
 | 
			
		||||
  void set_reactive_power_offset(uint8_t phase, int16_t offset) {
 | 
			
		||||
    this->power_offset_phase_[phase].reactive_power_offset = offset;
 | 
			
		||||
    this->has_config_reactive_power_offset_[phase] = true;
 | 
			
		||||
  }
 | 
			
		||||
  void set_freq_sensor(sensor::Sensor *freq_sensor) { freq_sensor_ = freq_sensor; }
 | 
			
		||||
  void set_peak_current_signed(bool flag) { peak_current_signed_ = flag; }
 | 
			
		||||
@@ -127,7 +141,7 @@ class ATM90E32Component : public PollingComponent,
 | 
			
		||||
#endif
 | 
			
		||||
  uint16_t read16_(uint16_t a_register);
 | 
			
		||||
  int read32_(uint16_t addr_h, uint16_t addr_l);
 | 
			
		||||
  void write16_(uint16_t a_register, uint16_t val);
 | 
			
		||||
  void write16_(uint16_t a_register, uint16_t val, bool validate = true);
 | 
			
		||||
  float get_local_phase_voltage_(uint8_t phase);
 | 
			
		||||
  float get_local_phase_current_(uint8_t phase);
 | 
			
		||||
  float get_local_phase_active_power_(uint8_t phase);
 | 
			
		||||
@@ -159,12 +173,15 @@ class ATM90E32Component : public PollingComponent,
 | 
			
		||||
  void restore_offset_calibrations_();
 | 
			
		||||
  void restore_power_offset_calibrations_();
 | 
			
		||||
  void restore_gain_calibrations_();
 | 
			
		||||
  void save_offset_calibration_to_memory_();
 | 
			
		||||
  void save_gain_calibration_to_memory_();
 | 
			
		||||
  void save_power_offset_calibration_to_memory_();
 | 
			
		||||
  void write_offsets_to_registers_(uint8_t phase, int16_t voltage_offset, int16_t current_offset);
 | 
			
		||||
  void write_power_offsets_to_registers_(uint8_t phase, int16_t p_offset, int16_t q_offset);
 | 
			
		||||
  void write_gains_to_registers_();
 | 
			
		||||
  bool verify_gain_writes_();
 | 
			
		||||
  bool validate_spi_read_(uint16_t expected, const char *context = nullptr);
 | 
			
		||||
  void log_calibration_status_();
 | 
			
		||||
 | 
			
		||||
  struct ATM90E32Phase {
 | 
			
		||||
    uint16_t voltage_gain_{0};
 | 
			
		||||
@@ -204,19 +221,33 @@ class ATM90E32Component : public PollingComponent,
 | 
			
		||||
    int16_t current_offset_{0};
 | 
			
		||||
  } offset_phase_[3];
 | 
			
		||||
 | 
			
		||||
  OffsetCalibration config_offset_phase_[3];
 | 
			
		||||
 | 
			
		||||
  struct PowerOffsetCalibration {
 | 
			
		||||
    int16_t active_power_offset{0};
 | 
			
		||||
    int16_t reactive_power_offset{0};
 | 
			
		||||
  } power_offset_phase_[3];
 | 
			
		||||
 | 
			
		||||
  PowerOffsetCalibration config_power_offset_phase_[3];
 | 
			
		||||
 | 
			
		||||
  struct GainCalibration {
 | 
			
		||||
    uint16_t voltage_gain{1};
 | 
			
		||||
    uint16_t current_gain{1};
 | 
			
		||||
  } gain_phase_[3];
 | 
			
		||||
 | 
			
		||||
  GainCalibration config_gain_phase_[3];
 | 
			
		||||
 | 
			
		||||
  bool has_config_voltage_offset_[3]{false, false, false};
 | 
			
		||||
  bool has_config_current_offset_[3]{false, false, false};
 | 
			
		||||
  bool has_config_active_power_offset_[3]{false, false, false};
 | 
			
		||||
  bool has_config_reactive_power_offset_[3]{false, false, false};
 | 
			
		||||
  bool has_config_voltage_gain_[3]{false, false, false};
 | 
			
		||||
  bool has_config_current_gain_[3]{false, false, false};
 | 
			
		||||
 | 
			
		||||
  ESPPreferenceObject offset_pref_;
 | 
			
		||||
  ESPPreferenceObject power_offset_pref_;
 | 
			
		||||
  ESPPreferenceObject gain_calibration_pref_;
 | 
			
		||||
  std::string cs_summary_;
 | 
			
		||||
 | 
			
		||||
  sensor::Sensor *freq_sensor_{nullptr};
 | 
			
		||||
#ifdef USE_TEXT_SENSOR
 | 
			
		||||
@@ -231,6 +262,13 @@ class ATM90E32Component : public PollingComponent,
 | 
			
		||||
  bool peak_current_signed_{false};
 | 
			
		||||
  bool enable_offset_calibration_{false};
 | 
			
		||||
  bool enable_gain_calibration_{false};
 | 
			
		||||
  bool restored_offset_calibration_{false};
 | 
			
		||||
  bool restored_power_offset_calibration_{false};
 | 
			
		||||
  bool restored_gain_calibration_{false};
 | 
			
		||||
  bool calibration_message_printed_{false};
 | 
			
		||||
  bool offset_calibration_mismatch_[3]{false, false, false};
 | 
			
		||||
  bool power_offset_calibration_mismatch_[3]{false, false, false};
 | 
			
		||||
  bool gain_calibration_mismatch_[3]{false, false, false};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace atm90e32
 | 
			
		||||
 
 | 
			
		||||
@@ -17,10 +17,12 @@ from esphome.const import (
 | 
			
		||||
    CONF_REACTIVE_POWER,
 | 
			
		||||
    CONF_REVERSE_ACTIVE_ENERGY,
 | 
			
		||||
    CONF_VOLTAGE,
 | 
			
		||||
    DEVICE_CLASS_APPARENT_POWER,
 | 
			
		||||
    DEVICE_CLASS_CURRENT,
 | 
			
		||||
    DEVICE_CLASS_ENERGY,
 | 
			
		||||
    DEVICE_CLASS_POWER,
 | 
			
		||||
    DEVICE_CLASS_POWER_FACTOR,
 | 
			
		||||
    DEVICE_CLASS_REACTIVE_POWER,
 | 
			
		||||
    DEVICE_CLASS_TEMPERATURE,
 | 
			
		||||
    DEVICE_CLASS_VOLTAGE,
 | 
			
		||||
    ENTITY_CATEGORY_DIAGNOSTIC,
 | 
			
		||||
@@ -100,13 +102,13 @@ ATM90E32_PHASE_SCHEMA = cv.Schema(
 | 
			
		||||
            unit_of_measurement=UNIT_VOLT_AMPS_REACTIVE,
 | 
			
		||||
            icon=ICON_LIGHTBULB,
 | 
			
		||||
            accuracy_decimals=2,
 | 
			
		||||
            device_class=DEVICE_CLASS_POWER,
 | 
			
		||||
            device_class=DEVICE_CLASS_REACTIVE_POWER,
 | 
			
		||||
            state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_APPARENT_POWER): sensor.sensor_schema(
 | 
			
		||||
            unit_of_measurement=UNIT_VOLT_AMPS,
 | 
			
		||||
            accuracy_decimals=2,
 | 
			
		||||
            device_class=DEVICE_CLASS_POWER,
 | 
			
		||||
            device_class=DEVICE_CLASS_APPARENT_POWER,
 | 
			
		||||
            state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_POWER_FACTOR): sensor.sensor_schema(
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@ from esphome import automation
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.const import CONF_ID, CONF_MIC_GAIN
 | 
			
		||||
from esphome.core import coroutine_with_priority
 | 
			
		||||
from esphome.core import CoroPriority, coroutine_with_priority
 | 
			
		||||
 | 
			
		||||
CODEOWNERS = ["@kbx81"]
 | 
			
		||||
IS_PLATFORM_COMPONENT = True
 | 
			
		||||
@@ -35,7 +35,7 @@ async def audio_adc_set_mic_gain_to_code(config, action_id, template_arg, args):
 | 
			
		||||
    return var
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@coroutine_with_priority(100.0)
 | 
			
		||||
@coroutine_with_priority(CoroPriority.CORE)
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    cg.add_define("USE_AUDIO_ADC")
 | 
			
		||||
    cg.add_global(audio_adc_ns.using)
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,7 @@ from esphome.automation import maybe_simple_id
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.const import CONF_ID, CONF_VOLUME
 | 
			
		||||
from esphome.core import coroutine_with_priority
 | 
			
		||||
from esphome.core import CoroPriority, coroutine_with_priority
 | 
			
		||||
 | 
			
		||||
CODEOWNERS = ["@kbx81"]
 | 
			
		||||
IS_PLATFORM_COMPONENT = True
 | 
			
		||||
@@ -51,7 +51,7 @@ async def audio_dac_set_volume_to_code(config, action_id, template_arg, args):
 | 
			
		||||
    return var
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@coroutine_with_priority(100.0)
 | 
			
		||||
@coroutine_with_priority(CoroPriority.CORE)
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    cg.add_define("USE_AUDIO_DAC")
 | 
			
		||||
    cg.add_global(audio_dac_ns.using)
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,7 @@ constexpr static const uint8_t AXS_READ_TOUCHPAD[11] = {0xb5, 0xab, 0xa5, 0x5a,
 | 
			
		||||
 | 
			
		||||
#define ERROR_CHECK(err) \
 | 
			
		||||
  if ((err) != i2c::ERROR_OK) { \
 | 
			
		||||
    this->status_set_warning("Failed to communicate"); \
 | 
			
		||||
    this->status_set_warning(LOG_STR("Failed to communicate")); \
 | 
			
		||||
    return; \
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -41,7 +41,7 @@ void AXS15231Touchscreen::update_touches() {
 | 
			
		||||
  i2c::ErrorCode err;
 | 
			
		||||
  uint8_t data[8]{};
 | 
			
		||||
 | 
			
		||||
  err = this->write(AXS_READ_TOUCHPAD, sizeof(AXS_READ_TOUCHPAD), false);
 | 
			
		||||
  err = this->write(AXS_READ_TOUCHPAD, sizeof(AXS_READ_TOUCHPAD));
 | 
			
		||||
  ERROR_CHECK(err);
 | 
			
		||||
  err = this->read(data, sizeof(data));
 | 
			
		||||
  ERROR_CHECK(err);
 | 
			
		||||
 
 | 
			
		||||
@@ -493,7 +493,7 @@ void BedJetHub::dump_config() {
 | 
			
		||||
                "  ble_client.app_id: %d\n"
 | 
			
		||||
                "  ble_client.conn_id: %d",
 | 
			
		||||
                this->get_name().c_str(), this->parent()->app_id, this->parent()->get_conn_id());
 | 
			
		||||
  LOG_UPDATE_INTERVAL(this)
 | 
			
		||||
  LOG_UPDATE_INTERVAL(this);
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Child components (%d):", this->children_.size());
 | 
			
		||||
  for (auto *child : this->children_) {
 | 
			
		||||
    ESP_LOGCONFIG(TAG, "    - %s", child->describe().c_str());
 | 
			
		||||
 
 | 
			
		||||
@@ -59,7 +59,7 @@ from esphome.const import (
 | 
			
		||||
    DEVICE_CLASS_VIBRATION,
 | 
			
		||||
    DEVICE_CLASS_WINDOW,
 | 
			
		||||
)
 | 
			
		||||
from esphome.core import CORE, coroutine_with_priority
 | 
			
		||||
from esphome.core import CORE, CoroPriority, coroutine_with_priority
 | 
			
		||||
from esphome.core.entity_helpers import entity_duplicate_validator, setup_entity
 | 
			
		||||
from esphome.cpp_generator import MockObjClass
 | 
			
		||||
from esphome.util import Registry
 | 
			
		||||
@@ -652,7 +652,7 @@ async def binary_sensor_is_off_to_code(config, condition_id, template_arg, args)
 | 
			
		||||
    return cg.new_Pvariable(condition_id, template_arg, paren, False)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@coroutine_with_priority(100.0)
 | 
			
		||||
@coroutine_with_priority(CoroPriority.CORE)
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    cg.add_global(binary_sensor_ns.using)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,19 @@ namespace binary_sensor {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "binary_sensor";
 | 
			
		||||
 | 
			
		||||
// Function implementation of LOG_BINARY_SENSOR macro to reduce code size
 | 
			
		||||
void log_binary_sensor(const char *tag, const char *prefix, const char *type, BinarySensor *obj) {
 | 
			
		||||
  if (obj == nullptr) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
 | 
			
		||||
 | 
			
		||||
  if (!obj->get_device_class_ref().empty()) {
 | 
			
		||||
    ESP_LOGCONFIG(tag, "%s  Device Class: '%s'", prefix, obj->get_device_class_ref().c_str());
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BinarySensor::publish_state(bool new_state) {
 | 
			
		||||
  if (this->filter_list_ == nullptr) {
 | 
			
		||||
    this->send_state_internal(new_state);
 | 
			
		||||
 
 | 
			
		||||
@@ -10,13 +10,10 @@ namespace esphome {
 | 
			
		||||
 | 
			
		||||
namespace binary_sensor {
 | 
			
		||||
 | 
			
		||||
#define LOG_BINARY_SENSOR(prefix, type, obj) \
 | 
			
		||||
  if ((obj) != nullptr) { \
 | 
			
		||||
    ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
 | 
			
		||||
    if (!(obj)->get_device_class().empty()) { \
 | 
			
		||||
      ESP_LOGCONFIG(TAG, "%s  Device Class: '%s'", prefix, (obj)->get_device_class().c_str()); \
 | 
			
		||||
    } \
 | 
			
		||||
  }
 | 
			
		||||
class BinarySensor;
 | 
			
		||||
void log_binary_sensor(const char *tag, const char *prefix, const char *type, BinarySensor *obj);
 | 
			
		||||
 | 
			
		||||
#define LOG_BINARY_SENSOR(prefix, type, obj) log_binary_sensor(TAG, prefix, LOG_STR_LITERAL(type), obj)
 | 
			
		||||
 | 
			
		||||
#define SUB_BINARY_SENSOR(name) \
 | 
			
		||||
 protected: \
 | 
			
		||||
 
 | 
			
		||||
@@ -1 +1,6 @@
 | 
			
		||||
CODEOWNERS = ["@tobias-"]
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
 | 
			
		||||
CODEOWNERS = ["@tobias-", "@dan-s-github"]
 | 
			
		||||
 | 
			
		||||
CONF_BL0940_ID = "bl0940_id"
 | 
			
		||||
bl0940_ns = cg.esphome_ns.namespace("bl0940")
 | 
			
		||||
 
 | 
			
		||||
@@ -7,28 +7,26 @@ namespace bl0940 {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "bl0940";
 | 
			
		||||
 | 
			
		||||
static const uint8_t BL0940_READ_COMMAND = 0x50;  // 0x58 according to documentation
 | 
			
		||||
static const uint8_t BL0940_FULL_PACKET = 0xAA;
 | 
			
		||||
static const uint8_t BL0940_PACKET_HEADER = 0x55;  // 0x58 according to documentation
 | 
			
		||||
static const uint8_t BL0940_PACKET_HEADER = 0x55;  // 0x58 according to en doc but 0x55 in cn doc
 | 
			
		||||
 | 
			
		||||
static const uint8_t BL0940_WRITE_COMMAND = 0xA0;  // 0xA8 according to documentation
 | 
			
		||||
static const uint8_t BL0940_REG_I_FAST_RMS_CTRL = 0x10;
 | 
			
		||||
static const uint8_t BL0940_REG_MODE = 0x18;
 | 
			
		||||
static const uint8_t BL0940_REG_SOFT_RESET = 0x19;
 | 
			
		||||
static const uint8_t BL0940_REG_USR_WRPROT = 0x1A;
 | 
			
		||||
static const uint8_t BL0940_REG_TPS_CTRL = 0x1B;
 | 
			
		||||
 | 
			
		||||
const uint8_t BL0940_INIT[5][6] = {
 | 
			
		||||
static const uint8_t BL0940_INIT[5][5] = {
 | 
			
		||||
    // Reset to default
 | 
			
		||||
    {BL0940_WRITE_COMMAND, BL0940_REG_SOFT_RESET, 0x5A, 0x5A, 0x5A, 0x38},
 | 
			
		||||
    {BL0940_REG_SOFT_RESET, 0x5A, 0x5A, 0x5A, 0x38},
 | 
			
		||||
    // Enable User Operation Write
 | 
			
		||||
    {BL0940_WRITE_COMMAND, BL0940_REG_USR_WRPROT, 0x55, 0x00, 0x00, 0xF0},
 | 
			
		||||
    {BL0940_REG_USR_WRPROT, 0x55, 0x00, 0x00, 0xF0},
 | 
			
		||||
    // 0x0100 = CF_UNABLE energy pulse, AC_FREQ_SEL 50Hz, RMS_UPDATE_SEL 800mS
 | 
			
		||||
    {BL0940_WRITE_COMMAND, BL0940_REG_MODE, 0x00, 0x10, 0x00, 0x37},
 | 
			
		||||
    {BL0940_REG_MODE, 0x00, 0x10, 0x00, 0x37},
 | 
			
		||||
    // 0x47FF = Over-current and leakage alarm on, Automatic temperature measurement, Interval 100mS
 | 
			
		||||
    {BL0940_WRITE_COMMAND, BL0940_REG_TPS_CTRL, 0xFF, 0x47, 0x00, 0xFE},
 | 
			
		||||
    {BL0940_REG_TPS_CTRL, 0xFF, 0x47, 0x00, 0xFE},
 | 
			
		||||
    // 0x181C = Half cycle, Fast RMS threshold 6172
 | 
			
		||||
    {BL0940_WRITE_COMMAND, BL0940_REG_I_FAST_RMS_CTRL, 0x1C, 0x18, 0x00, 0x1B}};
 | 
			
		||||
    {BL0940_REG_I_FAST_RMS_CTRL, 0x1C, 0x18, 0x00, 0x1B}};
 | 
			
		||||
 | 
			
		||||
void BL0940::loop() {
 | 
			
		||||
  DataPacket buffer;
 | 
			
		||||
@@ -36,8 +34,8 @@ void BL0940::loop() {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (read_array((uint8_t *) &buffer, sizeof(buffer))) {
 | 
			
		||||
    if (validate_checksum(&buffer)) {
 | 
			
		||||
      received_package_(&buffer);
 | 
			
		||||
    if (this->validate_checksum_(&buffer)) {
 | 
			
		||||
      this->received_package_(&buffer);
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGW(TAG, "Junk on wire. Throwing away partial message");
 | 
			
		||||
@@ -46,35 +44,151 @@ void BL0940::loop() {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool BL0940::validate_checksum(const DataPacket *data) {
 | 
			
		||||
  uint8_t checksum = BL0940_READ_COMMAND;
 | 
			
		||||
bool BL0940::validate_checksum_(DataPacket *data) {
 | 
			
		||||
  uint8_t checksum = this->read_command_;
 | 
			
		||||
  // Whole package but checksum
 | 
			
		||||
  for (uint32_t i = 0; i < sizeof(data->raw) - 1; i++) {
 | 
			
		||||
    checksum += data->raw[i];
 | 
			
		||||
  uint8_t *raw = (uint8_t *) data;
 | 
			
		||||
  for (uint32_t i = 0; i < sizeof(*data) - 1; i++) {
 | 
			
		||||
    checksum += raw[i];
 | 
			
		||||
  }
 | 
			
		||||
  checksum ^= 0xFF;
 | 
			
		||||
  if (checksum != data->checksum) {
 | 
			
		||||
    ESP_LOGW(TAG, "BL0940 invalid checksum! 0x%02X != 0x%02X", checksum, data->checksum);
 | 
			
		||||
    ESP_LOGW(TAG, "Invalid checksum! 0x%02X != 0x%02X", checksum, data->checksum);
 | 
			
		||||
  }
 | 
			
		||||
  return checksum == data->checksum;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BL0940::update() {
 | 
			
		||||
  this->flush();
 | 
			
		||||
  this->write_byte(BL0940_READ_COMMAND);
 | 
			
		||||
  this->write_byte(this->read_command_);
 | 
			
		||||
  this->write_byte(BL0940_FULL_PACKET);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BL0940::setup() {
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
  // add calibration callbacks
 | 
			
		||||
  if (this->voltage_calibration_number_ != nullptr) {
 | 
			
		||||
    this->voltage_calibration_number_->add_on_state_callback(
 | 
			
		||||
        [this](float state) { this->voltage_calibration_callback_(state); });
 | 
			
		||||
    if (this->voltage_calibration_number_->has_state()) {
 | 
			
		||||
      this->voltage_calibration_callback_(this->voltage_calibration_number_->state);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (this->current_calibration_number_ != nullptr) {
 | 
			
		||||
    this->current_calibration_number_->add_on_state_callback(
 | 
			
		||||
        [this](float state) { this->current_calibration_callback_(state); });
 | 
			
		||||
    if (this->current_calibration_number_->has_state()) {
 | 
			
		||||
      this->current_calibration_callback_(this->current_calibration_number_->state);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (this->power_calibration_number_ != nullptr) {
 | 
			
		||||
    this->power_calibration_number_->add_on_state_callback(
 | 
			
		||||
        [this](float state) { this->power_calibration_callback_(state); });
 | 
			
		||||
    if (this->power_calibration_number_->has_state()) {
 | 
			
		||||
      this->power_calibration_callback_(this->power_calibration_number_->state);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (this->energy_calibration_number_ != nullptr) {
 | 
			
		||||
    this->energy_calibration_number_->add_on_state_callback(
 | 
			
		||||
        [this](float state) { this->energy_calibration_callback_(state); });
 | 
			
		||||
    if (this->energy_calibration_number_->has_state()) {
 | 
			
		||||
      this->energy_calibration_callback_(this->energy_calibration_number_->state);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  // calculate calibrated reference values
 | 
			
		||||
  this->voltage_reference_cal_ = this->voltage_reference_ / this->voltage_cal_;
 | 
			
		||||
  this->current_reference_cal_ = this->current_reference_ / this->current_cal_;
 | 
			
		||||
  this->power_reference_cal_ = this->power_reference_ / this->power_cal_;
 | 
			
		||||
  this->energy_reference_cal_ = this->energy_reference_ / this->energy_cal_;
 | 
			
		||||
 | 
			
		||||
  for (auto *i : BL0940_INIT) {
 | 
			
		||||
    this->write_array(i, 6);
 | 
			
		||||
    this->write_byte(this->write_command_), this->write_array(i, 5);
 | 
			
		||||
    delay(1);
 | 
			
		||||
  }
 | 
			
		||||
  this->flush();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float BL0940::update_temp_(sensor::Sensor *sensor, ube16_t temperature) const {
 | 
			
		||||
  auto tb = (float) (temperature.h << 8 | temperature.l);
 | 
			
		||||
float BL0940::calculate_power_reference_() {
 | 
			
		||||
  // calculate power reference based on voltage and current reference
 | 
			
		||||
  return this->voltage_reference_cal_ * this->current_reference_cal_ * 4046 / 324004 / 79931;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float BL0940::calculate_energy_reference_() {
 | 
			
		||||
  // formula: 3600000 * 4046 * RL * R1 * 1000 / (1638.4 * 256) / Vref² / (R1 + R2)
 | 
			
		||||
  // or:  power_reference_ * 3600000 / (1638.4 * 256)
 | 
			
		||||
  return this->power_reference_cal_ * 3600000 / (1638.4 * 256);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float BL0940::calculate_calibration_value_(float state) { return (100 + state) / 100; }
 | 
			
		||||
 | 
			
		||||
void BL0940::reset_calibration() {
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
  if (this->current_calibration_number_ != nullptr && this->current_cal_ != 1) {
 | 
			
		||||
    this->current_calibration_number_->make_call().set_value(0).perform();
 | 
			
		||||
  }
 | 
			
		||||
  if (this->voltage_calibration_number_ != nullptr && this->voltage_cal_ != 1) {
 | 
			
		||||
    this->voltage_calibration_number_->make_call().set_value(0).perform();
 | 
			
		||||
  }
 | 
			
		||||
  if (this->power_calibration_number_ != nullptr && this->power_cal_ != 1) {
 | 
			
		||||
    this->power_calibration_number_->make_call().set_value(0).perform();
 | 
			
		||||
  }
 | 
			
		||||
  if (this->energy_calibration_number_ != nullptr && this->energy_cal_ != 1) {
 | 
			
		||||
    this->energy_calibration_number_->make_call().set_value(0).perform();
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
  ESP_LOGD(TAG, "external calibration values restored to initial state");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BL0940::current_calibration_callback_(float state) {
 | 
			
		||||
  this->current_cal_ = this->calculate_calibration_value_(state);
 | 
			
		||||
  ESP_LOGV(TAG, "update current calibration state: %f", this->current_cal_);
 | 
			
		||||
  this->recalibrate_();
 | 
			
		||||
}
 | 
			
		||||
void BL0940::voltage_calibration_callback_(float state) {
 | 
			
		||||
  this->voltage_cal_ = this->calculate_calibration_value_(state);
 | 
			
		||||
  ESP_LOGV(TAG, "update voltage calibration state: %f", this->voltage_cal_);
 | 
			
		||||
  this->recalibrate_();
 | 
			
		||||
}
 | 
			
		||||
void BL0940::power_calibration_callback_(float state) {
 | 
			
		||||
  this->power_cal_ = this->calculate_calibration_value_(state);
 | 
			
		||||
  ESP_LOGV(TAG, "update power calibration state: %f", this->power_cal_);
 | 
			
		||||
  this->recalibrate_();
 | 
			
		||||
}
 | 
			
		||||
void BL0940::energy_calibration_callback_(float state) {
 | 
			
		||||
  this->energy_cal_ = this->calculate_calibration_value_(state);
 | 
			
		||||
  ESP_LOGV(TAG, "update energy calibration state: %f", this->energy_cal_);
 | 
			
		||||
  this->recalibrate_();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BL0940::recalibrate_() {
 | 
			
		||||
  ESP_LOGV(TAG, "Recalibrating reference values");
 | 
			
		||||
  this->voltage_reference_cal_ = this->voltage_reference_ / this->voltage_cal_;
 | 
			
		||||
  this->current_reference_cal_ = this->current_reference_ / this->current_cal_;
 | 
			
		||||
 | 
			
		||||
  if (this->voltage_cal_ != 1 || this->current_cal_ != 1) {
 | 
			
		||||
    this->power_reference_ = this->calculate_power_reference_();
 | 
			
		||||
  }
 | 
			
		||||
  this->power_reference_cal_ = this->power_reference_ / this->power_cal_;
 | 
			
		||||
 | 
			
		||||
  if (this->voltage_cal_ != 1 || this->current_cal_ != 1 || this->power_cal_ != 1) {
 | 
			
		||||
    this->energy_reference_ = this->calculate_energy_reference_();
 | 
			
		||||
  }
 | 
			
		||||
  this->energy_reference_cal_ = this->energy_reference_ / this->energy_cal_;
 | 
			
		||||
 | 
			
		||||
  ESP_LOGD(TAG,
 | 
			
		||||
           "Recalibrated reference values:\n"
 | 
			
		||||
           "Voltage: %f\n, Current: %f\n, Power: %f\n, Energy: %f\n",
 | 
			
		||||
           this->voltage_reference_cal_, this->current_reference_cal_, this->power_reference_cal_,
 | 
			
		||||
           this->energy_reference_cal_);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float BL0940::update_temp_(sensor::Sensor *sensor, uint16_le_t temperature) const {
 | 
			
		||||
  auto tb = (float) temperature;
 | 
			
		||||
  float converted_temp = ((float) 170 / 448) * (tb / 2 - 32) - 45;
 | 
			
		||||
  if (sensor != nullptr) {
 | 
			
		||||
    if (sensor->has_state() && std::abs(converted_temp - sensor->get_state()) > max_temperature_diff_) {
 | 
			
		||||
@@ -87,33 +201,40 @@ float BL0940::update_temp_(sensor::Sensor *sensor, ube16_t temperature) const {
 | 
			
		||||
  return converted_temp;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BL0940::received_package_(const DataPacket *data) const {
 | 
			
		||||
void BL0940::received_package_(DataPacket *data) {
 | 
			
		||||
  // Bad header
 | 
			
		||||
  if (data->frame_header != BL0940_PACKET_HEADER) {
 | 
			
		||||
    ESP_LOGI(TAG, "Invalid data. Header mismatch: %d", data->frame_header);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  float v_rms = (float) to_uint32_t(data->v_rms) / voltage_reference_;
 | 
			
		||||
  float i_rms = (float) to_uint32_t(data->i_rms) / current_reference_;
 | 
			
		||||
  float watt = (float) to_int32_t(data->watt) / power_reference_;
 | 
			
		||||
  uint32_t cf_cnt = to_uint32_t(data->cf_cnt);
 | 
			
		||||
  float total_energy_consumption = (float) cf_cnt / energy_reference_;
 | 
			
		||||
  // cf_cnt is only 24 bits, so track overflows
 | 
			
		||||
  uint32_t cf_cnt = (uint24_t) data->cf_cnt;
 | 
			
		||||
  cf_cnt |= this->prev_cf_cnt_ & 0xff000000;
 | 
			
		||||
  if (cf_cnt < this->prev_cf_cnt_) {
 | 
			
		||||
    cf_cnt += 0x1000000;
 | 
			
		||||
  }
 | 
			
		||||
  this->prev_cf_cnt_ = cf_cnt;
 | 
			
		||||
 | 
			
		||||
  float tps1 = update_temp_(internal_temperature_sensor_, data->tps1);
 | 
			
		||||
  float tps2 = update_temp_(external_temperature_sensor_, data->tps2);
 | 
			
		||||
  float v_rms = (uint24_t) data->v_rms / this->voltage_reference_cal_;
 | 
			
		||||
  float i_rms = (uint24_t) data->i_rms / this->current_reference_cal_;
 | 
			
		||||
  float watt = (int24_t) data->watt / this->power_reference_cal_;
 | 
			
		||||
  float total_energy_consumption = cf_cnt / this->energy_reference_cal_;
 | 
			
		||||
 | 
			
		||||
  if (voltage_sensor_ != nullptr) {
 | 
			
		||||
    voltage_sensor_->publish_state(v_rms);
 | 
			
		||||
  float tps1 = update_temp_(this->internal_temperature_sensor_, data->tps1);
 | 
			
		||||
  float tps2 = update_temp_(this->external_temperature_sensor_, data->tps2);
 | 
			
		||||
 | 
			
		||||
  if (this->voltage_sensor_ != nullptr) {
 | 
			
		||||
    this->voltage_sensor_->publish_state(v_rms);
 | 
			
		||||
  }
 | 
			
		||||
  if (current_sensor_ != nullptr) {
 | 
			
		||||
    current_sensor_->publish_state(i_rms);
 | 
			
		||||
  if (this->current_sensor_ != nullptr) {
 | 
			
		||||
    this->current_sensor_->publish_state(i_rms);
 | 
			
		||||
  }
 | 
			
		||||
  if (power_sensor_ != nullptr) {
 | 
			
		||||
    power_sensor_->publish_state(watt);
 | 
			
		||||
  if (this->power_sensor_ != nullptr) {
 | 
			
		||||
    this->power_sensor_->publish_state(watt);
 | 
			
		||||
  }
 | 
			
		||||
  if (energy_sensor_ != nullptr) {
 | 
			
		||||
    energy_sensor_->publish_state(total_energy_consumption);
 | 
			
		||||
  if (this->energy_sensor_ != nullptr) {
 | 
			
		||||
    this->energy_sensor_->publish_state(total_energy_consumption);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ESP_LOGV(TAG, "BL0940: U %fV, I %fA, P %fW, Cnt %" PRId32 ", ∫P %fkWh, T1 %f°C, T2 %f°C", v_rms, i_rms, watt, cf_cnt,
 | 
			
		||||
@@ -121,7 +242,27 @@ void BL0940::received_package_(const DataPacket *data) const {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BL0940::dump_config() {  // NOLINT(readability-function-cognitive-complexity)
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "BL0940:");
 | 
			
		||||
  ESP_LOGCONFIG(TAG,
 | 
			
		||||
                "BL0940:\n"
 | 
			
		||||
                "  LEGACY MODE: %s\n"
 | 
			
		||||
                "  READ  CMD: 0x%02X\n"
 | 
			
		||||
                "  WRITE CMD: 0x%02X\n"
 | 
			
		||||
                "  ------------------\n"
 | 
			
		||||
                "  Current reference: %f\n"
 | 
			
		||||
                "  Energy reference: %f\n"
 | 
			
		||||
                "  Power reference: %f\n"
 | 
			
		||||
                "  Voltage reference: %f\n",
 | 
			
		||||
                TRUEFALSE(this->legacy_mode_enabled_), this->read_command_, this->write_command_,
 | 
			
		||||
                this->current_reference_, this->energy_reference_, this->power_reference_, this->voltage_reference_);
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
  ESP_LOGCONFIG(TAG,
 | 
			
		||||
                "BL0940:\n"
 | 
			
		||||
                "  Current calibration: %f\n"
 | 
			
		||||
                "  Energy calibration: %f\n"
 | 
			
		||||
                "  Power calibration: %f\n"
 | 
			
		||||
                "  Voltage calibration: %f\n",
 | 
			
		||||
                this->current_cal_, this->energy_cal_, this->power_cal_, this->voltage_cal_);
 | 
			
		||||
#endif
 | 
			
		||||
  LOG_SENSOR("", "Voltage", this->voltage_sensor_);
 | 
			
		||||
  LOG_SENSOR("", "Current", this->current_sensor_);
 | 
			
		||||
  LOG_SENSOR("", "Power", this->power_sensor_);
 | 
			
		||||
@@ -130,9 +271,5 @@ void BL0940::dump_config() {  // NOLINT(readability-function-cognitive-complexit
 | 
			
		||||
  LOG_SENSOR("", "External temperature", this->external_temperature_sensor_);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint32_t BL0940::to_uint32_t(ube24_t input) { return input.h << 16 | input.m << 8 | input.l; }
 | 
			
		||||
 | 
			
		||||
int32_t BL0940::to_int32_t(sbe24_t input) { return input.h << 16 | input.m << 8 | input.l; }
 | 
			
		||||
 | 
			
		||||
}  // namespace bl0940
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
@@ -1,66 +1,48 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/components/uart/uart.h"
 | 
			
		||||
#include "esphome/core/datatypes.h"
 | 
			
		||||
#include "esphome/core/defines.h"
 | 
			
		||||
#ifdef USE_BUTTON
 | 
			
		||||
#include "esphome/components/button/button.h"
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
#include "esphome/components/number/number.h"
 | 
			
		||||
#endif
 | 
			
		||||
#include "esphome/components/sensor/sensor.h"
 | 
			
		||||
#include "esphome/components/uart/uart.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace bl0940 {
 | 
			
		||||
 | 
			
		||||
static const float BL0940_PREF = 1430;
 | 
			
		||||
static const float BL0940_UREF = 33000;
 | 
			
		||||
static const float BL0940_IREF = 275000;  // 2750 from tasmota. Seems to generate values 100 times too high
 | 
			
		||||
 | 
			
		||||
// Measured to 297J  per click according to power consumption of 5 minutes
 | 
			
		||||
// Converted to kWh (3.6MJ per kwH). Used to be 256 * 1638.4
 | 
			
		||||
static const float BL0940_EREF = 3.6e6 / 297;
 | 
			
		||||
 | 
			
		||||
struct ube24_t {  // NOLINT(readability-identifier-naming,altera-struct-pack-align)
 | 
			
		||||
  uint8_t l;
 | 
			
		||||
  uint8_t m;
 | 
			
		||||
  uint8_t h;
 | 
			
		||||
} __attribute__((packed));
 | 
			
		||||
 | 
			
		||||
struct ube16_t {  // NOLINT(readability-identifier-naming,altera-struct-pack-align)
 | 
			
		||||
  uint8_t l;
 | 
			
		||||
  uint8_t h;
 | 
			
		||||
} __attribute__((packed));
 | 
			
		||||
 | 
			
		||||
struct sbe24_t {  // NOLINT(readability-identifier-naming,altera-struct-pack-align)
 | 
			
		||||
  uint8_t l;
 | 
			
		||||
  uint8_t m;
 | 
			
		||||
  int8_t h;
 | 
			
		||||
} __attribute__((packed));
 | 
			
		||||
 | 
			
		||||
// Caveat: All these values are big endian (low - middle - high)
 | 
			
		||||
 | 
			
		||||
union DataPacket {  // NOLINT(altera-struct-pack-align)
 | 
			
		||||
  uint8_t raw[35];
 | 
			
		||||
  struct {
 | 
			
		||||
    uint8_t frame_header;  // value of 0x58 according to docs. 0x55 according to Tasmota real world tests. Reality wins.
 | 
			
		||||
    ube24_t i_fast_rms;    // 0x00
 | 
			
		||||
    ube24_t i_rms;         // 0x04
 | 
			
		||||
    ube24_t RESERVED0;     // reserved
 | 
			
		||||
    ube24_t v_rms;         // 0x06
 | 
			
		||||
    ube24_t RESERVED1;     // reserved
 | 
			
		||||
    sbe24_t watt;          // 0x08
 | 
			
		||||
    ube24_t RESERVED2;     // reserved
 | 
			
		||||
    ube24_t cf_cnt;        // 0x0A
 | 
			
		||||
    ube24_t RESERVED3;     // reserved
 | 
			
		||||
    ube16_t tps1;          // 0x0c
 | 
			
		||||
    uint8_t RESERVED4;     // value of 0x00
 | 
			
		||||
    ube16_t tps2;          // 0x0c
 | 
			
		||||
    uint8_t RESERVED5;     // value of 0x00
 | 
			
		||||
    uint8_t checksum;      // checksum
 | 
			
		||||
  };
 | 
			
		||||
struct DataPacket {
 | 
			
		||||
  uint8_t frame_header;    // Packet header (0x58 in EN docs, 0x55 in CN docs and Tasmota tests)
 | 
			
		||||
  uint24_le_t i_fast_rms;  // Fast RMS current
 | 
			
		||||
  uint24_le_t i_rms;       // RMS current
 | 
			
		||||
  uint24_t RESERVED0;      // Reserved
 | 
			
		||||
  uint24_le_t v_rms;       // RMS voltage
 | 
			
		||||
  uint24_t RESERVED1;      // Reserved
 | 
			
		||||
  int24_le_t watt;         // Active power (can be negative for bidirectional measurement)
 | 
			
		||||
  uint24_t RESERVED2;      // Reserved
 | 
			
		||||
  uint24_le_t cf_cnt;      // Energy pulse count
 | 
			
		||||
  uint24_t RESERVED3;      // Reserved
 | 
			
		||||
  uint16_le_t tps1;        // Internal temperature sensor 1
 | 
			
		||||
  uint8_t RESERVED4;       // Reserved (should be 0x00)
 | 
			
		||||
  uint16_le_t tps2;        // Internal temperature sensor 2
 | 
			
		||||
  uint8_t RESERVED5;       // Reserved (should be 0x00)
 | 
			
		||||
  uint8_t checksum;        // Packet checksum
 | 
			
		||||
} __attribute__((packed));
 | 
			
		||||
 | 
			
		||||
class BL0940 : public PollingComponent, public uart::UARTDevice {
 | 
			
		||||
 public:
 | 
			
		||||
  // Sensor setters
 | 
			
		||||
  void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
 | 
			
		||||
  void set_current_sensor(sensor::Sensor *current_sensor) { current_sensor_ = current_sensor; }
 | 
			
		||||
  void set_power_sensor(sensor::Sensor *power_sensor) { power_sensor_ = power_sensor; }
 | 
			
		||||
  void set_energy_sensor(sensor::Sensor *energy_sensor) { energy_sensor_ = energy_sensor; }
 | 
			
		||||
 | 
			
		||||
  // Temperature sensor setters
 | 
			
		||||
  void set_internal_temperature_sensor(sensor::Sensor *internal_temperature_sensor) {
 | 
			
		||||
    internal_temperature_sensor_ = internal_temperature_sensor;
 | 
			
		||||
  }
 | 
			
		||||
@@ -68,42 +50,105 @@ class BL0940 : public PollingComponent, public uart::UARTDevice {
 | 
			
		||||
    external_temperature_sensor_ = external_temperature_sensor;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  void loop() override;
 | 
			
		||||
  // Configuration setters
 | 
			
		||||
  void set_legacy_mode(bool enable) { this->legacy_mode_enabled_ = enable; }
 | 
			
		||||
  void set_read_command(uint8_t read_command) { this->read_command_ = read_command; }
 | 
			
		||||
  void set_write_command(uint8_t write_command) { this->write_command_ = write_command; }
 | 
			
		||||
 | 
			
		||||
  // Reference value setters (used for calibration and conversion)
 | 
			
		||||
  void set_current_reference(float current_ref) { this->current_reference_ = current_ref; }
 | 
			
		||||
  void set_energy_reference(float energy_ref) { this->energy_reference_ = energy_ref; }
 | 
			
		||||
  void set_power_reference(float power_ref) { this->power_reference_ = power_ref; }
 | 
			
		||||
  void set_voltage_reference(float voltage_ref) { this->voltage_reference_ = voltage_ref; }
 | 
			
		||||
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
  // Calibration number setters (for Home Assistant number entities)
 | 
			
		||||
  void set_current_calibration_number(number::Number *num) { this->current_calibration_number_ = num; }
 | 
			
		||||
  void set_voltage_calibration_number(number::Number *num) { this->voltage_calibration_number_ = num; }
 | 
			
		||||
  void set_power_calibration_number(number::Number *num) { this->power_calibration_number_ = num; }
 | 
			
		||||
  void set_energy_calibration_number(number::Number *num) { this->energy_calibration_number_ = num; }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef USE_BUTTON
 | 
			
		||||
  // Resets all calibration values to defaults (can be triggered by a button)
 | 
			
		||||
  void reset_calibration();
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  // Core component methods
 | 
			
		||||
  void loop() override;
 | 
			
		||||
  void update() override;
 | 
			
		||||
  void setup() override;
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  sensor::Sensor *voltage_sensor_{nullptr};
 | 
			
		||||
  sensor::Sensor *current_sensor_{nullptr};
 | 
			
		||||
  // NB This may be negative as the circuits is seemingly able to measure
 | 
			
		||||
  // power in both directions
 | 
			
		||||
  sensor::Sensor *power_sensor_{nullptr};
 | 
			
		||||
  sensor::Sensor *energy_sensor_{nullptr};
 | 
			
		||||
  sensor::Sensor *internal_temperature_sensor_{nullptr};
 | 
			
		||||
  sensor::Sensor *external_temperature_sensor_{nullptr};
 | 
			
		||||
  // --- Sensor pointers ---
 | 
			
		||||
  sensor::Sensor *voltage_sensor_{nullptr};               // Voltage sensor
 | 
			
		||||
  sensor::Sensor *current_sensor_{nullptr};               // Current sensor
 | 
			
		||||
  sensor::Sensor *power_sensor_{nullptr};                 // Power sensor (can be negative for bidirectional)
 | 
			
		||||
  sensor::Sensor *energy_sensor_{nullptr};                // Energy sensor
 | 
			
		||||
  sensor::Sensor *internal_temperature_sensor_{nullptr};  // Internal temperature sensor
 | 
			
		||||
  sensor::Sensor *external_temperature_sensor_{nullptr};  // External temperature sensor
 | 
			
		||||
 | 
			
		||||
  // Max difference between two measurements of the temperature. Used to avoid noise.
 | 
			
		||||
  float max_temperature_diff_{0};
 | 
			
		||||
  // Divide by this to turn into Watt
 | 
			
		||||
  float power_reference_ = BL0940_PREF;
 | 
			
		||||
  // Divide by this to turn into Volt
 | 
			
		||||
  float voltage_reference_ = BL0940_UREF;
 | 
			
		||||
  // Divide by this to turn into Ampere
 | 
			
		||||
  float current_reference_ = BL0940_IREF;
 | 
			
		||||
  // Divide by this to turn into kWh
 | 
			
		||||
  float energy_reference_ = BL0940_EREF;
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
  // --- Calibration number entities (for dynamic calibration via HA UI) ---
 | 
			
		||||
  number::Number *voltage_calibration_number_{nullptr};
 | 
			
		||||
  number::Number *current_calibration_number_{nullptr};
 | 
			
		||||
  number::Number *power_calibration_number_{nullptr};
 | 
			
		||||
  number::Number *energy_calibration_number_{nullptr};
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  float update_temp_(sensor::Sensor *sensor, ube16_t packed_temperature) const;
 | 
			
		||||
  // --- Internal state ---
 | 
			
		||||
  uint32_t prev_cf_cnt_ = 0;       // Previous energy pulse count (for energy calculation)
 | 
			
		||||
  float max_temperature_diff_{0};  // Max allowed temperature difference between two measurements (noise filter)
 | 
			
		||||
 | 
			
		||||
  static uint32_t to_uint32_t(ube24_t input);
 | 
			
		||||
  // --- Reference values for conversion ---
 | 
			
		||||
  float power_reference_;        // Divider for raw power to get Watts
 | 
			
		||||
  float power_reference_cal_;    // Calibrated power reference
 | 
			
		||||
  float voltage_reference_;      // Divider for raw voltage to get Volts
 | 
			
		||||
  float voltage_reference_cal_;  // Calibrated voltage reference
 | 
			
		||||
  float current_reference_;      // Divider for raw current to get Amperes
 | 
			
		||||
  float current_reference_cal_;  // Calibrated current reference
 | 
			
		||||
  float energy_reference_;       // Divider for raw energy to get kWh
 | 
			
		||||
  float energy_reference_cal_;   // Calibrated energy reference
 | 
			
		||||
 | 
			
		||||
  static int32_t to_int32_t(sbe24_t input);
 | 
			
		||||
  // --- Home Assistant calibration values (multipliers, default 1) ---
 | 
			
		||||
  float current_cal_{1};
 | 
			
		||||
  float voltage_cal_{1};
 | 
			
		||||
  float power_cal_{1};
 | 
			
		||||
  float energy_cal_{1};
 | 
			
		||||
 | 
			
		||||
  static bool validate_checksum(const DataPacket *data);
 | 
			
		||||
  // --- Protocol commands ---
 | 
			
		||||
  uint8_t read_command_;
 | 
			
		||||
  uint8_t write_command_;
 | 
			
		||||
 | 
			
		||||
  void received_package_(const DataPacket *data) const;
 | 
			
		||||
  // --- Mode flags ---
 | 
			
		||||
  bool legacy_mode_enabled_ = true;
 | 
			
		||||
 | 
			
		||||
  // --- Methods ---
 | 
			
		||||
  // Converts packed temperature value to float and updates the sensor
 | 
			
		||||
  float update_temp_(sensor::Sensor *sensor, uint16_le_t packed_temperature) const;
 | 
			
		||||
 | 
			
		||||
  // Validates the checksum of a received data packet
 | 
			
		||||
  bool validate_checksum_(DataPacket *data);
 | 
			
		||||
 | 
			
		||||
  // Handles a received data packet
 | 
			
		||||
  void received_package_(DataPacket *data);
 | 
			
		||||
 | 
			
		||||
  // Calculates reference values for calibration and conversion
 | 
			
		||||
  float calculate_energy_reference_();
 | 
			
		||||
  float calculate_power_reference_();
 | 
			
		||||
  float calculate_calibration_value_(float state);
 | 
			
		||||
 | 
			
		||||
  // Calibration update callbacks (used with number entities)
 | 
			
		||||
  void current_calibration_callback_(float state);
 | 
			
		||||
  void voltage_calibration_callback_(float state);
 | 
			
		||||
  void power_calibration_callback_(float state);
 | 
			
		||||
  void energy_calibration_callback_(float state);
 | 
			
		||||
  void reset_calibration_callback_();
 | 
			
		||||
 | 
			
		||||
  // Recalculates all reference values after calibration changes
 | 
			
		||||
  void recalibrate_();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace bl0940
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										27
									
								
								esphome/components/bl0940/button/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								esphome/components/bl0940/button/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
from esphome.components import button
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.const import ENTITY_CATEGORY_CONFIG, ICON_RESTART
 | 
			
		||||
 | 
			
		||||
from .. import CONF_BL0940_ID, bl0940_ns
 | 
			
		||||
from ..sensor import BL0940
 | 
			
		||||
 | 
			
		||||
CalibrationResetButton = bl0940_ns.class_(
 | 
			
		||||
    "CalibrationResetButton", button.Button, cg.Component
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = cv.All(
 | 
			
		||||
    button.button_schema(
 | 
			
		||||
        CalibrationResetButton,
 | 
			
		||||
        entity_category=ENTITY_CATEGORY_CONFIG,
 | 
			
		||||
        icon=ICON_RESTART,
 | 
			
		||||
    )
 | 
			
		||||
    .extend({cv.GenerateID(CONF_BL0940_ID): cv.use_id(BL0940)})
 | 
			
		||||
    .extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    var = await button.new_button(config)
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
    await cg.register_parented(var, config[CONF_BL0940_ID])
 | 
			
		||||
@@ -0,0 +1,20 @@
 | 
			
		||||
#include "calibration_reset_button.h"
 | 
			
		||||
#include "../bl0940.h"
 | 
			
		||||
#include "esphome/core/hal.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
#include "esphome/core/application.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace bl0940 {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "bl0940.button.calibration_reset";
 | 
			
		||||
 | 
			
		||||
void CalibrationResetButton::dump_config() { LOG_BUTTON("", "Calibration Reset Button", this); }
 | 
			
		||||
 | 
			
		||||
void CalibrationResetButton::press_action() {
 | 
			
		||||
  ESP_LOGI(TAG, "Resetting calibration defaults...");
 | 
			
		||||
  this->parent_->reset_calibration();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace bl0940
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										19
									
								
								esphome/components/bl0940/button/calibration_reset_button.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								esphome/components/bl0940/button/calibration_reset_button.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/components/button/button.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace bl0940 {
 | 
			
		||||
 | 
			
		||||
class BL0940;  // Forward declaration of BL0940 class
 | 
			
		||||
 | 
			
		||||
class CalibrationResetButton : public button::Button, public Component, public Parented<BL0940> {
 | 
			
		||||
 public:
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
 | 
			
		||||
  void press_action() override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace bl0940
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										94
									
								
								esphome/components/bl0940/number/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								esphome/components/bl0940/number/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,94 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
from esphome.components import number
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_MAX_VALUE,
 | 
			
		||||
    CONF_MIN_VALUE,
 | 
			
		||||
    CONF_MODE,
 | 
			
		||||
    CONF_RESTORE_VALUE,
 | 
			
		||||
    CONF_STEP,
 | 
			
		||||
    ENTITY_CATEGORY_CONFIG,
 | 
			
		||||
    UNIT_PERCENT,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
from .. import CONF_BL0940_ID, bl0940_ns
 | 
			
		||||
from ..sensor import BL0940
 | 
			
		||||
 | 
			
		||||
# Define calibration types
 | 
			
		||||
CONF_CURRENT_CALIBRATION = "current_calibration"
 | 
			
		||||
CONF_VOLTAGE_CALIBRATION = "voltage_calibration"
 | 
			
		||||
CONF_POWER_CALIBRATION = "power_calibration"
 | 
			
		||||
CONF_ENERGY_CALIBRATION = "energy_calibration"
 | 
			
		||||
 | 
			
		||||
BL0940Number = bl0940_ns.class_("BL0940Number")
 | 
			
		||||
 | 
			
		||||
CalibrationNumber = bl0940_ns.class_(
 | 
			
		||||
    "CalibrationNumber", number.Number, cg.PollingComponent
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def validate_min_max(config):
 | 
			
		||||
    if config[CONF_MAX_VALUE] <= config[CONF_MIN_VALUE]:
 | 
			
		||||
        raise cv.Invalid("max_value must be greater than min_value")
 | 
			
		||||
    return config
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
CALIBRATION_SCHEMA = cv.All(
 | 
			
		||||
    number.number_schema(
 | 
			
		||||
        CalibrationNumber,
 | 
			
		||||
        entity_category=ENTITY_CATEGORY_CONFIG,
 | 
			
		||||
        unit_of_measurement=UNIT_PERCENT,
 | 
			
		||||
    )
 | 
			
		||||
    .extend(
 | 
			
		||||
        {
 | 
			
		||||
            cv.Optional(CONF_MODE, default="BOX"): cv.enum(number.NUMBER_MODES),
 | 
			
		||||
            cv.Optional(CONF_MAX_VALUE, default=10): cv.All(
 | 
			
		||||
                cv.float_, cv.Range(max=50)
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_MIN_VALUE, default=-10): cv.All(
 | 
			
		||||
                cv.float_, cv.Range(min=-50)
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_STEP, default=0.1): cv.positive_float,
 | 
			
		||||
            cv.Optional(CONF_RESTORE_VALUE): cv.boolean,
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
    .extend(cv.COMPONENT_SCHEMA),
 | 
			
		||||
    validate_min_max,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
# Configuration schema for BL0940 numbers
 | 
			
		||||
CONFIG_SCHEMA = cv.Schema(
 | 
			
		||||
    {
 | 
			
		||||
        cv.GenerateID(): cv.declare_id(BL0940Number),
 | 
			
		||||
        cv.GenerateID(CONF_BL0940_ID): cv.use_id(BL0940),
 | 
			
		||||
        cv.Optional(CONF_CURRENT_CALIBRATION): CALIBRATION_SCHEMA,
 | 
			
		||||
        cv.Optional(CONF_VOLTAGE_CALIBRATION): CALIBRATION_SCHEMA,
 | 
			
		||||
        cv.Optional(CONF_POWER_CALIBRATION): CALIBRATION_SCHEMA,
 | 
			
		||||
        cv.Optional(CONF_ENERGY_CALIBRATION): CALIBRATION_SCHEMA,
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    # Get the BL0940 component instance
 | 
			
		||||
    bl0940 = await cg.get_variable(config[CONF_BL0940_ID])
 | 
			
		||||
 | 
			
		||||
    # Process all calibration types
 | 
			
		||||
    for cal_type, setter_method in [
 | 
			
		||||
        (CONF_CURRENT_CALIBRATION, "set_current_calibration_number"),
 | 
			
		||||
        (CONF_VOLTAGE_CALIBRATION, "set_voltage_calibration_number"),
 | 
			
		||||
        (CONF_POWER_CALIBRATION, "set_power_calibration_number"),
 | 
			
		||||
        (CONF_ENERGY_CALIBRATION, "set_energy_calibration_number"),
 | 
			
		||||
    ]:
 | 
			
		||||
        if conf := config.get(cal_type):
 | 
			
		||||
            var = await number.new_number(
 | 
			
		||||
                conf,
 | 
			
		||||
                min_value=conf.get(CONF_MIN_VALUE),
 | 
			
		||||
                max_value=conf.get(CONF_MAX_VALUE),
 | 
			
		||||
                step=conf.get(CONF_STEP),
 | 
			
		||||
            )
 | 
			
		||||
            await cg.register_component(var, conf)
 | 
			
		||||
 | 
			
		||||
            if restore_value := config.get(CONF_RESTORE_VALUE):
 | 
			
		||||
                cg.add(var.set_restore_value(restore_value))
 | 
			
		||||
            cg.add(getattr(bl0940, setter_method)(var))
 | 
			
		||||
							
								
								
									
										29
									
								
								esphome/components/bl0940/number/calibration_number.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								esphome/components/bl0940/number/calibration_number.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
			
		||||
#include "calibration_number.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace bl0940 {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "bl0940.number";
 | 
			
		||||
 | 
			
		||||
void CalibrationNumber::setup() {
 | 
			
		||||
  float value = 0.0f;
 | 
			
		||||
  if (this->restore_value_) {
 | 
			
		||||
    this->pref_ = global_preferences->make_preference<float>(this->get_object_id_hash());
 | 
			
		||||
    if (!this->pref_.load(&value)) {
 | 
			
		||||
      value = 0.0f;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  this->publish_state(value);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CalibrationNumber::control(float value) {
 | 
			
		||||
  this->publish_state(value);
 | 
			
		||||
  if (this->restore_value_)
 | 
			
		||||
    this->pref_.save(&value);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CalibrationNumber::dump_config() { LOG_NUMBER("", "Calibration Number", this); }
 | 
			
		||||
 | 
			
		||||
}  // namespace bl0940
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										26
									
								
								esphome/components/bl0940/number/calibration_number.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								esphome/components/bl0940/number/calibration_number.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/number/number.h"
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/core/preferences.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace bl0940 {
 | 
			
		||||
 | 
			
		||||
class CalibrationNumber : public number::Number, public Component {
 | 
			
		||||
 public:
 | 
			
		||||
  void setup() override;
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
  float get_setup_priority() const override { return setup_priority::HARDWARE; }
 | 
			
		||||
 | 
			
		||||
  void set_restore_value(bool restore_value) { this->restore_value_ = restore_value; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void control(float value) override;
 | 
			
		||||
  bool restore_value_{true};
 | 
			
		||||
 | 
			
		||||
  ESPPreferenceObject pref_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace bl0940
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -8,6 +8,7 @@ from esphome.const import (
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    CONF_INTERNAL_TEMPERATURE,
 | 
			
		||||
    CONF_POWER,
 | 
			
		||||
    CONF_REFERENCE_VOLTAGE,
 | 
			
		||||
    CONF_VOLTAGE,
 | 
			
		||||
    DEVICE_CLASS_CURRENT,
 | 
			
		||||
    DEVICE_CLASS_ENERGY,
 | 
			
		||||
@@ -23,12 +24,133 @@ from esphome.const import (
 | 
			
		||||
    UNIT_WATT,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
from . import bl0940_ns
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["uart"]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
bl0940_ns = cg.esphome_ns.namespace("bl0940")
 | 
			
		||||
BL0940 = bl0940_ns.class_("BL0940", cg.PollingComponent, uart.UARTDevice)
 | 
			
		||||
 | 
			
		||||
CONF_LEGACY_MODE = "legacy_mode"
 | 
			
		||||
 | 
			
		||||
CONF_READ_COMMAND = "read_command"
 | 
			
		||||
CONF_WRITE_COMMAND = "write_command"
 | 
			
		||||
 | 
			
		||||
CONF_RESISTOR_SHUNT = "resistor_shunt"
 | 
			
		||||
CONF_RESISTOR_ONE = "resistor_one"
 | 
			
		||||
CONF_RESISTOR_TWO = "resistor_two"
 | 
			
		||||
 | 
			
		||||
CONF_CURRENT_REFERENCE = "current_reference"
 | 
			
		||||
CONF_ENERGY_REFERENCE = "energy_reference"
 | 
			
		||||
CONF_POWER_REFERENCE = "power_reference"
 | 
			
		||||
CONF_VOLTAGE_REFERENCE = "voltage_reference"
 | 
			
		||||
 | 
			
		||||
DEFAULT_BL0940_READ_COMMAND = 0x58
 | 
			
		||||
DEFAULT_BL0940_WRITE_COMMAND = 0xA1
 | 
			
		||||
 | 
			
		||||
# Values according to BL0940 application note:
 | 
			
		||||
# https://www.belling.com.cn/media/file_object/bel_product/BL0940/guide/BL0940_APPNote_TSSOP14_V1.04_EN.pdf
 | 
			
		||||
DEFAULT_BL0940_VREF = 1.218  # Vref = 1.218
 | 
			
		||||
DEFAULT_BL0940_RL = 1  # RL = 1 mΩ
 | 
			
		||||
DEFAULT_BL0940_R1 = 0.51  # R1 = 0.51 kΩ
 | 
			
		||||
DEFAULT_BL0940_R2 = 1950  # R2 = 5 x 390 kΩ -> 1950 kΩ
 | 
			
		||||
 | 
			
		||||
# ----------------------------------------------------
 | 
			
		||||
# values from initial implementation
 | 
			
		||||
DEFAULT_BL0940_LEGACY_READ_COMMAND = 0x50
 | 
			
		||||
DEFAULT_BL0940_LEGACY_WRITE_COMMAND = 0xA0
 | 
			
		||||
 | 
			
		||||
DEFAULT_BL0940_LEGACY_UREF = 33000
 | 
			
		||||
DEFAULT_BL0940_LEGACY_IREF = 275000
 | 
			
		||||
DEFAULT_BL0940_LEGACY_PREF = 1430
 | 
			
		||||
# Measured to 297J  per click according to power consumption of 5 minutes
 | 
			
		||||
# Converted to kWh (3.6MJ per kwH). Used to be 256 * 1638.4
 | 
			
		||||
DEFAULT_BL0940_LEGACY_EREF = 3.6e6 / 297
 | 
			
		||||
# ----------------------------------------------------
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# methods to calculate voltage and current reference values
 | 
			
		||||
def calculate_voltage_reference(vref, r_one, r_two):
 | 
			
		||||
    # formula: 79931 / Vref * (R1 * 1000) / (R1 + R2)
 | 
			
		||||
    return 79931 / vref * (r_one * 1000) / (r_one + r_two)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def calculate_current_reference(vref, r_shunt):
 | 
			
		||||
    # formula: 324004 * RL / Vref
 | 
			
		||||
    return 324004 * r_shunt / vref
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def calculate_power_reference(voltage_reference, current_reference):
 | 
			
		||||
    # calculate power reference based on voltage and current reference
 | 
			
		||||
    return voltage_reference * current_reference * 4046 / 324004 / 79931
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def calculate_energy_reference(power_reference):
 | 
			
		||||
    # formula: power_reference * 3600000 / (1638.4 * 256)
 | 
			
		||||
    return power_reference * 3600000 / (1638.4 * 256)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def validate_legacy_mode(config):
 | 
			
		||||
    # Only allow schematic calibration options if legacy_mode is False
 | 
			
		||||
    if config.get(CONF_LEGACY_MODE, True):
 | 
			
		||||
        forbidden = [
 | 
			
		||||
            CONF_REFERENCE_VOLTAGE,
 | 
			
		||||
            CONF_RESISTOR_SHUNT,
 | 
			
		||||
            CONF_RESISTOR_ONE,
 | 
			
		||||
            CONF_RESISTOR_TWO,
 | 
			
		||||
        ]
 | 
			
		||||
        for key in forbidden:
 | 
			
		||||
            if key in config:
 | 
			
		||||
                raise cv.Invalid(
 | 
			
		||||
                    f"Option '{key}' is only allowed when legacy_mode: false"
 | 
			
		||||
                )
 | 
			
		||||
    return config
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def set_command_defaults(config):
 | 
			
		||||
    # Set defaults for read_command and write_command based on legacy_mode
 | 
			
		||||
    legacy = config.get(CONF_LEGACY_MODE, True)
 | 
			
		||||
    if legacy:
 | 
			
		||||
        config.setdefault(CONF_READ_COMMAND, DEFAULT_BL0940_LEGACY_READ_COMMAND)
 | 
			
		||||
        config.setdefault(CONF_WRITE_COMMAND, DEFAULT_BL0940_LEGACY_WRITE_COMMAND)
 | 
			
		||||
    else:
 | 
			
		||||
        config.setdefault(CONF_READ_COMMAND, DEFAULT_BL0940_READ_COMMAND)
 | 
			
		||||
        config.setdefault(CONF_WRITE_COMMAND, DEFAULT_BL0940_WRITE_COMMAND)
 | 
			
		||||
    return config
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def set_reference_values(config):
 | 
			
		||||
    # Set default reference values based on legacy_mode
 | 
			
		||||
    if config.get(CONF_LEGACY_MODE, True):
 | 
			
		||||
        config.setdefault(CONF_VOLTAGE_REFERENCE, DEFAULT_BL0940_LEGACY_UREF)
 | 
			
		||||
        config.setdefault(CONF_CURRENT_REFERENCE, DEFAULT_BL0940_LEGACY_IREF)
 | 
			
		||||
        config.setdefault(CONF_POWER_REFERENCE, DEFAULT_BL0940_LEGACY_PREF)
 | 
			
		||||
        config.setdefault(CONF_ENERGY_REFERENCE, DEFAULT_BL0940_LEGACY_PREF)
 | 
			
		||||
    else:
 | 
			
		||||
        vref = config.get(CONF_VOLTAGE_REFERENCE, DEFAULT_BL0940_VREF)
 | 
			
		||||
        r_one = config.get(CONF_RESISTOR_ONE, DEFAULT_BL0940_R1)
 | 
			
		||||
        r_two = config.get(CONF_RESISTOR_TWO, DEFAULT_BL0940_R2)
 | 
			
		||||
        r_shunt = config.get(CONF_RESISTOR_SHUNT, DEFAULT_BL0940_RL)
 | 
			
		||||
 | 
			
		||||
        config.setdefault(
 | 
			
		||||
            CONF_VOLTAGE_REFERENCE, calculate_voltage_reference(vref, r_one, r_two)
 | 
			
		||||
        )
 | 
			
		||||
        config.setdefault(
 | 
			
		||||
            CONF_CURRENT_REFERENCE, calculate_current_reference(vref, r_shunt)
 | 
			
		||||
        )
 | 
			
		||||
        config.setdefault(
 | 
			
		||||
            CONF_POWER_REFERENCE,
 | 
			
		||||
            calculate_power_reference(
 | 
			
		||||
                config.get(CONF_VOLTAGE_REFERENCE), config.get(CONF_CURRENT_REFERENCE)
 | 
			
		||||
            ),
 | 
			
		||||
        )
 | 
			
		||||
        config.setdefault(
 | 
			
		||||
            CONF_ENERGY_REFERENCE,
 | 
			
		||||
            calculate_energy_reference(config.get(CONF_POWER_REFERENCE)),
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    return config
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = (
 | 
			
		||||
    cv.Schema(
 | 
			
		||||
        {
 | 
			
		||||
@@ -69,10 +191,24 @@ CONFIG_SCHEMA = (
 | 
			
		||||
                device_class=DEVICE_CLASS_TEMPERATURE,
 | 
			
		||||
                state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_LEGACY_MODE, default=True): cv.boolean,
 | 
			
		||||
            cv.Optional(CONF_READ_COMMAND): cv.hex_uint8_t,
 | 
			
		||||
            cv.Optional(CONF_WRITE_COMMAND): cv.hex_uint8_t,
 | 
			
		||||
            cv.Optional(CONF_REFERENCE_VOLTAGE): cv.float_,
 | 
			
		||||
            cv.Optional(CONF_RESISTOR_SHUNT): cv.float_,
 | 
			
		||||
            cv.Optional(CONF_RESISTOR_ONE): cv.float_,
 | 
			
		||||
            cv.Optional(CONF_RESISTOR_TWO): cv.float_,
 | 
			
		||||
            cv.Optional(CONF_CURRENT_REFERENCE): cv.float_,
 | 
			
		||||
            cv.Optional(CONF_ENERGY_REFERENCE): cv.float_,
 | 
			
		||||
            cv.Optional(CONF_POWER_REFERENCE): cv.float_,
 | 
			
		||||
            cv.Optional(CONF_VOLTAGE_REFERENCE): cv.float_,
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
    .extend(cv.polling_component_schema("60s"))
 | 
			
		||||
    .extend(uart.UART_DEVICE_SCHEMA)
 | 
			
		||||
    .add_extra(validate_legacy_mode)
 | 
			
		||||
    .add_extra(set_command_defaults)
 | 
			
		||||
    .add_extra(set_reference_values)
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -99,3 +235,16 @@ async def to_code(config):
 | 
			
		||||
    if external_temperature_config := config.get(CONF_EXTERNAL_TEMPERATURE):
 | 
			
		||||
        sens = await sensor.new_sensor(external_temperature_config)
 | 
			
		||||
        cg.add(var.set_external_temperature_sensor(sens))
 | 
			
		||||
 | 
			
		||||
    # enable legacy mode
 | 
			
		||||
    cg.add(var.set_legacy_mode(config.get(CONF_LEGACY_MODE)))
 | 
			
		||||
 | 
			
		||||
    # Set bl0940 commands after validator has determined which defaults to use if not set
 | 
			
		||||
    cg.add(var.set_read_command(config.get(CONF_READ_COMMAND)))
 | 
			
		||||
    cg.add(var.set_write_command(config.get(CONF_WRITE_COMMAND)))
 | 
			
		||||
 | 
			
		||||
    # Set reference values after validator has set the values either from defaults or calculated
 | 
			
		||||
    cg.add(var.set_current_reference(config.get(CONF_CURRENT_REFERENCE)))
 | 
			
		||||
    cg.add(var.set_voltage_reference(config.get(CONF_VOLTAGE_REFERENCE)))
 | 
			
		||||
    cg.add(var.set_power_reference(config.get(CONF_POWER_REFERENCE)))
 | 
			
		||||
    cg.add(var.set_energy_reference(config.get(CONF_ENERGY_REFERENCE)))
 | 
			
		||||
 
 | 
			
		||||
@@ -149,7 +149,7 @@ void BL0942::setup() {
 | 
			
		||||
  this->write_reg_(BL0942_REG_USR_WRPROT, 0);
 | 
			
		||||
 | 
			
		||||
  if (this->read_reg_(BL0942_REG_MODE) != mode)
 | 
			
		||||
    this->status_set_warning("BL0942 setup failed!");
 | 
			
		||||
    this->status_set_warning(LOG_STR("BL0942 setup failed!"));
 | 
			
		||||
 | 
			
		||||
  this->flush();
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -286,6 +286,7 @@ async def remove_bond_to_code(config, action_id, template_arg, args):
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    # Register the loggers this component needs
 | 
			
		||||
    esp32_ble.register_bt_logger(BTLoggers.GATT, BTLoggers.SMP)
 | 
			
		||||
    cg.add_define("USE_ESP32_BLE_UUID")
 | 
			
		||||
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
 
 | 
			
		||||
@@ -27,7 +27,7 @@ CONFIG_SCHEMA = cv.All(
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def to_code(config):
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    if len(config[CONF_SERVICE_UUID]) == len(esp32_ble_tracker.bt_uuid16_format):
 | 
			
		||||
        cg.add(
 | 
			
		||||
@@ -63,6 +63,6 @@ def to_code(config):
 | 
			
		||||
        )
 | 
			
		||||
        cg.add(var.set_char_uuid128(uuid128))
 | 
			
		||||
    cg.add(var.set_require_response(config[CONF_REQUIRE_RESPONSE]))
 | 
			
		||||
    yield output.register_output(var, config)
 | 
			
		||||
    yield ble_client.register_ble_node(var, config)
 | 
			
		||||
    yield cg.register_component(var, config)
 | 
			
		||||
    await output.register_output(var, config)
 | 
			
		||||
    await ble_client.register_ble_node(var, config)
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
 
 | 
			
		||||
@@ -80,7 +80,7 @@ CONFIG_SCHEMA = cv.All(
 | 
			
		||||
        cv.Schema(
 | 
			
		||||
            {
 | 
			
		||||
                cv.GenerateID(): cv.declare_id(BluetoothProxy),
 | 
			
		||||
                cv.Optional(CONF_ACTIVE, default=False): cv.boolean,
 | 
			
		||||
                cv.Optional(CONF_ACTIVE, default=True): cv.boolean,
 | 
			
		||||
                cv.SplitDefault(CONF_CACHE_SERVICES, esp32_idf=True): cv.All(
 | 
			
		||||
                    cv.only_with_esp_idf, cv.boolean
 | 
			
		||||
                ),
 | 
			
		||||
@@ -118,6 +118,12 @@ async def to_code(config):
 | 
			
		||||
    connection_count = len(config.get(CONF_CONNECTIONS, []))
 | 
			
		||||
    cg.add_define("BLUETOOTH_PROXY_MAX_CONNECTIONS", connection_count)
 | 
			
		||||
 | 
			
		||||
    # Define batch size for BLE advertisements
 | 
			
		||||
    # Each advertisement is up to 80 bytes when packaged (including protocol overhead)
 | 
			
		||||
    # 16 advertisements × 80 bytes (worst case) = 1280 bytes out of ~1320 bytes usable payload
 | 
			
		||||
    # This achieves ~97% WiFi MTU utilization while staying under the limit
 | 
			
		||||
    cg.add_define("BLUETOOTH_PROXY_ADVERTISEMENT_BATCH_SIZE", 16)
 | 
			
		||||
 | 
			
		||||
    for connection_conf in config.get(CONF_CONNECTIONS, []):
 | 
			
		||||
        connection_var = cg.new_Pvariable(connection_conf[CONF_ID])
 | 
			
		||||
        await cg.register_component(connection_var, connection_conf)
 | 
			
		||||
 
 | 
			
		||||
@@ -12,16 +12,30 @@ namespace esphome::bluetooth_proxy {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "bluetooth_proxy.connection";
 | 
			
		||||
 | 
			
		||||
// This function is allocation-free and directly packs UUIDs into the output array
 | 
			
		||||
// using precalculated constants for the Bluetooth base UUID
 | 
			
		||||
static void fill_128bit_uuid_array(std::array<uint64_t, 2> &out, esp_bt_uuid_t uuid_source) {
 | 
			
		||||
  esp_bt_uuid_t uuid = espbt::ESPBTUUID::from_uuid(uuid_source).as_128bit().get_uuid();
 | 
			
		||||
  out[0] = ((uint64_t) uuid.uuid.uuid128[15] << 56) | ((uint64_t) uuid.uuid.uuid128[14] << 48) |
 | 
			
		||||
           ((uint64_t) uuid.uuid.uuid128[13] << 40) | ((uint64_t) uuid.uuid.uuid128[12] << 32) |
 | 
			
		||||
           ((uint64_t) uuid.uuid.uuid128[11] << 24) | ((uint64_t) uuid.uuid.uuid128[10] << 16) |
 | 
			
		||||
           ((uint64_t) uuid.uuid.uuid128[9] << 8) | ((uint64_t) uuid.uuid.uuid128[8]);
 | 
			
		||||
  out[1] = ((uint64_t) uuid.uuid.uuid128[7] << 56) | ((uint64_t) uuid.uuid.uuid128[6] << 48) |
 | 
			
		||||
           ((uint64_t) uuid.uuid.uuid128[5] << 40) | ((uint64_t) uuid.uuid.uuid128[4] << 32) |
 | 
			
		||||
           ((uint64_t) uuid.uuid.uuid128[3] << 24) | ((uint64_t) uuid.uuid.uuid128[2] << 16) |
 | 
			
		||||
           ((uint64_t) uuid.uuid.uuid128[1] << 8) | ((uint64_t) uuid.uuid.uuid128[0]);
 | 
			
		||||
  // Bluetooth base UUID: 00000000-0000-1000-8000-00805F9B34FB
 | 
			
		||||
  // out[0] = bytes 8-15 (big-endian)
 | 
			
		||||
  // - For 128-bit UUIDs: use bytes 8-15 as-is
 | 
			
		||||
  // - For 16/32-bit UUIDs: insert into bytes 12-15, use 0x00001000 for bytes 8-11
 | 
			
		||||
  out[0] = uuid_source.len == ESP_UUID_LEN_128
 | 
			
		||||
               ? (((uint64_t) uuid_source.uuid.uuid128[15] << 56) | ((uint64_t) uuid_source.uuid.uuid128[14] << 48) |
 | 
			
		||||
                  ((uint64_t) uuid_source.uuid.uuid128[13] << 40) | ((uint64_t) uuid_source.uuid.uuid128[12] << 32) |
 | 
			
		||||
                  ((uint64_t) uuid_source.uuid.uuid128[11] << 24) | ((uint64_t) uuid_source.uuid.uuid128[10] << 16) |
 | 
			
		||||
                  ((uint64_t) uuid_source.uuid.uuid128[9] << 8) | ((uint64_t) uuid_source.uuid.uuid128[8]))
 | 
			
		||||
               : (((uint64_t) (uuid_source.len == ESP_UUID_LEN_16 ? uuid_source.uuid.uuid16 : uuid_source.uuid.uuid32)
 | 
			
		||||
                   << 32) |
 | 
			
		||||
                  0x00001000ULL);  // Base UUID bytes 8-11
 | 
			
		||||
  // out[1] = bytes 0-7 (big-endian)
 | 
			
		||||
  // - For 128-bit UUIDs: use bytes 0-7 as-is
 | 
			
		||||
  // - For 16/32-bit UUIDs: use precalculated base UUID constant
 | 
			
		||||
  out[1] = uuid_source.len == ESP_UUID_LEN_128
 | 
			
		||||
               ? ((uint64_t) uuid_source.uuid.uuid128[7] << 56) | ((uint64_t) uuid_source.uuid.uuid128[6] << 48) |
 | 
			
		||||
                     ((uint64_t) uuid_source.uuid.uuid128[5] << 40) | ((uint64_t) uuid_source.uuid.uuid128[4] << 32) |
 | 
			
		||||
                     ((uint64_t) uuid_source.uuid.uuid128[3] << 24) | ((uint64_t) uuid_source.uuid.uuid128[2] << 16) |
 | 
			
		||||
                     ((uint64_t) uuid_source.uuid.uuid128[1] << 8) | ((uint64_t) uuid_source.uuid.uuid128[0])
 | 
			
		||||
               : 0x800000805F9B34FBULL;  // Base UUID bytes 0-7: 80-00-00-80-5F-9B-34-FB
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Helper to fill UUID in the appropriate format based on client support and UUID type
 | 
			
		||||
@@ -80,9 +94,11 @@ void BluetoothConnection::dump_config() {
 | 
			
		||||
 | 
			
		||||
void BluetoothConnection::update_allocated_slot_(uint64_t find_value, uint64_t set_value) {
 | 
			
		||||
  auto &allocated = this->proxy_->connections_free_response_.allocated;
 | 
			
		||||
  auto *it = std::find(allocated.begin(), allocated.end(), find_value);
 | 
			
		||||
  if (it != allocated.end()) {
 | 
			
		||||
    *it = set_value;
 | 
			
		||||
  for (auto &slot : allocated) {
 | 
			
		||||
    if (slot == find_value) {
 | 
			
		||||
      slot = set_value;
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -105,13 +121,24 @@ void BluetoothConnection::set_address(uint64_t address) {
 | 
			
		||||
void BluetoothConnection::loop() {
 | 
			
		||||
  BLEClientBase::loop();
 | 
			
		||||
 | 
			
		||||
  // Early return if no active connection or not in service discovery phase
 | 
			
		||||
  if (this->address_ == 0 || this->send_service_ < 0 || this->send_service_ > this->service_count_) {
 | 
			
		||||
  // Early return if no active connection
 | 
			
		||||
  if (this->address_ == 0) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Handle service discovery
 | 
			
		||||
  this->send_service_for_discovery_();
 | 
			
		||||
  // Handle service discovery if in valid range
 | 
			
		||||
  if (this->send_service_ >= 0 && this->send_service_ <= this->service_count_) {
 | 
			
		||||
    this->send_service_for_discovery_();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Check if we should disable the loop
 | 
			
		||||
  // - For V3_WITH_CACHE: Services are never sent, disable after INIT state
 | 
			
		||||
  // - For V3_WITHOUT_CACHE: Disable only after service discovery is complete
 | 
			
		||||
  //   (send_service_ == DONE_SENDING_SERVICES, which is only set after services are sent)
 | 
			
		||||
  if (this->state_ != espbt::ClientState::INIT && (this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE ||
 | 
			
		||||
                                                   this->send_service_ == DONE_SENDING_SERVICES)) {
 | 
			
		||||
    this->disable_loop();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothConnection::reset_connection_(esp_err_t reason) {
 | 
			
		||||
@@ -125,7 +152,7 @@ void BluetoothConnection::reset_connection_(esp_err_t reason) {
 | 
			
		||||
  // to detect incomplete service discovery rather than relying on us to
 | 
			
		||||
  // tell them about a partial list.
 | 
			
		||||
  this->set_address(0);
 | 
			
		||||
  this->send_service_ = DONE_SENDING_SERVICES;
 | 
			
		||||
  this->send_service_ = INIT_SENDING_SERVICES;
 | 
			
		||||
  this->proxy_->send_connections_free();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -133,10 +160,7 @@ void BluetoothConnection::send_service_for_discovery_() {
 | 
			
		||||
  if (this->send_service_ >= this->service_count_) {
 | 
			
		||||
    this->send_service_ = DONE_SENDING_SERVICES;
 | 
			
		||||
    this->proxy_->send_gatt_services_done(this->address_);
 | 
			
		||||
    if (this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE ||
 | 
			
		||||
        this->connection_type_ == espbt::ConnectionType::V3_WITHOUT_CACHE) {
 | 
			
		||||
      this->release_services();
 | 
			
		||||
    }
 | 
			
		||||
    this->release_services();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -185,8 +209,7 @@ void BluetoothConnection::send_service_for_discovery_() {
 | 
			
		||||
                                     service_result.start_handle, service_result.end_handle, 0, &total_char_count);
 | 
			
		||||
 | 
			
		||||
    if (char_count_status != ESP_GATT_OK) {
 | 
			
		||||
      ESP_LOGE(TAG, "[%d] [%s] Error getting characteristic count, status=%d", this->connection_index_,
 | 
			
		||||
               this->address_str().c_str(), char_count_status);
 | 
			
		||||
      this->log_connection_error_("esp_ble_gattc_get_attr_count", char_count_status);
 | 
			
		||||
      this->send_service_ = DONE_SENDING_SERVICES;
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
@@ -220,8 +243,7 @@ void BluetoothConnection::send_service_for_discovery_() {
 | 
			
		||||
          break;
 | 
			
		||||
        }
 | 
			
		||||
        if (char_status != ESP_GATT_OK) {
 | 
			
		||||
          ESP_LOGE(TAG, "[%d] [%s] esp_ble_gattc_get_all_char error, status=%d", this->connection_index_,
 | 
			
		||||
                   this->address_str().c_str(), char_status);
 | 
			
		||||
          this->log_connection_error_("esp_ble_gattc_get_all_char", char_status);
 | 
			
		||||
          this->send_service_ = DONE_SENDING_SERVICES;
 | 
			
		||||
          return;
 | 
			
		||||
        }
 | 
			
		||||
@@ -244,8 +266,7 @@ void BluetoothConnection::send_service_for_discovery_() {
 | 
			
		||||
            this->gattc_if_, this->conn_id_, ESP_GATT_DB_DESCRIPTOR, 0, 0, char_result.char_handle, &total_desc_count);
 | 
			
		||||
 | 
			
		||||
        if (desc_count_status != ESP_GATT_OK) {
 | 
			
		||||
          ESP_LOGE(TAG, "[%d] [%s] Error getting descriptor count for char handle %d, status=%d",
 | 
			
		||||
                   this->connection_index_, this->address_str().c_str(), char_result.char_handle, desc_count_status);
 | 
			
		||||
          this->log_connection_error_("esp_ble_gattc_get_attr_count", desc_count_status);
 | 
			
		||||
          this->send_service_ = DONE_SENDING_SERVICES;
 | 
			
		||||
          return;
 | 
			
		||||
        }
 | 
			
		||||
@@ -266,8 +287,7 @@ void BluetoothConnection::send_service_for_discovery_() {
 | 
			
		||||
            break;
 | 
			
		||||
          }
 | 
			
		||||
          if (desc_status != ESP_GATT_OK) {
 | 
			
		||||
            ESP_LOGE(TAG, "[%d] [%s] esp_ble_gattc_get_all_descr error, status=%d", this->connection_index_,
 | 
			
		||||
                     this->address_str().c_str(), desc_status);
 | 
			
		||||
            this->log_connection_error_("esp_ble_gattc_get_all_descr", desc_status);
 | 
			
		||||
            this->send_service_ = DONE_SENDING_SERVICES;
 | 
			
		||||
            return;
 | 
			
		||||
          }
 | 
			
		||||
@@ -321,6 +341,33 @@ void BluetoothConnection::send_service_for_discovery_() {
 | 
			
		||||
  api_conn->send_message(resp, api::BluetoothGATTGetServicesResponse::MESSAGE_TYPE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothConnection::log_connection_error_(const char *operation, esp_gatt_status_t status) {
 | 
			
		||||
  ESP_LOGE(TAG, "[%d] [%s] %s error, status=%d", this->connection_index_, this->address_str().c_str(), operation,
 | 
			
		||||
           status);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothConnection::log_connection_warning_(const char *operation, esp_err_t err) {
 | 
			
		||||
  ESP_LOGW(TAG, "[%d] [%s] %s failed, err=%d", this->connection_index_, this->address_str().c_str(), operation, err);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothConnection::log_gatt_not_connected_(const char *action, const char *type) {
 | 
			
		||||
  ESP_LOGW(TAG, "[%d] [%s] Cannot %s GATT %s, not connected.", this->connection_index_, this->address_str().c_str(),
 | 
			
		||||
           action, type);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothConnection::log_gatt_operation_error_(const char *operation, uint16_t handle, esp_gatt_status_t status) {
 | 
			
		||||
  ESP_LOGW(TAG, "[%d] [%s] Error %s for handle 0x%2X, status=%d", this->connection_index_, this->address_str().c_str(),
 | 
			
		||||
           operation, handle, status);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
esp_err_t BluetoothConnection::check_and_log_error_(const char *operation, esp_err_t err) {
 | 
			
		||||
  if (err != ESP_OK) {
 | 
			
		||||
    this->log_connection_warning_(operation, err);
 | 
			
		||||
    return err;
 | 
			
		||||
  }
 | 
			
		||||
  return ESP_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
 | 
			
		||||
                                              esp_ble_gattc_cb_param_t *param) {
 | 
			
		||||
  if (!BLEClientBase::gattc_event_handler(event, gattc_if, param))
 | 
			
		||||
@@ -328,10 +375,19 @@ bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga
 | 
			
		||||
 | 
			
		||||
  switch (event) {
 | 
			
		||||
    case ESP_GATTC_DISCONNECT_EVT: {
 | 
			
		||||
      this->reset_connection_(param->disconnect.reason);
 | 
			
		||||
      // Don't reset connection yet - wait for CLOSE_EVT to ensure controller has freed resources
 | 
			
		||||
      // This prevents race condition where we mark slot as free before controller cleanup is complete
 | 
			
		||||
      ESP_LOGD(TAG, "[%d] [%s] Disconnect, reason=0x%02x", this->connection_index_, this->address_str_.c_str(),
 | 
			
		||||
               param->disconnect.reason);
 | 
			
		||||
      // Send disconnection notification but don't free the slot yet
 | 
			
		||||
      this->proxy_->send_device_connection(this->address_, false, 0, param->disconnect.reason);
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_CLOSE_EVT: {
 | 
			
		||||
      ESP_LOGD(TAG, "[%d] [%s] Close, reason=0x%02x, freeing slot", this->connection_index_, this->address_str_.c_str(),
 | 
			
		||||
               param->close.reason);
 | 
			
		||||
      // Now the GATT connection is fully closed and controller resources are freed
 | 
			
		||||
      // Safe to mark the connection slot as available
 | 
			
		||||
      this->reset_connection_(param->close.reason);
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
@@ -361,8 +417,7 @@ bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga
 | 
			
		||||
    case ESP_GATTC_READ_DESCR_EVT:
 | 
			
		||||
    case ESP_GATTC_READ_CHAR_EVT: {
 | 
			
		||||
      if (param->read.status != ESP_GATT_OK) {
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Error reading char/descriptor at handle 0x%2X, status=%d", this->connection_index_,
 | 
			
		||||
                 this->address_str_.c_str(), param->read.handle, param->read.status);
 | 
			
		||||
        this->log_gatt_operation_error_("reading char/descriptor", param->read.handle, param->read.status);
 | 
			
		||||
        this->proxy_->send_gatt_error(this->address_, param->read.handle, param->read.status);
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
@@ -376,8 +431,7 @@ bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga
 | 
			
		||||
    case ESP_GATTC_WRITE_CHAR_EVT:
 | 
			
		||||
    case ESP_GATTC_WRITE_DESCR_EVT: {
 | 
			
		||||
      if (param->write.status != ESP_GATT_OK) {
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Error writing char/descriptor at handle 0x%2X, status=%d", this->connection_index_,
 | 
			
		||||
                 this->address_str_.c_str(), param->write.handle, param->write.status);
 | 
			
		||||
        this->log_gatt_operation_error_("writing char/descriptor", param->write.handle, param->write.status);
 | 
			
		||||
        this->proxy_->send_gatt_error(this->address_, param->write.handle, param->write.status);
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
@@ -389,9 +443,8 @@ bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_UNREG_FOR_NOTIFY_EVT: {
 | 
			
		||||
      if (param->unreg_for_notify.status != ESP_GATT_OK) {
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Error unregistering notifications for handle 0x%2X, status=%d",
 | 
			
		||||
                 this->connection_index_, this->address_str_.c_str(), param->unreg_for_notify.handle,
 | 
			
		||||
                 param->unreg_for_notify.status);
 | 
			
		||||
        this->log_gatt_operation_error_("unregistering notifications", param->unreg_for_notify.handle,
 | 
			
		||||
                                        param->unreg_for_notify.status);
 | 
			
		||||
        this->proxy_->send_gatt_error(this->address_, param->unreg_for_notify.handle, param->unreg_for_notify.status);
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
@@ -403,8 +456,8 @@ bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
 | 
			
		||||
      if (param->reg_for_notify.status != ESP_GATT_OK) {
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Error registering notifications for handle 0x%2X, status=%d", this->connection_index_,
 | 
			
		||||
                 this->address_str_.c_str(), param->reg_for_notify.handle, param->reg_for_notify.status);
 | 
			
		||||
        this->log_gatt_operation_error_("registering notifications", param->reg_for_notify.handle,
 | 
			
		||||
                                        param->reg_for_notify.status);
 | 
			
		||||
        this->proxy_->send_gatt_error(this->address_, param->reg_for_notify.handle, param->reg_for_notify.status);
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
@@ -450,8 +503,7 @@ void BluetoothConnection::gap_event_handler(esp_gap_ble_cb_event_t event, esp_bl
 | 
			
		||||
 | 
			
		||||
esp_err_t BluetoothConnection::read_characteristic(uint16_t handle) {
 | 
			
		||||
  if (!this->connected()) {
 | 
			
		||||
    ESP_LOGW(TAG, "[%d] [%s] Cannot read GATT characteristic, not connected.", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str());
 | 
			
		||||
    this->log_gatt_not_connected_("read", "characteristic");
 | 
			
		||||
    return ESP_GATT_NOT_CONNECTED;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -459,18 +511,12 @@ esp_err_t BluetoothConnection::read_characteristic(uint16_t handle) {
 | 
			
		||||
           handle);
 | 
			
		||||
 | 
			
		||||
  esp_err_t err = esp_ble_gattc_read_char(this->gattc_if_, this->conn_id_, handle, ESP_GATT_AUTH_REQ_NONE);
 | 
			
		||||
  if (err != ERR_OK) {
 | 
			
		||||
    ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_read_char error, err=%d", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str(), err);
 | 
			
		||||
    return err;
 | 
			
		||||
  }
 | 
			
		||||
  return ESP_OK;
 | 
			
		||||
  return this->check_and_log_error_("esp_ble_gattc_read_char", err);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
esp_err_t BluetoothConnection::write_characteristic(uint16_t handle, const std::string &data, bool response) {
 | 
			
		||||
  if (!this->connected()) {
 | 
			
		||||
    ESP_LOGW(TAG, "[%d] [%s] Cannot write GATT characteristic, not connected.", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str());
 | 
			
		||||
    this->log_gatt_not_connected_("write", "characteristic");
 | 
			
		||||
    return ESP_GATT_NOT_CONNECTED;
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGV(TAG, "[%d] [%s] Writing GATT characteristic handle %d", this->connection_index_, this->address_str_.c_str(),
 | 
			
		||||
@@ -479,36 +525,24 @@ esp_err_t BluetoothConnection::write_characteristic(uint16_t handle, const std::
 | 
			
		||||
  esp_err_t err =
 | 
			
		||||
      esp_ble_gattc_write_char(this->gattc_if_, this->conn_id_, handle, data.size(), (uint8_t *) data.data(),
 | 
			
		||||
                               response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
 | 
			
		||||
  if (err != ERR_OK) {
 | 
			
		||||
    ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_write_char error, err=%d", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str(), err);
 | 
			
		||||
    return err;
 | 
			
		||||
  }
 | 
			
		||||
  return ESP_OK;
 | 
			
		||||
  return this->check_and_log_error_("esp_ble_gattc_write_char", err);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
esp_err_t BluetoothConnection::read_descriptor(uint16_t handle) {
 | 
			
		||||
  if (!this->connected()) {
 | 
			
		||||
    ESP_LOGW(TAG, "[%d] [%s] Cannot read GATT descriptor, not connected.", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str());
 | 
			
		||||
    this->log_gatt_not_connected_("read", "descriptor");
 | 
			
		||||
    return ESP_GATT_NOT_CONNECTED;
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGV(TAG, "[%d] [%s] Reading GATT descriptor handle %d", this->connection_index_, this->address_str_.c_str(),
 | 
			
		||||
           handle);
 | 
			
		||||
 | 
			
		||||
  esp_err_t err = esp_ble_gattc_read_char_descr(this->gattc_if_, this->conn_id_, handle, ESP_GATT_AUTH_REQ_NONE);
 | 
			
		||||
  if (err != ERR_OK) {
 | 
			
		||||
    ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_read_char_descr error, err=%d", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str(), err);
 | 
			
		||||
    return err;
 | 
			
		||||
  }
 | 
			
		||||
  return ESP_OK;
 | 
			
		||||
  return this->check_and_log_error_("esp_ble_gattc_read_char_descr", err);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
esp_err_t BluetoothConnection::write_descriptor(uint16_t handle, const std::string &data, bool response) {
 | 
			
		||||
  if (!this->connected()) {
 | 
			
		||||
    ESP_LOGW(TAG, "[%d] [%s] Cannot write GATT descriptor, not connected.", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str());
 | 
			
		||||
    this->log_gatt_not_connected_("write", "descriptor");
 | 
			
		||||
    return ESP_GATT_NOT_CONNECTED;
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGV(TAG, "[%d] [%s] Writing GATT descriptor handle %d", this->connection_index_, this->address_str_.c_str(),
 | 
			
		||||
@@ -517,18 +551,12 @@ esp_err_t BluetoothConnection::write_descriptor(uint16_t handle, const std::stri
 | 
			
		||||
  esp_err_t err = esp_ble_gattc_write_char_descr(
 | 
			
		||||
      this->gattc_if_, this->conn_id_, handle, data.size(), (uint8_t *) data.data(),
 | 
			
		||||
      response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
 | 
			
		||||
  if (err != ERR_OK) {
 | 
			
		||||
    ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_write_char_descr error, err=%d", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str(), err);
 | 
			
		||||
    return err;
 | 
			
		||||
  }
 | 
			
		||||
  return ESP_OK;
 | 
			
		||||
  return this->check_and_log_error_("esp_ble_gattc_write_char_descr", err);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
esp_err_t BluetoothConnection::notify_characteristic(uint16_t handle, bool enable) {
 | 
			
		||||
  if (!this->connected()) {
 | 
			
		||||
    ESP_LOGW(TAG, "[%d] [%s] Cannot notify GATT characteristic, not connected.", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str());
 | 
			
		||||
    this->log_gatt_not_connected_("notify", "characteristic");
 | 
			
		||||
    return ESP_GATT_NOT_CONNECTED;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -536,22 +564,13 @@ esp_err_t BluetoothConnection::notify_characteristic(uint16_t handle, bool enabl
 | 
			
		||||
    ESP_LOGV(TAG, "[%d] [%s] Registering for GATT characteristic notifications handle %d", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str(), handle);
 | 
			
		||||
    esp_err_t err = esp_ble_gattc_register_for_notify(this->gattc_if_, this->remote_bda_, handle);
 | 
			
		||||
    if (err != ESP_OK) {
 | 
			
		||||
      ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_register_for_notify failed, err=%d", this->connection_index_,
 | 
			
		||||
               this->address_str_.c_str(), err);
 | 
			
		||||
      return err;
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGV(TAG, "[%d] [%s] Unregistering for GATT characteristic notifications handle %d", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str(), handle);
 | 
			
		||||
    esp_err_t err = esp_ble_gattc_unregister_for_notify(this->gattc_if_, this->remote_bda_, handle);
 | 
			
		||||
    if (err != ESP_OK) {
 | 
			
		||||
      ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_unregister_for_notify failed, err=%d", this->connection_index_,
 | 
			
		||||
               this->address_str_.c_str(), err);
 | 
			
		||||
      return err;
 | 
			
		||||
    }
 | 
			
		||||
    return this->check_and_log_error_("esp_ble_gattc_register_for_notify", err);
 | 
			
		||||
  }
 | 
			
		||||
  return ESP_OK;
 | 
			
		||||
 | 
			
		||||
  ESP_LOGV(TAG, "[%d] [%s] Unregistering for GATT characteristic notifications handle %d", this->connection_index_,
 | 
			
		||||
           this->address_str_.c_str(), handle);
 | 
			
		||||
  esp_err_t err = esp_ble_gattc_unregister_for_notify(this->gattc_if_, this->remote_bda_, handle);
 | 
			
		||||
  return this->check_and_log_error_("esp_ble_gattc_unregister_for_notify", err);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
esp32_ble_tracker::AdvertisementParserType BluetoothConnection::get_advertisement_parser_type() {
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,7 @@ namespace esphome::bluetooth_proxy {
 | 
			
		||||
 | 
			
		||||
class BluetoothProxy;
 | 
			
		||||
 | 
			
		||||
class BluetoothConnection : public esp32_ble_client::BLEClientBase {
 | 
			
		||||
class BluetoothConnection final : public esp32_ble_client::BLEClientBase {
 | 
			
		||||
 public:
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
  void loop() override;
 | 
			
		||||
@@ -33,13 +33,18 @@ class BluetoothConnection : public esp32_ble_client::BLEClientBase {
 | 
			
		||||
  void send_service_for_discovery_();
 | 
			
		||||
  void reset_connection_(esp_err_t reason);
 | 
			
		||||
  void update_allocated_slot_(uint64_t find_value, uint64_t set_value);
 | 
			
		||||
  void log_connection_error_(const char *operation, esp_gatt_status_t status);
 | 
			
		||||
  void log_connection_warning_(const char *operation, esp_err_t err);
 | 
			
		||||
  void log_gatt_not_connected_(const char *action, const char *type);
 | 
			
		||||
  void log_gatt_operation_error_(const char *operation, uint16_t handle, esp_gatt_status_t status);
 | 
			
		||||
  esp_err_t check_and_log_error_(const char *operation, esp_err_t err);
 | 
			
		||||
 | 
			
		||||
  // Memory optimized layout for 32-bit systems
 | 
			
		||||
  // Group 1: Pointers (4 bytes each, naturally aligned)
 | 
			
		||||
  BluetoothProxy *proxy_;
 | 
			
		||||
 | 
			
		||||
  // Group 2: 2-byte types
 | 
			
		||||
  int16_t send_service_{-2};  // Needs to handle negative values and service count
 | 
			
		||||
  int16_t send_service_{-3};  // -3 = INIT_SENDING_SERVICES, -2 = DONE_SENDING_SERVICES, >=0 = service index
 | 
			
		||||
 | 
			
		||||
  // Group 3: 1-byte types
 | 
			
		||||
  bool seen_mtu_or_services_{false};
 | 
			
		||||
 
 | 
			
		||||
@@ -11,12 +11,8 @@ namespace esphome::bluetooth_proxy {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "bluetooth_proxy";
 | 
			
		||||
 | 
			
		||||
// Batch size for BLE advertisements to maximize WiFi efficiency
 | 
			
		||||
// Each advertisement is up to 80 bytes when packaged (including protocol overhead)
 | 
			
		||||
// Most advertisements are 20-30 bytes, allowing even more to fit per packet
 | 
			
		||||
// 16 advertisements × 80 bytes (worst case) = 1280 bytes out of ~1320 bytes usable payload
 | 
			
		||||
// This achieves ~97% WiFi MTU utilization while staying under the limit
 | 
			
		||||
static constexpr size_t FLUSH_BATCH_SIZE = 16;
 | 
			
		||||
// BLUETOOTH_PROXY_ADVERTISEMENT_BATCH_SIZE is defined during code generation
 | 
			
		||||
// It sets the batch size for BLE advertisements to maximize WiFi efficiency
 | 
			
		||||
 | 
			
		||||
// Verify BLE advertisement data array size matches the BLE specification (31 bytes adv + 31 bytes scan response)
 | 
			
		||||
static_assert(sizeof(((api::BluetoothLERawAdvertisement *) nullptr)->data) == 62,
 | 
			
		||||
@@ -25,19 +21,12 @@ static_assert(sizeof(((api::BluetoothLERawAdvertisement *) nullptr)->data) == 62
 | 
			
		||||
BluetoothProxy::BluetoothProxy() { global_bluetooth_proxy = this; }
 | 
			
		||||
 | 
			
		||||
void BluetoothProxy::setup() {
 | 
			
		||||
  // Pre-allocate response object
 | 
			
		||||
  this->response_ = std::make_unique<api::BluetoothLERawAdvertisementsResponse>();
 | 
			
		||||
 | 
			
		||||
  // Reserve capacity but start with size 0
 | 
			
		||||
  // Reserve 50% since we'll grow naturally and flush at FLUSH_BATCH_SIZE
 | 
			
		||||
  this->response_->advertisements.reserve(FLUSH_BATCH_SIZE / 2);
 | 
			
		||||
 | 
			
		||||
  // Don't pre-allocate pool - let it grow only if needed in busy environments
 | 
			
		||||
  // Many devices in quiet areas will never need the overflow pool
 | 
			
		||||
 | 
			
		||||
  this->connections_free_response_.limit = BLUETOOTH_PROXY_MAX_CONNECTIONS;
 | 
			
		||||
  this->connections_free_response_.free = BLUETOOTH_PROXY_MAX_CONNECTIONS;
 | 
			
		||||
 | 
			
		||||
  // Capture the configured scan mode from YAML before any API changes
 | 
			
		||||
  this->configured_scan_active_ = this->parent_->get_scan_active();
 | 
			
		||||
 | 
			
		||||
  this->parent_->add_scanner_state_callback([this](esp32_ble_tracker::ScannerState state) {
 | 
			
		||||
    if (this->api_connection_ != nullptr) {
 | 
			
		||||
      this->send_bluetooth_scanner_state_(state);
 | 
			
		||||
@@ -50,9 +39,32 @@ void BluetoothProxy::send_bluetooth_scanner_state_(esp32_ble_tracker::ScannerSta
 | 
			
		||||
  resp.state = static_cast<api::enums::BluetoothScannerState>(state);
 | 
			
		||||
  resp.mode = this->parent_->get_scan_active() ? api::enums::BluetoothScannerMode::BLUETOOTH_SCANNER_MODE_ACTIVE
 | 
			
		||||
                                               : api::enums::BluetoothScannerMode::BLUETOOTH_SCANNER_MODE_PASSIVE;
 | 
			
		||||
  resp.configured_mode = this->configured_scan_active_
 | 
			
		||||
                             ? api::enums::BluetoothScannerMode::BLUETOOTH_SCANNER_MODE_ACTIVE
 | 
			
		||||
                             : api::enums::BluetoothScannerMode::BLUETOOTH_SCANNER_MODE_PASSIVE;
 | 
			
		||||
  this->api_connection_->send_message(resp, api::BluetoothScannerStateResponse::MESSAGE_TYPE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothProxy::log_connection_request_ignored_(BluetoothConnection *connection, espbt::ClientState state) {
 | 
			
		||||
  ESP_LOGW(TAG, "[%d] [%s] Connection request ignored, state: %s", connection->get_connection_index(),
 | 
			
		||||
           connection->address_str().c_str(), espbt::client_state_to_string(state));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothProxy::log_connection_info_(BluetoothConnection *connection, const char *message) {
 | 
			
		||||
  ESP_LOGI(TAG, "[%d] [%s] Connecting %s", connection->get_connection_index(), connection->address_str().c_str(),
 | 
			
		||||
           message);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothProxy::log_not_connected_gatt_(const char *action, const char *type) {
 | 
			
		||||
  ESP_LOGW(TAG, "Cannot %s GATT %s, not connected", action, type);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothProxy::handle_gatt_not_connected_(uint64_t address, uint16_t handle, const char *action,
 | 
			
		||||
                                                const char *type) {
 | 
			
		||||
  this->log_not_connected_gatt_(action, type);
 | 
			
		||||
  this->send_gatt_error(address, handle, ESP_GATT_NOT_CONNECTED);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32_BLE_DEVICE
 | 
			
		||||
bool BluetoothProxy::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
 | 
			
		||||
  // This method should never be called since bluetooth_proxy always uses raw advertisements
 | 
			
		||||
@@ -65,39 +77,27 @@ bool BluetoothProxy::parse_devices(const esp32_ble::BLEScanResult *scan_results,
 | 
			
		||||
  if (!api::global_api_server->is_connected() || this->api_connection_ == nullptr)
 | 
			
		||||
    return false;
 | 
			
		||||
 | 
			
		||||
  auto &advertisements = this->response_->advertisements;
 | 
			
		||||
  auto &advertisements = this->response_.advertisements;
 | 
			
		||||
 | 
			
		||||
  for (size_t i = 0; i < count; i++) {
 | 
			
		||||
    auto &result = scan_results[i];
 | 
			
		||||
    uint8_t length = result.adv_data_len + result.scan_rsp_len;
 | 
			
		||||
 | 
			
		||||
    // Check if we need to expand the vector
 | 
			
		||||
    if (this->advertisement_count_ >= advertisements.size()) {
 | 
			
		||||
      if (this->advertisement_pool_.empty()) {
 | 
			
		||||
        // No room in pool, need to allocate
 | 
			
		||||
        advertisements.emplace_back();
 | 
			
		||||
      } else {
 | 
			
		||||
        // Pull from pool
 | 
			
		||||
        advertisements.push_back(std::move(this->advertisement_pool_.back()));
 | 
			
		||||
        this->advertisement_pool_.pop_back();
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Fill in the data directly at current position
 | 
			
		||||
    auto &adv = advertisements[this->advertisement_count_];
 | 
			
		||||
    auto &adv = advertisements[this->response_.advertisements_len];
 | 
			
		||||
    adv.address = esp32_ble::ble_addr_to_uint64(result.bda);
 | 
			
		||||
    adv.rssi = result.rssi;
 | 
			
		||||
    adv.address_type = result.ble_addr_type;
 | 
			
		||||
    adv.data_len = length;
 | 
			
		||||
    std::memcpy(adv.data, result.ble_adv, length);
 | 
			
		||||
 | 
			
		||||
    this->advertisement_count_++;
 | 
			
		||||
    this->response_.advertisements_len++;
 | 
			
		||||
 | 
			
		||||
    ESP_LOGV(TAG, "Queuing raw packet from %02X:%02X:%02X:%02X:%02X:%02X, length %d. RSSI: %d dB", result.bda[0],
 | 
			
		||||
             result.bda[1], result.bda[2], result.bda[3], result.bda[4], result.bda[5], length, result.rssi);
 | 
			
		||||
 | 
			
		||||
    // Flush if we have reached FLUSH_BATCH_SIZE
 | 
			
		||||
    if (this->advertisement_count_ >= FLUSH_BATCH_SIZE) {
 | 
			
		||||
    // Flush if we have reached BLUETOOTH_PROXY_ADVERTISEMENT_BATCH_SIZE
 | 
			
		||||
    if (this->response_.advertisements_len >= BLUETOOTH_PROXY_ADVERTISEMENT_BATCH_SIZE) {
 | 
			
		||||
      this->flush_pending_advertisements();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
@@ -106,32 +106,22 @@ bool BluetoothProxy::parse_devices(const esp32_ble::BLEScanResult *scan_results,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothProxy::flush_pending_advertisements() {
 | 
			
		||||
  if (this->advertisement_count_ == 0 || !api::global_api_server->is_connected() || this->api_connection_ == nullptr)
 | 
			
		||||
  if (this->response_.advertisements_len == 0 || !api::global_api_server->is_connected() ||
 | 
			
		||||
      this->api_connection_ == nullptr)
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  auto &advertisements = this->response_->advertisements;
 | 
			
		||||
 | 
			
		||||
  // Return any items beyond advertisement_count_ to the pool
 | 
			
		||||
  if (advertisements.size() > this->advertisement_count_) {
 | 
			
		||||
    // Move unused items back to pool
 | 
			
		||||
    this->advertisement_pool_.insert(this->advertisement_pool_.end(),
 | 
			
		||||
                                     std::make_move_iterator(advertisements.begin() + this->advertisement_count_),
 | 
			
		||||
                                     std::make_move_iterator(advertisements.end()));
 | 
			
		||||
 | 
			
		||||
    // Resize to actual count
 | 
			
		||||
    advertisements.resize(this->advertisement_count_);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Send the message
 | 
			
		||||
  this->api_connection_->send_message(*this->response_, api::BluetoothLERawAdvertisementsResponse::MESSAGE_TYPE);
 | 
			
		||||
  this->api_connection_->send_message(this->response_, api::BluetoothLERawAdvertisementsResponse::MESSAGE_TYPE);
 | 
			
		||||
 | 
			
		||||
  // Reset count - existing items will be overwritten in next batch
 | 
			
		||||
  this->advertisement_count_ = 0;
 | 
			
		||||
  ESP_LOGV(TAG, "Sent batch of %u BLE advertisements", this->response_.advertisements_len);
 | 
			
		||||
 | 
			
		||||
  // Reset the length for the next batch
 | 
			
		||||
  this->response_.advertisements_len = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothProxy::dump_config() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Bluetooth Proxy:");
 | 
			
		||||
  ESP_LOGCONFIG(TAG,
 | 
			
		||||
                "Bluetooth Proxy:\n"
 | 
			
		||||
                "  Active: %s\n"
 | 
			
		||||
                "  Connections: %d",
 | 
			
		||||
                YESNO(this->active_), this->connection_count_);
 | 
			
		||||
@@ -175,7 +165,7 @@ BluetoothConnection *BluetoothProxy::get_connection_(uint64_t address, bool rese
 | 
			
		||||
  for (uint8_t i = 0; i < this->connection_count_; i++) {
 | 
			
		||||
    auto *connection = this->connections_[i];
 | 
			
		||||
    if (connection->get_address() == 0) {
 | 
			
		||||
      connection->send_service_ = DONE_SENDING_SERVICES;
 | 
			
		||||
      connection->send_service_ = INIT_SENDING_SERVICES;
 | 
			
		||||
      connection->set_address(address);
 | 
			
		||||
      // All connections must start at INIT
 | 
			
		||||
      // We only set the state if we allocate the connection
 | 
			
		||||
@@ -192,33 +182,25 @@ BluetoothConnection *BluetoothProxy::get_connection_(uint64_t address, bool rese
 | 
			
		||||
void BluetoothProxy::bluetooth_device_request(const api::BluetoothDeviceRequest &msg) {
 | 
			
		||||
  switch (msg.request_type) {
 | 
			
		||||
    case api::enums::BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT_V3_WITH_CACHE:
 | 
			
		||||
    case api::enums::BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT_V3_WITHOUT_CACHE:
 | 
			
		||||
    case api::enums::BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT: {
 | 
			
		||||
    case api::enums::BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT_V3_WITHOUT_CACHE: {
 | 
			
		||||
      auto *connection = this->get_connection_(msg.address, true);
 | 
			
		||||
      if (connection == nullptr) {
 | 
			
		||||
        ESP_LOGW(TAG, "No free connections available");
 | 
			
		||||
        this->send_device_connection(msg.address, false);
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
      if (!msg.has_address_type) {
 | 
			
		||||
        ESP_LOGE(TAG, "[%d] [%s] Missing address type in connect request", connection->get_connection_index(),
 | 
			
		||||
                 connection->address_str().c_str());
 | 
			
		||||
        this->send_device_connection(msg.address, false);
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
      if (connection->state() == espbt::ClientState::CONNECTED ||
 | 
			
		||||
          connection->state() == espbt::ClientState::ESTABLISHED) {
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Connection already established", connection->get_connection_index(),
 | 
			
		||||
                 connection->address_str().c_str());
 | 
			
		||||
        this->log_connection_request_ignored_(connection, connection->state());
 | 
			
		||||
        this->send_device_connection(msg.address, true);
 | 
			
		||||
        this->send_connections_free();
 | 
			
		||||
        return;
 | 
			
		||||
      } else if (connection->state() == espbt::ClientState::SEARCHING) {
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Connection request ignored, already searching for device",
 | 
			
		||||
                 connection->get_connection_index(), connection->address_str().c_str());
 | 
			
		||||
        return;
 | 
			
		||||
      } else if (connection->state() == espbt::ClientState::DISCOVERED) {
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Connection request ignored, device already discovered",
 | 
			
		||||
                 connection->get_connection_index(), connection->address_str().c_str());
 | 
			
		||||
        return;
 | 
			
		||||
      } else if (connection->state() == espbt::ClientState::READY_TO_CONNECT) {
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Connection request ignored, waiting in line to connect",
 | 
			
		||||
                 connection->get_connection_index(), connection->address_str().c_str());
 | 
			
		||||
        return;
 | 
			
		||||
      } else if (connection->state() == espbt::ClientState::CONNECTING) {
 | 
			
		||||
        if (connection->disconnect_pending()) {
 | 
			
		||||
          ESP_LOGW(TAG, "[%d] [%s] Connection request while pending disconnect, cancelling pending disconnect",
 | 
			
		||||
@@ -226,37 +208,22 @@ void BluetoothProxy::bluetooth_device_request(const api::BluetoothDeviceRequest
 | 
			
		||||
          connection->cancel_pending_disconnect();
 | 
			
		||||
          return;
 | 
			
		||||
        }
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Connection request ignored, already connecting", connection->get_connection_index(),
 | 
			
		||||
                 connection->address_str().c_str());
 | 
			
		||||
        return;
 | 
			
		||||
      } else if (connection->state() == espbt::ClientState::DISCONNECTING) {
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Connection request ignored, device is disconnecting",
 | 
			
		||||
                 connection->get_connection_index(), connection->address_str().c_str());
 | 
			
		||||
        this->log_connection_request_ignored_(connection, connection->state());
 | 
			
		||||
        return;
 | 
			
		||||
      } else if (connection->state() != espbt::ClientState::INIT) {
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Connection already in progress", connection->get_connection_index(),
 | 
			
		||||
                 connection->address_str().c_str());
 | 
			
		||||
        this->log_connection_request_ignored_(connection, connection->state());
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
      if (msg.request_type == api::enums::BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT_V3_WITH_CACHE) {
 | 
			
		||||
        connection->set_connection_type(espbt::ConnectionType::V3_WITH_CACHE);
 | 
			
		||||
        ESP_LOGI(TAG, "[%d] [%s] Connecting v3 with cache", connection->get_connection_index(),
 | 
			
		||||
                 connection->address_str().c_str());
 | 
			
		||||
      } else if (msg.request_type == api::enums::BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT_V3_WITHOUT_CACHE) {
 | 
			
		||||
        this->log_connection_info_(connection, "v3 with cache");
 | 
			
		||||
      } else {  // BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT_V3_WITHOUT_CACHE
 | 
			
		||||
        connection->set_connection_type(espbt::ConnectionType::V3_WITHOUT_CACHE);
 | 
			
		||||
        ESP_LOGI(TAG, "[%d] [%s] Connecting v3 without cache", connection->get_connection_index(),
 | 
			
		||||
                 connection->address_str().c_str());
 | 
			
		||||
      } else {
 | 
			
		||||
        connection->set_connection_type(espbt::ConnectionType::V1);
 | 
			
		||||
        ESP_LOGI(TAG, "[%d] [%s] Connecting v1", connection->get_connection_index(), connection->address_str().c_str());
 | 
			
		||||
      }
 | 
			
		||||
      if (msg.has_address_type) {
 | 
			
		||||
        uint64_to_bd_addr(msg.address, connection->remote_bda_);
 | 
			
		||||
        connection->set_remote_addr_type(static_cast<esp_ble_addr_type_t>(msg.address_type));
 | 
			
		||||
        connection->set_state(espbt::ClientState::DISCOVERED);
 | 
			
		||||
      } else {
 | 
			
		||||
        connection->set_state(espbt::ClientState::SEARCHING);
 | 
			
		||||
        this->log_connection_info_(connection, "v3 without cache");
 | 
			
		||||
      }
 | 
			
		||||
      uint64_to_bd_addr(msg.address, connection->remote_bda_);
 | 
			
		||||
      connection->set_remote_addr_type(static_cast<esp_ble_addr_type_t>(msg.address_type));
 | 
			
		||||
      connection->set_state(espbt::ClientState::DISCOVERED);
 | 
			
		||||
      this->send_connections_free();
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
@@ -310,14 +277,18 @@ void BluetoothProxy::bluetooth_device_request(const api::BluetoothDeviceRequest
 | 
			
		||||
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case api::enums::BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT: {
 | 
			
		||||
      ESP_LOGE(TAG, "V1 connections removed");
 | 
			
		||||
      this->send_device_connection(msg.address, false);
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothProxy::bluetooth_gatt_read(const api::BluetoothGATTReadRequest &msg) {
 | 
			
		||||
  auto *connection = this->get_connection_(msg.address, false);
 | 
			
		||||
  if (connection == nullptr) {
 | 
			
		||||
    ESP_LOGW(TAG, "Cannot read GATT characteristic, not connected");
 | 
			
		||||
    this->send_gatt_error(msg.address, msg.handle, ESP_GATT_NOT_CONNECTED);
 | 
			
		||||
    this->handle_gatt_not_connected_(msg.address, msg.handle, "read", "characteristic");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -330,8 +301,7 @@ void BluetoothProxy::bluetooth_gatt_read(const api::BluetoothGATTReadRequest &ms
 | 
			
		||||
void BluetoothProxy::bluetooth_gatt_write(const api::BluetoothGATTWriteRequest &msg) {
 | 
			
		||||
  auto *connection = this->get_connection_(msg.address, false);
 | 
			
		||||
  if (connection == nullptr) {
 | 
			
		||||
    ESP_LOGW(TAG, "Cannot write GATT characteristic, not connected");
 | 
			
		||||
    this->send_gatt_error(msg.address, msg.handle, ESP_GATT_NOT_CONNECTED);
 | 
			
		||||
    this->handle_gatt_not_connected_(msg.address, msg.handle, "write", "characteristic");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -344,8 +314,7 @@ void BluetoothProxy::bluetooth_gatt_write(const api::BluetoothGATTWriteRequest &
 | 
			
		||||
void BluetoothProxy::bluetooth_gatt_read_descriptor(const api::BluetoothGATTReadDescriptorRequest &msg) {
 | 
			
		||||
  auto *connection = this->get_connection_(msg.address, false);
 | 
			
		||||
  if (connection == nullptr) {
 | 
			
		||||
    ESP_LOGW(TAG, "Cannot read GATT descriptor, not connected");
 | 
			
		||||
    this->send_gatt_error(msg.address, msg.handle, ESP_GATT_NOT_CONNECTED);
 | 
			
		||||
    this->handle_gatt_not_connected_(msg.address, msg.handle, "read", "descriptor");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -358,8 +327,7 @@ void BluetoothProxy::bluetooth_gatt_read_descriptor(const api::BluetoothGATTRead
 | 
			
		||||
void BluetoothProxy::bluetooth_gatt_write_descriptor(const api::BluetoothGATTWriteDescriptorRequest &msg) {
 | 
			
		||||
  auto *connection = this->get_connection_(msg.address, false);
 | 
			
		||||
  if (connection == nullptr) {
 | 
			
		||||
    ESP_LOGW(TAG, "Cannot write GATT descriptor, not connected");
 | 
			
		||||
    this->send_gatt_error(msg.address, msg.handle, ESP_GATT_NOT_CONNECTED);
 | 
			
		||||
    this->handle_gatt_not_connected_(msg.address, msg.handle, "write", "descriptor");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -372,8 +340,7 @@ void BluetoothProxy::bluetooth_gatt_write_descriptor(const api::BluetoothGATTWri
 | 
			
		||||
void BluetoothProxy::bluetooth_gatt_send_services(const api::BluetoothGATTGetServicesRequest &msg) {
 | 
			
		||||
  auto *connection = this->get_connection_(msg.address, false);
 | 
			
		||||
  if (connection == nullptr || !connection->connected()) {
 | 
			
		||||
    ESP_LOGW(TAG, "Cannot get GATT services, not connected");
 | 
			
		||||
    this->send_gatt_error(msg.address, 0, ESP_GATT_NOT_CONNECTED);
 | 
			
		||||
    this->handle_gatt_not_connected_(msg.address, 0, "get", "services");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (!connection->service_count_) {
 | 
			
		||||
@@ -381,16 +348,14 @@ void BluetoothProxy::bluetooth_gatt_send_services(const api::BluetoothGATTGetSer
 | 
			
		||||
    this->send_gatt_services_done(msg.address);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (connection->send_service_ ==
 | 
			
		||||
      DONE_SENDING_SERVICES)  // Only start sending services if we're not already sending them
 | 
			
		||||
  if (connection->send_service_ == INIT_SENDING_SERVICES)  // Start sending services if not started yet
 | 
			
		||||
    connection->send_service_ = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothProxy::bluetooth_gatt_notify(const api::BluetoothGATTNotifyRequest &msg) {
 | 
			
		||||
  auto *connection = this->get_connection_(msg.address, false);
 | 
			
		||||
  if (connection == nullptr) {
 | 
			
		||||
    ESP_LOGW(TAG, "Cannot notify GATT characteristic, not connected");
 | 
			
		||||
    this->send_gatt_error(msg.address, msg.handle, ESP_GATT_NOT_CONNECTED);
 | 
			
		||||
    this->handle_gatt_not_connected_(msg.address, msg.handle, "notify", "characteristic");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -23,6 +23,7 @@ namespace esphome::bluetooth_proxy {
 | 
			
		||||
 | 
			
		||||
static const esp_err_t ESP_GATT_NOT_CONNECTED = -1;
 | 
			
		||||
static const int DONE_SENDING_SERVICES = -2;
 | 
			
		||||
static const int INIT_SENDING_SERVICES = -3;
 | 
			
		||||
 | 
			
		||||
using namespace esp32_ble_client;
 | 
			
		||||
 | 
			
		||||
@@ -49,7 +50,7 @@ enum BluetoothProxySubscriptionFlag : uint32_t {
 | 
			
		||||
  SUBSCRIPTION_RAW_ADVERTISEMENTS = 1 << 0,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class BluetoothProxy : public esp32_ble_tracker::ESPBTDeviceListener, public Component {
 | 
			
		||||
class BluetoothProxy final : public esp32_ble_tracker::ESPBTDeviceListener, public Component {
 | 
			
		||||
  friend class BluetoothConnection;  // Allow connection to update connections_free_response_
 | 
			
		||||
 public:
 | 
			
		||||
  BluetoothProxy();
 | 
			
		||||
@@ -129,13 +130,19 @@ class BluetoothProxy : public esp32_ble_tracker::ESPBTDeviceListener, public Com
 | 
			
		||||
 | 
			
		||||
  std::string get_bluetooth_mac_address_pretty() {
 | 
			
		||||
    const uint8_t *mac = esp_bt_dev_get_address();
 | 
			
		||||
    return str_snprintf("%02X:%02X:%02X:%02X:%02X:%02X", 17, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
 | 
			
		||||
    char buf[18];
 | 
			
		||||
    format_mac_addr_upper(mac, buf);
 | 
			
		||||
    return std::string(buf);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void send_bluetooth_scanner_state_(esp32_ble_tracker::ScannerState state);
 | 
			
		||||
 | 
			
		||||
  BluetoothConnection *get_connection_(uint64_t address, bool reserve);
 | 
			
		||||
  void log_connection_request_ignored_(BluetoothConnection *connection, espbt::ClientState state);
 | 
			
		||||
  void log_connection_info_(BluetoothConnection *connection, const char *message);
 | 
			
		||||
  void log_not_connected_gatt_(const char *action, const char *type);
 | 
			
		||||
  void handle_gatt_not_connected_(uint64_t address, uint16_t handle, const char *action, const char *type);
 | 
			
		||||
 | 
			
		||||
  // Memory optimized layout for 32-bit systems
 | 
			
		||||
  // Group 1: Pointers (4 bytes each, naturally aligned)
 | 
			
		||||
@@ -145,8 +152,7 @@ class BluetoothProxy : public esp32_ble_tracker::ESPBTDeviceListener, public Com
 | 
			
		||||
  std::array<BluetoothConnection *, BLUETOOTH_PROXY_MAX_CONNECTIONS> connections_{};
 | 
			
		||||
 | 
			
		||||
  // BLE advertisement batching
 | 
			
		||||
  std::vector<api::BluetoothLERawAdvertisement> advertisement_pool_;
 | 
			
		||||
  std::unique_ptr<api::BluetoothLERawAdvertisementsResponse> response_;
 | 
			
		||||
  api::BluetoothLERawAdvertisementsResponse response_;
 | 
			
		||||
 | 
			
		||||
  // Group 3: 4-byte types
 | 
			
		||||
  uint32_t last_advertisement_flush_time_{0};
 | 
			
		||||
@@ -156,8 +162,8 @@ class BluetoothProxy : public esp32_ble_tracker::ESPBTDeviceListener, public Com
 | 
			
		||||
 | 
			
		||||
  // Group 4: 1-byte types grouped together
 | 
			
		||||
  bool active_;
 | 
			
		||||
  uint8_t advertisement_count_{0};
 | 
			
		||||
  uint8_t connection_count_{0};
 | 
			
		||||
  bool configured_scan_active_{false};  // Configured scan mode from YAML
 | 
			
		||||
  // 3 bytes used, 1 byte padding
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,8 @@
 | 
			
		||||
#include <esphome/components/sensor/sensor.h>
 | 
			
		||||
#include <esphome/core/component.h>
 | 
			
		||||
 | 
			
		||||
#define BME280_ERROR_WRONG_CHIP_ID "Wrong chip ID"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace bme280_base {
 | 
			
		||||
 | 
			
		||||
@@ -98,18 +100,18 @@ void BME280Component::setup() {
 | 
			
		||||
 | 
			
		||||
  if (!this->read_byte(BME280_REGISTER_CHIPID, &chip_id)) {
 | 
			
		||||
    this->error_code_ = COMMUNICATION_FAILED;
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    this->mark_failed(ESP_LOG_MSG_COMM_FAIL);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (chip_id != 0x60) {
 | 
			
		||||
    this->error_code_ = WRONG_CHIP_ID;
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    this->mark_failed(BME280_ERROR_WRONG_CHIP_ID);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Send a soft reset.
 | 
			
		||||
  if (!this->write_byte(BME280_REGISTER_RESET, BME280_SOFT_RESET)) {
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    this->mark_failed("Reset failed");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  // Wait until the NVM data has finished loading.
 | 
			
		||||
@@ -118,14 +120,12 @@ void BME280Component::setup() {
 | 
			
		||||
  do {  // NOLINT
 | 
			
		||||
    delay(2);
 | 
			
		||||
    if (!this->read_byte(BME280_REGISTER_STATUS, &status)) {
 | 
			
		||||
      ESP_LOGW(TAG, "Error reading status register.");
 | 
			
		||||
      this->mark_failed();
 | 
			
		||||
      this->mark_failed("Error reading status register");
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
  } while ((status & BME280_STATUS_IM_UPDATE) && (--retry));
 | 
			
		||||
  if (status & BME280_STATUS_IM_UPDATE) {
 | 
			
		||||
    ESP_LOGW(TAG, "Timeout loading NVM.");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    this->mark_failed("Timeout loading NVM");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -153,26 +153,26 @@ void BME280Component::setup() {
 | 
			
		||||
 | 
			
		||||
  uint8_t humid_control_val = 0;
 | 
			
		||||
  if (!this->read_byte(BME280_REGISTER_CONTROLHUMID, &humid_control_val)) {
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    this->mark_failed("Read humidity control");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  humid_control_val &= ~0b00000111;
 | 
			
		||||
  humid_control_val |= this->humidity_oversampling_ & 0b111;
 | 
			
		||||
  if (!this->write_byte(BME280_REGISTER_CONTROLHUMID, humid_control_val)) {
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    this->mark_failed("Write humidity control");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  uint8_t config_register = 0;
 | 
			
		||||
  if (!this->read_byte(BME280_REGISTER_CONFIG, &config_register)) {
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    this->mark_failed("Read config");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  config_register &= ~0b11111100;
 | 
			
		||||
  config_register |= 0b101 << 5;  // 1000 ms standby time
 | 
			
		||||
  config_register |= (this->iir_filter_ & 0b111) << 2;
 | 
			
		||||
  if (!this->write_byte(BME280_REGISTER_CONFIG, config_register)) {
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    this->mark_failed("Write config");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -183,7 +183,7 @@ void BME280Component::dump_config() {
 | 
			
		||||
      ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
 | 
			
		||||
      break;
 | 
			
		||||
    case WRONG_CHIP_ID:
 | 
			
		||||
      ESP_LOGE(TAG, "BME280 has wrong chip ID! Is it a BME280?");
 | 
			
		||||
      ESP_LOGE(TAG, BME280_ERROR_WRONG_CHIP_ID);
 | 
			
		||||
      break;
 | 
			
		||||
    case NONE:
 | 
			
		||||
    default:
 | 
			
		||||
@@ -223,21 +223,21 @@ void BME280Component::update() {
 | 
			
		||||
  this->set_timeout("data", uint32_t(ceilf(meas_time)), [this]() {
 | 
			
		||||
    uint8_t data[8];
 | 
			
		||||
    if (!this->read_bytes(BME280_REGISTER_MEASUREMENTS, data, 8)) {
 | 
			
		||||
      ESP_LOGW(TAG, "Error reading registers.");
 | 
			
		||||
      ESP_LOGW(TAG, "Error reading registers");
 | 
			
		||||
      this->status_set_warning();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    int32_t t_fine = 0;
 | 
			
		||||
    float const temperature = this->read_temperature_(data, &t_fine);
 | 
			
		||||
    if (std::isnan(temperature)) {
 | 
			
		||||
      ESP_LOGW(TAG, "Invalid temperature, cannot read pressure & humidity values.");
 | 
			
		||||
      ESP_LOGW(TAG, "Invalid temperature");
 | 
			
		||||
      this->status_set_warning();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    float const pressure = this->read_pressure_(data, t_fine);
 | 
			
		||||
    float const humidity = this->read_humidity_(data, t_fine);
 | 
			
		||||
 | 
			
		||||
    ESP_LOGV(TAG, "Got temperature=%.1f°C pressure=%.1fhPa humidity=%.1f%%", temperature, pressure, humidity);
 | 
			
		||||
    ESP_LOGV(TAG, "Temperature=%.1f°C Pressure=%.1fhPa Humidity=%.1f%%", temperature, pressure, humidity);
 | 
			
		||||
    if (this->temperature_sensor_ != nullptr)
 | 
			
		||||
      this->temperature_sensor_->publish_state(temperature);
 | 
			
		||||
    if (this->pressure_sensor_ != nullptr)
 | 
			
		||||
 
 | 
			
		||||
@@ -28,7 +28,7 @@ const float BME680_GAS_LOOKUP_TABLE_1[16] PROGMEM = {0.0, 0.0, 0.0,  0.0,  0.0,
 | 
			
		||||
const float BME680_GAS_LOOKUP_TABLE_2[16] PROGMEM = {0.0,  0.0, 0.0, 0.0, 0.1, 0.7, 0.0, -0.8,
 | 
			
		||||
                                                     -0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
 | 
			
		||||
 | 
			
		||||
static const char *oversampling_to_str(BME680Oversampling oversampling) {
 | 
			
		||||
[[maybe_unused]] static const char *oversampling_to_str(BME680Oversampling oversampling) {
 | 
			
		||||
  switch (oversampling) {
 | 
			
		||||
    case BME680_OVERSAMPLING_NONE:
 | 
			
		||||
      return "None";
 | 
			
		||||
@@ -47,7 +47,7 @@ static const char *oversampling_to_str(BME680Oversampling oversampling) {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const char *iir_filter_to_str(BME680IIRFilter filter) {
 | 
			
		||||
[[maybe_unused]] static const char *iir_filter_to_str(BME680IIRFilter filter) {
 | 
			
		||||
  switch (filter) {
 | 
			
		||||
    case BME680_IIR_FILTER_OFF:
 | 
			
		||||
      return "OFF";
 | 
			
		||||
 
 | 
			
		||||
@@ -203,7 +203,7 @@ void BMI160Component::dump_config() {
 | 
			
		||||
i2c::ErrorCode BMI160Component::read_le_int16_(uint8_t reg, int16_t *value, uint8_t len) {
 | 
			
		||||
  uint8_t raw_data[len * 2];
 | 
			
		||||
  // read using read_register because we have little-endian data, and read_bytes_16 will swap it
 | 
			
		||||
  i2c::ErrorCode err = this->read_register(reg, raw_data, len * 2, true);
 | 
			
		||||
  i2c::ErrorCode err = this->read_register(reg, raw_data, len * 2);
 | 
			
		||||
  if (err != i2c::ERROR_OK) {
 | 
			
		||||
    return err;
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,8 @@
 | 
			
		||||
#include "esphome/core/hal.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
#define BMP280_ERROR_WRONG_CHIP_ID "Wrong chip ID"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace bmp280_base {
 | 
			
		||||
 | 
			
		||||
@@ -61,25 +63,25 @@ void BMP280Component::setup() {
 | 
			
		||||
 | 
			
		||||
  // Read the chip id twice, to work around a bug where the first read is 0.
 | 
			
		||||
  // https://community.st.com/t5/stm32-mcus-products/issue-with-reading-bmp280-chip-id-using-spi/td-p/691855
 | 
			
		||||
  if (!this->read_byte(0xD0, &chip_id)) {
 | 
			
		||||
  if (!this->bmp_read_byte(0xD0, &chip_id)) {
 | 
			
		||||
    this->error_code_ = COMMUNICATION_FAILED;
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    this->mark_failed(ESP_LOG_MSG_COMM_FAIL);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (!this->read_byte(0xD0, &chip_id)) {
 | 
			
		||||
  if (!this->bmp_read_byte(0xD0, &chip_id)) {
 | 
			
		||||
    this->error_code_ = COMMUNICATION_FAILED;
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    this->mark_failed(ESP_LOG_MSG_COMM_FAIL);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (chip_id != 0x58) {
 | 
			
		||||
    this->error_code_ = WRONG_CHIP_ID;
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    this->mark_failed(BMP280_ERROR_WRONG_CHIP_ID);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Send a soft reset.
 | 
			
		||||
  if (!this->write_byte(BMP280_REGISTER_RESET, BMP280_SOFT_RESET)) {
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
  if (!this->bmp_write_byte(BMP280_REGISTER_RESET, BMP280_SOFT_RESET)) {
 | 
			
		||||
    this->mark_failed("Reset failed");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  // Wait until the NVM data has finished loading.
 | 
			
		||||
@@ -87,15 +89,13 @@ void BMP280Component::setup() {
 | 
			
		||||
  uint8_t retry = 5;
 | 
			
		||||
  do {
 | 
			
		||||
    delay(2);
 | 
			
		||||
    if (!this->read_byte(BMP280_REGISTER_STATUS, &status)) {
 | 
			
		||||
      ESP_LOGW(TAG, "Error reading status register.");
 | 
			
		||||
      this->mark_failed();
 | 
			
		||||
    if (!this->bmp_read_byte(BMP280_REGISTER_STATUS, &status)) {
 | 
			
		||||
      this->mark_failed("Error reading status register");
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
  } while ((status & BMP280_STATUS_IM_UPDATE) && (--retry));
 | 
			
		||||
  if (status & BMP280_STATUS_IM_UPDATE) {
 | 
			
		||||
    ESP_LOGW(TAG, "Timeout loading NVM.");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    this->mark_failed("Timeout loading NVM");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -115,15 +115,15 @@ void BMP280Component::setup() {
 | 
			
		||||
  this->calibration_.p9 = this->read_s16_le_(0x9E);
 | 
			
		||||
 | 
			
		||||
  uint8_t config_register = 0;
 | 
			
		||||
  if (!this->read_byte(BMP280_REGISTER_CONFIG, &config_register)) {
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
  if (!this->bmp_read_byte(BMP280_REGISTER_CONFIG, &config_register)) {
 | 
			
		||||
    this->mark_failed("Read config");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  config_register &= ~0b11111100;
 | 
			
		||||
  config_register |= 0b000 << 5;  // 0.5 ms standby time
 | 
			
		||||
  config_register |= (this->iir_filter_ & 0b111) << 2;
 | 
			
		||||
  if (!this->write_byte(BMP280_REGISTER_CONFIG, config_register)) {
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
  if (!this->bmp_write_byte(BMP280_REGISTER_CONFIG, config_register)) {
 | 
			
		||||
    this->mark_failed("Write config");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -134,7 +134,7 @@ void BMP280Component::dump_config() {
 | 
			
		||||
      ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
 | 
			
		||||
      break;
 | 
			
		||||
    case WRONG_CHIP_ID:
 | 
			
		||||
      ESP_LOGE(TAG, "BMP280 has wrong chip ID! Is it a BME280?");
 | 
			
		||||
      ESP_LOGE(TAG, BMP280_ERROR_WRONG_CHIP_ID);
 | 
			
		||||
      break;
 | 
			
		||||
    case NONE:
 | 
			
		||||
    default:
 | 
			
		||||
@@ -159,7 +159,7 @@ void BMP280Component::update() {
 | 
			
		||||
  meas_value |= (this->temperature_oversampling_ & 0b111) << 5;
 | 
			
		||||
  meas_value |= (this->pressure_oversampling_ & 0b111) << 2;
 | 
			
		||||
  meas_value |= 0b01;  // Forced mode
 | 
			
		||||
  if (!this->write_byte(BMP280_REGISTER_CONTROL, meas_value)) {
 | 
			
		||||
  if (!this->bmp_write_byte(BMP280_REGISTER_CONTROL, meas_value)) {
 | 
			
		||||
    this->status_set_warning();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
@@ -172,13 +172,13 @@ void BMP280Component::update() {
 | 
			
		||||
    int32_t t_fine = 0;
 | 
			
		||||
    float temperature = this->read_temperature_(&t_fine);
 | 
			
		||||
    if (std::isnan(temperature)) {
 | 
			
		||||
      ESP_LOGW(TAG, "Invalid temperature, cannot read pressure values.");
 | 
			
		||||
      ESP_LOGW(TAG, "Invalid temperature");
 | 
			
		||||
      this->status_set_warning();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    float pressure = this->read_pressure_(t_fine);
 | 
			
		||||
 | 
			
		||||
    ESP_LOGD(TAG, "Got temperature=%.1f°C pressure=%.1fhPa", temperature, pressure);
 | 
			
		||||
    ESP_LOGV(TAG, "Temperature=%.1f°C Pressure=%.1fhPa", temperature, pressure);
 | 
			
		||||
    if (this->temperature_sensor_ != nullptr)
 | 
			
		||||
      this->temperature_sensor_->publish_state(temperature);
 | 
			
		||||
    if (this->pressure_sensor_ != nullptr)
 | 
			
		||||
@@ -188,9 +188,10 @@ void BMP280Component::update() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float BMP280Component::read_temperature_(int32_t *t_fine) {
 | 
			
		||||
  uint8_t data[3];
 | 
			
		||||
  if (!this->read_bytes(BMP280_REGISTER_TEMPDATA, data, 3))
 | 
			
		||||
  uint8_t data[3]{};
 | 
			
		||||
  if (!this->bmp_read_bytes(BMP280_REGISTER_TEMPDATA, data, 3))
 | 
			
		||||
    return NAN;
 | 
			
		||||
  ESP_LOGV(TAG, "Read temperature data, raw: %02X %02X %02X", data[0], data[1], data[2]);
 | 
			
		||||
  int32_t adc = ((data[0] & 0xFF) << 16) | ((data[1] & 0xFF) << 8) | (data[2] & 0xFF);
 | 
			
		||||
  adc >>= 4;
 | 
			
		||||
  if (adc == 0x80000) {
 | 
			
		||||
@@ -212,7 +213,7 @@ float BMP280Component::read_temperature_(int32_t *t_fine) {
 | 
			
		||||
 | 
			
		||||
float BMP280Component::read_pressure_(int32_t t_fine) {
 | 
			
		||||
  uint8_t data[3];
 | 
			
		||||
  if (!this->read_bytes(BMP280_REGISTER_PRESSUREDATA, data, 3))
 | 
			
		||||
  if (!this->bmp_read_bytes(BMP280_REGISTER_PRESSUREDATA, data, 3))
 | 
			
		||||
    return NAN;
 | 
			
		||||
  int32_t adc = ((data[0] & 0xFF) << 16) | ((data[1] & 0xFF) << 8) | (data[2] & 0xFF);
 | 
			
		||||
  adc >>= 4;
 | 
			
		||||
@@ -258,12 +259,12 @@ void BMP280Component::set_pressure_oversampling(BMP280Oversampling pressure_over
 | 
			
		||||
void BMP280Component::set_iir_filter(BMP280IIRFilter iir_filter) { this->iir_filter_ = iir_filter; }
 | 
			
		||||
uint8_t BMP280Component::read_u8_(uint8_t a_register) {
 | 
			
		||||
  uint8_t data = 0;
 | 
			
		||||
  this->read_byte(a_register, &data);
 | 
			
		||||
  this->bmp_read_byte(a_register, &data);
 | 
			
		||||
  return data;
 | 
			
		||||
}
 | 
			
		||||
uint16_t BMP280Component::read_u16_le_(uint8_t a_register) {
 | 
			
		||||
  uint16_t data = 0;
 | 
			
		||||
  this->read_byte_16(a_register, &data);
 | 
			
		||||
  this->bmp_read_byte_16(a_register, &data);
 | 
			
		||||
  return (data >> 8) | (data << 8);
 | 
			
		||||
}
 | 
			
		||||
int16_t BMP280Component::read_s16_le_(uint8_t a_register) { return this->read_u16_le_(a_register); }
 | 
			
		||||
 
 | 
			
		||||
@@ -67,12 +67,12 @@ class BMP280Component : public PollingComponent {
 | 
			
		||||
  float get_setup_priority() const override;
 | 
			
		||||
  void update() override;
 | 
			
		||||
 | 
			
		||||
  virtual bool read_byte(uint8_t a_register, uint8_t *data) = 0;
 | 
			
		||||
  virtual bool write_byte(uint8_t a_register, uint8_t data) = 0;
 | 
			
		||||
  virtual bool read_bytes(uint8_t a_register, uint8_t *data, size_t len) = 0;
 | 
			
		||||
  virtual bool read_byte_16(uint8_t a_register, uint16_t *data) = 0;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  virtual bool bmp_read_byte(uint8_t a_register, uint8_t *data) = 0;
 | 
			
		||||
  virtual bool bmp_write_byte(uint8_t a_register, uint8_t data) = 0;
 | 
			
		||||
  virtual bool bmp_read_bytes(uint8_t a_register, uint8_t *data, size_t len) = 0;
 | 
			
		||||
  virtual bool bmp_read_byte_16(uint8_t a_register, uint16_t *data) = 0;
 | 
			
		||||
 | 
			
		||||
  /// Read the temperature value and store the calculated ambient temperature in t_fine.
 | 
			
		||||
  float read_temperature_(int32_t *t_fine);
 | 
			
		||||
  /// Read the pressure value in hPa using the provided t_fine value.
 | 
			
		||||
 
 | 
			
		||||
@@ -5,19 +5,6 @@
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace bmp280_i2c {
 | 
			
		||||
 | 
			
		||||
bool BMP280I2CComponent::read_byte(uint8_t a_register, uint8_t *data) {
 | 
			
		||||
  return I2CDevice::read_byte(a_register, data);
 | 
			
		||||
};
 | 
			
		||||
bool BMP280I2CComponent::write_byte(uint8_t a_register, uint8_t data) {
 | 
			
		||||
  return I2CDevice::write_byte(a_register, data);
 | 
			
		||||
};
 | 
			
		||||
bool BMP280I2CComponent::read_bytes(uint8_t a_register, uint8_t *data, size_t len) {
 | 
			
		||||
  return I2CDevice::read_bytes(a_register, data, len);
 | 
			
		||||
};
 | 
			
		||||
bool BMP280I2CComponent::read_byte_16(uint8_t a_register, uint16_t *data) {
 | 
			
		||||
  return I2CDevice::read_byte_16(a_register, data);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
void BMP280I2CComponent::dump_config() {
 | 
			
		||||
  LOG_I2C_DEVICE(this);
 | 
			
		||||
  BMP280Component::dump_config();
 | 
			
		||||
 
 | 
			
		||||
@@ -11,10 +11,12 @@ static const char *const TAG = "bmp280_i2c.sensor";
 | 
			
		||||
/// This class implements support for the BMP280 Temperature+Pressure i2c sensor.
 | 
			
		||||
class BMP280I2CComponent : public esphome::bmp280_base::BMP280Component, public i2c::I2CDevice {
 | 
			
		||||
 public:
 | 
			
		||||
  bool read_byte(uint8_t a_register, uint8_t *data) override;
 | 
			
		||||
  bool write_byte(uint8_t a_register, uint8_t data) override;
 | 
			
		||||
  bool read_bytes(uint8_t a_register, uint8_t *data, size_t len) override;
 | 
			
		||||
  bool read_byte_16(uint8_t a_register, uint16_t *data) override;
 | 
			
		||||
  bool bmp_read_byte(uint8_t a_register, uint8_t *data) override { return read_byte(a_register, data); }
 | 
			
		||||
  bool bmp_write_byte(uint8_t a_register, uint8_t data) override { return write_byte(a_register, data); }
 | 
			
		||||
  bool bmp_read_bytes(uint8_t a_register, uint8_t *data, size_t len) override {
 | 
			
		||||
    return read_bytes(a_register, data, len);
 | 
			
		||||
  }
 | 
			
		||||
  bool bmp_read_byte_16(uint8_t a_register, uint16_t *data) override { return read_byte_16(a_register, data); }
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -28,7 +28,7 @@ void BMP280SPIComponent::setup() {
 | 
			
		||||
// 0x77 is transferred, for read access, the byte 0xF7 is transferred.
 | 
			
		||||
// https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp280-ds001.pdf
 | 
			
		||||
 | 
			
		||||
bool BMP280SPIComponent::read_byte(uint8_t a_register, uint8_t *data) {
 | 
			
		||||
bool BMP280SPIComponent::bmp_read_byte(uint8_t a_register, uint8_t *data) {
 | 
			
		||||
  this->enable();
 | 
			
		||||
  this->transfer_byte(set_bit(a_register, 7));
 | 
			
		||||
  *data = this->transfer_byte(0);
 | 
			
		||||
@@ -36,7 +36,7 @@ bool BMP280SPIComponent::read_byte(uint8_t a_register, uint8_t *data) {
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool BMP280SPIComponent::write_byte(uint8_t a_register, uint8_t data) {
 | 
			
		||||
bool BMP280SPIComponent::bmp_write_byte(uint8_t a_register, uint8_t data) {
 | 
			
		||||
  this->enable();
 | 
			
		||||
  this->transfer_byte(clear_bit(a_register, 7));
 | 
			
		||||
  this->transfer_byte(data);
 | 
			
		||||
@@ -44,7 +44,7 @@ bool BMP280SPIComponent::write_byte(uint8_t a_register, uint8_t data) {
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool BMP280SPIComponent::read_bytes(uint8_t a_register, uint8_t *data, size_t len) {
 | 
			
		||||
bool BMP280SPIComponent::bmp_read_bytes(uint8_t a_register, uint8_t *data, size_t len) {
 | 
			
		||||
  this->enable();
 | 
			
		||||
  this->transfer_byte(set_bit(a_register, 7));
 | 
			
		||||
  this->read_array(data, len);
 | 
			
		||||
@@ -52,7 +52,7 @@ bool BMP280SPIComponent::read_bytes(uint8_t a_register, uint8_t *data, size_t le
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool BMP280SPIComponent::read_byte_16(uint8_t a_register, uint16_t *data) {
 | 
			
		||||
bool BMP280SPIComponent::bmp_read_byte_16(uint8_t a_register, uint16_t *data) {
 | 
			
		||||
  this->enable();
 | 
			
		||||
  this->transfer_byte(set_bit(a_register, 7));
 | 
			
		||||
  ((uint8_t *) data)[1] = this->transfer_byte(0);
 | 
			
		||||
 
 | 
			
		||||
@@ -10,10 +10,10 @@ class BMP280SPIComponent : public esphome::bmp280_base::BMP280Component,
 | 
			
		||||
                           public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW,
 | 
			
		||||
                                                 spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_200KHZ> {
 | 
			
		||||
  void setup() override;
 | 
			
		||||
  bool read_byte(uint8_t a_register, uint8_t *data) override;
 | 
			
		||||
  bool write_byte(uint8_t a_register, uint8_t data) override;
 | 
			
		||||
  bool read_bytes(uint8_t a_register, uint8_t *data, size_t len) override;
 | 
			
		||||
  bool read_byte_16(uint8_t a_register, uint16_t *data) override;
 | 
			
		||||
  bool bmp_read_byte(uint8_t a_register, uint8_t *data) override;
 | 
			
		||||
  bool bmp_write_byte(uint8_t a_register, uint8_t data) override;
 | 
			
		||||
  bool bmp_read_bytes(uint8_t a_register, uint8_t *data, size_t len) override;
 | 
			
		||||
  bool bmp_read_byte_16(uint8_t a_register, uint16_t *data) override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace bmp280_spi
 | 
			
		||||
 
 | 
			
		||||
@@ -17,7 +17,7 @@ from esphome.const import (
 | 
			
		||||
    DEVICE_CLASS_RESTART,
 | 
			
		||||
    DEVICE_CLASS_UPDATE,
 | 
			
		||||
)
 | 
			
		||||
from esphome.core import CORE, coroutine_with_priority
 | 
			
		||||
from esphome.core import CORE, CoroPriority, coroutine_with_priority
 | 
			
		||||
from esphome.core.entity_helpers import entity_duplicate_validator, setup_entity
 | 
			
		||||
from esphome.cpp_generator import MockObjClass
 | 
			
		||||
 | 
			
		||||
@@ -134,6 +134,6 @@ async def button_press_to_code(config, action_id, template_arg, args):
 | 
			
		||||
    return cg.new_Pvariable(action_id, template_arg, paren)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@coroutine_with_priority(100.0)
 | 
			
		||||
@coroutine_with_priority(CoroPriority.CORE)
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    cg.add_global(button_ns.using)
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,19 @@ namespace button {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "button";
 | 
			
		||||
 | 
			
		||||
// Function implementation of LOG_BUTTON macro to reduce code size
 | 
			
		||||
void log_button(const char *tag, const char *prefix, const char *type, Button *obj) {
 | 
			
		||||
  if (obj == nullptr) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ESP_LOGCONFIG(tag, "%s%s '%s'", prefix, type, obj->get_name().c_str());
 | 
			
		||||
 | 
			
		||||
  if (!obj->get_icon_ref().empty()) {
 | 
			
		||||
    ESP_LOGCONFIG(tag, "%s  Icon: '%s'", prefix, obj->get_icon_ref().c_str());
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Button::press() {
 | 
			
		||||
  ESP_LOGD(TAG, "'%s' Pressed.", this->get_name().c_str());
 | 
			
		||||
  this->press_action();
 | 
			
		||||
 
 | 
			
		||||
@@ -7,13 +7,10 @@
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace button {
 | 
			
		||||
 | 
			
		||||
#define LOG_BUTTON(prefix, type, obj) \
 | 
			
		||||
  if ((obj) != nullptr) { \
 | 
			
		||||
    ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
 | 
			
		||||
    if (!(obj)->get_icon().empty()) { \
 | 
			
		||||
      ESP_LOGCONFIG(TAG, "%s  Icon: '%s'", prefix, (obj)->get_icon().c_str()); \
 | 
			
		||||
    } \
 | 
			
		||||
  }
 | 
			
		||||
class Button;
 | 
			
		||||
void log_button(const char *tag, const char *prefix, const char *type, Button *obj);
 | 
			
		||||
 | 
			
		||||
#define LOG_BUTTON(prefix, type, obj) log_button(TAG, prefix, LOG_STR_LITERAL(type), obj)
 | 
			
		||||
 | 
			
		||||
#define SUB_BUTTON(name) \
 | 
			
		||||
 protected: \
 | 
			
		||||
 
 | 
			
		||||
Some files were not shown because too many files have changed in this diff Show More
		Reference in New Issue
	
	Block a user