mirror of
				https://github.com/esphome/esphome.git
				synced 2025-11-04 00:51:49 +00:00 
			
		
		
		
	Compare commits
	
		
			442 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					2d22a2d1c2 | ||
| 
						 | 
					c92968da8a | ||
| 
						 | 
					86580d07cb | ||
| 
						 | 
					03ea71034f | ||
| 
						 | 
					7bf676abfa | ||
| 
						 | 
					fb16e6b027 | ||
| 
						 | 
					4eb04afa62 | ||
| 
						 | 
					841a831c63 | ||
| 
						 | 
					ae4af2966a | ||
| 
						 | 
					6ced54ea8e | ||
| 
						 | 
					e0e3489335 | ||
| 
						 | 
					cc1813f5b9 | ||
| 
						 | 
					6eb3c65445 | ||
| 
						 | 
					29ec40db5f | ||
| 
						 | 
					61a45dcebe | ||
| 
						 | 
					7aa2c494c8 | ||
| 
						 | 
					373569d86d | ||
| 
						 | 
					fb94778c04 | ||
| 
						 | 
					6935b02d3f | ||
| 
						 | 
					0e769d77ff | ||
| 
						 | 
					082778d117 | ||
| 
						 | 
					e521662342 | ||
| 
						 | 
					061d5b4979 | ||
| 
						 | 
					71b3a14a29 | ||
| 
						 | 
					3eaf59cc5a | ||
| 
						 | 
					a91937dca5 | ||
| 
						 | 
					558588ee8a | ||
| 
						 | 
					f3ef05f5c3 | ||
| 
						 | 
					0ede4a3095 | ||
| 
						 | 
					fe789c8beb | ||
| 
						 | 
					05da0fb4cf | ||
| 
						 | 
					cfe16c92ee | ||
| 
						 | 
					9dbbc80c74 | ||
| 
						 | 
					164b42f5aa | ||
| 
						 | 
					5e9741f51c | ||
| 
						 | 
					b28821d846 | ||
| 
						 | 
					0fa0904bc5 | ||
| 
						 | 
					92798751c2 | ||
| 
						 | 
					23a9a704f3 | ||
| 
						 | 
					f2caf13d39 | ||
| 
						 | 
					25ab6f0297 | ||
| 
						 | 
					23071e932a | ||
| 
						 | 
					4812997429 | ||
| 
						 | 
					ec3162282c | ||
| 
						 | 
					f3997d0f77 | ||
| 
						 | 
					4e5534850c | ||
| 
						 | 
					354314dbf3 | ||
| 
						 | 
					2cda6462f3 | ||
| 
						 | 
					a6f864a4a3 | ||
| 
						 | 
					c35a21773e | ||
| 
						 | 
					1821ddd996 | ||
| 
						 | 
					aee702f84f | ||
| 
						 | 
					d5fe5b0899 | ||
| 
						 | 
					bd7fe1227c | ||
| 
						 | 
					0cbc06a9b9 | ||
| 
						 | 
					2f09624c07 | ||
| 
						 | 
					6a8da17ea3 | ||
| 
						 | 
					ed771abc8a | ||
| 
						 | 
					6561746f97 | ||
| 
						 | 
					1fef769496 | ||
| 
						 | 
					2283b3b443 | ||
| 
						 | 
					8267b3274c | ||
| 
						 | 
					6a6a70f1e5 | ||
| 
						 | 
					ea9de45d16 | ||
| 
						 | 
					045836c3fe | ||
| 
						 | 
					45c0d10eb0 | ||
| 
						 | 
					e2f2feafdd | ||
| 
						 | 
					c6f528583b | ||
| 
						 | 
					c9c8d39778 | ||
| 
						 | 
					39bd829236 | ||
| 
						 | 
					e731a2ffaa | ||
| 
						 | 
					b606e976e1 | ||
| 
						 | 
					0cd232cdf5 | ||
| 
						 | 
					7dced7f55d | ||
| 
						 | 
					36de644065 | ||
| 
						 | 
					95292dbba1 | ||
| 
						 | 
					86c9546362 | ||
| 
						 | 
					f37a812e59 | ||
| 
						 | 
					596943b683 | ||
| 
						 | 
					3854203037 | ||
| 
						 | 
					21337ffc67 | ||
| 
						 | 
					ea03058ace | ||
| 
						 | 
					e35cab018a | ||
| 
						 | 
					26acbbedbf | ||
| 
						 | 
					249cd67588 | ||
| 
						 | 
					72ab1700e7 | ||
| 
						 | 
					87cab92af6 | ||
| 
						 | 
					412c999f14 | ||
| 
						 | 
					8cd1798674 | ||
| 
						 | 
					e39099137d | ||
| 
						 | 
					83baa24022 | ||
| 
						 | 
					78a6509fb1 | ||
| 
						 | 
					8b2d76e8ce | ||
| 
						 | 
					dd2dca4d08 | ||
| 
						 | 
					534c14e313 | ||
| 
						 | 
					3fec8f9b53 | ||
| 
						 | 
					b8b6462844 | ||
| 
						 | 
					59e7c52341 | ||
| 
						 | 
					ff7de4c971 | ||
| 
						 | 
					978a676c7c | ||
| 
						 | 
					33051906bd | ||
| 
						 | 
					da56d333dc | ||
| 
						 | 
					48a4e6bae9 | ||
| 
						 | 
					5220c9edf8 | ||
| 
						 | 
					f567b5d28b | ||
| 
						 | 
					8e83c7dd19 | ||
| 
						 | 
					d551a2eba2 | ||
| 
						 | 
					ed2ab9e962 | ||
| 
						 | 
					aa04a3caaf | ||
| 
						 | 
					343a8c063e | ||
| 
						 | 
					4cc17dac0d | ||
| 
						 | 
					d616025fed | ||
| 
						 | 
					082d9fcf0e | ||
| 
						 | 
					4b783c0372 | ||
| 
						 | 
					fcd549e5b6 | ||
| 
						 | 
					aa8a533da6 | ||
| 
						 | 
					97be209aec | ||
| 
						 | 
					6dfdcff66c | ||
| 
						 | 
					87301a2e76 | ||
| 
						 | 
					2be19c4e45 | ||
| 
						 | 
					d9def0cb3a | ||
| 
						 | 
					65e6f9cba9 | ||
| 
						 | 
					79d00ec913 | ||
| 
						 | 
					869cdf122d | ||
| 
						 | 
					2bb5343d27 | ||
| 
						 | 
					e3d146ee44 | ||
| 
						 | 
					6061699eff | ||
| 
						 | 
					886d1a2d00 | ||
| 
						 | 
					9bdb9dc1a3 | ||
| 
						 | 
					696bfe6a87 | ||
| 
						 | 
					14bffaf8a7 | ||
| 
						 | 
					4202fe65b5 | ||
| 
						 | 
					fdd54d74a3 | ||
| 
						 | 
					a2e152ad12 | ||
| 
						 | 
					ae52164d9c | ||
| 
						 | 
					773cd0f414 | ||
| 
						 | 
					2a43e55452 | ||
| 
						 | 
					5ebb68f4ff | ||
| 
						 | 
					d3567f9ac6 | ||
| 
						 | 
					21ec42f495 | ||
| 
						 | 
					d4d49e38fc | ||
| 
						 | 
					3be97868fc | ||
| 
						 | 
					41dc73d228 | ||
| 
						 | 
					6ceefe08ab | ||
| 
						 | 
					21e5806a73 | ||
| 
						 | 
					4fd79fee2c | ||
| 
						 | 
					4c8c4a2579 | ||
| 
						 | 
					b68420b2cc | ||
| 
						 | 
					7bce999bba | ||
| 
						 | 
					dc0cc0b431 | ||
| 
						 | 
					0990d0812e | ||
| 
						 | 
					93ac765425 | ||
| 
						 | 
					46fc37b691 | ||
| 
						 | 
					de6fc6b1dd | ||
| 
						 | 
					fe15d993f9 | ||
| 
						 | 
					6583026e14 | ||
| 
						 | 
					8e674990b0 | ||
| 
						 | 
					a97fc4f758 | ||
| 
						 | 
					35388cf2a2 | ||
| 
						 | 
					417e37d291 | ||
| 
						 | 
					7dc35a1029 | ||
| 
						 | 
					9202a30dc7 | ||
| 
						 | 
					45f9f3d972 | ||
| 
						 | 
					46310ff223 | ||
| 
						 | 
					f5c99d1647 | ||
| 
						 | 
					46c4c61b40 | ||
| 
						 | 
					46255ad4df | ||
| 
						 | 
					d2d0058386 | ||
| 
						 | 
					676ae6b26e | ||
| 
						 | 
					bd6fa29f77 | ||
| 
						 | 
					4fb7e945f8 | ||
| 
						 | 
					9b72a3a584 | ||
| 
						 | 
					3de5b26d77 | ||
| 
						 | 
					19e5a4a81a | ||
| 
						 | 
					8e13c3e1b0 | ||
| 
						 | 
					4f8e3211bf | ||
| 
						 | 
					872519f7f6 | ||
| 
						 | 
					2a69a49061 | ||
| 
						 | 
					1a8e7854c7 | ||
| 
						 | 
					70fdc3c3f8 | ||
| 
						 | 
					059e4cee58 | ||
| 
						 | 
					513a02ce11 | ||
| 
						 | 
					31448a4fcd | ||
| 
						 | 
					0a779a9299 | ||
| 
						 | 
					6d3c7f035d | ||
| 
						 | 
					00ab17cb8e | ||
| 
						 | 
					2ee01e22cd | ||
| 
						 | 
					442820deaf | ||
| 
						 | 
					5e2df0b6a2 | ||
| 
						 | 
					74281b93c4 | ||
| 
						 | 
					222bb9b495 | ||
| 
						 | 
					d73ad39aed | ||
| 
						 | 
					f096f107e2 | ||
| 
						 | 
					223e6e8f13 | ||
| 
						 | 
					04b3547992 | ||
| 
						 | 
					a784f1e691 | ||
| 
						 | 
					c92715e403 | ||
| 
						 | 
					c305f61020 | ||
| 
						 | 
					784dff7574 | ||
| 
						 | 
					2a1d16f17b | ||
| 
						 | 
					937a9c96ce | ||
| 
						 | 
					b5932940ee | ||
| 
						 | 
					c6a37da9da | ||
| 
						 | 
					3c2383e261 | ||
| 
						 | 
					991880d53f | ||
| 
						 | 
					26277e4ba2 | ||
| 
						 | 
					cfa5e5c5a9 | ||
| 
						 | 
					8675955614 | ||
| 
						 | 
					816dcdf24f | ||
| 
						 | 
					bec1ad9396 | ||
| 
						 | 
					6a9e85438f | ||
| 
						 | 
					ab25e32509 | ||
| 
						 | 
					23ceddafed | ||
| 
						 | 
					84174aeb80 | ||
| 
						 | 
					d582cfa30a | ||
| 
						 | 
					2ccf985de5 | ||
| 
						 | 
					a64e96e7ad | ||
| 
						 | 
					fbf3d03a33 | ||
| 
						 | 
					7b2b6aaeb1 | ||
| 
						 | 
					e2a00f66b8 | ||
| 
						 | 
					1b3068a409 | ||
| 
						 | 
					e5414d70f5 | ||
| 
						 | 
					977e0184a7 | ||
| 
						 | 
					820f328248 | ||
| 
						 | 
					7807f0d892 | ||
| 
						 | 
					16798bbfb4 | ||
| 
						 | 
					efda2033f7 | ||
| 
						 | 
					cd06dc77ee | ||
| 
						 | 
					52b9668170 | ||
| 
						 | 
					0a117eb562 | ||
| 
						 | 
					3ea5054cf2 | ||
| 
						 | 
					8961e8ab32 | ||
| 
						 | 
					b59666c512 | ||
| 
						 | 
					dbfa77cb4b | ||
| 
						 | 
					ab22a3da34 | ||
| 
						 | 
					eefa1cd3ab | ||
| 
						 | 
					d0df73769d | ||
| 
						 | 
					e8ce780482 | ||
| 
						 | 
					168e704130 | ||
| 
						 | 
					2309f15ce0 | ||
| 
						 | 
					917e0f93ed | ||
| 
						 | 
					70dac54113 | ||
| 
						 | 
					d99598bba6 | ||
| 
						 | 
					bf258230cd | ||
| 
						 | 
					89c6f3d45d | ||
| 
						 | 
					0f4d7dadb3 | ||
| 
						 | 
					323f8c9bdb | ||
| 
						 | 
					2060d1ac89 | ||
| 
						 | 
					1d37edb63c | ||
| 
						 | 
					29fb2a5360 | ||
| 
						 | 
					8653972cb8 | ||
| 
						 | 
					8a23b7e0c8 | ||
| 
						 | 
					003d8b0cf5 | ||
| 
						 | 
					94904f44f9 | ||
| 
						 | 
					ea4e618f2a | ||
| 
						 | 
					2270c3050e | ||
| 
						 | 
					514db8b26e | ||
| 
						 | 
					e030c0fc45 | ||
| 
						 | 
					6b5eb7e656 | ||
| 
						 | 
					f28cf9348e | ||
| 
						 | 
					3e475c21ff | ||
| 
						 | 
					3c3ac92038 | ||
| 
						 | 
					836a3db163 | ||
| 
						 | 
					300343ae24 | ||
| 
						 | 
					0a188ad9d2 | ||
| 
						 | 
					a3cc551856 | ||
| 
						 | 
					8c37066ed9 | ||
| 
						 | 
					777cdb1c21 | ||
| 
						 | 
					cc5611bd89 | ||
| 
						 | 
					9f27eadaee | ||
| 
						 | 
					8cc44766e6 | ||
| 
						 | 
					5ab2c74519 | ||
| 
						 | 
					76a6e288b6 | ||
| 
						 | 
					6fd239362d | ||
| 
						 | 
					81aa48a5f3 | ||
| 
						 | 
					9a8bc9484d | ||
| 
						 | 
					f355972c9d | ||
| 
						 | 
					9daaadb3b6 | ||
| 
						 | 
					058c43e953 | ||
| 
						 | 
					f1f8689462 | ||
| 
						 | 
					2fcc5b3ef2 | ||
| 
						 | 
					6c7a133faa | ||
| 
						 | 
					a72725f4b4 | ||
| 
						 | 
					d0bcba3b3f | ||
| 
						 | 
					69026f7599 | ||
| 
						 | 
					8789925fe8 | ||
| 
						 | 
					29002c8f45 | ||
| 
						 | 
					03baaa94a8 | ||
| 
						 | 
					259a6d52e1 | ||
| 
						 | 
					c6dc336c4a | ||
| 
						 | 
					8e92bb7958 | ||
| 
						 | 
					04720b8440 | ||
| 
						 | 
					cc7d167e8b | ||
| 
						 | 
					ad79e4fe24 | ||
| 
						 | 
					47665164e8 | ||
| 
						 | 
					d0dd0e38db | ||
| 
						 | 
					0117de5b78 | ||
| 
						 | 
					7fb10547ed | ||
| 
						 | 
					39d026299e | ||
| 
						 | 
					b30430b0bd | ||
| 
						 | 
					86e6a8a503 | ||
| 
						 | 
					b62c099d54 | ||
| 
						 | 
					0906559afe | ||
| 
						 | 
					a6f1701902 | ||
| 
						 | 
					51428dcbc2 | ||
| 
						 | 
					049a7a0113 | ||
| 
						 | 
					c53874788a | ||
| 
						 | 
					f026f49415 | ||
| 
						 | 
					be07463fbd | ||
| 
						 | 
					8f70ef24a2 | ||
| 
						 | 
					89d7cdf86b | ||
| 
						 | 
					7fd08fb816 | ||
| 
						 | 
					1d0fb59208 | ||
| 
						 | 
					4d3730b50e | ||
| 
						 | 
					d9792b0d92 | ||
| 
						 | 
					df5394d51c | ||
| 
						 | 
					b8fe4f8d56 | ||
| 
						 | 
					657a7070cb | ||
| 
						 | 
					2a740963ba | ||
| 
						 | 
					c2183eb7f0 | ||
| 
						 | 
					0c71685d55 | ||
| 
						 | 
					29dcc4031f | ||
| 
						 | 
					e271faa544 | ||
| 
						 | 
					788f1b60e2 | ||
| 
						 | 
					782854ab36 | ||
| 
						 | 
					6cf4412e7b | ||
| 
						 | 
					391eff8fd5 | ||
| 
						 | 
					6424f831e2 | ||
| 
						 | 
					087733c2fd | ||
| 
						 | 
					af8258168b | ||
| 
						 | 
					3940c6ac4e | ||
| 
						 | 
					ad5f6b5687 | ||
| 
						 | 
					2f888ff7c5 | ||
| 
						 | 
					d1be686c54 | ||
| 
						 | 
					a8bc5ef46f | ||
| 
						 | 
					1e77271858 | ||
| 
						 | 
					993cd55b1d | ||
| 
						 | 
					ab1cc0ed6e | ||
| 
						 | 
					496c29aa04 | ||
| 
						 | 
					a66dec738d | ||
| 
						 | 
					ddff92c88b | ||
| 
						 | 
					ed9fd173a9 | ||
| 
						 | 
					175f00f41b | ||
| 
						 | 
					b8ee0dedec | ||
| 
						 | 
					676b37e6b0 | ||
| 
						 | 
					4b6fbd5db0 | ||
| 
						 | 
					4e6d3729e1 | ||
| 
						 | 
					28a3cddde3 | ||
| 
						 | 
					687f5ca633 | ||
| 
						 | 
					ff97639f79 | ||
| 
						 | 
					8e4b9c3c1e | ||
| 
						 | 
					460362b11f | ||
| 
						 | 
					019315afa0 | ||
| 
						 | 
					3b77f05cc9 | ||
| 
						 | 
					f63f722afb | ||
| 
						 | 
					a15a812466 | ||
| 
						 | 
					15180ee1e2 | ||
| 
						 | 
					1324d9e39a | ||
| 
						 | 
					0a7d3c367b | ||
| 
						 | 
					2e6d01ddff | ||
| 
						 | 
					dbdcb39af9 | ||
| 
						 | 
					ccd7f0661c | ||
| 
						 | 
					c43518c391 | ||
| 
						 | 
					636ee2b597 | ||
| 
						 | 
					91f1aa05ad | ||
| 
						 | 
					9f84b6390d | ||
| 
						 | 
					f456603c1b | ||
| 
						 | 
					5c31bec8c2 | ||
| 
						 | 
					9f8a896e13 | ||
| 
						 | 
					c571abeea8 | ||
| 
						 | 
					1762204b00 | ||
| 
						 | 
					1aa49c8956 | ||
| 
						 | 
					711faab329 | ||
| 
						 | 
					1204b4f1bd | ||
| 
						 | 
					2076db1ccd | ||
| 
						 | 
					49c09afb87 | ||
| 
						 | 
					3ac59180ab | ||
| 
						 | 
					8738cef5a3 | ||
| 
						 | 
					b809d02846 | ||
| 
						 | 
					e7038d077a | ||
| 
						 | 
					cf6b56c1ac | ||
| 
						 | 
					55f13dc347 | ||
| 
						 | 
					7d5ebeda52 | ||
| 
						 | 
					d5d97c4558 | ||
| 
						 | 
					5744490f2f | ||
| 
						 | 
					d462beea6e | ||
| 
						 | 
					e367ab26e1 | ||
| 
						 | 
					2aaee81313 | ||
| 
						 | 
					cd9bf29df1 | ||
| 
						 | 
					4e4fe3c26d | ||
| 
						 | 
					8fbb4e27d1 | ||
| 
						 | 
					3c243e663f | ||
| 
						 | 
					288af1f4d2 | ||
| 
						 | 
					6f8d7c6acd | ||
| 
						 | 
					32e3f26239 | ||
| 
						 | 
					5464368c08 | ||
| 
						 | 
					208edf89dc | ||
| 
						 | 
					fefdb80fdc | ||
| 
						 | 
					754bd5b7be | ||
| 
						 | 
					10a9129b7b | ||
| 
						 | 
					ef945d298c | ||
| 
						 | 
					149d814fab | ||
| 
						 | 
					5f1d8dfa5b | ||
| 
						 | 
					3644853d38 | ||
| 
						 | 
					4e3170dc95 | ||
| 
						 | 
					c795dbde26 | ||
| 
						 | 
					4ce627b4ee | ||
| 
						 | 
					86b4fdc139 | ||
| 
						 | 
					20ea8bf06e | ||
| 
						 | 
					642db6d92b | ||
| 
						 | 
					4aac5a23cd | ||
| 
						 | 
					214b419db2 | ||
| 
						 | 
					cdcb25be8e | ||
| 
						 | 
					aecc6655db | ||
| 
						 | 
					2754ddec1b | ||
| 
						 | 
					ae0e481cff | ||
| 
						 | 
					f198be39d7 | ||
| 
						 | 
					08fc96b890 | ||
| 
						 | 
					8c28bea5b1 | ||
| 
						 | 
					00eedeb8b3 | ||
| 
						 | 
					0a4853ba7b | ||
| 
						 | 
					45276cc244 | ||
| 
						 | 
					684cf10230 | ||
| 
						 | 
					63a277ba80 | ||
| 
						 | 
					53f3385c49 | ||
| 
						 | 
					51930a0243 | ||
| 
						 | 
					6a5cea171e | ||
| 
						 | 
					3363c8f434 | ||
| 
						 | 
					3b891bc146 | ||
| 
						 | 
					0f19450ab4 | ||
| 
						 | 
					98ec798bfc | ||
| 
						 | 
					01d28ce3fc | ||
| 
						 | 
					bc7519f645 | ||
| 
						 | 
					28513a0502 | ||
| 
						 | 
					3e3266fa74 | ||
| 
						 | 
					ce020b1f9f | ||
| 
						 | 
					d394b957d1 | ||
| 
						 | 
					cf22c55430 | ||
| 
						 | 
					511348974e | ||
| 
						 | 
					972598a698 | ||
| 
						 | 
					d81bec860b | ||
| 
						 | 
					fde7a04ee7 | 
@@ -37,6 +37,7 @@
 | 
			
		||||
          "!secret scalar",
 | 
			
		||||
          "!lambda scalar",
 | 
			
		||||
          "!extend scalar",
 | 
			
		||||
          "!remove scalar",
 | 
			
		||||
          "!include_dir_named scalar",
 | 
			
		||||
          "!include_dir_list scalar",
 | 
			
		||||
          "!include_dir_merge_list scalar",
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										97
									
								
								.github/actions/build-image/action.yaml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										97
									
								
								.github/actions/build-image/action.yaml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@@ -0,0 +1,97 @@
 | 
			
		||||
name: Build Image
 | 
			
		||||
inputs:
 | 
			
		||||
  platform:
 | 
			
		||||
    description: "Platform to build for"
 | 
			
		||||
    required: true
 | 
			
		||||
    example: "linux/amd64"
 | 
			
		||||
  target:
 | 
			
		||||
    description: "Target to build"
 | 
			
		||||
    required: true
 | 
			
		||||
    example: "docker"
 | 
			
		||||
  baseimg:
 | 
			
		||||
    description: "Base image type"
 | 
			
		||||
    required: true
 | 
			
		||||
    example: "docker"
 | 
			
		||||
  suffix:
 | 
			
		||||
    description: "Suffix to add to tags"
 | 
			
		||||
    required: true
 | 
			
		||||
  version:
 | 
			
		||||
    description: "Version to build"
 | 
			
		||||
    required: true
 | 
			
		||||
    example: "2023.12.0"
 | 
			
		||||
runs:
 | 
			
		||||
  using: "composite"
 | 
			
		||||
  steps:
 | 
			
		||||
    - name: Generate short tags
 | 
			
		||||
      id: tags
 | 
			
		||||
      shell: bash
 | 
			
		||||
      run: |
 | 
			
		||||
        output=$(docker/generate_tags.py \
 | 
			
		||||
          --tag "${{ inputs.version }}" \
 | 
			
		||||
          --suffix "${{ inputs.suffix }}")
 | 
			
		||||
        echo $output
 | 
			
		||||
        for l in $output; do
 | 
			
		||||
          echo $l >> $GITHUB_OUTPUT
 | 
			
		||||
        done
 | 
			
		||||
 | 
			
		||||
    - name: Build and push to ghcr by digest
 | 
			
		||||
      id: build-ghcr
 | 
			
		||||
      uses: docker/build-push-action@v5.0.0
 | 
			
		||||
      with:
 | 
			
		||||
        context: .
 | 
			
		||||
        file: ./docker/Dockerfile
 | 
			
		||||
        platforms: ${{ inputs.platform }}
 | 
			
		||||
        target: ${{ inputs.target }}
 | 
			
		||||
        cache-from: type=gha
 | 
			
		||||
        cache-to: type=gha,mode=max
 | 
			
		||||
        build-args: |
 | 
			
		||||
          BASEIMGTYPE=${{ inputs.baseimg }}
 | 
			
		||||
          BUILD_VERSION=${{ inputs.version }}
 | 
			
		||||
        outputs: |
 | 
			
		||||
          type=image,name=ghcr.io/${{ steps.tags.outputs.image_name }},push-by-digest=true,name-canonical=true,push=true
 | 
			
		||||
 | 
			
		||||
    - name: Export ghcr digests
 | 
			
		||||
      shell: bash
 | 
			
		||||
      run: |
 | 
			
		||||
        mkdir -p /tmp/digests/${{ inputs.target }}/ghcr
 | 
			
		||||
        digest="${{ steps.build-ghcr.outputs.digest }}"
 | 
			
		||||
        touch "/tmp/digests/${{ inputs.target }}/ghcr/${digest#sha256:}"
 | 
			
		||||
 | 
			
		||||
    - name: Upload ghcr digest
 | 
			
		||||
      uses: actions/upload-artifact@v3.1.3
 | 
			
		||||
      with:
 | 
			
		||||
        name: digests-${{ inputs.target }}-ghcr
 | 
			
		||||
        path: /tmp/digests/${{ inputs.target }}/ghcr/*
 | 
			
		||||
        if-no-files-found: error
 | 
			
		||||
        retention-days: 1
 | 
			
		||||
 | 
			
		||||
    - name: Build and push to dockerhub by digest
 | 
			
		||||
      id: build-dockerhub
 | 
			
		||||
      uses: docker/build-push-action@v5.0.0
 | 
			
		||||
      with:
 | 
			
		||||
        context: .
 | 
			
		||||
        file: ./docker/Dockerfile
 | 
			
		||||
        platforms: ${{ inputs.platform }}
 | 
			
		||||
        target: ${{ inputs.target }}
 | 
			
		||||
        cache-from: type=gha
 | 
			
		||||
        cache-to: type=gha,mode=max
 | 
			
		||||
        build-args: |
 | 
			
		||||
          BASEIMGTYPE=${{ inputs.baseimg }}
 | 
			
		||||
          BUILD_VERSION=${{ inputs.version }}
 | 
			
		||||
        outputs: |
 | 
			
		||||
          type=image,name=docker.io/${{ steps.tags.outputs.image_name }},push-by-digest=true,name-canonical=true,push=true
 | 
			
		||||
 | 
			
		||||
    - name: Export dockerhub digests
 | 
			
		||||
      shell: bash
 | 
			
		||||
      run: |
 | 
			
		||||
        mkdir -p /tmp/digests/${{ inputs.target }}/dockerhub
 | 
			
		||||
        digest="${{ steps.build-dockerhub.outputs.digest }}"
 | 
			
		||||
        touch "/tmp/digests/${{ inputs.target }}/dockerhub/${digest#sha256:}"
 | 
			
		||||
 | 
			
		||||
    - name: Upload dockerhub digest
 | 
			
		||||
      uses: actions/upload-artifact@v3.1.3
 | 
			
		||||
      with:
 | 
			
		||||
        name: digests-${{ inputs.target }}-dockerhub
 | 
			
		||||
        path: /tmp/digests/${{ inputs.target }}/dockerhub/*
 | 
			
		||||
        if-no-files-found: error
 | 
			
		||||
        retention-days: 1
 | 
			
		||||
							
								
								
									
										15
									
								
								.github/actions/restore-python/action.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										15
									
								
								.github/actions/restore-python/action.yml
									
									
									
									
										vendored
									
									
								
							@@ -17,7 +17,7 @@ runs:
 | 
			
		||||
  steps:
 | 
			
		||||
    - name: Set up Python ${{ inputs.python-version }}
 | 
			
		||||
      id: python
 | 
			
		||||
      uses: actions/setup-python@v4.7.0
 | 
			
		||||
      uses: actions/setup-python@v5.0.0
 | 
			
		||||
      with:
 | 
			
		||||
        python-version: ${{ inputs.python-version }}
 | 
			
		||||
    - name: Restore Python virtual environment
 | 
			
		||||
@@ -28,11 +28,20 @@ runs:
 | 
			
		||||
        # yamllint disable-line rule:line-length
 | 
			
		||||
        key: ${{ runner.os }}-${{ steps.python.outputs.python-version }}-venv-${{ inputs.cache-key }}
 | 
			
		||||
    - name: Create Python virtual environment
 | 
			
		||||
      if: steps.cache-venv.outputs.cache-hit != 'true'
 | 
			
		||||
      if: steps.cache-venv.outputs.cache-hit != 'true' && runner.os != 'Windows'
 | 
			
		||||
      shell: bash
 | 
			
		||||
      run: |
 | 
			
		||||
        python -m venv venv
 | 
			
		||||
        . venv/bin/activate
 | 
			
		||||
        source venv/bin/activate
 | 
			
		||||
        python --version
 | 
			
		||||
        pip install -r requirements.txt -r requirements_optional.txt -r requirements_test.txt
 | 
			
		||||
        pip install -e .
 | 
			
		||||
    - name: Create Python virtual environment
 | 
			
		||||
      if: steps.cache-venv.outputs.cache-hit != 'true' && runner.os == 'Windows'
 | 
			
		||||
      shell: bash
 | 
			
		||||
      run: |
 | 
			
		||||
        python -m venv venv
 | 
			
		||||
        ./venv/Scripts/activate
 | 
			
		||||
        python --version
 | 
			
		||||
        pip install -r requirements.txt -r requirements_optional.txt -r requirements_test.txt
 | 
			
		||||
        pip install -e .
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								.github/workflows/ci-docker.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/ci-docker.yml
									
									
									
									
										vendored
									
									
								
							@@ -42,7 +42,7 @@ jobs:
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: actions/checkout@v4.1.1
 | 
			
		||||
      - name: Set up Python
 | 
			
		||||
        uses: actions/setup-python@v4.7.1
 | 
			
		||||
        uses: actions/setup-python@v5.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          python-version: "3.9"
 | 
			
		||||
      - name: Set up Docker Buildx
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										107
									
								
								.github/workflows/ci.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										107
									
								
								.github/workflows/ci.yml
									
									
									
									
										vendored
									
									
								
							@@ -40,12 +40,12 @@ jobs:
 | 
			
		||||
        run: echo key="${{ hashFiles('requirements.txt', 'requirements_optional.txt', 'requirements_test.txt') }}" >> $GITHUB_OUTPUT
 | 
			
		||||
      - name: Set up Python ${{ env.DEFAULT_PYTHON }}
 | 
			
		||||
        id: python
 | 
			
		||||
        uses: actions/setup-python@v4.7.1
 | 
			
		||||
        uses: actions/setup-python@v5.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          python-version: ${{ env.DEFAULT_PYTHON }}
 | 
			
		||||
      - name: Restore Python virtual environment
 | 
			
		||||
        id: cache-venv
 | 
			
		||||
        uses: actions/cache@v3.3.2
 | 
			
		||||
        uses: actions/cache@v4.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          path: venv
 | 
			
		||||
          # yamllint disable-line rule:line-length
 | 
			
		||||
@@ -166,7 +166,35 @@ jobs:
 | 
			
		||||
 | 
			
		||||
  pytest:
 | 
			
		||||
    name: Run pytest
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    strategy:
 | 
			
		||||
      fail-fast: false
 | 
			
		||||
      matrix:
 | 
			
		||||
        python-version:
 | 
			
		||||
          - "3.9"
 | 
			
		||||
          - "3.10"
 | 
			
		||||
          - "3.11"
 | 
			
		||||
          - "3.12"
 | 
			
		||||
        os:
 | 
			
		||||
          - ubuntu-latest
 | 
			
		||||
          - macOS-latest
 | 
			
		||||
          - windows-latest
 | 
			
		||||
        exclude:
 | 
			
		||||
          # Minimize CI resource usage
 | 
			
		||||
          # by only running the Python version
 | 
			
		||||
          # version used for docker images on Windows and macOS
 | 
			
		||||
          - python-version: "3.12"
 | 
			
		||||
            os: windows-latest
 | 
			
		||||
          - python-version: "3.10"
 | 
			
		||||
            os: windows-latest
 | 
			
		||||
          - python-version: "3.9"
 | 
			
		||||
            os: windows-latest
 | 
			
		||||
          - python-version: "3.12"
 | 
			
		||||
            os: macOS-latest
 | 
			
		||||
          - python-version: "3.10"
 | 
			
		||||
            os: macOS-latest
 | 
			
		||||
          - python-version: "3.9"
 | 
			
		||||
            os: macOS-latest
 | 
			
		||||
    runs-on: ${{ matrix.os }}
 | 
			
		||||
    needs:
 | 
			
		||||
      - common
 | 
			
		||||
    steps:
 | 
			
		||||
@@ -175,14 +203,24 @@ jobs:
 | 
			
		||||
      - name: Restore Python
 | 
			
		||||
        uses: ./.github/actions/restore-python
 | 
			
		||||
        with:
 | 
			
		||||
          python-version: ${{ env.DEFAULT_PYTHON }}
 | 
			
		||||
          python-version: ${{ matrix.python-version }}
 | 
			
		||||
          cache-key: ${{ needs.common.outputs.cache-key }}
 | 
			
		||||
      - name: Register matcher
 | 
			
		||||
        run: echo "::add-matcher::.github/workflows/matchers/pytest.json"
 | 
			
		||||
      - name: Run pytest
 | 
			
		||||
        if: matrix.os == 'windows-latest'
 | 
			
		||||
        run: |
 | 
			
		||||
          ./venv/Scripts/activate
 | 
			
		||||
          pytest -vv --cov-report=xml --tb=native tests
 | 
			
		||||
      - name: Run pytest
 | 
			
		||||
        if: matrix.os == 'ubuntu-latest' || matrix.os == 'macOS-latest'
 | 
			
		||||
        run: |
 | 
			
		||||
          . venv/bin/activate
 | 
			
		||||
          pytest -vv --tb=native tests
 | 
			
		||||
          pytest -vv --cov-report=xml --tb=native tests
 | 
			
		||||
      - name: Upload coverage to Codecov
 | 
			
		||||
        uses: codecov/codecov-action@v3
 | 
			
		||||
        with:
 | 
			
		||||
          token: ${{ secrets.CODECOV_TOKEN }}
 | 
			
		||||
 | 
			
		||||
  clang-format:
 | 
			
		||||
    name: Check clang-format
 | 
			
		||||
@@ -327,7 +365,7 @@ jobs:
 | 
			
		||||
          python-version: ${{ env.DEFAULT_PYTHON }}
 | 
			
		||||
          cache-key: ${{ needs.common.outputs.cache-key }}
 | 
			
		||||
      - name: Cache platformio
 | 
			
		||||
        uses: actions/cache@v3.3.2
 | 
			
		||||
        uses: actions/cache@v4.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          path: ~/.platformio
 | 
			
		||||
          # yamllint disable-line rule:line-length
 | 
			
		||||
@@ -354,6 +392,62 @@ jobs:
 | 
			
		||||
        # yamllint disable-line rule:line-length
 | 
			
		||||
        if: always()
 | 
			
		||||
 | 
			
		||||
  list-components:
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    needs:
 | 
			
		||||
      - common
 | 
			
		||||
    outputs:
 | 
			
		||||
      matrix: ${{ steps.set-matrix.outputs.matrix }}
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v4.1.1
 | 
			
		||||
        with:
 | 
			
		||||
          # Fetch enough history so `git merge-base refs/remotes/origin/dev HEAD` works.
 | 
			
		||||
          fetch-depth: 500
 | 
			
		||||
      - name: Fetch dev branch
 | 
			
		||||
        run: |
 | 
			
		||||
          git -c protocol.version=2 fetch --no-tags --prune --no-recurse-submodules --depth=1 origin +refs/heads/dev*:refs/remotes/origin/dev* +refs/tags/dev*:refs/tags/dev*
 | 
			
		||||
          git merge-base refs/remotes/origin/dev HEAD
 | 
			
		||||
      - name: Restore Python
 | 
			
		||||
        uses: ./.github/actions/restore-python
 | 
			
		||||
        with:
 | 
			
		||||
          python-version: ${{ env.DEFAULT_PYTHON }}
 | 
			
		||||
          cache-key: ${{ needs.common.outputs.cache-key }}
 | 
			
		||||
      - name: Find changed components
 | 
			
		||||
        id: set-matrix
 | 
			
		||||
        run: |
 | 
			
		||||
          . venv/bin/activate
 | 
			
		||||
          echo "matrix=$(script/list-components.py --changed | jq -R -s -c 'split("\n")[:-1]')" >> $GITHUB_OUTPUT
 | 
			
		||||
 | 
			
		||||
  test-build-components:
 | 
			
		||||
    name: Component test ${{ matrix.file }}
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    needs:
 | 
			
		||||
      - common
 | 
			
		||||
      - list-components
 | 
			
		||||
    if: ${{ needs.list-components.outputs.matrix != '[]' && needs.list-components.outputs.matrix != '' }}
 | 
			
		||||
    strategy:
 | 
			
		||||
      fail-fast: false
 | 
			
		||||
      max-parallel: 2
 | 
			
		||||
      matrix:
 | 
			
		||||
        file: ${{ fromJson(needs.list-components.outputs.matrix) }}
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v4.1.1
 | 
			
		||||
      - name: Restore Python
 | 
			
		||||
        uses: ./.github/actions/restore-python
 | 
			
		||||
        with:
 | 
			
		||||
          python-version: ${{ env.DEFAULT_PYTHON }}
 | 
			
		||||
          cache-key: ${{ needs.common.outputs.cache-key }}
 | 
			
		||||
      - name: test_build_components -e config -c ${{ matrix.file }}
 | 
			
		||||
        run: |
 | 
			
		||||
          . venv/bin/activate
 | 
			
		||||
          ./script/test_build_components -e config -c ${{ matrix.file }}
 | 
			
		||||
      - name: test_build_components -e compile -c ${{ matrix.file }}
 | 
			
		||||
        run: |
 | 
			
		||||
          . venv/bin/activate
 | 
			
		||||
          ./script/test_build_components -e compile -c ${{ matrix.file }}
 | 
			
		||||
 | 
			
		||||
  ci-status:
 | 
			
		||||
    name: CI Status
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
@@ -368,6 +462,7 @@ jobs:
 | 
			
		||||
      - pyupgrade
 | 
			
		||||
      - compile-tests
 | 
			
		||||
      - clang-tidy
 | 
			
		||||
      - test-build-components
 | 
			
		||||
    if: always()
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Success
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								.github/workflows/lock.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/lock.yml
									
									
									
									
										vendored
									
									
								
							@@ -18,7 +18,7 @@ jobs:
 | 
			
		||||
  lock:
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: dessant/lock-threads@v4.0.1
 | 
			
		||||
      - uses: dessant/lock-threads@v5.0.1
 | 
			
		||||
        with:
 | 
			
		||||
          pr-inactive-days: "1"
 | 
			
		||||
          pr-lock-reason: ""
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								.github/workflows/needs-docs.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/needs-docs.yml
									
									
									
									
										vendored
									
									
								
							@@ -10,7 +10,7 @@ jobs:
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check for needs-docs label
 | 
			
		||||
        uses: actions/github-script@v6.4.1
 | 
			
		||||
        uses: actions/github-script@v7.0.1
 | 
			
		||||
        with:
 | 
			
		||||
          script: |
 | 
			
		||||
            const { data: labels } = await github.rest.issues.listLabelsOnIssue({
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										136
									
								
								.github/workflows/release.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										136
									
								
								.github/workflows/release.yml
									
									
									
									
										vendored
									
									
								
							@@ -45,7 +45,7 @@ jobs:
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: actions/checkout@v4.1.1
 | 
			
		||||
      - name: Set up Python
 | 
			
		||||
        uses: actions/setup-python@v4.7.1
 | 
			
		||||
        uses: actions/setup-python@v5.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          python-version: "3.x"
 | 
			
		||||
      - name: Set up python environment
 | 
			
		||||
@@ -63,40 +63,31 @@ jobs:
 | 
			
		||||
        run: twine upload dist/*
 | 
			
		||||
 | 
			
		||||
  deploy-docker:
 | 
			
		||||
    name: Build and publish ESPHome ${{ matrix.image.title}}
 | 
			
		||||
    name: Build ESPHome ${{ matrix.platform }}
 | 
			
		||||
    if: github.repository == 'esphome/esphome'
 | 
			
		||||
    permissions:
 | 
			
		||||
      contents: read
 | 
			
		||||
      packages: write
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    continue-on-error: ${{ matrix.image.title == 'lint' }}
 | 
			
		||||
    needs: [init]
 | 
			
		||||
    strategy:
 | 
			
		||||
      fail-fast: false
 | 
			
		||||
      matrix:
 | 
			
		||||
        image:
 | 
			
		||||
          - title: "ha-addon"
 | 
			
		||||
            suffix: "hassio"
 | 
			
		||||
            target: "hassio"
 | 
			
		||||
            baseimg: "hassio"
 | 
			
		||||
          - title: "docker"
 | 
			
		||||
            suffix: ""
 | 
			
		||||
            target: "docker"
 | 
			
		||||
            baseimg: "docker"
 | 
			
		||||
          - title: "lint"
 | 
			
		||||
            suffix: "lint"
 | 
			
		||||
            target: "lint"
 | 
			
		||||
            baseimg: "docker"
 | 
			
		||||
        platform:
 | 
			
		||||
          - linux/amd64
 | 
			
		||||
          - linux/arm/v7
 | 
			
		||||
          - linux/arm64
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: actions/checkout@v4.1.1
 | 
			
		||||
      - name: Set up Python
 | 
			
		||||
        uses: actions/setup-python@v4.7.1
 | 
			
		||||
        uses: actions/setup-python@v5.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          python-version: "3.9"
 | 
			
		||||
 | 
			
		||||
      - name: Set up Docker Buildx
 | 
			
		||||
        uses: docker/setup-buildx-action@v3.0.0
 | 
			
		||||
      - name: Set up QEMU
 | 
			
		||||
        if: matrix.platform != 'linux/amd64'
 | 
			
		||||
        uses: docker/setup-qemu-action@v3.0.0
 | 
			
		||||
 | 
			
		||||
      - name: Log in to docker hub
 | 
			
		||||
@@ -111,37 +102,108 @@ jobs:
 | 
			
		||||
          username: ${{ github.actor }}
 | 
			
		||||
          password: ${{ secrets.GITHUB_TOKEN }}
 | 
			
		||||
 | 
			
		||||
      - name: Build docker
 | 
			
		||||
        uses: ./.github/actions/build-image
 | 
			
		||||
        with:
 | 
			
		||||
          platform: ${{ matrix.platform }}
 | 
			
		||||
          target: docker
 | 
			
		||||
          baseimg: docker
 | 
			
		||||
          suffix: ""
 | 
			
		||||
          version: ${{ needs.init.outputs.tag }}
 | 
			
		||||
 | 
			
		||||
      - name: Build ha-addon
 | 
			
		||||
        uses: ./.github/actions/build-image
 | 
			
		||||
        with:
 | 
			
		||||
          platform: ${{ matrix.platform }}
 | 
			
		||||
          target: hassio
 | 
			
		||||
          baseimg: hassio
 | 
			
		||||
          suffix: "hassio"
 | 
			
		||||
          version: ${{ needs.init.outputs.tag }}
 | 
			
		||||
 | 
			
		||||
      - name: Build lint
 | 
			
		||||
        uses: ./.github/actions/build-image
 | 
			
		||||
        with:
 | 
			
		||||
          platform: ${{ matrix.platform }}
 | 
			
		||||
          target: lint
 | 
			
		||||
          baseimg: docker
 | 
			
		||||
          suffix: lint
 | 
			
		||||
          version: ${{ needs.init.outputs.tag }}
 | 
			
		||||
 | 
			
		||||
  deploy-manifest:
 | 
			
		||||
    name: Publish ESPHome ${{ matrix.image.title }} to ${{ matrix.registry }}
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    needs:
 | 
			
		||||
      - init
 | 
			
		||||
      - deploy-docker
 | 
			
		||||
    if: github.repository == 'esphome/esphome'
 | 
			
		||||
    permissions:
 | 
			
		||||
      contents: read
 | 
			
		||||
      packages: write
 | 
			
		||||
    strategy:
 | 
			
		||||
      fail-fast: false
 | 
			
		||||
      matrix:
 | 
			
		||||
        image:
 | 
			
		||||
          - title: "ha-addon"
 | 
			
		||||
            target: "hassio"
 | 
			
		||||
            suffix: "hassio"
 | 
			
		||||
          - title: "docker"
 | 
			
		||||
            target: "docker"
 | 
			
		||||
            suffix: ""
 | 
			
		||||
          - title: "lint"
 | 
			
		||||
            target: "lint"
 | 
			
		||||
            suffix: "lint"
 | 
			
		||||
        registry:
 | 
			
		||||
          - ghcr
 | 
			
		||||
          - dockerhub
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: actions/checkout@v4.1.1
 | 
			
		||||
      - name: Download digests
 | 
			
		||||
        uses: actions/download-artifact@v3.0.2
 | 
			
		||||
        with:
 | 
			
		||||
          name: digests-${{ matrix.image.target }}-${{ matrix.registry }}
 | 
			
		||||
          path: /tmp/digests
 | 
			
		||||
      - name: Set up Docker Buildx
 | 
			
		||||
        uses: docker/setup-buildx-action@v3.0.0
 | 
			
		||||
 | 
			
		||||
      - name: Log in to docker hub
 | 
			
		||||
        if: matrix.registry == 'dockerhub'
 | 
			
		||||
        uses: docker/login-action@v3.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          username: ${{ secrets.DOCKER_USER }}
 | 
			
		||||
          password: ${{ secrets.DOCKER_PASSWORD }}
 | 
			
		||||
      - name: Log in to the GitHub container registry
 | 
			
		||||
        if: matrix.registry == 'ghcr'
 | 
			
		||||
        uses: docker/login-action@v3.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          registry: ghcr.io
 | 
			
		||||
          username: ${{ github.actor }}
 | 
			
		||||
          password: ${{ secrets.GITHUB_TOKEN }}
 | 
			
		||||
 | 
			
		||||
      - name: Generate short tags
 | 
			
		||||
        id: tags
 | 
			
		||||
        run: |
 | 
			
		||||
          docker/generate_tags.py \
 | 
			
		||||
          output=$(docker/generate_tags.py \
 | 
			
		||||
            --tag "${{ needs.init.outputs.tag }}" \
 | 
			
		||||
            --suffix "${{ matrix.image.suffix }}"
 | 
			
		||||
            --suffix "${{ matrix.image.suffix }}" \
 | 
			
		||||
            --registry "${{ matrix.registry }}")
 | 
			
		||||
          echo $output
 | 
			
		||||
          for l in $output; do
 | 
			
		||||
            echo $l >> $GITHUB_OUTPUT
 | 
			
		||||
          done
 | 
			
		||||
 | 
			
		||||
      - name: Build and push
 | 
			
		||||
        uses: docker/build-push-action@v5.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          context: .
 | 
			
		||||
          file: ./docker/Dockerfile
 | 
			
		||||
          platforms: linux/amd64,linux/arm/v7,linux/arm64
 | 
			
		||||
          target: ${{ matrix.image.target }}
 | 
			
		||||
          push: true
 | 
			
		||||
          # yamllint disable rule:line-length
 | 
			
		||||
          cache-from: type=registry,ref=ghcr.io/${{ steps.tags.outputs.image }}:cache-${{ steps.tags.outputs.channel }}
 | 
			
		||||
          cache-to: type=registry,ref=ghcr.io/${{ steps.tags.outputs.image }}:cache-${{ steps.tags.outputs.channel }},mode=max
 | 
			
		||||
          # yamllint enable rule:line-length
 | 
			
		||||
          tags: ${{ steps.tags.outputs.tags }}
 | 
			
		||||
          build-args: |
 | 
			
		||||
            BASEIMGTYPE=${{ matrix.image.baseimg }}
 | 
			
		||||
            BUILD_VERSION=${{ needs.init.outputs.tag }}
 | 
			
		||||
      - name: Create manifest list and push
 | 
			
		||||
        working-directory: /tmp/digests
 | 
			
		||||
        run: |
 | 
			
		||||
          docker buildx imagetools create $(jq -Rcnr 'inputs | . / "," | map("-t " + .) | join(" ")' <<< "${{ steps.tags.outputs.tags}}") \
 | 
			
		||||
            $(printf '${{ steps.tags.outputs.image }}@sha256:%s ' *)
 | 
			
		||||
 | 
			
		||||
  deploy-ha-addon-repo:
 | 
			
		||||
    if: github.repository == 'esphome/esphome' && github.event_name == 'release'
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    needs: [deploy-docker]
 | 
			
		||||
    needs: [deploy-manifest]
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Trigger Workflow
 | 
			
		||||
        uses: actions/github-script@v6.4.1
 | 
			
		||||
        uses: actions/github-script@v7.0.1
 | 
			
		||||
        with:
 | 
			
		||||
          github-token: ${{ secrets.DEPLOY_HA_ADDON_REPO_TOKEN }}
 | 
			
		||||
          script: |
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										4
									
								
								.github/workflows/stale.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.github/workflows/stale.yml
									
									
									
									
										vendored
									
									
								
							@@ -18,7 +18,7 @@ jobs:
 | 
			
		||||
  stale:
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: actions/stale@v8.0.0
 | 
			
		||||
      - uses: actions/stale@v9.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          days-before-pr-stale: 90
 | 
			
		||||
          days-before-pr-close: 7
 | 
			
		||||
@@ -38,7 +38,7 @@ jobs:
 | 
			
		||||
  close-issues:
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: actions/stale@v8.0.0
 | 
			
		||||
      - uses: actions/stale@v9.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          days-before-pr-stale: -1
 | 
			
		||||
          days-before-pr-close: -1
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								.github/workflows/sync-device-classes.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/sync-device-classes.yml
									
									
									
									
										vendored
									
									
								
							@@ -22,7 +22,7 @@ jobs:
 | 
			
		||||
          path: lib/home-assistant
 | 
			
		||||
 | 
			
		||||
      - name: Setup Python
 | 
			
		||||
        uses: actions/setup-python@v4.7.1
 | 
			
		||||
        uses: actions/setup-python@v5.0.0
 | 
			
		||||
        with:
 | 
			
		||||
          python-version: 3.11
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								.github/workflows/yaml-lint.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/yaml-lint.yml
									
									
									
									
										vendored
									
									
								
							@@ -19,4 +19,4 @@ jobs:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v4.1.1
 | 
			
		||||
      - name: Run yamllint
 | 
			
		||||
        uses: frenck/action-yamllint@v1.4.1
 | 
			
		||||
        uses: frenck/action-yamllint@v1.4.2
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,7 @@
 | 
			
		||||
# See https://pre-commit.com/hooks.html for more hooks
 | 
			
		||||
repos:
 | 
			
		||||
  - repo: https://github.com/psf/black-pre-commit-mirror
 | 
			
		||||
    rev: 23.10.1
 | 
			
		||||
    rev: 23.12.1
 | 
			
		||||
    hooks:
 | 
			
		||||
      - id: black
 | 
			
		||||
        args:
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										40
									
								
								CODEOWNERS
									
									
									
									
									
								
							
							
						
						
									
										40
									
								
								CODEOWNERS
									
									
									
									
									
								
							@@ -12,6 +12,7 @@ esphome/core/* @esphome/core
 | 
			
		||||
 | 
			
		||||
# Integrations
 | 
			
		||||
esphome/components/a01nyub/* @MrSuicideParrot
 | 
			
		||||
esphome/components/a02yyuw/* @TH-Braemer
 | 
			
		||||
esphome/components/absolute_humidity/* @DAVe3283
 | 
			
		||||
esphome/components/ac_dimmer/* @glmnet
 | 
			
		||||
esphome/components/adc/* @esphome/core
 | 
			
		||||
@@ -24,7 +25,7 @@ esphome/components/airthings_ble/* @jeromelaban
 | 
			
		||||
esphome/components/airthings_wave_base/* @jeromelaban @kpfleming @ncareau
 | 
			
		||||
esphome/components/airthings_wave_mini/* @ncareau
 | 
			
		||||
esphome/components/airthings_wave_plus/* @jeromelaban
 | 
			
		||||
esphome/components/alarm_control_panel/* @grahambrown11
 | 
			
		||||
esphome/components/alarm_control_panel/* @grahambrown11 @hwstar
 | 
			
		||||
esphome/components/alpha3/* @jan-hofmeier
 | 
			
		||||
esphome/components/am43/* @buxtronix
 | 
			
		||||
esphome/components/am43/cover/* @buxtronix
 | 
			
		||||
@@ -33,6 +34,8 @@ esphome/components/analog_threshold/* @ianchi
 | 
			
		||||
esphome/components/animation/* @syndlex
 | 
			
		||||
esphome/components/anova/* @buxtronix
 | 
			
		||||
esphome/components/api/* @OttoWinter
 | 
			
		||||
esphome/components/as5600/* @ammmze
 | 
			
		||||
esphome/components/as5600/sensor/* @ammmze
 | 
			
		||||
esphome/components/as7341/* @mrgnr
 | 
			
		||||
esphome/components/async_tcp/* @OttoWinter
 | 
			
		||||
esphome/components/atc_mithermometer/* @ahpohl
 | 
			
		||||
@@ -49,8 +52,10 @@ esphome/components/bk72xx/* @kuba2k2
 | 
			
		||||
esphome/components/bl0939/* @ziceva
 | 
			
		||||
esphome/components/bl0940/* @tobias-
 | 
			
		||||
esphome/components/bl0942/* @dbuezas
 | 
			
		||||
esphome/components/ble_client/* @buxtronix
 | 
			
		||||
esphome/components/ble_client/* @buxtronix @clydebarrow
 | 
			
		||||
esphome/components/bluetooth_proxy/* @jesserockz
 | 
			
		||||
esphome/components/bme280_base/* @esphome/core
 | 
			
		||||
esphome/components/bme280_spi/* @apbodrov
 | 
			
		||||
esphome/components/bme680_bsec/* @trvrnrth
 | 
			
		||||
esphome/components/bmi160/* @flaviut
 | 
			
		||||
esphome/components/bmp3xx/* @martgras
 | 
			
		||||
@@ -66,6 +71,7 @@ esphome/components/cd74hc4067/* @asoehlke
 | 
			
		||||
esphome/components/climate/* @esphome/core
 | 
			
		||||
esphome/components/climate_ir/* @glmnet
 | 
			
		||||
esphome/components/color_temperature/* @jesserockz
 | 
			
		||||
esphome/components/combination/* @Cat-Ion @kahrendt
 | 
			
		||||
esphome/components/coolix/* @glmnet
 | 
			
		||||
esphome/components/copy/* @OttoWinter
 | 
			
		||||
esphome/components/cover/* @esphome/core
 | 
			
		||||
@@ -88,8 +94,9 @@ esphome/components/ds1307/* @badbadc0ffee
 | 
			
		||||
esphome/components/dsmr/* @glmnet @zuidwijk
 | 
			
		||||
esphome/components/duty_time/* @dudanov
 | 
			
		||||
esphome/components/ee895/* @Stock-M
 | 
			
		||||
esphome/components/ektf2232/* @jesserockz
 | 
			
		||||
esphome/components/ektf2232/touchscreen/* @jesserockz
 | 
			
		||||
esphome/components/emc2101/* @ellull
 | 
			
		||||
esphome/components/ens160/* @vincentscode
 | 
			
		||||
esphome/components/ens210/* @itn3rd77
 | 
			
		||||
esphome/components/esp32/* @esphome/core
 | 
			
		||||
esphome/components/esp32_ble/* @Rapsssito @jesserockz
 | 
			
		||||
@@ -109,23 +116,29 @@ esphome/components/fastled_base/* @OttoWinter
 | 
			
		||||
esphome/components/feedback/* @ianchi
 | 
			
		||||
esphome/components/fingerprint_grow/* @OnFreund @loongyh
 | 
			
		||||
esphome/components/fs3000/* @kahrendt
 | 
			
		||||
esphome/components/ft5x06/* @clydebarrow
 | 
			
		||||
esphome/components/ft63x6/* @gpambrozio
 | 
			
		||||
esphome/components/gcja5/* @gcormier
 | 
			
		||||
esphome/components/globals/* @esphome/core
 | 
			
		||||
esphome/components/gp8403/* @jesserockz
 | 
			
		||||
esphome/components/gpio/* @esphome/core
 | 
			
		||||
esphome/components/gps/* @coogle
 | 
			
		||||
esphome/components/graph/* @synco
 | 
			
		||||
esphome/components/graphical_display_menu/* @MrMDavidson
 | 
			
		||||
esphome/components/gree/* @orestismers
 | 
			
		||||
esphome/components/grove_tb6612fng/* @max246
 | 
			
		||||
esphome/components/growatt_solar/* @leeuwte
 | 
			
		||||
esphome/components/gt911/* @clydebarrow @jesserockz
 | 
			
		||||
esphome/components/haier/* @paveldn
 | 
			
		||||
esphome/components/havells_solar/* @sourabhjaiswal
 | 
			
		||||
esphome/components/hbridge/fan/* @WeekendWarrior
 | 
			
		||||
esphome/components/hbridge/light/* @DotNetDann
 | 
			
		||||
esphome/components/he60r/* @clydebarrow
 | 
			
		||||
esphome/components/heatpumpir/* @rob-deutsch
 | 
			
		||||
esphome/components/hitachi_ac424/* @sourabhjaiswal
 | 
			
		||||
esphome/components/hm3301/* @freekode
 | 
			
		||||
esphome/components/homeassistant/* @OttoWinter
 | 
			
		||||
esphome/components/honeywell_hih_i2c/* @Benichou34
 | 
			
		||||
esphome/components/honeywellabp/* @RubyBailey
 | 
			
		||||
esphome/components/honeywellabp2_i2c/* @jpfaff
 | 
			
		||||
esphome/components/host/* @esphome/core
 | 
			
		||||
@@ -149,7 +162,6 @@ esphome/components/integration/* @OttoWinter
 | 
			
		||||
esphome/components/internal_temperature/* @Mat931
 | 
			
		||||
esphome/components/interval/* @esphome/core
 | 
			
		||||
esphome/components/json/* @OttoWinter
 | 
			
		||||
esphome/components/kalman_combinator/* @Cat-Ion
 | 
			
		||||
esphome/components/key_collector/* @ssieb
 | 
			
		||||
esphome/components/key_provider/* @ssieb
 | 
			
		||||
esphome/components/kuntze/* @ssieb
 | 
			
		||||
@@ -187,6 +199,7 @@ esphome/components/mcp9808/* @k7hpn
 | 
			
		||||
esphome/components/md5/* @esphome/core
 | 
			
		||||
esphome/components/mdns/* @esphome/core
 | 
			
		||||
esphome/components/media_player/* @jesserockz
 | 
			
		||||
esphome/components/micro_wake_word/* @jesserockz @kahrendt
 | 
			
		||||
esphome/components/micronova/* @jorre05
 | 
			
		||||
esphome/components/microphone/* @jesserockz
 | 
			
		||||
esphome/components/mics_4514/* @jesserockz
 | 
			
		||||
@@ -216,7 +229,7 @@ esphome/components/nextion/binary_sensor/* @senexcrenshaw
 | 
			
		||||
esphome/components/nextion/sensor/* @senexcrenshaw
 | 
			
		||||
esphome/components/nextion/switch/* @senexcrenshaw
 | 
			
		||||
esphome/components/nextion/text_sensor/* @senexcrenshaw
 | 
			
		||||
esphome/components/nfc/* @jesserockz
 | 
			
		||||
esphome/components/nfc/* @jesserockz @kbx81
 | 
			
		||||
esphome/components/noblex/* @AGalfra
 | 
			
		||||
esphome/components/number/* @esphome/core
 | 
			
		||||
esphome/components/ota/* @esphome/core
 | 
			
		||||
@@ -233,11 +246,17 @@ esphome/components/pmwcs3/* @SeByDocKy
 | 
			
		||||
esphome/components/pn532/* @OttoWinter @jesserockz
 | 
			
		||||
esphome/components/pn532_i2c/* @OttoWinter @jesserockz
 | 
			
		||||
esphome/components/pn532_spi/* @OttoWinter @jesserockz
 | 
			
		||||
esphome/components/pn7150/* @jesserockz @kbx81
 | 
			
		||||
esphome/components/pn7150_i2c/* @jesserockz @kbx81
 | 
			
		||||
esphome/components/pn7160/* @jesserockz @kbx81
 | 
			
		||||
esphome/components/pn7160_i2c/* @jesserockz @kbx81
 | 
			
		||||
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/pvvx_mithermometer/* @pasiz
 | 
			
		||||
esphome/components/pylontech/* @functionpointer
 | 
			
		||||
esphome/components/qmp6988/* @andrewpc
 | 
			
		||||
esphome/components/qr_code/* @wjtje
 | 
			
		||||
esphome/components/qwiic_pir/* @kahrendt
 | 
			
		||||
@@ -301,6 +320,9 @@ esphome/components/ssd1331_base/* @kbx81
 | 
			
		||||
esphome/components/ssd1331_spi/* @kbx81
 | 
			
		||||
esphome/components/ssd1351_base/* @kbx81
 | 
			
		||||
esphome/components/ssd1351_spi/* @kbx81
 | 
			
		||||
esphome/components/st7567_base/* @latonita
 | 
			
		||||
esphome/components/st7567_i2c/* @latonita
 | 
			
		||||
esphome/components/st7567_spi/* @latonita
 | 
			
		||||
esphome/components/st7735/* @SenexCrenshaw
 | 
			
		||||
esphome/components/st7789v/* @kbx81
 | 
			
		||||
esphome/components/st7920/* @marsjan155
 | 
			
		||||
@@ -312,7 +334,7 @@ esphome/components/tca9548a/* @andreashergert1984
 | 
			
		||||
esphome/components/tcl112/* @glmnet
 | 
			
		||||
esphome/components/tee501/* @Stock-M
 | 
			
		||||
esphome/components/teleinfo/* @0hax
 | 
			
		||||
esphome/components/template/alarm_control_panel/* @grahambrown11
 | 
			
		||||
esphome/components/template/alarm_control_panel/* @grahambrown11 @hwstar
 | 
			
		||||
esphome/components/text/* @mauritskorse
 | 
			
		||||
esphome/components/thermostat/* @kbx81
 | 
			
		||||
esphome/components/time/* @OttoWinter
 | 
			
		||||
@@ -326,7 +348,7 @@ esphome/components/tmp1075/* @sybrenstuvel
 | 
			
		||||
esphome/components/tmp117/* @Azimath
 | 
			
		||||
esphome/components/tof10120/* @wstrzalka
 | 
			
		||||
esphome/components/toshiba/* @kbx81
 | 
			
		||||
esphome/components/touchscreen/* @jesserockz
 | 
			
		||||
esphome/components/touchscreen/* @jesserockz @nielsnl68
 | 
			
		||||
esphome/components/tsl2591/* @wjcarpenter
 | 
			
		||||
esphome/components/tt21100/* @kroimon
 | 
			
		||||
esphome/components/tuya/binary_sensor/* @jesserockz
 | 
			
		||||
@@ -342,9 +364,11 @@ esphome/components/ufire_ec/* @pvizeli
 | 
			
		||||
esphome/components/ufire_ise/* @pvizeli
 | 
			
		||||
esphome/components/ultrasonic/* @OttoWinter
 | 
			
		||||
esphome/components/vbus/* @ssieb
 | 
			
		||||
esphome/components/veml3235/* @kbx81
 | 
			
		||||
esphome/components/version/* @esphome/core
 | 
			
		||||
esphome/components/voice_assistant/* @jesserockz
 | 
			
		||||
esphome/components/wake_on_lan/* @willwill2will54
 | 
			
		||||
esphome/components/waveshare_epaper/* @clydebarrow
 | 
			
		||||
esphome/components/web_server_base/* @OttoWinter
 | 
			
		||||
esphome/components/web_server_idf/* @dentra
 | 
			
		||||
esphome/components/whirlpool/* @glmnet
 | 
			
		||||
@@ -359,6 +383,6 @@ esphome/components/xiaomi_mhoc303/* @drug123
 | 
			
		||||
esphome/components/xiaomi_mhoc401/* @vevsvevs
 | 
			
		||||
esphome/components/xiaomi_rtcgq02lm/* @jesserockz
 | 
			
		||||
esphome/components/xl9535/* @mreditor97
 | 
			
		||||
esphome/components/xpt2046/* @nielsnl68 @numo68
 | 
			
		||||
esphome/components/xpt2046/touchscreen/* @nielsnl68 @numo68
 | 
			
		||||
esphome/components/zhlt01/* @cfeenstra1024
 | 
			
		||||
esphome/components/zio_ultrasonic/* @kahrendt
 | 
			
		||||
 
 | 
			
		||||
@@ -10,5 +10,3 @@ Things to note when contributing:
 | 
			
		||||
   for more information.
 | 
			
		||||
 - Please also update the tests in the `tests/` folder. You can do so by just adding a line in one of the YAML files
 | 
			
		||||
   which checks if your new feature compiles correctly.
 | 
			
		||||
 - Sometimes I will let pull requests linger because I'm not 100% sure about them. Please feel free to ping
 | 
			
		||||
   me after some time.
 | 
			
		||||
 
 | 
			
		||||
@@ -34,10 +34,11 @@ RUN \
 | 
			
		||||
        python3-wheel=0.38.4-2 \
 | 
			
		||||
        iputils-ping=3:20221126-1 \
 | 
			
		||||
        git=1:2.39.2-1.1 \
 | 
			
		||||
        curl=7.88.1-10+deb12u4 \
 | 
			
		||||
        openssh-client=1:9.2p1-2+deb12u1 \
 | 
			
		||||
        curl=7.88.1-10+deb12u5 \
 | 
			
		||||
        openssh-client=1:9.2p1-2+deb12u2 \
 | 
			
		||||
        python3-cffi=1.15.1-5 \
 | 
			
		||||
        libcairo2=1.16.0-7 \
 | 
			
		||||
        libmagic1=1:5.44-3 \
 | 
			
		||||
        patch=2.7.6-7; \
 | 
			
		||||
    if [ "$TARGETARCH$TARGETVARIANT" = "armv7" ]; then \
 | 
			
		||||
        apt-get install -y --no-install-recommends \
 | 
			
		||||
@@ -49,7 +50,7 @@ RUN \
 | 
			
		||||
          libssl-dev=3.0.11-1~deb12u2 \
 | 
			
		||||
          libffi-dev=3.4.4-1 \
 | 
			
		||||
          libopenjp2-7=2.5.0-2 \
 | 
			
		||||
          libtiff6=4.5.0-6 \
 | 
			
		||||
          libtiff6=4.5.0-6+deb12u1 \
 | 
			
		||||
          cargo=0.66.0+ds1-1 \
 | 
			
		||||
          pkg-config=1.8.1-1 \
 | 
			
		||||
          gcc-arm-linux-gnueabihf=4:12.2.0-3; \
 | 
			
		||||
@@ -80,7 +81,7 @@ RUN \
 | 
			
		||||
    fi; \
 | 
			
		||||
    pip3 install \
 | 
			
		||||
    --break-system-packages --no-cache-dir \
 | 
			
		||||
    platformio==6.1.11 \
 | 
			
		||||
    platformio==6.1.13 \
 | 
			
		||||
    # Change some platformio settings
 | 
			
		||||
    && platformio settings set enable_telemetry No \
 | 
			
		||||
    && platformio settings set check_platformio_interval 1000000 \
 | 
			
		||||
 
 | 
			
		||||
@@ -1,13 +1,14 @@
 | 
			
		||||
#!/usr/bin/env python3
 | 
			
		||||
import re
 | 
			
		||||
import os
 | 
			
		||||
import argparse
 | 
			
		||||
import json
 | 
			
		||||
 | 
			
		||||
CHANNEL_DEV = "dev"
 | 
			
		||||
CHANNEL_BETA = "beta"
 | 
			
		||||
CHANNEL_RELEASE = "release"
 | 
			
		||||
 | 
			
		||||
GHCR = "ghcr"
 | 
			
		||||
DOCKERHUB = "dockerhub"
 | 
			
		||||
 | 
			
		||||
parser = argparse.ArgumentParser()
 | 
			
		||||
parser.add_argument(
 | 
			
		||||
    "--tag",
 | 
			
		||||
@@ -21,21 +22,31 @@ parser.add_argument(
 | 
			
		||||
    required=True,
 | 
			
		||||
    help="The suffix of the tag.",
 | 
			
		||||
)
 | 
			
		||||
parser.add_argument(
 | 
			
		||||
    "--registry",
 | 
			
		||||
    type=str,
 | 
			
		||||
    choices=[GHCR, DOCKERHUB],
 | 
			
		||||
    required=False,
 | 
			
		||||
    action="append",
 | 
			
		||||
    help="The registry to build tags for.",
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def main():
 | 
			
		||||
    args = parser.parse_args()
 | 
			
		||||
 | 
			
		||||
    # detect channel from tag
 | 
			
		||||
    match = re.match(r"^(\d+\.\d+)(?:\.\d+)?(b\d+)?$", args.tag)
 | 
			
		||||
    match = re.match(r"^(\d+\.\d+)(?:\.\d+)(?:(b\d+)|(-dev\d+))?$", args.tag)
 | 
			
		||||
    major_minor_version = None
 | 
			
		||||
    if match is None:
 | 
			
		||||
    if match is None:  # eg 2023.12.0-dev20231109-testbranch
 | 
			
		||||
        channel = None  # Ran with custom tag for a branch etc
 | 
			
		||||
    elif match.group(3) is not None:  # eg 2023.12.0-dev20231109
 | 
			
		||||
        channel = CHANNEL_DEV
 | 
			
		||||
    elif match.group(2) is None:
 | 
			
		||||
    elif match.group(2) is not None:  # eg 2023.12.0b1
 | 
			
		||||
        channel = CHANNEL_BETA
 | 
			
		||||
    else:  # eg 2023.12.0
 | 
			
		||||
        major_minor_version = match.group(1)
 | 
			
		||||
        channel = CHANNEL_RELEASE
 | 
			
		||||
    else:
 | 
			
		||||
        channel = CHANNEL_BETA
 | 
			
		||||
 | 
			
		||||
    tags_to_push = [args.tag]
 | 
			
		||||
    if channel == CHANNEL_DEV:
 | 
			
		||||
@@ -53,15 +64,28 @@ def main():
 | 
			
		||||
 | 
			
		||||
    suffix = f"-{args.suffix}" if args.suffix else ""
 | 
			
		||||
 | 
			
		||||
    with open(os.environ["GITHUB_OUTPUT"], "w") as f:
 | 
			
		||||
        print(f"channel={channel}", file=f)
 | 
			
		||||
        print(f"image=esphome/esphome{suffix}", file=f)
 | 
			
		||||
        full_tags = []
 | 
			
		||||
    image_name = f"esphome/esphome{suffix}"
 | 
			
		||||
 | 
			
		||||
        for tag in tags_to_push:
 | 
			
		||||
            full_tags += [f"ghcr.io/esphome/esphome{suffix}:{tag}"]
 | 
			
		||||
            full_tags += [f"esphome/esphome{suffix}:{tag}"]
 | 
			
		||||
        print(f"tags={','.join(full_tags)}", file=f)
 | 
			
		||||
    print(f"channel={channel}")
 | 
			
		||||
 | 
			
		||||
    if args.registry is None:
 | 
			
		||||
        args.registry = [GHCR, DOCKERHUB]
 | 
			
		||||
    elif len(args.registry) == 1:
 | 
			
		||||
        if GHCR in args.registry:
 | 
			
		||||
            print(f"image=ghcr.io/{image_name}")
 | 
			
		||||
        if DOCKERHUB in args.registry:
 | 
			
		||||
            print(f"image=docker.io/{image_name}")
 | 
			
		||||
 | 
			
		||||
    print(f"image_name={image_name}")
 | 
			
		||||
 | 
			
		||||
    full_tags = []
 | 
			
		||||
 | 
			
		||||
    for tag in tags_to_push:
 | 
			
		||||
        if GHCR in args.registry:
 | 
			
		||||
            full_tags += [f"ghcr.io/{image_name}:{tag}"]
 | 
			
		||||
        if DOCKERHUB in args.registry:
 | 
			
		||||
            full_tags += [f"docker.io/{image_name}:{tag}"]
 | 
			
		||||
    print(f"tags={','.join(full_tags)}")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,7 @@ import argcomplete
 | 
			
		||||
 | 
			
		||||
from esphome import const, writer, yaml_util
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
from esphome.config import iter_components, read_config, strip_default_ids
 | 
			
		||||
from esphome.config import iter_component_configs, read_config, strip_default_ids
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    ALLOWED_NAME_CHARS,
 | 
			
		||||
    CONF_BAUD_RATE,
 | 
			
		||||
@@ -196,7 +196,7 @@ def write_cpp(config):
 | 
			
		||||
def generate_cpp_contents(config):
 | 
			
		||||
    _LOGGER.info("Generating C++ source...")
 | 
			
		||||
 | 
			
		||||
    for name, component, conf in iter_components(CORE.config):
 | 
			
		||||
    for name, component, conf in iter_component_configs(CORE.config):
 | 
			
		||||
        if component.to_code is not None:
 | 
			
		||||
            coro = wrap_to_code(name, component)
 | 
			
		||||
            CORE.add_job(coro, conf)
 | 
			
		||||
@@ -389,7 +389,8 @@ def command_config(args, config):
 | 
			
		||||
        output = re.sub(
 | 
			
		||||
            r"(password|key|psk|ssid)\: (.+)", r"\1: \\033[5m\2\\033[6m", output
 | 
			
		||||
        )
 | 
			
		||||
    safe_print(output)
 | 
			
		||||
    if not CORE.quiet:
 | 
			
		||||
        safe_print(output)
 | 
			
		||||
    _LOGGER.info("Configuration is valid!")
 | 
			
		||||
    return 0
 | 
			
		||||
 | 
			
		||||
@@ -514,7 +515,7 @@ def command_clean(args, config):
 | 
			
		||||
def command_dashboard(args):
 | 
			
		||||
    from esphome.dashboard import dashboard
 | 
			
		||||
 | 
			
		||||
    return dashboard.start_web_server(args)
 | 
			
		||||
    return dashboard.start_dashboard(args)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def command_update_all(args):
 | 
			
		||||
 
 | 
			
		||||
@@ -8,50 +8,37 @@ namespace esphome {
 | 
			
		||||
namespace a01nyub {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "a01nyub.sensor";
 | 
			
		||||
static const uint8_t MAX_DATA_LENGTH_BYTES = 4;
 | 
			
		||||
 | 
			
		||||
void A01nyubComponent::loop() {
 | 
			
		||||
  uint8_t data;
 | 
			
		||||
  while (this->available() > 0) {
 | 
			
		||||
    if (this->read_byte(&data)) {
 | 
			
		||||
      buffer_.push_back(data);
 | 
			
		||||
    this->read_byte(&data);
 | 
			
		||||
    if (this->buffer_.empty() && (data != 0xff))
 | 
			
		||||
      continue;
 | 
			
		||||
    buffer_.push_back(data);
 | 
			
		||||
    if (this->buffer_.size() == 4)
 | 
			
		||||
      this->check_buffer_();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void A01nyubComponent::check_buffer_() {
 | 
			
		||||
  if (this->buffer_.size() >= MAX_DATA_LENGTH_BYTES) {
 | 
			
		||||
    size_t i;
 | 
			
		||||
    for (i = 0; i < this->buffer_.size(); i++) {
 | 
			
		||||
      // Look for the first packet
 | 
			
		||||
      if (this->buffer_[i] == 0xFF) {
 | 
			
		||||
        if (i + 1 + 3 < this->buffer_.size()) {  // Packet is not complete
 | 
			
		||||
          return;                                // Wait for completion
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        uint8_t checksum = (this->buffer_[i] + this->buffer_[i + 1] + this->buffer_[i + 2]) & 0xFF;
 | 
			
		||||
        if (this->buffer_[i + 3] == checksum) {
 | 
			
		||||
          float distance = (this->buffer_[i + 1] << 8) + this->buffer_[i + 2];
 | 
			
		||||
          if (distance > 280) {
 | 
			
		||||
            float meters = distance / 1000.0;
 | 
			
		||||
            ESP_LOGV(TAG, "Distance from sensor: %f mm, %f m", distance, meters);
 | 
			
		||||
            this->publish_state(meters);
 | 
			
		||||
          } else {
 | 
			
		||||
            ESP_LOGW(TAG, "Invalid data read from sensor: %s", format_hex_pretty(this->buffer_).c_str());
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
  uint8_t checksum = this->buffer_[0] + this->buffer_[1] + this->buffer_[2];
 | 
			
		||||
  if (this->buffer_[3] == checksum) {
 | 
			
		||||
    float distance = (this->buffer_[1] << 8) + this->buffer_[2];
 | 
			
		||||
    if (distance > 280) {
 | 
			
		||||
      float meters = distance / 1000.0;
 | 
			
		||||
      ESP_LOGV(TAG, "Distance from sensor: %f mm, %f m", distance, meters);
 | 
			
		||||
      this->publish_state(meters);
 | 
			
		||||
    } else {
 | 
			
		||||
      ESP_LOGW(TAG, "Invalid data read from sensor: %s", format_hex_pretty(this->buffer_).c_str());
 | 
			
		||||
    }
 | 
			
		||||
    this->buffer_.clear();
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGW(TAG, "checksum failed: %02x != %02x", checksum, this->buffer_[3]);
 | 
			
		||||
  }
 | 
			
		||||
  this->buffer_.clear();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void A01nyubComponent::dump_config() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "A01nyub Sensor:");
 | 
			
		||||
  LOG_SENSOR("  ", "Distance", this);
 | 
			
		||||
}
 | 
			
		||||
void A01nyubComponent::dump_config() { LOG_SENSOR("", "A01nyub Sensor", this); }
 | 
			
		||||
 | 
			
		||||
}  // namespace a01nyub
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										1
									
								
								esphome/components/a02yyuw/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								esphome/components/a02yyuw/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
CODEOWNERS = ["@TH-Braemer"]
 | 
			
		||||
							
								
								
									
										43
									
								
								esphome/components/a02yyuw/a02yyuw.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								esphome/components/a02yyuw/a02yyuw.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,43 @@
 | 
			
		||||
// Datasheet https://wiki.dfrobot.com/_A02YYUW_Waterproof_Ultrasonic_Sensor_SKU_SEN0311
 | 
			
		||||
 | 
			
		||||
#include "a02yyuw.h"
 | 
			
		||||
#include "esphome/core/helpers.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace a02yyuw {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "a02yyuw.sensor";
 | 
			
		||||
 | 
			
		||||
void A02yyuwComponent::loop() {
 | 
			
		||||
  uint8_t data;
 | 
			
		||||
  while (this->available() > 0) {
 | 
			
		||||
    this->read_byte(&data);
 | 
			
		||||
    if (this->buffer_.empty() && (data != 0xff))
 | 
			
		||||
      continue;
 | 
			
		||||
    buffer_.push_back(data);
 | 
			
		||||
    if (this->buffer_.size() == 4)
 | 
			
		||||
      this->check_buffer_();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void A02yyuwComponent::check_buffer_() {
 | 
			
		||||
  uint8_t checksum = this->buffer_[0] + this->buffer_[1] + this->buffer_[2];
 | 
			
		||||
  if (this->buffer_[3] == checksum) {
 | 
			
		||||
    float distance = (this->buffer_[1] << 8) + this->buffer_[2];
 | 
			
		||||
    if (distance > 30) {
 | 
			
		||||
      ESP_LOGV(TAG, "Distance from sensor: %f mm", distance);
 | 
			
		||||
      this->publish_state(distance);
 | 
			
		||||
    } else {
 | 
			
		||||
      ESP_LOGW(TAG, "Invalid data read from sensor: %s", format_hex_pretty(this->buffer_).c_str());
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGW(TAG, "checksum failed: %02x != %02x", checksum, this->buffer_[3]);
 | 
			
		||||
  }
 | 
			
		||||
  this->buffer_.clear();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void A02yyuwComponent::dump_config() { LOG_SENSOR("", "A02yyuw Sensor", this); }
 | 
			
		||||
 | 
			
		||||
}  // namespace a02yyuw
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										27
									
								
								esphome/components/a02yyuw/a02yyuw.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								esphome/components/a02yyuw/a02yyuw.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/components/sensor/sensor.h"
 | 
			
		||||
#include "esphome/components/uart/uart.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace a02yyuw {
 | 
			
		||||
 | 
			
		||||
class A02yyuwComponent : public sensor::Sensor, public Component, public uart::UARTDevice {
 | 
			
		||||
 public:
 | 
			
		||||
  // Nothing really public.
 | 
			
		||||
 | 
			
		||||
  // ========== INTERNAL METHODS ==========
 | 
			
		||||
  void loop() override;
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void check_buffer_();
 | 
			
		||||
 | 
			
		||||
  std::vector<uint8_t> buffer_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace a02yyuw
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										41
									
								
								esphome/components/a02yyuw/sensor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								esphome/components/a02yyuw/sensor.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,41 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
from esphome.components import sensor, uart
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    STATE_CLASS_MEASUREMENT,
 | 
			
		||||
    ICON_ARROW_EXPAND_VERTICAL,
 | 
			
		||||
    DEVICE_CLASS_DISTANCE,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CODEOWNERS = ["@TH-Braemer"]
 | 
			
		||||
DEPENDENCIES = ["uart"]
 | 
			
		||||
UNIT_MILLIMETERS = "mm"
 | 
			
		||||
 | 
			
		||||
a02yyuw_ns = cg.esphome_ns.namespace("a02yyuw")
 | 
			
		||||
A02yyuwComponent = a02yyuw_ns.class_(
 | 
			
		||||
    "A02yyuwComponent", sensor.Sensor, cg.Component, uart.UARTDevice
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = sensor.sensor_schema(
 | 
			
		||||
    A02yyuwComponent,
 | 
			
		||||
    unit_of_measurement=UNIT_MILLIMETERS,
 | 
			
		||||
    icon=ICON_ARROW_EXPAND_VERTICAL,
 | 
			
		||||
    accuracy_decimals=0,
 | 
			
		||||
    state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
    device_class=DEVICE_CLASS_DISTANCE,
 | 
			
		||||
).extend(uart.UART_DEVICE_SCHEMA)
 | 
			
		||||
 | 
			
		||||
FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema(
 | 
			
		||||
    "a02yyuw",
 | 
			
		||||
    baud_rate=9600,
 | 
			
		||||
    require_tx=False,
 | 
			
		||||
    require_rx=True,
 | 
			
		||||
    data_bits=8,
 | 
			
		||||
    parity=None,
 | 
			
		||||
    stop_bits=1,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    var = await sensor.new_sensor(config)
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
    await uart.register_uart_device(var, config)
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome import pins
 | 
			
		||||
from esphome.const import CONF_ANALOG, CONF_INPUT
 | 
			
		||||
from esphome.const import CONF_ANALOG, CONF_INPUT, CONF_NUMBER
 | 
			
		||||
 | 
			
		||||
from esphome.core import CORE
 | 
			
		||||
from esphome.components.esp32 import get_esp32_variant
 | 
			
		||||
@@ -139,6 +139,9 @@ ESP32_VARIANT_ADC2_PIN_TO_CHANNEL = {
 | 
			
		||||
    VARIANT_ESP32C3: {
 | 
			
		||||
        5: adc2_channel_t.ADC2_CHANNEL_0,
 | 
			
		||||
    },
 | 
			
		||||
    VARIANT_ESP32C2: {},
 | 
			
		||||
    VARIANT_ESP32C6: {},
 | 
			
		||||
    VARIANT_ESP32H2: {},
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -152,7 +155,8 @@ def validate_adc_pin(value):
 | 
			
		||||
        return cv.only_on_rp2040("TEMPERATURE")
 | 
			
		||||
 | 
			
		||||
    if CORE.is_esp32:
 | 
			
		||||
        value = pins.internal_gpio_input_pin_number(value)
 | 
			
		||||
        conf = pins.internal_gpio_input_pin_schema(value)
 | 
			
		||||
        value = conf[CONF_NUMBER]
 | 
			
		||||
        variant = get_esp32_variant()
 | 
			
		||||
        if (
 | 
			
		||||
            variant not in ESP32_VARIANT_ADC1_PIN_TO_CHANNEL
 | 
			
		||||
@@ -166,24 +170,23 @@ def validate_adc_pin(value):
 | 
			
		||||
        ):
 | 
			
		||||
            raise cv.Invalid(f"{variant} doesn't support ADC on this pin")
 | 
			
		||||
 | 
			
		||||
        return pins.internal_gpio_input_pin_schema(value)
 | 
			
		||||
        return conf
 | 
			
		||||
 | 
			
		||||
    if CORE.is_esp8266:
 | 
			
		||||
        value = pins.internal_gpio_pin_number({CONF_ANALOG: True, CONF_INPUT: True})(
 | 
			
		||||
            value
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        if value != 17:  # A0
 | 
			
		||||
            raise cv.Invalid("ESP8266: Only pin A0 (GPIO17) supports ADC")
 | 
			
		||||
        return pins.gpio_pin_schema(
 | 
			
		||||
        conf = pins.gpio_pin_schema(
 | 
			
		||||
            {CONF_ANALOG: True, CONF_INPUT: True}, internal=True
 | 
			
		||||
        )(value)
 | 
			
		||||
 | 
			
		||||
        if conf[CONF_NUMBER] != 17:  # A0
 | 
			
		||||
            raise cv.Invalid("ESP8266: Only pin A0 (GPIO17) supports ADC")
 | 
			
		||||
        return conf
 | 
			
		||||
 | 
			
		||||
    if CORE.is_rp2040:
 | 
			
		||||
        value = pins.internal_gpio_input_pin_number(value)
 | 
			
		||||
        if value not in (26, 27, 28, 29):
 | 
			
		||||
        conf = pins.internal_gpio_input_pin_schema(value)
 | 
			
		||||
        number = conf[CONF_NUMBER]
 | 
			
		||||
        if number not in (26, 27, 28, 29):
 | 
			
		||||
            raise cv.Invalid("RP2040: Only pins 26, 27, 28 and 29 support ADC")
 | 
			
		||||
        return pins.internal_gpio_input_pin_schema(value)
 | 
			
		||||
        return conf
 | 
			
		||||
 | 
			
		||||
    if CORE.is_libretiny:
 | 
			
		||||
        return pins.gpio_pin_schema(
 | 
			
		||||
 
 | 
			
		||||
@@ -10,7 +10,7 @@
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace addressable_light {
 | 
			
		||||
 | 
			
		||||
class AddressableLightDisplay : public display::DisplayBuffer, public PollingComponent {
 | 
			
		||||
class AddressableLightDisplay : public display::DisplayBuffer {
 | 
			
		||||
 public:
 | 
			
		||||
  light::AddressableLight *get_light() const { return this->light_; }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -45,7 +45,6 @@ async def to_code(config):
 | 
			
		||||
    cg.add(var.set_height(config[CONF_HEIGHT]))
 | 
			
		||||
    cg.add(var.set_light(wrapped_light))
 | 
			
		||||
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
    await display.register_display(var, config)
 | 
			
		||||
 | 
			
		||||
    if pixel_mapper := config.get(CONF_PIXEL_MAPPER):
 | 
			
		||||
 
 | 
			
		||||
@@ -21,36 +21,49 @@ namespace esphome {
 | 
			
		||||
namespace aht10 {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "aht10";
 | 
			
		||||
static const uint8_t AHT10_CALIBRATE_CMD[] = {0xE1};
 | 
			
		||||
static const size_t SIZE_CALIBRATE_CMD = 3;
 | 
			
		||||
static const uint8_t AHT10_CALIBRATE_CMD[] = {0xE1, 0x08, 0x00};
 | 
			
		||||
static const uint8_t AHT20_CALIBRATE_CMD[] = {0xBE, 0x08, 0x00};
 | 
			
		||||
static const uint8_t AHT10_MEASURE_CMD[] = {0xAC, 0x33, 0x00};
 | 
			
		||||
static const uint8_t AHT10_DEFAULT_DELAY = 5;    // ms, for calibration and temperature measurement
 | 
			
		||||
static const uint8_t AHT10_HUMIDITY_DELAY = 30;  // ms
 | 
			
		||||
static const uint8_t AHT10_ATTEMPTS = 3;         // safety margin, normally 3 attempts are enough: 3*30=90ms
 | 
			
		||||
static const uint8_t AHT10_CAL_ATTEMPTS = 10;
 | 
			
		||||
static const uint8_t AHT10_STATUS_BUSY = 0x80;
 | 
			
		||||
 | 
			
		||||
void AHT10Component::setup() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Setting up AHT10...");
 | 
			
		||||
  const uint8_t *calibrate_cmd;
 | 
			
		||||
  switch (this->variant_) {
 | 
			
		||||
    case AHT10Variant::AHT20:
 | 
			
		||||
      calibrate_cmd = AHT20_CALIBRATE_CMD;
 | 
			
		||||
      ESP_LOGCONFIG(TAG, "Setting up AHT20");
 | 
			
		||||
      break;
 | 
			
		||||
    case AHT10Variant::AHT10:
 | 
			
		||||
    default:
 | 
			
		||||
      calibrate_cmd = AHT10_CALIBRATE_CMD;
 | 
			
		||||
      ESP_LOGCONFIG(TAG, "Setting up AHT10");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (!this->write_bytes(0, AHT10_CALIBRATE_CMD, sizeof(AHT10_CALIBRATE_CMD))) {
 | 
			
		||||
  if (this->write(calibrate_cmd, SIZE_CALIBRATE_CMD) != i2c::ERROR_OK) {
 | 
			
		||||
    ESP_LOGE(TAG, "Communication with AHT10 failed!");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  uint8_t data = 0;
 | 
			
		||||
  if (this->write(&data, 1) != i2c::ERROR_OK) {
 | 
			
		||||
    ESP_LOGD(TAG, "Communication with AHT10 failed!");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  delay(AHT10_DEFAULT_DELAY);
 | 
			
		||||
  if (this->read(&data, 1) != i2c::ERROR_OK) {
 | 
			
		||||
    ESP_LOGD(TAG, "Communication with AHT10 failed!");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (this->read(&data, 1) != i2c::ERROR_OK) {
 | 
			
		||||
    ESP_LOGD(TAG, "Communication with AHT10 failed!");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  uint8_t data = AHT10_STATUS_BUSY;
 | 
			
		||||
  int cal_attempts = 0;
 | 
			
		||||
  while (data & AHT10_STATUS_BUSY) {
 | 
			
		||||
    delay(AHT10_DEFAULT_DELAY);
 | 
			
		||||
    if (this->read(&data, 1) != i2c::ERROR_OK) {
 | 
			
		||||
      ESP_LOGE(TAG, "Communication with AHT10 failed!");
 | 
			
		||||
      this->mark_failed();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    ++cal_attempts;
 | 
			
		||||
    if (cal_attempts > AHT10_CAL_ATTEMPTS) {
 | 
			
		||||
      ESP_LOGE(TAG, "AHT10 calibration timed out!");
 | 
			
		||||
      this->mark_failed();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  if ((data & 0x68) != 0x08) {  // Bit[6:5] = 0b00, NORMAL mode and Bit[3] = 0b1, CALIBRATED
 | 
			
		||||
    ESP_LOGE(TAG, "AHT10 calibration failed!");
 | 
			
		||||
@@ -62,7 +75,7 @@ void AHT10Component::setup() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void AHT10Component::update() {
 | 
			
		||||
  if (!this->write_bytes(0, AHT10_MEASURE_CMD, sizeof(AHT10_MEASURE_CMD))) {
 | 
			
		||||
  if (this->write(AHT10_MEASURE_CMD, sizeof(AHT10_MEASURE_CMD)) != i2c::ERROR_OK) {
 | 
			
		||||
    ESP_LOGE(TAG, "Communication with AHT10 failed!");
 | 
			
		||||
    this->status_set_warning();
 | 
			
		||||
    return;
 | 
			
		||||
@@ -89,7 +102,7 @@ void AHT10Component::update() {
 | 
			
		||||
        break;
 | 
			
		||||
      } else {
 | 
			
		||||
        ESP_LOGD(TAG, "ATH10 Unrealistic humidity (0x0), retrying...");
 | 
			
		||||
        if (!this->write_bytes(0, AHT10_MEASURE_CMD, sizeof(AHT10_MEASURE_CMD))) {
 | 
			
		||||
        if (this->write(AHT10_MEASURE_CMD, sizeof(AHT10_MEASURE_CMD)) != i2c::ERROR_OK) {
 | 
			
		||||
          ESP_LOGE(TAG, "Communication with AHT10 failed!");
 | 
			
		||||
          this->status_set_warning();
 | 
			
		||||
          return;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,7 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include <utility>
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/components/sensor/sensor.h"
 | 
			
		||||
#include "esphome/components/i2c/i2c.h"
 | 
			
		||||
@@ -7,12 +9,15 @@
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace aht10 {
 | 
			
		||||
 | 
			
		||||
enum AHT10Variant { AHT10, AHT20 };
 | 
			
		||||
 | 
			
		||||
class AHT10Component : public PollingComponent, public i2c::I2CDevice {
 | 
			
		||||
 public:
 | 
			
		||||
  void setup() override;
 | 
			
		||||
  void update() override;
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
  float get_setup_priority() const override;
 | 
			
		||||
  void set_variant(AHT10Variant variant) { this->variant_ = variant; }
 | 
			
		||||
 | 
			
		||||
  void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; }
 | 
			
		||||
  void set_humidity_sensor(sensor::Sensor *humidity_sensor) { humidity_sensor_ = humidity_sensor; }
 | 
			
		||||
@@ -20,6 +25,7 @@ class AHT10Component : public PollingComponent, public i2c::I2CDevice {
 | 
			
		||||
 protected:
 | 
			
		||||
  sensor::Sensor *temperature_sensor_{nullptr};
 | 
			
		||||
  sensor::Sensor *humidity_sensor_{nullptr};
 | 
			
		||||
  AHT10Variant variant_{};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace aht10
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,7 @@ from esphome.const import (
 | 
			
		||||
    STATE_CLASS_MEASUREMENT,
 | 
			
		||||
    UNIT_CELSIUS,
 | 
			
		||||
    UNIT_PERCENT,
 | 
			
		||||
    CONF_VARIANT,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["i2c"]
 | 
			
		||||
@@ -17,6 +18,12 @@ DEPENDENCIES = ["i2c"]
 | 
			
		||||
aht10_ns = cg.esphome_ns.namespace("aht10")
 | 
			
		||||
AHT10Component = aht10_ns.class_("AHT10Component", cg.PollingComponent, i2c.I2CDevice)
 | 
			
		||||
 | 
			
		||||
AHT10Variant = aht10_ns.enum("AHT10Variant")
 | 
			
		||||
AHT10_VARIANTS = {
 | 
			
		||||
    "AHT10": AHT10Variant.AHT10,
 | 
			
		||||
    "AHT20": AHT10Variant.AHT20,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = (
 | 
			
		||||
    cv.Schema(
 | 
			
		||||
        {
 | 
			
		||||
@@ -33,6 +40,9 @@ CONFIG_SCHEMA = (
 | 
			
		||||
                device_class=DEVICE_CLASS_HUMIDITY,
 | 
			
		||||
                state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_VARIANT, default="AHT10"): cv.enum(
 | 
			
		||||
                AHT10_VARIANTS, upper=True
 | 
			
		||||
            ),
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
    .extend(cv.polling_component_schema("60s"))
 | 
			
		||||
@@ -44,6 +54,7 @@ async def to_code(config):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
    await i2c.register_i2c_device(var, config)
 | 
			
		||||
    cg.add(var.set_variant(config[CONF_VARIANT]))
 | 
			
		||||
 | 
			
		||||
    if temperature := config.get(CONF_TEMPERATURE):
 | 
			
		||||
        sens = await sensor.new_sensor(temperature)
 | 
			
		||||
 
 | 
			
		||||
@@ -11,7 +11,7 @@ from esphome.const import (
 | 
			
		||||
)
 | 
			
		||||
from esphome.cpp_helpers import setup_entity
 | 
			
		||||
 | 
			
		||||
CODEOWNERS = ["@grahambrown11"]
 | 
			
		||||
CODEOWNERS = ["@grahambrown11", "@hwstar"]
 | 
			
		||||
IS_PLATFORM_COMPONENT = True
 | 
			
		||||
 | 
			
		||||
CONF_ON_TRIGGERED = "on_triggered"
 | 
			
		||||
@@ -22,6 +22,8 @@ CONF_ON_ARMED_HOME = "on_armed_home"
 | 
			
		||||
CONF_ON_ARMED_NIGHT = "on_armed_night"
 | 
			
		||||
CONF_ON_ARMED_AWAY = "on_armed_away"
 | 
			
		||||
CONF_ON_DISARMED = "on_disarmed"
 | 
			
		||||
CONF_ON_CHIME = "on_chime"
 | 
			
		||||
CONF_ON_READY = "on_ready"
 | 
			
		||||
 | 
			
		||||
alarm_control_panel_ns = cg.esphome_ns.namespace("alarm_control_panel")
 | 
			
		||||
AlarmControlPanel = alarm_control_panel_ns.class_("AlarmControlPanel", cg.EntityBase)
 | 
			
		||||
@@ -53,12 +55,22 @@ ArmedAwayTrigger = alarm_control_panel_ns.class_(
 | 
			
		||||
DisarmedTrigger = alarm_control_panel_ns.class_(
 | 
			
		||||
    "DisarmedTrigger", automation.Trigger.template()
 | 
			
		||||
)
 | 
			
		||||
ChimeTrigger = alarm_control_panel_ns.class_(
 | 
			
		||||
    "ChimeTrigger", automation.Trigger.template()
 | 
			
		||||
)
 | 
			
		||||
ReadyTrigger = alarm_control_panel_ns.class_(
 | 
			
		||||
    "ReadyTrigger", automation.Trigger.template()
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
ArmAwayAction = alarm_control_panel_ns.class_("ArmAwayAction", automation.Action)
 | 
			
		||||
ArmHomeAction = alarm_control_panel_ns.class_("ArmHomeAction", automation.Action)
 | 
			
		||||
ArmNightAction = alarm_control_panel_ns.class_("ArmNightAction", automation.Action)
 | 
			
		||||
DisarmAction = alarm_control_panel_ns.class_("DisarmAction", automation.Action)
 | 
			
		||||
PendingAction = alarm_control_panel_ns.class_("PendingAction", automation.Action)
 | 
			
		||||
TriggeredAction = alarm_control_panel_ns.class_("TriggeredAction", automation.Action)
 | 
			
		||||
ChimeAction = alarm_control_panel_ns.class_("ChimeAction", automation.Action)
 | 
			
		||||
ReadyAction = alarm_control_panel_ns.class_("ReadyAction", automation.Action)
 | 
			
		||||
 | 
			
		||||
AlarmControlPanelCondition = alarm_control_panel_ns.class_(
 | 
			
		||||
    "AlarmControlPanelCondition", automation.Condition
 | 
			
		||||
)
 | 
			
		||||
@@ -111,6 +123,16 @@ ALARM_CONTROL_PANEL_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(
 | 
			
		||||
                cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ClearedTrigger),
 | 
			
		||||
            }
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_ON_CHIME): automation.validate_automation(
 | 
			
		||||
            {
 | 
			
		||||
                cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ChimeTrigger),
 | 
			
		||||
            }
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_ON_READY): automation.validate_automation(
 | 
			
		||||
            {
 | 
			
		||||
                cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ReadyTrigger),
 | 
			
		||||
            }
 | 
			
		||||
        ),
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -157,6 +179,12 @@ async def setup_alarm_control_panel_core_(var, config):
 | 
			
		||||
    for conf in config.get(CONF_ON_CLEARED, []):
 | 
			
		||||
        trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
 | 
			
		||||
        await automation.build_automation(trigger, [], conf)
 | 
			
		||||
    for conf in config.get(CONF_ON_CHIME, []):
 | 
			
		||||
        trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
 | 
			
		||||
        await automation.build_automation(trigger, [], conf)
 | 
			
		||||
    for conf in config.get(CONF_ON_READY, []):
 | 
			
		||||
        trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
 | 
			
		||||
        await automation.build_automation(trigger, [], conf)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def register_alarm_control_panel(var, config):
 | 
			
		||||
@@ -232,6 +260,29 @@ async def alarm_action_trigger_to_code(config, action_id, template_arg, args):
 | 
			
		||||
    return var
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@automation.register_action(
 | 
			
		||||
    "alarm_control_panel.chime", ChimeAction, ALARM_CONTROL_PANEL_ACTION_SCHEMA
 | 
			
		||||
)
 | 
			
		||||
async def alarm_action_chime_to_code(config, action_id, template_arg, args):
 | 
			
		||||
    paren = await cg.get_variable(config[CONF_ID])
 | 
			
		||||
    var = cg.new_Pvariable(action_id, template_arg, paren)
 | 
			
		||||
    return var
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@automation.register_action(
 | 
			
		||||
    "alarm_control_panel.ready", ReadyAction, ALARM_CONTROL_PANEL_ACTION_SCHEMA
 | 
			
		||||
)
 | 
			
		||||
@automation.register_condition(
 | 
			
		||||
    "alarm_control_panel.ready",
 | 
			
		||||
    AlarmControlPanelCondition,
 | 
			
		||||
    ALARM_CONTROL_PANEL_CONDITION_SCHEMA,
 | 
			
		||||
)
 | 
			
		||||
async def alarm_action_ready_to_code(config, action_id, template_arg, args):
 | 
			
		||||
    paren = await cg.get_variable(config[CONF_ID])
 | 
			
		||||
    var = cg.new_Pvariable(action_id, template_arg, paren)
 | 
			
		||||
    return var
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@automation.register_condition(
 | 
			
		||||
    "alarm_control_panel.is_armed",
 | 
			
		||||
    AlarmControlPanelCondition,
 | 
			
		||||
 
 | 
			
		||||
@@ -96,6 +96,14 @@ void AlarmControlPanel::add_on_cleared_callback(std::function<void()> &&callback
 | 
			
		||||
  this->cleared_callback_.add(std::move(callback));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void AlarmControlPanel::add_on_chime_callback(std::function<void()> &&callback) {
 | 
			
		||||
  this->chime_callback_.add(std::move(callback));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void AlarmControlPanel::add_on_ready_callback(std::function<void()> &&callback) {
 | 
			
		||||
  this->ready_callback_.add(std::move(callback));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void AlarmControlPanel::arm_away(optional<std::string> code) {
 | 
			
		||||
  auto call = this->make_call();
 | 
			
		||||
  call.arm_away();
 | 
			
		||||
 
 | 
			
		||||
@@ -89,6 +89,18 @@ class AlarmControlPanel : public EntityBase {
 | 
			
		||||
   */
 | 
			
		||||
  void add_on_cleared_callback(std::function<void()> &&callback);
 | 
			
		||||
 | 
			
		||||
  /** Add a callback for when a chime zone goes from closed to open
 | 
			
		||||
   *
 | 
			
		||||
   * @param callback The callback function
 | 
			
		||||
   */
 | 
			
		||||
  void add_on_chime_callback(std::function<void()> &&callback);
 | 
			
		||||
 | 
			
		||||
  /** Add a callback for when a ready state changes
 | 
			
		||||
   *
 | 
			
		||||
   * @param callback The callback function
 | 
			
		||||
   */
 | 
			
		||||
  void add_on_ready_callback(std::function<void()> &&callback);
 | 
			
		||||
 | 
			
		||||
  /** A numeric representation of the supported features as per HomeAssistant
 | 
			
		||||
   *
 | 
			
		||||
   */
 | 
			
		||||
@@ -178,6 +190,10 @@ class AlarmControlPanel : public EntityBase {
 | 
			
		||||
  CallbackManager<void()> disarmed_callback_{};
 | 
			
		||||
  // clear callback
 | 
			
		||||
  CallbackManager<void()> cleared_callback_{};
 | 
			
		||||
  // chime callback
 | 
			
		||||
  CallbackManager<void()> chime_callback_{};
 | 
			
		||||
  // ready callback
 | 
			
		||||
  CallbackManager<void()> ready_callback_{};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace alarm_control_panel
 | 
			
		||||
 
 | 
			
		||||
@@ -69,6 +69,20 @@ class ClearedTrigger : public Trigger<> {
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class ChimeTrigger : public Trigger<> {
 | 
			
		||||
 public:
 | 
			
		||||
  explicit ChimeTrigger(AlarmControlPanel *alarm_control_panel) {
 | 
			
		||||
    alarm_control_panel->add_on_chime_callback([this]() { this->trigger(); });
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class ReadyTrigger : public Trigger<> {
 | 
			
		||||
 public:
 | 
			
		||||
  explicit ReadyTrigger(AlarmControlPanel *alarm_control_panel) {
 | 
			
		||||
    alarm_control_panel->add_on_ready_callback([this]() { this->trigger(); });
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
template<typename... Ts> class ArmAwayAction : public Action<Ts...> {
 | 
			
		||||
 public:
 | 
			
		||||
  explicit ArmAwayAction(AlarmControlPanel *alarm_control_panel) : alarm_control_panel_(alarm_control_panel) {}
 | 
			
		||||
 
 | 
			
		||||
@@ -365,6 +365,7 @@ message ListEntitiesFanResponse {
 | 
			
		||||
  bool disabled_by_default = 9;
 | 
			
		||||
  string icon = 10;
 | 
			
		||||
  EntityCategory entity_category = 11;
 | 
			
		||||
  repeated string supported_preset_modes = 12;
 | 
			
		||||
}
 | 
			
		||||
enum FanSpeed {
 | 
			
		||||
  FAN_SPEED_LOW = 0;
 | 
			
		||||
@@ -387,6 +388,7 @@ message FanStateResponse {
 | 
			
		||||
  FanSpeed speed = 4 [deprecated = true];
 | 
			
		||||
  FanDirection direction = 5;
 | 
			
		||||
  int32 speed_level = 6;
 | 
			
		||||
  string preset_mode = 7;
 | 
			
		||||
}
 | 
			
		||||
message FanCommandRequest {
 | 
			
		||||
  option (id) = 31;
 | 
			
		||||
@@ -405,6 +407,8 @@ message FanCommandRequest {
 | 
			
		||||
  FanDirection direction = 9;
 | 
			
		||||
  bool has_speed_level = 10;
 | 
			
		||||
  int32 speed_level = 11;
 | 
			
		||||
  bool has_preset_mode = 12;
 | 
			
		||||
  string preset_mode = 13;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==================== LIGHT ====================
 | 
			
		||||
@@ -855,6 +859,10 @@ message ListEntitiesClimateResponse {
 | 
			
		||||
  string icon = 19;
 | 
			
		||||
  EntityCategory entity_category = 20;
 | 
			
		||||
  float visual_current_temperature_step = 21;
 | 
			
		||||
  bool supports_current_humidity = 22;
 | 
			
		||||
  bool supports_target_humidity = 23;
 | 
			
		||||
  float visual_min_humidity = 24;
 | 
			
		||||
  float visual_max_humidity = 25;
 | 
			
		||||
}
 | 
			
		||||
message ClimateStateResponse {
 | 
			
		||||
  option (id) = 47;
 | 
			
		||||
@@ -875,6 +883,8 @@ message ClimateStateResponse {
 | 
			
		||||
  string custom_fan_mode = 11;
 | 
			
		||||
  ClimatePreset preset = 12;
 | 
			
		||||
  string custom_preset = 13;
 | 
			
		||||
  float current_humidity = 14;
 | 
			
		||||
  float target_humidity = 15;
 | 
			
		||||
}
 | 
			
		||||
message ClimateCommandRequest {
 | 
			
		||||
  option (id) = 48;
 | 
			
		||||
@@ -903,6 +913,8 @@ message ClimateCommandRequest {
 | 
			
		||||
  ClimatePreset preset = 19;
 | 
			
		||||
  bool has_custom_preset = 20;
 | 
			
		||||
  string custom_preset = 21;
 | 
			
		||||
  bool has_target_humidity = 22;
 | 
			
		||||
  float target_humidity = 23;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==================== NUMBER ====================
 | 
			
		||||
 
 | 
			
		||||
@@ -118,7 +118,9 @@ void APIConnection::loop() {
 | 
			
		||||
  this->list_entities_iterator_.advance();
 | 
			
		||||
  this->initial_state_iterator_.advance();
 | 
			
		||||
 | 
			
		||||
  const uint32_t keepalive = 60000;
 | 
			
		||||
  static uint32_t keepalive = 60000;
 | 
			
		||||
  static uint8_t max_ping_retries = 60;
 | 
			
		||||
  static uint16_t ping_retry_interval = 1000;
 | 
			
		||||
  const uint32_t now = millis();
 | 
			
		||||
  if (this->sent_ping_) {
 | 
			
		||||
    // Disconnect if not responded within 2.5*keepalive
 | 
			
		||||
@@ -126,10 +128,24 @@ void APIConnection::loop() {
 | 
			
		||||
      on_fatal_error();
 | 
			
		||||
      ESP_LOGW(TAG, "%s didn't respond to ping request in time. Disconnecting...", this->client_combined_info_.c_str());
 | 
			
		||||
    }
 | 
			
		||||
  } else if (now - this->last_traffic_ > keepalive) {
 | 
			
		||||
  } else if (now - this->last_traffic_ > keepalive && now > this->next_ping_retry_) {
 | 
			
		||||
    ESP_LOGVV(TAG, "Sending keepalive PING...");
 | 
			
		||||
    this->sent_ping_ = true;
 | 
			
		||||
    this->send_ping_request(PingRequest());
 | 
			
		||||
    this->sent_ping_ = this->send_ping_request(PingRequest());
 | 
			
		||||
    if (!this->sent_ping_) {
 | 
			
		||||
      this->next_ping_retry_ = now + ping_retry_interval;
 | 
			
		||||
      this->ping_retries_++;
 | 
			
		||||
      if (this->ping_retries_ >= max_ping_retries) {
 | 
			
		||||
        on_fatal_error();
 | 
			
		||||
        ESP_LOGE(TAG, "%s: Sending keepalive failed %d time(s). Disconnecting...", this->client_combined_info_.c_str(),
 | 
			
		||||
                 this->ping_retries_);
 | 
			
		||||
      } else if (this->ping_retries_ >= 10) {
 | 
			
		||||
        ESP_LOGW(TAG, "%s: Sending keepalive failed %d time(s), will retry in %d ms",
 | 
			
		||||
                 this->client_combined_info_.c_str(), this->ping_retries_, ping_retry_interval);
 | 
			
		||||
      } else {
 | 
			
		||||
        ESP_LOGD(TAG, "%s: Sending keepalive failed %d time(s), will retry in %d ms",
 | 
			
		||||
                 this->client_combined_info_.c_str(), this->ping_retries_, ping_retry_interval);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32_CAMERA
 | 
			
		||||
@@ -293,6 +309,8 @@ bool APIConnection::send_fan_state(fan::Fan *fan) {
 | 
			
		||||
  }
 | 
			
		||||
  if (traits.supports_direction())
 | 
			
		||||
    resp.direction = static_cast<enums::FanDirection>(fan->direction);
 | 
			
		||||
  if (traits.supports_preset_modes())
 | 
			
		||||
    resp.preset_mode = fan->preset_mode;
 | 
			
		||||
  return this->send_fan_state_response(resp);
 | 
			
		||||
}
 | 
			
		||||
bool APIConnection::send_fan_info(fan::Fan *fan) {
 | 
			
		||||
@@ -307,6 +325,8 @@ bool APIConnection::send_fan_info(fan::Fan *fan) {
 | 
			
		||||
  msg.supports_speed = traits.supports_speed();
 | 
			
		||||
  msg.supports_direction = traits.supports_direction();
 | 
			
		||||
  msg.supported_speed_count = traits.supported_speed_count();
 | 
			
		||||
  for (auto const &preset : traits.supported_preset_modes())
 | 
			
		||||
    msg.supported_preset_modes.push_back(preset);
 | 
			
		||||
  msg.disabled_by_default = fan->is_disabled_by_default();
 | 
			
		||||
  msg.icon = fan->get_icon();
 | 
			
		||||
  msg.entity_category = static_cast<enums::EntityCategory>(fan->get_entity_category());
 | 
			
		||||
@@ -328,6 +348,8 @@ void APIConnection::fan_command(const FanCommandRequest &msg) {
 | 
			
		||||
  }
 | 
			
		||||
  if (msg.has_direction)
 | 
			
		||||
    call.set_direction(static_cast<fan::FanDirection>(msg.direction));
 | 
			
		||||
  if (msg.has_preset_mode)
 | 
			
		||||
    call.set_preset_mode(msg.preset_mode);
 | 
			
		||||
  call.perform();
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -554,6 +576,10 @@ bool APIConnection::send_climate_state(climate::Climate *climate) {
 | 
			
		||||
    resp.custom_preset = climate->custom_preset.value();
 | 
			
		||||
  if (traits.get_supports_swing_modes())
 | 
			
		||||
    resp.swing_mode = static_cast<enums::ClimateSwingMode>(climate->swing_mode);
 | 
			
		||||
  if (traits.get_supports_current_humidity())
 | 
			
		||||
    resp.current_humidity = climate->current_humidity;
 | 
			
		||||
  if (traits.get_supports_target_humidity())
 | 
			
		||||
    resp.target_humidity = climate->target_humidity;
 | 
			
		||||
  return this->send_climate_state_response(resp);
 | 
			
		||||
}
 | 
			
		||||
bool APIConnection::send_climate_info(climate::Climate *climate) {
 | 
			
		||||
@@ -570,7 +596,9 @@ bool APIConnection::send_climate_info(climate::Climate *climate) {
 | 
			
		||||
  msg.entity_category = static_cast<enums::EntityCategory>(climate->get_entity_category());
 | 
			
		||||
 | 
			
		||||
  msg.supports_current_temperature = traits.get_supports_current_temperature();
 | 
			
		||||
  msg.supports_current_humidity = traits.get_supports_current_humidity();
 | 
			
		||||
  msg.supports_two_point_target_temperature = traits.get_supports_two_point_target_temperature();
 | 
			
		||||
  msg.supports_target_humidity = traits.get_supports_target_humidity();
 | 
			
		||||
 | 
			
		||||
  for (auto mode : traits.get_supported_modes())
 | 
			
		||||
    msg.supported_modes.push_back(static_cast<enums::ClimateMode>(mode));
 | 
			
		||||
@@ -579,6 +607,8 @@ bool APIConnection::send_climate_info(climate::Climate *climate) {
 | 
			
		||||
  msg.visual_max_temperature = traits.get_visual_max_temperature();
 | 
			
		||||
  msg.visual_target_temperature_step = traits.get_visual_target_temperature_step();
 | 
			
		||||
  msg.visual_current_temperature_step = traits.get_visual_current_temperature_step();
 | 
			
		||||
  msg.visual_min_humidity = traits.get_visual_min_humidity();
 | 
			
		||||
  msg.visual_max_humidity = traits.get_visual_max_humidity();
 | 
			
		||||
 | 
			
		||||
  msg.legacy_supports_away = traits.supports_preset(climate::CLIMATE_PRESET_AWAY);
 | 
			
		||||
  msg.supports_action = traits.get_supports_action();
 | 
			
		||||
@@ -609,6 +639,8 @@ void APIConnection::climate_command(const ClimateCommandRequest &msg) {
 | 
			
		||||
    call.set_target_temperature_low(msg.target_temperature_low);
 | 
			
		||||
  if (msg.has_target_temperature_high)
 | 
			
		||||
    call.set_target_temperature_high(msg.target_temperature_high);
 | 
			
		||||
  if (msg.has_target_humidity)
 | 
			
		||||
    call.set_target_humidity(msg.target_humidity);
 | 
			
		||||
  if (msg.has_fan_mode)
 | 
			
		||||
    call.set_fan_mode(static_cast<climate::ClimateFanMode>(msg.fan_mode));
 | 
			
		||||
  if (msg.has_custom_fan_mode)
 | 
			
		||||
 
 | 
			
		||||
@@ -140,6 +140,7 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
  void on_disconnect_response(const DisconnectResponse &value) override;
 | 
			
		||||
  void on_ping_response(const PingResponse &value) override {
 | 
			
		||||
    // we initiated ping
 | 
			
		||||
    this->ping_retries_ = 0;
 | 
			
		||||
    this->sent_ping_ = false;
 | 
			
		||||
  }
 | 
			
		||||
  void on_home_assistant_state_response(const HomeAssistantStateResponse &msg) override;
 | 
			
		||||
@@ -217,6 +218,8 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
  bool state_subscription_{false};
 | 
			
		||||
  int log_subscription_{ESPHOME_LOG_LEVEL_NONE};
 | 
			
		||||
  uint32_t last_traffic_;
 | 
			
		||||
  uint32_t next_ping_retry_{0};
 | 
			
		||||
  uint8_t ping_retries_{0};
 | 
			
		||||
  bool sent_ping_{false};
 | 
			
		||||
  bool service_call_subscription_{false};
 | 
			
		||||
  bool next_close_ = false;
 | 
			
		||||
 
 | 
			
		||||
@@ -1375,6 +1375,10 @@ bool ListEntitiesFanResponse::decode_length(uint32_t field_id, ProtoLengthDelimi
 | 
			
		||||
      this->icon = value.as_string();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 12: {
 | 
			
		||||
      this->supported_preset_modes.push_back(value.as_string());
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    default:
 | 
			
		||||
      return false;
 | 
			
		||||
  }
 | 
			
		||||
@@ -1401,6 +1405,9 @@ void ListEntitiesFanResponse::encode(ProtoWriteBuffer buffer) const {
 | 
			
		||||
  buffer.encode_bool(9, this->disabled_by_default);
 | 
			
		||||
  buffer.encode_string(10, this->icon);
 | 
			
		||||
  buffer.encode_enum<enums::EntityCategory>(11, this->entity_category);
 | 
			
		||||
  for (auto &it : this->supported_preset_modes) {
 | 
			
		||||
    buffer.encode_string(12, it, true);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
void ListEntitiesFanResponse::dump_to(std::string &out) const {
 | 
			
		||||
@@ -1451,6 +1458,12 @@ void ListEntitiesFanResponse::dump_to(std::string &out) const {
 | 
			
		||||
  out.append("  entity_category: ");
 | 
			
		||||
  out.append(proto_enum_to_string<enums::EntityCategory>(this->entity_category));
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  for (const auto &it : this->supported_preset_modes) {
 | 
			
		||||
    out.append("  supported_preset_modes: ");
 | 
			
		||||
    out.append("'").append(it).append("'");
 | 
			
		||||
    out.append("\n");
 | 
			
		||||
  }
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -1480,6 +1493,16 @@ bool FanStateResponse::decode_varint(uint32_t field_id, ProtoVarInt value) {
 | 
			
		||||
      return false;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
bool FanStateResponse::decode_length(uint32_t field_id, ProtoLengthDelimited value) {
 | 
			
		||||
  switch (field_id) {
 | 
			
		||||
    case 7: {
 | 
			
		||||
      this->preset_mode = value.as_string();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    default:
 | 
			
		||||
      return false;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
bool FanStateResponse::decode_32bit(uint32_t field_id, Proto32Bit value) {
 | 
			
		||||
  switch (field_id) {
 | 
			
		||||
    case 1: {
 | 
			
		||||
@@ -1497,6 +1520,7 @@ void FanStateResponse::encode(ProtoWriteBuffer buffer) const {
 | 
			
		||||
  buffer.encode_enum<enums::FanSpeed>(4, this->speed);
 | 
			
		||||
  buffer.encode_enum<enums::FanDirection>(5, this->direction);
 | 
			
		||||
  buffer.encode_int32(6, this->speed_level);
 | 
			
		||||
  buffer.encode_string(7, this->preset_mode);
 | 
			
		||||
}
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
void FanStateResponse::dump_to(std::string &out) const {
 | 
			
		||||
@@ -1527,6 +1551,10 @@ void FanStateResponse::dump_to(std::string &out) const {
 | 
			
		||||
  sprintf(buffer, "%" PRId32, this->speed_level);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  preset_mode: ");
 | 
			
		||||
  out.append("'").append(this->preset_mode).append("'");
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -1572,6 +1600,20 @@ bool FanCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) {
 | 
			
		||||
      this->speed_level = value.as_int32();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 12: {
 | 
			
		||||
      this->has_preset_mode = value.as_bool();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    default:
 | 
			
		||||
      return false;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
bool FanCommandRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value) {
 | 
			
		||||
  switch (field_id) {
 | 
			
		||||
    case 13: {
 | 
			
		||||
      this->preset_mode = value.as_string();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    default:
 | 
			
		||||
      return false;
 | 
			
		||||
  }
 | 
			
		||||
@@ -1598,6 +1640,8 @@ void FanCommandRequest::encode(ProtoWriteBuffer buffer) const {
 | 
			
		||||
  buffer.encode_enum<enums::FanDirection>(9, this->direction);
 | 
			
		||||
  buffer.encode_bool(10, this->has_speed_level);
 | 
			
		||||
  buffer.encode_int32(11, this->speed_level);
 | 
			
		||||
  buffer.encode_bool(12, this->has_preset_mode);
 | 
			
		||||
  buffer.encode_string(13, this->preset_mode);
 | 
			
		||||
}
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
void FanCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
@@ -1648,6 +1692,14 @@ void FanCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  sprintf(buffer, "%" PRId32, this->speed_level);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  has_preset_mode: ");
 | 
			
		||||
  out.append(YESNO(this->has_preset_mode));
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  preset_mode: ");
 | 
			
		||||
  out.append("'").append(this->preset_mode).append("'");
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -3559,6 +3611,14 @@ bool ListEntitiesClimateResponse::decode_varint(uint32_t field_id, ProtoVarInt v
 | 
			
		||||
      this->entity_category = value.as_enum<enums::EntityCategory>();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 22: {
 | 
			
		||||
      this->supports_current_humidity = value.as_bool();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 23: {
 | 
			
		||||
      this->supports_target_humidity = value.as_bool();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    default:
 | 
			
		||||
      return false;
 | 
			
		||||
  }
 | 
			
		||||
@@ -3615,6 +3675,14 @@ bool ListEntitiesClimateResponse::decode_32bit(uint32_t field_id, Proto32Bit val
 | 
			
		||||
      this->visual_current_temperature_step = value.as_float();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 24: {
 | 
			
		||||
      this->visual_min_humidity = value.as_float();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 25: {
 | 
			
		||||
      this->visual_max_humidity = value.as_float();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    default:
 | 
			
		||||
      return false;
 | 
			
		||||
  }
 | 
			
		||||
@@ -3653,6 +3721,10 @@ void ListEntitiesClimateResponse::encode(ProtoWriteBuffer buffer) const {
 | 
			
		||||
  buffer.encode_string(19, this->icon);
 | 
			
		||||
  buffer.encode_enum<enums::EntityCategory>(20, this->entity_category);
 | 
			
		||||
  buffer.encode_float(21, this->visual_current_temperature_step);
 | 
			
		||||
  buffer.encode_bool(22, this->supports_current_humidity);
 | 
			
		||||
  buffer.encode_bool(23, this->supports_target_humidity);
 | 
			
		||||
  buffer.encode_float(24, this->visual_min_humidity);
 | 
			
		||||
  buffer.encode_float(25, this->visual_max_humidity);
 | 
			
		||||
}
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
void ListEntitiesClimateResponse::dump_to(std::string &out) const {
 | 
			
		||||
@@ -3758,6 +3830,24 @@ void ListEntitiesClimateResponse::dump_to(std::string &out) const {
 | 
			
		||||
  sprintf(buffer, "%g", this->visual_current_temperature_step);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  supports_current_humidity: ");
 | 
			
		||||
  out.append(YESNO(this->supports_current_humidity));
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  supports_target_humidity: ");
 | 
			
		||||
  out.append(YESNO(this->supports_target_humidity));
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  visual_min_humidity: ");
 | 
			
		||||
  sprintf(buffer, "%g", this->visual_min_humidity);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  visual_max_humidity: ");
 | 
			
		||||
  sprintf(buffer, "%g", this->visual_max_humidity);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -3827,6 +3917,14 @@ bool ClimateStateResponse::decode_32bit(uint32_t field_id, Proto32Bit value) {
 | 
			
		||||
      this->target_temperature_high = value.as_float();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 14: {
 | 
			
		||||
      this->current_humidity = value.as_float();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 15: {
 | 
			
		||||
      this->target_humidity = value.as_float();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    default:
 | 
			
		||||
      return false;
 | 
			
		||||
  }
 | 
			
		||||
@@ -3845,6 +3943,8 @@ void ClimateStateResponse::encode(ProtoWriteBuffer buffer) const {
 | 
			
		||||
  buffer.encode_string(11, this->custom_fan_mode);
 | 
			
		||||
  buffer.encode_enum<enums::ClimatePreset>(12, this->preset);
 | 
			
		||||
  buffer.encode_string(13, this->custom_preset);
 | 
			
		||||
  buffer.encode_float(14, this->current_humidity);
 | 
			
		||||
  buffer.encode_float(15, this->target_humidity);
 | 
			
		||||
}
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
void ClimateStateResponse::dump_to(std::string &out) const {
 | 
			
		||||
@@ -3906,6 +4006,16 @@ void ClimateStateResponse::dump_to(std::string &out) const {
 | 
			
		||||
  out.append("  custom_preset: ");
 | 
			
		||||
  out.append("'").append(this->custom_preset).append("'");
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  current_humidity: ");
 | 
			
		||||
  sprintf(buffer, "%g", this->current_humidity);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  target_humidity: ");
 | 
			
		||||
  sprintf(buffer, "%g", this->target_humidity);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -3971,6 +4081,10 @@ bool ClimateCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value)
 | 
			
		||||
      this->has_custom_preset = value.as_bool();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 22: {
 | 
			
		||||
      this->has_target_humidity = value.as_bool();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    default:
 | 
			
		||||
      return false;
 | 
			
		||||
  }
 | 
			
		||||
@@ -4007,6 +4121,10 @@ bool ClimateCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) {
 | 
			
		||||
      this->target_temperature_high = value.as_float();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 23: {
 | 
			
		||||
      this->target_humidity = value.as_float();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    default:
 | 
			
		||||
      return false;
 | 
			
		||||
  }
 | 
			
		||||
@@ -4033,6 +4151,8 @@ void ClimateCommandRequest::encode(ProtoWriteBuffer buffer) const {
 | 
			
		||||
  buffer.encode_enum<enums::ClimatePreset>(19, this->preset);
 | 
			
		||||
  buffer.encode_bool(20, this->has_custom_preset);
 | 
			
		||||
  buffer.encode_string(21, this->custom_preset);
 | 
			
		||||
  buffer.encode_bool(22, this->has_target_humidity);
 | 
			
		||||
  buffer.encode_float(23, this->target_humidity);
 | 
			
		||||
}
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
void ClimateCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
@@ -4125,6 +4245,15 @@ void ClimateCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  out.append("  custom_preset: ");
 | 
			
		||||
  out.append("'").append(this->custom_preset).append("'");
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  has_target_humidity: ");
 | 
			
		||||
  out.append(YESNO(this->has_target_humidity));
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  target_humidity: ");
 | 
			
		||||
  sprintf(buffer, "%g", this->target_humidity);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -472,6 +472,7 @@ class ListEntitiesFanResponse : public ProtoMessage {
 | 
			
		||||
  bool disabled_by_default{false};
 | 
			
		||||
  std::string icon{};
 | 
			
		||||
  enums::EntityCategory entity_category{};
 | 
			
		||||
  std::vector<std::string> supported_preset_modes{};
 | 
			
		||||
  void encode(ProtoWriteBuffer buffer) const override;
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
  void dump_to(std::string &out) const override;
 | 
			
		||||
@@ -490,6 +491,7 @@ class FanStateResponse : public ProtoMessage {
 | 
			
		||||
  enums::FanSpeed speed{};
 | 
			
		||||
  enums::FanDirection direction{};
 | 
			
		||||
  int32_t speed_level{0};
 | 
			
		||||
  std::string preset_mode{};
 | 
			
		||||
  void encode(ProtoWriteBuffer buffer) const override;
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
  void dump_to(std::string &out) const override;
 | 
			
		||||
@@ -497,6 +499,7 @@ class FanStateResponse : public ProtoMessage {
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
 | 
			
		||||
  bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
 | 
			
		||||
  bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
 | 
			
		||||
};
 | 
			
		||||
class FanCommandRequest : public ProtoMessage {
 | 
			
		||||
@@ -512,6 +515,8 @@ class FanCommandRequest : public ProtoMessage {
 | 
			
		||||
  enums::FanDirection direction{};
 | 
			
		||||
  bool has_speed_level{false};
 | 
			
		||||
  int32_t speed_level{0};
 | 
			
		||||
  bool has_preset_mode{false};
 | 
			
		||||
  std::string preset_mode{};
 | 
			
		||||
  void encode(ProtoWriteBuffer buffer) const override;
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
  void dump_to(std::string &out) const override;
 | 
			
		||||
@@ -519,6 +524,7 @@ class FanCommandRequest : public ProtoMessage {
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
 | 
			
		||||
  bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
 | 
			
		||||
  bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
 | 
			
		||||
};
 | 
			
		||||
class ListEntitiesLightResponse : public ProtoMessage {
 | 
			
		||||
@@ -979,6 +985,10 @@ class ListEntitiesClimateResponse : public ProtoMessage {
 | 
			
		||||
  std::string icon{};
 | 
			
		||||
  enums::EntityCategory entity_category{};
 | 
			
		||||
  float visual_current_temperature_step{0.0f};
 | 
			
		||||
  bool supports_current_humidity{false};
 | 
			
		||||
  bool supports_target_humidity{false};
 | 
			
		||||
  float visual_min_humidity{0.0f};
 | 
			
		||||
  float visual_max_humidity{0.0f};
 | 
			
		||||
  void encode(ProtoWriteBuffer buffer) const override;
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
  void dump_to(std::string &out) const override;
 | 
			
		||||
@@ -1004,6 +1014,8 @@ class ClimateStateResponse : public ProtoMessage {
 | 
			
		||||
  std::string custom_fan_mode{};
 | 
			
		||||
  enums::ClimatePreset preset{};
 | 
			
		||||
  std::string custom_preset{};
 | 
			
		||||
  float current_humidity{0.0f};
 | 
			
		||||
  float target_humidity{0.0f};
 | 
			
		||||
  void encode(ProtoWriteBuffer buffer) const override;
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
  void dump_to(std::string &out) const override;
 | 
			
		||||
@@ -1037,6 +1049,8 @@ class ClimateCommandRequest : public ProtoMessage {
 | 
			
		||||
  enums::ClimatePreset preset{};
 | 
			
		||||
  bool has_custom_preset{false};
 | 
			
		||||
  std::string custom_preset{};
 | 
			
		||||
  bool has_target_humidity{false};
 | 
			
		||||
  float target_humidity{0.0f};
 | 
			
		||||
  void encode(ProtoWriteBuffer buffer) const override;
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
  void dump_to(std::string &out) const override;
 | 
			
		||||
 
 | 
			
		||||
@@ -319,7 +319,7 @@ void APIServer::set_reboot_timeout(uint32_t reboot_timeout) { this->reboot_timeo
 | 
			
		||||
#ifdef USE_HOMEASSISTANT_TIME
 | 
			
		||||
void APIServer::request_time() {
 | 
			
		||||
  for (auto &client : this->clients_) {
 | 
			
		||||
    if (!client->remove_ && client->connection_state_ == APIConnection::ConnectionState::CONNECTED)
 | 
			
		||||
    if (!client->remove_ && client->is_authenticated())
 | 
			
		||||
      client->send_time_request();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,6 @@ from typing import Any
 | 
			
		||||
from aioesphomeapi import APIClient
 | 
			
		||||
from aioesphomeapi.api_pb2 import SubscribeLogsResponse
 | 
			
		||||
from aioesphomeapi.log_runner import async_run
 | 
			
		||||
from zeroconf.asyncio import AsyncZeroconf
 | 
			
		||||
 | 
			
		||||
from esphome.const import CONF_KEY, CONF_PASSWORD, CONF_PORT, __version__
 | 
			
		||||
from esphome.core import CORE
 | 
			
		||||
@@ -18,24 +17,22 @@ from . import CONF_ENCRYPTION
 | 
			
		||||
_LOGGER = logging.getLogger(__name__)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def async_run_logs(config, address):
 | 
			
		||||
async def async_run_logs(config: dict[str, Any], address: str) -> None:
 | 
			
		||||
    """Run the logs command in the event loop."""
 | 
			
		||||
    conf = config["api"]
 | 
			
		||||
    name = config["esphome"]["name"]
 | 
			
		||||
    port: int = int(conf[CONF_PORT])
 | 
			
		||||
    password: str = conf[CONF_PASSWORD]
 | 
			
		||||
    noise_psk: str | None = None
 | 
			
		||||
    if CONF_ENCRYPTION in conf:
 | 
			
		||||
        noise_psk = conf[CONF_ENCRYPTION][CONF_KEY]
 | 
			
		||||
    _LOGGER.info("Starting log output from %s using esphome API", address)
 | 
			
		||||
    aiozc = AsyncZeroconf()
 | 
			
		||||
 | 
			
		||||
    cli = APIClient(
 | 
			
		||||
        address,
 | 
			
		||||
        port,
 | 
			
		||||
        password,
 | 
			
		||||
        client_info=f"ESPHome Logs {__version__}",
 | 
			
		||||
        noise_psk=noise_psk,
 | 
			
		||||
        zeroconf_instance=aiozc.zeroconf,
 | 
			
		||||
    )
 | 
			
		||||
    dashboard = CORE.dashboard
 | 
			
		||||
 | 
			
		||||
@@ -48,12 +45,10 @@ async def async_run_logs(config, address):
 | 
			
		||||
            text = text.replace("\033", "\\033")
 | 
			
		||||
        print(f"[{time_.hour:02}:{time_.minute:02}:{time_.second:02}]{text}")
 | 
			
		||||
 | 
			
		||||
    stop = await async_run(cli, on_log, aio_zeroconf_instance=aiozc)
 | 
			
		||||
    stop = await async_run(cli, on_log, name=name)
 | 
			
		||||
    try:
 | 
			
		||||
        while True:
 | 
			
		||||
            await asyncio.sleep(60)
 | 
			
		||||
        await asyncio.Event().wait()
 | 
			
		||||
    finally:
 | 
			
		||||
        await aiozc.async_close()
 | 
			
		||||
        await stop()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -160,8 +160,7 @@ class ProtoWriteBuffer {
 | 
			
		||||
    this->encode_field_raw(field_id, 2);
 | 
			
		||||
    this->encode_varint_raw(len);
 | 
			
		||||
    auto *data = reinterpret_cast<const uint8_t *>(string);
 | 
			
		||||
    for (size_t i = 0; i < len; i++)
 | 
			
		||||
      this->write(data[i]);
 | 
			
		||||
    this->buffer_->insert(this->buffer_->end(), data, data + len);
 | 
			
		||||
  }
 | 
			
		||||
  void encode_string(uint32_t field_id, const std::string &value, bool force = false) {
 | 
			
		||||
    this->encode_string(field_id, value.data(), value.size());
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										228
									
								
								esphome/components/as5600/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										228
									
								
								esphome/components/as5600/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,228 @@
 | 
			
		||||
from esphome import pins
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.components import i2c
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    CONF_DIR_PIN,
 | 
			
		||||
    CONF_DIRECTION,
 | 
			
		||||
    CONF_HYSTERESIS,
 | 
			
		||||
    CONF_RANGE,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CODEOWNERS = ["@ammmze"]
 | 
			
		||||
DEPENDENCIES = ["i2c"]
 | 
			
		||||
MULTI_CONF = True
 | 
			
		||||
 | 
			
		||||
as5600_ns = cg.esphome_ns.namespace("as5600")
 | 
			
		||||
AS5600Component = as5600_ns.class_("AS5600Component", cg.Component, i2c.I2CDevice)
 | 
			
		||||
 | 
			
		||||
DIRECTION = {
 | 
			
		||||
    "CLOCKWISE": 0,
 | 
			
		||||
    "COUNTERCLOCKWISE": 1,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
POWER_MODE = {
 | 
			
		||||
    "NOMINAL": 0,
 | 
			
		||||
    "LOW1": 1,
 | 
			
		||||
    "LOW2": 2,
 | 
			
		||||
    "LOW3": 3,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
HYSTERESIS = {
 | 
			
		||||
    "NONE": 0,
 | 
			
		||||
    "LSB1": 1,
 | 
			
		||||
    "LSB2": 2,
 | 
			
		||||
    "LSB3": 3,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
SLOW_FILTER = {
 | 
			
		||||
    "16X": 0,
 | 
			
		||||
    "8X": 1,
 | 
			
		||||
    "4X": 2,
 | 
			
		||||
    "2X": 3,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
FAST_FILTER = {
 | 
			
		||||
    "NONE": 0,
 | 
			
		||||
    "LSB6": 1,
 | 
			
		||||
    "LSB7": 2,
 | 
			
		||||
    "LSB9": 3,
 | 
			
		||||
    "LSB18": 4,
 | 
			
		||||
    "LSB21": 5,
 | 
			
		||||
    "LSB24": 6,
 | 
			
		||||
    "LSB10": 7,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
CONF_ANGLE = "angle"
 | 
			
		||||
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"
 | 
			
		||||
CONF_END_POSITION = "end_position"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
RESOLUTION = 4096
 | 
			
		||||
MAX_POSITION = RESOLUTION - 1
 | 
			
		||||
ANGLE_TO_POSITION = RESOLUTION / 360
 | 
			
		||||
POSITION_TO_ANGLE = 360 / RESOLUTION
 | 
			
		||||
# validate min range of 18deg (per datasheet) ... though i seem to get valid values down to a range of 192steps (16.875deg)
 | 
			
		||||
MIN_RANGE = round(18 * ANGLE_TO_POSITION)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def angle(min=-360, max=360):
 | 
			
		||||
    return cv.All(
 | 
			
		||||
        cv.float_with_unit("angle", "(°|deg)"), cv.float_range(min=min, max=max)
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def angle_to_position(value, min=-360, max=360):
 | 
			
		||||
    try:
 | 
			
		||||
        value = angle(min=min, max=max)(value)
 | 
			
		||||
        return (RESOLUTION + round(value * ANGLE_TO_POSITION)) % RESOLUTION
 | 
			
		||||
    except cv.Invalid as e:
 | 
			
		||||
        raise cv.Invalid(f"When using angle, {e.error_message}")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def percent_to_position(value):
 | 
			
		||||
    value = cv.possibly_negative_percentage(value)
 | 
			
		||||
    return (RESOLUTION + round(value * RESOLUTION)) % RESOLUTION
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def position(min=-MAX_POSITION, max=MAX_POSITION):
 | 
			
		||||
    """Validate that the config option is a position.
 | 
			
		||||
    Accepts integers, degrees, or percentage (of 360 degrees).
 | 
			
		||||
    """
 | 
			
		||||
 | 
			
		||||
    def validator(value):
 | 
			
		||||
        if isinstance(value, str) and value.endswith("%"):
 | 
			
		||||
            value = percent_to_position(value)
 | 
			
		||||
 | 
			
		||||
        if isinstance(value, str) and (value.endswith("°") or value.endswith("deg")):
 | 
			
		||||
            return angle_to_position(
 | 
			
		||||
                value,
 | 
			
		||||
                min=round(min * POSITION_TO_ANGLE),
 | 
			
		||||
                max=round(max * POSITION_TO_ANGLE),
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
        return cv.int_range(min=min, max=max)(value)
 | 
			
		||||
 | 
			
		||||
    return validator
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def position_range():
 | 
			
		||||
    """Validate that value given is a valid range for the device.
 | 
			
		||||
    A valid range is one of the following:
 | 
			
		||||
    - a value of 0 (meaning full range)
 | 
			
		||||
    - 18 thru 360 degrees
 | 
			
		||||
    - negative 360 thru negative 18 degrees (notes: these are normalized to their positive values, accepting negatives is for convenience)
 | 
			
		||||
    """
 | 
			
		||||
    zero_validator = position(min=0, max=0)
 | 
			
		||||
    negative_validator = cv.Any(
 | 
			
		||||
        position(min=-MAX_POSITION, max=-MIN_RANGE),
 | 
			
		||||
        zero_validator,
 | 
			
		||||
    )
 | 
			
		||||
    positive_validator = cv.Any(
 | 
			
		||||
        position(min=MIN_RANGE, max=MAX_POSITION),
 | 
			
		||||
        zero_validator,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    def validator(value):
 | 
			
		||||
        is_negative_str = isinstance(value, str) and value.startswith("-")
 | 
			
		||||
        is_negative_num = isinstance(value, (float, int)) and value < 0
 | 
			
		||||
        if is_negative_str or is_negative_num:
 | 
			
		||||
            return negative_validator(value)
 | 
			
		||||
        return positive_validator(value)
 | 
			
		||||
 | 
			
		||||
    return validator
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def has_valid_range_config():
 | 
			
		||||
    """Validate that that the config start + end position results in a valid
 | 
			
		||||
    positional range, which must be >= 18degrees
 | 
			
		||||
    """
 | 
			
		||||
    range_validator = position_range()
 | 
			
		||||
 | 
			
		||||
    def validator(config):
 | 
			
		||||
        # if we don't have an end position, then there is nothing to do
 | 
			
		||||
        if CONF_END_POSITION not in config:
 | 
			
		||||
            return config
 | 
			
		||||
 | 
			
		||||
        # determine the range by taking the difference from the end and start
 | 
			
		||||
        range = config[CONF_END_POSITION] - config[CONF_START_POSITION]
 | 
			
		||||
 | 
			
		||||
        # but need to account for start position being greater than end position
 | 
			
		||||
        # where the range rolls back around the 0 position
 | 
			
		||||
        if config[CONF_END_POSITION] < config[CONF_START_POSITION]:
 | 
			
		||||
            range = RESOLUTION + config[CONF_END_POSITION] - config[CONF_START_POSITION]
 | 
			
		||||
 | 
			
		||||
        try:
 | 
			
		||||
            range_validator(range)
 | 
			
		||||
            return config
 | 
			
		||||
        except cv.Invalid as e:
 | 
			
		||||
            raise cv.Invalid(
 | 
			
		||||
                f"The range between start and end position is invalid. It was was {range} but {e.error_message}"
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
    return validator
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = cv.All(
 | 
			
		||||
    cv.Schema(
 | 
			
		||||
        {
 | 
			
		||||
            cv.GenerateID(): cv.declare_id(AS5600Component),
 | 
			
		||||
            cv.Optional(CONF_DIR_PIN): pins.gpio_input_pin_schema,
 | 
			
		||||
            cv.Optional(CONF_DIRECTION, default="CLOCKWISE"): cv.enum(
 | 
			
		||||
                DIRECTION, upper=True
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_WATCHDOG, default=False): cv.boolean,
 | 
			
		||||
            cv.Optional(CONF_POWER_MODE, default="NOMINAL"): cv.enum(
 | 
			
		||||
                POWER_MODE, upper=True, space=""
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_HYSTERESIS, default="NONE"): cv.enum(
 | 
			
		||||
                HYSTERESIS, upper=True, space=""
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_SLOW_FILTER, default="16X"): cv.enum(
 | 
			
		||||
                SLOW_FILTER, upper=True, space=""
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_FAST_FILTER, default="NONE"): cv.enum(
 | 
			
		||||
                FAST_FILTER, upper=True, space=""
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_START_POSITION, default=0): position(),
 | 
			
		||||
            cv.Optional(CONF_END_POSITION): position(),
 | 
			
		||||
            cv.Optional(CONF_RANGE): position_range(),
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
    .extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
    .extend(i2c.i2c_device_schema(0x36)),
 | 
			
		||||
    # ensure end_position and range are mutually exclusive
 | 
			
		||||
    cv.has_at_most_one_key(CONF_END_POSITION, CONF_RANGE),
 | 
			
		||||
    has_valid_range_config(),
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
    await i2c.register_i2c_device(var, config)
 | 
			
		||||
 | 
			
		||||
    cg.add(var.set_direction(config[CONF_DIRECTION]))
 | 
			
		||||
    cg.add(var.set_watchdog(config[CONF_WATCHDOG]))
 | 
			
		||||
    cg.add(var.set_power_mode(config[CONF_POWER_MODE]))
 | 
			
		||||
    cg.add(var.set_hysteresis(config[CONF_HYSTERESIS]))
 | 
			
		||||
    cg.add(var.set_slow_filter(config[CONF_SLOW_FILTER]))
 | 
			
		||||
    cg.add(var.set_fast_filter(config[CONF_FAST_FILTER]))
 | 
			
		||||
    cg.add(var.set_start_position(config[CONF_START_POSITION]))
 | 
			
		||||
 | 
			
		||||
    if dir_pin_config := config.get(CONF_DIR_PIN):
 | 
			
		||||
        pin = await cg.gpio_pin_expression(dir_pin_config)
 | 
			
		||||
        cg.add(var.set_dir_pin(pin))
 | 
			
		||||
 | 
			
		||||
    if (end_position_config := config.get(CONF_END_POSITION, None)) is not None:
 | 
			
		||||
        cg.add(var.set_end_position(end_position_config))
 | 
			
		||||
 | 
			
		||||
    if (range_config := config.get(CONF_RANGE, None)) is not None:
 | 
			
		||||
        cg.add(var.set_range(range_config))
 | 
			
		||||
							
								
								
									
										138
									
								
								esphome/components/as5600/as5600.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										138
									
								
								esphome/components/as5600/as5600.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,138 @@
 | 
			
		||||
#include "as5600.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace as5600 {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "as5600";
 | 
			
		||||
 | 
			
		||||
// Configuration registers
 | 
			
		||||
static const uint8_t REGISTER_ZMCO = 0x00;  // 8 bytes  / R
 | 
			
		||||
static const uint8_t REGISTER_ZPOS = 0x01;  // 16 bytes / RW
 | 
			
		||||
static const uint8_t REGISTER_MPOS = 0x03;  // 16 bytes / RW
 | 
			
		||||
static const uint8_t REGISTER_MANG = 0x05;  // 16 bytes / RW
 | 
			
		||||
static const uint8_t REGISTER_CONF = 0x07;  // 16 bytes / RW
 | 
			
		||||
 | 
			
		||||
// Output registers
 | 
			
		||||
static const uint8_t REGISTER_ANGLE_RAW = 0x0C;  // 16 bytes / R
 | 
			
		||||
static const uint8_t REGISTER_ANGLE = 0x0E;      // 16 bytes / R
 | 
			
		||||
 | 
			
		||||
// Status registers
 | 
			
		||||
static const uint8_t REGISTER_STATUS = 0x0B;     // 8 bytes  / R
 | 
			
		||||
static const uint8_t REGISTER_AGC = 0x1A;        // 8 bytes  / R
 | 
			
		||||
static const uint8_t REGISTER_MAGNITUDE = 0x1B;  // 16 bytes / R
 | 
			
		||||
 | 
			
		||||
void AS5600Component::setup() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Setting up AS5600...");
 | 
			
		||||
 | 
			
		||||
  if (!this->read_byte(REGISTER_STATUS).has_value()) {
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // configuration direction pin, if given
 | 
			
		||||
  // the dir pin on the chip should be low for clockwise
 | 
			
		||||
  // and high for counterclockwise. If the pin is left floating
 | 
			
		||||
  // the reported positions will be erratic.
 | 
			
		||||
  if (this->dir_pin_ != nullptr) {
 | 
			
		||||
    this->dir_pin_->pin_mode(gpio::FLAG_OUTPUT);
 | 
			
		||||
    this->dir_pin_->digital_write(this->direction_ == 1);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // build config register
 | 
			
		||||
  // take the value, shift it left, and add mask to it to ensure we
 | 
			
		||||
  // are only changing the bits appropriate for that setting in the
 | 
			
		||||
  // off chance we somehow have bad value in there and it makes for
 | 
			
		||||
  // a nice visual for the bit positions.
 | 
			
		||||
  uint16_t config = 0;
 | 
			
		||||
  // clang-format off
 | 
			
		||||
  config |= (this->watchdog_      << 13) & 0b0010000000000000;
 | 
			
		||||
  config |= (this->fast_filter_   << 10) & 0b0001110000000000;
 | 
			
		||||
  config |= (this->slow_filter_   <<  8) & 0b0000001100000000;
 | 
			
		||||
  config |= (this->pwm_frequency_ <<  6) & 0b0000000011000000;
 | 
			
		||||
  config |= (this->output_mode_   <<  4) & 0b0000000000110000;
 | 
			
		||||
  config |= (this->hysteresis_    <<  2) & 0b0000000000001100;
 | 
			
		||||
  config |= (this->power_mode_    <<  0) & 0b0000000000000011;
 | 
			
		||||
  // clang-format on
 | 
			
		||||
 | 
			
		||||
  // write config to config register
 | 
			
		||||
  if (!this->write_byte_16(REGISTER_CONF, config)) {
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // configure the start position
 | 
			
		||||
  this->write_byte_16(REGISTER_ZPOS, this->start_position_);
 | 
			
		||||
 | 
			
		||||
  // configure either end position or max angle
 | 
			
		||||
  if (this->end_mode_ == END_MODE_POSITION) {
 | 
			
		||||
    this->write_byte_16(REGISTER_MPOS, this->end_position_);
 | 
			
		||||
  } else {
 | 
			
		||||
    this->write_byte_16(REGISTER_MANG, this->end_position_);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // calculate the raw max from end position or start + range
 | 
			
		||||
  this->raw_max_ = this->end_mode_ == END_MODE_POSITION ? this->end_position_ & 4095
 | 
			
		||||
                                                        : (this->start_position_ + this->end_position_) & 4095;
 | 
			
		||||
 | 
			
		||||
  // calculate allowed range of motion by taking the start from the end
 | 
			
		||||
  // but only if the end is greater than the start. If the start is greater
 | 
			
		||||
  // than the end position, then that means we take the start all the way to
 | 
			
		||||
  // reset point (i.e. 0 deg raw) and then we that with the end position
 | 
			
		||||
  uint16_t range = this->raw_max_ > this->start_position_ ? this->raw_max_ - this->start_position_
 | 
			
		||||
                                                          : (4095 - this->start_position_) + this->raw_max_;
 | 
			
		||||
 | 
			
		||||
  // range scale is ratio of actual allowed range to the full range
 | 
			
		||||
  this->range_scale_ = range / 4095.0f;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void AS5600Component::dump_config() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "AS5600:");
 | 
			
		||||
  LOG_I2C_DEVICE(this);
 | 
			
		||||
 | 
			
		||||
  if (this->is_failed()) {
 | 
			
		||||
    ESP_LOGE(TAG, "Communication with AS5600 failed!");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Watchdog: %d", this->watchdog_);
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Fast Filter: %d", this->fast_filter_);
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Slow Filter: %d", this->slow_filter_);
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Hysteresis: %d", this->hysteresis_);
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Start Position: %d", this->start_position_);
 | 
			
		||||
  if (this->end_mode_ == END_MODE_POSITION) {
 | 
			
		||||
    ESP_LOGCONFIG(TAG, "  End Position: %d", this->end_position_);
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGCONFIG(TAG, "  Range: %d", this->end_position_);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool AS5600Component::in_range(uint16_t raw_position) {
 | 
			
		||||
  return this->raw_max_ > this->start_position_
 | 
			
		||||
             ? raw_position >= this->start_position_ && raw_position <= this->raw_max_
 | 
			
		||||
             : raw_position >= this->start_position_ || raw_position <= this->raw_max_;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
AS5600MagnetStatus AS5600Component::read_magnet_status() {
 | 
			
		||||
  uint8_t status = this->reg(REGISTER_STATUS).get() >> 3 & 0b000111;
 | 
			
		||||
  return static_cast<AS5600MagnetStatus>(status);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
optional<uint16_t> AS5600Component::read_position() {
 | 
			
		||||
  uint16_t pos = 0;
 | 
			
		||||
  if (!this->read_byte_16(REGISTER_ANGLE, &pos)) {
 | 
			
		||||
    return {};
 | 
			
		||||
  }
 | 
			
		||||
  return pos;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
optional<uint16_t> AS5600Component::read_raw_position() {
 | 
			
		||||
  uint16_t pos = 0;
 | 
			
		||||
  if (!this->read_byte_16(REGISTER_ANGLE_RAW, &pos)) {
 | 
			
		||||
    return {};
 | 
			
		||||
  }
 | 
			
		||||
  return pos;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace as5600
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										105
									
								
								esphome/components/as5600/as5600.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										105
									
								
								esphome/components/as5600/as5600.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,105 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/core/hal.h"
 | 
			
		||||
#include "esphome/core/preferences.h"
 | 
			
		||||
#include "esphome/components/i2c/i2c.h"
 | 
			
		||||
#include "esphome/components/sensor/sensor.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace as5600 {
 | 
			
		||||
 | 
			
		||||
static const uint16_t POSITION_COUNT = 4096;
 | 
			
		||||
static const float RAW_TO_DEGREES = 360.0 / POSITION_COUNT;
 | 
			
		||||
static const float DEGREES_TO_RAW = POSITION_COUNT / 360.0;
 | 
			
		||||
 | 
			
		||||
enum EndPositionMode : uint8_t {
 | 
			
		||||
  // In this mode, the end position is calculated by taking the start position
 | 
			
		||||
  // and adding the range/positions. For example, you could say start at 90deg,
 | 
			
		||||
  // and have a range of 180deg and effectively the sensor will report values
 | 
			
		||||
  // from the physical 90deg thru 270deg.
 | 
			
		||||
  END_MODE_RANGE,
 | 
			
		||||
  // In this mode, the end position is explicitly set, and changing the start
 | 
			
		||||
  // position will NOT change the end position.
 | 
			
		||||
  END_MODE_POSITION,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum OutRangeMode : uint8_t {
 | 
			
		||||
  // In this mode, the AS5600 chip itself actually reports these values, but
 | 
			
		||||
  // effectively it splits the out-of-range values in half, and when positioned
 | 
			
		||||
  // over the half closest to the min/start position, it will report 0 and when
 | 
			
		||||
  // positioned over the half closes to the max/end position, it will report the
 | 
			
		||||
  // max/end value.
 | 
			
		||||
  OUT_RANGE_MODE_MIN_MAX,
 | 
			
		||||
  // In this mode, when the magnet is positioned outside the configured
 | 
			
		||||
  // range, the sensor will report NAN, which translates to "Unknown"
 | 
			
		||||
  // in Home Assistant.
 | 
			
		||||
  OUT_RANGE_MODE_NAN,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum AS5600MagnetStatus : uint8_t {
 | 
			
		||||
  MAGNET_GONE = 2,    // 0b010 / magnet not detected
 | 
			
		||||
  MAGNET_OK = 4,      // 0b100 / magnet just right
 | 
			
		||||
  MAGNET_STRONG = 5,  // 0b101 / magnet too strong
 | 
			
		||||
  MAGNET_WEAK = 6,    // 0b110 / magnet too weak
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class AS5600Component : public Component, public i2c::I2CDevice {
 | 
			
		||||
 public:
 | 
			
		||||
  /// Set up the internal sensor array.
 | 
			
		||||
  void setup() override;
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
  /// HARDWARE_LATE setup priority
 | 
			
		||||
  float get_setup_priority() const override { return setup_priority::DATA; }
 | 
			
		||||
 | 
			
		||||
  // configuration setters
 | 
			
		||||
  void set_dir_pin(InternalGPIOPin *pin) { this->dir_pin_ = pin; }
 | 
			
		||||
  void set_direction(uint8_t direction) { this->direction_ = direction; }
 | 
			
		||||
  void set_fast_filter(uint8_t fast_filter) { this->fast_filter_ = fast_filter; }
 | 
			
		||||
  void set_hysteresis(uint8_t hysteresis) { this->hysteresis_ = hysteresis; }
 | 
			
		||||
  void set_power_mode(uint8_t power_mode) { this->power_mode_ = power_mode; }
 | 
			
		||||
  void set_slow_filter(uint8_t slow_filter) { this->slow_filter_ = slow_filter; }
 | 
			
		||||
  void set_watchdog(bool watchdog) { this->watchdog_ = watchdog; }
 | 
			
		||||
  bool get_watchdog() { return this->watchdog_; }
 | 
			
		||||
  void set_start_position(uint16_t start_position) { this->start_position_ = start_position % POSITION_COUNT; }
 | 
			
		||||
  void set_end_position(uint16_t end_position) {
 | 
			
		||||
    this->end_position_ = end_position % POSITION_COUNT;
 | 
			
		||||
    this->end_mode_ = END_MODE_POSITION;
 | 
			
		||||
  }
 | 
			
		||||
  void set_range(uint16_t range) {
 | 
			
		||||
    this->end_position_ = range % POSITION_COUNT;
 | 
			
		||||
    this->end_mode_ = END_MODE_RANGE;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Gets the scale value for the configured range.
 | 
			
		||||
  // For example, if configured to start at 0deg and end at 180deg, the
 | 
			
		||||
  // range is 50% of the native/raw range, so the range scale would be 0.5.
 | 
			
		||||
  // If configured to use the full 360deg, the range scale would be 1.0.
 | 
			
		||||
  float get_range_scale() { return this->range_scale_; }
 | 
			
		||||
 | 
			
		||||
  // Indicates whether the given *raw* position is within the configured range
 | 
			
		||||
  bool in_range(uint16_t raw_position);
 | 
			
		||||
 | 
			
		||||
  AS5600MagnetStatus read_magnet_status();
 | 
			
		||||
  optional<uint16_t> read_position();
 | 
			
		||||
  optional<uint16_t> read_raw_position();
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  InternalGPIOPin *dir_pin_{nullptr};
 | 
			
		||||
  uint8_t direction_;
 | 
			
		||||
  uint8_t fast_filter_;
 | 
			
		||||
  uint8_t hysteresis_;
 | 
			
		||||
  uint8_t power_mode_;
 | 
			
		||||
  uint8_t slow_filter_;
 | 
			
		||||
  uint8_t pwm_frequency_{0};
 | 
			
		||||
  uint8_t output_mode_{0};
 | 
			
		||||
  bool watchdog_;
 | 
			
		||||
  uint16_t start_position_;
 | 
			
		||||
  uint16_t end_position_{0};
 | 
			
		||||
  uint16_t raw_max_;
 | 
			
		||||
  EndPositionMode end_mode_{END_MODE_RANGE};
 | 
			
		||||
  float range_scale_{1.0};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace as5600
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										119
									
								
								esphome/components/as5600/sensor/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										119
									
								
								esphome/components/as5600/sensor/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,119 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.components import sensor
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    STATE_CLASS_MEASUREMENT,
 | 
			
		||||
    ICON_MAGNET,
 | 
			
		||||
    ICON_ROTATE_RIGHT,
 | 
			
		||||
    CONF_GAIN,
 | 
			
		||||
    ENTITY_CATEGORY_DIAGNOSTIC,
 | 
			
		||||
    CONF_MAGNITUDE,
 | 
			
		||||
    CONF_STATUS,
 | 
			
		||||
    CONF_POSITION,
 | 
			
		||||
)
 | 
			
		||||
from .. import as5600_ns, AS5600Component
 | 
			
		||||
 | 
			
		||||
CODEOWNERS = ["@ammmze"]
 | 
			
		||||
DEPENDENCIES = ["as5600"]
 | 
			
		||||
 | 
			
		||||
AS5600Sensor = as5600_ns.class_("AS5600Sensor", sensor.Sensor, cg.PollingComponent)
 | 
			
		||||
 | 
			
		||||
CONF_ANGLE = "angle"
 | 
			
		||||
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"
 | 
			
		||||
CONF_BURN_COUNT = "burn_count"
 | 
			
		||||
CONF_START_POSITION = "start_position"
 | 
			
		||||
CONF_END_POSITION = "end_position"
 | 
			
		||||
CONF_OUT_OF_RANGE_MODE = "out_of_range_mode"
 | 
			
		||||
 | 
			
		||||
OutOfRangeMode = as5600_ns.enum("OutRangeMode")
 | 
			
		||||
OUT_OF_RANGE_MODES = {
 | 
			
		||||
    "MIN_MAX": OutOfRangeMode.OUT_RANGE_MODE_MIN_MAX,
 | 
			
		||||
    "NAN": OutOfRangeMode.OUT_RANGE_MODE_NAN,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
CONF_AS5600_ID = "as5600_id"
 | 
			
		||||
CONFIG_SCHEMA = (
 | 
			
		||||
    sensor.sensor_schema(
 | 
			
		||||
        AS5600Sensor,
 | 
			
		||||
        accuracy_decimals=0,
 | 
			
		||||
        icon=ICON_ROTATE_RIGHT,
 | 
			
		||||
        state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
    )
 | 
			
		||||
    .extend(
 | 
			
		||||
        {
 | 
			
		||||
            cv.GenerateID(CONF_AS5600_ID): cv.use_id(AS5600Component),
 | 
			
		||||
            cv.Optional(CONF_OUT_OF_RANGE_MODE): cv.enum(
 | 
			
		||||
                OUT_OF_RANGE_MODES, upper=True, space="_"
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_RAW_POSITION): sensor.sensor_schema(
 | 
			
		||||
                accuracy_decimals=0,
 | 
			
		||||
                icon=ICON_ROTATE_RIGHT,
 | 
			
		||||
                state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_GAIN): sensor.sensor_schema(
 | 
			
		||||
                accuracy_decimals=0,
 | 
			
		||||
                state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
                entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_MAGNITUDE): sensor.sensor_schema(
 | 
			
		||||
                accuracy_decimals=0,
 | 
			
		||||
                icon=ICON_MAGNET,
 | 
			
		||||
                state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
                entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_STATUS): sensor.sensor_schema(
 | 
			
		||||
                accuracy_decimals=0,
 | 
			
		||||
                icon=ICON_MAGNET,
 | 
			
		||||
                state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
                entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
 | 
			
		||||
            ),
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
    .extend(cv.polling_component_schema("60s"))
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    await cg.register_parented(var, config[CONF_AS5600_ID])
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
    await sensor.register_sensor(var, config)
 | 
			
		||||
 | 
			
		||||
    if out_of_range_mode_config := config.get(CONF_OUT_OF_RANGE_MODE):
 | 
			
		||||
        cg.add(var.set_out_of_range_mode(out_of_range_mode_config))
 | 
			
		||||
 | 
			
		||||
    if angle_config := config.get(CONF_ANGLE):
 | 
			
		||||
        sens = await sensor.new_sensor(angle_config)
 | 
			
		||||
        cg.add(var.set_angle_sensor(sens))
 | 
			
		||||
 | 
			
		||||
    if raw_angle_config := config.get(CONF_RAW_ANGLE):
 | 
			
		||||
        sens = await sensor.new_sensor(raw_angle_config)
 | 
			
		||||
        cg.add(var.set_raw_angle_sensor(sens))
 | 
			
		||||
 | 
			
		||||
    if position_config := config.get(CONF_POSITION):
 | 
			
		||||
        sens = await sensor.new_sensor(position_config)
 | 
			
		||||
        cg.add(var.set_position_sensor(sens))
 | 
			
		||||
 | 
			
		||||
    if raw_position_config := config.get(CONF_RAW_POSITION):
 | 
			
		||||
        sens = await sensor.new_sensor(raw_position_config)
 | 
			
		||||
        cg.add(var.set_raw_position_sensor(sens))
 | 
			
		||||
 | 
			
		||||
    if gain_config := config.get(CONF_GAIN):
 | 
			
		||||
        sens = await sensor.new_sensor(gain_config)
 | 
			
		||||
        cg.add(var.set_gain_sensor(sens))
 | 
			
		||||
 | 
			
		||||
    if magnitude_config := config.get(CONF_MAGNITUDE):
 | 
			
		||||
        sens = await sensor.new_sensor(magnitude_config)
 | 
			
		||||
        cg.add(var.set_magnitude_sensor(sens))
 | 
			
		||||
 | 
			
		||||
    if status_config := config.get(CONF_STATUS):
 | 
			
		||||
        sens = await sensor.new_sensor(status_config)
 | 
			
		||||
        cg.add(var.set_status_sensor(sens))
 | 
			
		||||
							
								
								
									
										98
									
								
								esphome/components/as5600/sensor/as5600_sensor.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										98
									
								
								esphome/components/as5600/sensor/as5600_sensor.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,98 @@
 | 
			
		||||
#include "as5600_sensor.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace as5600 {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "as5600.sensor";
 | 
			
		||||
 | 
			
		||||
// Configuration registers
 | 
			
		||||
static const uint8_t REGISTER_ZMCO = 0x00;  // 8 bytes  / R
 | 
			
		||||
static const uint8_t REGISTER_ZPOS = 0x01;  // 16 bytes / RW
 | 
			
		||||
static const uint8_t REGISTER_MPOS = 0x03;  // 16 bytes / RW
 | 
			
		||||
static const uint8_t REGISTER_MANG = 0x05;  // 16 bytes / RW
 | 
			
		||||
static const uint8_t REGISTER_CONF = 0x07;  // 16 bytes / RW
 | 
			
		||||
 | 
			
		||||
// Output registers
 | 
			
		||||
static const uint8_t REGISTER_ANGLE_RAW = 0x0C;  // 16 bytes / R
 | 
			
		||||
static const uint8_t REGISTER_ANGLE = 0x0E;      // 16 bytes / R
 | 
			
		||||
 | 
			
		||||
// Status registers
 | 
			
		||||
static const uint8_t REGISTER_STATUS = 0x0B;     // 8 bytes  / R
 | 
			
		||||
static const uint8_t REGISTER_AGC = 0x1A;        // 8 bytes  / R
 | 
			
		||||
static const uint8_t REGISTER_MAGNITUDE = 0x1B;  // 16 bytes / R
 | 
			
		||||
 | 
			
		||||
float AS5600Sensor::get_setup_priority() const { return setup_priority::DATA; }
 | 
			
		||||
 | 
			
		||||
void AS5600Sensor::dump_config() {
 | 
			
		||||
  LOG_SENSOR("", "AS5600 Sensor", this);
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Out of Range Mode: %u", this->out_of_range_mode_);
 | 
			
		||||
  if (this->angle_sensor_ != nullptr) {
 | 
			
		||||
    LOG_SENSOR("  ", "Angle Sensor", this->angle_sensor_);
 | 
			
		||||
  }
 | 
			
		||||
  if (this->raw_angle_sensor_ != nullptr) {
 | 
			
		||||
    LOG_SENSOR("  ", "Raw Angle Sensor", this->raw_angle_sensor_);
 | 
			
		||||
  }
 | 
			
		||||
  if (this->position_sensor_ != nullptr) {
 | 
			
		||||
    LOG_SENSOR("  ", "Position Sensor", this->position_sensor_);
 | 
			
		||||
  }
 | 
			
		||||
  if (this->raw_position_sensor_ != nullptr) {
 | 
			
		||||
    LOG_SENSOR("  ", "Raw Position Sensor", this->raw_position_sensor_);
 | 
			
		||||
  }
 | 
			
		||||
  if (this->gain_sensor_ != nullptr) {
 | 
			
		||||
    LOG_SENSOR("  ", "Gain Sensor", this->gain_sensor_);
 | 
			
		||||
  }
 | 
			
		||||
  if (this->magnitude_sensor_ != nullptr) {
 | 
			
		||||
    LOG_SENSOR("  ", "Magnitude Sensor", this->magnitude_sensor_);
 | 
			
		||||
  }
 | 
			
		||||
  if (this->status_sensor_ != nullptr) {
 | 
			
		||||
    LOG_SENSOR("  ", "Status Sensor", this->status_sensor_);
 | 
			
		||||
  }
 | 
			
		||||
  LOG_UPDATE_INTERVAL(this);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void AS5600Sensor::update() {
 | 
			
		||||
  if (this->gain_sensor_ != nullptr) {
 | 
			
		||||
    this->gain_sensor_->publish_state(this->parent_->reg(REGISTER_AGC).get());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (this->magnitude_sensor_ != nullptr) {
 | 
			
		||||
    uint16_t value = 0;
 | 
			
		||||
    this->parent_->read_byte_16(REGISTER_MAGNITUDE, &value);
 | 
			
		||||
    this->magnitude_sensor_->publish_state(value);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // 2 = magnet not detected
 | 
			
		||||
  // 4 = magnet just right
 | 
			
		||||
  // 5 = magnet too strong
 | 
			
		||||
  // 6 = magnet too weak
 | 
			
		||||
  if (this->status_sensor_ != nullptr) {
 | 
			
		||||
    this->status_sensor_->publish_state(this->parent_->read_magnet_status());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  auto pos = this->parent_->read_position();
 | 
			
		||||
  if (!pos.has_value()) {
 | 
			
		||||
    this->status_set_warning();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  auto raw = this->parent_->read_raw_position();
 | 
			
		||||
  if (!raw.has_value()) {
 | 
			
		||||
    this->status_set_warning();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (this->out_of_range_mode_ == OUT_RANGE_MODE_NAN) {
 | 
			
		||||
    this->publish_state(this->parent_->in_range(raw.value()) ? pos.value() : NAN);
 | 
			
		||||
  } else {
 | 
			
		||||
    this->publish_state(pos.value());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (this->raw_position_sensor_ != nullptr) {
 | 
			
		||||
    this->raw_position_sensor_->publish_state(raw.value());
 | 
			
		||||
  }
 | 
			
		||||
  this->status_clear_warning();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace as5600
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										43
									
								
								esphome/components/as5600/sensor/as5600_sensor.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								esphome/components/as5600/sensor/as5600_sensor.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,43 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/core/hal.h"
 | 
			
		||||
#include "esphome/core/preferences.h"
 | 
			
		||||
#include "esphome/components/i2c/i2c.h"
 | 
			
		||||
#include "esphome/components/sensor/sensor.h"
 | 
			
		||||
#include "esphome/components/as5600/as5600.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace as5600 {
 | 
			
		||||
 | 
			
		||||
class AS5600Sensor : public PollingComponent, public Parented<AS5600Component>, public sensor::Sensor {
 | 
			
		||||
 public:
 | 
			
		||||
  void update() override;
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
  float get_setup_priority() const override;
 | 
			
		||||
 | 
			
		||||
  void set_angle_sensor(sensor::Sensor *angle_sensor) { this->angle_sensor_ = angle_sensor; }
 | 
			
		||||
  void set_raw_angle_sensor(sensor::Sensor *raw_angle_sensor) { this->raw_angle_sensor_ = raw_angle_sensor; }
 | 
			
		||||
  void set_position_sensor(sensor::Sensor *position_sensor) { this->position_sensor_ = position_sensor; }
 | 
			
		||||
  void set_raw_position_sensor(sensor::Sensor *raw_position_sensor) {
 | 
			
		||||
    this->raw_position_sensor_ = raw_position_sensor;
 | 
			
		||||
  }
 | 
			
		||||
  void set_gain_sensor(sensor::Sensor *gain_sensor) { this->gain_sensor_ = gain_sensor; }
 | 
			
		||||
  void set_magnitude_sensor(sensor::Sensor *magnitude_sensor) { this->magnitude_sensor_ = magnitude_sensor; }
 | 
			
		||||
  void set_status_sensor(sensor::Sensor *status_sensor) { this->status_sensor_ = status_sensor; }
 | 
			
		||||
  void set_out_of_range_mode(OutRangeMode oor_mode) { this->out_of_range_mode_ = oor_mode; }
 | 
			
		||||
  OutRangeMode get_out_of_range_mode() { return this->out_of_range_mode_; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  sensor::Sensor *angle_sensor_{nullptr};
 | 
			
		||||
  sensor::Sensor *raw_angle_sensor_{nullptr};
 | 
			
		||||
  sensor::Sensor *position_sensor_{nullptr};
 | 
			
		||||
  sensor::Sensor *raw_position_sensor_{nullptr};
 | 
			
		||||
  sensor::Sensor *gain_sensor_{nullptr};
 | 
			
		||||
  sensor::Sensor *magnitude_sensor_{nullptr};
 | 
			
		||||
  sensor::Sensor *status_sensor_{nullptr};
 | 
			
		||||
  OutRangeMode out_of_range_mode_{OUT_RANGE_MODE_MIN_MAX};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace as5600
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -15,6 +15,16 @@ void BangBangClimate::setup() {
 | 
			
		||||
    this->publish_state();
 | 
			
		||||
  });
 | 
			
		||||
  this->current_temperature = this->sensor_->state;
 | 
			
		||||
 | 
			
		||||
  // register for humidity values and get initial state
 | 
			
		||||
  if (this->humidity_sensor_ != nullptr) {
 | 
			
		||||
    this->humidity_sensor_->add_on_state_callback([this](float state) {
 | 
			
		||||
      this->current_humidity = state;
 | 
			
		||||
      this->publish_state();
 | 
			
		||||
    });
 | 
			
		||||
    this->current_humidity = this->humidity_sensor_->state;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // restore set points
 | 
			
		||||
  auto restore = this->restore_state_();
 | 
			
		||||
  if (restore.has_value()) {
 | 
			
		||||
@@ -47,6 +57,8 @@ void BangBangClimate::control(const climate::ClimateCall &call) {
 | 
			
		||||
climate::ClimateTraits BangBangClimate::traits() {
 | 
			
		||||
  auto traits = climate::ClimateTraits();
 | 
			
		||||
  traits.set_supports_current_temperature(true);
 | 
			
		||||
  if (this->humidity_sensor_ != nullptr)
 | 
			
		||||
    traits.set_supports_current_humidity(true);
 | 
			
		||||
  traits.set_supported_modes({
 | 
			
		||||
      climate::CLIMATE_MODE_OFF,
 | 
			
		||||
  });
 | 
			
		||||
@@ -171,6 +183,7 @@ void BangBangClimate::set_away_config(const BangBangClimateTargetTempConfig &awa
 | 
			
		||||
BangBangClimate::BangBangClimate()
 | 
			
		||||
    : idle_trigger_(new Trigger<>()), cool_trigger_(new Trigger<>()), heat_trigger_(new Trigger<>()) {}
 | 
			
		||||
void BangBangClimate::set_sensor(sensor::Sensor *sensor) { this->sensor_ = sensor; }
 | 
			
		||||
void BangBangClimate::set_humidity_sensor(sensor::Sensor *humidity_sensor) { this->humidity_sensor_ = humidity_sensor; }
 | 
			
		||||
Trigger<> *BangBangClimate::get_idle_trigger() const { return this->idle_trigger_; }
 | 
			
		||||
Trigger<> *BangBangClimate::get_cool_trigger() const { return this->cool_trigger_; }
 | 
			
		||||
void BangBangClimate::set_supports_cool(bool supports_cool) { this->supports_cool_ = supports_cool; }
 | 
			
		||||
 
 | 
			
		||||
@@ -24,6 +24,7 @@ class BangBangClimate : public climate::Climate, public Component {
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
 | 
			
		||||
  void set_sensor(sensor::Sensor *sensor);
 | 
			
		||||
  void set_humidity_sensor(sensor::Sensor *humidity_sensor);
 | 
			
		||||
  Trigger<> *get_idle_trigger() const;
 | 
			
		||||
  Trigger<> *get_cool_trigger() const;
 | 
			
		||||
  void set_supports_cool(bool supports_cool);
 | 
			
		||||
@@ -48,6 +49,9 @@ class BangBangClimate : public climate::Climate, public Component {
 | 
			
		||||
 | 
			
		||||
  /// The sensor used for getting the current temperature
 | 
			
		||||
  sensor::Sensor *sensor_{nullptr};
 | 
			
		||||
  /// The sensor used for getting the current humidity
 | 
			
		||||
  sensor::Sensor *humidity_sensor_{nullptr};
 | 
			
		||||
 | 
			
		||||
  /** The trigger to call when the controller should switch to idle mode.
 | 
			
		||||
   *
 | 
			
		||||
   * In idle mode, the controller is assumed to have both heating and cooling disabled.
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,7 @@ from esphome.const import (
 | 
			
		||||
    CONF_DEFAULT_TARGET_TEMPERATURE_HIGH,
 | 
			
		||||
    CONF_DEFAULT_TARGET_TEMPERATURE_LOW,
 | 
			
		||||
    CONF_HEAT_ACTION,
 | 
			
		||||
    CONF_HUMIDITY_SENSOR,
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    CONF_IDLE_ACTION,
 | 
			
		||||
    CONF_SENSOR,
 | 
			
		||||
@@ -22,6 +23,7 @@ CONFIG_SCHEMA = cv.All(
 | 
			
		||||
        {
 | 
			
		||||
            cv.GenerateID(): cv.declare_id(BangBangClimate),
 | 
			
		||||
            cv.Required(CONF_SENSOR): cv.use_id(sensor.Sensor),
 | 
			
		||||
            cv.Optional(CONF_HUMIDITY_SENSOR): cv.use_id(sensor.Sensor),
 | 
			
		||||
            cv.Required(CONF_DEFAULT_TARGET_TEMPERATURE_LOW): cv.temperature,
 | 
			
		||||
            cv.Required(CONF_DEFAULT_TARGET_TEMPERATURE_HIGH): cv.temperature,
 | 
			
		||||
            cv.Required(CONF_IDLE_ACTION): automation.validate_automation(single=True),
 | 
			
		||||
@@ -47,6 +49,10 @@ async def to_code(config):
 | 
			
		||||
    sens = await cg.get_variable(config[CONF_SENSOR])
 | 
			
		||||
    cg.add(var.set_sensor(sens))
 | 
			
		||||
 | 
			
		||||
    if CONF_HUMIDITY_SENSOR in config:
 | 
			
		||||
        sens = await cg.get_variable(config[CONF_HUMIDITY_SENSOR])
 | 
			
		||||
        cg.add(var.set_humidity_sensor(sens))
 | 
			
		||||
 | 
			
		||||
    normal_config = BangBangClimateTargetTempConfig(
 | 
			
		||||
        config[CONF_DEFAULT_TARGET_TEMPERATURE_LOW],
 | 
			
		||||
        config[CONF_DEFAULT_TARGET_TEMPERATURE_HIGH],
 | 
			
		||||
 
 | 
			
		||||
@@ -242,7 +242,7 @@ void BedJetHub::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t ga
 | 
			
		||||
        this->set_notify_(true);
 | 
			
		||||
 | 
			
		||||
#ifdef USE_TIME
 | 
			
		||||
        if (this->time_id_.has_value()) {
 | 
			
		||||
        if (this->time_id_ != nullptr) {
 | 
			
		||||
          this->send_local_time();
 | 
			
		||||
        }
 | 
			
		||||
#endif
 | 
			
		||||
@@ -441,9 +441,8 @@ uint8_t BedJetHub::write_notify_config_descriptor_(bool enable) {
 | 
			
		||||
 | 
			
		||||
#ifdef USE_TIME
 | 
			
		||||
void BedJetHub::send_local_time() {
 | 
			
		||||
  if (this->time_id_.has_value()) {
 | 
			
		||||
    auto *time_id = *this->time_id_;
 | 
			
		||||
    ESPTime now = time_id->now();
 | 
			
		||||
  if (this->time_id_ != nullptr) {
 | 
			
		||||
    ESPTime now = this->time_id_->now();
 | 
			
		||||
    if (now.is_valid()) {
 | 
			
		||||
      this->set_clock(now.hour, now.minute);
 | 
			
		||||
      ESP_LOGD(TAG, "Using time component to set BedJet clock: %d:%02d", now.hour, now.minute);
 | 
			
		||||
@@ -454,10 +453,9 @@ void BedJetHub::send_local_time() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BedJetHub::setup_time_() {
 | 
			
		||||
  if (this->time_id_.has_value()) {
 | 
			
		||||
  if (this->time_id_ != nullptr) {
 | 
			
		||||
    this->send_local_time();
 | 
			
		||||
    auto *time_id = *this->time_id_;
 | 
			
		||||
    time_id->add_on_time_sync_callback([this] { this->send_local_time(); });
 | 
			
		||||
    this->time_id_->add_on_time_sync_callback([this] { this->send_local_time(); });
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGI(TAG, "`time_id` is not configured: will not sync BedJet clock.");
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -141,7 +141,7 @@ class BedJetHub : public esphome::ble_client::BLEClientNode, public PollingCompo
 | 
			
		||||
#ifdef USE_TIME
 | 
			
		||||
  /** Initializes time sync callbacks to support syncing current time to the BedJet. */
 | 
			
		||||
  void setup_time_();
 | 
			
		||||
  optional<time::RealTimeClock *> time_id_{};
 | 
			
		||||
  time::RealTimeClock *time_id_{nullptr};
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  uint32_t timeout_{DEFAULT_STATUS_TIMEOUT};
 | 
			
		||||
 
 | 
			
		||||
@@ -141,6 +141,7 @@ DelayedOffFilter = binary_sensor_ns.class_("DelayedOffFilter", Filter, cg.Compon
 | 
			
		||||
InvertFilter = binary_sensor_ns.class_("InvertFilter", Filter)
 | 
			
		||||
AutorepeatFilter = binary_sensor_ns.class_("AutorepeatFilter", Filter, cg.Component)
 | 
			
		||||
LambdaFilter = binary_sensor_ns.class_("LambdaFilter", Filter)
 | 
			
		||||
SettleFilter = binary_sensor_ns.class_("SettleFilter", Filter, cg.Component)
 | 
			
		||||
 | 
			
		||||
FILTER_REGISTRY = Registry()
 | 
			
		||||
validate_filters = cv.validate_registry("filter", FILTER_REGISTRY)
 | 
			
		||||
@@ -259,6 +260,19 @@ async def lambda_filter_to_code(config, filter_id):
 | 
			
		||||
    return cg.new_Pvariable(filter_id, lambda_)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@register_filter(
 | 
			
		||||
    "settle",
 | 
			
		||||
    SettleFilter,
 | 
			
		||||
    cv.templatable(cv.positive_time_period_milliseconds),
 | 
			
		||||
)
 | 
			
		||||
async def settle_filter_to_code(config, filter_id):
 | 
			
		||||
    var = cg.new_Pvariable(filter_id)
 | 
			
		||||
    await cg.register_component(var, {})
 | 
			
		||||
    template_ = await cg.templatable(config, [], cg.uint32)
 | 
			
		||||
    cg.add(var.set_delay(template_))
 | 
			
		||||
    return var
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
MULTI_CLICK_TIMING_SCHEMA = cv.Schema(
 | 
			
		||||
    {
 | 
			
		||||
        cv.Optional(CONF_STATE): cv.boolean,
 | 
			
		||||
 
 | 
			
		||||
@@ -111,6 +111,23 @@ LambdaFilter::LambdaFilter(std::function<optional<bool>(bool)> f) : f_(std::move
 | 
			
		||||
 | 
			
		||||
optional<bool> LambdaFilter::new_value(bool value, bool is_initial) { return this->f_(value); }
 | 
			
		||||
 | 
			
		||||
optional<bool> SettleFilter::new_value(bool value, bool is_initial) {
 | 
			
		||||
  if (!this->steady_) {
 | 
			
		||||
    this->set_timeout("SETTLE", this->delay_.value(), [this, value, is_initial]() {
 | 
			
		||||
      this->steady_ = true;
 | 
			
		||||
      this->output(value, is_initial);
 | 
			
		||||
    });
 | 
			
		||||
    return {};
 | 
			
		||||
  } else {
 | 
			
		||||
    this->steady_ = false;
 | 
			
		||||
    this->output(value, is_initial);
 | 
			
		||||
    this->set_timeout("SETTLE", this->delay_.value(), [this]() { this->steady_ = true; });
 | 
			
		||||
    return value;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float SettleFilter::get_setup_priority() const { return setup_priority::HARDWARE; }
 | 
			
		||||
 | 
			
		||||
}  // namespace binary_sensor
 | 
			
		||||
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
@@ -108,6 +108,19 @@ class LambdaFilter : public Filter {
 | 
			
		||||
  std::function<optional<bool>(bool)> f_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class SettleFilter : public Filter, public Component {
 | 
			
		||||
 public:
 | 
			
		||||
  optional<bool> new_value(bool value, bool is_initial) override;
 | 
			
		||||
 | 
			
		||||
  float get_setup_priority() const override;
 | 
			
		||||
 | 
			
		||||
  template<typename T> void set_delay(T delay) { this->delay_ = delay; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  TemplatableValue<uint32_t> delay_{};
 | 
			
		||||
  bool steady_{true};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace binary_sensor
 | 
			
		||||
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
@@ -18,6 +18,7 @@ from esphome.const import (
 | 
			
		||||
    UNIT_KILOWATT_HOURS,
 | 
			
		||||
    UNIT_VOLT,
 | 
			
		||||
    UNIT_WATT,
 | 
			
		||||
    STATE_CLASS_TOTAL_INCREASING,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["uart"]
 | 
			
		||||
@@ -54,6 +55,7 @@ CONFIG_SCHEMA = (
 | 
			
		||||
                unit_of_measurement=UNIT_KILOWATT_HOURS,
 | 
			
		||||
                accuracy_decimals=0,
 | 
			
		||||
                device_class=DEVICE_CLASS_ENERGY,
 | 
			
		||||
                state_class=STATE_CLASS_TOTAL_INCREASING,
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_INTERNAL_TEMPERATURE): sensor.sensor_schema(
 | 
			
		||||
                unit_of_measurement=UNIT_CELSIUS,
 | 
			
		||||
 
 | 
			
		||||
@@ -19,6 +19,7 @@ from esphome.const import (
 | 
			
		||||
    UNIT_VOLT,
 | 
			
		||||
    UNIT_WATT,
 | 
			
		||||
    UNIT_HERTZ,
 | 
			
		||||
    STATE_CLASS_TOTAL_INCREASING,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["uart"]
 | 
			
		||||
@@ -52,6 +53,7 @@ CONFIG_SCHEMA = (
 | 
			
		||||
                unit_of_measurement=UNIT_KILOWATT_HOURS,
 | 
			
		||||
                accuracy_decimals=0,
 | 
			
		||||
                device_class=DEVICE_CLASS_ENERGY,
 | 
			
		||||
                state_class=STATE_CLASS_TOTAL_INCREASING,
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_FREQUENCY): sensor.sensor_schema(
 | 
			
		||||
                unit_of_measurement=UNIT_HERTZ,
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.automation import maybe_simple_id
 | 
			
		||||
from esphome.components import esp32_ble_tracker, esp32_ble_client
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_CHARACTERISTIC_UUID,
 | 
			
		||||
@@ -15,7 +16,7 @@ from esphome.const import (
 | 
			
		||||
from esphome import automation
 | 
			
		||||
 | 
			
		||||
AUTO_LOAD = ["esp32_ble_client"]
 | 
			
		||||
CODEOWNERS = ["@buxtronix"]
 | 
			
		||||
CODEOWNERS = ["@buxtronix", "@clydebarrow"]
 | 
			
		||||
DEPENDENCIES = ["esp32_ble_tracker"]
 | 
			
		||||
 | 
			
		||||
ble_client_ns = cg.esphome_ns.namespace("ble_client")
 | 
			
		||||
@@ -43,6 +44,10 @@ BLEClientNumericComparisonRequestTrigger = ble_client_ns.class_(
 | 
			
		||||
 | 
			
		||||
# Actions
 | 
			
		||||
BLEWriteAction = ble_client_ns.class_("BLEClientWriteAction", automation.Action)
 | 
			
		||||
BLEConnectAction = ble_client_ns.class_("BLEClientConnectAction", automation.Action)
 | 
			
		||||
BLEDisconnectAction = ble_client_ns.class_(
 | 
			
		||||
    "BLEClientDisconnectAction", automation.Action
 | 
			
		||||
)
 | 
			
		||||
BLEPasskeyReplyAction = ble_client_ns.class_(
 | 
			
		||||
    "BLEClientPasskeyReplyAction", automation.Action
 | 
			
		||||
)
 | 
			
		||||
@@ -58,6 +63,7 @@ CONF_ACCEPT = "accept"
 | 
			
		||||
CONF_ON_PASSKEY_REQUEST = "on_passkey_request"
 | 
			
		||||
CONF_ON_PASSKEY_NOTIFICATION = "on_passkey_notification"
 | 
			
		||||
CONF_ON_NUMERIC_COMPARISON_REQUEST = "on_numeric_comparison_request"
 | 
			
		||||
CONF_AUTO_CONNECT = "auto_connect"
 | 
			
		||||
 | 
			
		||||
# Espressif platformio framework is built with MAX_BLE_CONN to 3, so
 | 
			
		||||
# enforce this in yaml checks.
 | 
			
		||||
@@ -69,6 +75,7 @@ CONFIG_SCHEMA = (
 | 
			
		||||
            cv.GenerateID(): cv.declare_id(BLEClient),
 | 
			
		||||
            cv.Required(CONF_MAC_ADDRESS): cv.mac_address,
 | 
			
		||||
            cv.Optional(CONF_NAME): cv.string,
 | 
			
		||||
            cv.Optional(CONF_AUTO_CONNECT, default=True): cv.boolean,
 | 
			
		||||
            cv.Optional(CONF_ON_CONNECT): automation.validate_automation(
 | 
			
		||||
                {
 | 
			
		||||
                    cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
 | 
			
		||||
@@ -135,6 +142,12 @@ BLE_WRITE_ACTION_SCHEMA = cv.Schema(
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
BLE_CONNECT_ACTION_SCHEMA = maybe_simple_id(
 | 
			
		||||
    {
 | 
			
		||||
        cv.GenerateID(CONF_ID): cv.use_id(BLEClient),
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
BLE_NUMERIC_COMPARISON_REPLY_ACTION_SCHEMA = cv.Schema(
 | 
			
		||||
    {
 | 
			
		||||
        cv.GenerateID(CONF_ID): cv.use_id(BLEClient),
 | 
			
		||||
@@ -157,6 +170,24 @@ BLE_REMOVE_BOND_ACTION_SCHEMA = cv.Schema(
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@automation.register_action(
 | 
			
		||||
    "ble_client.disconnect", BLEDisconnectAction, BLE_CONNECT_ACTION_SCHEMA
 | 
			
		||||
)
 | 
			
		||||
async def ble_disconnect_to_code(config, action_id, template_arg, args):
 | 
			
		||||
    parent = await cg.get_variable(config[CONF_ID])
 | 
			
		||||
    var = cg.new_Pvariable(action_id, template_arg, parent)
 | 
			
		||||
    return var
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@automation.register_action(
 | 
			
		||||
    "ble_client.connect", BLEConnectAction, BLE_CONNECT_ACTION_SCHEMA
 | 
			
		||||
)
 | 
			
		||||
async def ble_connect_to_code(config, action_id, template_arg, args):
 | 
			
		||||
    parent = await cg.get_variable(config[CONF_ID])
 | 
			
		||||
    var = cg.new_Pvariable(action_id, template_arg, parent)
 | 
			
		||||
    return var
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@automation.register_action(
 | 
			
		||||
    "ble_client.ble_write", BLEWriteAction, BLE_WRITE_ACTION_SCHEMA
 | 
			
		||||
)
 | 
			
		||||
@@ -261,6 +292,7 @@ async def to_code(config):
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
    await esp32_ble_tracker.register_client(var, config)
 | 
			
		||||
    cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex))
 | 
			
		||||
    cg.add(var.set_auto_connect(config[CONF_AUTO_CONNECT]))
 | 
			
		||||
    for conf in config.get(CONF_ON_CONNECT, []):
 | 
			
		||||
        trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
 | 
			
		||||
        await automation.build_automation(trigger, [], conf)
 | 
			
		||||
 
 | 
			
		||||
@@ -2,76 +2,10 @@
 | 
			
		||||
 | 
			
		||||
#include "automation.h"
 | 
			
		||||
 | 
			
		||||
#include <esp_bt_defs.h>
 | 
			
		||||
#include <esp_gap_ble_api.h>
 | 
			
		||||
#include <esp_gattc_api.h>
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ble_client {
 | 
			
		||||
static const char *const TAG = "ble_client.automation";
 | 
			
		||||
 | 
			
		||||
void BLEWriterClientNode::write(const std::vector<uint8_t> &value) {
 | 
			
		||||
  if (this->node_state != espbt::ClientState::ESTABLISHED) {
 | 
			
		||||
    ESP_LOGW(TAG, "Cannot write to BLE characteristic - not connected");
 | 
			
		||||
    return;
 | 
			
		||||
  } else if (this->ble_char_handle_ == 0) {
 | 
			
		||||
    ESP_LOGW(TAG, "Cannot write to BLE characteristic - characteristic not found");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  esp_gatt_write_type_t write_type;
 | 
			
		||||
  if (this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE) {
 | 
			
		||||
    write_type = ESP_GATT_WRITE_TYPE_RSP;
 | 
			
		||||
    ESP_LOGD(TAG, "Write type: ESP_GATT_WRITE_TYPE_RSP");
 | 
			
		||||
  } else if (this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE_NR) {
 | 
			
		||||
    write_type = ESP_GATT_WRITE_TYPE_NO_RSP;
 | 
			
		||||
    ESP_LOGD(TAG, "Write type: ESP_GATT_WRITE_TYPE_NO_RSP");
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGE(TAG, "Characteristic %s does not allow writing", this->char_uuid_.to_string().c_str());
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGVV(TAG, "Will write %d bytes: %s", value.size(), format_hex_pretty(value).c_str());
 | 
			
		||||
  esp_err_t err =
 | 
			
		||||
      esp_ble_gattc_write_char(this->parent()->get_gattc_if(), this->parent()->get_conn_id(), this->ble_char_handle_,
 | 
			
		||||
                               value.size(), const_cast<uint8_t *>(value.data()), write_type, ESP_GATT_AUTH_REQ_NONE);
 | 
			
		||||
  if (err != ESP_OK) {
 | 
			
		||||
    ESP_LOGE(TAG, "Error writing to characteristic: %s!", esp_err_to_name(err));
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BLEWriterClientNode::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
 | 
			
		||||
                                              esp_ble_gattc_cb_param_t *param) {
 | 
			
		||||
  switch (event) {
 | 
			
		||||
    case ESP_GATTC_REG_EVT:
 | 
			
		||||
      break;
 | 
			
		||||
    case ESP_GATTC_OPEN_EVT:
 | 
			
		||||
      this->node_state = espbt::ClientState::ESTABLISHED;
 | 
			
		||||
      ESP_LOGD(TAG, "Connection established with %s", ble_client_->address_str().c_str());
 | 
			
		||||
      break;
 | 
			
		||||
    case ESP_GATTC_SEARCH_CMPL_EVT: {
 | 
			
		||||
      auto *chr = this->parent()->get_characteristic(this->service_uuid_, this->char_uuid_);
 | 
			
		||||
      if (chr == nullptr) {
 | 
			
		||||
        ESP_LOGW("ble_write_action", "Characteristic %s was not found in service %s",
 | 
			
		||||
                 this->char_uuid_.to_string().c_str(), this->service_uuid_.to_string().c_str());
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      this->ble_char_handle_ = chr->handle;
 | 
			
		||||
      this->char_props_ = chr->properties;
 | 
			
		||||
      this->node_state = espbt::ClientState::ESTABLISHED;
 | 
			
		||||
      ESP_LOGD(TAG, "Found characteristic %s on device %s", this->char_uuid_.to_string().c_str(),
 | 
			
		||||
               ble_client_->address_str().c_str());
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_DISCONNECT_EVT:
 | 
			
		||||
      this->node_state = espbt::ClientState::IDLE;
 | 
			
		||||
      this->ble_char_handle_ = 0;
 | 
			
		||||
      ESP_LOGD(TAG, "Disconnected from %s", ble_client_->address_str().c_str());
 | 
			
		||||
      break;
 | 
			
		||||
    default:
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
const char *const Automation::TAG = "ble_client.automation";
 | 
			
		||||
 | 
			
		||||
}  // namespace ble_client
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
@@ -7,9 +7,19 @@
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/automation.h"
 | 
			
		||||
#include "esphome/components/ble_client/ble_client.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ble_client {
 | 
			
		||||
 | 
			
		||||
// placeholder class for static TAG .
 | 
			
		||||
class Automation {
 | 
			
		||||
 public:
 | 
			
		||||
  // could be made inline with C++17
 | 
			
		||||
  static const char *const TAG;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// implement on_connect automation.
 | 
			
		||||
class BLEClientConnectTrigger : public Trigger<>, public BLEClientNode {
 | 
			
		||||
 public:
 | 
			
		||||
  explicit BLEClientConnectTrigger(BLEClient *parent) { parent->register_ble_node(this); }
 | 
			
		||||
@@ -23,17 +33,28 @@ class BLEClientConnectTrigger : public Trigger<>, public BLEClientNode {
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// on_disconnect automation
 | 
			
		||||
class BLEClientDisconnectTrigger : public Trigger<>, public BLEClientNode {
 | 
			
		||||
 public:
 | 
			
		||||
  explicit BLEClientDisconnectTrigger(BLEClient *parent) { parent->register_ble_node(this); }
 | 
			
		||||
  void loop() override {}
 | 
			
		||||
  void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
 | 
			
		||||
                           esp_ble_gattc_cb_param_t *param) override {
 | 
			
		||||
    if (event == ESP_GATTC_DISCONNECT_EVT &&
 | 
			
		||||
        memcmp(param->disconnect.remote_bda, this->parent_->get_remote_bda(), 6) == 0)
 | 
			
		||||
      this->trigger();
 | 
			
		||||
    if (event == ESP_GATTC_SEARCH_CMPL_EVT)
 | 
			
		||||
      this->node_state = espbt::ClientState::ESTABLISHED;
 | 
			
		||||
    // test for CLOSE and not DISCONNECT - DISCONNECT can occur even if no virtual connection (OPEN event) occurred.
 | 
			
		||||
    // So this will not trigger unless a complete open has previously succeeded.
 | 
			
		||||
    switch (event) {
 | 
			
		||||
      case ESP_GATTC_SEARCH_CMPL_EVT: {
 | 
			
		||||
        this->node_state = espbt::ClientState::ESTABLISHED;
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      case ESP_GATTC_CLOSE_EVT: {
 | 
			
		||||
        this->trigger();
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      default: {
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@@ -42,10 +63,8 @@ class BLEClientPasskeyRequestTrigger : public Trigger<>, public BLEClientNode {
 | 
			
		||||
  explicit BLEClientPasskeyRequestTrigger(BLEClient *parent) { parent->register_ble_node(this); }
 | 
			
		||||
  void loop() override {}
 | 
			
		||||
  void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override {
 | 
			
		||||
    if (event == ESP_GAP_BLE_PASSKEY_REQ_EVT &&
 | 
			
		||||
        memcmp(param->ble_security.auth_cmpl.bd_addr, this->parent_->get_remote_bda(), 6) == 0) {
 | 
			
		||||
    if (event == ESP_GAP_BLE_PASSKEY_REQ_EVT && this->parent_->check_addr(param->ble_security.auth_cmpl.bd_addr))
 | 
			
		||||
      this->trigger();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@@ -54,10 +73,8 @@ class BLEClientPasskeyNotificationTrigger : public Trigger<uint32_t>, public BLE
 | 
			
		||||
  explicit BLEClientPasskeyNotificationTrigger(BLEClient *parent) { parent->register_ble_node(this); }
 | 
			
		||||
  void loop() override {}
 | 
			
		||||
  void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override {
 | 
			
		||||
    if (event == ESP_GAP_BLE_PASSKEY_NOTIF_EVT &&
 | 
			
		||||
        memcmp(param->ble_security.auth_cmpl.bd_addr, this->parent_->get_remote_bda(), 6) == 0) {
 | 
			
		||||
      uint32_t passkey = param->ble_security.key_notif.passkey;
 | 
			
		||||
      this->trigger(passkey);
 | 
			
		||||
    if (event == ESP_GAP_BLE_PASSKEY_NOTIF_EVT && this->parent_->check_addr(param->ble_security.auth_cmpl.bd_addr)) {
 | 
			
		||||
      this->trigger(param->ble_security.key_notif.passkey);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
@@ -67,24 +84,20 @@ class BLEClientNumericComparisonRequestTrigger : public Trigger<uint32_t>, publi
 | 
			
		||||
  explicit BLEClientNumericComparisonRequestTrigger(BLEClient *parent) { parent->register_ble_node(this); }
 | 
			
		||||
  void loop() override {}
 | 
			
		||||
  void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override {
 | 
			
		||||
    if (event == ESP_GAP_BLE_NC_REQ_EVT &&
 | 
			
		||||
        memcmp(param->ble_security.auth_cmpl.bd_addr, this->parent_->get_remote_bda(), 6) == 0) {
 | 
			
		||||
      uint32_t passkey = param->ble_security.key_notif.passkey;
 | 
			
		||||
      this->trigger(passkey);
 | 
			
		||||
    if (event == ESP_GAP_BLE_NC_REQ_EVT && this->parent_->check_addr(param->ble_security.auth_cmpl.bd_addr)) {
 | 
			
		||||
      this->trigger(param->ble_security.key_notif.passkey);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class BLEWriterClientNode : public BLEClientNode {
 | 
			
		||||
// implement the ble_client.ble_write action.
 | 
			
		||||
template<typename... Ts> class BLEClientWriteAction : public Action<Ts...>, public BLEClientNode {
 | 
			
		||||
 public:
 | 
			
		||||
  BLEWriterClientNode(BLEClient *ble_client) {
 | 
			
		||||
  BLEClientWriteAction(BLEClient *ble_client) {
 | 
			
		||||
    ble_client->register_ble_node(this);
 | 
			
		||||
    ble_client_ = ble_client;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Attempts to write the contents of value to char_uuid_.
 | 
			
		||||
  void write(const std::vector<uint8_t> &value);
 | 
			
		||||
 | 
			
		||||
  void set_service_uuid16(uint16_t uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_uint16(uuid); }
 | 
			
		||||
  void set_service_uuid32(uint32_t uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
 | 
			
		||||
  void set_service_uuid128(uint8_t *uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_raw(uuid); }
 | 
			
		||||
@@ -93,29 +106,6 @@ class BLEWriterClientNode : public BLEClientNode {
 | 
			
		||||
  void set_char_uuid32(uint32_t uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
 | 
			
		||||
  void set_char_uuid128(uint8_t *uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_raw(uuid); }
 | 
			
		||||
 | 
			
		||||
  void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
 | 
			
		||||
                           esp_ble_gattc_cb_param_t *param) override;
 | 
			
		||||
 | 
			
		||||
 private:
 | 
			
		||||
  BLEClient *ble_client_;
 | 
			
		||||
  int ble_char_handle_ = 0;
 | 
			
		||||
  esp_gatt_char_prop_t char_props_;
 | 
			
		||||
  espbt::ESPBTUUID service_uuid_;
 | 
			
		||||
  espbt::ESPBTUUID char_uuid_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
template<typename... Ts> class BLEClientWriteAction : public Action<Ts...>, public BLEWriterClientNode {
 | 
			
		||||
 public:
 | 
			
		||||
  BLEClientWriteAction(BLEClient *ble_client) : BLEWriterClientNode(ble_client) {}
 | 
			
		||||
 | 
			
		||||
  void play(Ts... x) override {
 | 
			
		||||
    if (has_simple_value_) {
 | 
			
		||||
      return write(this->value_simple_);
 | 
			
		||||
    } else {
 | 
			
		||||
      return write(this->value_template_(x...));
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  void set_value_template(std::function<std::vector<uint8_t>(Ts...)> func) {
 | 
			
		||||
    this->value_template_ = std::move(func);
 | 
			
		||||
    has_simple_value_ = false;
 | 
			
		||||
@@ -126,10 +116,94 @@ template<typename... Ts> class BLEClientWriteAction : public Action<Ts...>, publ
 | 
			
		||||
    has_simple_value_ = true;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  void play(Ts... x) override {}
 | 
			
		||||
 | 
			
		||||
  void play_complex(Ts... x) override {
 | 
			
		||||
    this->num_running_++;
 | 
			
		||||
    this->var_ = std::make_tuple(x...);
 | 
			
		||||
    auto value = this->has_simple_value_ ? this->value_simple_ : this->value_template_(x...);
 | 
			
		||||
    // on write failure, continue the automation chain rather than stopping so that e.g. disconnect can work.
 | 
			
		||||
    if (!write(value))
 | 
			
		||||
      this->play_next_(x...);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Note about logging: the esph_log_X macros are used here because the CI checks complain about use of the ESP LOG
 | 
			
		||||
   * macros in header files (Can't even write it in a comment!)
 | 
			
		||||
   * Not sure why, because they seem to work just fine.
 | 
			
		||||
   * The problem is that the implementation of a templated class can't be placed in a .cpp file when using C++ less than
 | 
			
		||||
   * 17, so the methods have to be here.  The esph_log_X macros are equivalent in function, but don't trigger the CI
 | 
			
		||||
   * errors.
 | 
			
		||||
   */
 | 
			
		||||
  // initiate the write. Return true if all went well, will be followed by a WRITE_CHAR event.
 | 
			
		||||
  bool write(const std::vector<uint8_t> &value) {
 | 
			
		||||
    if (this->node_state != espbt::ClientState::ESTABLISHED) {
 | 
			
		||||
      esph_log_w(Automation::TAG, "Cannot write to BLE characteristic - not connected");
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
    esph_log_vv(Automation::TAG, "Will write %d bytes: %s", value.size(), format_hex_pretty(value).c_str());
 | 
			
		||||
    esp_err_t err = esp_ble_gattc_write_char(this->parent()->get_gattc_if(), this->parent()->get_conn_id(),
 | 
			
		||||
                                             this->char_handle_, value.size(), const_cast<uint8_t *>(value.data()),
 | 
			
		||||
                                             this->write_type_, ESP_GATT_AUTH_REQ_NONE);
 | 
			
		||||
    if (err != ESP_OK) {
 | 
			
		||||
      esph_log_e(Automation::TAG, "Error writing to characteristic: %s!", esp_err_to_name(err));
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
 | 
			
		||||
                           esp_ble_gattc_cb_param_t *param) override {
 | 
			
		||||
    switch (event) {
 | 
			
		||||
      case ESP_GATTC_WRITE_CHAR_EVT:
 | 
			
		||||
        // upstream code checked the MAC address, verify the characteristic.
 | 
			
		||||
        if (param->write.handle == this->char_handle_)
 | 
			
		||||
          this->parent()->run_later([this]() { this->play_next_tuple_(this->var_); });
 | 
			
		||||
        break;
 | 
			
		||||
      case ESP_GATTC_DISCONNECT_EVT:
 | 
			
		||||
        if (this->num_running_ != 0)
 | 
			
		||||
          this->stop_complex();
 | 
			
		||||
        break;
 | 
			
		||||
      case ESP_GATTC_SEARCH_CMPL_EVT: {
 | 
			
		||||
        auto *chr = this->parent()->get_characteristic(this->service_uuid_, this->char_uuid_);
 | 
			
		||||
        if (chr == nullptr) {
 | 
			
		||||
          esph_log_w("ble_write_action", "Characteristic %s was not found in service %s",
 | 
			
		||||
                     this->char_uuid_.to_string().c_str(), this->service_uuid_.to_string().c_str());
 | 
			
		||||
          break;
 | 
			
		||||
        }
 | 
			
		||||
        this->char_handle_ = chr->handle;
 | 
			
		||||
        this->char_props_ = chr->properties;
 | 
			
		||||
        if (this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE) {
 | 
			
		||||
          this->write_type_ = ESP_GATT_WRITE_TYPE_RSP;
 | 
			
		||||
          esph_log_d(Automation::TAG, "Write type: ESP_GATT_WRITE_TYPE_RSP");
 | 
			
		||||
        } else if (this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE_NR) {
 | 
			
		||||
          this->write_type_ = ESP_GATT_WRITE_TYPE_NO_RSP;
 | 
			
		||||
          esph_log_d(Automation::TAG, "Write type: ESP_GATT_WRITE_TYPE_NO_RSP");
 | 
			
		||||
        } else {
 | 
			
		||||
          esph_log_e(Automation::TAG, "Characteristic %s does not allow writing", this->char_uuid_.to_string().c_str());
 | 
			
		||||
          break;
 | 
			
		||||
        }
 | 
			
		||||
        this->node_state = espbt::ClientState::ESTABLISHED;
 | 
			
		||||
        esph_log_d(Automation::TAG, "Found characteristic %s on device %s", this->char_uuid_.to_string().c_str(),
 | 
			
		||||
                   ble_client_->address_str().c_str());
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      default:
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 private:
 | 
			
		||||
  BLEClient *ble_client_;
 | 
			
		||||
  bool has_simple_value_ = true;
 | 
			
		||||
  std::vector<uint8_t> value_simple_;
 | 
			
		||||
  std::function<std::vector<uint8_t>(Ts...)> value_template_{};
 | 
			
		||||
  espbt::ESPBTUUID service_uuid_;
 | 
			
		||||
  espbt::ESPBTUUID char_uuid_;
 | 
			
		||||
  std::tuple<Ts...> var_{};
 | 
			
		||||
  uint16_t char_handle_{};
 | 
			
		||||
  esp_gatt_char_prop_t char_props_{};
 | 
			
		||||
  esp_gatt_write_type_t write_type_{};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
template<typename... Ts> class BLEClientPasskeyReplyAction : public Action<Ts...> {
 | 
			
		||||
@@ -212,6 +286,92 @@ template<typename... Ts> class BLEClientRemoveBondAction : public Action<Ts...>
 | 
			
		||||
  BLEClient *parent_{nullptr};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
template<typename... Ts> class BLEClientConnectAction : public Action<Ts...>, public BLEClientNode {
 | 
			
		||||
 public:
 | 
			
		||||
  BLEClientConnectAction(BLEClient *ble_client) {
 | 
			
		||||
    ble_client->register_ble_node(this);
 | 
			
		||||
    ble_client_ = ble_client;
 | 
			
		||||
  }
 | 
			
		||||
  void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
 | 
			
		||||
                           esp_ble_gattc_cb_param_t *param) override {
 | 
			
		||||
    if (this->num_running_ == 0)
 | 
			
		||||
      return;
 | 
			
		||||
    switch (event) {
 | 
			
		||||
      case ESP_GATTC_SEARCH_CMPL_EVT:
 | 
			
		||||
        this->node_state = espbt::ClientState::ESTABLISHED;
 | 
			
		||||
        this->parent()->run_later([this]() { this->play_next_tuple_(this->var_); });
 | 
			
		||||
        break;
 | 
			
		||||
      // if the connection is closed, terminate the automation chain.
 | 
			
		||||
      case ESP_GATTC_DISCONNECT_EVT:
 | 
			
		||||
        this->stop_complex();
 | 
			
		||||
        break;
 | 
			
		||||
      default:
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // not used since we override play_complex_
 | 
			
		||||
  void play(Ts... x) override {}
 | 
			
		||||
 | 
			
		||||
  void play_complex(Ts... x) override {
 | 
			
		||||
    // it makes no sense to have multiple instances of this running at the same time.
 | 
			
		||||
    // this would occur only if the same automation was re-triggered while still
 | 
			
		||||
    // running. So just cancel the second chain if this is detected.
 | 
			
		||||
    if (this->num_running_ != 0) {
 | 
			
		||||
      this->stop_complex();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    this->num_running_++;
 | 
			
		||||
    if (this->node_state == espbt::ClientState::ESTABLISHED) {
 | 
			
		||||
      this->play_next_(x...);
 | 
			
		||||
    } else {
 | 
			
		||||
      this->var_ = std::make_tuple(x...);
 | 
			
		||||
      this->ble_client_->connect();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 private:
 | 
			
		||||
  BLEClient *ble_client_;
 | 
			
		||||
  std::tuple<Ts...> var_{};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
template<typename... Ts> class BLEClientDisconnectAction : public Action<Ts...>, public BLEClientNode {
 | 
			
		||||
 public:
 | 
			
		||||
  BLEClientDisconnectAction(BLEClient *ble_client) {
 | 
			
		||||
    ble_client->register_ble_node(this);
 | 
			
		||||
    ble_client_ = ble_client;
 | 
			
		||||
  }
 | 
			
		||||
  void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
 | 
			
		||||
                           esp_ble_gattc_cb_param_t *param) override {
 | 
			
		||||
    if (this->num_running_ == 0)
 | 
			
		||||
      return;
 | 
			
		||||
    switch (event) {
 | 
			
		||||
      case ESP_GATTC_CLOSE_EVT:
 | 
			
		||||
      case ESP_GATTC_DISCONNECT_EVT:
 | 
			
		||||
        this->parent()->run_later([this]() { this->play_next_tuple_(this->var_); });
 | 
			
		||||
        break;
 | 
			
		||||
      default:
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // not used since we override play_complex_
 | 
			
		||||
  void play(Ts... x) override {}
 | 
			
		||||
 | 
			
		||||
  void play_complex(Ts... x) override {
 | 
			
		||||
    this->num_running_++;
 | 
			
		||||
    if (this->node_state == espbt::ClientState::IDLE) {
 | 
			
		||||
      this->play_next_(x...);
 | 
			
		||||
    } else {
 | 
			
		||||
      this->var_ = std::make_tuple(x...);
 | 
			
		||||
      this->ble_client_->disconnect();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 private:
 | 
			
		||||
  BLEClient *ble_client_;
 | 
			
		||||
  std::tuple<Ts...> var_{};
 | 
			
		||||
};
 | 
			
		||||
}  // namespace ble_client
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -26,6 +26,7 @@ void BLEClient::loop() {
 | 
			
		||||
void BLEClient::dump_config() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "BLE Client:");
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Address: %s", this->address_str().c_str());
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Auto-Connect: %s", TRUEFALSE(this->auto_connect_));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool BLEClient::parse_device(const espbt::ESPBTDevice &device) {
 | 
			
		||||
@@ -37,31 +38,24 @@ bool BLEClient::parse_device(const espbt::ESPBTDevice &device) {
 | 
			
		||||
void BLEClient::set_enabled(bool enabled) {
 | 
			
		||||
  if (enabled == this->enabled)
 | 
			
		||||
    return;
 | 
			
		||||
  if (!enabled && this->state() != espbt::ClientState::IDLE) {
 | 
			
		||||
    ESP_LOGI(TAG, "[%s] Disabling BLE client.", this->address_str().c_str());
 | 
			
		||||
    auto ret = esp_ble_gattc_close(this->gattc_if_, this->conn_id_);
 | 
			
		||||
    if (ret) {
 | 
			
		||||
      ESP_LOGW(TAG, "esp_ble_gattc_close error, address=%s status=%d", this->address_str().c_str(), ret);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  this->enabled = enabled;
 | 
			
		||||
  if (!enabled) {
 | 
			
		||||
    ESP_LOGI(TAG, "[%s] Disabling BLE client.", this->address_str().c_str());
 | 
			
		||||
    this->disconnect();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t esp_gattc_if,
 | 
			
		||||
                                    esp_ble_gattc_cb_param_t *param) {
 | 
			
		||||
  bool all_established = this->all_nodes_established_();
 | 
			
		||||
 | 
			
		||||
  if (!BLEClientBase::gattc_event_handler(event, esp_gattc_if, param))
 | 
			
		||||
    return false;
 | 
			
		||||
 | 
			
		||||
  for (auto *node : this->nodes_)
 | 
			
		||||
    node->gattc_event_handler(event, esp_gattc_if, param);
 | 
			
		||||
 | 
			
		||||
  // Delete characteristics after clients have used them to save RAM.
 | 
			
		||||
  if (!all_established && this->all_nodes_established_()) {
 | 
			
		||||
    for (auto &svc : this->services_)
 | 
			
		||||
      delete svc;  // NOLINT(cppcoreguidelines-owning-memory)
 | 
			
		||||
    this->services_.clear();
 | 
			
		||||
  if (!this->services_.empty() && this->all_nodes_established_()) {
 | 
			
		||||
    this->release_services();
 | 
			
		||||
    ESP_LOGD(TAG, "All clients established, services released");
 | 
			
		||||
  }
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -19,26 +19,36 @@ void BLEBinaryOutput::dump_config() {
 | 
			
		||||
void BLEBinaryOutput::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
 | 
			
		||||
                                          esp_ble_gattc_cb_param_t *param) {
 | 
			
		||||
  switch (event) {
 | 
			
		||||
    case ESP_GATTC_OPEN_EVT:
 | 
			
		||||
      this->client_state_ = espbt::ClientState::ESTABLISHED;
 | 
			
		||||
      ESP_LOGW(TAG, "[%s] Connected successfully!", this->char_uuid_.to_string().c_str());
 | 
			
		||||
      break;
 | 
			
		||||
    case ESP_GATTC_DISCONNECT_EVT:
 | 
			
		||||
      ESP_LOGW(TAG, "[%s] Disconnected", this->char_uuid_.to_string().c_str());
 | 
			
		||||
      this->client_state_ = espbt::ClientState::IDLE;
 | 
			
		||||
      break;
 | 
			
		||||
    case ESP_GATTC_WRITE_CHAR_EVT: {
 | 
			
		||||
      if (param->write.status == 0) {
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
    case ESP_GATTC_SEARCH_CMPL_EVT: {
 | 
			
		||||
      auto *chr = this->parent()->get_characteristic(this->service_uuid_, this->char_uuid_);
 | 
			
		||||
      if (chr == nullptr) {
 | 
			
		||||
        ESP_LOGW(TAG, "[%s] Characteristic not found.", this->char_uuid_.to_string().c_str());
 | 
			
		||||
        ESP_LOGW(TAG, "Characteristic %s was not found in service %s", this->char_uuid_.to_string().c_str(),
 | 
			
		||||
                 this->service_uuid_.to_string().c_str());
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      if (param->write.handle == chr->handle) {
 | 
			
		||||
        ESP_LOGW(TAG, "[%s] Write error, status=%d", this->char_uuid_.to_string().c_str(), param->write.status);
 | 
			
		||||
      this->char_handle_ = chr->handle;
 | 
			
		||||
      this->char_props_ = chr->properties;
 | 
			
		||||
      if (this->require_response_ && this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE) {
 | 
			
		||||
        this->write_type_ = ESP_GATT_WRITE_TYPE_RSP;
 | 
			
		||||
        ESP_LOGD(TAG, "Write type: ESP_GATT_WRITE_TYPE_RSP");
 | 
			
		||||
      } else if (!this->require_response_ && this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE_NR) {
 | 
			
		||||
        this->write_type_ = ESP_GATT_WRITE_TYPE_NO_RSP;
 | 
			
		||||
        ESP_LOGD(TAG, "Write type: ESP_GATT_WRITE_TYPE_NO_RSP");
 | 
			
		||||
      } else {
 | 
			
		||||
        ESP_LOGE(TAG, "Characteristic %s does not allow writing with%s response", this->char_uuid_.to_string().c_str(),
 | 
			
		||||
                 this->require_response_ ? "" : "out");
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      this->node_state = espbt::ClientState::ESTABLISHED;
 | 
			
		||||
      ESP_LOGD(TAG, "Found characteristic %s on device %s", this->char_uuid_.to_string().c_str(),
 | 
			
		||||
               this->parent()->address_str().c_str());
 | 
			
		||||
      this->node_state = espbt::ClientState::ESTABLISHED;
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_WRITE_CHAR_EVT: {
 | 
			
		||||
      if (param->write.handle == this->char_handle_) {
 | 
			
		||||
        if (param->write.status != 0)
 | 
			
		||||
          ESP_LOGW(TAG, "[%s] Write error, status=%d", this->char_uuid_.to_string().c_str(), param->write.status);
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
@@ -48,26 +58,18 @@ void BLEBinaryOutput::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_i
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BLEBinaryOutput::write_state(bool state) {
 | 
			
		||||
  if (this->client_state_ != espbt::ClientState::ESTABLISHED) {
 | 
			
		||||
  if (this->node_state != espbt::ClientState::ESTABLISHED) {
 | 
			
		||||
    ESP_LOGW(TAG, "[%s] Not connected to BLE client.  State update can not be written.",
 | 
			
		||||
             this->char_uuid_.to_string().c_str());
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  auto *chr = this->parent()->get_characteristic(this->service_uuid_, this->char_uuid_);
 | 
			
		||||
  if (chr == nullptr) {
 | 
			
		||||
    ESP_LOGW(TAG, "[%s] Characteristic not found.  State update can not be written.",
 | 
			
		||||
             this->char_uuid_.to_string().c_str());
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  uint8_t state_as_uint = (uint8_t) state;
 | 
			
		||||
  ESP_LOGV(TAG, "[%s] Write State: %d", this->char_uuid_.to_string().c_str(), state_as_uint);
 | 
			
		||||
  if (this->require_response_) {
 | 
			
		||||
    chr->write_value(&state_as_uint, sizeof(state_as_uint), ESP_GATT_WRITE_TYPE_RSP);
 | 
			
		||||
  } else {
 | 
			
		||||
    chr->write_value(&state_as_uint, sizeof(state_as_uint), ESP_GATT_WRITE_TYPE_NO_RSP);
 | 
			
		||||
  }
 | 
			
		||||
  esp_err_t err =
 | 
			
		||||
      esp_ble_gattc_write_char(this->parent()->get_gattc_if(), this->parent()->get_conn_id(), this->char_handle_,
 | 
			
		||||
                               sizeof(state_as_uint), &state_as_uint, this->write_type_, ESP_GATT_AUTH_REQ_NONE);
 | 
			
		||||
  if (err != ESP_GATT_OK)
 | 
			
		||||
    ESP_LOGW(TAG, "[%s] Write error, err=%d", this->char_uuid_.to_string().c_str(), err);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace ble_client
 | 
			
		||||
 
 | 
			
		||||
@@ -32,7 +32,9 @@ class BLEBinaryOutput : public output::BinaryOutput, public BLEClientNode, publi
 | 
			
		||||
  bool require_response_;
 | 
			
		||||
  espbt::ESPBTUUID service_uuid_;
 | 
			
		||||
  espbt::ESPBTUUID char_uuid_;
 | 
			
		||||
  espbt::ClientState client_state_;
 | 
			
		||||
  uint16_t char_handle_{};
 | 
			
		||||
  esp_gatt_char_prop_t char_props_{};
 | 
			
		||||
  esp_gatt_write_type_t write_type_{};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace ble_client
 | 
			
		||||
 
 | 
			
		||||
@@ -14,15 +14,17 @@ class BLESensorNotifyTrigger : public Trigger<float>, public BLESensor {
 | 
			
		||||
  void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
 | 
			
		||||
                           esp_ble_gattc_cb_param_t *param) override {
 | 
			
		||||
    switch (event) {
 | 
			
		||||
      case ESP_GATTC_SEARCH_CMPL_EVT: {
 | 
			
		||||
        this->sensor_->node_state = espbt::ClientState::ESTABLISHED;
 | 
			
		||||
      case ESP_GATTC_NOTIFY_EVT: {
 | 
			
		||||
        if (param->notify.handle == this->sensor_->handle)
 | 
			
		||||
          this->trigger(this->sensor_->parent()->parse_char_value(param->notify.value, param->notify.value_len));
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      case ESP_GATTC_NOTIFY_EVT: {
 | 
			
		||||
        if (param->notify.conn_id != this->sensor_->parent()->get_conn_id() ||
 | 
			
		||||
            param->notify.handle != this->sensor_->handle)
 | 
			
		||||
          break;
 | 
			
		||||
        this->trigger(this->sensor_->parent()->parse_char_value(param->notify.value, param->notify.value_len));
 | 
			
		||||
      case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
 | 
			
		||||
        // confirms notifications are being listened for. While enabling of notifications may still be in
 | 
			
		||||
        // progress by the parent, we assume it will happen.
 | 
			
		||||
        if (param->reg_for_notify.status == ESP_GATT_OK && param->reg_for_notify.handle == this->sensor_->handle)
 | 
			
		||||
          this->node_state = espbt::ClientState::ESTABLISHED;
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      default:
 | 
			
		||||
        break;
 | 
			
		||||
 
 | 
			
		||||
@@ -22,26 +22,19 @@ void BLEClientRSSISensor::dump_config() {
 | 
			
		||||
void BLEClientRSSISensor::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
 | 
			
		||||
                                              esp_ble_gattc_cb_param_t *param) {
 | 
			
		||||
  switch (event) {
 | 
			
		||||
    case ESP_GATTC_OPEN_EVT: {
 | 
			
		||||
      if (param->open.status == ESP_GATT_OK) {
 | 
			
		||||
        ESP_LOGI(TAG, "[%s] Connected successfully!", this->get_name().c_str());
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_DISCONNECT_EVT: {
 | 
			
		||||
      ESP_LOGW(TAG, "[%s] Disconnected!", this->get_name().c_str());
 | 
			
		||||
    case ESP_GATTC_CLOSE_EVT: {
 | 
			
		||||
      this->status_set_warning();
 | 
			
		||||
      this->publish_state(NAN);
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_SEARCH_CMPL_EVT:
 | 
			
		||||
    case ESP_GATTC_SEARCH_CMPL_EVT: {
 | 
			
		||||
      this->node_state = espbt::ClientState::ESTABLISHED;
 | 
			
		||||
      if (this->should_update_) {
 | 
			
		||||
        this->should_update_ = false;
 | 
			
		||||
        this->get_rssi_();
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    default:
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -33,7 +33,7 @@ void BLESensor::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t ga
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_DISCONNECT_EVT: {
 | 
			
		||||
    case ESP_GATTC_CLOSE_EVT: {
 | 
			
		||||
      ESP_LOGW(TAG, "[%s] Disconnected!", this->get_name().c_str());
 | 
			
		||||
      this->status_set_warning();
 | 
			
		||||
      this->publish_state(NAN);
 | 
			
		||||
@@ -74,8 +74,6 @@ void BLESensor::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t ga
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_READ_CHAR_EVT: {
 | 
			
		||||
      if (param->read.conn_id != this->parent()->get_conn_id())
 | 
			
		||||
        break;
 | 
			
		||||
      if (param->read.status != ESP_GATT_OK) {
 | 
			
		||||
        ESP_LOGW(TAG, "Error reading char at handle %d, status=%d", param->read.handle, param->read.status);
 | 
			
		||||
        break;
 | 
			
		||||
@@ -87,15 +85,23 @@ void BLESensor::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t ga
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_NOTIFY_EVT: {
 | 
			
		||||
      if (param->notify.conn_id != this->parent()->get_conn_id() || param->notify.handle != this->handle)
 | 
			
		||||
        break;
 | 
			
		||||
      ESP_LOGV(TAG, "[%s] ESP_GATTC_NOTIFY_EVT: handle=0x%x, value=0x%x", this->get_name().c_str(),
 | 
			
		||||
      ESP_LOGD(TAG, "[%s] ESP_GATTC_NOTIFY_EVT: handle=0x%x, value=0x%x", this->get_name().c_str(),
 | 
			
		||||
               param->notify.handle, param->notify.value[0]);
 | 
			
		||||
      if (param->notify.handle != this->handle)
 | 
			
		||||
        break;
 | 
			
		||||
      this->publish_state(this->parse_data_(param->notify.value, param->notify.value_len));
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
 | 
			
		||||
      this->node_state = espbt::ClientState::ESTABLISHED;
 | 
			
		||||
      if (param->reg_for_notify.handle == this->handle) {
 | 
			
		||||
        if (param->reg_for_notify.status != ESP_GATT_OK) {
 | 
			
		||||
          ESP_LOGW(TAG, "Error registering for notifications at handle %d, status=%d", param->reg_for_notify.handle,
 | 
			
		||||
                   param->reg_for_notify.status);
 | 
			
		||||
          break;
 | 
			
		||||
        }
 | 
			
		||||
        this->node_state = espbt::ClientState::ESTABLISHED;
 | 
			
		||||
        ESP_LOGD(TAG, "Register for notify on %s complete", this->char_uuid_.to_string().c_str());
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    default:
 | 
			
		||||
 
 | 
			
		||||
@@ -17,14 +17,11 @@ void BLEClientSwitch::write_state(bool state) {
 | 
			
		||||
void BLEClientSwitch::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
 | 
			
		||||
                                          esp_ble_gattc_cb_param_t *param) {
 | 
			
		||||
  switch (event) {
 | 
			
		||||
    case ESP_GATTC_REG_EVT:
 | 
			
		||||
    case ESP_GATTC_CLOSE_EVT:
 | 
			
		||||
      this->publish_state(this->parent_->enabled);
 | 
			
		||||
      break;
 | 
			
		||||
    case ESP_GATTC_OPEN_EVT:
 | 
			
		||||
    case ESP_GATTC_SEARCH_CMPL_EVT:
 | 
			
		||||
      this->node_state = espbt::ClientState::ESTABLISHED;
 | 
			
		||||
      break;
 | 
			
		||||
    case ESP_GATTC_DISCONNECT_EVT:
 | 
			
		||||
      this->node_state = espbt::ClientState::IDLE;
 | 
			
		||||
      this->publish_state(this->parent_->enabled);
 | 
			
		||||
      break;
 | 
			
		||||
    default:
 | 
			
		||||
 
 | 
			
		||||
@@ -36,8 +36,7 @@ void BLETextSensor::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_DISCONNECT_EVT: {
 | 
			
		||||
      ESP_LOGW(TAG, "[%s] Disconnected!", this->get_name().c_str());
 | 
			
		||||
    case ESP_GATTC_CLOSE_EVT: {
 | 
			
		||||
      this->status_set_warning();
 | 
			
		||||
      this->publish_state(EMPTY);
 | 
			
		||||
      break;
 | 
			
		||||
@@ -77,20 +76,18 @@ void BLETextSensor::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_READ_CHAR_EVT: {
 | 
			
		||||
      if (param->read.conn_id != this->parent()->get_conn_id())
 | 
			
		||||
        break;
 | 
			
		||||
      if (param->read.status != ESP_GATT_OK) {
 | 
			
		||||
        ESP_LOGW(TAG, "Error reading char at handle %d, status=%d", param->read.handle, param->read.status);
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      if (param->read.handle == this->handle) {
 | 
			
		||||
        if (param->read.status != ESP_GATT_OK) {
 | 
			
		||||
          ESP_LOGW(TAG, "Error reading char at handle %d, status=%d", param->read.handle, param->read.status);
 | 
			
		||||
          break;
 | 
			
		||||
        }
 | 
			
		||||
        this->status_clear_warning();
 | 
			
		||||
        this->publish_state(this->parse_data(param->read.value, param->read.value_len));
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_NOTIFY_EVT: {
 | 
			
		||||
      if (param->notify.conn_id != this->parent()->get_conn_id() || param->notify.handle != this->handle)
 | 
			
		||||
      if (param->notify.handle != this->handle)
 | 
			
		||||
        break;
 | 
			
		||||
      ESP_LOGV(TAG, "[%s] ESP_GATTC_NOTIFY_EVT: handle=0x%x, value=0x%x", this->get_name().c_str(),
 | 
			
		||||
               param->notify.handle, param->notify.value[0]);
 | 
			
		||||
@@ -98,7 +95,8 @@ void BLETextSensor::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
 | 
			
		||||
      this->node_state = espbt::ClientState::ESTABLISHED;
 | 
			
		||||
      if (param->reg_for_notify.status == ESP_GATT_OK && param->reg_for_notify.handle == this->handle)
 | 
			
		||||
        this->node_state = espbt::ClientState::ESTABLISHED;
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    default:
 | 
			
		||||
 
 | 
			
		||||
@@ -1,116 +0,0 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.components import i2c, sensor
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_HUMIDITY,
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    CONF_IIR_FILTER,
 | 
			
		||||
    CONF_OVERSAMPLING,
 | 
			
		||||
    CONF_PRESSURE,
 | 
			
		||||
    CONF_TEMPERATURE,
 | 
			
		||||
    DEVICE_CLASS_HUMIDITY,
 | 
			
		||||
    DEVICE_CLASS_PRESSURE,
 | 
			
		||||
    DEVICE_CLASS_TEMPERATURE,
 | 
			
		||||
    STATE_CLASS_MEASUREMENT,
 | 
			
		||||
    UNIT_CELSIUS,
 | 
			
		||||
    UNIT_HECTOPASCAL,
 | 
			
		||||
    UNIT_PERCENT,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["i2c"]
 | 
			
		||||
 | 
			
		||||
bme280_ns = cg.esphome_ns.namespace("bme280")
 | 
			
		||||
BME280Oversampling = bme280_ns.enum("BME280Oversampling")
 | 
			
		||||
OVERSAMPLING_OPTIONS = {
 | 
			
		||||
    "NONE": BME280Oversampling.BME280_OVERSAMPLING_NONE,
 | 
			
		||||
    "1X": BME280Oversampling.BME280_OVERSAMPLING_1X,
 | 
			
		||||
    "2X": BME280Oversampling.BME280_OVERSAMPLING_2X,
 | 
			
		||||
    "4X": BME280Oversampling.BME280_OVERSAMPLING_4X,
 | 
			
		||||
    "8X": BME280Oversampling.BME280_OVERSAMPLING_8X,
 | 
			
		||||
    "16X": BME280Oversampling.BME280_OVERSAMPLING_16X,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
BME280IIRFilter = bme280_ns.enum("BME280IIRFilter")
 | 
			
		||||
IIR_FILTER_OPTIONS = {
 | 
			
		||||
    "OFF": BME280IIRFilter.BME280_IIR_FILTER_OFF,
 | 
			
		||||
    "2X": BME280IIRFilter.BME280_IIR_FILTER_2X,
 | 
			
		||||
    "4X": BME280IIRFilter.BME280_IIR_FILTER_4X,
 | 
			
		||||
    "8X": BME280IIRFilter.BME280_IIR_FILTER_8X,
 | 
			
		||||
    "16X": BME280IIRFilter.BME280_IIR_FILTER_16X,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
BME280Component = bme280_ns.class_(
 | 
			
		||||
    "BME280Component", cg.PollingComponent, i2c.I2CDevice
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = (
 | 
			
		||||
    cv.Schema(
 | 
			
		||||
        {
 | 
			
		||||
            cv.GenerateID(): cv.declare_id(BME280Component),
 | 
			
		||||
            cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
 | 
			
		||||
                unit_of_measurement=UNIT_CELSIUS,
 | 
			
		||||
                accuracy_decimals=1,
 | 
			
		||||
                device_class=DEVICE_CLASS_TEMPERATURE,
 | 
			
		||||
                state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
            ).extend(
 | 
			
		||||
                {
 | 
			
		||||
                    cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum(
 | 
			
		||||
                        OVERSAMPLING_OPTIONS, upper=True
 | 
			
		||||
                    ),
 | 
			
		||||
                }
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_PRESSURE): sensor.sensor_schema(
 | 
			
		||||
                unit_of_measurement=UNIT_HECTOPASCAL,
 | 
			
		||||
                accuracy_decimals=1,
 | 
			
		||||
                device_class=DEVICE_CLASS_PRESSURE,
 | 
			
		||||
                state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
            ).extend(
 | 
			
		||||
                {
 | 
			
		||||
                    cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum(
 | 
			
		||||
                        OVERSAMPLING_OPTIONS, upper=True
 | 
			
		||||
                    ),
 | 
			
		||||
                }
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(
 | 
			
		||||
                unit_of_measurement=UNIT_PERCENT,
 | 
			
		||||
                accuracy_decimals=1,
 | 
			
		||||
                device_class=DEVICE_CLASS_HUMIDITY,
 | 
			
		||||
                state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
            ).extend(
 | 
			
		||||
                {
 | 
			
		||||
                    cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum(
 | 
			
		||||
                        OVERSAMPLING_OPTIONS, upper=True
 | 
			
		||||
                    ),
 | 
			
		||||
                }
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_IIR_FILTER, default="OFF"): cv.enum(
 | 
			
		||||
                IIR_FILTER_OPTIONS, upper=True
 | 
			
		||||
            ),
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
    .extend(cv.polling_component_schema("60s"))
 | 
			
		||||
    .extend(i2c.i2c_device_schema(0x77))
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
    await i2c.register_i2c_device(var, config)
 | 
			
		||||
 | 
			
		||||
    if temperature_config := config.get(CONF_TEMPERATURE):
 | 
			
		||||
        sens = await sensor.new_sensor(temperature_config)
 | 
			
		||||
        cg.add(var.set_temperature_sensor(sens))
 | 
			
		||||
        cg.add(var.set_temperature_oversampling(temperature_config[CONF_OVERSAMPLING]))
 | 
			
		||||
 | 
			
		||||
    if pressure_config := config.get(CONF_PRESSURE):
 | 
			
		||||
        sens = await sensor.new_sensor(pressure_config)
 | 
			
		||||
        cg.add(var.set_pressure_sensor(sens))
 | 
			
		||||
        cg.add(var.set_pressure_oversampling(pressure_config[CONF_OVERSAMPLING]))
 | 
			
		||||
 | 
			
		||||
    if humidity_config := config.get(CONF_HUMIDITY):
 | 
			
		||||
        sens = await sensor.new_sensor(humidity_config)
 | 
			
		||||
        cg.add(var.set_humidity_sensor(sens))
 | 
			
		||||
        cg.add(var.set_humidity_oversampling(humidity_config[CONF_OVERSAMPLING]))
 | 
			
		||||
 | 
			
		||||
    cg.add(var.set_iir_filter(config[CONF_IIR_FILTER]))
 | 
			
		||||
							
								
								
									
										1
									
								
								esphome/components/bme280_base/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								esphome/components/bme280_base/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
CODEOWNERS = ["@esphome/core"]
 | 
			
		||||
@@ -1,9 +1,14 @@
 | 
			
		||||
#include "bme280.h"
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
 | 
			
		||||
#include "bme280_base.h"
 | 
			
		||||
#include "esphome/core/hal.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
#include <esphome/components/sensor/sensor.h>
 | 
			
		||||
#include <esphome/core/component.h>
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace bme280 {
 | 
			
		||||
namespace bme280_base {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "bme280.sensor";
 | 
			
		||||
 | 
			
		||||
@@ -46,7 +51,24 @@ static const uint8_t BME280_STATUS_IM_UPDATE = 0b01;
 | 
			
		||||
 | 
			
		||||
inline uint16_t combine_bytes(uint8_t msb, uint8_t lsb) { return ((msb & 0xFF) << 8) | (lsb & 0xFF); }
 | 
			
		||||
 | 
			
		||||
static const char *oversampling_to_str(BME280Oversampling oversampling) {
 | 
			
		||||
const char *iir_filter_to_str(BME280IIRFilter filter) {  // NOLINT
 | 
			
		||||
  switch (filter) {
 | 
			
		||||
    case BME280_IIR_FILTER_OFF:
 | 
			
		||||
      return "OFF";
 | 
			
		||||
    case BME280_IIR_FILTER_2X:
 | 
			
		||||
      return "2x";
 | 
			
		||||
    case BME280_IIR_FILTER_4X:
 | 
			
		||||
      return "4x";
 | 
			
		||||
    case BME280_IIR_FILTER_8X:
 | 
			
		||||
      return "8x";
 | 
			
		||||
    case BME280_IIR_FILTER_16X:
 | 
			
		||||
      return "16x";
 | 
			
		||||
    default:
 | 
			
		||||
      return "UNKNOWN";
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const char *oversampling_to_str(BME280Oversampling oversampling) {  // NOLINT
 | 
			
		||||
  switch (oversampling) {
 | 
			
		||||
    case BME280_OVERSAMPLING_NONE:
 | 
			
		||||
      return "None";
 | 
			
		||||
@@ -65,23 +87,6 @@ static const char *oversampling_to_str(BME280Oversampling oversampling) {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const char *iir_filter_to_str(BME280IIRFilter filter) {
 | 
			
		||||
  switch (filter) {
 | 
			
		||||
    case BME280_IIR_FILTER_OFF:
 | 
			
		||||
      return "OFF";
 | 
			
		||||
    case BME280_IIR_FILTER_2X:
 | 
			
		||||
      return "2x";
 | 
			
		||||
    case BME280_IIR_FILTER_4X:
 | 
			
		||||
      return "4x";
 | 
			
		||||
    case BME280_IIR_FILTER_8X:
 | 
			
		||||
      return "8x";
 | 
			
		||||
    case BME280_IIR_FILTER_16X:
 | 
			
		||||
      return "16x";
 | 
			
		||||
    default:
 | 
			
		||||
      return "UNKNOWN";
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BME280Component::setup() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Setting up BME280...");
 | 
			
		||||
  uint8_t chip_id = 0;
 | 
			
		||||
@@ -112,7 +117,7 @@ void BME280Component::setup() {
 | 
			
		||||
  // Wait until the NVM data has finished loading.
 | 
			
		||||
  uint8_t status;
 | 
			
		||||
  uint8_t retry = 5;
 | 
			
		||||
  do {
 | 
			
		||||
  do {  // NOLINT
 | 
			
		||||
    delay(2);
 | 
			
		||||
    if (!this->read_byte(BME280_REGISTER_STATUS, &status)) {
 | 
			
		||||
      ESP_LOGW(TAG, "Error reading status register.");
 | 
			
		||||
@@ -175,7 +180,6 @@ void BME280Component::setup() {
 | 
			
		||||
}
 | 
			
		||||
void BME280Component::dump_config() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "BME280:");
 | 
			
		||||
  LOG_I2C_DEVICE(this);
 | 
			
		||||
  switch (this->error_code_) {
 | 
			
		||||
    case COMMUNICATION_FAILED:
 | 
			
		||||
      ESP_LOGE(TAG, "Communication with BME280 failed!");
 | 
			
		||||
@@ -226,14 +230,14 @@ void BME280Component::update() {
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    int32_t t_fine = 0;
 | 
			
		||||
    float temperature = this->read_temperature_(data, &t_fine);
 | 
			
		||||
    float const temperature = this->read_temperature_(data, &t_fine);
 | 
			
		||||
    if (std::isnan(temperature)) {
 | 
			
		||||
      ESP_LOGW(TAG, "Invalid temperature, cannot read pressure & humidity values.");
 | 
			
		||||
      this->status_set_warning();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    float pressure = this->read_pressure_(data, t_fine);
 | 
			
		||||
    float humidity = this->read_humidity_(data, t_fine);
 | 
			
		||||
    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);
 | 
			
		||||
    if (this->temperature_sensor_ != nullptr)
 | 
			
		||||
@@ -257,12 +261,12 @@ float BME280Component::read_temperature_(const uint8_t *data, int32_t *t_fine) {
 | 
			
		||||
  const int32_t t2 = this->calibration_.t2;
 | 
			
		||||
  const int32_t t3 = this->calibration_.t3;
 | 
			
		||||
 | 
			
		||||
  int32_t var1 = (((adc >> 3) - (t1 << 1)) * t2) >> 11;
 | 
			
		||||
  int32_t var2 = (((((adc >> 4) - t1) * ((adc >> 4) - t1)) >> 12) * t3) >> 14;
 | 
			
		||||
  int32_t const var1 = (((adc >> 3) - (t1 << 1)) * t2) >> 11;
 | 
			
		||||
  int32_t const var2 = (((((adc >> 4) - t1) * ((adc >> 4) - t1)) >> 12) * t3) >> 14;
 | 
			
		||||
  *t_fine = var1 + var2;
 | 
			
		||||
 | 
			
		||||
  float temperature = (*t_fine * 5 + 128) >> 8;
 | 
			
		||||
  return temperature / 100.0f;
 | 
			
		||||
  float const temperature = (*t_fine * 5 + 128);
 | 
			
		||||
  return temperature / 25600.0f;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float BME280Component::read_pressure_(const uint8_t *data, int32_t t_fine) {
 | 
			
		||||
@@ -303,11 +307,11 @@ float BME280Component::read_pressure_(const uint8_t *data, int32_t t_fine) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float BME280Component::read_humidity_(const uint8_t *data, int32_t t_fine) {
 | 
			
		||||
  uint16_t raw_adc = ((data[6] & 0xFF) << 8) | (data[7] & 0xFF);
 | 
			
		||||
  uint16_t const raw_adc = ((data[6] & 0xFF) << 8) | (data[7] & 0xFF);
 | 
			
		||||
  if (raw_adc == 0x8000)
 | 
			
		||||
    return NAN;
 | 
			
		||||
 | 
			
		||||
  int32_t adc = raw_adc;
 | 
			
		||||
  int32_t const adc = raw_adc;
 | 
			
		||||
 | 
			
		||||
  const int32_t h1 = this->calibration_.h1;
 | 
			
		||||
  const int32_t h2 = this->calibration_.h2;
 | 
			
		||||
@@ -325,7 +329,7 @@ float BME280Component::read_humidity_(const uint8_t *data, int32_t t_fine) {
 | 
			
		||||
 | 
			
		||||
  v_x1_u32r = v_x1_u32r < 0 ? 0 : v_x1_u32r;
 | 
			
		||||
  v_x1_u32r = v_x1_u32r > 419430400 ? 419430400 : v_x1_u32r;
 | 
			
		||||
  float h = v_x1_u32r >> 12;
 | 
			
		||||
  float const h = v_x1_u32r >> 12;
 | 
			
		||||
 | 
			
		||||
  return h / 1024.0f;
 | 
			
		||||
}
 | 
			
		||||
@@ -351,5 +355,5 @@ uint16_t BME280Component::read_u16_le_(uint8_t a_register) {
 | 
			
		||||
}
 | 
			
		||||
int16_t BME280Component::read_s16_le_(uint8_t a_register) { return this->read_u16_le_(a_register); }
 | 
			
		||||
 | 
			
		||||
}  // namespace bme280
 | 
			
		||||
}  // namespace bme280_base
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -2,10 +2,9 @@
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/components/sensor/sensor.h"
 | 
			
		||||
#include "esphome/components/i2c/i2c.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace bme280 {
 | 
			
		||||
namespace bme280_base {
 | 
			
		||||
 | 
			
		||||
/// Internal struct storing the calibration values of an BME280.
 | 
			
		||||
struct BME280CalibrationData {
 | 
			
		||||
@@ -57,8 +56,8 @@ enum BME280IIRFilter {
 | 
			
		||||
  BME280_IIR_FILTER_16X = 0b100,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/// This class implements support for the BME280 Temperature+Pressure+Humidity i2c sensor.
 | 
			
		||||
class BME280Component : public PollingComponent, public i2c::I2CDevice {
 | 
			
		||||
/// This class implements support for the BME280 Temperature+Pressure+Humidity sensor.
 | 
			
		||||
class BME280Component : public PollingComponent {
 | 
			
		||||
 public:
 | 
			
		||||
  void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; }
 | 
			
		||||
  void set_pressure_sensor(sensor::Sensor *pressure_sensor) { pressure_sensor_ = pressure_sensor; }
 | 
			
		||||
@@ -91,6 +90,11 @@ class BME280Component : public PollingComponent, public i2c::I2CDevice {
 | 
			
		||||
  uint16_t read_u16_le_(uint8_t a_register);
 | 
			
		||||
  int16_t read_s16_le_(uint8_t a_register);
 | 
			
		||||
 | 
			
		||||
  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;
 | 
			
		||||
 | 
			
		||||
  BME280CalibrationData calibration_;
 | 
			
		||||
  BME280Oversampling temperature_oversampling_{BME280_OVERSAMPLING_16X};
 | 
			
		||||
  BME280Oversampling pressure_oversampling_{BME280_OVERSAMPLING_16X};
 | 
			
		||||
@@ -106,5 +110,5 @@ class BME280Component : public PollingComponent, public i2c::I2CDevice {
 | 
			
		||||
  } error_code_{NONE};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace bme280
 | 
			
		||||
}  // namespace bme280_base
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										106
									
								
								esphome/components/bme280_base/sensor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										106
									
								
								esphome/components/bme280_base/sensor.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,106 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.components import sensor
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_HUMIDITY,
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    CONF_IIR_FILTER,
 | 
			
		||||
    CONF_OVERSAMPLING,
 | 
			
		||||
    CONF_PRESSURE,
 | 
			
		||||
    CONF_TEMPERATURE,
 | 
			
		||||
    DEVICE_CLASS_HUMIDITY,
 | 
			
		||||
    DEVICE_CLASS_PRESSURE,
 | 
			
		||||
    DEVICE_CLASS_TEMPERATURE,
 | 
			
		||||
    STATE_CLASS_MEASUREMENT,
 | 
			
		||||
    UNIT_CELSIUS,
 | 
			
		||||
    UNIT_HECTOPASCAL,
 | 
			
		||||
    UNIT_PERCENT,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
bme280_ns = cg.esphome_ns.namespace("bme280_base")
 | 
			
		||||
BME280Oversampling = bme280_ns.enum("BME280Oversampling")
 | 
			
		||||
OVERSAMPLING_OPTIONS = {
 | 
			
		||||
    "NONE": BME280Oversampling.BME280_OVERSAMPLING_NONE,
 | 
			
		||||
    "1X": BME280Oversampling.BME280_OVERSAMPLING_1X,
 | 
			
		||||
    "2X": BME280Oversampling.BME280_OVERSAMPLING_2X,
 | 
			
		||||
    "4X": BME280Oversampling.BME280_OVERSAMPLING_4X,
 | 
			
		||||
    "8X": BME280Oversampling.BME280_OVERSAMPLING_8X,
 | 
			
		||||
    "16X": BME280Oversampling.BME280_OVERSAMPLING_16X,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
BME280IIRFilter = bme280_ns.enum("BME280IIRFilter")
 | 
			
		||||
IIR_FILTER_OPTIONS = {
 | 
			
		||||
    "OFF": BME280IIRFilter.BME280_IIR_FILTER_OFF,
 | 
			
		||||
    "2X": BME280IIRFilter.BME280_IIR_FILTER_2X,
 | 
			
		||||
    "4X": BME280IIRFilter.BME280_IIR_FILTER_4X,
 | 
			
		||||
    "8X": BME280IIRFilter.BME280_IIR_FILTER_8X,
 | 
			
		||||
    "16X": BME280IIRFilter.BME280_IIR_FILTER_16X,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA_BASE = cv.Schema(
 | 
			
		||||
    {
 | 
			
		||||
        cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
 | 
			
		||||
            unit_of_measurement=UNIT_CELSIUS,
 | 
			
		||||
            accuracy_decimals=1,
 | 
			
		||||
            device_class=DEVICE_CLASS_TEMPERATURE,
 | 
			
		||||
            state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
        ).extend(
 | 
			
		||||
            {
 | 
			
		||||
                cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum(
 | 
			
		||||
                    OVERSAMPLING_OPTIONS, upper=True
 | 
			
		||||
                ),
 | 
			
		||||
            }
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_PRESSURE): sensor.sensor_schema(
 | 
			
		||||
            unit_of_measurement=UNIT_HECTOPASCAL,
 | 
			
		||||
            accuracy_decimals=1,
 | 
			
		||||
            device_class=DEVICE_CLASS_PRESSURE,
 | 
			
		||||
            state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
        ).extend(
 | 
			
		||||
            {
 | 
			
		||||
                cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum(
 | 
			
		||||
                    OVERSAMPLING_OPTIONS, upper=True
 | 
			
		||||
                ),
 | 
			
		||||
            }
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_HUMIDITY): sensor.sensor_schema(
 | 
			
		||||
            unit_of_measurement=UNIT_PERCENT,
 | 
			
		||||
            accuracy_decimals=1,
 | 
			
		||||
            device_class=DEVICE_CLASS_HUMIDITY,
 | 
			
		||||
            state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
        ).extend(
 | 
			
		||||
            {
 | 
			
		||||
                cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum(
 | 
			
		||||
                    OVERSAMPLING_OPTIONS, upper=True
 | 
			
		||||
                ),
 | 
			
		||||
            }
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_IIR_FILTER, default="OFF"): cv.enum(
 | 
			
		||||
            IIR_FILTER_OPTIONS, upper=True
 | 
			
		||||
        ),
 | 
			
		||||
    }
 | 
			
		||||
).extend(cv.polling_component_schema("60s"))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config, func=None):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
    if func is not None:
 | 
			
		||||
        await func(var, config)
 | 
			
		||||
 | 
			
		||||
    if temperature_config := config.get(CONF_TEMPERATURE):
 | 
			
		||||
        sens = await sensor.new_sensor(temperature_config)
 | 
			
		||||
        cg.add(var.set_temperature_sensor(sens))
 | 
			
		||||
        cg.add(var.set_temperature_oversampling(temperature_config[CONF_OVERSAMPLING]))
 | 
			
		||||
 | 
			
		||||
    if pressure_config := config.get(CONF_PRESSURE):
 | 
			
		||||
        sens = await sensor.new_sensor(pressure_config)
 | 
			
		||||
        cg.add(var.set_pressure_sensor(sens))
 | 
			
		||||
        cg.add(var.set_pressure_oversampling(pressure_config[CONF_OVERSAMPLING]))
 | 
			
		||||
 | 
			
		||||
    if humidity_config := config.get(CONF_HUMIDITY):
 | 
			
		||||
        sens = await sensor.new_sensor(humidity_config)
 | 
			
		||||
        cg.add(var.set_humidity_sensor(sens))
 | 
			
		||||
        cg.add(var.set_humidity_oversampling(humidity_config[CONF_OVERSAMPLING]))
 | 
			
		||||
 | 
			
		||||
    cg.add(var.set_iir_filter(config[CONF_IIR_FILTER]))
 | 
			
		||||
							
								
								
									
										30
									
								
								esphome/components/bme280_i2c/bme280_i2c.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								esphome/components/bme280_i2c/bme280_i2c.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
#include <cstddef>
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
 | 
			
		||||
#include "bme280_i2c.h"
 | 
			
		||||
#include "esphome/components/i2c/i2c.h"
 | 
			
		||||
#include "../bme280_base/bme280_base.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace bme280_i2c {
 | 
			
		||||
 | 
			
		||||
bool BME280I2CComponent::read_byte(uint8_t a_register, uint8_t *data) {
 | 
			
		||||
  return I2CDevice::read_byte(a_register, data);
 | 
			
		||||
};
 | 
			
		||||
bool BME280I2CComponent::write_byte(uint8_t a_register, uint8_t data) {
 | 
			
		||||
  return I2CDevice::write_byte(a_register, data);
 | 
			
		||||
};
 | 
			
		||||
bool BME280I2CComponent::read_bytes(uint8_t a_register, uint8_t *data, size_t len) {
 | 
			
		||||
  return I2CDevice::read_bytes(a_register, data, len);
 | 
			
		||||
};
 | 
			
		||||
bool BME280I2CComponent::read_byte_16(uint8_t a_register, uint16_t *data) {
 | 
			
		||||
  return I2CDevice::read_byte_16(a_register, data);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
void BME280I2CComponent::dump_config() {
 | 
			
		||||
  LOG_I2C_DEVICE(this);
 | 
			
		||||
  BME280Component::dump_config();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace bme280_i2c
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										20
									
								
								esphome/components/bme280_i2c/bme280_i2c.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								esphome/components/bme280_i2c/bme280_i2c.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/bme280_base/bme280_base.h"
 | 
			
		||||
#include "esphome/components/i2c/i2c.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace bme280_i2c {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "bme280_i2c.sensor";
 | 
			
		||||
 | 
			
		||||
class BME280I2CComponent : public esphome::bme280_base::BME280Component, public i2c::I2CDevice {
 | 
			
		||||
  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;
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace bme280_i2c
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										19
									
								
								esphome/components/bme280_i2c/sensor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								esphome/components/bme280_i2c/sensor.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
from esphome.components import i2c
 | 
			
		||||
from ..bme280_base.sensor import to_code as to_code_base, cv, CONFIG_SCHEMA_BASE
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["i2c"]
 | 
			
		||||
AUTO_LOAD = ["bme280_base"]
 | 
			
		||||
 | 
			
		||||
bme280_ns = cg.esphome_ns.namespace("bme280_i2c")
 | 
			
		||||
BME280I2CComponent = bme280_ns.class_(
 | 
			
		||||
    "BME280I2CComponent", cg.PollingComponent, i2c.I2CDevice
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = CONFIG_SCHEMA_BASE.extend(
 | 
			
		||||
    i2c.i2c_device_schema(default_address=0x77)
 | 
			
		||||
).extend({cv.GenerateID(): cv.declare_id(BME280I2CComponent)})
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    await to_code_base(config, func=i2c.register_i2c_device)
 | 
			
		||||
							
								
								
									
										1
									
								
								esphome/components/bme280_spi/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								esphome/components/bme280_spi/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
CODEOWNERS = ["@apbodrov"]
 | 
			
		||||
							
								
								
									
										66
									
								
								esphome/components/bme280_spi/bme280_spi.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								esphome/components/bme280_spi/bme280_spi.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,66 @@
 | 
			
		||||
#include <cstdint>
 | 
			
		||||
#include <cstddef>
 | 
			
		||||
 | 
			
		||||
#include "bme280_spi.h"
 | 
			
		||||
#include <esphome/components/bme280_base/bme280_base.h>
 | 
			
		||||
 | 
			
		||||
int set_bit(uint8_t num, int position) {
 | 
			
		||||
  int mask = 1 << position;
 | 
			
		||||
  return num | mask;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int clear_bit(uint8_t num, int position) {
 | 
			
		||||
  int mask = 1 << position;
 | 
			
		||||
  return num & ~mask;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace bme280_spi {
 | 
			
		||||
 | 
			
		||||
void BME280SPIComponent::setup() {
 | 
			
		||||
  this->spi_setup();
 | 
			
		||||
  BME280Component::setup();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// In SPI mode, only 7 bits of the register addresses are used; the MSB of register address is not used
 | 
			
		||||
// and replaced by a read/write bit (RW = ‘0’ for write and RW = ‘1’ for read).
 | 
			
		||||
// Example: address 0xF7 is accessed by using SPI register address 0x77. For write access, the byte
 | 
			
		||||
// 0x77 is transferred, for read access, the byte 0xF7 is transferred.
 | 
			
		||||
// https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf
 | 
			
		||||
 | 
			
		||||
bool BME280SPIComponent::read_byte(uint8_t a_register, uint8_t *data) {
 | 
			
		||||
  this->enable();
 | 
			
		||||
  // cause: *data = this->delegate_->transfer(tmp) doesnt work
 | 
			
		||||
  this->delegate_->transfer(set_bit(a_register, 7));
 | 
			
		||||
  *data = this->delegate_->transfer(0);
 | 
			
		||||
  this->disable();
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool BME280SPIComponent::write_byte(uint8_t a_register, uint8_t data) {
 | 
			
		||||
  this->enable();
 | 
			
		||||
  this->delegate_->transfer(clear_bit(a_register, 7));
 | 
			
		||||
  this->delegate_->transfer(data);
 | 
			
		||||
  this->disable();
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool BME280SPIComponent::read_bytes(uint8_t a_register, uint8_t *data, size_t len) {
 | 
			
		||||
  this->enable();
 | 
			
		||||
  this->delegate_->transfer(set_bit(a_register, 7));
 | 
			
		||||
  this->delegate_->read_array(data, len);
 | 
			
		||||
  this->disable();
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool BME280SPIComponent::read_byte_16(uint8_t a_register, uint16_t *data) {
 | 
			
		||||
  this->enable();
 | 
			
		||||
  this->delegate_->transfer(set_bit(a_register, 7));
 | 
			
		||||
  ((uint8_t *) data)[1] = this->delegate_->transfer(0);
 | 
			
		||||
  ((uint8_t *) data)[0] = this->delegate_->transfer(0);
 | 
			
		||||
  this->disable();
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace bme280_spi
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										20
									
								
								esphome/components/bme280_spi/bme280_spi.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								esphome/components/bme280_spi/bme280_spi.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/bme280_base/bme280_base.h"
 | 
			
		||||
#include "esphome/components/spi/spi.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace bme280_spi {
 | 
			
		||||
 | 
			
		||||
class BME280SPIComponent : public esphome::bme280_base::BME280Component,
 | 
			
		||||
                           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;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace bme280_spi
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										24
									
								
								esphome/components/bme280_spi/sensor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								esphome/components/bme280_spi/sensor.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
from esphome.components import spi
 | 
			
		||||
from esphome.components.bme280_base.sensor import (
 | 
			
		||||
    to_code as to_code_base,
 | 
			
		||||
    cv,
 | 
			
		||||
    CONFIG_SCHEMA_BASE,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["spi"]
 | 
			
		||||
AUTO_LOAD = ["bme280_base"]
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
bme280_spi_ns = cg.esphome_ns.namespace("bme280_spi")
 | 
			
		||||
BME280SPIComponent = bme280_spi_ns.class_(
 | 
			
		||||
    "BME280SPIComponent", cg.PollingComponent, spi.SPIDevice
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = CONFIG_SCHEMA_BASE.extend(spi.spi_device_schema()).extend(
 | 
			
		||||
    {cv.GenerateID(): cv.declare_id(BME280SPIComponent)}
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    await to_code_base(config, func=spi.register_spi_device)
 | 
			
		||||
@@ -200,8 +200,8 @@ float BMP280Component::read_temperature_(int32_t *t_fine) {
 | 
			
		||||
  int32_t var2 = (((((adc >> 4) - t1) * ((adc >> 4) - t1)) >> 12) * t3) >> 14;
 | 
			
		||||
  *t_fine = var1 + var2;
 | 
			
		||||
 | 
			
		||||
  float temperature = (*t_fine * 5 + 128) >> 8;
 | 
			
		||||
  return temperature / 100.0f;
 | 
			
		||||
  float temperature = (*t_fine * 5 + 128);
 | 
			
		||||
  return temperature / 25600.0f;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float BMP280Component::read_pressure_(int32_t t_fine) {
 | 
			
		||||
 
 | 
			
		||||
@@ -90,40 +90,41 @@ void BP1658CJ::set_channel_value_(uint8_t channel, uint16_t value) {
 | 
			
		||||
 | 
			
		||||
void BP1658CJ::write_bit_(bool value) {
 | 
			
		||||
  this->data_pin_->digital_write(value);
 | 
			
		||||
  this->clock_pin_->digital_write(true);
 | 
			
		||||
 | 
			
		||||
  delayMicroseconds(BP1658CJ_DELAY);
 | 
			
		||||
 | 
			
		||||
  this->clock_pin_->digital_write(true);
 | 
			
		||||
  delayMicroseconds(BP1658CJ_DELAY);
 | 
			
		||||
  this->clock_pin_->digital_write(false);
 | 
			
		||||
  delayMicroseconds(BP1658CJ_DELAY);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BP1658CJ::write_byte_(uint8_t data) {
 | 
			
		||||
  for (uint8_t mask = 0x80; mask; mask >>= 1) {
 | 
			
		||||
    this->write_bit_(data & mask);
 | 
			
		||||
    delayMicroseconds(BP1658CJ_DELAY);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // ack bit
 | 
			
		||||
  this->data_pin_->pin_mode(gpio::FLAG_INPUT);
 | 
			
		||||
  this->clock_pin_->digital_write(true);
 | 
			
		||||
 | 
			
		||||
  delayMicroseconds(BP1658CJ_DELAY);
 | 
			
		||||
 | 
			
		||||
  this->clock_pin_->digital_write(false);
 | 
			
		||||
  delayMicroseconds(BP1658CJ_DELAY);
 | 
			
		||||
  this->data_pin_->pin_mode(gpio::FLAG_OUTPUT);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BP1658CJ::write_buffer_(uint8_t *buffer, uint8_t size) {
 | 
			
		||||
  this->data_pin_->digital_write(false);
 | 
			
		||||
  delayMicroseconds(BP1658CJ_DELAY);
 | 
			
		||||
  this->clock_pin_->digital_write(false);
 | 
			
		||||
  delayMicroseconds(BP1658CJ_DELAY);
 | 
			
		||||
 | 
			
		||||
  for (uint32_t i = 0; i < size; i++) {
 | 
			
		||||
    this->write_byte_(buffer[i]);
 | 
			
		||||
    delayMicroseconds(BP1658CJ_DELAY);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->clock_pin_->digital_write(true);
 | 
			
		||||
  delayMicroseconds(BP1658CJ_DELAY);
 | 
			
		||||
  this->data_pin_->digital_write(true);
 | 
			
		||||
  delayMicroseconds(BP1658CJ_DELAY);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace bp1658cj
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,7 @@ from esphome.const import (
 | 
			
		||||
    CONF_AWAY,
 | 
			
		||||
    CONF_AWAY_COMMAND_TOPIC,
 | 
			
		||||
    CONF_AWAY_STATE_TOPIC,
 | 
			
		||||
    CONF_CURRENT_HUMIDITY_STATE_TOPIC,
 | 
			
		||||
    CONF_CURRENT_TEMPERATURE_STATE_TOPIC,
 | 
			
		||||
    CONF_CUSTOM_FAN_MODE,
 | 
			
		||||
    CONF_CUSTOM_PRESET,
 | 
			
		||||
@@ -28,6 +29,8 @@ from esphome.const import (
 | 
			
		||||
    CONF_SWING_MODE,
 | 
			
		||||
    CONF_SWING_MODE_COMMAND_TOPIC,
 | 
			
		||||
    CONF_SWING_MODE_STATE_TOPIC,
 | 
			
		||||
    CONF_TARGET_HUMIDITY_COMMAND_TOPIC,
 | 
			
		||||
    CONF_TARGET_HUMIDITY_STATE_TOPIC,
 | 
			
		||||
    CONF_TARGET_TEMPERATURE,
 | 
			
		||||
    CONF_TARGET_TEMPERATURE_COMMAND_TOPIC,
 | 
			
		||||
    CONF_TARGET_TEMPERATURE_STATE_TOPIC,
 | 
			
		||||
@@ -106,6 +109,9 @@ CLIMATE_SWING_MODES = {
 | 
			
		||||
validate_climate_swing_mode = cv.enum(CLIMATE_SWING_MODES, upper=True)
 | 
			
		||||
 | 
			
		||||
CONF_CURRENT_TEMPERATURE = "current_temperature"
 | 
			
		||||
CONF_MIN_HUMIDITY = "min_humidity"
 | 
			
		||||
CONF_MAX_HUMIDITY = "max_humidity"
 | 
			
		||||
CONF_TARGET_HUMIDITY = "target_humidity"
 | 
			
		||||
 | 
			
		||||
visual_temperature = cv.float_with_unit(
 | 
			
		||||
    "visual_temperature", "(°C|° C|°|C|° K|° K|K|°F|° F|F)?"
 | 
			
		||||
@@ -153,6 +159,8 @@ CLIMATE_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).
 | 
			
		||||
                cv.Optional(CONF_MIN_TEMPERATURE): cv.temperature,
 | 
			
		||||
                cv.Optional(CONF_MAX_TEMPERATURE): cv.temperature,
 | 
			
		||||
                cv.Optional(CONF_TEMPERATURE_STEP): VISUAL_TEMPERATURE_STEP_SCHEMA,
 | 
			
		||||
                cv.Optional(CONF_MIN_HUMIDITY): cv.percentage_int,
 | 
			
		||||
                cv.Optional(CONF_MAX_HUMIDITY): cv.percentage_int,
 | 
			
		||||
            }
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_ACTION_STATE_TOPIC): cv.All(
 | 
			
		||||
@@ -167,6 +175,9 @@ CLIMATE_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).
 | 
			
		||||
        cv.Optional(CONF_CURRENT_TEMPERATURE_STATE_TOPIC): cv.All(
 | 
			
		||||
            cv.requires_component("mqtt"), cv.publish_topic
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_CURRENT_HUMIDITY_STATE_TOPIC): cv.All(
 | 
			
		||||
            cv.requires_component("mqtt"), cv.publish_topic
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_FAN_MODE_COMMAND_TOPIC): cv.All(
 | 
			
		||||
            cv.requires_component("mqtt"), cv.publish_topic
 | 
			
		||||
        ),
 | 
			
		||||
@@ -209,6 +220,12 @@ CLIMATE_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMMAND_COMPONENT_SCHEMA).
 | 
			
		||||
        cv.Optional(CONF_TARGET_TEMPERATURE_LOW_STATE_TOPIC): cv.All(
 | 
			
		||||
            cv.requires_component("mqtt"), cv.publish_topic
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_TARGET_HUMIDITY_COMMAND_TOPIC): cv.All(
 | 
			
		||||
            cv.requires_component("mqtt"), cv.publish_topic
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_TARGET_HUMIDITY_STATE_TOPIC): cv.All(
 | 
			
		||||
            cv.requires_component("mqtt"), cv.publish_topic
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_ON_CONTROL): automation.validate_automation(
 | 
			
		||||
            {
 | 
			
		||||
                cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ControlTrigger),
 | 
			
		||||
@@ -238,6 +255,10 @@ async def setup_climate_core_(var, config):
 | 
			
		||||
                visual[CONF_TEMPERATURE_STEP][CONF_CURRENT_TEMPERATURE],
 | 
			
		||||
            )
 | 
			
		||||
        )
 | 
			
		||||
    if CONF_MIN_HUMIDITY in visual:
 | 
			
		||||
        cg.add(var.set_visual_min_humidity_override(visual[CONF_MIN_HUMIDITY]))
 | 
			
		||||
    if CONF_MAX_HUMIDITY in visual:
 | 
			
		||||
        cg.add(var.set_visual_max_humidity_override(visual[CONF_MAX_HUMIDITY]))
 | 
			
		||||
 | 
			
		||||
    if CONF_MQTT_ID in config:
 | 
			
		||||
        mqtt_ = cg.new_Pvariable(config[CONF_MQTT_ID], var)
 | 
			
		||||
@@ -255,6 +276,12 @@ async def setup_climate_core_(var, config):
 | 
			
		||||
                    config[CONF_CURRENT_TEMPERATURE_STATE_TOPIC]
 | 
			
		||||
                )
 | 
			
		||||
            )
 | 
			
		||||
        if CONF_CURRENT_HUMIDITY_STATE_TOPIC in config:
 | 
			
		||||
            cg.add(
 | 
			
		||||
                mqtt_.set_custom_current_humidity_state_topic(
 | 
			
		||||
                    config[CONF_CURRENT_HUMIDITY_STATE_TOPIC]
 | 
			
		||||
                )
 | 
			
		||||
            )
 | 
			
		||||
        if CONF_FAN_MODE_COMMAND_TOPIC in config:
 | 
			
		||||
            cg.add(
 | 
			
		||||
                mqtt_.set_custom_fan_mode_command_topic(
 | 
			
		||||
@@ -323,6 +350,18 @@ async def setup_climate_core_(var, config):
 | 
			
		||||
                    config[CONF_TARGET_TEMPERATURE_LOW_STATE_TOPIC]
 | 
			
		||||
                )
 | 
			
		||||
            )
 | 
			
		||||
        if CONF_TARGET_HUMIDITY_COMMAND_TOPIC in config:
 | 
			
		||||
            cg.add(
 | 
			
		||||
                mqtt_.set_custom_target_humidity_command_topic(
 | 
			
		||||
                    config[CONF_TARGET_HUMIDITY_COMMAND_TOPIC]
 | 
			
		||||
                )
 | 
			
		||||
            )
 | 
			
		||||
        if CONF_TARGET_HUMIDITY_STATE_TOPIC in config:
 | 
			
		||||
            cg.add(
 | 
			
		||||
                mqtt_.set_custom_target_humidity_state_topic(
 | 
			
		||||
                    config[CONF_TARGET_HUMIDITY_STATE_TOPIC]
 | 
			
		||||
                )
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
    for conf in config.get(CONF_ON_STATE, []):
 | 
			
		||||
        trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
 | 
			
		||||
@@ -351,6 +390,7 @@ CLIMATE_CONTROL_ACTION_SCHEMA = cv.Schema(
 | 
			
		||||
        cv.Optional(CONF_TARGET_TEMPERATURE): cv.templatable(cv.temperature),
 | 
			
		||||
        cv.Optional(CONF_TARGET_TEMPERATURE_LOW): cv.templatable(cv.temperature),
 | 
			
		||||
        cv.Optional(CONF_TARGET_TEMPERATURE_HIGH): cv.templatable(cv.temperature),
 | 
			
		||||
        cv.Optional(CONF_TARGET_HUMIDITY): cv.templatable(cv.percentage_int),
 | 
			
		||||
        cv.Optional(CONF_AWAY): cv.invalid("Use preset instead"),
 | 
			
		||||
        cv.Exclusive(CONF_FAN_MODE, "fan_mode"): cv.templatable(
 | 
			
		||||
            validate_climate_fan_mode
 | 
			
		||||
@@ -387,6 +427,9 @@ async def climate_control_to_code(config, action_id, template_arg, args):
 | 
			
		||||
            config[CONF_TARGET_TEMPERATURE_HIGH], args, float
 | 
			
		||||
        )
 | 
			
		||||
        cg.add(var.set_target_temperature_high(template_))
 | 
			
		||||
    if CONF_TARGET_HUMIDITY in config:
 | 
			
		||||
        template_ = await cg.templatable(config[CONF_TARGET_HUMIDITY], args, float)
 | 
			
		||||
        cg.add(var.set_target_humidity(template_))
 | 
			
		||||
    if CONF_FAN_MODE in config:
 | 
			
		||||
        template_ = await cg.templatable(config[CONF_FAN_MODE], args, ClimateFanMode)
 | 
			
		||||
        cg.add(var.set_fan_mode(template_))
 | 
			
		||||
 
 | 
			
		||||
@@ -14,6 +14,7 @@ template<typename... Ts> class ControlAction : public Action<Ts...> {
 | 
			
		||||
  TEMPLATABLE_VALUE(float, target_temperature)
 | 
			
		||||
  TEMPLATABLE_VALUE(float, target_temperature_low)
 | 
			
		||||
  TEMPLATABLE_VALUE(float, target_temperature_high)
 | 
			
		||||
  TEMPLATABLE_VALUE(float, target_humidity)
 | 
			
		||||
  TEMPLATABLE_VALUE(bool, away)
 | 
			
		||||
  TEMPLATABLE_VALUE(ClimateFanMode, fan_mode)
 | 
			
		||||
  TEMPLATABLE_VALUE(std::string, custom_fan_mode)
 | 
			
		||||
@@ -27,6 +28,7 @@ template<typename... Ts> class ControlAction : public Action<Ts...> {
 | 
			
		||||
    call.set_target_temperature(this->target_temperature_.optional_value(x...));
 | 
			
		||||
    call.set_target_temperature_low(this->target_temperature_low_.optional_value(x...));
 | 
			
		||||
    call.set_target_temperature_high(this->target_temperature_high_.optional_value(x...));
 | 
			
		||||
    call.set_target_humidity(this->target_humidity_.optional_value(x...));
 | 
			
		||||
    if (away_.has_value()) {
 | 
			
		||||
      call.set_preset(away_.value(x...) ? CLIMATE_PRESET_AWAY : CLIMATE_PRESET_HOME);
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -45,6 +45,9 @@ void ClimateCall::perform() {
 | 
			
		||||
  if (this->target_temperature_high_.has_value()) {
 | 
			
		||||
    ESP_LOGD(TAG, "  Target Temperature High: %.2f", *this->target_temperature_high_);
 | 
			
		||||
  }
 | 
			
		||||
  if (this->target_humidity_.has_value()) {
 | 
			
		||||
    ESP_LOGD(TAG, "  Target Humidity: %.0f", *this->target_humidity_);
 | 
			
		||||
  }
 | 
			
		||||
  this->parent_->control(*this);
 | 
			
		||||
}
 | 
			
		||||
void ClimateCall::validate_() {
 | 
			
		||||
@@ -262,10 +265,16 @@ ClimateCall &ClimateCall::set_target_temperature_high(float target_temperature_h
 | 
			
		||||
  this->target_temperature_high_ = target_temperature_high;
 | 
			
		||||
  return *this;
 | 
			
		||||
}
 | 
			
		||||
ClimateCall &ClimateCall::set_target_humidity(float target_humidity) {
 | 
			
		||||
  this->target_humidity_ = target_humidity;
 | 
			
		||||
  return *this;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const optional<ClimateMode> &ClimateCall::get_mode() const { return this->mode_; }
 | 
			
		||||
const optional<float> &ClimateCall::get_target_temperature() const { return this->target_temperature_; }
 | 
			
		||||
const optional<float> &ClimateCall::get_target_temperature_low() const { return this->target_temperature_low_; }
 | 
			
		||||
const optional<float> &ClimateCall::get_target_temperature_high() const { return this->target_temperature_high_; }
 | 
			
		||||
const optional<float> &ClimateCall::get_target_humidity() const { return this->target_humidity_; }
 | 
			
		||||
const optional<ClimateFanMode> &ClimateCall::get_fan_mode() const { return this->fan_mode_; }
 | 
			
		||||
const optional<std::string> &ClimateCall::get_custom_fan_mode() const { return this->custom_fan_mode_; }
 | 
			
		||||
const optional<ClimatePreset> &ClimateCall::get_preset() const { return this->preset_; }
 | 
			
		||||
@@ -283,6 +292,10 @@ ClimateCall &ClimateCall::set_target_temperature(optional<float> target_temperat
 | 
			
		||||
  this->target_temperature_ = target_temperature;
 | 
			
		||||
  return *this;
 | 
			
		||||
}
 | 
			
		||||
ClimateCall &ClimateCall::set_target_humidity(optional<float> target_humidity) {
 | 
			
		||||
  this->target_humidity_ = target_humidity;
 | 
			
		||||
  return *this;
 | 
			
		||||
}
 | 
			
		||||
ClimateCall &ClimateCall::set_mode(optional<ClimateMode> mode) {
 | 
			
		||||
  this->mode_ = mode;
 | 
			
		||||
  return *this;
 | 
			
		||||
@@ -343,6 +356,9 @@ void Climate::save_state_() {
 | 
			
		||||
  } else {
 | 
			
		||||
    state.target_temperature = this->target_temperature;
 | 
			
		||||
  }
 | 
			
		||||
  if (traits.get_supports_target_humidity()) {
 | 
			
		||||
    state.target_humidity = this->target_humidity;
 | 
			
		||||
  }
 | 
			
		||||
  if (traits.get_supports_fan_modes() && fan_mode.has_value()) {
 | 
			
		||||
    state.uses_custom_fan_mode = false;
 | 
			
		||||
    state.fan_mode = this->fan_mode.value();
 | 
			
		||||
@@ -408,6 +424,12 @@ void Climate::publish_state() {
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGD(TAG, "  Target Temperature: %.2f°C", this->target_temperature);
 | 
			
		||||
  }
 | 
			
		||||
  if (traits.get_supports_current_humidity()) {
 | 
			
		||||
    ESP_LOGD(TAG, "  Current Humidity: %.0f%%", this->current_humidity);
 | 
			
		||||
  }
 | 
			
		||||
  if (traits.get_supports_target_humidity()) {
 | 
			
		||||
    ESP_LOGD(TAG, "  Target Humidity: %.0f%%", this->target_humidity);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Send state to frontend
 | 
			
		||||
  this->state_callback_.call(*this);
 | 
			
		||||
@@ -427,6 +449,12 @@ ClimateTraits Climate::get_traits() {
 | 
			
		||||
    traits.set_visual_target_temperature_step(*this->visual_target_temperature_step_override_);
 | 
			
		||||
    traits.set_visual_current_temperature_step(*this->visual_current_temperature_step_override_);
 | 
			
		||||
  }
 | 
			
		||||
  if (this->visual_min_humidity_override_.has_value()) {
 | 
			
		||||
    traits.set_visual_min_humidity(*this->visual_min_humidity_override_);
 | 
			
		||||
  }
 | 
			
		||||
  if (this->visual_max_humidity_override_.has_value()) {
 | 
			
		||||
    traits.set_visual_max_humidity(*this->visual_max_humidity_override_);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return traits;
 | 
			
		||||
}
 | 
			
		||||
@@ -441,6 +469,12 @@ void Climate::set_visual_temperature_step_override(float target, float current)
 | 
			
		||||
  this->visual_target_temperature_step_override_ = target;
 | 
			
		||||
  this->visual_current_temperature_step_override_ = current;
 | 
			
		||||
}
 | 
			
		||||
void Climate::set_visual_min_humidity_override(float visual_min_humidity_override) {
 | 
			
		||||
  this->visual_min_humidity_override_ = visual_min_humidity_override;
 | 
			
		||||
}
 | 
			
		||||
void Climate::set_visual_max_humidity_override(float visual_max_humidity_override) {
 | 
			
		||||
  this->visual_max_humidity_override_ = visual_max_humidity_override;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ClimateCall Climate::make_call() { return ClimateCall(this); }
 | 
			
		||||
 | 
			
		||||
@@ -454,6 +488,9 @@ ClimateCall ClimateDeviceRestoreState::to_call(Climate *climate) {
 | 
			
		||||
  } else {
 | 
			
		||||
    call.set_target_temperature(this->target_temperature);
 | 
			
		||||
  }
 | 
			
		||||
  if (traits.get_supports_target_humidity()) {
 | 
			
		||||
    call.set_target_humidity(this->target_humidity);
 | 
			
		||||
  }
 | 
			
		||||
  if (traits.get_supports_fan_modes() || !traits.get_supported_custom_fan_modes().empty()) {
 | 
			
		||||
    call.set_fan_mode(this->fan_mode);
 | 
			
		||||
  }
 | 
			
		||||
@@ -474,6 +511,9 @@ void ClimateDeviceRestoreState::apply(Climate *climate) {
 | 
			
		||||
  } else {
 | 
			
		||||
    climate->target_temperature = this->target_temperature;
 | 
			
		||||
  }
 | 
			
		||||
  if (traits.get_supports_target_humidity()) {
 | 
			
		||||
    climate->target_humidity = this->target_humidity;
 | 
			
		||||
  }
 | 
			
		||||
  if (traits.get_supports_fan_modes() && !this->uses_custom_fan_mode) {
 | 
			
		||||
    climate->fan_mode = this->fan_mode;
 | 
			
		||||
  }
 | 
			
		||||
@@ -530,17 +570,25 @@ void Climate::dump_traits_(const char *tag) {
 | 
			
		||||
  auto traits = this->get_traits();
 | 
			
		||||
  ESP_LOGCONFIG(tag, "ClimateTraits:");
 | 
			
		||||
  ESP_LOGCONFIG(tag, "  [x] Visual settings:");
 | 
			
		||||
  ESP_LOGCONFIG(tag, "      - Min: %.1f", traits.get_visual_min_temperature());
 | 
			
		||||
  ESP_LOGCONFIG(tag, "      - Max: %.1f", traits.get_visual_max_temperature());
 | 
			
		||||
  ESP_LOGCONFIG(tag, "      - Step:");
 | 
			
		||||
  ESP_LOGCONFIG(tag, "      - Min temperature: %.1f", traits.get_visual_min_temperature());
 | 
			
		||||
  ESP_LOGCONFIG(tag, "      - Max temperature: %.1f", traits.get_visual_max_temperature());
 | 
			
		||||
  ESP_LOGCONFIG(tag, "      - Temperature step:");
 | 
			
		||||
  ESP_LOGCONFIG(tag, "          Target: %.1f", traits.get_visual_target_temperature_step());
 | 
			
		||||
  ESP_LOGCONFIG(tag, "          Current: %.1f", traits.get_visual_current_temperature_step());
 | 
			
		||||
  ESP_LOGCONFIG(tag, "      - Min humidity: %.0f", traits.get_visual_min_humidity());
 | 
			
		||||
  ESP_LOGCONFIG(tag, "      - Max humidity: %.0f", traits.get_visual_max_humidity());
 | 
			
		||||
  if (traits.get_supports_current_temperature()) {
 | 
			
		||||
    ESP_LOGCONFIG(tag, "  [x] Supports current temperature");
 | 
			
		||||
  }
 | 
			
		||||
  if (traits.get_supports_current_humidity()) {
 | 
			
		||||
    ESP_LOGCONFIG(tag, "  [x] Supports current humidity");
 | 
			
		||||
  }
 | 
			
		||||
  if (traits.get_supports_two_point_target_temperature()) {
 | 
			
		||||
    ESP_LOGCONFIG(tag, "  [x] Supports two-point target temperature");
 | 
			
		||||
  }
 | 
			
		||||
  if (traits.get_supports_target_humidity()) {
 | 
			
		||||
    ESP_LOGCONFIG(tag, "  [x] Supports target humidity");
 | 
			
		||||
  }
 | 
			
		||||
  if (traits.get_supports_action()) {
 | 
			
		||||
    ESP_LOGCONFIG(tag, "  [x] Supports action");
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -64,6 +64,10 @@ class ClimateCall {
 | 
			
		||||
   * For climate devices with two point target temperature control
 | 
			
		||||
   */
 | 
			
		||||
  ClimateCall &set_target_temperature_high(optional<float> target_temperature_high);
 | 
			
		||||
  /// Set the target humidity of the climate device.
 | 
			
		||||
  ClimateCall &set_target_humidity(float target_humidity);
 | 
			
		||||
  /// Set the target humidity of the climate device.
 | 
			
		||||
  ClimateCall &set_target_humidity(optional<float> target_humidity);
 | 
			
		||||
  /// Set the fan mode of the climate device.
 | 
			
		||||
  ClimateCall &set_fan_mode(ClimateFanMode fan_mode);
 | 
			
		||||
  /// Set the fan mode of the climate device.
 | 
			
		||||
@@ -93,6 +97,7 @@ class ClimateCall {
 | 
			
		||||
  const optional<float> &get_target_temperature() const;
 | 
			
		||||
  const optional<float> &get_target_temperature_low() const;
 | 
			
		||||
  const optional<float> &get_target_temperature_high() const;
 | 
			
		||||
  const optional<float> &get_target_humidity() const;
 | 
			
		||||
  const optional<ClimateFanMode> &get_fan_mode() const;
 | 
			
		||||
  const optional<ClimateSwingMode> &get_swing_mode() const;
 | 
			
		||||
  const optional<std::string> &get_custom_fan_mode() const;
 | 
			
		||||
@@ -107,6 +112,7 @@ class ClimateCall {
 | 
			
		||||
  optional<float> target_temperature_;
 | 
			
		||||
  optional<float> target_temperature_low_;
 | 
			
		||||
  optional<float> target_temperature_high_;
 | 
			
		||||
  optional<float> target_humidity_;
 | 
			
		||||
  optional<ClimateFanMode> fan_mode_;
 | 
			
		||||
  optional<ClimateSwingMode> swing_mode_;
 | 
			
		||||
  optional<std::string> custom_fan_mode_;
 | 
			
		||||
@@ -136,6 +142,7 @@ struct ClimateDeviceRestoreState {
 | 
			
		||||
      float target_temperature_high;
 | 
			
		||||
    };
 | 
			
		||||
  };
 | 
			
		||||
  float target_humidity;
 | 
			
		||||
 | 
			
		||||
  /// Convert this struct to a climate call that can be performed.
 | 
			
		||||
  ClimateCall to_call(Climate *climate);
 | 
			
		||||
@@ -160,24 +167,34 @@ struct ClimateDeviceRestoreState {
 | 
			
		||||
 */
 | 
			
		||||
class Climate : public EntityBase {
 | 
			
		||||
 public:
 | 
			
		||||
  Climate() {}
 | 
			
		||||
 | 
			
		||||
  /// The active mode of the climate device.
 | 
			
		||||
  ClimateMode mode{CLIMATE_MODE_OFF};
 | 
			
		||||
 | 
			
		||||
  /// The active state of the climate device.
 | 
			
		||||
  ClimateAction action{CLIMATE_ACTION_OFF};
 | 
			
		||||
 | 
			
		||||
  /// The current temperature of the climate device, as reported from the integration.
 | 
			
		||||
  float current_temperature{NAN};
 | 
			
		||||
 | 
			
		||||
  /// The current humidity of the climate device, as reported from the integration.
 | 
			
		||||
  float current_humidity{NAN};
 | 
			
		||||
 | 
			
		||||
  union {
 | 
			
		||||
    /// The target temperature of the climate device.
 | 
			
		||||
    float target_temperature;
 | 
			
		||||
    struct {
 | 
			
		||||
      /// The minimum target temperature of the climate device, for climate devices with split target temperature.
 | 
			
		||||
      float target_temperature_low;
 | 
			
		||||
      float target_temperature_low{NAN};
 | 
			
		||||
      /// The maximum target temperature of the climate device, for climate devices with split target temperature.
 | 
			
		||||
      float target_temperature_high;
 | 
			
		||||
      float target_temperature_high{NAN};
 | 
			
		||||
    };
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  /// The target humidity of the climate device.
 | 
			
		||||
  float target_humidity;
 | 
			
		||||
 | 
			
		||||
  /// The active fan mode of the climate device.
 | 
			
		||||
  optional<ClimateFanMode> fan_mode;
 | 
			
		||||
 | 
			
		||||
@@ -231,6 +248,8 @@ class Climate : public EntityBase {
 | 
			
		||||
  void set_visual_min_temperature_override(float visual_min_temperature_override);
 | 
			
		||||
  void set_visual_max_temperature_override(float visual_max_temperature_override);
 | 
			
		||||
  void set_visual_temperature_step_override(float target, float current);
 | 
			
		||||
  void set_visual_min_humidity_override(float visual_min_humidity_override);
 | 
			
		||||
  void set_visual_max_humidity_override(float visual_max_humidity_override);
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  friend ClimateCall;
 | 
			
		||||
@@ -280,6 +299,8 @@ class Climate : public EntityBase {
 | 
			
		||||
  optional<float> visual_max_temperature_override_{};
 | 
			
		||||
  optional<float> visual_target_temperature_step_override_{};
 | 
			
		||||
  optional<float> visual_current_temperature_step_override_{};
 | 
			
		||||
  optional<float> visual_min_humidity_override_{};
 | 
			
		||||
  optional<float> visual_max_humidity_override_{};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace climate
 | 
			
		||||
 
 | 
			
		||||
@@ -44,10 +44,18 @@ class ClimateTraits {
 | 
			
		||||
  void set_supports_current_temperature(bool supports_current_temperature) {
 | 
			
		||||
    supports_current_temperature_ = supports_current_temperature;
 | 
			
		||||
  }
 | 
			
		||||
  bool get_supports_current_humidity() const { return supports_current_humidity_; }
 | 
			
		||||
  void set_supports_current_humidity(bool supports_current_humidity) {
 | 
			
		||||
    supports_current_humidity_ = supports_current_humidity;
 | 
			
		||||
  }
 | 
			
		||||
  bool get_supports_two_point_target_temperature() const { return supports_two_point_target_temperature_; }
 | 
			
		||||
  void set_supports_two_point_target_temperature(bool supports_two_point_target_temperature) {
 | 
			
		||||
    supports_two_point_target_temperature_ = supports_two_point_target_temperature;
 | 
			
		||||
  }
 | 
			
		||||
  bool get_supports_target_humidity() const { return supports_target_humidity_; }
 | 
			
		||||
  void set_supports_target_humidity(bool supports_target_humidity) {
 | 
			
		||||
    supports_target_humidity_ = supports_target_humidity;
 | 
			
		||||
  }
 | 
			
		||||
  void set_supported_modes(std::set<ClimateMode> modes) { supported_modes_ = std::move(modes); }
 | 
			
		||||
  void add_supported_mode(ClimateMode mode) { supported_modes_.insert(mode); }
 | 
			
		||||
  ESPDEPRECATED("This method is deprecated, use set_supported_modes() instead", "v1.20")
 | 
			
		||||
@@ -153,6 +161,11 @@ class ClimateTraits {
 | 
			
		||||
  int8_t get_target_temperature_accuracy_decimals() const;
 | 
			
		||||
  int8_t get_current_temperature_accuracy_decimals() const;
 | 
			
		||||
 | 
			
		||||
  float get_visual_min_humidity() const { return visual_min_humidity_; }
 | 
			
		||||
  void set_visual_min_humidity(float visual_min_humidity) { visual_min_humidity_ = visual_min_humidity; }
 | 
			
		||||
  float get_visual_max_humidity() const { return visual_max_humidity_; }
 | 
			
		||||
  void set_visual_max_humidity(float visual_max_humidity) { visual_max_humidity_ = visual_max_humidity; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void set_mode_support_(climate::ClimateMode mode, bool supported) {
 | 
			
		||||
    if (supported) {
 | 
			
		||||
@@ -177,7 +190,9 @@ class ClimateTraits {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  bool supports_current_temperature_{false};
 | 
			
		||||
  bool supports_current_humidity_{false};
 | 
			
		||||
  bool supports_two_point_target_temperature_{false};
 | 
			
		||||
  bool supports_target_humidity_{false};
 | 
			
		||||
  std::set<climate::ClimateMode> supported_modes_ = {climate::CLIMATE_MODE_OFF};
 | 
			
		||||
  bool supports_action_{false};
 | 
			
		||||
  std::set<climate::ClimateFanMode> supported_fan_modes_;
 | 
			
		||||
@@ -190,6 +205,8 @@ class ClimateTraits {
 | 
			
		||||
  float visual_max_temperature_{30};
 | 
			
		||||
  float visual_target_temperature_step_{0.1};
 | 
			
		||||
  float visual_current_temperature_step_{0.1};
 | 
			
		||||
  float visual_min_humidity_{30};
 | 
			
		||||
  float visual_max_humidity_{99};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace climate
 | 
			
		||||
 
 | 
			
		||||
@@ -1,38 +1,37 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.components import (
 | 
			
		||||
    climate,
 | 
			
		||||
    remote_transmitter,
 | 
			
		||||
    remote_receiver,
 | 
			
		||||
    sensor,
 | 
			
		||||
    remote_base,
 | 
			
		||||
)
 | 
			
		||||
from esphome.components.remote_base import CONF_RECEIVER_ID, CONF_TRANSMITTER_ID
 | 
			
		||||
from esphome.components import climate, sensor, remote_base
 | 
			
		||||
from esphome.const import CONF_SUPPORTS_COOL, CONF_SUPPORTS_HEAT, CONF_SENSOR
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["remote_transmitter"]
 | 
			
		||||
AUTO_LOAD = ["sensor", "remote_base"]
 | 
			
		||||
CODEOWNERS = ["@glmnet"]
 | 
			
		||||
 | 
			
		||||
climate_ir_ns = cg.esphome_ns.namespace("climate_ir")
 | 
			
		||||
ClimateIR = climate_ir_ns.class_(
 | 
			
		||||
    "ClimateIR", climate.Climate, cg.Component, remote_base.RemoteReceiverListener
 | 
			
		||||
    "ClimateIR",
 | 
			
		||||
    climate.Climate,
 | 
			
		||||
    cg.Component,
 | 
			
		||||
    remote_base.RemoteReceiverListener,
 | 
			
		||||
    remote_base.RemoteTransmittable,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CLIMATE_IR_SCHEMA = climate.CLIMATE_SCHEMA.extend(
 | 
			
		||||
    {
 | 
			
		||||
        cv.GenerateID(CONF_TRANSMITTER_ID): cv.use_id(
 | 
			
		||||
            remote_transmitter.RemoteTransmitterComponent
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_SUPPORTS_COOL, default=True): cv.boolean,
 | 
			
		||||
        cv.Optional(CONF_SUPPORTS_HEAT, default=True): cv.boolean,
 | 
			
		||||
        cv.Optional(CONF_SENSOR): cv.use_id(sensor.Sensor),
 | 
			
		||||
    }
 | 
			
		||||
).extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
CLIMATE_IR_SCHEMA = (
 | 
			
		||||
    climate.CLIMATE_SCHEMA.extend(
 | 
			
		||||
        {
 | 
			
		||||
            cv.Optional(CONF_SUPPORTS_COOL, default=True): cv.boolean,
 | 
			
		||||
            cv.Optional(CONF_SUPPORTS_HEAT, default=True): cv.boolean,
 | 
			
		||||
            cv.Optional(CONF_SENSOR): cv.use_id(sensor.Sensor),
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
    .extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
    .extend(remote_base.REMOTE_TRANSMITTABLE_SCHEMA)
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CLIMATE_IR_WITH_RECEIVER_SCHEMA = CLIMATE_IR_SCHEMA.extend(
 | 
			
		||||
    {
 | 
			
		||||
        cv.Optional(CONF_RECEIVER_ID): cv.use_id(
 | 
			
		||||
            remote_receiver.RemoteReceiverComponent
 | 
			
		||||
        cv.Optional(remote_base.CONF_RECEIVER_ID): cv.use_id(
 | 
			
		||||
            remote_base.RemoteReceiverBase
 | 
			
		||||
        ),
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
@@ -41,15 +40,11 @@ CLIMATE_IR_WITH_RECEIVER_SCHEMA = CLIMATE_IR_SCHEMA.extend(
 | 
			
		||||
async def register_climate_ir(var, config):
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
    await climate.register_climate(var, config)
 | 
			
		||||
 | 
			
		||||
    await remote_base.register_transmittable(var, config)
 | 
			
		||||
    cg.add(var.set_supports_cool(config[CONF_SUPPORTS_COOL]))
 | 
			
		||||
    cg.add(var.set_supports_heat(config[CONF_SUPPORTS_HEAT]))
 | 
			
		||||
    if remote_base.CONF_RECEIVER_ID in config:
 | 
			
		||||
        await remote_base.register_listener(var, config)
 | 
			
		||||
    if sensor_id := config.get(CONF_SENSOR):
 | 
			
		||||
        sens = await cg.get_variable(sensor_id)
 | 
			
		||||
        cg.add(var.set_sensor(sens))
 | 
			
		||||
    if receiver_id := config.get(CONF_RECEIVER_ID):
 | 
			
		||||
        receiver = await cg.get_variable(receiver_id)
 | 
			
		||||
        cg.add(receiver.register_listener(var))
 | 
			
		||||
 | 
			
		||||
    transmitter = await cg.get_variable(config[CONF_TRANSMITTER_ID])
 | 
			
		||||
    cg.add(var.set_transmitter(transmitter))
 | 
			
		||||
 
 | 
			
		||||
@@ -18,7 +18,10 @@ namespace climate_ir {
 | 
			
		||||
    Likewise to decode a IR into the AC state, implement
 | 
			
		||||
      bool RemoteReceiverListener::on_receive(remote_base::RemoteReceiveData data) and return true
 | 
			
		||||
*/
 | 
			
		||||
class ClimateIR : public climate::Climate, public Component, public remote_base::RemoteReceiverListener {
 | 
			
		||||
class ClimateIR : public Component,
 | 
			
		||||
                  public climate::Climate,
 | 
			
		||||
                  public remote_base::RemoteReceiverListener,
 | 
			
		||||
                  public remote_base::RemoteTransmittable {
 | 
			
		||||
 public:
 | 
			
		||||
  ClimateIR(float minimum_temperature, float maximum_temperature, float temperature_step = 1.0f,
 | 
			
		||||
            bool supports_dry = false, bool supports_fan_only = false, std::set<climate::ClimateFanMode> fan_modes = {},
 | 
			
		||||
@@ -35,9 +38,6 @@ class ClimateIR : public climate::Climate, public Component, public remote_base:
 | 
			
		||||
 | 
			
		||||
  void setup() override;
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
  void set_transmitter(remote_transmitter::RemoteTransmitterComponent *transmitter) {
 | 
			
		||||
    this->transmitter_ = transmitter;
 | 
			
		||||
  }
 | 
			
		||||
  void set_supports_cool(bool supports_cool) { this->supports_cool_ = supports_cool; }
 | 
			
		||||
  void set_supports_heat(bool supports_heat) { this->supports_heat_ = supports_heat; }
 | 
			
		||||
  void set_sensor(sensor::Sensor *sensor) { this->sensor_ = sensor; }
 | 
			
		||||
@@ -64,7 +64,6 @@ class ClimateIR : public climate::Climate, public Component, public remote_base:
 | 
			
		||||
  std::set<climate::ClimateSwingMode> swing_modes_ = {};
 | 
			
		||||
  std::set<climate::ClimatePreset> presets_ = {};
 | 
			
		||||
 | 
			
		||||
  remote_transmitter::RemoteTransmitterComponent *transmitter_;
 | 
			
		||||
  sensor::Sensor *sensor_{nullptr};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										0
									
								
								esphome/components/combination/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								esphome/components/combination/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										262
									
								
								esphome/components/combination/combination.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										262
									
								
								esphome/components/combination/combination.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,262 @@
 | 
			
		||||
#include "combination.h"
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
#include "esphome/core/hal.h"
 | 
			
		||||
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <functional>
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace combination {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "combination";
 | 
			
		||||
 | 
			
		||||
void CombinationComponent::log_config_(const LogString *combo_type) {
 | 
			
		||||
  LOG_SENSOR("", "Combination Sensor:", this);
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Combination Type: %s", LOG_STR_ARG(combo_type));
 | 
			
		||||
  this->log_source_sensors();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CombinationNoParameterComponent::add_source(Sensor *sensor) { this->sensors_.emplace_back(sensor); }
 | 
			
		||||
 | 
			
		||||
void CombinationOneParameterComponent::add_source(Sensor *sensor, std::function<float(float)> const &stddev) {
 | 
			
		||||
  this->sensor_pairs_.emplace_back(sensor, stddev);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CombinationOneParameterComponent::add_source(Sensor *sensor, float stddev) {
 | 
			
		||||
  this->add_source(sensor, std::function<float(float)>{[stddev](float x) -> float { return stddev; }});
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CombinationNoParameterComponent::log_source_sensors() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Source Sensors:");
 | 
			
		||||
  for (const auto &sensor : this->sensors_) {
 | 
			
		||||
    ESP_LOGCONFIG(TAG, "    - %s", sensor->get_name().c_str());
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CombinationOneParameterComponent::log_source_sensors() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Source Sensors:");
 | 
			
		||||
  for (const auto &sensor : this->sensor_pairs_) {
 | 
			
		||||
    auto &entity = *sensor.first;
 | 
			
		||||
    ESP_LOGCONFIG(TAG, "    - %s", entity.get_name().c_str());
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CombinationNoParameterComponent::setup() {
 | 
			
		||||
  for (const auto &sensor : this->sensors_) {
 | 
			
		||||
    // All sensor updates are deferred until the next loop. This avoids publishing the combined sensor's result
 | 
			
		||||
    // repeatedly in the same loop if multiple source senors update.
 | 
			
		||||
    sensor->add_on_state_callback(
 | 
			
		||||
        [this](float value) -> void { this->defer("update", [this, value]() { this->handle_new_value(value); }); });
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void KalmanCombinationComponent::dump_config() {
 | 
			
		||||
  this->log_config_(LOG_STR("kalman"));
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Update variance: %f per ms", this->update_variance_value_);
 | 
			
		||||
 | 
			
		||||
  if (this->std_dev_sensor_ != nullptr) {
 | 
			
		||||
    LOG_SENSOR("  ", "Standard Deviation Sensor:", this->std_dev_sensor_);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void KalmanCombinationComponent::setup() {
 | 
			
		||||
  for (const auto &sensor : this->sensor_pairs_) {
 | 
			
		||||
    const auto stddev = sensor.second;
 | 
			
		||||
    sensor.first->add_on_state_callback([this, stddev](float x) -> void { this->correct_(x, stddev(x)); });
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void KalmanCombinationComponent::update_variance_() {
 | 
			
		||||
  uint32_t now = millis();
 | 
			
		||||
 | 
			
		||||
  // Variance increases by update_variance_ each millisecond
 | 
			
		||||
  auto dt = now - this->last_update_;
 | 
			
		||||
  auto dv = this->update_variance_value_ * dt;
 | 
			
		||||
  this->variance_ += dv;
 | 
			
		||||
  this->last_update_ = now;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void KalmanCombinationComponent::correct_(float value, float stddev) {
 | 
			
		||||
  if (std::isnan(value) || std::isinf(stddev)) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (std::isnan(this->state_) || std::isinf(this->variance_)) {
 | 
			
		||||
    this->state_ = value;
 | 
			
		||||
    this->variance_ = stddev * stddev;
 | 
			
		||||
    if (this->std_dev_sensor_ != nullptr) {
 | 
			
		||||
      this->std_dev_sensor_->publish_state(stddev);
 | 
			
		||||
    }
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->update_variance_();
 | 
			
		||||
 | 
			
		||||
  // Combine two gaussian distributions mu1+-var1, mu2+-var2 to a new one around mu
 | 
			
		||||
  // Use the value with the smaller variance as mu1 to prevent precision errors
 | 
			
		||||
  const bool this_first = this->variance_ < (stddev * stddev);
 | 
			
		||||
  const float mu1 = this_first ? this->state_ : value;
 | 
			
		||||
  const float mu2 = this_first ? value : this->state_;
 | 
			
		||||
 | 
			
		||||
  const float var1 = this_first ? this->variance_ : stddev * stddev;
 | 
			
		||||
  const float var2 = this_first ? stddev * stddev : this->variance_;
 | 
			
		||||
 | 
			
		||||
  const float mu = mu1 + var1 * (mu2 - mu1) / (var1 + var2);
 | 
			
		||||
  const float var = var1 - (var1 * var1) / (var1 + var2);
 | 
			
		||||
 | 
			
		||||
  // Update and publish state
 | 
			
		||||
  this->state_ = mu;
 | 
			
		||||
  this->variance_ = var;
 | 
			
		||||
 | 
			
		||||
  this->publish_state(mu);
 | 
			
		||||
  if (this->std_dev_sensor_ != nullptr) {
 | 
			
		||||
    this->std_dev_sensor_->publish_state(std::sqrt(var));
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LinearCombinationComponent::setup() {
 | 
			
		||||
  for (const auto &sensor : this->sensor_pairs_) {
 | 
			
		||||
    // All sensor updates are deferred until the next loop. This avoids publishing the combined sensor's result
 | 
			
		||||
    // repeatedly in the same loop if multiple source senors update.
 | 
			
		||||
    sensor.first->add_on_state_callback(
 | 
			
		||||
        [this](float value) -> void { this->defer("update", [this, value]() { this->handle_new_value(value); }); });
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LinearCombinationComponent::handle_new_value(float value) {
 | 
			
		||||
  // Multiplies each sensor state by a configured coeffecient and then sums
 | 
			
		||||
 | 
			
		||||
  if (!std::isfinite(value))
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  float sum = 0.0;
 | 
			
		||||
 | 
			
		||||
  for (const auto &sensor : this->sensor_pairs_) {
 | 
			
		||||
    const float sensor_state = sensor.first->state;
 | 
			
		||||
    if (std::isfinite(sensor_state)) {
 | 
			
		||||
      sum += sensor_state * sensor.second(sensor_state);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->publish_state(sum);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
void MaximumCombinationComponent::handle_new_value(float value) {
 | 
			
		||||
  if (!std::isfinite(value))
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  float max_value = (-1) * std::numeric_limits<float>::infinity();  // note x = max(x, -infinity)
 | 
			
		||||
 | 
			
		||||
  for (const auto &sensor : this->sensors_) {
 | 
			
		||||
    if (std::isfinite(sensor->state)) {
 | 
			
		||||
      max_value = std::max(max_value, sensor->state);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->publish_state(max_value);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MeanCombinationComponent::handle_new_value(float value) {
 | 
			
		||||
  if (!std::isfinite(value))
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  float sum = 0.0;
 | 
			
		||||
  size_t count = 0.0;
 | 
			
		||||
 | 
			
		||||
  for (const auto &sensor : this->sensors_) {
 | 
			
		||||
    if (std::isfinite(sensor->state)) {
 | 
			
		||||
      ++count;
 | 
			
		||||
      sum += sensor->state;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  float mean = sum / count;
 | 
			
		||||
 | 
			
		||||
  this->publish_state(mean);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MedianCombinationComponent::handle_new_value(float value) {
 | 
			
		||||
  // Sorts sensor states in ascending order and determines the middle value
 | 
			
		||||
 | 
			
		||||
  if (!std::isfinite(value))
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  std::vector<float> sensor_states;
 | 
			
		||||
  for (const auto &sensor : this->sensors_) {
 | 
			
		||||
    if (std::isfinite(sensor->state)) {
 | 
			
		||||
      sensor_states.push_back(sensor->state);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  sort(sensor_states.begin(), sensor_states.end());
 | 
			
		||||
  size_t sensor_states_size = sensor_states.size();
 | 
			
		||||
 | 
			
		||||
  float median = NAN;
 | 
			
		||||
 | 
			
		||||
  if (sensor_states_size) {
 | 
			
		||||
    if (sensor_states_size % 2) {
 | 
			
		||||
      // Odd number of measurements, use middle measurement
 | 
			
		||||
      median = sensor_states[sensor_states_size / 2];
 | 
			
		||||
    } else {
 | 
			
		||||
      // Even number of measurements, use the average of the two middle measurements
 | 
			
		||||
      median = (sensor_states[sensor_states_size / 2] + sensor_states[sensor_states_size / 2 - 1]) / 2.0;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->publish_state(median);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MinimumCombinationComponent::handle_new_value(float value) {
 | 
			
		||||
  if (!std::isfinite(value))
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  float min_value = std::numeric_limits<float>::infinity();  // note x = min(x, infinity)
 | 
			
		||||
 | 
			
		||||
  for (const auto &sensor : this->sensors_) {
 | 
			
		||||
    if (std::isfinite(sensor->state)) {
 | 
			
		||||
      min_value = std::min(min_value, sensor->state);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->publish_state(min_value);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MostRecentCombinationComponent::handle_new_value(float value) { this->publish_state(value); }
 | 
			
		||||
 | 
			
		||||
void RangeCombinationComponent::handle_new_value(float value) {
 | 
			
		||||
  // Sorts sensor states then takes difference between largest and smallest states
 | 
			
		||||
 | 
			
		||||
  if (!std::isfinite(value))
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  std::vector<float> sensor_states;
 | 
			
		||||
  for (const auto &sensor : this->sensors_) {
 | 
			
		||||
    if (std::isfinite(sensor->state)) {
 | 
			
		||||
      sensor_states.push_back(sensor->state);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  sort(sensor_states.begin(), sensor_states.end());
 | 
			
		||||
 | 
			
		||||
  float range = sensor_states.back() - sensor_states.front();
 | 
			
		||||
  this->publish_state(range);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SumCombinationComponent::handle_new_value(float value) {
 | 
			
		||||
  if (!std::isfinite(value))
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  float sum = 0.0;
 | 
			
		||||
  for (const auto &sensor : this->sensors_) {
 | 
			
		||||
    if (std::isfinite(sensor->state)) {
 | 
			
		||||
      sum += sensor->state;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->publish_state(sum);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace combination
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										141
									
								
								esphome/components/combination/combination.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										141
									
								
								esphome/components/combination/combination.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,141 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/components/sensor/sensor.h"
 | 
			
		||||
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace combination {
 | 
			
		||||
 | 
			
		||||
class CombinationComponent : public Component, public sensor::Sensor {
 | 
			
		||||
 public:
 | 
			
		||||
  float get_setup_priority() const override { return esphome::setup_priority::DATA; }
 | 
			
		||||
 | 
			
		||||
  /// @brief Logs all source sensor's names
 | 
			
		||||
  virtual void log_source_sensors() = 0;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  /// @brief Logs the sensor for use in dump_config
 | 
			
		||||
  /// @param combo_type Name of the combination operation
 | 
			
		||||
  void log_config_(const LogString *combo_type);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/// @brief Base class for operations that do not require an extra parameter to compute the combination
 | 
			
		||||
class CombinationNoParameterComponent : public CombinationComponent {
 | 
			
		||||
 public:
 | 
			
		||||
  /// @brief Adds a callback to each source sensor
 | 
			
		||||
  void setup() override;
 | 
			
		||||
 | 
			
		||||
  void add_source(Sensor *sensor);
 | 
			
		||||
 | 
			
		||||
  /// @brief Computes the combination
 | 
			
		||||
  /// @param value Newest sensor measurement
 | 
			
		||||
  virtual void handle_new_value(float value) = 0;
 | 
			
		||||
 | 
			
		||||
  /// @brief Logs all source sensor's names in sensors_
 | 
			
		||||
  void log_source_sensors() override;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  std::vector<Sensor *> sensors_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// Base class for opertions that require one parameter to compute the combination
 | 
			
		||||
class CombinationOneParameterComponent : public CombinationComponent {
 | 
			
		||||
 public:
 | 
			
		||||
  void add_source(Sensor *sensor, std::function<float(float)> const &stddev);
 | 
			
		||||
  void add_source(Sensor *sensor, float stddev);
 | 
			
		||||
 | 
			
		||||
  /// @brief Logs all source sensor's names in sensor_pairs_
 | 
			
		||||
  void log_source_sensors() override;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  std::vector<std::pair<Sensor *, std::function<float(float)>>> sensor_pairs_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class KalmanCombinationComponent : public CombinationOneParameterComponent {
 | 
			
		||||
 public:
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
  void setup() override;
 | 
			
		||||
 | 
			
		||||
  void set_process_std_dev(float process_std_dev) {
 | 
			
		||||
    this->update_variance_value_ = process_std_dev * process_std_dev * 0.001f;
 | 
			
		||||
  }
 | 
			
		||||
  void set_std_dev_sensor(Sensor *sensor) { this->std_dev_sensor_ = sensor; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void update_variance_();
 | 
			
		||||
  void correct_(float value, float stddev);
 | 
			
		||||
 | 
			
		||||
  // Optional sensor for publishing the current error
 | 
			
		||||
  sensor::Sensor *std_dev_sensor_{nullptr};
 | 
			
		||||
 | 
			
		||||
  // Tick of the last update
 | 
			
		||||
  uint32_t last_update_{0};
 | 
			
		||||
  // Change of the variance, per ms
 | 
			
		||||
  float update_variance_value_{0.f};
 | 
			
		||||
 | 
			
		||||
  // Best guess for the state and its variance
 | 
			
		||||
  float state_{NAN};
 | 
			
		||||
  float variance_{INFINITY};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class LinearCombinationComponent : public CombinationOneParameterComponent {
 | 
			
		||||
 public:
 | 
			
		||||
  void dump_config() override { this->log_config_(LOG_STR("linear")); }
 | 
			
		||||
  void setup() override;
 | 
			
		||||
 | 
			
		||||
  void handle_new_value(float value);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class MaximumCombinationComponent : public CombinationNoParameterComponent {
 | 
			
		||||
 public:
 | 
			
		||||
  void dump_config() override { this->log_config_(LOG_STR("max")); }
 | 
			
		||||
 | 
			
		||||
  void handle_new_value(float value) override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class MeanCombinationComponent : public CombinationNoParameterComponent {
 | 
			
		||||
 public:
 | 
			
		||||
  void dump_config() override { this->log_config_(LOG_STR("mean")); }
 | 
			
		||||
 | 
			
		||||
  void handle_new_value(float value) override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class MedianCombinationComponent : public CombinationNoParameterComponent {
 | 
			
		||||
 public:
 | 
			
		||||
  void dump_config() override { this->log_config_(LOG_STR("median")); }
 | 
			
		||||
 | 
			
		||||
  void handle_new_value(float value) override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class MinimumCombinationComponent : public CombinationNoParameterComponent {
 | 
			
		||||
 public:
 | 
			
		||||
  void dump_config() override { this->log_config_(LOG_STR("min")); }
 | 
			
		||||
 | 
			
		||||
  void handle_new_value(float value) override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class MostRecentCombinationComponent : public CombinationNoParameterComponent {
 | 
			
		||||
 public:
 | 
			
		||||
  void dump_config() override { this->log_config_(LOG_STR("most_recently_updated")); }
 | 
			
		||||
 | 
			
		||||
  void handle_new_value(float value) override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class RangeCombinationComponent : public CombinationNoParameterComponent {
 | 
			
		||||
 public:
 | 
			
		||||
  void dump_config() override { this->log_config_(LOG_STR("range")); }
 | 
			
		||||
 | 
			
		||||
  void handle_new_value(float value) override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class SumCombinationComponent : public CombinationNoParameterComponent {
 | 
			
		||||
 public:
 | 
			
		||||
  void dump_config() override { this->log_config_(LOG_STR("sum")); }
 | 
			
		||||
 | 
			
		||||
  void handle_new_value(float value) override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace combination
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										176
									
								
								esphome/components/combination/sensor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										176
									
								
								esphome/components/combination/sensor.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,176 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.components import sensor
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_ACCURACY_DECIMALS,
 | 
			
		||||
    CONF_DEVICE_CLASS,
 | 
			
		||||
    CONF_ENTITY_CATEGORY,
 | 
			
		||||
    CONF_ICON,
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    CONF_RANGE,
 | 
			
		||||
    CONF_SOURCE,
 | 
			
		||||
    CONF_SUM,
 | 
			
		||||
    CONF_TYPE,
 | 
			
		||||
    CONF_UNIT_OF_MEASUREMENT,
 | 
			
		||||
)
 | 
			
		||||
from esphome.core.entity_helpers import inherit_property_from
 | 
			
		||||
 | 
			
		||||
CODEOWNERS = ["@Cat-Ion", "@kahrendt"]
 | 
			
		||||
 | 
			
		||||
combination_ns = cg.esphome_ns.namespace("combination")
 | 
			
		||||
 | 
			
		||||
KalmanCombinationComponent = combination_ns.class_(
 | 
			
		||||
    "KalmanCombinationComponent", cg.Component, sensor.Sensor
 | 
			
		||||
)
 | 
			
		||||
LinearCombinationComponent = combination_ns.class_(
 | 
			
		||||
    "LinearCombinationComponent", cg.Component, sensor.Sensor
 | 
			
		||||
)
 | 
			
		||||
MaximumCombinationComponent = combination_ns.class_(
 | 
			
		||||
    "MaximumCombinationComponent", cg.Component, sensor.Sensor
 | 
			
		||||
)
 | 
			
		||||
MeanCombinationComponent = combination_ns.class_(
 | 
			
		||||
    "MeanCombinationComponent", cg.Component, sensor.Sensor
 | 
			
		||||
)
 | 
			
		||||
MedianCombinationComponent = combination_ns.class_(
 | 
			
		||||
    "MedianCombinationComponent", cg.Component, sensor.Sensor
 | 
			
		||||
)
 | 
			
		||||
MinimumCombinationComponent = combination_ns.class_(
 | 
			
		||||
    "MinimumCombinationComponent", cg.Component, sensor.Sensor
 | 
			
		||||
)
 | 
			
		||||
MostRecentCombinationComponent = combination_ns.class_(
 | 
			
		||||
    "MostRecentCombinationComponent", cg.Component, sensor.Sensor
 | 
			
		||||
)
 | 
			
		||||
RangeCombinationComponent = combination_ns.class_(
 | 
			
		||||
    "RangeCombinationComponent", cg.Component, sensor.Sensor
 | 
			
		||||
)
 | 
			
		||||
SumCombinationComponent = combination_ns.class_(
 | 
			
		||||
    "SumCombinationComponent", cg.Component, sensor.Sensor
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CONF_COEFFECIENT = "coeffecient"
 | 
			
		||||
CONF_ERROR = "error"
 | 
			
		||||
CONF_KALMAN = "kalman"
 | 
			
		||||
CONF_LINEAR = "linear"
 | 
			
		||||
CONF_MAX = "max"
 | 
			
		||||
CONF_MEAN = "mean"
 | 
			
		||||
CONF_MEDIAN = "median"
 | 
			
		||||
CONF_MIN = "min"
 | 
			
		||||
CONF_MOST_RECENTLY_UPDATED = "most_recently_updated"
 | 
			
		||||
CONF_PROCESS_STD_DEV = "process_std_dev"
 | 
			
		||||
CONF_SOURCES = "sources"
 | 
			
		||||
CONF_STD_DEV = "std_dev"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
KALMAN_SOURCE_SCHEMA = cv.Schema(
 | 
			
		||||
    {
 | 
			
		||||
        cv.Required(CONF_SOURCE): cv.use_id(sensor.Sensor),
 | 
			
		||||
        cv.Required(CONF_ERROR): cv.templatable(cv.positive_float),
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
LINEAR_SOURCE_SCHEMA = cv.Schema(
 | 
			
		||||
    {
 | 
			
		||||
        cv.Required(CONF_SOURCE): cv.use_id(sensor.Sensor),
 | 
			
		||||
        cv.Required(CONF_COEFFECIENT): cv.templatable(cv.float_),
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
SENSOR_ONLY_SOURCE_SCHEMA = cv.Schema(
 | 
			
		||||
    {
 | 
			
		||||
        cv.Required(CONF_SOURCE): cv.use_id(sensor.Sensor),
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = cv.typed_schema(
 | 
			
		||||
    {
 | 
			
		||||
        CONF_KALMAN: sensor.sensor_schema(KalmanCombinationComponent)
 | 
			
		||||
        .extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
        .extend(
 | 
			
		||||
            {
 | 
			
		||||
                cv.Required(CONF_PROCESS_STD_DEV): cv.positive_float,
 | 
			
		||||
                cv.Required(CONF_SOURCES): cv.ensure_list(KALMAN_SOURCE_SCHEMA),
 | 
			
		||||
                cv.Optional(CONF_STD_DEV): sensor.sensor_schema(),
 | 
			
		||||
            }
 | 
			
		||||
        ),
 | 
			
		||||
        CONF_LINEAR: sensor.sensor_schema(LinearCombinationComponent)
 | 
			
		||||
        .extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
        .extend({cv.Required(CONF_SOURCES): cv.ensure_list(LINEAR_SOURCE_SCHEMA)}),
 | 
			
		||||
        CONF_MAX: sensor.sensor_schema(MaximumCombinationComponent)
 | 
			
		||||
        .extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
        .extend({cv.Required(CONF_SOURCES): cv.ensure_list(SENSOR_ONLY_SOURCE_SCHEMA)}),
 | 
			
		||||
        CONF_MEAN: sensor.sensor_schema(MeanCombinationComponent)
 | 
			
		||||
        .extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
        .extend({cv.Required(CONF_SOURCES): cv.ensure_list(SENSOR_ONLY_SOURCE_SCHEMA)}),
 | 
			
		||||
        CONF_MEDIAN: sensor.sensor_schema(MedianCombinationComponent)
 | 
			
		||||
        .extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
        .extend({cv.Required(CONF_SOURCES): cv.ensure_list(SENSOR_ONLY_SOURCE_SCHEMA)}),
 | 
			
		||||
        CONF_MIN: sensor.sensor_schema(MinimumCombinationComponent)
 | 
			
		||||
        .extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
        .extend({cv.Required(CONF_SOURCES): cv.ensure_list(SENSOR_ONLY_SOURCE_SCHEMA)}),
 | 
			
		||||
        CONF_MOST_RECENTLY_UPDATED: sensor.sensor_schema(MostRecentCombinationComponent)
 | 
			
		||||
        .extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
        .extend({cv.Required(CONF_SOURCES): cv.ensure_list(SENSOR_ONLY_SOURCE_SCHEMA)}),
 | 
			
		||||
        CONF_RANGE: sensor.sensor_schema(RangeCombinationComponent)
 | 
			
		||||
        .extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
        .extend({cv.Required(CONF_SOURCES): cv.ensure_list(SENSOR_ONLY_SOURCE_SCHEMA)}),
 | 
			
		||||
        CONF_SUM: sensor.sensor_schema(SumCombinationComponent)
 | 
			
		||||
        .extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
        .extend({cv.Required(CONF_SOURCES): cv.ensure_list(SENSOR_ONLY_SOURCE_SCHEMA)}),
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# Inherit some sensor values from the first source, for both the state and the error value
 | 
			
		||||
# CONF_STATE_CLASS could also be inherited, but might lead to unexpected behaviour with "total_increasing"
 | 
			
		||||
properties_to_inherit = [
 | 
			
		||||
    CONF_ACCURACY_DECIMALS,
 | 
			
		||||
    CONF_DEVICE_CLASS,
 | 
			
		||||
    CONF_ENTITY_CATEGORY,
 | 
			
		||||
    CONF_ICON,
 | 
			
		||||
    CONF_UNIT_OF_MEASUREMENT,
 | 
			
		||||
]
 | 
			
		||||
inherit_schema_for_state = [
 | 
			
		||||
    inherit_property_from(property, [CONF_SOURCES, 0, CONF_SOURCE])
 | 
			
		||||
    for property in properties_to_inherit
 | 
			
		||||
]
 | 
			
		||||
inherit_schema_for_std_dev = [
 | 
			
		||||
    inherit_property_from([CONF_STD_DEV, property], [CONF_SOURCES, 0, CONF_SOURCE])
 | 
			
		||||
    for property in properties_to_inherit
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
FINAL_VALIDATE_SCHEMA = cv.All(
 | 
			
		||||
    *inherit_schema_for_state,
 | 
			
		||||
    *inherit_schema_for_std_dev,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
    await sensor.register_sensor(var, config)
 | 
			
		||||
 | 
			
		||||
    if proces_std_dev := config.get(CONF_PROCESS_STD_DEV):
 | 
			
		||||
        cg.add(var.set_process_std_dev(proces_std_dev))
 | 
			
		||||
 | 
			
		||||
    for source_conf in config[CONF_SOURCES]:
 | 
			
		||||
        source = await cg.get_variable(source_conf[CONF_SOURCE])
 | 
			
		||||
        if config[CONF_TYPE] == CONF_KALMAN:
 | 
			
		||||
            error = await cg.templatable(
 | 
			
		||||
                source_conf[CONF_ERROR],
 | 
			
		||||
                [(float, "x")],
 | 
			
		||||
                cg.float_,
 | 
			
		||||
            )
 | 
			
		||||
            cg.add(var.add_source(source, error))
 | 
			
		||||
        elif config[CONF_TYPE] == CONF_LINEAR:
 | 
			
		||||
            coeffecient = await cg.templatable(
 | 
			
		||||
                source_conf[CONF_COEFFECIENT],
 | 
			
		||||
                [(float, "x")],
 | 
			
		||||
                cg.float_,
 | 
			
		||||
            )
 | 
			
		||||
            cg.add(var.add_source(source, coeffecient))
 | 
			
		||||
        else:
 | 
			
		||||
            cg.add(var.add_source(source))
 | 
			
		||||
 | 
			
		||||
    if CONF_STD_DEV in config:
 | 
			
		||||
        sens = await sensor.new_sensor(config[CONF_STD_DEV])
 | 
			
		||||
        cg.add(var.set_std_dev_sensor(sens))
 | 
			
		||||
@@ -102,11 +102,7 @@ void CoolixClimate::transmit_state() {
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGV(TAG, "Sending coolix code: 0x%06" PRIX32, remote_state);
 | 
			
		||||
 | 
			
		||||
  auto transmit = this->transmitter_->transmit();
 | 
			
		||||
  auto *data = transmit.get_data();
 | 
			
		||||
  remote_base::CoolixProtocol().encode(data, remote_state);
 | 
			
		||||
  transmit.perform();
 | 
			
		||||
  this->transmit_<remote_base::CoolixProtocol>(remote_state);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool CoolixClimate::on_coolix(climate::Climate *parent, remote_base::RemoteReceiveData data) {
 | 
			
		||||
 
 | 
			
		||||
@@ -12,6 +12,7 @@ void CopyFan::setup() {
 | 
			
		||||
    this->oscillating = source_->oscillating;
 | 
			
		||||
    this->speed = source_->speed;
 | 
			
		||||
    this->direction = source_->direction;
 | 
			
		||||
    this->preset_mode = source_->preset_mode;
 | 
			
		||||
    this->publish_state();
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
@@ -19,6 +20,7 @@ void CopyFan::setup() {
 | 
			
		||||
  this->oscillating = source_->oscillating;
 | 
			
		||||
  this->speed = source_->speed;
 | 
			
		||||
  this->direction = source_->direction;
 | 
			
		||||
  this->preset_mode = source_->preset_mode;
 | 
			
		||||
  this->publish_state();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -33,6 +35,7 @@ fan::FanTraits CopyFan::get_traits() {
 | 
			
		||||
  traits.set_speed(base.supports_speed());
 | 
			
		||||
  traits.set_supported_speed_count(base.supported_speed_count());
 | 
			
		||||
  traits.set_direction(base.supports_direction());
 | 
			
		||||
  traits.set_supported_preset_modes(base.supported_preset_modes());
 | 
			
		||||
  return traits;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -46,6 +49,8 @@ void CopyFan::control(const fan::FanCall &call) {
 | 
			
		||||
    call2.set_speed(*call.get_speed());
 | 
			
		||||
  if (call.get_direction().has_value())
 | 
			
		||||
    call2.set_direction(*call.get_direction());
 | 
			
		||||
  if (!call.get_preset_mode().empty())
 | 
			
		||||
    call2.set_preset_mode(call.get_preset_mode());
 | 
			
		||||
  call2.perform();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,8 @@
 | 
			
		||||
#include "cse7766.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
#include <cinttypes>
 | 
			
		||||
#include <iomanip>
 | 
			
		||||
#include <sstream>
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace cse7766 {
 | 
			
		||||
@@ -68,20 +70,26 @@ bool CSE7766Component::check_byte_() {
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
void CSE7766Component::parse_data_() {
 | 
			
		||||
  ESP_LOGVV(TAG, "CSE7766 Data: ");
 | 
			
		||||
  for (uint8_t i = 0; i < 23; i++) {
 | 
			
		||||
    ESP_LOGVV(TAG, "  %u: 0b" BYTE_TO_BINARY_PATTERN " (0x%02X)", i + 1, BYTE_TO_BINARY(this->raw_data_[i]),
 | 
			
		||||
              this->raw_data_[i]);
 | 
			
		||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
 | 
			
		||||
  {
 | 
			
		||||
    std::stringstream ss;
 | 
			
		||||
    ss << "Raw data:" << std::hex << std::uppercase << std::setfill('0');
 | 
			
		||||
    for (uint8_t i = 0; i < 23; i++) {
 | 
			
		||||
      ss << ' ' << std::setw(2) << static_cast<unsigned>(this->raw_data_[i]);
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGVV(TAG, "%s", ss.str().c_str());
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  // Parse header
 | 
			
		||||
  uint8_t header1 = this->raw_data_[0];
 | 
			
		||||
 | 
			
		||||
  if (header1 == 0xAA) {
 | 
			
		||||
    ESP_LOGE(TAG, "CSE7766 not calibrated!");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  bool power_cycle_exceeds_range = false;
 | 
			
		||||
 | 
			
		||||
  if ((header1 & 0xF0) == 0xF0) {
 | 
			
		||||
    if (header1 & 0xD) {
 | 
			
		||||
      ESP_LOGE(TAG, "CSE7766 reports abnormal external circuit or chip damage: (0x%02X)", header1);
 | 
			
		||||
@@ -94,99 +102,106 @@ void CSE7766Component::parse_data_() {
 | 
			
		||||
      if (header1 & (1 << 0)) {
 | 
			
		||||
        ESP_LOGE(TAG, "  Coefficient storage area is abnormal.");
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      // Datasheet: voltage or current cycle exceeding range means invalid values
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    power_cycle_exceeds_range = header1 & (1 << 1);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  uint32_t voltage_calib = this->get_24_bit_uint_(2);
 | 
			
		||||
  // Parse data frame
 | 
			
		||||
  uint32_t voltage_coeff = this->get_24_bit_uint_(2);
 | 
			
		||||
  uint32_t voltage_cycle = this->get_24_bit_uint_(5);
 | 
			
		||||
  uint32_t current_calib = this->get_24_bit_uint_(8);
 | 
			
		||||
  uint32_t current_coeff = this->get_24_bit_uint_(8);
 | 
			
		||||
  uint32_t current_cycle = this->get_24_bit_uint_(11);
 | 
			
		||||
  uint32_t power_calib = this->get_24_bit_uint_(14);
 | 
			
		||||
  uint32_t power_coeff = this->get_24_bit_uint_(14);
 | 
			
		||||
  uint32_t power_cycle = this->get_24_bit_uint_(17);
 | 
			
		||||
 | 
			
		||||
  uint8_t adj = this->raw_data_[20];
 | 
			
		||||
  uint32_t cf_pulses = (this->raw_data_[21] << 8) + this->raw_data_[22];
 | 
			
		||||
 | 
			
		||||
  bool have_power = adj & 0x10;
 | 
			
		||||
  bool have_current = adj & 0x20;
 | 
			
		||||
  bool have_voltage = adj & 0x40;
 | 
			
		||||
 | 
			
		||||
  float voltage = 0.0f;
 | 
			
		||||
  if (have_voltage) {
 | 
			
		||||
    // voltage cycle of serial port outputted is a complete cycle;
 | 
			
		||||
    this->voltage_acc_ += voltage_calib / float(voltage_cycle);
 | 
			
		||||
    this->voltage_counts_ += 1;
 | 
			
		||||
    voltage = voltage_coeff / float(voltage_cycle);
 | 
			
		||||
    if (this->voltage_sensor_ != nullptr) {
 | 
			
		||||
      this->voltage_sensor_->publish_state(voltage);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  bool have_power = adj & 0x10;
 | 
			
		||||
  float power = 0.0f;
 | 
			
		||||
 | 
			
		||||
  if (have_power) {
 | 
			
		||||
    // power cycle of serial port outputted is a complete cycle;
 | 
			
		||||
    // According to the user manual, power cycle exceeding range means the measured power is 0
 | 
			
		||||
    if (!power_cycle_exceeds_range) {
 | 
			
		||||
      power = power_calib / float(power_cycle);
 | 
			
		||||
  float energy = 0.0f;
 | 
			
		||||
  if (power_cycle_exceeds_range) {
 | 
			
		||||
    // Datasheet: power cycle exceeding range means active power is 0
 | 
			
		||||
    if (this->power_sensor_ != nullptr) {
 | 
			
		||||
      this->power_sensor_->publish_state(0.0f);
 | 
			
		||||
    }
 | 
			
		||||
  } else if (have_power) {
 | 
			
		||||
    power = power_coeff / float(power_cycle);
 | 
			
		||||
    if (this->power_sensor_ != nullptr) {
 | 
			
		||||
      this->power_sensor_->publish_state(power);
 | 
			
		||||
    }
 | 
			
		||||
    this->power_acc_ += power;
 | 
			
		||||
    this->power_counts_ += 1;
 | 
			
		||||
 | 
			
		||||
    uint32_t difference;
 | 
			
		||||
    // Add CF pulses to the total energy only if we have Power coefficient to multiply by
 | 
			
		||||
 | 
			
		||||
    if (this->cf_pulses_last_ == 0) {
 | 
			
		||||
      this->cf_pulses_last_ = cf_pulses;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    uint32_t cf_diff;
 | 
			
		||||
    if (cf_pulses < this->cf_pulses_last_) {
 | 
			
		||||
      difference = cf_pulses + (0x10000 - this->cf_pulses_last_);
 | 
			
		||||
      cf_diff = cf_pulses + (0x10000 - this->cf_pulses_last_);
 | 
			
		||||
    } else {
 | 
			
		||||
      difference = cf_pulses - this->cf_pulses_last_;
 | 
			
		||||
      cf_diff = cf_pulses - this->cf_pulses_last_;
 | 
			
		||||
    }
 | 
			
		||||
    this->cf_pulses_last_ = cf_pulses;
 | 
			
		||||
    this->energy_total_ += difference * float(power_calib) / 1000000.0f / 3600.0f;
 | 
			
		||||
    this->energy_total_counts_ += 1;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (adj & 0x20) {
 | 
			
		||||
    // indicates current cycle of serial port outputted is a complete cycle;
 | 
			
		||||
    float current = 0.0f;
 | 
			
		||||
    if (have_voltage && !have_power) {
 | 
			
		||||
      // Testing has shown that when we have voltage and current but not power, that means the power is 0.
 | 
			
		||||
      // We report a power of 0, which in turn means we should report a current of 0.
 | 
			
		||||
      this->power_counts_ += 1;
 | 
			
		||||
    } else if (power != 0.0f) {
 | 
			
		||||
      current = current_calib / float(current_cycle);
 | 
			
		||||
    }
 | 
			
		||||
    this->current_acc_ += current;
 | 
			
		||||
    this->current_counts_ += 1;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
void CSE7766Component::update() {
 | 
			
		||||
  const auto publish_state = [](const char *name, sensor::Sensor *sensor, float &acc, uint32_t &counts) {
 | 
			
		||||
    if (counts != 0) {
 | 
			
		||||
      const auto avg = acc / counts;
 | 
			
		||||
 | 
			
		||||
      ESP_LOGV(TAG, "Got %s_acc=%.2f %s_counts=%" PRIu32 " %s=%.1f", name, acc, name, counts, name, avg);
 | 
			
		||||
 | 
			
		||||
      if (sensor != nullptr) {
 | 
			
		||||
        sensor->publish_state(avg);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      acc = 0.0f;
 | 
			
		||||
      counts = 0;
 | 
			
		||||
    }
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  publish_state("voltage", this->voltage_sensor_, this->voltage_acc_, this->voltage_counts_);
 | 
			
		||||
  publish_state("current", this->current_sensor_, this->current_acc_, this->current_counts_);
 | 
			
		||||
  publish_state("power", this->power_sensor_, this->power_acc_, this->power_counts_);
 | 
			
		||||
 | 
			
		||||
  if (this->energy_total_counts_ != 0) {
 | 
			
		||||
    ESP_LOGV(TAG, "Got energy_total=%.2f energy_total_counts=%" PRIu32, this->energy_total_,
 | 
			
		||||
             this->energy_total_counts_);
 | 
			
		||||
 | 
			
		||||
    if (this->energy_sensor_ != nullptr) {
 | 
			
		||||
    energy = cf_diff * float(power_coeff) / 1000000.0f / 3600.0f;
 | 
			
		||||
    this->energy_total_ += energy;
 | 
			
		||||
    if (this->energy_sensor_ != nullptr)
 | 
			
		||||
      this->energy_sensor_->publish_state(this->energy_total_);
 | 
			
		||||
    }
 | 
			
		||||
    this->energy_total_counts_ = 0;
 | 
			
		||||
  } else if ((this->energy_sensor_ != nullptr) && !this->energy_sensor_->has_state()) {
 | 
			
		||||
    this->energy_sensor_->publish_state(0);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  float current = 0.0f;
 | 
			
		||||
  float calculated_current = 0.0f;
 | 
			
		||||
  if (have_current) {
 | 
			
		||||
    // Assumption: if we don't have power measurement, then current is likely below 50mA
 | 
			
		||||
    if (have_power && voltage > 1.0f) {
 | 
			
		||||
      calculated_current = power / voltage;
 | 
			
		||||
    }
 | 
			
		||||
    // Datasheet: minimum measured current is 50mA
 | 
			
		||||
    if (calculated_current > 0.05f) {
 | 
			
		||||
      current = current_coeff / float(current_cycle);
 | 
			
		||||
    }
 | 
			
		||||
    if (this->current_sensor_ != nullptr) {
 | 
			
		||||
      this->current_sensor_->publish_state(current);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
 | 
			
		||||
  {
 | 
			
		||||
    std::stringstream ss;
 | 
			
		||||
    ss << "Parsed:";
 | 
			
		||||
    if (have_voltage) {
 | 
			
		||||
      ss << " V=" << voltage << "V";
 | 
			
		||||
    }
 | 
			
		||||
    if (have_current) {
 | 
			
		||||
      ss << " I=" << current * 1000.0f << "mA (~" << calculated_current * 1000.0f << "mA)";
 | 
			
		||||
    }
 | 
			
		||||
    if (have_power) {
 | 
			
		||||
      ss << " P=" << power << "W";
 | 
			
		||||
    }
 | 
			
		||||
    if (energy != 0.0f) {
 | 
			
		||||
      ss << " E=" << energy << "kWh (" << cf_pulses << ")";
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGVV(TAG, "%s", ss.str().c_str());
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint32_t CSE7766Component::get_24_bit_uint_(uint8_t start_index) {
 | 
			
		||||
@@ -196,7 +211,6 @@ uint32_t CSE7766Component::get_24_bit_uint_(uint8_t start_index) {
 | 
			
		||||
 | 
			
		||||
void CSE7766Component::dump_config() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "CSE7766:");
 | 
			
		||||
  LOG_UPDATE_INTERVAL(this);
 | 
			
		||||
  LOG_SENSOR("  ", "Voltage", this->voltage_sensor_);
 | 
			
		||||
  LOG_SENSOR("  ", "Current", this->current_sensor_);
 | 
			
		||||
  LOG_SENSOR("  ", "Power", this->power_sensor_);
 | 
			
		||||
 
 | 
			
		||||
@@ -7,7 +7,7 @@
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace cse7766 {
 | 
			
		||||
 | 
			
		||||
class CSE7766Component : public PollingComponent, public uart::UARTDevice {
 | 
			
		||||
class CSE7766Component : public Component, public uart::UARTDevice {
 | 
			
		||||
 public:
 | 
			
		||||
  void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
 | 
			
		||||
  void set_current_sensor(sensor::Sensor *current_sensor) { current_sensor_ = current_sensor; }
 | 
			
		||||
@@ -16,7 +16,6 @@ class CSE7766Component : public PollingComponent, public uart::UARTDevice {
 | 
			
		||||
 | 
			
		||||
  void loop() override;
 | 
			
		||||
  float get_setup_priority() const override;
 | 
			
		||||
  void update() override;
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
@@ -31,16 +30,8 @@ class CSE7766Component : public PollingComponent, public uart::UARTDevice {
 | 
			
		||||
  sensor::Sensor *current_sensor_{nullptr};
 | 
			
		||||
  sensor::Sensor *power_sensor_{nullptr};
 | 
			
		||||
  sensor::Sensor *energy_sensor_{nullptr};
 | 
			
		||||
  float voltage_acc_{0.0f};
 | 
			
		||||
  float current_acc_{0.0f};
 | 
			
		||||
  float power_acc_{0.0f};
 | 
			
		||||
  float energy_total_{0.0f};
 | 
			
		||||
  uint32_t cf_pulses_last_{0};
 | 
			
		||||
  uint32_t voltage_counts_{0};
 | 
			
		||||
  uint32_t current_counts_{0};
 | 
			
		||||
  uint32_t power_counts_{0};
 | 
			
		||||
  // Setting this to 1 means it will always publish 0 once at startup
 | 
			
		||||
  uint32_t energy_total_counts_{1};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace cse7766
 | 
			
		||||
 
 | 
			
		||||
@@ -22,43 +22,37 @@ from esphome.const import (
 | 
			
		||||
DEPENDENCIES = ["uart"]
 | 
			
		||||
 | 
			
		||||
cse7766_ns = cg.esphome_ns.namespace("cse7766")
 | 
			
		||||
CSE7766Component = cse7766_ns.class_(
 | 
			
		||||
    "CSE7766Component", cg.PollingComponent, uart.UARTDevice
 | 
			
		||||
)
 | 
			
		||||
CSE7766Component = cse7766_ns.class_("CSE7766Component", cg.Component, uart.UARTDevice)
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = (
 | 
			
		||||
    cv.Schema(
 | 
			
		||||
        {
 | 
			
		||||
            cv.GenerateID(): cv.declare_id(CSE7766Component),
 | 
			
		||||
            cv.Optional(CONF_VOLTAGE): sensor.sensor_schema(
 | 
			
		||||
                unit_of_measurement=UNIT_VOLT,
 | 
			
		||||
                accuracy_decimals=1,
 | 
			
		||||
                device_class=DEVICE_CLASS_VOLTAGE,
 | 
			
		||||
                state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_CURRENT): sensor.sensor_schema(
 | 
			
		||||
                unit_of_measurement=UNIT_AMPERE,
 | 
			
		||||
                accuracy_decimals=2,
 | 
			
		||||
                device_class=DEVICE_CLASS_CURRENT,
 | 
			
		||||
                state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_POWER): sensor.sensor_schema(
 | 
			
		||||
                unit_of_measurement=UNIT_WATT,
 | 
			
		||||
                accuracy_decimals=1,
 | 
			
		||||
                device_class=DEVICE_CLASS_POWER,
 | 
			
		||||
                state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_ENERGY): sensor.sensor_schema(
 | 
			
		||||
                unit_of_measurement=UNIT_WATT_HOURS,
 | 
			
		||||
                accuracy_decimals=3,
 | 
			
		||||
                device_class=DEVICE_CLASS_ENERGY,
 | 
			
		||||
                state_class=STATE_CLASS_TOTAL_INCREASING,
 | 
			
		||||
            ),
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
    .extend(cv.polling_component_schema("60s"))
 | 
			
		||||
    .extend(uart.UART_DEVICE_SCHEMA)
 | 
			
		||||
)
 | 
			
		||||
CONFIG_SCHEMA = cv.Schema(
 | 
			
		||||
    {
 | 
			
		||||
        cv.GenerateID(): cv.declare_id(CSE7766Component),
 | 
			
		||||
        cv.Optional(CONF_VOLTAGE): sensor.sensor_schema(
 | 
			
		||||
            unit_of_measurement=UNIT_VOLT,
 | 
			
		||||
            accuracy_decimals=1,
 | 
			
		||||
            device_class=DEVICE_CLASS_VOLTAGE,
 | 
			
		||||
            state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_CURRENT): sensor.sensor_schema(
 | 
			
		||||
            unit_of_measurement=UNIT_AMPERE,
 | 
			
		||||
            accuracy_decimals=2,
 | 
			
		||||
            device_class=DEVICE_CLASS_CURRENT,
 | 
			
		||||
            state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_POWER): sensor.sensor_schema(
 | 
			
		||||
            unit_of_measurement=UNIT_WATT,
 | 
			
		||||
            accuracy_decimals=1,
 | 
			
		||||
            device_class=DEVICE_CLASS_POWER,
 | 
			
		||||
            state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_ENERGY): sensor.sensor_schema(
 | 
			
		||||
            unit_of_measurement=UNIT_WATT_HOURS,
 | 
			
		||||
            accuracy_decimals=3,
 | 
			
		||||
            device_class=DEVICE_CLASS_ENERGY,
 | 
			
		||||
            state_class=STATE_CLASS_TOTAL_INCREASING,
 | 
			
		||||
        ),
 | 
			
		||||
    }
 | 
			
		||||
).extend(uart.UART_DEVICE_SCHEMA)
 | 
			
		||||
FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema(
 | 
			
		||||
    "cse7766", baud_rate=4800, require_rx=True
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
@@ -168,10 +168,6 @@ bool IRAM_ATTR DallasTemperatureSensor::read_scratch_pad() {
 | 
			
		||||
    if (!wire->reset()) {
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  {
 | 
			
		||||
    InterruptLock lock;
 | 
			
		||||
 | 
			
		||||
    wire->select(this->address_);
 | 
			
		||||
    wire->write8(DALLAS_COMMAND_READ_SCRATCH_PAD);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,6 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome import automation
 | 
			
		||||
from esphome import core
 | 
			
		||||
from esphome.automation import maybe_simple_id
 | 
			
		||||
from esphome.const import CONF_ID
 | 
			
		||||
from esphome.components import uart
 | 
			
		||||
@@ -101,7 +100,7 @@ def range_segment_list(input):
 | 
			
		||||
 | 
			
		||||
    largest_distance = -1
 | 
			
		||||
    for distance in input:
 | 
			
		||||
        if isinstance(distance, core.Lambda):
 | 
			
		||||
        if isinstance(distance, cv.Lambda):
 | 
			
		||||
            continue
 | 
			
		||||
        m = cv.distance(distance)
 | 
			
		||||
        if m > 9:
 | 
			
		||||
@@ -128,14 +127,14 @@ MMWAVE_SETTINGS_SCHEMA = cv.Schema(
 | 
			
		||||
        cv.Optional(CONF_OUTPUT_LATENCY): {
 | 
			
		||||
            cv.Required(CONF_DELAY_AFTER_DETECT): cv.templatable(
 | 
			
		||||
                cv.All(
 | 
			
		||||
                    cv.positive_time_period,
 | 
			
		||||
                    cv.Range(max=core.TimePeriod(seconds=1638.375)),
 | 
			
		||||
                    cv.positive_time_period_milliseconds,
 | 
			
		||||
                    cv.Range(max=cv.TimePeriod(seconds=1638.375)),
 | 
			
		||||
                )
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Required(CONF_DELAY_AFTER_DISAPPEAR): cv.templatable(
 | 
			
		||||
                cv.All(
 | 
			
		||||
                    cv.positive_time_period,
 | 
			
		||||
                    cv.Range(max=core.TimePeriod(seconds=1638.375)),
 | 
			
		||||
                    cv.positive_time_period_milliseconds,
 | 
			
		||||
                    cv.Range(max=cv.TimePeriod(seconds=1638.375)),
 | 
			
		||||
                )
 | 
			
		||||
            ),
 | 
			
		||||
        },
 | 
			
		||||
 
 | 
			
		||||
Some files were not shown because too many files have changed in this diff Show More
		Reference in New Issue
	
	Block a user