mirror of
				https://github.com/esphome/esphome.git
				synced 2025-11-04 00:51:49 +00:00 
			
		
		
		
	Compare commits
	
		
			310 Commits
		
	
	
		
			oral-b-bru
			...
			2022.1.3
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					c0523590b4 | ||
| 
						 | 
					c7f091ab10 | ||
| 
						 | 
					7479e0aada | ||
| 
						 | 
					5bbee1a1fe | ||
| 
						 | 
					bdb9546ca3 | ||
| 
						 | 
					46af4cad6e | ||
| 
						 | 
					76a238912b | ||
| 
						 | 
					909a526967 | ||
| 
						 | 
					cd6f4fb93f | ||
| 
						 | 
					c19458696e | ||
| 
						 | 
					318b930e9f | ||
| 
						 | 
					9296a078a7 | ||
| 
						 | 
					5dc776e55f | ||
| 
						 | 
					72d60f30f7 | ||
| 
						 | 
					869743a742 | ||
| 
						 | 
					7b03e07908 | ||
| 
						 | 
					348f880e15 | ||
| 
						 | 
					ead597d0fb | ||
| 
						 | 
					afbf989715 | ||
| 
						 | 
					01b62a16c3 | ||
| 
						 | 
					c5eba04517 | ||
| 
						 | 
					282313ab52 | ||
| 
						 | 
					d274545e77 | ||
| 
						 | 
					d3fda37615 | ||
| 
						 | 
					cbe3092404 | ||
| 
						 | 
					6dfe3039d0 | ||
| 
						 | 
					d6009453df | ||
| 
						 | 
					c81323ef91 | ||
| 
						 | 
					961c27f1c2 | ||
| 
						 | 
					fe4a14e6cc | ||
| 
						 | 
					ee58ad1ac0 | ||
| 
						 | 
					c0ff899812 | ||
| 
						 | 
					d9c938de33 | ||
| 
						 | 
					56547b3d50 | ||
| 
						 | 
					5026bc7a78 | ||
| 
						 | 
					27364ee72c | ||
| 
						 | 
					ece71a0228 | ||
| 
						 | 
					073828235f | ||
| 
						 | 
					41bcc8c0f4 | ||
| 
						 | 
					a0ea2aae6e | ||
| 
						 | 
					f34b46a621 | ||
| 
						 | 
					7217a4f7a4 | ||
| 
						 | 
					6383eca54a | ||
| 
						 | 
					e55bd1e559 | ||
| 
						 | 
					9e8b701dea | ||
| 
						 | 
					a4431abea8 | ||
| 
						 | 
					5844c1767b | ||
| 
						 | 
					9a70bfa471 | ||
| 
						 | 
					b406c6403c | ||
| 
						 | 
					499625f266 | ||
| 
						 | 
					6b773553fc | ||
| 
						 | 
					15fe049a99 | ||
| 
						 | 
					e4555f6997 | ||
| 
						 | 
					470071e0b0 | ||
| 
						 | 
					ea1be8e7bf | ||
| 
						 | 
					84a830195f | ||
| 
						 | 
					e62c3e00c1 | ||
| 
						 | 
					07e790f900 | ||
| 
						 | 
					640142fc0c | ||
| 
						 | 
					5c339d4597 | ||
| 
						 | 
					a4931f5d78 | ||
| 
						 | 
					5e1e543b06 | ||
| 
						 | 
					df929f9445 | ||
| 
						 | 
					d8e719d1c4 | ||
| 
						 | 
					3067e482fc | ||
| 
						 | 
					ed5930e934 | ||
| 
						 | 
					ffea3597f4 | ||
| 
						 | 
					193d3e0206 | ||
| 
						 | 
					c8f4fbb7dd | ||
| 
						 | 
					c855bc31b4 | ||
| 
						 | 
					b924b179ab | ||
| 
						 | 
					3df0fee3de | ||
| 
						 | 
					b601560e81 | ||
| 
						 | 
					e5775cf812 | ||
| 
						 | 
					26dd1f8532 | ||
| 
						 | 
					5143a5b5c5 | ||
| 
						 | 
					15ce27992e | ||
| 
						 | 
					dbc2812022 | ||
| 
						 | 
					dce3713f12 | ||
| 
						 | 
					f849d45bb6 | ||
| 
						 | 
					8ad06fb9ea | ||
| 
						 | 
					9124d9d6e6 | ||
| 
						 | 
					45ebe51e4f | ||
| 
						 | 
					407661d56b | ||
| 
						 | 
					998d4229af | ||
| 
						 | 
					a02d2e2e11 | ||
| 
						 | 
					72fa68849f | ||
| 
						 | 
					33f17f75a0 | ||
| 
						 | 
					23edb18d7e | ||
| 
						 | 
					07ff3a853f | ||
| 
						 | 
					2cf36bdb46 | ||
| 
						 | 
					50848c2f4d | ||
| 
						 | 
					d32633b3c7 | ||
| 
						 | 
					b37739eec2 | ||
| 
						 | 
					28f87dc804 | ||
| 
						 | 
					41879e41e6 | ||
| 
						 | 
					fc0a6546a2 | ||
| 
						 | 
					ffd4280d6c | ||
| 
						 | 
					f859b346a6 | ||
| 
						 | 
					cb0677cafe | ||
| 
						 | 
					c6956527d1 | ||
| 
						 | 
					72c6bfaa50 | ||
| 
						 | 
					7927b5f624 | ||
| 
						 | 
					b7aad39daf | ||
| 
						 | 
					f48de6dd43 | ||
| 
						 | 
					79d73d8f8b | ||
| 
						 | 
					cc5947467f | ||
| 
						 | 
					e152f128c8 | ||
| 
						 | 
					99bd808ebe | ||
| 
						 | 
					beb5f3dc9d | ||
| 
						 | 
					f5c3b3446f | ||
| 
						 | 
					db3b955b0f | ||
| 
						 | 
					5516f65971 | ||
| 
						 | 
					9471df0a1b | ||
| 
						 | 
					6d39f64be7 | ||
| 
						 | 
					b89d0a9a73 | ||
| 
						 | 
					4bb779d9a5 | ||
| 
						 | 
					386a5b6362 | ||
| 
						 | 
					e32a999cd0 | ||
| 
						 | 
					bfbc6a4bad | ||
| 
						 | 
					8c9e0e552d | ||
| 
						 | 
					8aaf9fd83f | ||
| 
						 | 
					08057720b8 | ||
| 
						 | 
					bfaa648837 | ||
| 
						 | 
					d504daef91 | ||
| 
						 | 
					b8d3ef2f49 | ||
| 
						 | 
					3bf6320030 | ||
| 
						 | 
					708b928c73 | ||
| 
						 | 
					649366ff44 | ||
| 
						 | 
					e5c9e87fad | ||
| 
						 | 
					f3d9d707b6 | ||
| 
						 | 
					090e10730c | ||
| 
						 | 
					fbc84861c7 | ||
| 
						 | 
					e763469af8 | ||
| 
						 | 
					3c0c514e44 | ||
| 
						 | 
					ed5e2dd332 | ||
| 
						 | 
					09b7c6f550 | ||
| 
						 | 
					df315a1f51 | ||
| 
						 | 
					7ee4bb621c | ||
| 
						 | 
					24874f4c3c | ||
| 
						 | 
					c128880033 | ||
| 
						 | 
					a66e94a0b0 | ||
| 
						 | 
					56870ed4a8 | ||
| 
						 | 
					3ac720df47 | ||
| 
						 | 
					1bc757ad06 | ||
| 
						 | 
					f72abc6f3d | ||
| 
						 | 
					5ac88de985 | ||
| 
						 | 
					0826b367d6 | ||
| 
						 | 
					329bf861d6 | ||
| 
						 | 
					9dcd3d18a0 | ||
| 
						 | 
					db66cd88b6 | ||
| 
						 | 
					86c205fe43 | ||
| 
						 | 
					c6414138c7 | ||
| 
						 | 
					36b355eb82 | ||
| 
						 | 
					7be9291b13 | ||
| 
						 | 
					ea9e75039b | ||
| 
						 | 
					a5fb036011 | ||
| 
						 | 
					e55506f9db | ||
| 
						 | 
					50ec1d0445 | ||
| 
						 | 
					3d5e1d8d91 | ||
| 
						 | 
					db2128a344 | ||
| 
						 | 
					21db43db06 | ||
| 
						 | 
					5009b3029f | ||
| 
						 | 
					57a029189c | ||
| 
						 | 
					0cb715bb76 | ||
| 
						 | 
					7d03823afd | ||
| 
						 | 
					8e1c9f5042 | ||
| 
						 | 
					980b7cda8f | ||
| 
						 | 
					3a72dd5cb6 | ||
| 
						 | 
					3178243811 | ||
| 
						 | 
					d30e2f2a4f | ||
| 
						 | 
					6226dae05c | ||
| 
						 | 
					9c6a475a6e | ||
| 
						 | 
					8294d10d5b | ||
| 
						 | 
					67558bec47 | ||
| 
						 | 
					84873d4074 | ||
| 
						 | 
					58a0b28a39 | ||
| 
						 | 
					b37d3a66cc | ||
| 
						 | 
					7e495a5e27 | ||
| 
						 | 
					c41547fd4a | ||
| 
						 | 
					0d47d41c85 | ||
| 
						 | 
					41a3a17456 | ||
| 
						 | 
					cbbafbcca2 | ||
| 
						 | 
					c75566b374 | ||
| 
						 | 
					7279f1fcc1 | ||
| 
						 | 
					d7432f7c10 | ||
| 
						 | 
					b0a0a153f3 | ||
| 
						 | 
					024632dbd0 | ||
| 
						 | 
					0a545a28b9 | ||
| 
						 | 
					0f2df59998 | ||
| 
						 | 
					29a7d32f77 | ||
| 
						 | 
					687a7e9b2f | ||
| 
						 | 
					09e8782318 | ||
| 
						 | 
					f2aea02210 | ||
| 
						 | 
					194f922312 | ||
| 
						 | 
					fea3c48098 | ||
| 
						 | 
					c2f57baec2 | ||
| 
						 | 
					f4a140e126 | ||
| 
						 | 
					ab506b09fe | ||
| 
						 | 
					87e1cdeedb | ||
| 
						 | 
					81a36146ef | ||
| 
						 | 
					7fa4a68a27 | ||
| 
						 | 
					f1c5e2ef81 | ||
| 
						 | 
					b526155cce | ||
| 
						 | 
					62c3f301e7 | ||
| 
						 | 
					38cb988809 | ||
| 
						 | 
					b976ac54c8 | ||
| 
						 | 
					78026e766f | ||
| 
						 | 
					b4cd8d21a5 | ||
| 
						 | 
					7552893311 | ||
| 
						 | 
					21c896d8f8 | ||
| 
						 | 
					4b7fe202ec | ||
| 
						 | 
					9f4519210f | ||
| 
						 | 
					b0506afa5b | ||
| 
						 | 
					8cbb379898 | ||
| 
						 | 
					b226215593 | ||
| 
						 | 
					19970729a9 | ||
| 
						 | 
					d2ebfd2833 | ||
| 
						 | 
					bd782fc828 | ||
| 
						 | 
					23560e608c | ||
| 
						 | 
					f1377b560e | ||
| 
						 | 
					72108684ea | ||
| 
						 | 
					c6adaaea97 | ||
| 
						 | 
					91999a38ca | ||
| 
						 | 
					b34eed125d | ||
| 
						 | 
					2abe09529a | ||
| 
						 | 
					9aaaf4dd4b | ||
| 
						 | 
					cbfbcf7f1b | ||
| 
						 | 
					c7651dc40d | ||
| 
						 | 
					eda1c471ad | ||
| 
						 | 
					c7ef18fbc4 | ||
| 
						 | 
					901ec918b1 | ||
| 
						 | 
					6bdae55ee1 | ||
| 
						 | 
					dfb96e4b7f | ||
| 
						 | 
					ff2c316b18 | ||
| 
						 | 
					5be52f71f9 | ||
| 
						 | 
					42873dd37c | ||
| 
						 | 
					f93e7d4e3a | ||
| 
						 | 
					bbcd523967 | ||
| 
						 | 
					68cbe58d00 | ||
| 
						 | 
					115bca98f1 | ||
| 
						 | 
					ed0b34b2fe | ||
| 
						 | 
					ab34401421 | ||
| 
						 | 
					eed0c18d65 | ||
| 
						 | 
					e5a38ce748 | ||
| 
						 | 
					7d9d9fcf36 | ||
| 
						 | 
					f0aba6ceb2 | ||
| 
						 | 
					ab07ee57c6 | ||
| 
						 | 
					eae3d72a4d | ||
| 
						 | 
					7b8d826704 | ||
| 
						 | 
					e7baa42e63 | ||
| 
						 | 
					2f32833a22 | ||
| 
						 | 
					f6935a4b4b | ||
| 
						 | 
					332c9e891b | ||
| 
						 | 
					b91ee4847f | ||
| 
						 | 
					625463d871 | ||
| 
						 | 
					6433a01e07 | ||
| 
						 | 
					56cc31e8e7 | ||
| 
						 | 
					3af297aa76 | ||
| 
						 | 
					996ec59d28 | ||
| 
						 | 
					95593eeeab | ||
| 
						 | 
					dad244fb7a | ||
| 
						 | 
					adb5d27d95 | ||
| 
						 | 
					8456a8cecb | ||
| 
						 | 
					b9f66373c1 | ||
| 
						 | 
					9ac365feef | ||
| 
						 | 
					43bbd58a44 | ||
| 
						 | 
					7feffa64f3 | ||
| 
						 | 
					ea0977abb4 | ||
| 
						 | 
					4c83dc7c28 | ||
| 
						 | 
					e10ab1da78 | ||
| 
						 | 
					1b0e60374b | ||
| 
						 | 
					3a760fbb44 | ||
| 
						 | 
					6ef57a2973 | ||
| 
						 | 
					3e9c7f2e9f | ||
| 
						 | 
					430598b7a1 | ||
| 
						 | 
					91611b09b4 | ||
| 
						 | 
					ecd115851f | ||
| 
						 | 
					4a1e50fed1 | ||
| 
						 | 
					d6d037047b | ||
| 
						 | 
					b5734c2b20 | ||
| 
						 | 
					723fb7eaac | ||
| 
						 | 
					63a9acaa19 | ||
| 
						 | 
					0524f8c677 | ||
| 
						 | 
					70b62f272e | ||
| 
						 | 
					f0089b7940 | ||
| 
						 | 
					4b44280d53 | ||
| 
						 | 
					f045382d20 | ||
| 
						 | 
					db3fa1ade7 | ||
| 
						 | 
					f83950fd75 | ||
| 
						 | 
					4dd1bf920d | ||
| 
						 | 
					98755f3621 | ||
| 
						 | 
					c3a8a044b9 | ||
| 
						 | 
					15b5ea43a7 | ||
| 
						 | 
					ec683fc227 | ||
| 
						 | 
					d4e65eb82a | ||
| 
						 | 
					10c6601b0a | ||
| 
						 | 
					73940bc1bd | ||
| 
						 | 
					9b7fb829f9 | ||
| 
						 | 
					c51d8c9021 | ||
| 
						 | 
					d8a6dfe5ce | ||
| 
						 | 
					5f7cef0b06 | ||
| 
						 | 
					48ff2ffc68 | ||
| 
						 | 
					b3b9ccd314 | ||
| 
						 | 
					e63c7b483b | ||
| 
						 | 
					f57980b069 | ||
| 
						 | 
					7006aa0d2a | ||
| 
						 | 
					8051c1ca99 | ||
| 
						 | 
					a779592414 | ||
| 
						 | 
					112215848d | 
							
								
								
									
										20
									
								
								.github/workflows/ci.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										20
									
								
								.github/workflows/ci.yml
									
									
									
									
										vendored
									
									
								
							@@ -51,26 +51,26 @@ jobs:
 | 
			
		||||
            name: Run script/clang-format
 | 
			
		||||
          - id: clang-tidy
 | 
			
		||||
            name: Run script/clang-tidy for ESP8266
 | 
			
		||||
            options: --environment esp8266-tidy --grep USE_ESP8266
 | 
			
		||||
            options: --environment esp8266-arduino-tidy --grep USE_ESP8266
 | 
			
		||||
            pio_cache_key: tidyesp8266
 | 
			
		||||
          - id: clang-tidy
 | 
			
		||||
            name: Run script/clang-tidy for ESP32 1/4
 | 
			
		||||
            options: --environment esp32-tidy --split-num 4 --split-at 1
 | 
			
		||||
            name: Run script/clang-tidy for ESP32 Arduino 1/4
 | 
			
		||||
            options: --environment esp32-arduino-tidy --split-num 4 --split-at 1
 | 
			
		||||
            pio_cache_key: tidyesp32
 | 
			
		||||
          - id: clang-tidy
 | 
			
		||||
            name: Run script/clang-tidy for ESP32 2/4
 | 
			
		||||
            options: --environment esp32-tidy --split-num 4 --split-at 2
 | 
			
		||||
            name: Run script/clang-tidy for ESP32 Arduino 2/4
 | 
			
		||||
            options: --environment esp32-arduino-tidy --split-num 4 --split-at 2
 | 
			
		||||
            pio_cache_key: tidyesp32
 | 
			
		||||
          - id: clang-tidy
 | 
			
		||||
            name: Run script/clang-tidy for ESP32 3/4
 | 
			
		||||
            options: --environment esp32-tidy --split-num 4 --split-at 3
 | 
			
		||||
            name: Run script/clang-tidy for ESP32 Arduino 3/4
 | 
			
		||||
            options: --environment esp32-arduino-tidy --split-num 4 --split-at 3
 | 
			
		||||
            pio_cache_key: tidyesp32
 | 
			
		||||
          - id: clang-tidy
 | 
			
		||||
            name: Run script/clang-tidy for ESP32 4/4
 | 
			
		||||
            options: --environment esp32-tidy --split-num 4 --split-at 4
 | 
			
		||||
            name: Run script/clang-tidy for ESP32 Arduino 4/4
 | 
			
		||||
            options: --environment esp32-arduino-tidy --split-num 4 --split-at 4
 | 
			
		||||
            pio_cache_key: tidyesp32
 | 
			
		||||
          - id: clang-tidy
 | 
			
		||||
            name: Run script/clang-tidy for ESP32 esp-idf
 | 
			
		||||
            name: Run script/clang-tidy for ESP32 IDF
 | 
			
		||||
            options: --environment esp32-idf-tidy --grep USE_ESP_IDF
 | 
			
		||||
            pio_cache_key: tidyesp32-idf
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										10
									
								
								CODEOWNERS
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								CODEOWNERS
									
									
									
									
									
								
							@@ -28,13 +28,16 @@ esphome/components/b_parasite/* @rbaron
 | 
			
		||||
esphome/components/ballu/* @bazuchan
 | 
			
		||||
esphome/components/bang_bang/* @OttoWinter
 | 
			
		||||
esphome/components/binary_sensor/* @esphome/core
 | 
			
		||||
esphome/components/bl0940/* @tobias-
 | 
			
		||||
esphome/components/ble_client/* @buxtronix
 | 
			
		||||
esphome/components/bme680_bsec/* @trvrnrth
 | 
			
		||||
esphome/components/bmp3xx/* @martgras
 | 
			
		||||
esphome/components/button/* @esphome/core
 | 
			
		||||
esphome/components/canbus/* @danielschramm @mvturnho
 | 
			
		||||
esphome/components/cap1188/* @MrEditor97
 | 
			
		||||
esphome/components/captive_portal/* @OttoWinter
 | 
			
		||||
esphome/components/ccs811/* @habbie
 | 
			
		||||
esphome/components/cd74hc4067/* @asoehlke
 | 
			
		||||
esphome/components/climate/* @esphome/core
 | 
			
		||||
esphome/components/climate_ir/* @glmnet
 | 
			
		||||
esphome/components/color_temperature/* @jesserockz
 | 
			
		||||
@@ -55,6 +58,7 @@ esphome/components/esp32/* @esphome/core
 | 
			
		||||
esphome/components/esp32_ble/* @jesserockz
 | 
			
		||||
esphome/components/esp32_ble_server/* @jesserockz
 | 
			
		||||
esphome/components/esp32_camera_web_server/* @ayufan
 | 
			
		||||
esphome/components/esp32_can/* @Sympatron
 | 
			
		||||
esphome/components/esp32_improv/* @jesserockz
 | 
			
		||||
esphome/components/esp8266/* @esphome/core
 | 
			
		||||
esphome/components/exposure_notifications/* @OttoWinter
 | 
			
		||||
@@ -75,11 +79,13 @@ esphome/components/homeassistant/* @OttoWinter
 | 
			
		||||
esphome/components/hrxl_maxsonar_wr/* @netmikey
 | 
			
		||||
esphome/components/i2c/* @esphome/core
 | 
			
		||||
esphome/components/improv_serial/* @esphome/core
 | 
			
		||||
esphome/components/ina260/* @MrEditor97
 | 
			
		||||
esphome/components/inkbird_ibsth1_mini/* @fkirill
 | 
			
		||||
esphome/components/inkplate6/* @jesserockz
 | 
			
		||||
esphome/components/integration/* @OttoWinter
 | 
			
		||||
esphome/components/interval/* @esphome/core
 | 
			
		||||
esphome/components/json/* @OttoWinter
 | 
			
		||||
esphome/components/kalman_combinator/* @Cat-Ion
 | 
			
		||||
esphome/components/ledc/* @OttoWinter
 | 
			
		||||
esphome/components/light/* @esphome/core
 | 
			
		||||
esphome/components/logger/* @esphome/core
 | 
			
		||||
@@ -93,10 +99,13 @@ esphome/components/mcp23x08_base/* @jesserockz
 | 
			
		||||
esphome/components/mcp23x17_base/* @jesserockz
 | 
			
		||||
esphome/components/mcp23xxx_base/* @jesserockz
 | 
			
		||||
esphome/components/mcp2515/* @danielschramm @mvturnho
 | 
			
		||||
esphome/components/mcp3204/* @rsumner
 | 
			
		||||
esphome/components/mcp47a1/* @jesserockz
 | 
			
		||||
esphome/components/mcp9808/* @k7hpn
 | 
			
		||||
esphome/components/md5/* @esphome/core
 | 
			
		||||
esphome/components/mdns/* @esphome/core
 | 
			
		||||
esphome/components/midea/* @dudanov
 | 
			
		||||
esphome/components/midea_ir/* @dudanov
 | 
			
		||||
esphome/components/mitsubishi/* @RubyBailey
 | 
			
		||||
esphome/components/modbus_controller/* @martgras
 | 
			
		||||
esphome/components/modbus_controller/binary_sensor/* @martgras
 | 
			
		||||
@@ -124,6 +133,7 @@ esphome/components/pn532_i2c/* @OttoWinter @jesserockz
 | 
			
		||||
esphome/components/pn532_spi/* @OttoWinter @jesserockz
 | 
			
		||||
esphome/components/power_supply/* @esphome/core
 | 
			
		||||
esphome/components/preferences/* @esphome/core
 | 
			
		||||
esphome/components/psram/* @esphome/core
 | 
			
		||||
esphome/components/pulse_meter/* @stevebaxter
 | 
			
		||||
esphome/components/pvvx_mithermometer/* @pasiz
 | 
			
		||||
esphome/components/rc522/* @glmnet
 | 
			
		||||
 
 | 
			
		||||
@@ -4,4 +4,5 @@ include requirements.txt
 | 
			
		||||
include esphome/dashboard/templates/*.html
 | 
			
		||||
recursive-include esphome/dashboard/static *.ico *.js *.css *.woff* LICENSE
 | 
			
		||||
recursive-include esphome *.cpp *.h *.tcc
 | 
			
		||||
recursive-include esphome *.py.script
 | 
			
		||||
recursive-include esphome LICENSE.txt
 | 
			
		||||
 
 | 
			
		||||
@@ -5,12 +5,12 @@
 | 
			
		||||
# One of "docker", "hassio"
 | 
			
		||||
ARG BASEIMGTYPE=docker
 | 
			
		||||
 | 
			
		||||
FROM ghcr.io/hassio-addons/debian-base/amd64:5.1.1 AS base-hassio-amd64
 | 
			
		||||
FROM ghcr.io/hassio-addons/debian-base/aarch64:5.1.1 AS base-hassio-arm64
 | 
			
		||||
FROM ghcr.io/hassio-addons/debian-base/armv7:5.1.1 AS base-hassio-armv7
 | 
			
		||||
FROM debian:bullseye-20211011-slim AS base-docker-amd64
 | 
			
		||||
FROM debian:bullseye-20211011-slim AS base-docker-arm64
 | 
			
		||||
FROM debian:bullseye-20211011-slim AS base-docker-armv7
 | 
			
		||||
FROM ghcr.io/hassio-addons/debian-base/amd64:5.2.3 AS base-hassio-amd64
 | 
			
		||||
FROM ghcr.io/hassio-addons/debian-base/aarch64:5.2.3 AS base-hassio-arm64
 | 
			
		||||
FROM ghcr.io/hassio-addons/debian-base/armv7:5.2.3 AS base-hassio-armv7
 | 
			
		||||
FROM debian:bullseye-20211220-slim AS base-docker-amd64
 | 
			
		||||
FROM debian:bullseye-20211220-slim AS base-docker-arm64
 | 
			
		||||
FROM debian:bullseye-20211220-slim AS base-docker-armv7
 | 
			
		||||
 | 
			
		||||
# Use TARGETARCH/TARGETVARIANT defined by docker
 | 
			
		||||
# https://docs.docker.com/engine/reference/builder/#automatic-platform-args-in-the-global-scope
 | 
			
		||||
@@ -27,7 +27,7 @@ RUN \
 | 
			
		||||
        python3-cryptography=3.3.2-1 \
 | 
			
		||||
        iputils-ping=3:20210202-1 \
 | 
			
		||||
        git=1:2.30.2-1 \
 | 
			
		||||
        curl=7.74.0-1.3+b1 \
 | 
			
		||||
        curl=7.74.0-1.3+deb11u1 \
 | 
			
		||||
    && rm -rf \
 | 
			
		||||
        /tmp/* \
 | 
			
		||||
        /var/{cache,log}/* \
 | 
			
		||||
@@ -42,8 +42,8 @@ ENV \
 | 
			
		||||
RUN \
 | 
			
		||||
    # Ubuntu python3-pip is missing wheel
 | 
			
		||||
    pip3 install --no-cache-dir \
 | 
			
		||||
        wheel==0.36.2 \
 | 
			
		||||
        platformio==5.2.2 \
 | 
			
		||||
        wheel==0.37.1 \
 | 
			
		||||
        platformio==5.2.4 \
 | 
			
		||||
    # Change some platformio settings
 | 
			
		||||
    && platformio settings set enable_telemetry No \
 | 
			
		||||
    && platformio settings set check_libraries_interval 1000000 \
 | 
			
		||||
@@ -64,7 +64,7 @@ RUN \
 | 
			
		||||
 | 
			
		||||
# Copy esphome and install
 | 
			
		||||
COPY . /esphome
 | 
			
		||||
RUN pip3 install --no-cache-dir -e /esphome
 | 
			
		||||
RUN pip3 install --no-cache-dir --no-use-pep517 -e /esphome
 | 
			
		||||
 | 
			
		||||
# Settings for dashboard
 | 
			
		||||
ENV USERNAME="" PASSWORD=""
 | 
			
		||||
@@ -112,7 +112,7 @@ RUN \
 | 
			
		||||
 | 
			
		||||
# Copy esphome and install
 | 
			
		||||
COPY . /esphome
 | 
			
		||||
RUN pip3 install --no-cache-dir -e /esphome
 | 
			
		||||
RUN pip3 install --no-cache-dir --no-use-pep517 -e /esphome
 | 
			
		||||
 | 
			
		||||
# Labels
 | 
			
		||||
LABEL \
 | 
			
		||||
 
 | 
			
		||||
@@ -32,6 +32,7 @@ parser.add_argument("--dry-run", action="store_true", help="Don't run any comman
 | 
			
		||||
subparsers = parser.add_subparsers(help="Action to perform", dest="command", required=True)
 | 
			
		||||
build_parser = subparsers.add_parser("build", help="Build the image")
 | 
			
		||||
build_parser.add_argument("--push", help="Also push the images", action="store_true")
 | 
			
		||||
build_parser.add_argument("--load", help="Load the docker image locally", action="store_true")
 | 
			
		||||
manifest_parser = subparsers.add_parser("manifest", help="Create a manifest from already pushed images")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -132,6 +133,8 @@ def main():
 | 
			
		||||
            cmd += ["--tag", img]
 | 
			
		||||
        if args.push:
 | 
			
		||||
            cmd += ["--push", "--cache-to", f"type=registry,ref={cache_img},mode=max"]
 | 
			
		||||
        if args.load:
 | 
			
		||||
            cmd += ["--load"]
 | 
			
		||||
 | 
			
		||||
        run_command(*cmd, ".")
 | 
			
		||||
    elif args.command == "manifest":
 | 
			
		||||
 
 | 
			
		||||
@@ -75,8 +75,7 @@ from esphome.cpp_types import (  # noqa
 | 
			
		||||
    optional,
 | 
			
		||||
    arduino_json_ns,
 | 
			
		||||
    JsonObject,
 | 
			
		||||
    JsonObjectRef,
 | 
			
		||||
    JsonObjectConstRef,
 | 
			
		||||
    JsonObjectConst,
 | 
			
		||||
    Controller,
 | 
			
		||||
    GPIOPin,
 | 
			
		||||
    InternalGPIOPin,
 | 
			
		||||
 
 | 
			
		||||
@@ -76,6 +76,8 @@ async def to_code(config):
 | 
			
		||||
        pos = 0
 | 
			
		||||
        for frameIndex in range(frames):
 | 
			
		||||
            image.seek(frameIndex)
 | 
			
		||||
            if CONF_RESIZE in config:
 | 
			
		||||
                image.thumbnail(config[CONF_RESIZE])
 | 
			
		||||
            frame = image.convert("RGB")
 | 
			
		||||
            if CONF_RESIZE in config:
 | 
			
		||||
                frame = frame.resize([width, height])
 | 
			
		||||
 
 | 
			
		||||
@@ -20,6 +20,7 @@ namespace esphome {
 | 
			
		||||
namespace api {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "api.connection";
 | 
			
		||||
static const int ESP32_CAMERA_STOP_STREAM = 5000;
 | 
			
		||||
 | 
			
		||||
APIConnection::APIConnection(std::unique_ptr<socket::Socket> sock, APIServer *parent)
 | 
			
		||||
    : parent_(parent), initial_state_iterator_(parent, this), list_entities_iterator_(parent, this) {
 | 
			
		||||
@@ -704,7 +705,9 @@ void APIConnection::send_camera_state(std::shared_ptr<esp32_camera::CameraImage>
 | 
			
		||||
    return;
 | 
			
		||||
  if (this->image_reader_.available())
 | 
			
		||||
    return;
 | 
			
		||||
  this->image_reader_.set_image(std::move(image));
 | 
			
		||||
  if (image->was_requested_by(esphome::esp32_camera::API_REQUESTER) ||
 | 
			
		||||
      image->was_requested_by(esphome::esp32_camera::IDLE))
 | 
			
		||||
    this->image_reader_.set_image(std::move(image));
 | 
			
		||||
}
 | 
			
		||||
bool APIConnection::send_camera_info(esp32_camera::ESP32Camera *camera) {
 | 
			
		||||
  ListEntitiesCameraResponse msg;
 | 
			
		||||
@@ -722,9 +725,14 @@ void APIConnection::camera_image(const CameraImageRequest &msg) {
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  if (msg.single)
 | 
			
		||||
    esp32_camera::global_esp32_camera->request_image();
 | 
			
		||||
  if (msg.stream)
 | 
			
		||||
    esp32_camera::global_esp32_camera->request_stream();
 | 
			
		||||
    esp32_camera::global_esp32_camera->request_image(esphome::esp32_camera::API_REQUESTER);
 | 
			
		||||
  if (msg.stream) {
 | 
			
		||||
    esp32_camera::global_esp32_camera->start_stream(esphome::esp32_camera::API_REQUESTER);
 | 
			
		||||
 | 
			
		||||
    App.scheduler.set_timeout(this->parent_, "api_esp32_camera_stop_stream", ESP32_CAMERA_STOP_STREAM, []() {
 | 
			
		||||
      esp32_camera::global_esp32_camera->stop_stream(esphome::esp32_camera::API_REQUESTER);
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
#include "api_frame_helper.h"
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
#include "esphome/core/hal.h"
 | 
			
		||||
#include "esphome/core/helpers.h"
 | 
			
		||||
#include "proto.h"
 | 
			
		||||
#include <cstring>
 | 
			
		||||
@@ -721,7 +722,12 @@ APIError APINoiseFrameHelper::shutdown(int how) {
 | 
			
		||||
}
 | 
			
		||||
extern "C" {
 | 
			
		||||
// declare how noise generates random bytes (here with a good HWRNG based on the RF system)
 | 
			
		||||
void noise_rand_bytes(void *output, size_t len) { esphome::fill_random(reinterpret_cast<uint8_t *>(output), len); }
 | 
			
		||||
void noise_rand_bytes(void *output, size_t len) {
 | 
			
		||||
  if (!esphome::random_bytes(reinterpret_cast<uint8_t *>(output), len)) {
 | 
			
		||||
    ESP_LOGE(TAG, "Failed to acquire random bytes, rebooting!");
 | 
			
		||||
    arch_restart();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
#endif  // USE_API_NOISE
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -12,10 +12,10 @@ template<typename... X> class TemplatableStringValue : public TemplatableValue<s
 | 
			
		||||
 public:
 | 
			
		||||
  TemplatableStringValue() : TemplatableValue<std::string, X...>() {}
 | 
			
		||||
 | 
			
		||||
  template<typename F, enable_if_t<!is_callable<F, X...>::value, int> = 0>
 | 
			
		||||
  template<typename F, enable_if_t<!is_invocable<F, X...>::value, int> = 0>
 | 
			
		||||
  TemplatableStringValue(F value) : TemplatableValue<std::string, X...>(value) {}
 | 
			
		||||
 | 
			
		||||
  template<typename F, enable_if_t<is_callable<F, X...>::value, int> = 0>
 | 
			
		||||
  template<typename F, enable_if_t<is_invocable<F, X...>::value, int> = 0>
 | 
			
		||||
  TemplatableStringValue(F f)
 | 
			
		||||
      : TemplatableValue<std::string, X...>([f](X... x) -> std::string { return to_string(f(x...)); }) {}
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
@@ -45,6 +45,8 @@ bool ATCMiThermometer::parse_device(const esp32_ble_tracker::ESPBTDevice &device
 | 
			
		||||
      this->battery_voltage_->publish_state(*res->battery_voltage);
 | 
			
		||||
    success = true;
 | 
			
		||||
  }
 | 
			
		||||
  if (this->signal_strength_ != nullptr)
 | 
			
		||||
    this->signal_strength_->publish_state(device.get_rssi());
 | 
			
		||||
 | 
			
		||||
  return success;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -28,6 +28,7 @@ class ATCMiThermometer : public Component, public esp32_ble_tracker::ESPBTDevice
 | 
			
		||||
  void set_humidity(sensor::Sensor *humidity) { humidity_ = humidity; }
 | 
			
		||||
  void set_battery_level(sensor::Sensor *battery_level) { battery_level_ = battery_level; }
 | 
			
		||||
  void set_battery_voltage(sensor::Sensor *battery_voltage) { battery_voltage_ = battery_voltage; }
 | 
			
		||||
  void set_signal_strength(sensor::Sensor *signal_strength) { signal_strength_ = signal_strength; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  uint64_t address_;
 | 
			
		||||
@@ -35,6 +36,7 @@ class ATCMiThermometer : public Component, public esp32_ble_tracker::ESPBTDevice
 | 
			
		||||
  sensor::Sensor *humidity_{nullptr};
 | 
			
		||||
  sensor::Sensor *battery_level_{nullptr};
 | 
			
		||||
  sensor::Sensor *battery_voltage_{nullptr};
 | 
			
		||||
  sensor::Sensor *signal_strength_{nullptr};
 | 
			
		||||
 | 
			
		||||
  optional<ParseResult> parse_header_(const esp32_ble_tracker::ServiceData &service_data);
 | 
			
		||||
  bool parse_message_(const std::vector<uint8_t> &message, ParseResult &result);
 | 
			
		||||
 
 | 
			
		||||
@@ -6,15 +6,18 @@ from esphome.const import (
 | 
			
		||||
    CONF_BATTERY_VOLTAGE,
 | 
			
		||||
    CONF_MAC_ADDRESS,
 | 
			
		||||
    CONF_HUMIDITY,
 | 
			
		||||
    CONF_SIGNAL_STRENGTH,
 | 
			
		||||
    CONF_TEMPERATURE,
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    DEVICE_CLASS_BATTERY,
 | 
			
		||||
    DEVICE_CLASS_HUMIDITY,
 | 
			
		||||
    DEVICE_CLASS_SIGNAL_STRENGTH,
 | 
			
		||||
    DEVICE_CLASS_TEMPERATURE,
 | 
			
		||||
    DEVICE_CLASS_VOLTAGE,
 | 
			
		||||
    ENTITY_CATEGORY_DIAGNOSTIC,
 | 
			
		||||
    STATE_CLASS_MEASUREMENT,
 | 
			
		||||
    UNIT_CELSIUS,
 | 
			
		||||
    UNIT_DECIBEL_MILLIWATT,
 | 
			
		||||
    UNIT_PERCENT,
 | 
			
		||||
    UNIT_VOLT,
 | 
			
		||||
)
 | 
			
		||||
@@ -59,6 +62,13 @@ CONFIG_SCHEMA = (
 | 
			
		||||
                state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
                entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_SIGNAL_STRENGTH): sensor.sensor_schema(
 | 
			
		||||
                unit_of_measurement=UNIT_DECIBEL_MILLIWATT,
 | 
			
		||||
                accuracy_decimals=0,
 | 
			
		||||
                device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
 | 
			
		||||
                state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
                entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
 | 
			
		||||
            ),
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
    .extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
 | 
			
		||||
@@ -85,3 +95,6 @@ async def to_code(config):
 | 
			
		||||
    if CONF_BATTERY_VOLTAGE in config:
 | 
			
		||||
        sens = await sensor.new_sensor(config[CONF_BATTERY_VOLTAGE])
 | 
			
		||||
        cg.add(var.set_battery_voltage(sens))
 | 
			
		||||
    if CONF_SIGNAL_STRENGTH in config:
 | 
			
		||||
        sens = await sensor.new_sensor(config[CONF_SIGNAL_STRENGTH])
 | 
			
		||||
        cg.add(var.set_signal_strength(sens))
 | 
			
		||||
 
 | 
			
		||||
@@ -80,21 +80,23 @@ void BangBangClimate::compute_state_() {
 | 
			
		||||
 | 
			
		||||
  climate::ClimateAction target_action;
 | 
			
		||||
  if (too_cold) {
 | 
			
		||||
    // too cold -> enable heating if possible, else idle
 | 
			
		||||
    if (this->supports_heat_)
 | 
			
		||||
    // too cold -> enable heating if possible and enabled, else idle
 | 
			
		||||
    if (this->supports_heat_ &&
 | 
			
		||||
        (this->mode == climate::CLIMATE_MODE_HEAT_COOL || this->mode == climate::CLIMATE_MODE_HEAT))
 | 
			
		||||
      target_action = climate::CLIMATE_ACTION_HEATING;
 | 
			
		||||
    else
 | 
			
		||||
      target_action = climate::CLIMATE_ACTION_IDLE;
 | 
			
		||||
  } else if (too_hot) {
 | 
			
		||||
    // too hot -> enable cooling if possible, else idle
 | 
			
		||||
    if (this->supports_cool_)
 | 
			
		||||
    // too hot -> enable cooling if possible and enabled, else idle
 | 
			
		||||
    if (this->supports_cool_ &&
 | 
			
		||||
        (this->mode == climate::CLIMATE_MODE_HEAT_COOL || this->mode == climate::CLIMATE_MODE_COOL))
 | 
			
		||||
      target_action = climate::CLIMATE_ACTION_COOLING;
 | 
			
		||||
    else
 | 
			
		||||
      target_action = climate::CLIMATE_ACTION_IDLE;
 | 
			
		||||
  } else {
 | 
			
		||||
    // neither too hot nor too cold -> in range
 | 
			
		||||
    if (this->supports_cool_ && this->supports_heat_) {
 | 
			
		||||
      // if supports both ends, go to idle action
 | 
			
		||||
    if (this->supports_cool_ && this->supports_heat_ && this->mode == climate::CLIMATE_MODE_HEAT_COOL) {
 | 
			
		||||
      // if supports both ends and both cooling and heating enabled, go to idle action
 | 
			
		||||
      target_action = climate::CLIMATE_ACTION_IDLE;
 | 
			
		||||
    } else {
 | 
			
		||||
      // else use current mode and don't change (hysteresis)
 | 
			
		||||
 
 | 
			
		||||
@@ -48,7 +48,10 @@ void BinarySensor::set_device_class(const std::string &device_class) { this->dev
 | 
			
		||||
std::string BinarySensor::get_device_class() {
 | 
			
		||||
  if (this->device_class_.has_value())
 | 
			
		||||
    return *this->device_class_;
 | 
			
		||||
#pragma GCC diagnostic push
 | 
			
		||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
 | 
			
		||||
  return this->device_class();
 | 
			
		||||
#pragma GCC diagnostic pop
 | 
			
		||||
}
 | 
			
		||||
void BinarySensor::add_filter(Filter *filter) {
 | 
			
		||||
  filter->parent_ = this;
 | 
			
		||||
 
 | 
			
		||||
@@ -74,7 +74,10 @@ class BinarySensor : public EntityBase {
 | 
			
		||||
 | 
			
		||||
  // ========== OVERRIDE METHODS ==========
 | 
			
		||||
  // (You'll only need this when creating your own custom binary sensor)
 | 
			
		||||
  /// Get the default device class for this sensor, or empty string for no default.
 | 
			
		||||
  /** Override this to set the default device class.
 | 
			
		||||
   *
 | 
			
		||||
   * @deprecated This method is deprecated, set the property during config validation instead. (2022.1)
 | 
			
		||||
   */
 | 
			
		||||
  virtual std::string device_class();
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										1
									
								
								esphome/components/bl0940/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								esphome/components/bl0940/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
CODEOWNERS = ["@tobias-"]
 | 
			
		||||
							
								
								
									
										137
									
								
								esphome/components/bl0940/bl0940.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										137
									
								
								esphome/components/bl0940/bl0940.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,137 @@
 | 
			
		||||
#include "bl0940.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace bl0940 {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "bl0940";
 | 
			
		||||
 | 
			
		||||
static const uint8_t BL0940_READ_COMMAND = 0x50;  // 0x58 according to documentation
 | 
			
		||||
static const uint8_t BL0940_FULL_PACKET = 0xAA;
 | 
			
		||||
static const uint8_t BL0940_PACKET_HEADER = 0x55;  // 0x58 according to documentation
 | 
			
		||||
 | 
			
		||||
static const uint8_t BL0940_WRITE_COMMAND = 0xA0;  // 0xA8 according to documentation
 | 
			
		||||
static const uint8_t BL0940_REG_I_FAST_RMS_CTRL = 0x10;
 | 
			
		||||
static const uint8_t BL0940_REG_MODE = 0x18;
 | 
			
		||||
static const uint8_t BL0940_REG_SOFT_RESET = 0x19;
 | 
			
		||||
static const uint8_t BL0940_REG_USR_WRPROT = 0x1A;
 | 
			
		||||
static const uint8_t BL0940_REG_TPS_CTRL = 0x1B;
 | 
			
		||||
 | 
			
		||||
const uint8_t BL0940_INIT[5][6] = {
 | 
			
		||||
    // Reset to default
 | 
			
		||||
    {BL0940_WRITE_COMMAND, BL0940_REG_SOFT_RESET, 0x5A, 0x5A, 0x5A, 0x38},
 | 
			
		||||
    // Enable User Operation Write
 | 
			
		||||
    {BL0940_WRITE_COMMAND, BL0940_REG_USR_WRPROT, 0x55, 0x00, 0x00, 0xF0},
 | 
			
		||||
    // 0x0100 = CF_UNABLE energy pulse, AC_FREQ_SEL 50Hz, RMS_UPDATE_SEL 800mS
 | 
			
		||||
    {BL0940_WRITE_COMMAND, BL0940_REG_MODE, 0x00, 0x10, 0x00, 0x37},
 | 
			
		||||
    // 0x47FF = Over-current and leakage alarm on, Automatic temperature measurement, Interval 100mS
 | 
			
		||||
    {BL0940_WRITE_COMMAND, BL0940_REG_TPS_CTRL, 0xFF, 0x47, 0x00, 0xFE},
 | 
			
		||||
    // 0x181C = Half cycle, Fast RMS threshold 6172
 | 
			
		||||
    {BL0940_WRITE_COMMAND, BL0940_REG_I_FAST_RMS_CTRL, 0x1C, 0x18, 0x00, 0x1B}};
 | 
			
		||||
 | 
			
		||||
void BL0940::loop() {
 | 
			
		||||
  DataPacket buffer;
 | 
			
		||||
  if (!this->available()) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (read_array((uint8_t *) &buffer, sizeof(buffer))) {
 | 
			
		||||
    if (validate_checksum(&buffer)) {
 | 
			
		||||
      received_package_(&buffer);
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGW(TAG, "Junk on wire. Throwing away partial message");
 | 
			
		||||
    while (read() >= 0)
 | 
			
		||||
      ;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool BL0940::validate_checksum(const DataPacket *data) {
 | 
			
		||||
  uint8_t checksum = BL0940_READ_COMMAND;
 | 
			
		||||
  // Whole package but checksum
 | 
			
		||||
  for (uint32_t i = 0; i < sizeof(data->raw) - 1; i++) {
 | 
			
		||||
    checksum += data->raw[i];
 | 
			
		||||
  }
 | 
			
		||||
  checksum ^= 0xFF;
 | 
			
		||||
  if (checksum != data->checksum) {
 | 
			
		||||
    ESP_LOGW(TAG, "BL0940 invalid checksum! 0x%02X != 0x%02X", checksum, data->checksum);
 | 
			
		||||
  }
 | 
			
		||||
  return checksum == data->checksum;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BL0940::update() {
 | 
			
		||||
  this->flush();
 | 
			
		||||
  this->write_byte(BL0940_READ_COMMAND);
 | 
			
		||||
  this->write_byte(BL0940_FULL_PACKET);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BL0940::setup() {
 | 
			
		||||
  for (auto i : BL0940_INIT) {
 | 
			
		||||
    this->write_array(i, 6);
 | 
			
		||||
    delay(1);
 | 
			
		||||
  }
 | 
			
		||||
  this->flush();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float BL0940::update_temp_(sensor::Sensor *sensor, ube16_t temperature) const {
 | 
			
		||||
  auto tb = (float) (temperature.h << 8 | temperature.l);
 | 
			
		||||
  float converted_temp = ((float) 170 / 448) * (tb / 2 - 32) - 45;
 | 
			
		||||
  if (sensor != nullptr) {
 | 
			
		||||
    if (sensor->has_state() && std::abs(converted_temp - sensor->get_state()) > max_temperature_diff_) {
 | 
			
		||||
      ESP_LOGD("bl0940", "Invalid temperature change. Sensor: '%s', Old temperature: %f, New temperature: %f",
 | 
			
		||||
               sensor->get_name().c_str(), sensor->get_state(), converted_temp);
 | 
			
		||||
      return 0.0f;
 | 
			
		||||
    }
 | 
			
		||||
    sensor->publish_state(converted_temp);
 | 
			
		||||
  }
 | 
			
		||||
  return converted_temp;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BL0940::received_package_(const DataPacket *data) const {
 | 
			
		||||
  // Bad header
 | 
			
		||||
  if (data->frame_header != BL0940_PACKET_HEADER) {
 | 
			
		||||
    ESP_LOGI("bl0940", "Invalid data. Header mismatch: %d", data->frame_header);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  float v_rms = (float) to_uint32_t(data->v_rms) / voltage_reference_;
 | 
			
		||||
  float i_rms = (float) to_uint32_t(data->i_rms) / current_reference_;
 | 
			
		||||
  float watt = (float) to_int32_t(data->watt) / power_reference_;
 | 
			
		||||
  uint32_t cf_cnt = to_uint32_t(data->cf_cnt);
 | 
			
		||||
  float total_energy_consumption = (float) cf_cnt / energy_reference_;
 | 
			
		||||
 | 
			
		||||
  float tps1 = update_temp_(internal_temperature_sensor_, data->tps1);
 | 
			
		||||
  float tps2 = update_temp_(external_temperature_sensor_, data->tps2);
 | 
			
		||||
 | 
			
		||||
  if (voltage_sensor_ != nullptr) {
 | 
			
		||||
    voltage_sensor_->publish_state(v_rms);
 | 
			
		||||
  }
 | 
			
		||||
  if (current_sensor_ != nullptr) {
 | 
			
		||||
    current_sensor_->publish_state(i_rms);
 | 
			
		||||
  }
 | 
			
		||||
  if (power_sensor_ != nullptr) {
 | 
			
		||||
    power_sensor_->publish_state(watt);
 | 
			
		||||
  }
 | 
			
		||||
  if (energy_sensor_ != nullptr) {
 | 
			
		||||
    energy_sensor_->publish_state(total_energy_consumption);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ESP_LOGV("bl0940", "BL0940: U %fV, I %fA, P %fW, Cnt %d, ∫P %fkWh, T1 %f°C, T2 %f°C", v_rms, i_rms, watt, cf_cnt,
 | 
			
		||||
           total_energy_consumption, tps1, tps2);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BL0940::dump_config() {  // NOLINT(readability-function-cognitive-complexity)
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "BL0940:");
 | 
			
		||||
  LOG_SENSOR("", "Voltage", this->voltage_sensor_);
 | 
			
		||||
  LOG_SENSOR("", "Current", this->current_sensor_);
 | 
			
		||||
  LOG_SENSOR("", "Power", this->power_sensor_);
 | 
			
		||||
  LOG_SENSOR("", "Energy", this->energy_sensor_);
 | 
			
		||||
  LOG_SENSOR("", "Internal temperature", this->internal_temperature_sensor_);
 | 
			
		||||
  LOG_SENSOR("", "External temperature", this->external_temperature_sensor_);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint32_t BL0940::to_uint32_t(ube24_t input) { return input.h << 16 | input.m << 8 | input.l; }
 | 
			
		||||
 | 
			
		||||
int32_t BL0940::to_int32_t(sbe24_t input) { return input.h << 16 | input.m << 8 | input.l; }
 | 
			
		||||
 | 
			
		||||
}  // namespace bl0940
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										109
									
								
								esphome/components/bl0940/bl0940.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										109
									
								
								esphome/components/bl0940/bl0940.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,109 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/components/uart/uart.h"
 | 
			
		||||
#include "esphome/components/sensor/sensor.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace bl0940 {
 | 
			
		||||
 | 
			
		||||
static const float BL0940_PREF = 1430;
 | 
			
		||||
static const float BL0940_UREF = 33000;
 | 
			
		||||
static const float BL0940_IREF = 275000;  // 2750 from tasmota. Seems to generate values 100 times too high
 | 
			
		||||
 | 
			
		||||
// Measured to 297J  per click according to power consumption of 5 minutes
 | 
			
		||||
// Converted to kWh (3.6MJ per kwH). Used to be 256 * 1638.4
 | 
			
		||||
static const float BL0940_EREF = 3.6e6 / 297;
 | 
			
		||||
 | 
			
		||||
struct ube24_t {  // NOLINT(readability-identifier-naming,altera-struct-pack-align)
 | 
			
		||||
  uint8_t l;
 | 
			
		||||
  uint8_t m;
 | 
			
		||||
  uint8_t h;
 | 
			
		||||
} __attribute__((packed));
 | 
			
		||||
 | 
			
		||||
struct ube16_t {  // NOLINT(readability-identifier-naming,altera-struct-pack-align)
 | 
			
		||||
  uint8_t l;
 | 
			
		||||
  uint8_t h;
 | 
			
		||||
} __attribute__((packed));
 | 
			
		||||
 | 
			
		||||
struct sbe24_t {  // NOLINT(readability-identifier-naming,altera-struct-pack-align)
 | 
			
		||||
  uint8_t l;
 | 
			
		||||
  uint8_t m;
 | 
			
		||||
  int8_t h;
 | 
			
		||||
} __attribute__((packed));
 | 
			
		||||
 | 
			
		||||
// Caveat: All these values are big endian (low - middle - high)
 | 
			
		||||
 | 
			
		||||
union DataPacket {  // NOLINT(altera-struct-pack-align)
 | 
			
		||||
  uint8_t raw[35];
 | 
			
		||||
  struct {
 | 
			
		||||
    uint8_t frame_header;  // value of 0x58 according to docs. 0x55 according to Tasmota real world tests. Reality wins.
 | 
			
		||||
    ube24_t i_fast_rms;    // 0x00
 | 
			
		||||
    ube24_t i_rms;         // 0x04
 | 
			
		||||
    ube24_t RESERVED0;     // reserved
 | 
			
		||||
    ube24_t v_rms;         // 0x06
 | 
			
		||||
    ube24_t RESERVED1;     // reserved
 | 
			
		||||
    sbe24_t watt;          // 0x08
 | 
			
		||||
    ube24_t RESERVED2;     // reserved
 | 
			
		||||
    ube24_t cf_cnt;        // 0x0A
 | 
			
		||||
    ube24_t RESERVED3;     // reserved
 | 
			
		||||
    ube16_t tps1;          // 0x0c
 | 
			
		||||
    uint8_t RESERVED4;     // value of 0x00
 | 
			
		||||
    ube16_t tps2;          // 0x0c
 | 
			
		||||
    uint8_t RESERVED5;     // value of 0x00
 | 
			
		||||
    uint8_t checksum;      // checksum
 | 
			
		||||
  };
 | 
			
		||||
} __attribute__((packed));
 | 
			
		||||
 | 
			
		||||
class BL0940 : public PollingComponent, 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; }
 | 
			
		||||
  void set_power_sensor(sensor::Sensor *power_sensor) { power_sensor_ = power_sensor; }
 | 
			
		||||
  void set_energy_sensor(sensor::Sensor *energy_sensor) { energy_sensor_ = energy_sensor; }
 | 
			
		||||
  void set_internal_temperature_sensor(sensor::Sensor *internal_temperature_sensor) {
 | 
			
		||||
    internal_temperature_sensor_ = internal_temperature_sensor;
 | 
			
		||||
  }
 | 
			
		||||
  void set_external_temperature_sensor(sensor::Sensor *external_temperature_sensor) {
 | 
			
		||||
    external_temperature_sensor_ = external_temperature_sensor;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  void loop() override;
 | 
			
		||||
 | 
			
		||||
  void update() override;
 | 
			
		||||
  void setup() override;
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  sensor::Sensor *voltage_sensor_;
 | 
			
		||||
  sensor::Sensor *current_sensor_;
 | 
			
		||||
  // NB This may be negative as the circuits is seemingly able to measure
 | 
			
		||||
  // power in both directions
 | 
			
		||||
  sensor::Sensor *power_sensor_;
 | 
			
		||||
  sensor::Sensor *energy_sensor_;
 | 
			
		||||
  sensor::Sensor *internal_temperature_sensor_;
 | 
			
		||||
  sensor::Sensor *external_temperature_sensor_;
 | 
			
		||||
 | 
			
		||||
  // Max difference between two measurements of the temperature. Used to avoid noise.
 | 
			
		||||
  float max_temperature_diff_{0};
 | 
			
		||||
  // Divide by this to turn into Watt
 | 
			
		||||
  float power_reference_ = BL0940_PREF;
 | 
			
		||||
  // Divide by this to turn into Volt
 | 
			
		||||
  float voltage_reference_ = BL0940_UREF;
 | 
			
		||||
  // Divide by this to turn into Ampere
 | 
			
		||||
  float current_reference_ = BL0940_IREF;
 | 
			
		||||
  // Divide by this to turn into kWh
 | 
			
		||||
  float energy_reference_ = BL0940_EREF;
 | 
			
		||||
 | 
			
		||||
  float update_temp_(sensor::Sensor *sensor, ube16_t packed_temperature) const;
 | 
			
		||||
 | 
			
		||||
  static uint32_t to_uint32_t(ube24_t input);
 | 
			
		||||
 | 
			
		||||
  static int32_t to_int32_t(sbe24_t input);
 | 
			
		||||
 | 
			
		||||
  static bool validate_checksum(const DataPacket *data);
 | 
			
		||||
 | 
			
		||||
  void received_package_(const DataPacket *data) const;
 | 
			
		||||
};
 | 
			
		||||
}  // namespace bl0940
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										106
									
								
								esphome/components/bl0940/sensor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										106
									
								
								esphome/components/bl0940/sensor.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,106 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.components import sensor, uart
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_CURRENT,
 | 
			
		||||
    CONF_ENERGY,
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    CONF_POWER,
 | 
			
		||||
    CONF_VOLTAGE,
 | 
			
		||||
    DEVICE_CLASS_CURRENT,
 | 
			
		||||
    DEVICE_CLASS_ENERGY,
 | 
			
		||||
    DEVICE_CLASS_POWER,
 | 
			
		||||
    DEVICE_CLASS_VOLTAGE,
 | 
			
		||||
    DEVICE_CLASS_TEMPERATURE,
 | 
			
		||||
    ICON_EMPTY,
 | 
			
		||||
    STATE_CLASS_MEASUREMENT,
 | 
			
		||||
    STATE_CLASS_NONE,
 | 
			
		||||
    UNIT_AMPERE,
 | 
			
		||||
    UNIT_CELSIUS,
 | 
			
		||||
    UNIT_KILOWATT_HOURS,
 | 
			
		||||
    UNIT_VOLT,
 | 
			
		||||
    UNIT_WATT,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["uart"]
 | 
			
		||||
 | 
			
		||||
CONF_INTERNAL_TEMPERATURE = "internal_temperature"
 | 
			
		||||
CONF_EXTERNAL_TEMPERATURE = "external_temperature"
 | 
			
		||||
 | 
			
		||||
bl0940_ns = cg.esphome_ns.namespace("bl0940")
 | 
			
		||||
BL0940 = bl0940_ns.class_("BL0940", cg.PollingComponent, uart.UARTDevice)
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = (
 | 
			
		||||
    cv.Schema(
 | 
			
		||||
        {
 | 
			
		||||
            cv.GenerateID(): cv.declare_id(BL0940),
 | 
			
		||||
            cv.Optional(CONF_VOLTAGE): sensor.sensor_schema(
 | 
			
		||||
                UNIT_VOLT, ICON_EMPTY, 1, DEVICE_CLASS_VOLTAGE, STATE_CLASS_MEASUREMENT
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_CURRENT): sensor.sensor_schema(
 | 
			
		||||
                UNIT_AMPERE,
 | 
			
		||||
                ICON_EMPTY,
 | 
			
		||||
                2,
 | 
			
		||||
                DEVICE_CLASS_CURRENT,
 | 
			
		||||
                STATE_CLASS_MEASUREMENT,
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_POWER): sensor.sensor_schema(
 | 
			
		||||
                UNIT_WATT, ICON_EMPTY, 0, DEVICE_CLASS_POWER, STATE_CLASS_MEASUREMENT
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_ENERGY): sensor.sensor_schema(
 | 
			
		||||
                UNIT_KILOWATT_HOURS,
 | 
			
		||||
                ICON_EMPTY,
 | 
			
		||||
                0,
 | 
			
		||||
                DEVICE_CLASS_ENERGY,
 | 
			
		||||
                STATE_CLASS_NONE,
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_INTERNAL_TEMPERATURE): sensor.sensor_schema(
 | 
			
		||||
                UNIT_CELSIUS,
 | 
			
		||||
                ICON_EMPTY,
 | 
			
		||||
                0,
 | 
			
		||||
                DEVICE_CLASS_TEMPERATURE,
 | 
			
		||||
                STATE_CLASS_NONE,
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_EXTERNAL_TEMPERATURE): sensor.sensor_schema(
 | 
			
		||||
                UNIT_CELSIUS,
 | 
			
		||||
                ICON_EMPTY,
 | 
			
		||||
                0,
 | 
			
		||||
                DEVICE_CLASS_TEMPERATURE,
 | 
			
		||||
                STATE_CLASS_NONE,
 | 
			
		||||
            ),
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
    .extend(cv.polling_component_schema("60s"))
 | 
			
		||||
    .extend(uart.UART_DEVICE_SCHEMA)
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
    await uart.register_uart_device(var, config)
 | 
			
		||||
 | 
			
		||||
    if CONF_VOLTAGE in config:
 | 
			
		||||
        conf = config[CONF_VOLTAGE]
 | 
			
		||||
        sens = await sensor.new_sensor(conf)
 | 
			
		||||
        cg.add(var.set_voltage_sensor(sens))
 | 
			
		||||
    if CONF_CURRENT in config:
 | 
			
		||||
        conf = config[CONF_CURRENT]
 | 
			
		||||
        sens = await sensor.new_sensor(conf)
 | 
			
		||||
        cg.add(var.set_current_sensor(sens))
 | 
			
		||||
    if CONF_POWER in config:
 | 
			
		||||
        conf = config[CONF_POWER]
 | 
			
		||||
        sens = await sensor.new_sensor(conf)
 | 
			
		||||
        cg.add(var.set_power_sensor(sens))
 | 
			
		||||
    if CONF_ENERGY in config:
 | 
			
		||||
        conf = config[CONF_ENERGY]
 | 
			
		||||
        sens = await sensor.new_sensor(conf)
 | 
			
		||||
        cg.add(var.set_energy_sensor(sens))
 | 
			
		||||
    if CONF_INTERNAL_TEMPERATURE in config:
 | 
			
		||||
        conf = config[CONF_INTERNAL_TEMPERATURE]
 | 
			
		||||
        sens = await sensor.new_sensor(conf)
 | 
			
		||||
        cg.add(var.set_internal_temperature_sensor(sens))
 | 
			
		||||
    if CONF_EXTERNAL_TEMPERATURE in config:
 | 
			
		||||
        conf = config[CONF_EXTERNAL_TEMPERATURE]
 | 
			
		||||
        sens = await sensor.new_sensor(conf)
 | 
			
		||||
        cg.add(var.set_external_temperature_sensor(sens))
 | 
			
		||||
							
								
								
									
										388
									
								
								esphome/components/bmp3xx/bmp3xx.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										388
									
								
								esphome/components/bmp3xx/bmp3xx.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,388 @@
 | 
			
		||||
/*
 | 
			
		||||
  based on BMP388_DEV by Martin Lindupp
 | 
			
		||||
  under MIT License (MIT)
 | 
			
		||||
  Copyright (C) Martin Lindupp 2020
 | 
			
		||||
  http://github.com/MartinL1/BMP388_DEV
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#include "bmp3xx.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
#include "esphome/core/hal.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace bmp3xx {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "bmp3xx.sensor";
 | 
			
		||||
 | 
			
		||||
static const LogString *chip_type_to_str(uint8_t chip_type) {
 | 
			
		||||
  switch (chip_type) {
 | 
			
		||||
    case BMP388_ID:
 | 
			
		||||
      return LOG_STR("BMP 388");
 | 
			
		||||
    case BMP390_ID:
 | 
			
		||||
      return LOG_STR("BMP 390");
 | 
			
		||||
    default:
 | 
			
		||||
      return LOG_STR("Unknown Chip Type");
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const LogString *oversampling_to_str(Oversampling oversampling) {
 | 
			
		||||
  switch (oversampling) {
 | 
			
		||||
    case Oversampling::OVERSAMPLING_NONE:
 | 
			
		||||
      return LOG_STR("None");
 | 
			
		||||
    case Oversampling::OVERSAMPLING_X2:
 | 
			
		||||
      return LOG_STR("2x");
 | 
			
		||||
    case Oversampling::OVERSAMPLING_X4:
 | 
			
		||||
      return LOG_STR("4x");
 | 
			
		||||
    case Oversampling::OVERSAMPLING_X8:
 | 
			
		||||
      return LOG_STR("8x");
 | 
			
		||||
    case Oversampling::OVERSAMPLING_X16:
 | 
			
		||||
      return LOG_STR("16x");
 | 
			
		||||
    case Oversampling::OVERSAMPLING_X32:
 | 
			
		||||
      return LOG_STR("32x");
 | 
			
		||||
    default:
 | 
			
		||||
      return LOG_STR("");
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const LogString *iir_filter_to_str(IIRFilter filter) {
 | 
			
		||||
  switch (filter) {
 | 
			
		||||
    case IIRFilter::IIR_FILTER_OFF:
 | 
			
		||||
      return LOG_STR("OFF");
 | 
			
		||||
    case IIRFilter::IIR_FILTER_2:
 | 
			
		||||
      return LOG_STR("2x");
 | 
			
		||||
    case IIRFilter::IIR_FILTER_4:
 | 
			
		||||
      return LOG_STR("4x");
 | 
			
		||||
    case IIRFilter::IIR_FILTER_8:
 | 
			
		||||
      return LOG_STR("8x");
 | 
			
		||||
    case IIRFilter::IIR_FILTER_16:
 | 
			
		||||
      return LOG_STR("16x");
 | 
			
		||||
    case IIRFilter::IIR_FILTER_32:
 | 
			
		||||
      return LOG_STR("32x");
 | 
			
		||||
    case IIRFilter::IIR_FILTER_64:
 | 
			
		||||
      return LOG_STR("64x");
 | 
			
		||||
    case IIRFilter::IIR_FILTER_128:
 | 
			
		||||
      return LOG_STR("128x");
 | 
			
		||||
    default:
 | 
			
		||||
      return LOG_STR("");
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BMP3XXComponent::setup() {
 | 
			
		||||
  this->error_code_ = NONE;
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Setting up BMP3XX...");
 | 
			
		||||
  // Call the Device base class "initialise" function
 | 
			
		||||
  if (!reset()) {
 | 
			
		||||
    ESP_LOGE(TAG, "Failed to reset BMP3XX...");
 | 
			
		||||
    this->error_code_ = ERROR_SENSOR_RESET;
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (!read_byte(BMP388_CHIP_ID, &this->chip_id_.reg)) {
 | 
			
		||||
    ESP_LOGE(TAG, "Can't read chip id");
 | 
			
		||||
    this->error_code_ = ERROR_COMMUNICATION_FAILED;
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Chip %s Id 0x%X", LOG_STR_ARG(chip_type_to_str(this->chip_id_.reg)), this->chip_id_.reg);
 | 
			
		||||
 | 
			
		||||
  if (chip_id_.reg != BMP388_ID && chip_id_.reg != BMP390_ID) {
 | 
			
		||||
    ESP_LOGE(TAG, "Unknown chip id - is this really a BMP388 or BMP390?");
 | 
			
		||||
    this->error_code_ = ERROR_WRONG_CHIP_ID;
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  // set sensor in sleep mode
 | 
			
		||||
  stop_conversion();
 | 
			
		||||
  // Read the calibration parameters into the params structure
 | 
			
		||||
  if (!read_bytes(BMP388_TRIM_PARAMS, (uint8_t *) &compensation_params_, sizeof(compensation_params_))) {
 | 
			
		||||
    ESP_LOGE(TAG, "Can't read calibration data");
 | 
			
		||||
    this->error_code_ = ERROR_COMMUNICATION_FAILED;
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  compensation_float_params_.param_T1 =
 | 
			
		||||
      (float) compensation_params_.param_T1 / powf(2.0f, -8.0f);  // Calculate the floating point trim parameters
 | 
			
		||||
  compensation_float_params_.param_T2 = (float) compensation_params_.param_T2 / powf(2.0f, 30.0f);
 | 
			
		||||
  compensation_float_params_.param_T3 = (float) compensation_params_.param_T3 / powf(2.0f, 48.0f);
 | 
			
		||||
  compensation_float_params_.param_P1 = ((float) compensation_params_.param_P1 - powf(2.0f, 14.0f)) / powf(2.0f, 20.0f);
 | 
			
		||||
  compensation_float_params_.param_P2 = ((float) compensation_params_.param_P2 - powf(2.0f, 14.0f)) / powf(2.0f, 29.0f);
 | 
			
		||||
  compensation_float_params_.param_P3 = (float) compensation_params_.param_P3 / powf(2.0f, 32.0f);
 | 
			
		||||
  compensation_float_params_.param_P4 = (float) compensation_params_.param_P4 / powf(2.0f, 37.0f);
 | 
			
		||||
  compensation_float_params_.param_P5 = (float) compensation_params_.param_P5 / powf(2.0f, -3.0f);
 | 
			
		||||
  compensation_float_params_.param_P6 = (float) compensation_params_.param_P6 / powf(2.0f, 6.0f);
 | 
			
		||||
  compensation_float_params_.param_P7 = (float) compensation_params_.param_P7 / powf(2.0f, 8.0f);
 | 
			
		||||
  compensation_float_params_.param_P8 = (float) compensation_params_.param_P8 / powf(2.0f, 15.0f);
 | 
			
		||||
  compensation_float_params_.param_P9 = (float) compensation_params_.param_P9 / powf(2.0f, 48.0f);
 | 
			
		||||
  compensation_float_params_.param_P10 = (float) compensation_params_.param_P10 / powf(2.0f, 48.0f);
 | 
			
		||||
  compensation_float_params_.param_P11 = (float) compensation_params_.param_P11 / powf(2.0f, 65.0f);
 | 
			
		||||
 | 
			
		||||
  // Initialise the BMP388 IIR filter register
 | 
			
		||||
  if (!set_iir_filter(this->iir_filter_)) {
 | 
			
		||||
    ESP_LOGE(TAG, "Failed to set IIR filter");
 | 
			
		||||
    this->error_code_ = ERROR_COMMUNICATION_FAILED;
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Set power control registers
 | 
			
		||||
  pwr_ctrl_.bit.press_en = 1;
 | 
			
		||||
  pwr_ctrl_.bit.temp_en = 1;
 | 
			
		||||
  // Disable pressure if no sensor defined
 | 
			
		||||
  // keep temperature enabled since it's needed for compensation
 | 
			
		||||
  if (this->pressure_sensor_ == nullptr) {
 | 
			
		||||
    pwr_ctrl_.bit.press_en = 0;
 | 
			
		||||
    this->pressure_oversampling_ = OVERSAMPLING_NONE;
 | 
			
		||||
  }
 | 
			
		||||
  // just disable oeversampling for temp if not used
 | 
			
		||||
  if (this->temperature_sensor_ == nullptr) {
 | 
			
		||||
    this->temperature_oversampling_ = OVERSAMPLING_NONE;
 | 
			
		||||
  }
 | 
			
		||||
  // Initialise the BMP388 oversampling register
 | 
			
		||||
  if (!set_oversampling_register(this->pressure_oversampling_, this->temperature_oversampling_)) {
 | 
			
		||||
    ESP_LOGE(TAG, "Failed to set oversampling register");
 | 
			
		||||
    this->error_code_ = ERROR_COMMUNICATION_FAILED;
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BMP3XXComponent::dump_config() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "BMP3XX:");
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Type: %s (0x%X)", LOG_STR_ARG(chip_type_to_str(this->chip_id_.reg)), this->chip_id_.reg);
 | 
			
		||||
  LOG_I2C_DEVICE(this);
 | 
			
		||||
  switch (this->error_code_) {
 | 
			
		||||
    case NONE:
 | 
			
		||||
      break;
 | 
			
		||||
    case ERROR_COMMUNICATION_FAILED:
 | 
			
		||||
      ESP_LOGE(TAG, "Communication with BMP3XX failed!");
 | 
			
		||||
      break;
 | 
			
		||||
    case ERROR_WRONG_CHIP_ID:
 | 
			
		||||
      ESP_LOGE(
 | 
			
		||||
          TAG,
 | 
			
		||||
          "BMP3XX has wrong chip ID (reported id: 0x%X) - please check if you are really using a BMP 388 or BMP 390",
 | 
			
		||||
          this->chip_id_.reg);
 | 
			
		||||
      break;
 | 
			
		||||
    case ERROR_SENSOR_RESET:
 | 
			
		||||
      ESP_LOGE(TAG, "BMP3XX failed to reset");
 | 
			
		||||
      break;
 | 
			
		||||
    default:
 | 
			
		||||
      ESP_LOGE(TAG, "BMP3XX error code %d", (int) this->error_code_);
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  IIR Filter: %s", LOG_STR_ARG(iir_filter_to_str(this->iir_filter_)));
 | 
			
		||||
  LOG_UPDATE_INTERVAL(this);
 | 
			
		||||
  if (this->temperature_sensor_) {
 | 
			
		||||
    LOG_SENSOR("  ", "Temperature", this->temperature_sensor_);
 | 
			
		||||
    ESP_LOGCONFIG(TAG, "    Oversampling: %s", LOG_STR_ARG(oversampling_to_str(this->temperature_oversampling_)));
 | 
			
		||||
  }
 | 
			
		||||
  if (this->pressure_sensor_) {
 | 
			
		||||
    LOG_SENSOR("  ", "Pressure", this->pressure_sensor_);
 | 
			
		||||
    ESP_LOGCONFIG(TAG, "    Oversampling: %s", LOG_STR_ARG(oversampling_to_str(this->pressure_oversampling_)));
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
float BMP3XXComponent::get_setup_priority() const { return setup_priority::DATA; }
 | 
			
		||||
 | 
			
		||||
inline uint8_t oversampling_to_time(Oversampling over_sampling) { return (1 << uint8_t(over_sampling)); }
 | 
			
		||||
 | 
			
		||||
void BMP3XXComponent::update() {
 | 
			
		||||
  // Enable sensor
 | 
			
		||||
  ESP_LOGV(TAG, "Sending conversion request...");
 | 
			
		||||
  float meas_time = 1.0f;
 | 
			
		||||
  // Ref: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp390-ds002.pdf 3.9.2
 | 
			
		||||
  meas_time += 2.02f * oversampling_to_time(this->temperature_oversampling_) + 0.163f;
 | 
			
		||||
  meas_time += 2.02f * oversampling_to_time(this->pressure_oversampling_) + 0.392f;
 | 
			
		||||
  meas_time += 0.234f;
 | 
			
		||||
  if (!set_mode(FORCED_MODE)) {
 | 
			
		||||
    ESP_LOGE(TAG, "Failed start forced mode");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ESP_LOGVV(TAG, "measurement time %d", uint32_t(ceilf(meas_time)));
 | 
			
		||||
  this->set_timeout("data", uint32_t(ceilf(meas_time)), [this]() {
 | 
			
		||||
    float temperature = 0.0f;
 | 
			
		||||
    float pressure = 0.0f;
 | 
			
		||||
    if (this->pressure_sensor_ != nullptr) {
 | 
			
		||||
      if (!get_measurements(temperature, pressure)) {
 | 
			
		||||
        ESP_LOGW(TAG, "Failed to read pressure and temperature - skipping update");
 | 
			
		||||
        this->status_set_warning();
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
      ESP_LOGD(TAG, "Got temperature=%.1f°C pressure=%.1fhPa", temperature, pressure);
 | 
			
		||||
    } else {
 | 
			
		||||
      if (!get_temperature(temperature)) {
 | 
			
		||||
        ESP_LOGW(TAG, "Failed to read temperature - skipping update");
 | 
			
		||||
        this->status_set_warning();
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
      ESP_LOGD(TAG, "Got temperature=%.1f°C", temperature);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (this->temperature_sensor_ != nullptr)
 | 
			
		||||
      this->temperature_sensor_->publish_state(temperature);
 | 
			
		||||
    if (this->pressure_sensor_ != nullptr)
 | 
			
		||||
      this->pressure_sensor_->publish_state(pressure);
 | 
			
		||||
    this->status_clear_warning();
 | 
			
		||||
    set_mode(SLEEP_MODE);
 | 
			
		||||
  });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Reset the BMP3XX
 | 
			
		||||
uint8_t BMP3XXComponent::reset() {
 | 
			
		||||
  write_byte(BMP388_CMD, RESET_CODE);  // Write the reset code to the command register
 | 
			
		||||
  // Wait for 10ms
 | 
			
		||||
  delay(10);
 | 
			
		||||
  this->read_byte(BMP388_EVENT, &event_.reg);  // Read the BMP388's event register
 | 
			
		||||
  return event_.bit.por_detected;              // Return if device reset is complete
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Start a one shot measurement in FORCED_MODE
 | 
			
		||||
bool BMP3XXComponent::start_forced_conversion() {
 | 
			
		||||
  // Only set FORCED_MODE if we're already in SLEEP_MODE
 | 
			
		||||
  if (pwr_ctrl_.bit.mode == SLEEP_MODE) {
 | 
			
		||||
    return set_mode(FORCED_MODE);
 | 
			
		||||
  }
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Stop the conversion and return to SLEEP_MODE
 | 
			
		||||
bool BMP3XXComponent::stop_conversion() { return set_mode(SLEEP_MODE); }
 | 
			
		||||
 | 
			
		||||
// Set the pressure oversampling rate
 | 
			
		||||
bool BMP3XXComponent::set_pressure_oversampling(Oversampling oversampling) {
 | 
			
		||||
  osr_.bit.osr_p = oversampling;
 | 
			
		||||
  return this->write_byte(BMP388_OSR, osr_.reg);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Set the temperature oversampling rate
 | 
			
		||||
bool BMP3XXComponent::set_temperature_oversampling(Oversampling oversampling) {
 | 
			
		||||
  osr_.bit.osr_t = oversampling;
 | 
			
		||||
  return this->write_byte(BMP388_OSR, osr_.reg);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Set the IIR filter setting
 | 
			
		||||
bool BMP3XXComponent::set_iir_filter(IIRFilter iir_filter) {
 | 
			
		||||
  config_.bit.iir_filter = iir_filter;
 | 
			
		||||
  return this->write_byte(BMP388_CONFIG, config_.reg);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Get temperature
 | 
			
		||||
bool BMP3XXComponent::get_temperature(float &temperature) {
 | 
			
		||||
  // Check if a measurement is ready
 | 
			
		||||
  if (!data_ready()) {
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
  uint8_t data[3];
 | 
			
		||||
  // Read the temperature
 | 
			
		||||
  if (!this->read_bytes(BMP388_DATA_3, &data[0], 3)) {
 | 
			
		||||
    ESP_LOGE(TAG, "Failed to read temperature");
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
  // Copy the temperature data into the adc variables
 | 
			
		||||
  int32_t adc_temp = (int32_t) data[2] << 16 | (int32_t) data[1] << 8 | (int32_t) data[0];
 | 
			
		||||
  // Temperature compensation (function from BMP388 datasheet)
 | 
			
		||||
  temperature = bmp388_compensate_temperature_((float) adc_temp);
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Get the pressure
 | 
			
		||||
bool BMP3XXComponent::get_pressure(float &pressure) {
 | 
			
		||||
  float temperature;
 | 
			
		||||
  return get_measurements(temperature, pressure);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Get temperature and pressure
 | 
			
		||||
bool BMP3XXComponent::get_measurements(float &temperature, float &pressure) {
 | 
			
		||||
  // Check if a measurement is ready
 | 
			
		||||
  if (!data_ready()) {
 | 
			
		||||
    ESP_LOGD(TAG, "BMP3XX Get measurement - data not ready skipping update");
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  uint8_t data[6];
 | 
			
		||||
  // Read the temperature and pressure data
 | 
			
		||||
  if (!this->read_bytes(BMP388_DATA_0, &data[0], 6)) {
 | 
			
		||||
    ESP_LOGE(TAG, "Failed to read measurements");
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
  // Copy the temperature and pressure data into the adc variables
 | 
			
		||||
  int32_t adc_pres = (int32_t) data[2] << 16 | (int32_t) data[1] << 8 | (int32_t) data[0];
 | 
			
		||||
  int32_t adc_temp = (int32_t) data[5] << 16 | (int32_t) data[4] << 8 | (int32_t) data[3];
 | 
			
		||||
 | 
			
		||||
  // Temperature compensation (function from BMP388 datasheet)
 | 
			
		||||
  temperature = bmp388_compensate_temperature_((float) adc_temp);
 | 
			
		||||
  // Pressure compensation (function from BMP388 datasheet)
 | 
			
		||||
  pressure = bmp388_compensate_pressure_((float) adc_pres, temperature);
 | 
			
		||||
  // Calculate the pressure in millibar/hPa
 | 
			
		||||
  pressure /= 100.0f;
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Set the BMP388's mode in the power control register
 | 
			
		||||
bool BMP3XXComponent::set_mode(OperationMode mode) {
 | 
			
		||||
  pwr_ctrl_.bit.mode = mode;
 | 
			
		||||
  return this->write_byte(BMP388_PWR_CTRL, pwr_ctrl_.reg);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Set the BMP388 oversampling register
 | 
			
		||||
bool BMP3XXComponent::set_oversampling_register(Oversampling pressure_oversampling,
 | 
			
		||||
                                                Oversampling temperature_oversampling) {
 | 
			
		||||
  osr_.reg = temperature_oversampling << 3 | pressure_oversampling;
 | 
			
		||||
  return this->write_byte(BMP388_OSR, osr_.reg);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Check if measurement data is ready
 | 
			
		||||
bool BMP3XXComponent::data_ready() {
 | 
			
		||||
  // If we're in SLEEP_MODE return immediately
 | 
			
		||||
  if (pwr_ctrl_.bit.mode == SLEEP_MODE) {
 | 
			
		||||
    ESP_LOGD(TAG, "Not ready - sensor is in sleep mode");
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
  // Read the interrupt status register
 | 
			
		||||
  uint8_t status;
 | 
			
		||||
  if (!this->read_byte(BMP388_INT_STATUS, &status)) {
 | 
			
		||||
    ESP_LOGE(TAG, "Failed to read status register");
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
  int_status_.reg = status;
 | 
			
		||||
  ESP_LOGVV(TAG, "data ready status %d", status);
 | 
			
		||||
  // If we're in FORCED_MODE switch back to SLEEP_MODE
 | 
			
		||||
  if (int_status_.bit.drdy) {
 | 
			
		||||
    if (pwr_ctrl_.bit.mode == FORCED_MODE) {
 | 
			
		||||
      pwr_ctrl_.bit.mode = SLEEP_MODE;
 | 
			
		||||
    }
 | 
			
		||||
    return true;  // The measurement is ready
 | 
			
		||||
  }
 | 
			
		||||
  return false;  // The measurement is still pending
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
////////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
// Bosch BMP3XXComponent (Private) Member Functions
 | 
			
		||||
////////////////////////////////////////////////////////////////////////////////
 | 
			
		||||
 | 
			
		||||
float BMP3XXComponent::bmp388_compensate_temperature_(float uncomp_temp) {
 | 
			
		||||
  float partial_data1 = uncomp_temp - compensation_float_params_.param_T1;
 | 
			
		||||
  float partial_data2 = partial_data1 * compensation_float_params_.param_T2;
 | 
			
		||||
  return partial_data2 + partial_data1 * partial_data1 * compensation_float_params_.param_T3;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float BMP3XXComponent::bmp388_compensate_pressure_(float uncomp_press, float t_lin) {
 | 
			
		||||
  float partial_data1 = compensation_float_params_.param_P6 * t_lin;
 | 
			
		||||
  float partial_data2 = compensation_float_params_.param_P7 * t_lin * t_lin;
 | 
			
		||||
  float partial_data3 = compensation_float_params_.param_P8 * t_lin * t_lin * t_lin;
 | 
			
		||||
  float partial_out1 = compensation_float_params_.param_P5 + partial_data1 + partial_data2 + partial_data3;
 | 
			
		||||
  partial_data1 = compensation_float_params_.param_P2 * t_lin;
 | 
			
		||||
  partial_data2 = compensation_float_params_.param_P3 * t_lin * t_lin;
 | 
			
		||||
  partial_data3 = compensation_float_params_.param_P4 * t_lin * t_lin * t_lin;
 | 
			
		||||
  float partial_out2 =
 | 
			
		||||
      uncomp_press * (compensation_float_params_.param_P1 + partial_data1 + partial_data2 + partial_data3);
 | 
			
		||||
  partial_data1 = uncomp_press * uncomp_press;
 | 
			
		||||
  partial_data2 = compensation_float_params_.param_P9 + compensation_float_params_.param_P10 * t_lin;
 | 
			
		||||
  partial_data3 = partial_data1 * partial_data2;
 | 
			
		||||
  float partial_data4 =
 | 
			
		||||
      partial_data3 + uncomp_press * uncomp_press * uncomp_press * compensation_float_params_.param_P11;
 | 
			
		||||
  return partial_out1 + partial_out2 + partial_data4;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace bmp3xx
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										237
									
								
								esphome/components/bmp3xx/bmp3xx.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										237
									
								
								esphome/components/bmp3xx/bmp3xx.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,237 @@
 | 
			
		||||
/*
 | 
			
		||||
  based on BMP388_DEV by Martin Lindupp
 | 
			
		||||
  under MIT License (MIT)
 | 
			
		||||
  Copyright (C) Martin Lindupp 2020
 | 
			
		||||
  http://github.com/MartinL1/BMP388_DEV
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/components/sensor/sensor.h"
 | 
			
		||||
#include "esphome/components/i2c/i2c.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace bmp3xx {
 | 
			
		||||
 | 
			
		||||
static const uint8_t BMP388_ID = 0x50;   // The BMP388 device ID
 | 
			
		||||
static const uint8_t BMP390_ID = 0x60;   // The BMP390 device ID
 | 
			
		||||
static const uint8_t RESET_CODE = 0xB6;  // The BMP388 reset code
 | 
			
		||||
 | 
			
		||||
/// BMP388_DEV Registers
 | 
			
		||||
enum {
 | 
			
		||||
  BMP388_CHIP_ID = 0x00,       // Chip ID register sub-address
 | 
			
		||||
  BMP388_ERR_REG = 0x02,       // Error register sub-address
 | 
			
		||||
  BMP388_STATUS = 0x03,        // Status register sub-address
 | 
			
		||||
  BMP388_DATA_0 = 0x04,        // Pressure eXtended Least Significant Byte (XLSB) register sub-address
 | 
			
		||||
  BMP388_DATA_1 = 0x05,        // Pressure Least Significant Byte (LSB) register sub-address
 | 
			
		||||
  BMP388_DATA_2 = 0x06,        // Pressure Most Significant Byte (MSB) register sub-address
 | 
			
		||||
  BMP388_DATA_3 = 0x07,        // Temperature eXtended Least Significant Byte (XLSB) register sub-address
 | 
			
		||||
  BMP388_DATA_4 = 0x08,        // Temperature Least Significant Byte (LSB) register sub-address
 | 
			
		||||
  BMP388_DATA_5 = 0x09,        // Temperature Most Significant Byte (MSB) register sub-address
 | 
			
		||||
  BMP388_SENSORTIME_0 = 0x0C,  // Sensor time register 0 sub-address
 | 
			
		||||
  BMP388_SENSORTIME_1 = 0x0D,  // Sensor time register 1 sub-address
 | 
			
		||||
  BMP388_SENSORTIME_2 = 0x0E,  // Sensor time register 2 sub-address
 | 
			
		||||
  BMP388_EVENT = 0x10,         // Event register sub-address
 | 
			
		||||
  BMP388_INT_STATUS = 0x11,    // Interrupt Status register sub-address
 | 
			
		||||
  BMP388_INT_CTRL = 0x19,      // Interrupt Control register sub-address
 | 
			
		||||
  BMP388_IF_CONFIG = 0x1A,     // Interface Configuration register sub-address
 | 
			
		||||
  BMP388_PWR_CTRL = 0x1B,      // Power Control register sub-address
 | 
			
		||||
  BMP388_OSR = 0x1C,           // Oversampling register sub-address
 | 
			
		||||
  BMP388_ODR = 0x1D,           // Output Data Rate register sub-address
 | 
			
		||||
  BMP388_CONFIG = 0x1F,        // Configuration register sub-address
 | 
			
		||||
  BMP388_TRIM_PARAMS = 0x31,   // Trim parameter registers' base sub-address
 | 
			
		||||
  BMP388_CMD = 0x7E            // Command register sub-address
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/// Device mode bitfield in the control and measurement register
 | 
			
		||||
enum OperationMode { SLEEP_MODE = 0x00, FORCED_MODE = 0x01, NORMAL_MODE = 0x03 };
 | 
			
		||||
 | 
			
		||||
/// Oversampling bit fields in the control and measurement register
 | 
			
		||||
enum Oversampling {
 | 
			
		||||
  OVERSAMPLING_NONE = 0x00,
 | 
			
		||||
  OVERSAMPLING_X2 = 0x01,
 | 
			
		||||
  OVERSAMPLING_X4 = 0x02,
 | 
			
		||||
  OVERSAMPLING_X8 = 0x03,
 | 
			
		||||
  OVERSAMPLING_X16 = 0x04,
 | 
			
		||||
  OVERSAMPLING_X32 = 0x05
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/// Infinite Impulse Response (IIR) filter bit field in the configuration register
 | 
			
		||||
enum IIRFilter {
 | 
			
		||||
  IIR_FILTER_OFF = 0x00,
 | 
			
		||||
  IIR_FILTER_2 = 0x01,
 | 
			
		||||
  IIR_FILTER_4 = 0x02,
 | 
			
		||||
  IIR_FILTER_8 = 0x03,
 | 
			
		||||
  IIR_FILTER_16 = 0x04,
 | 
			
		||||
  IIR_FILTER_32 = 0x05,
 | 
			
		||||
  IIR_FILTER_64 = 0x06,
 | 
			
		||||
  IIR_FILTER_128 = 0x07
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/// This class implements support for the BMP3XX Temperature+Pressure i2c sensor.
 | 
			
		||||
class BMP3XXComponent : public PollingComponent, public i2c::I2CDevice {
 | 
			
		||||
 public:
 | 
			
		||||
  void setup() override;
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
  float get_setup_priority() const override;
 | 
			
		||||
  void update() override;
 | 
			
		||||
 | 
			
		||||
  void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; }
 | 
			
		||||
  void set_pressure_sensor(sensor::Sensor *pressure_sensor) { pressure_sensor_ = pressure_sensor; }
 | 
			
		||||
 | 
			
		||||
  /// Set the oversampling value for the temperature sensor. Default is 16x.
 | 
			
		||||
  void set_temperature_oversampling_config(Oversampling temperature_oversampling) {
 | 
			
		||||
    this->temperature_oversampling_ = temperature_oversampling;
 | 
			
		||||
  }
 | 
			
		||||
  /// Set the oversampling value for the pressure sensor. Default is 16x.
 | 
			
		||||
  void set_pressure_oversampling_config(Oversampling pressure_oversampling) {
 | 
			
		||||
    this->pressure_oversampling_ = pressure_oversampling;
 | 
			
		||||
  }
 | 
			
		||||
  /// Set the IIR Filter used to increase accuracy, defaults to no IIR Filter.
 | 
			
		||||
  void set_iir_filter_config(IIRFilter iir_filter) { this->iir_filter_ = iir_filter; }
 | 
			
		||||
 | 
			
		||||
  /// Soft reset the sensor
 | 
			
		||||
  uint8_t reset();
 | 
			
		||||
  /// Start continuous measurement in NORMAL_MODE
 | 
			
		||||
  bool start_normal_conversion();
 | 
			
		||||
  /// Start a one shot measurement in FORCED_MODE
 | 
			
		||||
  bool start_forced_conversion();
 | 
			
		||||
  /// Stop the conversion and return to SLEEP_MODE
 | 
			
		||||
  bool stop_conversion();
 | 
			
		||||
  /// Set the pressure oversampling: OFF, X1, X2, X4, X8, X16, X32
 | 
			
		||||
  bool set_pressure_oversampling(Oversampling pressure_oversampling);
 | 
			
		||||
  /// Set the temperature oversampling: OFF, X1, X2, X4, X8, X16, X32
 | 
			
		||||
  bool set_temperature_oversampling(Oversampling temperature_oversampling);
 | 
			
		||||
  /// Set the IIR filter setting: OFF, 2, 3, 8, 16, 32
 | 
			
		||||
  bool set_iir_filter(IIRFilter iir_filter);
 | 
			
		||||
  /// Get a temperature measurement
 | 
			
		||||
  bool get_temperature(float &temperature);
 | 
			
		||||
  /// Get a pressure measurement
 | 
			
		||||
  bool get_pressure(float &pressure);
 | 
			
		||||
  /// Get a temperature and pressure measurement
 | 
			
		||||
  bool get_measurements(float &temperature, float &pressure);
 | 
			
		||||
  /// Get a temperature and pressure measurement
 | 
			
		||||
  bool get_measurement();
 | 
			
		||||
  /// Set the barometer mode
 | 
			
		||||
  bool set_mode(OperationMode mode);
 | 
			
		||||
  /// Set the BMP388 oversampling register
 | 
			
		||||
  bool set_oversampling_register(Oversampling pressure_oversampling, Oversampling temperature_oversampling);
 | 
			
		||||
  /// Checks if a measurement is ready
 | 
			
		||||
  bool data_ready();
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  Oversampling temperature_oversampling_{OVERSAMPLING_X16};
 | 
			
		||||
  Oversampling pressure_oversampling_{OVERSAMPLING_X16};
 | 
			
		||||
  IIRFilter iir_filter_{IIR_FILTER_OFF};
 | 
			
		||||
  OperationMode operation_mode_{FORCED_MODE};
 | 
			
		||||
  sensor::Sensor *temperature_sensor_;
 | 
			
		||||
  sensor::Sensor *pressure_sensor_;
 | 
			
		||||
  enum ErrorCode {
 | 
			
		||||
    NONE = 0,
 | 
			
		||||
    ERROR_COMMUNICATION_FAILED,
 | 
			
		||||
    ERROR_WRONG_CHIP_ID,
 | 
			
		||||
    ERROR_SENSOR_STATUS,
 | 
			
		||||
    ERROR_SENSOR_RESET,
 | 
			
		||||
  } error_code_{NONE};
 | 
			
		||||
 | 
			
		||||
  struct {  // The BMP388 compensation trim parameters (coefficients)
 | 
			
		||||
    uint16_t param_T1;
 | 
			
		||||
    uint16_t param_T2;
 | 
			
		||||
    int8_t param_T3;
 | 
			
		||||
    int16_t param_P1;
 | 
			
		||||
    int16_t param_P2;
 | 
			
		||||
    int8_t param_P3;
 | 
			
		||||
    int8_t param_P4;
 | 
			
		||||
    uint16_t param_P5;
 | 
			
		||||
    uint16_t param_P6;
 | 
			
		||||
    int8_t param_P7;
 | 
			
		||||
    int8_t param_P8;
 | 
			
		||||
    int16_t param_P9;
 | 
			
		||||
    int8_t param_P10;
 | 
			
		||||
    int8_t param_P11;
 | 
			
		||||
  } __attribute__((packed)) compensation_params_;
 | 
			
		||||
 | 
			
		||||
  struct FloatParams {  // The BMP388 float point compensation trim parameters
 | 
			
		||||
    float param_T1;
 | 
			
		||||
    float param_T2;
 | 
			
		||||
    float param_T3;
 | 
			
		||||
    float param_P1;
 | 
			
		||||
    float param_P2;
 | 
			
		||||
    float param_P3;
 | 
			
		||||
    float param_P4;
 | 
			
		||||
    float param_P5;
 | 
			
		||||
    float param_P6;
 | 
			
		||||
    float param_P7;
 | 
			
		||||
    float param_P8;
 | 
			
		||||
    float param_P9;
 | 
			
		||||
    float param_P10;
 | 
			
		||||
    float param_P11;
 | 
			
		||||
  } compensation_float_params_;
 | 
			
		||||
 | 
			
		||||
  union {  // Copy of the BMP388's chip id register
 | 
			
		||||
    struct {
 | 
			
		||||
      uint8_t chip_id_nvm : 4;
 | 
			
		||||
      uint8_t chip_id_fixed : 4;
 | 
			
		||||
    } bit;
 | 
			
		||||
    uint8_t reg;
 | 
			
		||||
  } chip_id_ = {.reg = 0};
 | 
			
		||||
 | 
			
		||||
  union {  // Copy of the BMP388's event register
 | 
			
		||||
    struct {
 | 
			
		||||
      uint8_t por_detected : 1;
 | 
			
		||||
    } bit;
 | 
			
		||||
    uint8_t reg;
 | 
			
		||||
  } event_ = {.reg = 0};
 | 
			
		||||
 | 
			
		||||
  union {  // Copy of the BMP388's interrupt status register
 | 
			
		||||
    struct {
 | 
			
		||||
      uint8_t fwm_int : 1;
 | 
			
		||||
      uint8_t ffull_int : 1;
 | 
			
		||||
      uint8_t : 1;
 | 
			
		||||
      uint8_t drdy : 1;
 | 
			
		||||
    } bit;
 | 
			
		||||
    uint8_t reg;
 | 
			
		||||
  } int_status_ = {.reg = 0};
 | 
			
		||||
 | 
			
		||||
  union {  // Copy of the BMP388's power control register
 | 
			
		||||
    struct {
 | 
			
		||||
      uint8_t press_en : 1;
 | 
			
		||||
      uint8_t temp_en : 1;
 | 
			
		||||
      uint8_t : 2;
 | 
			
		||||
      uint8_t mode : 2;
 | 
			
		||||
    } bit;
 | 
			
		||||
    uint8_t reg;
 | 
			
		||||
  } pwr_ctrl_ = {.reg = 0};
 | 
			
		||||
 | 
			
		||||
  union {  // Copy of the BMP388's oversampling register
 | 
			
		||||
    struct {
 | 
			
		||||
      uint8_t osr_p : 3;
 | 
			
		||||
      uint8_t osr_t : 3;
 | 
			
		||||
    } bit;
 | 
			
		||||
    uint8_t reg;
 | 
			
		||||
  } osr_ = {.reg = 0};
 | 
			
		||||
 | 
			
		||||
  union {  // Copy of the BMP388's output data rate register
 | 
			
		||||
    struct {
 | 
			
		||||
      uint8_t odr_sel : 5;
 | 
			
		||||
    } bit;
 | 
			
		||||
    uint8_t reg;
 | 
			
		||||
  } odr_ = {.reg = 0};
 | 
			
		||||
 | 
			
		||||
  union {  // Copy of the BMP388's configuration register
 | 
			
		||||
    struct {
 | 
			
		||||
      uint8_t : 1;
 | 
			
		||||
      uint8_t iir_filter : 3;
 | 
			
		||||
    } bit;
 | 
			
		||||
    uint8_t reg;
 | 
			
		||||
  } config_ = {.reg = 0};
 | 
			
		||||
 | 
			
		||||
  // Bosch temperature compensation function
 | 
			
		||||
  float bmp388_compensate_temperature_(float uncomp_temp);
 | 
			
		||||
  // Bosch pressure compensation function
 | 
			
		||||
  float bmp388_compensate_pressure_(float uncomp_press, float t_lin);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace bmp3xx
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										100
									
								
								esphome/components/bmp3xx/sensor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										100
									
								
								esphome/components/bmp3xx/sensor.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,100 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.components import i2c, sensor
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    CONF_IIR_FILTER,
 | 
			
		||||
    CONF_OVERSAMPLING,
 | 
			
		||||
    CONF_PRESSURE,
 | 
			
		||||
    CONF_TEMPERATURE,
 | 
			
		||||
    DEVICE_CLASS_PRESSURE,
 | 
			
		||||
    DEVICE_CLASS_TEMPERATURE,
 | 
			
		||||
    STATE_CLASS_MEASUREMENT,
 | 
			
		||||
    UNIT_CELSIUS,
 | 
			
		||||
    UNIT_HECTOPASCAL,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CODEOWNERS = ["@martgras"]
 | 
			
		||||
DEPENDENCIES = ["i2c"]
 | 
			
		||||
 | 
			
		||||
bmp3xx_ns = cg.esphome_ns.namespace("bmp3xx")
 | 
			
		||||
Oversampling = bmp3xx_ns.enum("Oversampling")
 | 
			
		||||
OVERSAMPLING_OPTIONS = {
 | 
			
		||||
    "NONE": Oversampling.OVERSAMPLING_NONE,
 | 
			
		||||
    "2X": Oversampling.OVERSAMPLING_X2,
 | 
			
		||||
    "4X": Oversampling.OVERSAMPLING_X4,
 | 
			
		||||
    "8X": Oversampling.OVERSAMPLING_X8,
 | 
			
		||||
    "16X": Oversampling.OVERSAMPLING_X16,
 | 
			
		||||
    "32x": Oversampling.OVERSAMPLING_X32,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
IIRFilter = bmp3xx_ns.enum("IIRFilter")
 | 
			
		||||
IIR_FILTER_OPTIONS = {
 | 
			
		||||
    "OFF": IIRFilter.IIR_FILTER_OFF,
 | 
			
		||||
    "2X": IIRFilter.IIR_FILTER_2,
 | 
			
		||||
    "4X": IIRFilter.IIR_FILTER_4,
 | 
			
		||||
    "8X": IIRFilter.IIR_FILTER_8,
 | 
			
		||||
    "16X": IIRFilter.IIR_FILTER_16,
 | 
			
		||||
    "32X": IIRFilter.IIR_FILTER_32,
 | 
			
		||||
    "64X": IIRFilter.IIR_FILTER_64,
 | 
			
		||||
    "128X": IIRFilter.IIR_FILTER_128,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
BMP3XXComponent = bmp3xx_ns.class_(
 | 
			
		||||
    "BMP3XXComponent", cg.PollingComponent, i2c.I2CDevice
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = (
 | 
			
		||||
    cv.Schema(
 | 
			
		||||
        {
 | 
			
		||||
            cv.GenerateID(): cv.declare_id(BMP3XXComponent),
 | 
			
		||||
            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="2X"): 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_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)
 | 
			
		||||
    cg.add(var.set_iir_filter_config(config[CONF_IIR_FILTER]))
 | 
			
		||||
    if CONF_TEMPERATURE in config:
 | 
			
		||||
        conf = config[CONF_TEMPERATURE]
 | 
			
		||||
        sens = await sensor.new_sensor(conf)
 | 
			
		||||
        cg.add(var.set_temperature_sensor(sens))
 | 
			
		||||
        cg.add(var.set_temperature_oversampling_config(conf[CONF_OVERSAMPLING]))
 | 
			
		||||
 | 
			
		||||
    if CONF_PRESSURE in config:
 | 
			
		||||
        conf = config[CONF_PRESSURE]
 | 
			
		||||
        sens = await sensor.new_sensor(conf)
 | 
			
		||||
        cg.add(var.set_pressure_sensor(sens))
 | 
			
		||||
        cg.add(var.set_pressure_oversampling_config(conf[CONF_OVERSAMPLING]))
 | 
			
		||||
							
								
								
									
										53
									
								
								esphome/components/cd74hc4067/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								esphome/components/cd74hc4067/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,53 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
from esphome import pins
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_DELAY,
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CODEOWNERS = ["@asoehlke"]
 | 
			
		||||
AUTO_LOAD = ["sensor", "voltage_sampler"]
 | 
			
		||||
 | 
			
		||||
cd74hc4067_ns = cg.esphome_ns.namespace("cd74hc4067")
 | 
			
		||||
 | 
			
		||||
CD74HC4067Component = cd74hc4067_ns.class_(
 | 
			
		||||
    "CD74HC4067Component", cg.Component, cg.PollingComponent
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CONF_PIN_S0 = "pin_s0"
 | 
			
		||||
CONF_PIN_S1 = "pin_s1"
 | 
			
		||||
CONF_PIN_S2 = "pin_s2"
 | 
			
		||||
CONF_PIN_S3 = "pin_s3"
 | 
			
		||||
 | 
			
		||||
DEFAULT_DELAY = "2ms"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = cv.Schema(
 | 
			
		||||
    {
 | 
			
		||||
        cv.GenerateID(): cv.declare_id(CD74HC4067Component),
 | 
			
		||||
        cv.Required(CONF_PIN_S0): pins.internal_gpio_output_pin_schema,
 | 
			
		||||
        cv.Required(CONF_PIN_S1): pins.internal_gpio_output_pin_schema,
 | 
			
		||||
        cv.Required(CONF_PIN_S2): pins.internal_gpio_output_pin_schema,
 | 
			
		||||
        cv.Required(CONF_PIN_S3): pins.internal_gpio_output_pin_schema,
 | 
			
		||||
        cv.Optional(
 | 
			
		||||
            CONF_DELAY, default=DEFAULT_DELAY
 | 
			
		||||
        ): cv.positive_time_period_milliseconds,
 | 
			
		||||
    }
 | 
			
		||||
).extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
 | 
			
		||||
    pin_s0 = await cg.gpio_pin_expression(config[CONF_PIN_S0])
 | 
			
		||||
    cg.add(var.set_pin_s0(pin_s0))
 | 
			
		||||
    pin_s1 = await cg.gpio_pin_expression(config[CONF_PIN_S1])
 | 
			
		||||
    cg.add(var.set_pin_s1(pin_s1))
 | 
			
		||||
    pin_s2 = await cg.gpio_pin_expression(config[CONF_PIN_S2])
 | 
			
		||||
    cg.add(var.set_pin_s2(pin_s2))
 | 
			
		||||
    pin_s3 = await cg.gpio_pin_expression(config[CONF_PIN_S3])
 | 
			
		||||
    cg.add(var.set_pin_s3(pin_s3))
 | 
			
		||||
 | 
			
		||||
    cg.add(var.set_switch_delay(config[CONF_DELAY]))
 | 
			
		||||
							
								
								
									
										86
									
								
								esphome/components/cd74hc4067/cd74hc4067.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										86
									
								
								esphome/components/cd74hc4067/cd74hc4067.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,86 @@
 | 
			
		||||
#include "cd74hc4067.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace cd74hc4067 {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "cd74hc4067";
 | 
			
		||||
 | 
			
		||||
float CD74HC4067Component::get_setup_priority() const { return setup_priority::DATA; }
 | 
			
		||||
 | 
			
		||||
void CD74HC4067Component::setup() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Setting up CD74HC4067...");
 | 
			
		||||
 | 
			
		||||
  this->pin_s0_->setup();
 | 
			
		||||
  this->pin_s1_->setup();
 | 
			
		||||
  this->pin_s2_->setup();
 | 
			
		||||
  this->pin_s3_->setup();
 | 
			
		||||
 | 
			
		||||
  // set other pin, so that activate_pin will really switch
 | 
			
		||||
  this->active_pin_ = 1;
 | 
			
		||||
  this->activate_pin(0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CD74HC4067Component::dump_config() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "CD74HC4067 Multiplexer:");
 | 
			
		||||
  LOG_PIN("  S0 Pin: ", this->pin_s0_);
 | 
			
		||||
  LOG_PIN("  S1 Pin: ", this->pin_s1_);
 | 
			
		||||
  LOG_PIN("  S2 Pin: ", this->pin_s2_);
 | 
			
		||||
  LOG_PIN("  S3 Pin: ", this->pin_s3_);
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "switch delay: %d", this->switch_delay_);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CD74HC4067Component::activate_pin(uint8_t pin) {
 | 
			
		||||
  if (this->active_pin_ != pin) {
 | 
			
		||||
    ESP_LOGD(TAG, "switch to input %d", pin);
 | 
			
		||||
 | 
			
		||||
    static int mux_channel[16][4] = {
 | 
			
		||||
        {0, 0, 0, 0},  // channel 0
 | 
			
		||||
        {1, 0, 0, 0},  // channel 1
 | 
			
		||||
        {0, 1, 0, 0},  // channel 2
 | 
			
		||||
        {1, 1, 0, 0},  // channel 3
 | 
			
		||||
        {0, 0, 1, 0},  // channel 4
 | 
			
		||||
        {1, 0, 1, 0},  // channel 5
 | 
			
		||||
        {0, 1, 1, 0},  // channel 6
 | 
			
		||||
        {1, 1, 1, 0},  // channel 7
 | 
			
		||||
        {0, 0, 0, 1},  // channel 8
 | 
			
		||||
        {1, 0, 0, 1},  // channel 9
 | 
			
		||||
        {0, 1, 0, 1},  // channel 10
 | 
			
		||||
        {1, 1, 0, 1},  // channel 11
 | 
			
		||||
        {0, 0, 1, 1},  // channel 12
 | 
			
		||||
        {1, 0, 1, 1},  // channel 13
 | 
			
		||||
        {0, 1, 1, 1},  // channel 14
 | 
			
		||||
        {1, 1, 1, 1}   // channel 15
 | 
			
		||||
    };
 | 
			
		||||
    this->pin_s0_->digital_write(mux_channel[pin][0]);
 | 
			
		||||
    this->pin_s1_->digital_write(mux_channel[pin][1]);
 | 
			
		||||
    this->pin_s2_->digital_write(mux_channel[pin][2]);
 | 
			
		||||
    this->pin_s3_->digital_write(mux_channel[pin][3]);
 | 
			
		||||
    // small delay is needed to let the multiplexer switch
 | 
			
		||||
    delay(this->switch_delay_);
 | 
			
		||||
    this->active_pin_ = pin;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
CD74HC4067Sensor::CD74HC4067Sensor(CD74HC4067Component *parent) : parent_(parent) {}
 | 
			
		||||
 | 
			
		||||
void CD74HC4067Sensor::update() {
 | 
			
		||||
  float value_v = this->sample();
 | 
			
		||||
  this->publish_state(value_v);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float CD74HC4067Sensor::get_setup_priority() const { return this->parent_->get_setup_priority() - 1.0f; }
 | 
			
		||||
 | 
			
		||||
float CD74HC4067Sensor::sample() {
 | 
			
		||||
  this->parent_->activate_pin(this->pin_);
 | 
			
		||||
  return this->source_->sample();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CD74HC4067Sensor::dump_config() {
 | 
			
		||||
  LOG_SENSOR(TAG, "CD74HC4067 Sensor", this);
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Pin: %u", this->pin_);
 | 
			
		||||
  LOG_UPDATE_INTERVAL(this);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace cd74hc4067
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										65
									
								
								esphome/components/cd74hc4067/cd74hc4067.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								esphome/components/cd74hc4067/cd74hc4067.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,65 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/core/hal.h"
 | 
			
		||||
#include "esphome/components/sensor/sensor.h"
 | 
			
		||||
#include "esphome/components/voltage_sampler/voltage_sampler.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace cd74hc4067 {
 | 
			
		||||
 | 
			
		||||
class CD74HC4067Component : public Component {
 | 
			
		||||
 public:
 | 
			
		||||
  /// Set up the internal sensor array.
 | 
			
		||||
  void setup() override;
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
  float get_setup_priority() const override;
 | 
			
		||||
 | 
			
		||||
  /// setting pin active by setting the right combination of the four multiplexer input pins
 | 
			
		||||
  void activate_pin(uint8_t pin);
 | 
			
		||||
 | 
			
		||||
  /// set the pin connected to multiplexer control pin 0
 | 
			
		||||
  void set_pin_s0(InternalGPIOPin *pin) { this->pin_s0_ = pin; }
 | 
			
		||||
  /// set the pin connected to multiplexer control pin 1
 | 
			
		||||
  void set_pin_s1(InternalGPIOPin *pin) { this->pin_s1_ = pin; }
 | 
			
		||||
  /// set the pin connected to multiplexer control pin 2
 | 
			
		||||
  void set_pin_s2(InternalGPIOPin *pin) { this->pin_s2_ = pin; }
 | 
			
		||||
  /// set the pin connected to multiplexer control pin 3
 | 
			
		||||
  void set_pin_s3(InternalGPIOPin *pin) { this->pin_s3_ = pin; }
 | 
			
		||||
 | 
			
		||||
  /// set the delay needed after an input switch
 | 
			
		||||
  void set_switch_delay(uint32_t switch_delay) { this->switch_delay_ = switch_delay; }
 | 
			
		||||
 | 
			
		||||
 private:
 | 
			
		||||
  InternalGPIOPin *pin_s0_;
 | 
			
		||||
  InternalGPIOPin *pin_s1_;
 | 
			
		||||
  InternalGPIOPin *pin_s2_;
 | 
			
		||||
  InternalGPIOPin *pin_s3_;
 | 
			
		||||
  /// the currently active pin
 | 
			
		||||
  uint8_t active_pin_;
 | 
			
		||||
  uint32_t switch_delay_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class CD74HC4067Sensor : public sensor::Sensor, public PollingComponent, public voltage_sampler::VoltageSampler {
 | 
			
		||||
 public:
 | 
			
		||||
  CD74HC4067Sensor(CD74HC4067Component *parent);
 | 
			
		||||
 | 
			
		||||
  void update() override;
 | 
			
		||||
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
  /// `HARDWARE_LATE` setup priority.
 | 
			
		||||
  float get_setup_priority() const override;
 | 
			
		||||
  void set_pin(uint8_t pin) { this->pin_ = pin; }
 | 
			
		||||
  void set_source(voltage_sampler::VoltageSampler *source) { this->source_ = source; }
 | 
			
		||||
 | 
			
		||||
  float sample() override;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  CD74HC4067Component *parent_;
 | 
			
		||||
  /// The sampling source to read values from.
 | 
			
		||||
  voltage_sampler::VoltageSampler *source_;
 | 
			
		||||
 | 
			
		||||
  uint8_t pin_;
 | 
			
		||||
};
 | 
			
		||||
}  // namespace cd74hc4067
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										55
									
								
								esphome/components/cd74hc4067/sensor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								esphome/components/cd74hc4067/sensor.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,55 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.components import sensor, voltage_sampler
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    CONF_SENSOR,
 | 
			
		||||
    CONF_NUMBER,
 | 
			
		||||
    ICON_FLASH,
 | 
			
		||||
    UNIT_VOLT,
 | 
			
		||||
    STATE_CLASS_MEASUREMENT,
 | 
			
		||||
    DEVICE_CLASS_VOLTAGE,
 | 
			
		||||
)
 | 
			
		||||
from . import cd74hc4067_ns, CD74HC4067Component
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["cd74hc4067"]
 | 
			
		||||
 | 
			
		||||
CD74HC4067Sensor = cd74hc4067_ns.class_(
 | 
			
		||||
    "CD74HC4067Sensor",
 | 
			
		||||
    sensor.Sensor,
 | 
			
		||||
    cg.PollingComponent,
 | 
			
		||||
    voltage_sampler.VoltageSampler,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CONF_CD74HC4067_ID = "cd74hc4067_id"
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = (
 | 
			
		||||
    sensor.sensor_schema(
 | 
			
		||||
        unit_of_measurement=UNIT_VOLT,
 | 
			
		||||
        accuracy_decimals=3,
 | 
			
		||||
        device_class=DEVICE_CLASS_VOLTAGE,
 | 
			
		||||
        state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
        icon=ICON_FLASH,
 | 
			
		||||
    )
 | 
			
		||||
    .extend(
 | 
			
		||||
        {
 | 
			
		||||
            cv.GenerateID(): cv.declare_id(CD74HC4067Sensor),
 | 
			
		||||
            cv.GenerateID(CONF_CD74HC4067_ID): cv.use_id(CD74HC4067Component),
 | 
			
		||||
            cv.Required(CONF_NUMBER): cv.int_range(0, 15),
 | 
			
		||||
            cv.Required(CONF_SENSOR): cv.use_id(voltage_sampler.VoltageSampler),
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
    .extend(cv.polling_component_schema("60s"))
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    parent = await cg.get_variable(config[CONF_CD74HC4067_ID])
 | 
			
		||||
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID], parent)
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
    await sensor.register_sensor(var, config)
 | 
			
		||||
    cg.add(var.set_pin(config[CONF_NUMBER]))
 | 
			
		||||
 | 
			
		||||
    sens = await cg.get_variable(config[CONF_SENSOR])
 | 
			
		||||
    cg.add(var.set_source(sens))
 | 
			
		||||
@@ -10,21 +10,22 @@ climate::ClimateTraits ClimateIR::traits() {
 | 
			
		||||
  auto traits = climate::ClimateTraits();
 | 
			
		||||
  traits.set_supports_current_temperature(this->sensor_ != nullptr);
 | 
			
		||||
  traits.set_supported_modes({climate::CLIMATE_MODE_OFF, climate::CLIMATE_MODE_HEAT_COOL});
 | 
			
		||||
  if (supports_cool_)
 | 
			
		||||
  if (this->supports_cool_)
 | 
			
		||||
    traits.add_supported_mode(climate::CLIMATE_MODE_COOL);
 | 
			
		||||
  if (supports_heat_)
 | 
			
		||||
  if (this->supports_heat_)
 | 
			
		||||
    traits.add_supported_mode(climate::CLIMATE_MODE_HEAT);
 | 
			
		||||
  if (supports_dry_)
 | 
			
		||||
  if (this->supports_dry_)
 | 
			
		||||
    traits.add_supported_mode(climate::CLIMATE_MODE_DRY);
 | 
			
		||||
  if (supports_fan_only_)
 | 
			
		||||
  if (this->supports_fan_only_)
 | 
			
		||||
    traits.add_supported_mode(climate::CLIMATE_MODE_FAN_ONLY);
 | 
			
		||||
 | 
			
		||||
  traits.set_supports_two_point_target_temperature(false);
 | 
			
		||||
  traits.set_visual_min_temperature(this->minimum_temperature_);
 | 
			
		||||
  traits.set_visual_max_temperature(this->maximum_temperature_);
 | 
			
		||||
  traits.set_visual_temperature_step(this->temperature_step_);
 | 
			
		||||
  traits.set_supported_fan_modes(fan_modes_);
 | 
			
		||||
  traits.set_supported_swing_modes(swing_modes_);
 | 
			
		||||
  traits.set_supported_fan_modes(this->fan_modes_);
 | 
			
		||||
  traits.set_supported_swing_modes(this->swing_modes_);
 | 
			
		||||
  traits.set_supported_presets(this->presets_);
 | 
			
		||||
  return traits;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -50,6 +51,7 @@ void ClimateIR::setup() {
 | 
			
		||||
        roundf(clamp(this->current_temperature, this->minimum_temperature_, this->maximum_temperature_));
 | 
			
		||||
    this->fan_mode = climate::CLIMATE_FAN_AUTO;
 | 
			
		||||
    this->swing_mode = climate::CLIMATE_SWING_OFF;
 | 
			
		||||
    this->preset = climate::CLIMATE_PRESET_NONE;
 | 
			
		||||
  }
 | 
			
		||||
  // Never send nan to HA
 | 
			
		||||
  if (std::isnan(this->target_temperature))
 | 
			
		||||
@@ -65,6 +67,8 @@ void ClimateIR::control(const climate::ClimateCall &call) {
 | 
			
		||||
    this->fan_mode = *call.get_fan_mode();
 | 
			
		||||
  if (call.get_swing_mode().has_value())
 | 
			
		||||
    this->swing_mode = *call.get_swing_mode();
 | 
			
		||||
  if (call.get_preset().has_value())
 | 
			
		||||
    this->preset = *call.get_preset();
 | 
			
		||||
  this->transmit_state();
 | 
			
		||||
  this->publish_state();
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -22,7 +22,7 @@ class ClimateIR : public climate::Climate, public Component, public remote_base:
 | 
			
		||||
 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 = {},
 | 
			
		||||
            std::set<climate::ClimateSwingMode> swing_modes = {}) {
 | 
			
		||||
            std::set<climate::ClimateSwingMode> swing_modes = {}, std::set<climate::ClimatePreset> presets = {}) {
 | 
			
		||||
    this->minimum_temperature_ = minimum_temperature;
 | 
			
		||||
    this->maximum_temperature_ = maximum_temperature;
 | 
			
		||||
    this->temperature_step_ = temperature_step;
 | 
			
		||||
@@ -30,6 +30,7 @@ class ClimateIR : public climate::Climate, public Component, public remote_base:
 | 
			
		||||
    this->supports_fan_only_ = supports_fan_only;
 | 
			
		||||
    this->fan_modes_ = std::move(fan_modes);
 | 
			
		||||
    this->swing_modes_ = std::move(swing_modes);
 | 
			
		||||
    this->presets_ = std::move(presets);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  void setup() override;
 | 
			
		||||
@@ -61,6 +62,7 @@ class ClimateIR : public climate::Climate, public Component, public remote_base:
 | 
			
		||||
  bool supports_fan_only_{false};
 | 
			
		||||
  std::set<climate::ClimateFanMode> fan_modes_ = {};
 | 
			
		||||
  std::set<climate::ClimateSwingMode> swing_modes_ = {};
 | 
			
		||||
  std::set<climate::ClimatePreset> presets_ = {};
 | 
			
		||||
 | 
			
		||||
  remote_transmitter::RemoteTransmitterComponent *transmitter_;
 | 
			
		||||
  sensor::Sensor *sensor_{nullptr};
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,5 @@
 | 
			
		||||
#include "coolix.h"
 | 
			
		||||
#include "esphome/components/remote_base/coolix_protocol.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
@@ -6,29 +7,29 @@ namespace coolix {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "coolix.climate";
 | 
			
		||||
 | 
			
		||||
const uint32_t COOLIX_OFF = 0xB27BE0;
 | 
			
		||||
const uint32_t COOLIX_SWING = 0xB26BE0;
 | 
			
		||||
const uint32_t COOLIX_LED = 0xB5F5A5;
 | 
			
		||||
const uint32_t COOLIX_SILENCE_FP = 0xB5F5B6;
 | 
			
		||||
static const uint32_t COOLIX_OFF = 0xB27BE0;
 | 
			
		||||
static const uint32_t COOLIX_SWING = 0xB26BE0;
 | 
			
		||||
static const uint32_t COOLIX_LED = 0xB5F5A5;
 | 
			
		||||
static const uint32_t COOLIX_SILENCE_FP = 0xB5F5B6;
 | 
			
		||||
 | 
			
		||||
// On, 25C, Mode: Auto, Fan: Auto, Zone Follow: Off, Sensor Temp: Ignore.
 | 
			
		||||
const uint8_t COOLIX_COOL = 0b0000;
 | 
			
		||||
const uint8_t COOLIX_DRY_FAN = 0b0100;
 | 
			
		||||
const uint8_t COOLIX_AUTO = 0b1000;
 | 
			
		||||
const uint8_t COOLIX_HEAT = 0b1100;
 | 
			
		||||
const uint32_t COOLIX_MODE_MASK = 0b1100;
 | 
			
		||||
const uint32_t COOLIX_FAN_MASK = 0xF000;
 | 
			
		||||
const uint32_t COOLIX_FAN_MODE_AUTO_DRY = 0x1000;
 | 
			
		||||
const uint32_t COOLIX_FAN_AUTO = 0xB000;
 | 
			
		||||
const uint32_t COOLIX_FAN_MIN = 0x9000;
 | 
			
		||||
const uint32_t COOLIX_FAN_MED = 0x5000;
 | 
			
		||||
const uint32_t COOLIX_FAN_MAX = 0x3000;
 | 
			
		||||
static const uint8_t COOLIX_COOL = 0b0000;
 | 
			
		||||
static const uint8_t COOLIX_DRY_FAN = 0b0100;
 | 
			
		||||
static const uint8_t COOLIX_AUTO = 0b1000;
 | 
			
		||||
static const uint8_t COOLIX_HEAT = 0b1100;
 | 
			
		||||
static const uint32_t COOLIX_MODE_MASK = 0b1100;
 | 
			
		||||
static const uint32_t COOLIX_FAN_MASK = 0xF000;
 | 
			
		||||
static const uint32_t COOLIX_FAN_MODE_AUTO_DRY = 0x1000;
 | 
			
		||||
static const uint32_t COOLIX_FAN_AUTO = 0xB000;
 | 
			
		||||
static const uint32_t COOLIX_FAN_MIN = 0x9000;
 | 
			
		||||
static const uint32_t COOLIX_FAN_MED = 0x5000;
 | 
			
		||||
static const uint32_t COOLIX_FAN_MAX = 0x3000;
 | 
			
		||||
 | 
			
		||||
// Temperature
 | 
			
		||||
const uint8_t COOLIX_TEMP_RANGE = COOLIX_TEMP_MAX - COOLIX_TEMP_MIN + 1;
 | 
			
		||||
const uint8_t COOLIX_FAN_TEMP_CODE = 0b11100000;  // Part of Fan Mode.
 | 
			
		||||
const uint32_t COOLIX_TEMP_MASK = 0b11110000;
 | 
			
		||||
const uint8_t COOLIX_TEMP_MAP[COOLIX_TEMP_RANGE] = {
 | 
			
		||||
static const uint8_t COOLIX_TEMP_RANGE = COOLIX_TEMP_MAX - COOLIX_TEMP_MIN + 1;
 | 
			
		||||
static const uint8_t COOLIX_FAN_TEMP_CODE = 0b11100000;  // Part of Fan Mode.
 | 
			
		||||
static const uint32_t COOLIX_TEMP_MASK = 0b11110000;
 | 
			
		||||
static const uint8_t COOLIX_TEMP_MAP[COOLIX_TEMP_RANGE] = {
 | 
			
		||||
    0b00000000,  // 17C
 | 
			
		||||
    0b00010000,  // 18c
 | 
			
		||||
    0b00110000,  // 19C
 | 
			
		||||
@@ -45,17 +46,6 @@ const uint8_t COOLIX_TEMP_MAP[COOLIX_TEMP_RANGE] = {
 | 
			
		||||
    0b10110000   // 30C
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// Constants
 | 
			
		||||
static const uint32_t BIT_MARK_US = 660;
 | 
			
		||||
static const uint32_t HEADER_MARK_US = 560 * 8;
 | 
			
		||||
static const uint32_t HEADER_SPACE_US = 560 * 8;
 | 
			
		||||
static const uint32_t BIT_ONE_SPACE_US = 1500;
 | 
			
		||||
static const uint32_t BIT_ZERO_SPACE_US = 450;
 | 
			
		||||
static const uint32_t FOOTER_MARK_US = BIT_MARK_US;
 | 
			
		||||
static const uint32_t FOOTER_SPACE_US = HEADER_SPACE_US;
 | 
			
		||||
 | 
			
		||||
const uint16_t COOLIX_BITS = 24;
 | 
			
		||||
 | 
			
		||||
void CoolixClimate::transmit_state() {
 | 
			
		||||
  uint32_t remote_state = 0xB20F00;
 | 
			
		||||
 | 
			
		||||
@@ -111,119 +101,60 @@ void CoolixClimate::transmit_state() {
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGV(TAG, "Sending coolix code: 0x%02X", remote_state);
 | 
			
		||||
  ESP_LOGV(TAG, "Sending coolix code: 0x%06X", remote_state);
 | 
			
		||||
 | 
			
		||||
  auto transmit = this->transmitter_->transmit();
 | 
			
		||||
  auto data = transmit.get_data();
 | 
			
		||||
 | 
			
		||||
  data->set_carrier_frequency(38000);
 | 
			
		||||
  uint16_t repeat = 1;
 | 
			
		||||
  for (uint16_t r = 0; r <= repeat; r++) {
 | 
			
		||||
    // Header
 | 
			
		||||
    data->mark(HEADER_MARK_US);
 | 
			
		||||
    data->space(HEADER_SPACE_US);
 | 
			
		||||
    // Data
 | 
			
		||||
    //   Break data into bytes, starting at the Most Significant
 | 
			
		||||
    //   Byte. Each byte then being sent normal, then followed inverted.
 | 
			
		||||
    for (uint16_t i = 8; i <= COOLIX_BITS; i += 8) {
 | 
			
		||||
      // Grab a bytes worth of data.
 | 
			
		||||
      uint8_t byte = (remote_state >> (COOLIX_BITS - i)) & 0xFF;
 | 
			
		||||
      // Normal
 | 
			
		||||
      for (uint64_t mask = 1ULL << 7; mask; mask >>= 1) {
 | 
			
		||||
        data->mark(BIT_MARK_US);
 | 
			
		||||
        data->space((byte & mask) ? BIT_ONE_SPACE_US : BIT_ZERO_SPACE_US);
 | 
			
		||||
      }
 | 
			
		||||
      // Inverted
 | 
			
		||||
      for (uint64_t mask = 1ULL << 7; mask; mask >>= 1) {
 | 
			
		||||
        data->mark(BIT_MARK_US);
 | 
			
		||||
        data->space(!(byte & mask) ? BIT_ONE_SPACE_US : BIT_ZERO_SPACE_US);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    // Footer
 | 
			
		||||
    data->mark(BIT_MARK_US);
 | 
			
		||||
    data->space(FOOTER_SPACE_US);  // Pause before repeating
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  remote_base::CoolixProtocol().encode(data, remote_state);
 | 
			
		||||
  transmit.perform();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool CoolixClimate::on_receive(remote_base::RemoteReceiveData data) {
 | 
			
		||||
bool CoolixClimate::on_coolix(climate::Climate *parent, remote_base::RemoteReceiveData data) {
 | 
			
		||||
  auto decoded = remote_base::CoolixProtocol().decode(data);
 | 
			
		||||
  if (!decoded.has_value())
 | 
			
		||||
    return false;
 | 
			
		||||
  // Decoded remote state y 3 bytes long code.
 | 
			
		||||
  uint32_t remote_state = 0;
 | 
			
		||||
  // The protocol sends the data twice, read here
 | 
			
		||||
  uint32_t loop_read;
 | 
			
		||||
  for (uint16_t loop = 1; loop <= 2; loop++) {
 | 
			
		||||
    if (!data.expect_item(HEADER_MARK_US, HEADER_SPACE_US))
 | 
			
		||||
      return false;
 | 
			
		||||
    loop_read = 0;
 | 
			
		||||
    for (uint8_t a_byte = 0; a_byte < 3; a_byte++) {
 | 
			
		||||
      uint8_t byte = 0;
 | 
			
		||||
      for (int8_t a_bit = 7; a_bit >= 0; a_bit--) {
 | 
			
		||||
        if (data.expect_item(BIT_MARK_US, BIT_ONE_SPACE_US))
 | 
			
		||||
          byte |= 1 << a_bit;
 | 
			
		||||
        else if (!data.expect_item(BIT_MARK_US, BIT_ZERO_SPACE_US))
 | 
			
		||||
          return false;
 | 
			
		||||
      }
 | 
			
		||||
      // Need to see this segment inverted
 | 
			
		||||
      for (int8_t a_bit = 7; a_bit >= 0; a_bit--) {
 | 
			
		||||
        bool bit = byte & (1 << a_bit);
 | 
			
		||||
        if (!data.expect_item(BIT_MARK_US, bit ? BIT_ZERO_SPACE_US : BIT_ONE_SPACE_US))
 | 
			
		||||
          return false;
 | 
			
		||||
      }
 | 
			
		||||
      // Receiving MSB first: reorder bytes
 | 
			
		||||
      loop_read |= byte << ((2 - a_byte) * 8);
 | 
			
		||||
    }
 | 
			
		||||
    // Footer Mark
 | 
			
		||||
    if (!data.expect_mark(BIT_MARK_US))
 | 
			
		||||
      return false;
 | 
			
		||||
    if (loop == 1) {
 | 
			
		||||
      // Back up state on first loop
 | 
			
		||||
      remote_state = loop_read;
 | 
			
		||||
      if (!data.expect_space(FOOTER_SPACE_US))
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ESP_LOGV(TAG, "Decoded 0x%02X", remote_state);
 | 
			
		||||
  if (remote_state != loop_read || (remote_state & 0xFF0000) != 0xB20000)
 | 
			
		||||
  uint32_t remote_state = *decoded;
 | 
			
		||||
  ESP_LOGV(TAG, "Decoded 0x%06X", remote_state);
 | 
			
		||||
  if ((remote_state & 0xFF0000) != 0xB20000)
 | 
			
		||||
    return false;
 | 
			
		||||
 | 
			
		||||
  if (remote_state == COOLIX_OFF) {
 | 
			
		||||
    this->mode = climate::CLIMATE_MODE_OFF;
 | 
			
		||||
    parent->mode = climate::CLIMATE_MODE_OFF;
 | 
			
		||||
  } else if (remote_state == COOLIX_SWING) {
 | 
			
		||||
    this->swing_mode =
 | 
			
		||||
        this->swing_mode == climate::CLIMATE_SWING_OFF ? climate::CLIMATE_SWING_VERTICAL : climate::CLIMATE_SWING_OFF;
 | 
			
		||||
    parent->swing_mode =
 | 
			
		||||
        parent->swing_mode == climate::CLIMATE_SWING_OFF ? climate::CLIMATE_SWING_VERTICAL : climate::CLIMATE_SWING_OFF;
 | 
			
		||||
  } else {
 | 
			
		||||
    if ((remote_state & COOLIX_MODE_MASK) == COOLIX_HEAT)
 | 
			
		||||
      this->mode = climate::CLIMATE_MODE_HEAT;
 | 
			
		||||
      parent->mode = climate::CLIMATE_MODE_HEAT;
 | 
			
		||||
    else if ((remote_state & COOLIX_MODE_MASK) == COOLIX_AUTO)
 | 
			
		||||
      this->mode = climate::CLIMATE_MODE_HEAT_COOL;
 | 
			
		||||
      parent->mode = climate::CLIMATE_MODE_HEAT_COOL;
 | 
			
		||||
    else if ((remote_state & COOLIX_MODE_MASK) == COOLIX_DRY_FAN) {
 | 
			
		||||
      if ((remote_state & COOLIX_FAN_MASK) == COOLIX_FAN_MODE_AUTO_DRY)
 | 
			
		||||
        this->mode = climate::CLIMATE_MODE_DRY;
 | 
			
		||||
        parent->mode = climate::CLIMATE_MODE_DRY;
 | 
			
		||||
      else
 | 
			
		||||
        this->mode = climate::CLIMATE_MODE_FAN_ONLY;
 | 
			
		||||
        parent->mode = climate::CLIMATE_MODE_FAN_ONLY;
 | 
			
		||||
    } else
 | 
			
		||||
      this->mode = climate::CLIMATE_MODE_COOL;
 | 
			
		||||
      parent->mode = climate::CLIMATE_MODE_COOL;
 | 
			
		||||
 | 
			
		||||
    // Fan Speed
 | 
			
		||||
    if ((remote_state & COOLIX_FAN_AUTO) == COOLIX_FAN_AUTO || this->mode == climate::CLIMATE_MODE_HEAT_COOL ||
 | 
			
		||||
        this->mode == climate::CLIMATE_MODE_DRY)
 | 
			
		||||
      this->fan_mode = climate::CLIMATE_FAN_AUTO;
 | 
			
		||||
    if ((remote_state & COOLIX_FAN_AUTO) == COOLIX_FAN_AUTO || parent->mode == climate::CLIMATE_MODE_HEAT_COOL ||
 | 
			
		||||
        parent->mode == climate::CLIMATE_MODE_DRY)
 | 
			
		||||
      parent->fan_mode = climate::CLIMATE_FAN_AUTO;
 | 
			
		||||
    else if ((remote_state & COOLIX_FAN_MIN) == COOLIX_FAN_MIN)
 | 
			
		||||
      this->fan_mode = climate::CLIMATE_FAN_LOW;
 | 
			
		||||
      parent->fan_mode = climate::CLIMATE_FAN_LOW;
 | 
			
		||||
    else if ((remote_state & COOLIX_FAN_MED) == COOLIX_FAN_MED)
 | 
			
		||||
      this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
 | 
			
		||||
      parent->fan_mode = climate::CLIMATE_FAN_MEDIUM;
 | 
			
		||||
    else if ((remote_state & COOLIX_FAN_MAX) == COOLIX_FAN_MAX)
 | 
			
		||||
      this->fan_mode = climate::CLIMATE_FAN_HIGH;
 | 
			
		||||
      parent->fan_mode = climate::CLIMATE_FAN_HIGH;
 | 
			
		||||
 | 
			
		||||
    // Temperature
 | 
			
		||||
    uint8_t temperature_code = remote_state & COOLIX_TEMP_MASK;
 | 
			
		||||
    for (uint8_t i = 0; i < COOLIX_TEMP_RANGE; i++)
 | 
			
		||||
      if (COOLIX_TEMP_MAP[i] == temperature_code)
 | 
			
		||||
        this->target_temperature = i + COOLIX_TEMP_MIN;
 | 
			
		||||
        parent->target_temperature = i + COOLIX_TEMP_MIN;
 | 
			
		||||
  }
 | 
			
		||||
  this->publish_state();
 | 
			
		||||
  parent->publish_state();
 | 
			
		||||
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -26,11 +26,15 @@ class CoolixClimate : public climate_ir::ClimateIR {
 | 
			
		||||
    climate_ir::ClimateIR::control(call);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// This static method can be used in other climate components that accept the Coolix protocol. See midea_ir for
 | 
			
		||||
  /// example.
 | 
			
		||||
  static bool on_coolix(climate::Climate *parent, remote_base::RemoteReceiveData data);
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  /// Transmit via IR the state of this climate controller.
 | 
			
		||||
  void transmit_state() override;
 | 
			
		||||
  /// Handle received IR Buffer
 | 
			
		||||
  bool on_receive(remote_base::RemoteReceiveData data) override;
 | 
			
		||||
  bool on_receive(remote_base::RemoteReceiveData data) override { return CoolixClimate::on_coolix(this, data); }
 | 
			
		||||
 | 
			
		||||
  bool send_swing_cmd_{false};
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
@@ -210,7 +210,10 @@ Cover::Cover() : Cover("") {}
 | 
			
		||||
std::string Cover::get_device_class() {
 | 
			
		||||
  if (this->device_class_override_.has_value())
 | 
			
		||||
    return *this->device_class_override_;
 | 
			
		||||
#pragma GCC diagnostic push
 | 
			
		||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
 | 
			
		||||
  return this->device_class();
 | 
			
		||||
#pragma GCC diagnostic pop
 | 
			
		||||
}
 | 
			
		||||
bool Cover::is_fully_open() const { return this->position == COVER_OPEN; }
 | 
			
		||||
bool Cover::is_fully_closed() const { return this->position == COVER_CLOSED; }
 | 
			
		||||
 
 | 
			
		||||
@@ -169,6 +169,11 @@ class Cover : public EntityBase {
 | 
			
		||||
  friend CoverCall;
 | 
			
		||||
 | 
			
		||||
  virtual void control(const CoverCall &call) = 0;
 | 
			
		||||
 | 
			
		||||
  /** Override this to set the default device class.
 | 
			
		||||
   *
 | 
			
		||||
   * @deprecated This method is deprecated, set the property during config validation instead. (2022.1)
 | 
			
		||||
   */
 | 
			
		||||
  virtual std::string device_class();
 | 
			
		||||
 | 
			
		||||
  optional<CoverRestoreState> restore_state_();
 | 
			
		||||
 
 | 
			
		||||
@@ -38,10 +38,9 @@ void DallasComponent::setup() {
 | 
			
		||||
  raw_sensors = this->one_wire_->search_vec();
 | 
			
		||||
 | 
			
		||||
  for (auto &address : raw_sensors) {
 | 
			
		||||
    std::string s = uint64_to_string(address);
 | 
			
		||||
    auto *address8 = reinterpret_cast<uint8_t *>(&address);
 | 
			
		||||
    if (crc8(address8, 7) != address8[7]) {
 | 
			
		||||
      ESP_LOGW(TAG, "Dallas device 0x%s has invalid CRC.", s.c_str());
 | 
			
		||||
      ESP_LOGW(TAG, "Dallas device 0x%s has invalid CRC.", format_hex(address).c_str());
 | 
			
		||||
      continue;
 | 
			
		||||
    }
 | 
			
		||||
    if (address8[0] != DALLAS_MODEL_DS18S20 && address8[0] != DALLAS_MODEL_DS1822 &&
 | 
			
		||||
@@ -77,8 +76,7 @@ void DallasComponent::dump_config() {
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGD(TAG, "  Found sensors:");
 | 
			
		||||
    for (auto &address : this->found_sensors_) {
 | 
			
		||||
      std::string s = uint64_to_string(address);
 | 
			
		||||
      ESP_LOGD(TAG, "    0x%s", s.c_str());
 | 
			
		||||
      ESP_LOGD(TAG, "    0x%s", format_hex(address).c_str());
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -147,7 +145,7 @@ void DallasTemperatureSensor::set_index(uint8_t index) { this->index_ = index; }
 | 
			
		||||
uint8_t *DallasTemperatureSensor::get_address8() { return reinterpret_cast<uint8_t *>(&this->address_); }
 | 
			
		||||
const std::string &DallasTemperatureSensor::get_address_name() {
 | 
			
		||||
  if (this->address_name_.empty()) {
 | 
			
		||||
    this->address_name_ = std::string("0x") + uint64_to_string(this->address_);
 | 
			
		||||
    this->address_name_ = std::string("0x") + format_hex(this->address_);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return this->address_name_;
 | 
			
		||||
@@ -237,7 +235,7 @@ float DallasTemperatureSensor::get_temp_c() {
 | 
			
		||||
 | 
			
		||||
  return temp / 128.0f;
 | 
			
		||||
}
 | 
			
		||||
std::string DallasTemperatureSensor::unique_id() { return "dallas-" + uint64_to_string(this->address_); }
 | 
			
		||||
std::string DallasTemperatureSensor::unique_id() { return "dallas-" + str_lower_case(format_hex(this->address_)); }
 | 
			
		||||
 | 
			
		||||
}  // namespace dallas
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@ from pathlib import Path
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.components.packages import validate_source_shorthand
 | 
			
		||||
from esphome.wizard import wizard_file
 | 
			
		||||
from esphome.yaml_util import dump
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -48,12 +49,24 @@ def import_config(path: str, name: str, project_name: str, import_url: str) -> N
 | 
			
		||||
    if p.exists():
 | 
			
		||||
        raise FileExistsError
 | 
			
		||||
 | 
			
		||||
    config = {
 | 
			
		||||
        "substitutions": {"name": name},
 | 
			
		||||
        "packages": {project_name: import_url},
 | 
			
		||||
        "esphome": {"name_add_mac_suffix": False},
 | 
			
		||||
    }
 | 
			
		||||
    p.write_text(
 | 
			
		||||
        dump(config) + WIFI_CONFIG,
 | 
			
		||||
        encoding="utf8",
 | 
			
		||||
    )
 | 
			
		||||
    if project_name == "esphome.web":
 | 
			
		||||
        p.write_text(
 | 
			
		||||
            wizard_file(
 | 
			
		||||
                name=name,
 | 
			
		||||
                platform="ESP32" if "esp32" in import_url else "ESP8266",
 | 
			
		||||
                board="esp32dev" if "esp32" in import_url else "esp01_1m",
 | 
			
		||||
                ssid="!secret wifi_ssid",
 | 
			
		||||
                psk="!secret wifi_password",
 | 
			
		||||
            ),
 | 
			
		||||
            encoding="utf8",
 | 
			
		||||
        )
 | 
			
		||||
    else:
 | 
			
		||||
        config = {
 | 
			
		||||
            "substitutions": {"name": name},
 | 
			
		||||
            "packages": {project_name: import_url},
 | 
			
		||||
            "esphome": {"name_add_mac_suffix": False},
 | 
			
		||||
        }
 | 
			
		||||
        p.write_text(
 | 
			
		||||
            dump(config) + WIFI_CONFIG,
 | 
			
		||||
            encoding="utf8",
 | 
			
		||||
        )
 | 
			
		||||
 
 | 
			
		||||
@@ -101,7 +101,7 @@ void DebugComponent::dump_config() {
 | 
			
		||||
    info.features &= ~CHIP_FEATURE_BT;
 | 
			
		||||
  }
 | 
			
		||||
  if (info.features)
 | 
			
		||||
    features += "Other:" + uint64_to_string(info.features);
 | 
			
		||||
    features += "Other:" + format_hex(info.features);
 | 
			
		||||
  ESP_LOGD(TAG, "Chip: Model=%s, Features=%s Cores=%u, Revision=%u", model, features.c_str(), info.cores,
 | 
			
		||||
           info.revision);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -33,7 +33,7 @@ DHT = dht_ns.class_("DHT", cg.PollingComponent)
 | 
			
		||||
CONFIG_SCHEMA = cv.Schema(
 | 
			
		||||
    {
 | 
			
		||||
        cv.GenerateID(): cv.declare_id(DHT),
 | 
			
		||||
        cv.Required(CONF_PIN): pins.gpio_input_pin_schema,
 | 
			
		||||
        cv.Required(CONF_PIN): pins.internal_gpio_input_pin_schema,
 | 
			
		||||
        cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
 | 
			
		||||
            unit_of_measurement=UNIT_CELSIUS,
 | 
			
		||||
            accuracy_decimals=1,
 | 
			
		||||
 
 | 
			
		||||
@@ -5,6 +5,7 @@
 | 
			
		||||
#include "esphome/core/color.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
#include "esphome/core/hal.h"
 | 
			
		||||
#include "esphome/core/helpers.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace display {
 | 
			
		||||
@@ -15,7 +16,8 @@ const Color COLOR_OFF(0, 0, 0, 0);
 | 
			
		||||
const Color COLOR_ON(255, 255, 255, 255);
 | 
			
		||||
 | 
			
		||||
void DisplayBuffer::init_internal_(uint32_t buffer_length) {
 | 
			
		||||
  this->buffer_ = new (std::nothrow) uint8_t[buffer_length];  // NOLINT
 | 
			
		||||
  ExternalRAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
 | 
			
		||||
  this->buffer_ = allocator.allocate(buffer_length);
 | 
			
		||||
  if (this->buffer_ == nullptr) {
 | 
			
		||||
    ESP_LOGE(TAG, "Could not allocate buffer for display!");
 | 
			
		||||
    return;
 | 
			
		||||
 
 | 
			
		||||
@@ -2,8 +2,9 @@ from dataclasses import dataclass
 | 
			
		||||
from typing import Union
 | 
			
		||||
from pathlib import Path
 | 
			
		||||
import logging
 | 
			
		||||
import os
 | 
			
		||||
 | 
			
		||||
from esphome.helpers import write_file_if_changed
 | 
			
		||||
from esphome.helpers import copy_file_if_changed, write_file_if_changed
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_BOARD,
 | 
			
		||||
    CONF_FRAMEWORK,
 | 
			
		||||
@@ -295,6 +296,8 @@ async def to_code(config):
 | 
			
		||||
    conf = config[CONF_FRAMEWORK]
 | 
			
		||||
    cg.add_platformio_option("platform", conf[CONF_PLATFORM_VERSION])
 | 
			
		||||
 | 
			
		||||
    cg.add_platformio_option("extra_scripts", ["post:post_build.py"])
 | 
			
		||||
 | 
			
		||||
    if conf[CONF_TYPE] == FRAMEWORK_ESP_IDF:
 | 
			
		||||
        cg.add_platformio_option("framework", "espidf")
 | 
			
		||||
        cg.add_build_flag("-DUSE_ESP_IDF")
 | 
			
		||||
@@ -412,3 +415,10 @@ def copy_files():
 | 
			
		||||
            CORE.relative_build_path("partitions.csv"),
 | 
			
		||||
            IDF_PARTITIONS_CSV,
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
    dir = os.path.dirname(__file__)
 | 
			
		||||
    post_build_file = os.path.join(dir, "post_build.py.script")
 | 
			
		||||
    copy_file_if_changed(
 | 
			
		||||
        post_build_file,
 | 
			
		||||
        CORE.relative_build_path("post_build.py"),
 | 
			
		||||
    )
 | 
			
		||||
 
 | 
			
		||||
@@ -261,6 +261,37 @@ ESP32_BOARD_PINS = {
 | 
			
		||||
        "SS": 33,
 | 
			
		||||
        "TX": 17,
 | 
			
		||||
    },
 | 
			
		||||
    "featheresp32-s2": {
 | 
			
		||||
        "SDA": 3,
 | 
			
		||||
        "SCL": 4,
 | 
			
		||||
        "SS": 42,
 | 
			
		||||
        "MOSI": 35,
 | 
			
		||||
        "SCK": 36,
 | 
			
		||||
        "MISO": 37,
 | 
			
		||||
        "A0": 18,
 | 
			
		||||
        "A1": 17,
 | 
			
		||||
        "A10": 27,
 | 
			
		||||
        "A11": 12,
 | 
			
		||||
        "A12": 13,
 | 
			
		||||
        "A13": 35,
 | 
			
		||||
        "A2": 16,
 | 
			
		||||
        "A3": 15,
 | 
			
		||||
        "A4": 14,
 | 
			
		||||
        "A5": 8,
 | 
			
		||||
        "LED": 13,
 | 
			
		||||
        "TX": 39,
 | 
			
		||||
        "RX": 38,
 | 
			
		||||
        "T5": 5,
 | 
			
		||||
        "T8": 8,
 | 
			
		||||
        "T9": 9,
 | 
			
		||||
        "T10": 10,
 | 
			
		||||
        "T11": 11,
 | 
			
		||||
        "T12": 12,
 | 
			
		||||
        "T13": 13,
 | 
			
		||||
        "T14": 14,
 | 
			
		||||
        "DAC1": 17,
 | 
			
		||||
        "DAC2": 18,
 | 
			
		||||
    },
 | 
			
		||||
    "firebeetle32": {"LED": 2},
 | 
			
		||||
    "fm-devkit": {
 | 
			
		||||
        "D0": 34,
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										43
									
								
								esphome/components/esp32/post_build.py.script
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								esphome/components/esp32/post_build.py.script
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,43 @@
 | 
			
		||||
# Source https://github.com/letscontrolit/ESPEasy/pull/3845#issuecomment-1005864664
 | 
			
		||||
 | 
			
		||||
import esptool
 | 
			
		||||
 | 
			
		||||
# pylint: disable=E0602
 | 
			
		||||
Import("env")  # noqa
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def esp32_create_combined_bin(source, target, env):
 | 
			
		||||
    print("Generating combined binary for serial flashing")
 | 
			
		||||
    app_offset = 0x10000
 | 
			
		||||
 | 
			
		||||
    new_file_name = env.subst("$BUILD_DIR/${PROGNAME}-factory.bin")
 | 
			
		||||
    sections = env.subst(env.get("FLASH_EXTRA_IMAGES"))
 | 
			
		||||
    firmware_name = env.subst("$BUILD_DIR/${PROGNAME}.bin")
 | 
			
		||||
    chip = env.get("BOARD_MCU")
 | 
			
		||||
    flash_size = env.BoardConfig().get("upload.flash_size")
 | 
			
		||||
    cmd = [
 | 
			
		||||
        "--chip",
 | 
			
		||||
        chip,
 | 
			
		||||
        "merge_bin",
 | 
			
		||||
        "-o",
 | 
			
		||||
        new_file_name,
 | 
			
		||||
        "--flash_size",
 | 
			
		||||
        flash_size,
 | 
			
		||||
    ]
 | 
			
		||||
    print("    Offset | File")
 | 
			
		||||
    for section in sections:
 | 
			
		||||
        sect_adr, sect_file = section.split(" ", 1)
 | 
			
		||||
        print(f" -  {sect_adr} | {sect_file}")
 | 
			
		||||
        cmd += [sect_adr, sect_file]
 | 
			
		||||
 | 
			
		||||
    print(f" - {hex(app_offset)} | {firmware_name}")
 | 
			
		||||
    cmd += [hex(app_offset), firmware_name]
 | 
			
		||||
 | 
			
		||||
    print()
 | 
			
		||||
    print(f"Using esptool.py arguments: {' '.join(cmd)}")
 | 
			
		||||
    print()
 | 
			
		||||
    esptool.main(cmd)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# pylint: disable=E0602
 | 
			
		||||
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", esp32_create_combined_bin)  # noqa
 | 
			
		||||
@@ -19,6 +19,8 @@ from esphome.cpp_helpers import setup_entity
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["esp32"]
 | 
			
		||||
 | 
			
		||||
AUTO_LOAD = ["psram"]
 | 
			
		||||
 | 
			
		||||
esp32_camera_ns = cg.esphome_ns.namespace("esp32_camera")
 | 
			
		||||
ESP32Camera = esp32_camera_ns.class_("ESP32Camera", cg.PollingComponent, cg.EntityBase)
 | 
			
		||||
ESP32CameraFrameSize = esp32_camera_ns.enum("ESP32CameraFrameSize")
 | 
			
		||||
@@ -153,9 +155,7 @@ async def to_code(config):
 | 
			
		||||
    cg.add(var.set_frame_size(config[CONF_RESOLUTION]))
 | 
			
		||||
 | 
			
		||||
    cg.add_define("USE_ESP32_CAMERA")
 | 
			
		||||
    cg.add_build_flag("-DBOARD_HAS_PSRAM")
 | 
			
		||||
 | 
			
		||||
    if CORE.using_esp_idf:
 | 
			
		||||
        cg.add_library("espressif/esp32-camera", "1.0.0")
 | 
			
		||||
        add_idf_sdkconfig_option("CONFIG_RTCIO_SUPPORT_RTC_GPIO_DESC", True)
 | 
			
		||||
        add_idf_sdkconfig_option("CONFIG_ESP32_SPIRAM_SUPPORT", True)
 | 
			
		||||
 
 | 
			
		||||
@@ -49,9 +49,6 @@ void ESP32Camera::dump_config() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "ESP32 Camera:");
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Name: %s", this->name_.c_str());
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Internal: %s", YESNO(this->internal_));
 | 
			
		||||
#ifdef USE_ARDUINO
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Board Has PSRAM: %s", YESNO(psramFound()));
 | 
			
		||||
#endif  // USE_ARDUINO
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Data Pins: D0:%d D1:%d D2:%d D3:%d D4:%d D5:%d D6:%d D7:%d", conf.pin_d0, conf.pin_d1,
 | 
			
		||||
                conf.pin_d2, conf.pin_d3, conf.pin_d4, conf.pin_d5, conf.pin_d6, conf.pin_d7);
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  VSYNC Pin: %d", conf.pin_vsync);
 | 
			
		||||
@@ -136,6 +133,13 @@ void ESP32Camera::loop() {
 | 
			
		||||
    this->current_image_.reset();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // request idle image every idle_update_interval
 | 
			
		||||
  const uint32_t now = millis();
 | 
			
		||||
  if (this->idle_update_interval_ != 0 && now - this->last_idle_request_ > this->idle_update_interval_) {
 | 
			
		||||
    this->last_idle_request_ = now;
 | 
			
		||||
    this->request_image(IDLE);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Check if we should fetch a new image
 | 
			
		||||
  if (!this->has_requested_image_())
 | 
			
		||||
    return;
 | 
			
		||||
@@ -143,7 +147,6 @@ void ESP32Camera::loop() {
 | 
			
		||||
    // image is still in use
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  const uint32_t now = millis();
 | 
			
		||||
  if (now - this->last_update_ <= this->max_update_interval_)
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
@@ -160,12 +163,12 @@ void ESP32Camera::loop() {
 | 
			
		||||
    xQueueSend(this->framebuffer_return_queue_, &fb, portMAX_DELAY);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  this->current_image_ = std::make_shared<CameraImage>(fb);
 | 
			
		||||
  this->current_image_ = std::make_shared<CameraImage>(fb, this->single_requesters_ | this->stream_requesters_);
 | 
			
		||||
 | 
			
		||||
  ESP_LOGD(TAG, "Got Image: len=%u", fb->len);
 | 
			
		||||
  this->new_image_callback_.call(this->current_image_);
 | 
			
		||||
  this->last_update_ = now;
 | 
			
		||||
  this->single_requester_ = false;
 | 
			
		||||
  this->single_requesters_ = 0;
 | 
			
		||||
}
 | 
			
		||||
void ESP32Camera::framebuffer_task(void *pv) {
 | 
			
		||||
  while (true) {
 | 
			
		||||
@@ -261,24 +264,10 @@ void ESP32Camera::set_brightness(int brightness) { this->brightness_ = brightnes
 | 
			
		||||
void ESP32Camera::set_saturation(int saturation) { this->saturation_ = saturation; }
 | 
			
		||||
float ESP32Camera::get_setup_priority() const { return setup_priority::DATA; }
 | 
			
		||||
uint32_t ESP32Camera::hash_base() { return 3010542557UL; }
 | 
			
		||||
void ESP32Camera::request_image() { this->single_requester_ = true; }
 | 
			
		||||
void ESP32Camera::request_stream() { this->last_stream_request_ = millis(); }
 | 
			
		||||
bool ESP32Camera::has_requested_image_() const {
 | 
			
		||||
  if (this->single_requester_)
 | 
			
		||||
    // single request
 | 
			
		||||
    return true;
 | 
			
		||||
 | 
			
		||||
  uint32_t now = millis();
 | 
			
		||||
  if (now - this->last_stream_request_ < 5000)
 | 
			
		||||
    // stream request
 | 
			
		||||
    return true;
 | 
			
		||||
 | 
			
		||||
  if (this->idle_update_interval_ != 0 && now - this->last_update_ > this->idle_update_interval_)
 | 
			
		||||
    // idle update
 | 
			
		||||
    return true;
 | 
			
		||||
 | 
			
		||||
  return false;
 | 
			
		||||
}
 | 
			
		||||
void ESP32Camera::request_image(CameraRequester requester) { this->single_requesters_ |= 1 << requester; }
 | 
			
		||||
void ESP32Camera::start_stream(CameraRequester requester) { this->stream_requesters_ |= 1 << requester; }
 | 
			
		||||
void ESP32Camera::stop_stream(CameraRequester requester) { this->stream_requesters_ &= ~(1 << requester); }
 | 
			
		||||
bool ESP32Camera::has_requested_image_() const { return this->single_requesters_ || this->stream_requesters_; }
 | 
			
		||||
bool ESP32Camera::can_return_image_() const { return this->current_image_.use_count() == 1; }
 | 
			
		||||
void ESP32Camera::set_max_update_interval(uint32_t max_update_interval) {
 | 
			
		||||
  this->max_update_interval_ = max_update_interval;
 | 
			
		||||
@@ -307,7 +296,10 @@ uint8_t *CameraImageReader::peek_data_buffer() { return this->image_->get_data_b
 | 
			
		||||
camera_fb_t *CameraImage::get_raw_buffer() { return this->buffer_; }
 | 
			
		||||
uint8_t *CameraImage::get_data_buffer() { return this->buffer_->buf; }
 | 
			
		||||
size_t CameraImage::get_data_length() { return this->buffer_->len; }
 | 
			
		||||
CameraImage::CameraImage(camera_fb_t *buffer) : buffer_(buffer) {}
 | 
			
		||||
bool CameraImage::was_requested_by(CameraRequester requester) const {
 | 
			
		||||
  return (this->requesters_ & (1 << requester)) != 0;
 | 
			
		||||
}
 | 
			
		||||
CameraImage::CameraImage(camera_fb_t *buffer, uint8_t requesters) : buffer_(buffer), requesters_(requesters) {}
 | 
			
		||||
 | 
			
		||||
}  // namespace esp32_camera
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
@@ -14,15 +14,19 @@ namespace esp32_camera {
 | 
			
		||||
 | 
			
		||||
class ESP32Camera;
 | 
			
		||||
 | 
			
		||||
enum CameraRequester { IDLE, API_REQUESTER, WEB_REQUESTER };
 | 
			
		||||
 | 
			
		||||
class CameraImage {
 | 
			
		||||
 public:
 | 
			
		||||
  CameraImage(camera_fb_t *buffer);
 | 
			
		||||
  CameraImage(camera_fb_t *buffer, uint8_t requester);
 | 
			
		||||
  camera_fb_t *get_raw_buffer();
 | 
			
		||||
  uint8_t *get_data_buffer();
 | 
			
		||||
  size_t get_data_length();
 | 
			
		||||
  bool was_requested_by(CameraRequester requester) const;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  camera_fb_t *buffer_;
 | 
			
		||||
  uint8_t requesters_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class CameraImageReader {
 | 
			
		||||
@@ -81,8 +85,9 @@ class ESP32Camera : public Component, public EntityBase {
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
  void add_image_callback(std::function<void(std::shared_ptr<CameraImage>)> &&f);
 | 
			
		||||
  float get_setup_priority() const override;
 | 
			
		||||
  void request_stream();
 | 
			
		||||
  void request_image();
 | 
			
		||||
  void start_stream(CameraRequester requester);
 | 
			
		||||
  void stop_stream(CameraRequester requester);
 | 
			
		||||
  void request_image(CameraRequester requester);
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  uint32_t hash_base() override;
 | 
			
		||||
@@ -104,13 +109,14 @@ class ESP32Camera : public Component, public EntityBase {
 | 
			
		||||
 | 
			
		||||
  esp_err_t init_error_{ESP_OK};
 | 
			
		||||
  std::shared_ptr<CameraImage> current_image_;
 | 
			
		||||
  uint32_t last_stream_request_{0};
 | 
			
		||||
  bool single_requester_{false};
 | 
			
		||||
  uint8_t single_requesters_{0};
 | 
			
		||||
  uint8_t stream_requesters_{0};
 | 
			
		||||
  QueueHandle_t framebuffer_get_queue_;
 | 
			
		||||
  QueueHandle_t framebuffer_return_queue_;
 | 
			
		||||
  CallbackManager<void(std::shared_ptr<CameraImage>)> new_image_callback_;
 | 
			
		||||
  uint32_t max_update_interval_{1000};
 | 
			
		||||
  uint32_t idle_update_interval_{15000};
 | 
			
		||||
  uint32_t last_idle_request_{0};
 | 
			
		||||
  uint32_t last_update_{0};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,7 @@
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace esp32_camera_web_server {
 | 
			
		||||
 | 
			
		||||
static const int IMAGE_REQUEST_TIMEOUT = 2000;
 | 
			
		||||
static const int IMAGE_REQUEST_TIMEOUT = 5000;
 | 
			
		||||
static const char *const TAG = "esp32_camera_web_server";
 | 
			
		||||
 | 
			
		||||
#define PART_BOUNDARY "123456789000000000000987654321"
 | 
			
		||||
@@ -68,7 +68,7 @@ void CameraWebServer::setup() {
 | 
			
		||||
  httpd_register_uri_handler(this->httpd_, &uri);
 | 
			
		||||
 | 
			
		||||
  esp32_camera::global_esp32_camera->add_image_callback([this](std::shared_ptr<esp32_camera::CameraImage> image) {
 | 
			
		||||
    if (this->running_) {
 | 
			
		||||
    if (this->running_ && image->was_requested_by(esp32_camera::WEB_REQUESTER)) {
 | 
			
		||||
      this->image_ = std::move(image);
 | 
			
		||||
      xSemaphoreGive(this->semaphore_);
 | 
			
		||||
    }
 | 
			
		||||
@@ -169,11 +169,9 @@ esp_err_t CameraWebServer::streaming_handler_(struct httpd_req *req) {
 | 
			
		||||
  uint32_t last_frame = millis();
 | 
			
		||||
  uint32_t frames = 0;
 | 
			
		||||
 | 
			
		||||
  while (res == ESP_OK && this->running_) {
 | 
			
		||||
    if (esp32_camera::global_esp32_camera != nullptr) {
 | 
			
		||||
      esp32_camera::global_esp32_camera->request_stream();
 | 
			
		||||
    }
 | 
			
		||||
  esp32_camera::global_esp32_camera->start_stream(esphome::esp32_camera::WEB_REQUESTER);
 | 
			
		||||
 | 
			
		||||
  while (res == ESP_OK && this->running_) {
 | 
			
		||||
    auto image = this->wait_for_image_();
 | 
			
		||||
 | 
			
		||||
    if (!image) {
 | 
			
		||||
@@ -204,6 +202,8 @@ esp_err_t CameraWebServer::streaming_handler_(struct httpd_req *req) {
 | 
			
		||||
    res = httpd_send_all(req, STREAM_ERROR, strlen(STREAM_ERROR));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  esp32_camera::global_esp32_camera->stop_stream(esphome::esp32_camera::WEB_REQUESTER);
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "STREAM: closed. Frames: %u", frames);
 | 
			
		||||
 | 
			
		||||
  return res;
 | 
			
		||||
@@ -212,9 +212,7 @@ esp_err_t CameraWebServer::streaming_handler_(struct httpd_req *req) {
 | 
			
		||||
esp_err_t CameraWebServer::snapshot_handler_(struct httpd_req *req) {
 | 
			
		||||
  esp_err_t res = ESP_OK;
 | 
			
		||||
 | 
			
		||||
  if (esp32_camera::global_esp32_camera != nullptr) {
 | 
			
		||||
    esp32_camera::global_esp32_camera->request_image();
 | 
			
		||||
  }
 | 
			
		||||
  esp32_camera::global_esp32_camera->request_image(esphome::esp32_camera::WEB_REQUESTER);
 | 
			
		||||
 | 
			
		||||
  auto image = this->wait_for_image_();
 | 
			
		||||
 | 
			
		||||
@@ -233,9 +231,6 @@ esp_err_t CameraWebServer::snapshot_handler_(struct httpd_req *req) {
 | 
			
		||||
 | 
			
		||||
  httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=capture.jpg");
 | 
			
		||||
 | 
			
		||||
  if (res == ESP_OK) {
 | 
			
		||||
    res = httpd_resp_set_hdr(req, CONTENT_LENGTH, esphome::to_string(image->get_data_length()).c_str());
 | 
			
		||||
  }
 | 
			
		||||
  if (res == ESP_OK) {
 | 
			
		||||
    res = httpd_resp_send(req, (const char *) image->get_data_buffer(), image->get_data_length());
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										0
									
								
								esphome/components/esp32_can/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								esphome/components/esp32_can/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										39
									
								
								esphome/components/esp32_can/canbus.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								esphome/components/esp32_can/canbus.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome import pins
 | 
			
		||||
from esphome.components import canbus
 | 
			
		||||
from esphome.const import CONF_ID, CONF_RX_PIN, CONF_TX_PIN
 | 
			
		||||
from esphome.components.canbus import CanbusComponent, CanSpeed, CONF_BIT_RATE
 | 
			
		||||
 | 
			
		||||
CODEOWNERS = ["@Sympatron"]
 | 
			
		||||
DEPENDENCIES = ["esp32"]
 | 
			
		||||
 | 
			
		||||
esp32_can_ns = cg.esphome_ns.namespace("esp32_can")
 | 
			
		||||
esp32_can = esp32_can_ns.class_("ESP32Can", CanbusComponent)
 | 
			
		||||
 | 
			
		||||
# Currently the driver only supports a subset of the bit rates defined in canbus
 | 
			
		||||
CAN_SPEEDS = {
 | 
			
		||||
    "50KBPS": CanSpeed.CAN_50KBPS,
 | 
			
		||||
    "100KBPS": CanSpeed.CAN_100KBPS,
 | 
			
		||||
    "125KBPS": CanSpeed.CAN_125KBPS,
 | 
			
		||||
    "250KBPS": CanSpeed.CAN_250KBPS,
 | 
			
		||||
    "500KBPS": CanSpeed.CAN_500KBPS,
 | 
			
		||||
    "1000KBPS": CanSpeed.CAN_1000KBPS,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = canbus.CANBUS_SCHEMA.extend(
 | 
			
		||||
    {
 | 
			
		||||
        cv.GenerateID(): cv.declare_id(esp32_can),
 | 
			
		||||
        cv.Optional(CONF_BIT_RATE, default="125KBPS"): cv.enum(CAN_SPEEDS, upper=True),
 | 
			
		||||
        cv.Required(CONF_RX_PIN): pins.internal_gpio_input_pin_number,
 | 
			
		||||
        cv.Required(CONF_TX_PIN): pins.internal_gpio_output_pin_number,
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    await canbus.register_canbus(var, config)
 | 
			
		||||
 | 
			
		||||
    cg.add(var.set_rx(config[CONF_RX_PIN]))
 | 
			
		||||
    cg.add(var.set_tx(config[CONF_TX_PIN]))
 | 
			
		||||
							
								
								
									
										123
									
								
								esphome/components/esp32_can/esp32_can.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										123
									
								
								esphome/components/esp32_can/esp32_can.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,123 @@
 | 
			
		||||
#ifdef USE_ESP32
 | 
			
		||||
#include "esp32_can.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
#include <driver/can.h>
 | 
			
		||||
 | 
			
		||||
// WORKAROUND, because CAN_IO_UNUSED is just defined as (-1) in this version
 | 
			
		||||
// of the framework which does not work with -fpermissive
 | 
			
		||||
#undef CAN_IO_UNUSED
 | 
			
		||||
#define CAN_IO_UNUSED ((gpio_num_t) -1)
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace esp32_can {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "esp32_can";
 | 
			
		||||
 | 
			
		||||
static bool get_bitrate(canbus::CanSpeed bitrate, can_timing_config_t *t_config) {
 | 
			
		||||
  switch (bitrate) {
 | 
			
		||||
    case canbus::CAN_50KBPS:
 | 
			
		||||
      *t_config = (can_timing_config_t) CAN_TIMING_CONFIG_50KBITS();
 | 
			
		||||
      return true;
 | 
			
		||||
    case canbus::CAN_100KBPS:
 | 
			
		||||
      *t_config = (can_timing_config_t) CAN_TIMING_CONFIG_100KBITS();
 | 
			
		||||
      return true;
 | 
			
		||||
    case canbus::CAN_125KBPS:
 | 
			
		||||
      *t_config = (can_timing_config_t) CAN_TIMING_CONFIG_125KBITS();
 | 
			
		||||
      return true;
 | 
			
		||||
    case canbus::CAN_250KBPS:
 | 
			
		||||
      *t_config = (can_timing_config_t) CAN_TIMING_CONFIG_250KBITS();
 | 
			
		||||
      return true;
 | 
			
		||||
    case canbus::CAN_500KBPS:
 | 
			
		||||
      *t_config = (can_timing_config_t) CAN_TIMING_CONFIG_500KBITS();
 | 
			
		||||
      return true;
 | 
			
		||||
    case canbus::CAN_1000KBPS:
 | 
			
		||||
      *t_config = (can_timing_config_t) CAN_TIMING_CONFIG_1MBITS();
 | 
			
		||||
      return true;
 | 
			
		||||
    default:
 | 
			
		||||
      return false;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool ESP32Can::setup_internal() {
 | 
			
		||||
  can_general_config_t g_config =
 | 
			
		||||
      CAN_GENERAL_CONFIG_DEFAULT((gpio_num_t) this->tx_, (gpio_num_t) this->rx_, CAN_MODE_NORMAL);
 | 
			
		||||
  can_filter_config_t f_config = CAN_FILTER_CONFIG_ACCEPT_ALL();
 | 
			
		||||
  can_timing_config_t t_config;
 | 
			
		||||
 | 
			
		||||
  if (!get_bitrate(this->bit_rate_, &t_config)) {
 | 
			
		||||
    // invalid bit rate
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Install CAN driver
 | 
			
		||||
  if (can_driver_install(&g_config, &t_config, &f_config) != ESP_OK) {
 | 
			
		||||
    // Failed to install driver
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Start CAN driver
 | 
			
		||||
  if (can_start() != ESP_OK) {
 | 
			
		||||
    // Failed to start driver
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
canbus::Error ESP32Can::send_message(struct canbus::CanFrame *frame) {
 | 
			
		||||
  if (frame->can_data_length_code > canbus::CAN_MAX_DATA_LENGTH) {
 | 
			
		||||
    return canbus::ERROR_FAILTX;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  uint32_t flags = CAN_MSG_FLAG_NONE;
 | 
			
		||||
  if (frame->use_extended_id) {
 | 
			
		||||
    flags |= CAN_MSG_FLAG_EXTD;
 | 
			
		||||
  }
 | 
			
		||||
  if (frame->remote_transmission_request) {
 | 
			
		||||
    flags |= CAN_MSG_FLAG_RTR;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  can_message_t message = {
 | 
			
		||||
      .flags = flags,
 | 
			
		||||
      .identifier = frame->can_id,
 | 
			
		||||
      .data_length_code = frame->can_data_length_code,
 | 
			
		||||
  };
 | 
			
		||||
  if (!frame->remote_transmission_request) {
 | 
			
		||||
    memcpy(message.data, frame->data, frame->can_data_length_code);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (can_transmit(&message, pdMS_TO_TICKS(1000)) == ESP_OK) {
 | 
			
		||||
    return canbus::ERROR_OK;
 | 
			
		||||
  } else {
 | 
			
		||||
    return canbus::ERROR_ALLTXBUSY;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
canbus::Error ESP32Can::read_message(struct canbus::CanFrame *frame) {
 | 
			
		||||
  can_message_t message;
 | 
			
		||||
 | 
			
		||||
  if (can_receive(&message, 0) != ESP_OK) {
 | 
			
		||||
    return canbus::ERROR_NOMSG;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  frame->can_id = message.identifier;
 | 
			
		||||
  frame->use_extended_id = message.flags & CAN_MSG_FLAG_EXTD;
 | 
			
		||||
  frame->remote_transmission_request = message.flags & CAN_MSG_FLAG_RTR;
 | 
			
		||||
  frame->can_data_length_code = message.data_length_code;
 | 
			
		||||
 | 
			
		||||
  if (!frame->remote_transmission_request) {
 | 
			
		||||
    size_t dlc =
 | 
			
		||||
        message.data_length_code < canbus::CAN_MAX_DATA_LENGTH ? message.data_length_code : canbus::CAN_MAX_DATA_LENGTH;
 | 
			
		||||
    memcpy(frame->data, message.data, dlc);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return canbus::ERROR_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace esp32_can
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										29
									
								
								esphome/components/esp32_can/esp32_can.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								esphome/components/esp32_can/esp32_can.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/canbus/canbus.h"
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace esp32_can {
 | 
			
		||||
 | 
			
		||||
class ESP32Can : public canbus::Canbus {
 | 
			
		||||
 public:
 | 
			
		||||
  void set_rx(int rx) { rx_ = rx; }
 | 
			
		||||
  void set_tx(int tx) { tx_ = tx; }
 | 
			
		||||
  ESP32Can(){};
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  bool setup_internal() override;
 | 
			
		||||
  canbus::Error send_message(struct canbus::CanFrame *frame) override;
 | 
			
		||||
  canbus::Error read_message(struct canbus::CanFrame *frame) override;
 | 
			
		||||
 | 
			
		||||
  int rx_{-1};
 | 
			
		||||
  int tx_{-1};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace esp32_can
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
@@ -1,4 +1,5 @@
 | 
			
		||||
import logging
 | 
			
		||||
import os
 | 
			
		||||
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_BOARD,
 | 
			
		||||
@@ -14,6 +15,7 @@ from esphome.const import (
 | 
			
		||||
from esphome.core import CORE, coroutine_with_priority
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
from esphome.helpers import copy_file_if_changed
 | 
			
		||||
 | 
			
		||||
from .const import CONF_RESTORE_FROM_FLASH, KEY_BOARD, KEY_ESP8266, esp8266_ns
 | 
			
		||||
from .boards import ESP8266_FLASH_SIZES, ESP8266_LD_SCRIPTS
 | 
			
		||||
@@ -158,6 +160,8 @@ async def to_code(config):
 | 
			
		||||
    cg.add_define("ESPHOME_BOARD", config[CONF_BOARD])
 | 
			
		||||
    cg.add_define("ESPHOME_VARIANT", "ESP8266")
 | 
			
		||||
 | 
			
		||||
    cg.add_platformio_option("extra_scripts", ["post:post_build.py"])
 | 
			
		||||
 | 
			
		||||
    conf = config[CONF_FRAMEWORK]
 | 
			
		||||
    cg.add_platformio_option("framework", "arduino")
 | 
			
		||||
    cg.add_build_flag("-DUSE_ARDUINO")
 | 
			
		||||
@@ -210,3 +214,14 @@ async def to_code(config):
 | 
			
		||||
 | 
			
		||||
        if ld_script is not None:
 | 
			
		||||
            cg.add_platformio_option("board_build.ldscript", ld_script)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# Called by writer.py
 | 
			
		||||
def copy_files():
 | 
			
		||||
 | 
			
		||||
    dir = os.path.dirname(__file__)
 | 
			
		||||
    post_build_file = os.path.join(dir, "post_build.py.script")
 | 
			
		||||
    copy_file_if_changed(
 | 
			
		||||
        post_build_file,
 | 
			
		||||
        CORE.relative_build_path("post_build.py"),
 | 
			
		||||
    )
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										15
									
								
								esphome/components/esp8266/post_build.py.script
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								esphome/components/esp8266/post_build.py.script
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
			
		||||
import shutil
 | 
			
		||||
 | 
			
		||||
# pylint: disable=E0602
 | 
			
		||||
Import("env")  # noqa
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def esp8266_copy_factory_bin(source, target, env):
 | 
			
		||||
    firmware_name = env.subst("$BUILD_DIR/${PROGNAME}.bin")
 | 
			
		||||
    new_file_name = env.subst("$BUILD_DIR/${PROGNAME}-factory.bin")
 | 
			
		||||
 | 
			
		||||
    shutil.copyfile(firmware_name, new_file_name)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# pylint: disable=E0602
 | 
			
		||||
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", esp8266_copy_factory_bin)  # noqa
 | 
			
		||||
@@ -21,7 +21,9 @@ void HDC1080Component::setup() {
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  if (!this->write_bytes(HDC1080_CMD_CONFIGURATION, data, 2)) {
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    // as instruction is same as powerup defaults (for now), interpret as warning if this fails
 | 
			
		||||
    ESP_LOGW(TAG, "HDC1080 initial config instruction error");
 | 
			
		||||
    this->status_set_warning();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -109,8 +109,8 @@ def to_code(config):
 | 
			
		||||
    cg.add(var.set_protocol(config[CONF_PROTOCOL]))
 | 
			
		||||
    cg.add(var.set_horizontal_default(config[CONF_HORIZONTAL_DEFAULT]))
 | 
			
		||||
    cg.add(var.set_vertical_default(config[CONF_VERTICAL_DEFAULT]))
 | 
			
		||||
    cg.add(var.set_max_temperature(config[CONF_MIN_TEMPERATURE]))
 | 
			
		||||
    cg.add(var.set_min_temperature(config[CONF_MAX_TEMPERATURE]))
 | 
			
		||||
    cg.add(var.set_max_temperature(config[CONF_MAX_TEMPERATURE]))
 | 
			
		||||
    cg.add(var.set_min_temperature(config[CONF_MIN_TEMPERATURE]))
 | 
			
		||||
 | 
			
		||||
    # PIO isn't updating releases, so referencing the release tag directly. See:
 | 
			
		||||
    # https://github.com/ToniA/arduino-heatpumpir/commit/0948c619d86407a4e50e8db2f3c193e0576c86fd
 | 
			
		||||
 
 | 
			
		||||
@@ -172,7 +172,7 @@ async def http_request_action_to_code(config, action_id, template_arg, args):
 | 
			
		||||
    if CONF_JSON in config:
 | 
			
		||||
        json_ = config[CONF_JSON]
 | 
			
		||||
        if isinstance(json_, Lambda):
 | 
			
		||||
            args_ = args + [(cg.JsonObjectRef, "root")]
 | 
			
		||||
            args_ = args + [(cg.JsonObject, "root")]
 | 
			
		||||
            lambda_ = await cg.process_lambda(json_, args_, return_type=cg.void)
 | 
			
		||||
            cg.add(var.set_json(lambda_))
 | 
			
		||||
        else:
 | 
			
		||||
 
 | 
			
		||||
@@ -115,8 +115,11 @@ void HttpRequestComponent::close() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const char *HttpRequestComponent::get_string() {
 | 
			
		||||
  static const String STR = this->client_.getString();
 | 
			
		||||
  return STR.c_str();
 | 
			
		||||
  // The static variable is here because HTTPClient::getString() returns a String on ESP32, and we need something to
 | 
			
		||||
  // to keep a buffer alive.
 | 
			
		||||
  static std::string str;
 | 
			
		||||
  str = this->client_.getString().c_str();
 | 
			
		||||
  return str.c_str();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace http_request
 | 
			
		||||
 
 | 
			
		||||
@@ -78,7 +78,7 @@ template<typename... Ts> class HttpRequestSendAction : public Action<Ts...> {
 | 
			
		||||
 | 
			
		||||
  void add_json(const char *key, TemplatableValue<std::string, Ts...> value) { this->json_.insert({key, value}); }
 | 
			
		||||
 | 
			
		||||
  void set_json(std::function<void(Ts..., JsonObject &)> json_func) { this->json_func_ = json_func; }
 | 
			
		||||
  void set_json(std::function<void(Ts..., JsonObject)> json_func) { this->json_func_ = json_func; }
 | 
			
		||||
 | 
			
		||||
  void register_response_trigger(HttpRequestResponseTrigger *trigger) { this->response_triggers_.push_back(trigger); }
 | 
			
		||||
 | 
			
		||||
@@ -118,17 +118,17 @@ template<typename... Ts> class HttpRequestSendAction : public Action<Ts...> {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void encode_json_(Ts... x, JsonObject &root) {
 | 
			
		||||
  void encode_json_(Ts... x, JsonObject root) {
 | 
			
		||||
    for (const auto &item : this->json_) {
 | 
			
		||||
      auto val = item.second;
 | 
			
		||||
      root[item.first] = val.value(x...);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  void encode_json_func_(Ts... x, JsonObject &root) { this->json_func_(x..., root); }
 | 
			
		||||
  void encode_json_func_(Ts... x, JsonObject root) { this->json_func_(x..., root); }
 | 
			
		||||
  HttpRequestComponent *parent_;
 | 
			
		||||
  std::map<const char *, TemplatableValue<const char *, Ts...>> headers_{};
 | 
			
		||||
  std::map<const char *, TemplatableValue<std::string, Ts...>> json_{};
 | 
			
		||||
  std::function<void(Ts..., JsonObject &)> json_func_{nullptr};
 | 
			
		||||
  std::function<void(Ts..., JsonObject)> json_func_{nullptr};
 | 
			
		||||
  std::vector<HttpRequestResponseTrigger *> response_triggers_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -25,7 +25,7 @@ void ArduinoI2CBus::setup() {
 | 
			
		||||
  wire_ = &Wire;  // NOLINT(cppcoreguidelines-prefer-member-initializer)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  wire_->begin(sda_pin_, scl_pin_);
 | 
			
		||||
  wire_->begin(static_cast<int>(sda_pin_), static_cast<int>(scl_pin_));
 | 
			
		||||
  wire_->setClock(frequency_);
 | 
			
		||||
  initialized_ = true;
 | 
			
		||||
  if (this->scan_) {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										0
									
								
								esphome/components/ina260/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								esphome/components/ina260/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										128
									
								
								esphome/components/ina260/ina260.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										128
									
								
								esphome/components/ina260/ina260.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,128 @@
 | 
			
		||||
#include "ina260.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
#include "esphome/core/hal.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ina260 {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "ina260";
 | 
			
		||||
 | 
			
		||||
// | A0   | A1   | Address |
 | 
			
		||||
// | GND  | GND  | 0x40    |
 | 
			
		||||
// | GND  | V_S+ | 0x41    |
 | 
			
		||||
// | GND  | SDA  | 0x42    |
 | 
			
		||||
// | GND  | SCL  | 0x43    |
 | 
			
		||||
// | V_S+ | GND  | 0x44    |
 | 
			
		||||
// | V_S+ | V_S+ | 0x45    |
 | 
			
		||||
// | V_S+ | SDA  | 0x46    |
 | 
			
		||||
// | V_S+ | SCL  | 0x47    |
 | 
			
		||||
// | SDA  | GND  | 0x48    |
 | 
			
		||||
// | SDA  | V_S+ | 0x49    |
 | 
			
		||||
// | SDA  | SDA  | 0x4A    |
 | 
			
		||||
// | SDA  | SCL  | 0x4B    |
 | 
			
		||||
// | SCL  | GND  | 0x4C    |
 | 
			
		||||
// | SCL  | V_S+ | 0x4D    |
 | 
			
		||||
// | SCL  | SDA  | 0x4E    |
 | 
			
		||||
// | SCL  | SCL  | 0x4F    |
 | 
			
		||||
 | 
			
		||||
static const uint8_t INA260_REGISTER_CONFIG = 0x00;
 | 
			
		||||
static const uint8_t INA260_REGISTER_CURRENT = 0x01;
 | 
			
		||||
static const uint8_t INA260_REGISTER_BUS_VOLTAGE = 0x02;
 | 
			
		||||
static const uint8_t INA260_REGISTER_POWER = 0x03;
 | 
			
		||||
static const uint8_t INA260_REGISTER_MASK_ENABLE = 0x06;
 | 
			
		||||
static const uint8_t INA260_REGISTER_ALERT_LIMIT = 0x07;
 | 
			
		||||
static const uint8_t INA260_REGISTER_MANUFACTURE_ID = 0xFE;
 | 
			
		||||
static const uint8_t INA260_REGISTER_DEVICE_ID = 0xFF;
 | 
			
		||||
 | 
			
		||||
void INA260Component::setup() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Setting up INA260...");
 | 
			
		||||
 | 
			
		||||
  // Reset device on setup
 | 
			
		||||
  if (!this->write_byte_16(INA260_REGISTER_CONFIG, 0x8000)) {
 | 
			
		||||
    this->error_code_ = DEVICE_RESET_FAILED;
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  delay(2);
 | 
			
		||||
 | 
			
		||||
  this->read_byte_16(INA260_REGISTER_MANUFACTURE_ID, &this->manufacture_id_);
 | 
			
		||||
  this->read_byte_16(INA260_REGISTER_DEVICE_ID, &this->device_id_);
 | 
			
		||||
 | 
			
		||||
  if (this->manufacture_id_ != (uint16_t) 0x5449 || this->device_id_ != (uint16_t) 0x2270) {
 | 
			
		||||
    this->error_code_ = COMMUNICATION_FAILED;
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (!this->write_byte_16(INA260_REGISTER_CONFIG, (uint16_t) 0b0000001100000111)) {
 | 
			
		||||
    this->error_code_ = FAILED_TO_UPDATE_CONFIGURATION;
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void INA260Component::dump_config() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "INA260:");
 | 
			
		||||
  LOG_I2C_DEVICE(this);
 | 
			
		||||
  LOG_UPDATE_INTERVAL(this);
 | 
			
		||||
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Manufacture ID: 0x%x", this->manufacture_id_);
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Device ID: 0x%x", this->device_id_);
 | 
			
		||||
 | 
			
		||||
  LOG_SENSOR("  ", "Bus Voltage", this->bus_voltage_sensor_);
 | 
			
		||||
  LOG_SENSOR("  ", "Current", this->current_sensor_);
 | 
			
		||||
  LOG_SENSOR("  ", "Power", this->power_sensor_);
 | 
			
		||||
 | 
			
		||||
  switch (this->error_code_) {
 | 
			
		||||
    case COMMUNICATION_FAILED:
 | 
			
		||||
      ESP_LOGE(TAG, "Connected device does not match a known INA260 sensor");
 | 
			
		||||
      break;
 | 
			
		||||
    case DEVICE_RESET_FAILED:
 | 
			
		||||
      ESP_LOGE(TAG, "Device reset failed - Is the device connected?");
 | 
			
		||||
      break;
 | 
			
		||||
    case FAILED_TO_UPDATE_CONFIGURATION:
 | 
			
		||||
      ESP_LOGE(TAG, "Failed to update device configuration");
 | 
			
		||||
      break;
 | 
			
		||||
    case NONE:
 | 
			
		||||
    default:
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void INA260Component::update() {
 | 
			
		||||
  if (this->bus_voltage_sensor_ != nullptr) {
 | 
			
		||||
    uint16_t raw_bus_voltage;
 | 
			
		||||
    if (!this->read_byte_16(INA260_REGISTER_BUS_VOLTAGE, &raw_bus_voltage)) {
 | 
			
		||||
      this->status_set_warning();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    float bus_voltage_v = int16_t(raw_bus_voltage) * 0.00125f;
 | 
			
		||||
    this->bus_voltage_sensor_->publish_state(bus_voltage_v);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (this->current_sensor_ != nullptr) {
 | 
			
		||||
    uint16_t raw_current;
 | 
			
		||||
    if (!this->read_byte_16(INA260_REGISTER_CURRENT, &raw_current)) {
 | 
			
		||||
      this->status_set_warning();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    float current_a = int16_t(raw_current) * 0.00125f;
 | 
			
		||||
    this->current_sensor_->publish_state(current_a);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (this->power_sensor_ != nullptr) {
 | 
			
		||||
    uint16_t raw_power;
 | 
			
		||||
    if (!this->read_byte_16(INA260_REGISTER_POWER, &raw_power)) {
 | 
			
		||||
      this->status_set_warning();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    float power_w = ((int16_t(raw_power) * 10.0f) / 1000.0f);
 | 
			
		||||
    this->power_sensor_->publish_state(power_w);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->status_clear_warning();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace ina260
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										39
									
								
								esphome/components/ina260/ina260.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								esphome/components/ina260/ina260.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/components/sensor/sensor.h"
 | 
			
		||||
#include "esphome/components/i2c/i2c.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ina260 {
 | 
			
		||||
 | 
			
		||||
class INA260Component : public PollingComponent, public i2c::I2CDevice {
 | 
			
		||||
 public:
 | 
			
		||||
  void setup() override;
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
  void update() override;
 | 
			
		||||
 | 
			
		||||
  float get_setup_priority() const override { return setup_priority::DATA; }
 | 
			
		||||
 | 
			
		||||
  void set_bus_voltage_sensor(sensor::Sensor *bus_voltage_sensor) { this->bus_voltage_sensor_ = bus_voltage_sensor; }
 | 
			
		||||
  void set_current_sensor(sensor::Sensor *current_sensor) { this->current_sensor_ = current_sensor; }
 | 
			
		||||
  void set_power_sensor(sensor::Sensor *power_sensor) { this->power_sensor_ = power_sensor; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  uint16_t manufacture_id_{0};
 | 
			
		||||
  uint16_t device_id_{0};
 | 
			
		||||
 | 
			
		||||
  sensor::Sensor *bus_voltage_sensor_{nullptr};
 | 
			
		||||
  sensor::Sensor *current_sensor_{nullptr};
 | 
			
		||||
  sensor::Sensor *power_sensor_{nullptr};
 | 
			
		||||
 | 
			
		||||
  enum ErrorCode {
 | 
			
		||||
    NONE,
 | 
			
		||||
    COMMUNICATION_FAILED,
 | 
			
		||||
    DEVICE_RESET_FAILED,
 | 
			
		||||
    FAILED_TO_UPDATE_CONFIGURATION,
 | 
			
		||||
  } error_code_{NONE};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace ina260
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										71
									
								
								esphome/components/ina260/sensor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								esphome/components/ina260/sensor.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,71 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.components import i2c, sensor
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    CONF_BUS_VOLTAGE,
 | 
			
		||||
    CONF_CURRENT,
 | 
			
		||||
    CONF_POWER,
 | 
			
		||||
    DEVICE_CLASS_VOLTAGE,
 | 
			
		||||
    DEVICE_CLASS_CURRENT,
 | 
			
		||||
    DEVICE_CLASS_POWER,
 | 
			
		||||
    STATE_CLASS_MEASUREMENT,
 | 
			
		||||
    UNIT_VOLT,
 | 
			
		||||
    UNIT_AMPERE,
 | 
			
		||||
    UNIT_WATT,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["i2c"]
 | 
			
		||||
CODEOWNERS = ["@MrEditor97"]
 | 
			
		||||
 | 
			
		||||
ina260_ns = cg.esphome_ns.namespace("ina260")
 | 
			
		||||
INA260Component = ina260_ns.class_(
 | 
			
		||||
    "INA260Component", cg.PollingComponent, i2c.I2CDevice
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = cv.All(
 | 
			
		||||
    cv.Schema(
 | 
			
		||||
        {
 | 
			
		||||
            cv.GenerateID(): cv.declare_id(INA260Component),
 | 
			
		||||
            cv.Optional(CONF_BUS_VOLTAGE): sensor.sensor_schema(
 | 
			
		||||
                unit_of_measurement=UNIT_VOLT,
 | 
			
		||||
                accuracy_decimals=2,
 | 
			
		||||
                device_class=DEVICE_CLASS_VOLTAGE,
 | 
			
		||||
                state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_CURRENT): sensor.sensor_schema(
 | 
			
		||||
                unit_of_measurement=UNIT_AMPERE,
 | 
			
		||||
                accuracy_decimals=3,
 | 
			
		||||
                device_class=DEVICE_CLASS_CURRENT,
 | 
			
		||||
                state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_POWER): sensor.sensor_schema(
 | 
			
		||||
                unit_of_measurement=UNIT_WATT,
 | 
			
		||||
                accuracy_decimals=2,
 | 
			
		||||
                device_class=DEVICE_CLASS_POWER,
 | 
			
		||||
                state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
            ),
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
    .extend(cv.polling_component_schema("60s"))
 | 
			
		||||
    .extend(i2c.i2c_device_schema(0x40))
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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 CONF_BUS_VOLTAGE in config:
 | 
			
		||||
        sens = await sensor.new_sensor(config[CONF_BUS_VOLTAGE])
 | 
			
		||||
        cg.add(var.set_bus_voltage_sensor(sens))
 | 
			
		||||
 | 
			
		||||
    if CONF_CURRENT in config:
 | 
			
		||||
        sens = await sensor.new_sensor(config[CONF_CURRENT])
 | 
			
		||||
        cg.add(var.set_current_sensor(sens))
 | 
			
		||||
 | 
			
		||||
    if CONF_POWER in config:
 | 
			
		||||
        sens = await sensor.new_sensor(config[CONF_POWER])
 | 
			
		||||
        cg.add(var.set_power_sensor(sens))
 | 
			
		||||
@@ -6,11 +6,13 @@ from esphome.const import (
 | 
			
		||||
    CONF_FULL_UPDATE_EVERY,
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    CONF_LAMBDA,
 | 
			
		||||
    CONF_MODEL,
 | 
			
		||||
    CONF_PAGES,
 | 
			
		||||
    CONF_WAKEUP_PIN,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["i2c", "esp32"]
 | 
			
		||||
AUTO_LOAD = ["psram"]
 | 
			
		||||
 | 
			
		||||
CONF_DISPLAY_DATA_0_PIN = "display_data_0_pin"
 | 
			
		||||
CONF_DISPLAY_DATA_1_PIN = "display_data_1_pin"
 | 
			
		||||
@@ -40,6 +42,13 @@ Inkplate6 = inkplate6_ns.class_(
 | 
			
		||||
    "Inkplate6", cg.PollingComponent, i2c.I2CDevice, display.DisplayBuffer
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
InkplateModel = inkplate6_ns.enum("InkplateModel")
 | 
			
		||||
 | 
			
		||||
MODELS = {
 | 
			
		||||
    "inkplate_6": InkplateModel.INKPLATE_6,
 | 
			
		||||
    "inkplate_10": InkplateModel.INKPLATE_10,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = cv.All(
 | 
			
		||||
    display.FULL_DISPLAY_SCHEMA.extend(
 | 
			
		||||
        {
 | 
			
		||||
@@ -47,6 +56,9 @@ CONFIG_SCHEMA = cv.All(
 | 
			
		||||
            cv.Optional(CONF_GREYSCALE, default=False): cv.boolean,
 | 
			
		||||
            cv.Optional(CONF_PARTIAL_UPDATING, default=True): cv.boolean,
 | 
			
		||||
            cv.Optional(CONF_FULL_UPDATE_EVERY, default=10): cv.uint32_t,
 | 
			
		||||
            cv.Optional(CONF_MODEL, default="inkplate_6"): cv.enum(
 | 
			
		||||
                MODELS, lower=True, space="_"
 | 
			
		||||
            ),
 | 
			
		||||
            # Control pins
 | 
			
		||||
            cv.Required(CONF_CKV_PIN): pins.gpio_output_pin_schema,
 | 
			
		||||
            cv.Required(CONF_GMOD_PIN): pins.gpio_output_pin_schema,
 | 
			
		||||
@@ -110,6 +122,8 @@ async def to_code(config):
 | 
			
		||||
    cg.add(var.set_partial_updating(config[CONF_PARTIAL_UPDATING]))
 | 
			
		||||
    cg.add(var.set_full_update_every(config[CONF_FULL_UPDATE_EVERY]))
 | 
			
		||||
 | 
			
		||||
    cg.add(var.set_model(config[CONF_MODEL]))
 | 
			
		||||
 | 
			
		||||
    ckv = await cg.gpio_pin_expression(config[CONF_CKV_PIN])
 | 
			
		||||
    cg.add(var.set_ckv_pin(ckv))
 | 
			
		||||
 | 
			
		||||
@@ -166,5 +180,3 @@ async def to_code(config):
 | 
			
		||||
 | 
			
		||||
    display_data_7 = await cg.gpio_pin_expression(config[CONF_DISPLAY_DATA_7_PIN])
 | 
			
		||||
    cg.add(var.set_display_data_7_pin(display_data_7))
 | 
			
		||||
 | 
			
		||||
    cg.add_build_flag("-DBOARD_HAS_PSRAM")
 | 
			
		||||
 
 | 
			
		||||
@@ -42,32 +42,32 @@ void Inkplate6::setup() {
 | 
			
		||||
  this->display();
 | 
			
		||||
}
 | 
			
		||||
void Inkplate6::initialize_() {
 | 
			
		||||
  ExternalRAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
 | 
			
		||||
  uint32_t buffer_size = this->get_buffer_length_();
 | 
			
		||||
  if (buffer_size == 0)
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  if (this->partial_buffer_ != nullptr) {
 | 
			
		||||
    free(this->partial_buffer_);  // NOLINT
 | 
			
		||||
  }
 | 
			
		||||
  if (this->partial_buffer_2_ != nullptr) {
 | 
			
		||||
    free(this->partial_buffer_2_);  // NOLINT
 | 
			
		||||
  }
 | 
			
		||||
  if (this->buffer_ != nullptr) {
 | 
			
		||||
    free(this->buffer_);  // NOLINT
 | 
			
		||||
  }
 | 
			
		||||
  if (this->partial_buffer_ != nullptr)
 | 
			
		||||
    allocator.deallocate(this->partial_buffer_, buffer_size);
 | 
			
		||||
  if (this->partial_buffer_2_ != nullptr)
 | 
			
		||||
    allocator.deallocate(this->partial_buffer_2_, buffer_size * 2);
 | 
			
		||||
  if (this->buffer_ != nullptr)
 | 
			
		||||
    allocator.deallocate(this->buffer_, buffer_size);
 | 
			
		||||
 | 
			
		||||
  this->buffer_ = (uint8_t *) ps_malloc(buffer_size);
 | 
			
		||||
  this->buffer_ = allocator.allocate(buffer_size);
 | 
			
		||||
  if (this->buffer_ == nullptr) {
 | 
			
		||||
    ESP_LOGE(TAG, "Could not allocate buffer for display!");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (!this->greyscale_) {
 | 
			
		||||
    this->partial_buffer_ = (uint8_t *) ps_malloc(buffer_size);
 | 
			
		||||
    this->partial_buffer_ = allocator.allocate(buffer_size);
 | 
			
		||||
    if (this->partial_buffer_ == nullptr) {
 | 
			
		||||
      ESP_LOGE(TAG, "Could not allocate partial buffer for display!");
 | 
			
		||||
      this->mark_failed();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    this->partial_buffer_2_ = (uint8_t *) ps_malloc(buffer_size * 2);
 | 
			
		||||
    this->partial_buffer_2_ = allocator.allocate(buffer_size * 2);
 | 
			
		||||
    if (this->partial_buffer_2_ == nullptr) {
 | 
			
		||||
      ESP_LOGE(TAG, "Could not allocate partial buffer 2 for display!");
 | 
			
		||||
      this->mark_failed();
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,11 @@
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace inkplate6 {
 | 
			
		||||
 | 
			
		||||
enum InkplateModel : uint8_t {
 | 
			
		||||
  INKPLATE_6 = 0,
 | 
			
		||||
  INKPLATE_10 = 1,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class Inkplate6 : public PollingComponent, public display::DisplayBuffer, public i2c::I2CDevice {
 | 
			
		||||
 public:
 | 
			
		||||
  const uint8_t LUT2[16] = {0b10101010, 0b10101001, 0b10100110, 0b10100101, 0b10011010, 0b10011001,
 | 
			
		||||
@@ -43,6 +48,8 @@ class Inkplate6 : public PollingComponent, public display::DisplayBuffer, public
 | 
			
		||||
  void set_partial_updating(bool partial_updating) { this->partial_updating_ = partial_updating; }
 | 
			
		||||
  void set_full_update_every(uint32_t full_update_every) { this->full_update_every_ = full_update_every; }
 | 
			
		||||
 | 
			
		||||
  void set_model(InkplateModel model) { this->model_ = model; }
 | 
			
		||||
 | 
			
		||||
  void set_display_data_0_pin(InternalGPIOPin *data) { this->display_data_0_pin_ = data; }
 | 
			
		||||
  void set_display_data_1_pin(InternalGPIOPin *data) { this->display_data_1_pin_ = data; }
 | 
			
		||||
  void set_display_data_2_pin(InternalGPIOPin *data) { this->display_data_2_pin_ = data; }
 | 
			
		||||
@@ -101,9 +108,21 @@ class Inkplate6 : public PollingComponent, public display::DisplayBuffer, public
 | 
			
		||||
  void pins_z_state_();
 | 
			
		||||
  void pins_as_outputs_();
 | 
			
		||||
 | 
			
		||||
  int get_width_internal() override { return 800; }
 | 
			
		||||
  int get_width_internal() override {
 | 
			
		||||
    if (this->model_ == INKPLATE_6)
 | 
			
		||||
      return 800;
 | 
			
		||||
    else if (this->model_ == INKPLATE_10)
 | 
			
		||||
      return 1200;
 | 
			
		||||
    return 0;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  int get_height_internal() override { return 600; }
 | 
			
		||||
  int get_height_internal() override {
 | 
			
		||||
    if (this->model_ == INKPLATE_6)
 | 
			
		||||
      return 600;
 | 
			
		||||
    else if (this->model_ == INKPLATE_10)
 | 
			
		||||
      return 825;
 | 
			
		||||
    return 0;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  size_t get_buffer_length_();
 | 
			
		||||
 | 
			
		||||
@@ -133,6 +152,8 @@ class Inkplate6 : public PollingComponent, public display::DisplayBuffer, public
 | 
			
		||||
  bool greyscale_;
 | 
			
		||||
  bool partial_updating_;
 | 
			
		||||
 | 
			
		||||
  InkplateModel model_;
 | 
			
		||||
 | 
			
		||||
  InternalGPIOPin *display_data_0_pin_;
 | 
			
		||||
  InternalGPIOPin *display_data_1_pin_;
 | 
			
		||||
  InternalGPIOPin *display_data_2_pin_;
 | 
			
		||||
 
 | 
			
		||||
@@ -23,31 +23,6 @@ void IntegrationSensor::setup() {
 | 
			
		||||
  this->sensor_->add_on_state_callback([this](float state) { this->process_sensor_value_(state); });
 | 
			
		||||
}
 | 
			
		||||
void IntegrationSensor::dump_config() { LOG_SENSOR("", "Integration Sensor", this); }
 | 
			
		||||
std::string IntegrationSensor::unit_of_measurement() {
 | 
			
		||||
  std::string suffix;
 | 
			
		||||
  switch (this->time_) {
 | 
			
		||||
    case INTEGRATION_SENSOR_TIME_MILLISECOND:
 | 
			
		||||
      suffix = "ms";
 | 
			
		||||
      break;
 | 
			
		||||
    case INTEGRATION_SENSOR_TIME_SECOND:
 | 
			
		||||
      suffix = "s";
 | 
			
		||||
      break;
 | 
			
		||||
    case INTEGRATION_SENSOR_TIME_MINUTE:
 | 
			
		||||
      suffix = "min";
 | 
			
		||||
      break;
 | 
			
		||||
    case INTEGRATION_SENSOR_TIME_HOUR:
 | 
			
		||||
      suffix = "h";
 | 
			
		||||
      break;
 | 
			
		||||
    case INTEGRATION_SENSOR_TIME_DAY:
 | 
			
		||||
      suffix = "d";
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
  std::string base = this->sensor_->get_unit_of_measurement();
 | 
			
		||||
  if (str_endswith(base, "/" + suffix)) {
 | 
			
		||||
    return base.substr(0, base.size() - suffix.size() - 1);
 | 
			
		||||
  }
 | 
			
		||||
  return base + suffix;
 | 
			
		||||
}
 | 
			
		||||
void IntegrationSensor::process_sensor_value_(float value) {
 | 
			
		||||
  const uint32_t now = millis();
 | 
			
		||||
  const double old_value = this->last_value_;
 | 
			
		||||
 
 | 
			
		||||
@@ -63,8 +63,6 @@ class IntegrationSensor : public sensor::Sensor, public Component {
 | 
			
		||||
    this->last_save_ = now;
 | 
			
		||||
    this->rtc_.save(&result_f);
 | 
			
		||||
  }
 | 
			
		||||
  std::string unit_of_measurement() override;
 | 
			
		||||
  int8_t accuracy_decimals() override { return this->sensor_->get_accuracy_decimals() + 2; }
 | 
			
		||||
 | 
			
		||||
  sensor::Sensor *sensor_;
 | 
			
		||||
  IntegrationSensorTime time_;
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,14 @@ import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome import automation
 | 
			
		||||
from esphome.components import sensor
 | 
			
		||||
from esphome.const import CONF_ICON, CONF_ID, CONF_SENSOR, CONF_RESTORE
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_ICON,
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    CONF_SENSOR,
 | 
			
		||||
    CONF_RESTORE,
 | 
			
		||||
    CONF_UNIT_OF_MEASUREMENT,
 | 
			
		||||
    CONF_ACCURACY_DECIMALS,
 | 
			
		||||
)
 | 
			
		||||
from esphome.core.entity_helpers import inherit_property_from
 | 
			
		||||
 | 
			
		||||
integration_ns = cg.esphome_ns.namespace("integration")
 | 
			
		||||
@@ -30,6 +37,18 @@ CONF_TIME_UNIT = "time_unit"
 | 
			
		||||
CONF_INTEGRATION_METHOD = "integration_method"
 | 
			
		||||
CONF_MIN_SAVE_INTERVAL = "min_save_interval"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def inherit_unit_of_measurement(uom, config):
 | 
			
		||||
    suffix = config[CONF_TIME_UNIT]
 | 
			
		||||
    if uom.endswith("/" + suffix):
 | 
			
		||||
        return uom[0 : -len("/" + suffix)]
 | 
			
		||||
    return uom + suffix
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def inherit_accuracy_decimals(decimals, config):
 | 
			
		||||
    return decimals + 2
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend(
 | 
			
		||||
    {
 | 
			
		||||
        cv.GenerateID(): cv.declare_id(IntegrationSensor),
 | 
			
		||||
@@ -51,11 +70,19 @@ FINAL_VALIDATE_SCHEMA = cv.All(
 | 
			
		||||
        {
 | 
			
		||||
            cv.Required(CONF_ID): cv.use_id(IntegrationSensor),
 | 
			
		||||
            cv.Optional(CONF_ICON): cv.icon,
 | 
			
		||||
            cv.Optional(CONF_UNIT_OF_MEASUREMENT): sensor.validate_unit_of_measurement,
 | 
			
		||||
            cv.Optional(CONF_ACCURACY_DECIMALS): sensor.validate_accuracy_decimals,
 | 
			
		||||
            cv.Required(CONF_SENSOR): cv.use_id(sensor.Sensor),
 | 
			
		||||
        },
 | 
			
		||||
        extra=cv.ALLOW_EXTRA,
 | 
			
		||||
    ),
 | 
			
		||||
    inherit_property_from(CONF_ICON, CONF_SENSOR),
 | 
			
		||||
    inherit_property_from(
 | 
			
		||||
        CONF_UNIT_OF_MEASUREMENT, CONF_SENSOR, transform=inherit_unit_of_measurement
 | 
			
		||||
    ),
 | 
			
		||||
    inherit_property_from(
 | 
			
		||||
        CONF_ACCURACY_DECIMALS, CONF_SENSOR, transform=inherit_accuracy_decimals
 | 
			
		||||
    ),
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -7,12 +7,11 @@ json_ns = cg.esphome_ns.namespace("json")
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = cv.All(
 | 
			
		||||
    cv.Schema({}),
 | 
			
		||||
    cv.only_with_arduino,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@coroutine_with_priority(1.0)
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    cg.add_library("ottowinter/ArduinoJson-esphomelib", "5.13.3")
 | 
			
		||||
    cg.add_library("bblanchon/ArduinoJson", "6.18.5")
 | 
			
		||||
    cg.add_define("USE_JSON")
 | 
			
		||||
    cg.add_global(json_ns.using)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,13 @@
 | 
			
		||||
#ifdef USE_ARDUINO
 | 
			
		||||
 | 
			
		||||
#include "json_util.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP8266
 | 
			
		||||
#include <Esp.h>
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_ESP32
 | 
			
		||||
#include <esp_heap_caps.h>
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace json {
 | 
			
		||||
 | 
			
		||||
@@ -10,110 +15,49 @@ static const char *const TAG = "json";
 | 
			
		||||
 | 
			
		||||
static std::vector<char> global_json_build_buffer;  // NOLINT
 | 
			
		||||
 | 
			
		||||
const char *build_json(const json_build_t &f, size_t *length) {
 | 
			
		||||
  global_json_buffer.clear();
 | 
			
		||||
  JsonObject &root = global_json_buffer.createObject();
 | 
			
		||||
std::string build_json(const json_build_t &f) {
 | 
			
		||||
  // Here we are allocating as much heap memory as available minus 2kb to be safe
 | 
			
		||||
  // as we can not have a true dynamic sized document.
 | 
			
		||||
  // The excess memory is freed below with `shrinkToFit()`
 | 
			
		||||
#ifdef USE_ESP8266
 | 
			
		||||
  const size_t free_heap = ESP.getMaxFreeBlockSize() - 2048;  // NOLINT(readability-static-accessed-through-instance)
 | 
			
		||||
#elif defined(USE_ESP32)
 | 
			
		||||
  const size_t free_heap = heap_caps_get_largest_free_block(MALLOC_CAP_DEFAULT) - 2048;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  DynamicJsonDocument json_document(free_heap);
 | 
			
		||||
  JsonObject root = json_document.to<JsonObject>();
 | 
			
		||||
  f(root);
 | 
			
		||||
  json_document.shrinkToFit();
 | 
			
		||||
 | 
			
		||||
  // The Json buffer size gives us a good estimate for the required size.
 | 
			
		||||
  // Usually, it's a bit larger than the actual required string size
 | 
			
		||||
  //             | JSON Buffer Size | String Size |
 | 
			
		||||
  // Discovery   | 388              | 351         |
 | 
			
		||||
  // Discovery   | 372              | 356         |
 | 
			
		||||
  // Discovery   | 336              | 311         |
 | 
			
		||||
  // Discovery   | 408              | 393         |
 | 
			
		||||
  global_json_build_buffer.reserve(global_json_buffer.size() + 1);
 | 
			
		||||
  size_t bytes_written = root.printTo(global_json_build_buffer.data(), global_json_build_buffer.capacity());
 | 
			
		||||
 | 
			
		||||
  if (bytes_written >= global_json_build_buffer.capacity() - 1) {
 | 
			
		||||
    global_json_build_buffer.reserve(root.measureLength() + 1);
 | 
			
		||||
    bytes_written = root.printTo(global_json_build_buffer.data(), global_json_build_buffer.capacity());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  *length = bytes_written;
 | 
			
		||||
  return global_json_build_buffer.data();
 | 
			
		||||
  std::string output;
 | 
			
		||||
  serializeJson(json_document, output);
 | 
			
		||||
  return output;
 | 
			
		||||
}
 | 
			
		||||
void parse_json(const std::string &data, const json_parse_t &f) {
 | 
			
		||||
  global_json_buffer.clear();
 | 
			
		||||
  JsonObject &root = global_json_buffer.parseObject(data);
 | 
			
		||||
 | 
			
		||||
  if (!root.success()) {
 | 
			
		||||
void parse_json(const std::string &data, const json_parse_t &f) {
 | 
			
		||||
  // Here we are allocating as much heap memory as available minus 2kb to be safe
 | 
			
		||||
  // as we can not have a true dynamic sized document.
 | 
			
		||||
  // The excess memory is freed below with `shrinkToFit()`
 | 
			
		||||
#ifdef USE_ESP8266
 | 
			
		||||
  const size_t free_heap = ESP.getMaxFreeBlockSize() - 2048;  // NOLINT(readability-static-accessed-through-instance)
 | 
			
		||||
#elif defined(USE_ESP32)
 | 
			
		||||
  const size_t free_heap = heap_caps_get_largest_free_block(MALLOC_CAP_DEFAULT) - 2048;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  DynamicJsonDocument json_document(free_heap);
 | 
			
		||||
  DeserializationError err = deserializeJson(json_document, data);
 | 
			
		||||
  json_document.shrinkToFit();
 | 
			
		||||
 | 
			
		||||
  JsonObject root = json_document.as<JsonObject>();
 | 
			
		||||
 | 
			
		||||
  if (err) {
 | 
			
		||||
    ESP_LOGW(TAG, "Parsing JSON failed.");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  f(root);
 | 
			
		||||
}
 | 
			
		||||
std::string build_json(const json_build_t &f) {
 | 
			
		||||
  size_t len;
 | 
			
		||||
  const char *c_str = build_json(f, &len);
 | 
			
		||||
  return std::string(c_str, len);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
VectorJsonBuffer::String::String(VectorJsonBuffer *parent) : parent_(parent), start_(parent->size_) {}
 | 
			
		||||
void VectorJsonBuffer::String::append(char c) const {
 | 
			
		||||
  char *last = static_cast<char *>(this->parent_->do_alloc(1));
 | 
			
		||||
  *last = c;
 | 
			
		||||
}
 | 
			
		||||
const char *VectorJsonBuffer::String::c_str() const {
 | 
			
		||||
  this->append('\0');
 | 
			
		||||
  return &this->parent_->buffer_[this->start_];
 | 
			
		||||
}
 | 
			
		||||
void VectorJsonBuffer::clear() {
 | 
			
		||||
  for (char *block : this->free_blocks_)
 | 
			
		||||
    free(block);  // NOLINT
 | 
			
		||||
 | 
			
		||||
  this->size_ = 0;
 | 
			
		||||
  this->free_blocks_.clear();
 | 
			
		||||
}
 | 
			
		||||
VectorJsonBuffer::String VectorJsonBuffer::startString() { return {this}; }  // NOLINT
 | 
			
		||||
void *VectorJsonBuffer::alloc(size_t bytes) {
 | 
			
		||||
  // Make sure memory addresses are aligned
 | 
			
		||||
  uint32_t new_size = round_size_up(this->size_);
 | 
			
		||||
  this->resize(new_size);
 | 
			
		||||
  return this->do_alloc(bytes);
 | 
			
		||||
}
 | 
			
		||||
void *VectorJsonBuffer::do_alloc(size_t bytes) {  // NOLINT
 | 
			
		||||
  const uint32_t begin = this->size_;
 | 
			
		||||
  this->resize(begin + bytes);
 | 
			
		||||
  return &this->buffer_[begin];
 | 
			
		||||
}
 | 
			
		||||
void VectorJsonBuffer::resize(size_t size) {  // NOLINT
 | 
			
		||||
  if (size <= this->size_) {
 | 
			
		||||
    this->size_ = size;
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->reserve(size);
 | 
			
		||||
  this->size_ = size;
 | 
			
		||||
}
 | 
			
		||||
void VectorJsonBuffer::reserve(size_t size) {  // NOLINT
 | 
			
		||||
  if (size <= this->capacity_)
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  uint32_t target_capacity = this->capacity_;
 | 
			
		||||
  if (this->capacity_ == 0) {
 | 
			
		||||
    // lazily initialize with a reasonable size
 | 
			
		||||
    target_capacity = JSON_OBJECT_SIZE(16);
 | 
			
		||||
  }
 | 
			
		||||
  while (target_capacity < size)
 | 
			
		||||
    target_capacity *= 2;
 | 
			
		||||
 | 
			
		||||
  char *old_buffer = this->buffer_;
 | 
			
		||||
  this->buffer_ = new char[target_capacity];  // NOLINT
 | 
			
		||||
  if (old_buffer != nullptr && this->capacity_ != 0) {
 | 
			
		||||
    this->free_blocks_.push_back(old_buffer);
 | 
			
		||||
    memcpy(this->buffer_, old_buffer, this->capacity_);
 | 
			
		||||
  }
 | 
			
		||||
  this->capacity_ = target_capacity;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
size_t VectorJsonBuffer::size() const { return this->size_; }
 | 
			
		||||
 | 
			
		||||
VectorJsonBuffer global_json_buffer;  // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
 | 
			
		||||
 | 
			
		||||
}  // namespace json
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 | 
			
		||||
#endif  // USE_ARDUINO
 | 
			
		||||
 
 | 
			
		||||
@@ -1,68 +1,28 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ARDUINO
 | 
			
		||||
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/helpers.h"
 | 
			
		||||
 | 
			
		||||
#undef ARDUINOJSON_ENABLE_STD_STRING
 | 
			
		||||
#define ARDUINOJSON_ENABLE_STD_STRING 1  // NOLINT
 | 
			
		||||
 | 
			
		||||
#include <ArduinoJson.h>
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace json {
 | 
			
		||||
 | 
			
		||||
/// Callback function typedef for parsing JsonObjects.
 | 
			
		||||
using json_parse_t = std::function<void(JsonObject &)>;
 | 
			
		||||
using json_parse_t = std::function<void(JsonObject)>;
 | 
			
		||||
 | 
			
		||||
/// Callback function typedef for building JsonObjects.
 | 
			
		||||
using json_build_t = std::function<void(JsonObject &)>;
 | 
			
		||||
using json_build_t = std::function<void(JsonObject)>;
 | 
			
		||||
 | 
			
		||||
/// Build a JSON string with the provided json build function.
 | 
			
		||||
const char *build_json(const json_build_t &f, size_t *length);
 | 
			
		||||
 | 
			
		||||
std::string build_json(const json_build_t &f);
 | 
			
		||||
 | 
			
		||||
/// Parse a JSON string and run the provided json parse function if it's valid.
 | 
			
		||||
void parse_json(const std::string &data, const json_parse_t &f);
 | 
			
		||||
 | 
			
		||||
class VectorJsonBuffer : public ArduinoJson::Internals::JsonBufferBase<VectorJsonBuffer> {
 | 
			
		||||
 public:
 | 
			
		||||
  class String {
 | 
			
		||||
   public:
 | 
			
		||||
    String(VectorJsonBuffer *parent);
 | 
			
		||||
 | 
			
		||||
    void append(char c) const;
 | 
			
		||||
 | 
			
		||||
    const char *c_str() const;
 | 
			
		||||
 | 
			
		||||
   protected:
 | 
			
		||||
    VectorJsonBuffer *parent_;
 | 
			
		||||
    uint32_t start_;
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  void *alloc(size_t bytes) override;
 | 
			
		||||
 | 
			
		||||
  size_t size() const;
 | 
			
		||||
 | 
			
		||||
  void clear();
 | 
			
		||||
 | 
			
		||||
  String startString();  // NOLINT
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void *do_alloc(size_t bytes);  // NOLINT
 | 
			
		||||
 | 
			
		||||
  void resize(size_t size);  // NOLINT
 | 
			
		||||
 | 
			
		||||
  void reserve(size_t size);  // NOLINT
 | 
			
		||||
 | 
			
		||||
  char *buffer_{nullptr};
 | 
			
		||||
  size_t size_{0};
 | 
			
		||||
  size_t capacity_{0};
 | 
			
		||||
  std::vector<char *> free_blocks_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
extern VectorJsonBuffer global_json_buffer;  // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
 | 
			
		||||
 | 
			
		||||
}  // namespace json
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 | 
			
		||||
#endif  // USE_ARDUINO
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										1
									
								
								esphome/components/kalman_combinator/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								esphome/components/kalman_combinator/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
CODEOWNERS = ["@Cat-Ion"]
 | 
			
		||||
							
								
								
									
										82
									
								
								esphome/components/kalman_combinator/kalman_combinator.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								esphome/components/kalman_combinator/kalman_combinator.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,82 @@
 | 
			
		||||
#include "kalman_combinator.h"
 | 
			
		||||
#include "esphome/core/hal.h"
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <functional>
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace kalman_combinator {
 | 
			
		||||
 | 
			
		||||
void KalmanCombinatorComponent::dump_config() {
 | 
			
		||||
  ESP_LOGCONFIG("kalman_combinator", "Kalman Combinator:");
 | 
			
		||||
  ESP_LOGCONFIG("kalman_combinator", "  Update variance: %f per ms", this->update_variance_value_);
 | 
			
		||||
  ESP_LOGCONFIG("kalman_combinator", "  Sensors:");
 | 
			
		||||
  for (const auto &sensor : this->sensors_) {
 | 
			
		||||
    auto &entity = *sensor.first;
 | 
			
		||||
    ESP_LOGCONFIG("kalman_combinator", "    - %s", entity.get_name().c_str());
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void KalmanCombinatorComponent::setup() {
 | 
			
		||||
  for (const auto &sensor : this->sensors_) {
 | 
			
		||||
    const auto stddev = sensor.second;
 | 
			
		||||
    sensor.first->add_on_state_callback([this, stddev](float x) -> void { this->correct_(x, stddev(x)); });
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void KalmanCombinatorComponent::add_source(Sensor *sensor, std::function<float(float)> const &stddev) {
 | 
			
		||||
  this->sensors_.emplace_back(sensor, stddev);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void KalmanCombinatorComponent::add_source(Sensor *sensor, float stddev) {
 | 
			
		||||
  this->add_source(sensor, std::function<float(float)>{[stddev](float) -> float { return stddev; }});
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void KalmanCombinatorComponent::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 KalmanCombinatorComponent::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));
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
}  // namespace kalman_combinator
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										46
									
								
								esphome/components/kalman_combinator/kalman_combinator.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								esphome/components/kalman_combinator/kalman_combinator.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/components/sensor/sensor.h"
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace kalman_combinator {
 | 
			
		||||
 | 
			
		||||
class KalmanCombinatorComponent : public Component, public sensor::Sensor {
 | 
			
		||||
 public:
 | 
			
		||||
  KalmanCombinatorComponent() = default;
 | 
			
		||||
 | 
			
		||||
  float get_setup_priority() const override { return esphome::setup_priority::DATA; }
 | 
			
		||||
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
  void setup() override;
 | 
			
		||||
 | 
			
		||||
  void add_source(Sensor *sensor, std::function<float(float)> const &stddev);
 | 
			
		||||
  void add_source(Sensor *sensor, float stddev);
 | 
			
		||||
  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; }
 | 
			
		||||
 | 
			
		||||
 private:
 | 
			
		||||
  void update_variance_();
 | 
			
		||||
  void correct_(float value, float stddev);
 | 
			
		||||
 | 
			
		||||
  // Source sensors and their error functions
 | 
			
		||||
  std::vector<std::pair<Sensor *, std::function<float(float)>>> sensors_;
 | 
			
		||||
 | 
			
		||||
  // 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};
 | 
			
		||||
};
 | 
			
		||||
}  // namespace kalman_combinator
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										87
									
								
								esphome/components/kalman_combinator/sensor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										87
									
								
								esphome/components/kalman_combinator/sensor.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,87 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.components import sensor
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    CONF_SOURCE,
 | 
			
		||||
    CONF_ACCURACY_DECIMALS,
 | 
			
		||||
    CONF_DEVICE_CLASS,
 | 
			
		||||
    CONF_ENTITY_CATEGORY,
 | 
			
		||||
    CONF_ICON,
 | 
			
		||||
    CONF_UNIT_OF_MEASUREMENT,
 | 
			
		||||
)
 | 
			
		||||
from esphome.core.entity_helpers import inherit_property_from
 | 
			
		||||
 | 
			
		||||
kalman_combinator_ns = cg.esphome_ns.namespace("kalman_combinator")
 | 
			
		||||
KalmanCombinatorComponent = kalman_combinator_ns.class_(
 | 
			
		||||
    "KalmanCombinatorComponent", cg.Component, sensor.Sensor
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CONF_ERROR = "error"
 | 
			
		||||
CONF_SOURCES = "sources"
 | 
			
		||||
CONF_PROCESS_STD_DEV = "process_std_dev"
 | 
			
		||||
CONF_STD_DEV = "std_dev"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend(cv.COMPONENT_SCHEMA).extend(
 | 
			
		||||
    {
 | 
			
		||||
        cv.GenerateID(): cv.declare_id(KalmanCombinatorComponent),
 | 
			
		||||
        cv.Required(CONF_PROCESS_STD_DEV): cv.positive_float,
 | 
			
		||||
        cv.Required(CONF_SOURCES): cv.ensure_list(
 | 
			
		||||
            cv.Schema(
 | 
			
		||||
                {
 | 
			
		||||
                    cv.Required(CONF_SOURCE): cv.use_id(sensor.Sensor),
 | 
			
		||||
                    cv.Required(CONF_ERROR): cv.templatable(cv.positive_float),
 | 
			
		||||
                }
 | 
			
		||||
            ),
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_STD_DEV): sensor.SENSOR_SCHEMA,
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
# Inherit some sensor values from the first source, for both the state and the error value
 | 
			
		||||
properties_to_inherit = [
 | 
			
		||||
    CONF_ACCURACY_DECIMALS,
 | 
			
		||||
    CONF_DEVICE_CLASS,
 | 
			
		||||
    CONF_ENTITY_CATEGORY,
 | 
			
		||||
    CONF_ICON,
 | 
			
		||||
    CONF_UNIT_OF_MEASUREMENT,
 | 
			
		||||
    # CONF_STATE_CLASS could also be inherited, but might lead to unexpected behaviour with "total_increasing"
 | 
			
		||||
]
 | 
			
		||||
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(
 | 
			
		||||
    CONFIG_SCHEMA.extend(
 | 
			
		||||
        {cv.Required(CONF_ID): cv.use_id(KalmanCombinatorComponent)},
 | 
			
		||||
        extra=cv.ALLOW_EXTRA,
 | 
			
		||||
    ),
 | 
			
		||||
    *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)
 | 
			
		||||
 | 
			
		||||
    cg.add(var.set_process_std_dev(config[CONF_PROCESS_STD_DEV]))
 | 
			
		||||
    for source_conf in config[CONF_SOURCES]:
 | 
			
		||||
        source = await cg.get_variable(source_conf[CONF_SOURCE])
 | 
			
		||||
        error = await cg.templatable(
 | 
			
		||||
            source_conf[CONF_ERROR],
 | 
			
		||||
            [(float, "x")],
 | 
			
		||||
            cg.float_,
 | 
			
		||||
        )
 | 
			
		||||
        cg.add(var.add_source(source, error))
 | 
			
		||||
 | 
			
		||||
    if CONF_STD_DEV in config:
 | 
			
		||||
        sens = await sensor.new_sensor(config[CONF_STD_DEV])
 | 
			
		||||
        cg.add(var.set_std_dev_sensor(sens))
 | 
			
		||||
@@ -331,9 +331,10 @@ class AddressableFlickerEffect : public AddressableLightEffect {
 | 
			
		||||
      return;
 | 
			
		||||
 | 
			
		||||
    this->last_update_ = now;
 | 
			
		||||
    fast_random_set_seed(random_uint32());
 | 
			
		||||
    uint32_t rng_state = random_uint32();
 | 
			
		||||
    for (auto var : it) {
 | 
			
		||||
      const uint8_t flicker = fast_random_8() % intensity;
 | 
			
		||||
      rng_state = (rng_state * 0x9E3779B9) + 0x9E37;
 | 
			
		||||
      const uint8_t flicker = (rng_state & 0xFF) % intensity;
 | 
			
		||||
      // scale down by random factor
 | 
			
		||||
      var = var.get() * (255 - flicker);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,7 @@ namespace light {
 | 
			
		||||
 | 
			
		||||
// See https://www.home-assistant.io/integrations/light.mqtt/#json-schema for documentation on the schema
 | 
			
		||||
 | 
			
		||||
void LightJSONSchema::dump_json(LightState &state, JsonObject &root) {
 | 
			
		||||
void LightJSONSchema::dump_json(LightState &state, JsonObject root) {
 | 
			
		||||
  if (state.supports_effects())
 | 
			
		||||
    root["effect"] = state.get_effect_name();
 | 
			
		||||
 | 
			
		||||
@@ -52,7 +52,7 @@ void LightJSONSchema::dump_json(LightState &state, JsonObject &root) {
 | 
			
		||||
  if (values.get_color_mode() & ColorCapability::BRIGHTNESS)
 | 
			
		||||
    root["brightness"] = uint8_t(values.get_brightness() * 255);
 | 
			
		||||
 | 
			
		||||
  JsonObject &color = root.createNestedObject("color");
 | 
			
		||||
  JsonObject color = root.createNestedObject("color");
 | 
			
		||||
  if (values.get_color_mode() & ColorCapability::RGB) {
 | 
			
		||||
    color["r"] = uint8_t(values.get_color_brightness() * values.get_red() * 255);
 | 
			
		||||
    color["g"] = uint8_t(values.get_color_brightness() * values.get_green() * 255);
 | 
			
		||||
@@ -72,7 +72,7 @@ void LightJSONSchema::dump_json(LightState &state, JsonObject &root) {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LightJSONSchema::parse_color_json(LightState &state, LightCall &call, JsonObject &root) {
 | 
			
		||||
void LightJSONSchema::parse_color_json(LightState &state, LightCall &call, JsonObject root) {
 | 
			
		||||
  if (root.containsKey("state")) {
 | 
			
		||||
    auto val = parse_on_off(root["state"]);
 | 
			
		||||
    switch (val) {
 | 
			
		||||
@@ -95,7 +95,7 @@ void LightJSONSchema::parse_color_json(LightState &state, LightCall &call, JsonO
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (root.containsKey("color")) {
 | 
			
		||||
    JsonObject &color = root["color"];
 | 
			
		||||
    JsonObject color = root["color"];
 | 
			
		||||
    // HA also encodes brightness information in the r, g, b values, so extract that and set it as color brightness.
 | 
			
		||||
    float max_rgb = 0.0f;
 | 
			
		||||
    if (color.containsKey("r")) {
 | 
			
		||||
@@ -140,7 +140,7 @@ void LightJSONSchema::parse_color_json(LightState &state, LightCall &call, JsonO
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LightJSONSchema::parse_json(LightState &state, LightCall &call, JsonObject &root) {
 | 
			
		||||
void LightJSONSchema::parse_json(LightState &state, LightCall &call, JsonObject root) {
 | 
			
		||||
  LightJSONSchema::parse_color_json(state, call, root);
 | 
			
		||||
 | 
			
		||||
  if (root.containsKey("flash")) {
 | 
			
		||||
 
 | 
			
		||||
@@ -14,12 +14,12 @@ namespace light {
 | 
			
		||||
class LightJSONSchema {
 | 
			
		||||
 public:
 | 
			
		||||
  /// Dump the state of a light as JSON.
 | 
			
		||||
  static void dump_json(LightState &state, JsonObject &root);
 | 
			
		||||
  static void dump_json(LightState &state, JsonObject root);
 | 
			
		||||
  /// Parse the JSON state of a light to a LightCall.
 | 
			
		||||
  static void parse_json(LightState &state, LightCall &call, JsonObject &root);
 | 
			
		||||
  static void parse_json(LightState &state, LightCall &call, JsonObject root);
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  static void parse_color_json(LightState &state, LightCall &call, JsonObject &root);
 | 
			
		||||
  static void parse_color_json(LightState &state, LightCall &call, JsonObject root);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace light
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										27
									
								
								esphome/components/mcp3204/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								esphome/components/mcp3204/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.components import spi
 | 
			
		||||
from esphome.const import CONF_ID
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["spi"]
 | 
			
		||||
MULTI_CONF = True
 | 
			
		||||
CODEOWNERS = ["@rsumner"]
 | 
			
		||||
 | 
			
		||||
mcp3204_ns = cg.esphome_ns.namespace("mcp3204")
 | 
			
		||||
MCP3204 = mcp3204_ns.class_("MCP3204", cg.Component, spi.SPIDevice)
 | 
			
		||||
 | 
			
		||||
CONF_REFERENCE_VOLTAGE = "reference_voltage"
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = cv.Schema(
 | 
			
		||||
    {
 | 
			
		||||
        cv.GenerateID(): cv.declare_id(MCP3204),
 | 
			
		||||
        cv.Optional(CONF_REFERENCE_VOLTAGE, default="3.3V"): cv.voltage,
 | 
			
		||||
    }
 | 
			
		||||
).extend(spi.spi_device_schema(cs_pin_required=True))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    cg.add(var.set_reference_voltage(config[CONF_REFERENCE_VOLTAGE]))
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
    await spi.register_spi_device(var, config)
 | 
			
		||||
							
								
								
									
										35
									
								
								esphome/components/mcp3204/mcp3204.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								esphome/components/mcp3204/mcp3204.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
#include "mcp3204.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace mcp3204 {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "mcp3204";
 | 
			
		||||
 | 
			
		||||
float MCP3204::get_setup_priority() const { return setup_priority::HARDWARE; }
 | 
			
		||||
 | 
			
		||||
void MCP3204::setup() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Setting up mcp3204");
 | 
			
		||||
  this->spi_setup();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MCP3204::dump_config() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "MCP3204:");
 | 
			
		||||
  LOG_PIN("  CS Pin:", this->cs_);
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Reference Voltage: %.2fV", this->reference_voltage_);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float MCP3204::read_data(uint8_t pin) {
 | 
			
		||||
  uint8_t adc_primary_config = 0b00000110 & 0b00000111;
 | 
			
		||||
  uint8_t adc_secondary_config = pin << 6;
 | 
			
		||||
  this->enable();
 | 
			
		||||
  this->transfer_byte(adc_primary_config);
 | 
			
		||||
  uint8_t adc_primary_byte = this->transfer_byte(adc_secondary_config);
 | 
			
		||||
  uint8_t adc_secondary_byte = this->transfer_byte(0x00);
 | 
			
		||||
  this->disable();
 | 
			
		||||
  uint16_t digital_value = (adc_primary_byte << 8 | adc_secondary_byte) & 0b111111111111;
 | 
			
		||||
  return float(digital_value) / 4096.000 * this->reference_voltage_;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace mcp3204
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										28
									
								
								esphome/components/mcp3204/mcp3204.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								esphome/components/mcp3204/mcp3204.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/core/hal.h"
 | 
			
		||||
#include "esphome/components/spi/spi.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace mcp3204 {
 | 
			
		||||
 | 
			
		||||
class MCP3204 : public Component,
 | 
			
		||||
                public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
 | 
			
		||||
                                      spi::DATA_RATE_1MHZ> {
 | 
			
		||||
 public:
 | 
			
		||||
  MCP3204() = default;
 | 
			
		||||
 | 
			
		||||
  void set_reference_voltage(float reference_voltage) { this->reference_voltage_ = reference_voltage; }
 | 
			
		||||
 | 
			
		||||
  void setup() override;
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
  float get_setup_priority() const override;
 | 
			
		||||
  float read_data(uint8_t pin);
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  float reference_voltage_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace mcp3204
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										32
									
								
								esphome/components/mcp3204/sensor/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								esphome/components/mcp3204/sensor/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.components import sensor, voltage_sampler
 | 
			
		||||
from esphome.const import CONF_ID, CONF_NUMBER
 | 
			
		||||
from .. import mcp3204_ns, MCP3204
 | 
			
		||||
 | 
			
		||||
AUTO_LOAD = ["voltage_sampler"]
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["mcp3204"]
 | 
			
		||||
 | 
			
		||||
MCP3204Sensor = mcp3204_ns.class_(
 | 
			
		||||
    "MCP3204Sensor", sensor.Sensor, cg.PollingComponent, voltage_sampler.VoltageSampler
 | 
			
		||||
)
 | 
			
		||||
CONF_MCP3204_ID = "mcp3204_id"
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend(
 | 
			
		||||
    {
 | 
			
		||||
        cv.GenerateID(): cv.declare_id(MCP3204Sensor),
 | 
			
		||||
        cv.GenerateID(CONF_MCP3204_ID): cv.use_id(MCP3204),
 | 
			
		||||
        cv.Required(CONF_NUMBER): cv.int_range(min=0, max=3),
 | 
			
		||||
    }
 | 
			
		||||
).extend(cv.polling_component_schema("60s"))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    var = cg.new_Pvariable(
 | 
			
		||||
        config[CONF_ID],
 | 
			
		||||
        config[CONF_NUMBER],
 | 
			
		||||
    )
 | 
			
		||||
    await cg.register_parented(var, config[CONF_MCP3204_ID])
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
    await sensor.register_sensor(var, config)
 | 
			
		||||
							
								
								
									
										23
									
								
								esphome/components/mcp3204/sensor/mcp3204_sensor.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								esphome/components/mcp3204/sensor/mcp3204_sensor.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,23 @@
 | 
			
		||||
#include "mcp3204_sensor.h"
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace mcp3204 {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "mcp3204.sensor";
 | 
			
		||||
 | 
			
		||||
MCP3204Sensor::MCP3204Sensor(uint8_t pin) : pin_(pin) {}
 | 
			
		||||
 | 
			
		||||
float MCP3204Sensor::get_setup_priority() const { return setup_priority::DATA; }
 | 
			
		||||
 | 
			
		||||
void MCP3204Sensor::dump_config() {
 | 
			
		||||
  LOG_SENSOR("", "MCP3204 Sensor", this);
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Pin: %u", this->pin_);
 | 
			
		||||
  LOG_UPDATE_INTERVAL(this);
 | 
			
		||||
}
 | 
			
		||||
float MCP3204Sensor::sample() { return this->parent_->read_data(this->pin_); }
 | 
			
		||||
void MCP3204Sensor::update() { this->publish_state(this->sample()); }
 | 
			
		||||
 | 
			
		||||
}  // namespace mcp3204
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										30
									
								
								esphome/components/mcp3204/sensor/mcp3204_sensor.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								esphome/components/mcp3204/sensor/mcp3204_sensor.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/sensor/sensor.h"
 | 
			
		||||
#include "esphome/components/voltage_sampler/voltage_sampler.h"
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/core/hal.h"
 | 
			
		||||
 | 
			
		||||
#include "../mcp3204.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace mcp3204 {
 | 
			
		||||
 | 
			
		||||
class MCP3204Sensor : public PollingComponent,
 | 
			
		||||
                      public Parented<MCP3204>,
 | 
			
		||||
                      public sensor::Sensor,
 | 
			
		||||
                      public voltage_sampler::VoltageSampler {
 | 
			
		||||
 public:
 | 
			
		||||
  MCP3204Sensor(uint8_t pin);
 | 
			
		||||
 | 
			
		||||
  void update() override;
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
  float get_setup_priority() const override;
 | 
			
		||||
  float sample() override;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  uint8_t pin_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace mcp3204
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										0
									
								
								esphome/components/mcp47a1/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								esphome/components/mcp47a1/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										21
									
								
								esphome/components/mcp47a1/mcp47a1.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								esphome/components/mcp47a1/mcp47a1.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
#include "mcp47a1.h"
 | 
			
		||||
#include "esphome/core/helpers.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace mcp47a1 {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "mcp47a1";
 | 
			
		||||
 | 
			
		||||
void MCP47A1::dump_config() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "MCP47A1 Output:");
 | 
			
		||||
  LOG_I2C_DEVICE(this);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MCP47A1::write_state(float state) {
 | 
			
		||||
  const uint8_t value = remap(state, 0.0f, 1.0f, 63, 0);
 | 
			
		||||
  this->write_byte(0, value);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace mcp47a1
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										17
									
								
								esphome/components/mcp47a1/mcp47a1.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								esphome/components/mcp47a1/mcp47a1.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/i2c/i2c.h"
 | 
			
		||||
#include "esphome/components/output/float_output.h"
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace mcp47a1 {
 | 
			
		||||
 | 
			
		||||
class MCP47A1 : public Component, public output::FloatOutput, public i2c::I2CDevice {
 | 
			
		||||
 public:
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
  void write_state(float state) override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace mcp47a1
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										27
									
								
								esphome/components/mcp47a1/output.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								esphome/components/mcp47a1/output.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
from esphome.components import output, i2c
 | 
			
		||||
from esphome.const import CONF_ID
 | 
			
		||||
 | 
			
		||||
CODEOWNERS = ["@jesserockz"]
 | 
			
		||||
DEPENDENCIES = ["i2c"]
 | 
			
		||||
 | 
			
		||||
mcp47a1_ns = cg.esphome_ns.namespace("mcp47a1")
 | 
			
		||||
MCP47A1 = mcp47a1_ns.class_("MCP47A1", output.FloatOutput, cg.Component, i2c.I2CDevice)
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = (
 | 
			
		||||
    output.FLOAT_OUTPUT_SCHEMA.extend(
 | 
			
		||||
        {
 | 
			
		||||
            cv.Required(CONF_ID): cv.declare_id(MCP47A1),
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
    .extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
    .extend(i2c.i2c_device_schema(0x2E))
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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)
 | 
			
		||||
    await output.register_output(var, config)
 | 
			
		||||
@@ -23,12 +23,12 @@ class IrFollowMeData : public IrData {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /* TEMPERATURE */
 | 
			
		||||
  uint8_t temp() const { return this->data_[4] - 1; }
 | 
			
		||||
  void set_temp(uint8_t val) { this->data_[4] = std::min(MAX_TEMP, val) + 1; }
 | 
			
		||||
  uint8_t temp() const { return this->get_value_(4) - 1; }
 | 
			
		||||
  void set_temp(uint8_t val) { this->set_value_(4, std::min(MAX_TEMP, val) + 1); }
 | 
			
		||||
 | 
			
		||||
  /* BEEPER */
 | 
			
		||||
  bool beeper() const { return this->data_[3] & 128; }
 | 
			
		||||
  void set_beeper(bool val) { this->set_value_(3, 1, 7, val); }
 | 
			
		||||
  bool beeper() const { return this->get_value_(3, 128); }
 | 
			
		||||
  void set_beeper(bool val) { this->set_mask_(3, val, 128); }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  static const uint8_t MAX_TEMP = 37;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										0
									
								
								esphome/components/midea_ir/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								esphome/components/midea_ir/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										25
									
								
								esphome/components/midea_ir/climate.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								esphome/components/midea_ir/climate.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.components import climate_ir
 | 
			
		||||
from esphome.const import CONF_ID
 | 
			
		||||
 | 
			
		||||
AUTO_LOAD = ["climate_ir", "coolix"]
 | 
			
		||||
CODEOWNERS = ["@dudanov"]
 | 
			
		||||
 | 
			
		||||
midea_ir_ns = cg.esphome_ns.namespace("midea_ir")
 | 
			
		||||
MideaIR = midea_ir_ns.class_("MideaIR", climate_ir.ClimateIR)
 | 
			
		||||
 | 
			
		||||
CONF_USE_FAHRENHEIT = "use_fahrenheit"
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = climate_ir.CLIMATE_IR_WITH_RECEIVER_SCHEMA.extend(
 | 
			
		||||
    {
 | 
			
		||||
        cv.GenerateID(): cv.declare_id(MideaIR),
 | 
			
		||||
        cv.Optional(CONF_USE_FAHRENHEIT, default=False): cv.boolean,
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    await climate_ir.register_climate_ir(var, config)
 | 
			
		||||
    cg.add(var.set_fahrenheit(config[CONF_USE_FAHRENHEIT]))
 | 
			
		||||
							
								
								
									
										92
									
								
								esphome/components/midea_ir/midea_data.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										92
									
								
								esphome/components/midea_ir/midea_data.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,92 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/remote_base/midea_protocol.h"
 | 
			
		||||
#include "esphome/components/climate/climate_mode.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace midea_ir {
 | 
			
		||||
 | 
			
		||||
using climate::ClimateMode;
 | 
			
		||||
using climate::ClimateFanMode;
 | 
			
		||||
using remote_base::MideaData;
 | 
			
		||||
 | 
			
		||||
class ControlData : public MideaData {
 | 
			
		||||
 public:
 | 
			
		||||
  // Default constructor (power: ON, mode: AUTO, fan: AUTO, temp: 25C)
 | 
			
		||||
  ControlData() : MideaData({MIDEA_TYPE_CONTROL, 0x82, 0x48, 0xFF, 0xFF}) {}
 | 
			
		||||
  // Copy from Base
 | 
			
		||||
  ControlData(const MideaData &data) : MideaData(data) {}
 | 
			
		||||
 | 
			
		||||
  void set_temp(float temp);
 | 
			
		||||
  float get_temp() const;
 | 
			
		||||
 | 
			
		||||
  void set_mode(ClimateMode mode);
 | 
			
		||||
  ClimateMode get_mode() const;
 | 
			
		||||
 | 
			
		||||
  void set_fan_mode(ClimateFanMode mode);
 | 
			
		||||
  ClimateFanMode get_fan_mode() const;
 | 
			
		||||
 | 
			
		||||
  void set_sleep_preset(bool value) { this->set_mask_(1, value, 64); }
 | 
			
		||||
  bool get_sleep_preset() const { return this->get_value_(1, 64); }
 | 
			
		||||
 | 
			
		||||
  void set_fahrenheit(bool value) { this->set_mask_(2, value, 32); }
 | 
			
		||||
  bool get_fahrenheit() const { return this->get_value_(2, 32); }
 | 
			
		||||
 | 
			
		||||
  void fix();
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  enum Mode : uint8_t {
 | 
			
		||||
    MODE_COOL,
 | 
			
		||||
    MODE_DRY,
 | 
			
		||||
    MODE_AUTO,
 | 
			
		||||
    MODE_HEAT,
 | 
			
		||||
    MODE_FAN_ONLY,
 | 
			
		||||
  };
 | 
			
		||||
  enum FanMode : uint8_t {
 | 
			
		||||
    FAN_AUTO,
 | 
			
		||||
    FAN_LOW,
 | 
			
		||||
    FAN_MEDIUM,
 | 
			
		||||
    FAN_HIGH,
 | 
			
		||||
  };
 | 
			
		||||
  void set_fan_mode_(FanMode mode) { this->set_value_(1, mode, 3, 3); }
 | 
			
		||||
  FanMode get_fan_mode_() const { return static_cast<FanMode>(this->get_value_(1, 3, 3)); }
 | 
			
		||||
  void set_mode_(Mode mode) { this->set_value_(1, mode, 7); }
 | 
			
		||||
  Mode get_mode_() const { return static_cast<Mode>(this->get_value_(1, 7)); }
 | 
			
		||||
  void set_power_(bool value) { this->set_mask_(1, value, 128); }
 | 
			
		||||
  bool get_power_() const { return this->get_value_(1, 128); }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class FollowMeData : public MideaData {
 | 
			
		||||
 public:
 | 
			
		||||
  // Default constructor (temp: 30C, beeper: off)
 | 
			
		||||
  FollowMeData() : MideaData({MIDEA_TYPE_FOLLOW_ME, 0x82, 0x48, 0x7F, 0x1F}) {}
 | 
			
		||||
  // Copy from Base
 | 
			
		||||
  FollowMeData(const MideaData &data) : MideaData(data) {}
 | 
			
		||||
  // Direct from temperature and beeper values
 | 
			
		||||
  FollowMeData(uint8_t temp, bool beeper = false) : FollowMeData() {
 | 
			
		||||
    this->set_temp(temp);
 | 
			
		||||
    this->set_beeper(beeper);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /* TEMPERATURE */
 | 
			
		||||
  uint8_t temp() const { return this->get_value_(4) - 1; }
 | 
			
		||||
  void set_temp(uint8_t val) { this->set_value_(4, std::min(MAX_TEMP, val) + 1); }
 | 
			
		||||
 | 
			
		||||
  /* BEEPER */
 | 
			
		||||
  bool beeper() const { return this->get_value_(3, 128); }
 | 
			
		||||
  void set_beeper(bool value) { this->set_mask_(3, value, 128); }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  static const uint8_t MAX_TEMP = 37;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class SpecialData : public MideaData {
 | 
			
		||||
 public:
 | 
			
		||||
  SpecialData(uint8_t code) : MideaData({MIDEA_TYPE_SPECIAL, code, 0xFF, 0xFF, 0xFF}) {}
 | 
			
		||||
  static const uint8_t VSWING_STEP = 1;
 | 
			
		||||
  static const uint8_t VSWING_TOGGLE = 2;
 | 
			
		||||
  static const uint8_t TURBO_TOGGLE = 9;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace midea_ir
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										201
									
								
								esphome/components/midea_ir/midea_ir.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										201
									
								
								esphome/components/midea_ir/midea_ir.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,201 @@
 | 
			
		||||
#include "midea_ir.h"
 | 
			
		||||
#include "midea_data.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
#include "esphome/core/helpers.h"
 | 
			
		||||
#include "esphome/components/coolix/coolix.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace midea_ir {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "midea_ir.climate";
 | 
			
		||||
 | 
			
		||||
void ControlData::set_temp(float temp) {
 | 
			
		||||
  uint8_t min;
 | 
			
		||||
  if (this->get_fahrenheit()) {
 | 
			
		||||
    min = MIDEA_TEMPF_MIN;
 | 
			
		||||
    temp = esphome::clamp<float>(celsius_to_fahrenheit(temp), MIDEA_TEMPF_MIN, MIDEA_TEMPF_MAX);
 | 
			
		||||
  } else {
 | 
			
		||||
    min = MIDEA_TEMPC_MIN;
 | 
			
		||||
    temp = esphome::clamp<float>(temp, MIDEA_TEMPC_MIN, MIDEA_TEMPC_MAX);
 | 
			
		||||
  }
 | 
			
		||||
  this->set_value_(2, lroundf(temp) - min, 31);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float ControlData::get_temp() const {
 | 
			
		||||
  const uint8_t temp = this->get_value_(2, 31);
 | 
			
		||||
  if (this->get_fahrenheit())
 | 
			
		||||
    return fahrenheit_to_celsius(static_cast<float>(temp + MIDEA_TEMPF_MIN));
 | 
			
		||||
  return static_cast<float>(temp + MIDEA_TEMPC_MIN);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ControlData::fix() {
 | 
			
		||||
  // In FAN_AUTO, modes COOL, HEAT and FAN_ONLY bit #5 in byte #1 must be set
 | 
			
		||||
  const uint8_t value = this->get_value_(1, 31);
 | 
			
		||||
  if (value == 0 || value == 3 || value == 4)
 | 
			
		||||
    this->set_mask_(1, true, 32);
 | 
			
		||||
  // In FAN_ONLY mode we need to set all temperature bits
 | 
			
		||||
  if (this->get_mode_() == MODE_FAN_ONLY)
 | 
			
		||||
    this->set_mask_(2, true, 31);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ControlData::set_mode(ClimateMode mode) {
 | 
			
		||||
  switch (mode) {
 | 
			
		||||
    case ClimateMode::CLIMATE_MODE_OFF:
 | 
			
		||||
      this->set_power_(false);
 | 
			
		||||
      return;
 | 
			
		||||
    case ClimateMode::CLIMATE_MODE_COOL:
 | 
			
		||||
      this->set_mode_(MODE_COOL);
 | 
			
		||||
      break;
 | 
			
		||||
    case ClimateMode::CLIMATE_MODE_DRY:
 | 
			
		||||
      this->set_mode_(MODE_DRY);
 | 
			
		||||
      break;
 | 
			
		||||
    case ClimateMode::CLIMATE_MODE_FAN_ONLY:
 | 
			
		||||
      this->set_mode_(MODE_FAN_ONLY);
 | 
			
		||||
      break;
 | 
			
		||||
    case ClimateMode::CLIMATE_MODE_HEAT:
 | 
			
		||||
      this->set_mode_(MODE_HEAT);
 | 
			
		||||
      break;
 | 
			
		||||
    default:
 | 
			
		||||
      this->set_mode_(MODE_AUTO);
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
  this->set_power_(true);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ClimateMode ControlData::get_mode() const {
 | 
			
		||||
  if (!this->get_power_())
 | 
			
		||||
    return ClimateMode::CLIMATE_MODE_OFF;
 | 
			
		||||
  switch (this->get_mode_()) {
 | 
			
		||||
    case MODE_COOL:
 | 
			
		||||
      return ClimateMode::CLIMATE_MODE_COOL;
 | 
			
		||||
    case MODE_DRY:
 | 
			
		||||
      return ClimateMode::CLIMATE_MODE_DRY;
 | 
			
		||||
    case MODE_FAN_ONLY:
 | 
			
		||||
      return ClimateMode::CLIMATE_MODE_FAN_ONLY;
 | 
			
		||||
    case MODE_HEAT:
 | 
			
		||||
      return ClimateMode::CLIMATE_MODE_HEAT;
 | 
			
		||||
    default:
 | 
			
		||||
      return ClimateMode::CLIMATE_MODE_HEAT_COOL;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ControlData::set_fan_mode(ClimateFanMode mode) {
 | 
			
		||||
  switch (mode) {
 | 
			
		||||
    case ClimateFanMode::CLIMATE_FAN_LOW:
 | 
			
		||||
      this->set_fan_mode_(FAN_LOW);
 | 
			
		||||
      break;
 | 
			
		||||
    case ClimateFanMode::CLIMATE_FAN_MEDIUM:
 | 
			
		||||
      this->set_fan_mode_(FAN_MEDIUM);
 | 
			
		||||
      break;
 | 
			
		||||
    case ClimateFanMode::CLIMATE_FAN_HIGH:
 | 
			
		||||
      this->set_fan_mode_(FAN_HIGH);
 | 
			
		||||
      break;
 | 
			
		||||
    default:
 | 
			
		||||
      this->set_fan_mode_(FAN_AUTO);
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ClimateFanMode ControlData::get_fan_mode() const {
 | 
			
		||||
  switch (this->get_fan_mode_()) {
 | 
			
		||||
    case FAN_LOW:
 | 
			
		||||
      return ClimateFanMode::CLIMATE_FAN_LOW;
 | 
			
		||||
    case FAN_MEDIUM:
 | 
			
		||||
      return ClimateFanMode::CLIMATE_FAN_MEDIUM;
 | 
			
		||||
    case FAN_HIGH:
 | 
			
		||||
      return ClimateFanMode::CLIMATE_FAN_HIGH;
 | 
			
		||||
    default:
 | 
			
		||||
      return ClimateFanMode::CLIMATE_FAN_AUTO;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MideaIR::control(const climate::ClimateCall &call) {
 | 
			
		||||
  // swing and preset resets after unit powered off
 | 
			
		||||
  if (call.get_mode() == climate::CLIMATE_MODE_OFF) {
 | 
			
		||||
    this->swing_mode = climate::CLIMATE_SWING_OFF;
 | 
			
		||||
    this->preset = climate::CLIMATE_PRESET_NONE;
 | 
			
		||||
  } else if (call.get_swing_mode().has_value() && ((*call.get_swing_mode() == climate::CLIMATE_SWING_OFF &&
 | 
			
		||||
                                                    this->swing_mode == climate::CLIMATE_SWING_VERTICAL) ||
 | 
			
		||||
                                                   (*call.get_swing_mode() == climate::CLIMATE_SWING_VERTICAL &&
 | 
			
		||||
                                                    this->swing_mode == climate::CLIMATE_SWING_OFF))) {
 | 
			
		||||
    this->swing_ = true;
 | 
			
		||||
  } else if (call.get_preset().has_value() &&
 | 
			
		||||
             ((*call.get_preset() == climate::CLIMATE_PRESET_NONE && this->preset == climate::CLIMATE_PRESET_BOOST) ||
 | 
			
		||||
              (*call.get_preset() == climate::CLIMATE_PRESET_BOOST && this->preset == climate::CLIMATE_PRESET_NONE))) {
 | 
			
		||||
    this->boost_ = true;
 | 
			
		||||
  }
 | 
			
		||||
  climate_ir::ClimateIR::control(call);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MideaIR::transmit_(MideaData &data) {
 | 
			
		||||
  data.finalize();
 | 
			
		||||
  auto transmit = this->transmitter_->transmit();
 | 
			
		||||
  remote_base::MideaProtocol().encode(transmit.get_data(), data);
 | 
			
		||||
  transmit.perform();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MideaIR::transmit_state() {
 | 
			
		||||
  if (this->swing_) {
 | 
			
		||||
    SpecialData data(SpecialData::VSWING_TOGGLE);
 | 
			
		||||
    this->transmit_(data);
 | 
			
		||||
    this->swing_ = false;
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (this->boost_) {
 | 
			
		||||
    SpecialData data(SpecialData::TURBO_TOGGLE);
 | 
			
		||||
    this->transmit_(data);
 | 
			
		||||
    this->boost_ = false;
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  ControlData data;
 | 
			
		||||
  data.set_fahrenheit(this->fahrenheit_);
 | 
			
		||||
  data.set_temp(this->target_temperature);
 | 
			
		||||
  data.set_mode(this->mode);
 | 
			
		||||
  data.set_fan_mode(this->fan_mode.value_or(ClimateFanMode::CLIMATE_FAN_AUTO));
 | 
			
		||||
  data.set_sleep_preset(this->preset == climate::CLIMATE_PRESET_SLEEP);
 | 
			
		||||
  data.fix();
 | 
			
		||||
  this->transmit_(data);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool MideaIR::on_receive(remote_base::RemoteReceiveData data) {
 | 
			
		||||
  auto midea = remote_base::MideaProtocol().decode(data);
 | 
			
		||||
  if (midea.has_value())
 | 
			
		||||
    return this->on_midea_(*midea);
 | 
			
		||||
  return coolix::CoolixClimate::on_coolix(this, data);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool MideaIR::on_midea_(const MideaData &data) {
 | 
			
		||||
  ESP_LOGV(TAG, "Decoded Midea IR data: %s", data.to_string().c_str());
 | 
			
		||||
  if (data.type() == MideaData::MIDEA_TYPE_CONTROL) {
 | 
			
		||||
    const ControlData status = data;
 | 
			
		||||
    if (status.get_mode() != climate::CLIMATE_MODE_FAN_ONLY)
 | 
			
		||||
      this->target_temperature = status.get_temp();
 | 
			
		||||
    this->mode = status.get_mode();
 | 
			
		||||
    this->fan_mode = status.get_fan_mode();
 | 
			
		||||
    if (status.get_sleep_preset())
 | 
			
		||||
      this->preset = climate::CLIMATE_PRESET_SLEEP;
 | 
			
		||||
    else if (this->preset == climate::CLIMATE_PRESET_SLEEP)
 | 
			
		||||
      this->preset = climate::CLIMATE_PRESET_NONE;
 | 
			
		||||
    this->publish_state();
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
  if (data.type() == MideaData::MIDEA_TYPE_SPECIAL) {
 | 
			
		||||
    switch (data[1]) {
 | 
			
		||||
      case SpecialData::VSWING_TOGGLE:
 | 
			
		||||
        this->swing_mode = this->swing_mode == climate::CLIMATE_SWING_VERTICAL ? climate::CLIMATE_SWING_OFF
 | 
			
		||||
                                                                               : climate::CLIMATE_SWING_VERTICAL;
 | 
			
		||||
        break;
 | 
			
		||||
      case SpecialData::TURBO_TOGGLE:
 | 
			
		||||
        this->preset = this->preset == climate::CLIMATE_PRESET_BOOST ? climate::CLIMATE_PRESET_NONE
 | 
			
		||||
                                                                     : climate::CLIMATE_PRESET_BOOST;
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
    this->publish_state();
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace midea_ir
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										47
									
								
								esphome/components/midea_ir/midea_ir.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								esphome/components/midea_ir/midea_ir.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,47 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/climate_ir/climate_ir.h"
 | 
			
		||||
#include "midea_data.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace midea_ir {
 | 
			
		||||
 | 
			
		||||
// Temperature
 | 
			
		||||
const uint8_t MIDEA_TEMPC_MIN = 17;  // Celsius
 | 
			
		||||
const uint8_t MIDEA_TEMPC_MAX = 30;  // Celsius
 | 
			
		||||
const uint8_t MIDEA_TEMPF_MIN = 62;  // Fahrenheit
 | 
			
		||||
const uint8_t MIDEA_TEMPF_MAX = 86;  // Fahrenheit
 | 
			
		||||
 | 
			
		||||
class MideaIR : public climate_ir::ClimateIR {
 | 
			
		||||
 public:
 | 
			
		||||
  MideaIR()
 | 
			
		||||
      : climate_ir::ClimateIR(
 | 
			
		||||
            MIDEA_TEMPC_MIN, MIDEA_TEMPC_MAX, 1.0f, true, true,
 | 
			
		||||
            {climate::CLIMATE_FAN_AUTO, climate::CLIMATE_FAN_LOW, climate::CLIMATE_FAN_MEDIUM,
 | 
			
		||||
             climate::CLIMATE_FAN_HIGH},
 | 
			
		||||
            {climate::CLIMATE_SWING_OFF, climate::CLIMATE_SWING_VERTICAL},
 | 
			
		||||
            {climate::CLIMATE_PRESET_NONE, climate::CLIMATE_PRESET_SLEEP, climate::CLIMATE_PRESET_BOOST}) {}
 | 
			
		||||
 | 
			
		||||
  /// Override control to change settings of the climate device.
 | 
			
		||||
  void control(const climate::ClimateCall &call) override;
 | 
			
		||||
 | 
			
		||||
  /// Set use of Fahrenheit units
 | 
			
		||||
  void set_fahrenheit(bool value) {
 | 
			
		||||
    this->fahrenheit_ = value;
 | 
			
		||||
    this->temperature_step_ = value ? 0.5f : 1.0f;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  /// Transmit via IR the state of this climate controller.
 | 
			
		||||
  void transmit_state() override;
 | 
			
		||||
  void transmit_(MideaData &data);
 | 
			
		||||
  /// Handle received IR Buffer
 | 
			
		||||
  bool on_receive(remote_base::RemoteReceiveData data) override;
 | 
			
		||||
  bool on_midea_(const MideaData &data);
 | 
			
		||||
  bool fahrenheit_{false};
 | 
			
		||||
  bool swing_{false};
 | 
			
		||||
  bool boost_{false};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace midea_ir
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -69,7 +69,7 @@ bool Modbus::parse_modbus_byte_(uint8_t byte) {
 | 
			
		||||
  uint8_t data_len = raw[2];
 | 
			
		||||
  uint8_t data_offset = 3;
 | 
			
		||||
  // the response for write command mirrors the requests and data startes at offset 2 instead of 3 for read commands
 | 
			
		||||
  if (function_code == 0x5 || function_code == 0x06 || function_code == 0x10) {
 | 
			
		||||
  if (function_code == 0x5 || function_code == 0x06 || function_code == 0xF || function_code == 0x10) {
 | 
			
		||||
    data_offset = 2;
 | 
			
		||||
    data_len = 4;
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -47,11 +47,16 @@ MODBUS_FUNCTION_CODE = {
 | 
			
		||||
 | 
			
		||||
ModbusRegisterType_ns = modbus_controller_ns.namespace("ModbusRegisterType")
 | 
			
		||||
ModbusRegisterType = ModbusRegisterType_ns.enum("ModbusRegisterType")
 | 
			
		||||
MODBUS_REGISTER_TYPE = {
 | 
			
		||||
 | 
			
		||||
MODBUS_WRITE_REGISTER_TYPE = {
 | 
			
		||||
    "custom": ModbusRegisterType.CUSTOM,
 | 
			
		||||
    "coil": ModbusRegisterType.COIL,
 | 
			
		||||
    "discrete_input": ModbusRegisterType.DISCRETE_INPUT,
 | 
			
		||||
    "holding": ModbusRegisterType.HOLDING,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
MODBUS_REGISTER_TYPE = {
 | 
			
		||||
    **MODBUS_WRITE_REGISTER_TYPE,
 | 
			
		||||
    "discrete_input": ModbusRegisterType.DISCRETE_INPUT,
 | 
			
		||||
    "read": ModbusRegisterType.READ,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -57,4 +57,4 @@ async def to_code(config):
 | 
			
		||||
 | 
			
		||||
    paren = await cg.get_variable(config[CONF_MODBUS_CONTROLLER_ID])
 | 
			
		||||
    cg.add(paren.add_sensor_item(var))
 | 
			
		||||
    await add_modbus_base_properties(var, config, ModbusBinarySensor, cg.float_, bool)
 | 
			
		||||
    await add_modbus_base_properties(var, config, ModbusBinarySensor, bool, bool)
 | 
			
		||||
 
 | 
			
		||||
@@ -31,12 +31,11 @@ class ModbusBinarySensor : public Component, public binary_sensor::BinarySensor,
 | 
			
		||||
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
 | 
			
		||||
  using transform_func_t =
 | 
			
		||||
      optional<std::function<optional<bool>(ModbusBinarySensor *, bool, const std::vector<uint8_t> &)>>;
 | 
			
		||||
  using transform_func_t = std::function<optional<bool>(ModbusBinarySensor *, bool, const std::vector<uint8_t> &)>;
 | 
			
		||||
  void set_template(transform_func_t &&f) { this->transform_func_ = f; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  transform_func_t transform_func_{nullopt};
 | 
			
		||||
  optional<transform_func_t> transform_func_{nullopt};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace modbus_controller
 | 
			
		||||
 
 | 
			
		||||
@@ -24,15 +24,22 @@ bool ModbusController::send_next_command_() {
 | 
			
		||||
  if ((last_send > this->command_throttle_) && !waiting_for_response() && !command_queue_.empty()) {
 | 
			
		||||
    auto &command = command_queue_.front();
 | 
			
		||||
 | 
			
		||||
    ESP_LOGV(TAG, "Sending next modbus command to device %d register 0x%02X count %d", this->address_,
 | 
			
		||||
             command->register_address, command->register_count);
 | 
			
		||||
    command->send();
 | 
			
		||||
    this->last_command_timestamp_ = millis();
 | 
			
		||||
    // remove from queue if no handler is defined or command was sent too often
 | 
			
		||||
    if (!command->on_data_func || command->send_countdown < 1) {
 | 
			
		||||
      ESP_LOGD(TAG, "Modbus command to device=%d register=0x%02X countdown=%d removed from queue after send",
 | 
			
		||||
               this->address_, command->register_address, command->send_countdown);
 | 
			
		||||
    // remove from queue if command was sent too often
 | 
			
		||||
    if (command->send_countdown < 1) {
 | 
			
		||||
      ESP_LOGD(
 | 
			
		||||
          TAG,
 | 
			
		||||
          "Modbus command to device=%d register=0x%02X countdown=%d no response received - removed from send queue",
 | 
			
		||||
          this->address_, command->register_address, command->send_countdown);
 | 
			
		||||
      command_queue_.pop_front();
 | 
			
		||||
    } else {
 | 
			
		||||
      ESP_LOGV(TAG, "Sending next modbus command to device %d register 0x%02X count %d", this->address_,
 | 
			
		||||
               command->register_address, command->register_count);
 | 
			
		||||
      command->send();
 | 
			
		||||
      this->last_command_timestamp_ = millis();
 | 
			
		||||
      // remove from queue if no handler is defined
 | 
			
		||||
      if (!command->on_data_func) {
 | 
			
		||||
        command_queue_.pop_front();
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return (!command_queue_.empty());
 | 
			
		||||
@@ -72,36 +79,28 @@ void ModbusController::on_modbus_error(uint8_t function_code, uint8_t exception_
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::map<uint64_t, SensorItem *>::iterator ModbusController::find_register_(ModbusRegisterType register_type,
 | 
			
		||||
                                                                            uint16_t start_address) {
 | 
			
		||||
  auto vec_it = find_if(begin(register_ranges_), end(register_ranges_), [=](RegisterRange const &r) {
 | 
			
		||||
SensorSet ModbusController::find_sensors_(ModbusRegisterType register_type, uint16_t start_address) const {
 | 
			
		||||
  auto reg_it = find_if(begin(register_ranges_), end(register_ranges_), [=](RegisterRange const &r) {
 | 
			
		||||
    return (r.start_address == start_address && r.register_type == register_type);
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  if (vec_it == register_ranges_.end()) {
 | 
			
		||||
    ESP_LOGE(TAG, "No matching range for sensor found - start_address :  0x%X", start_address);
 | 
			
		||||
  if (reg_it == register_ranges_.end()) {
 | 
			
		||||
    ESP_LOGE(TAG, "No matching range for sensor found - start_address : 0x%X", start_address);
 | 
			
		||||
  } else {
 | 
			
		||||
    auto map_it = sensormap_.find(vec_it->first_sensorkey);
 | 
			
		||||
    if (map_it == sensormap_.end()) {
 | 
			
		||||
      ESP_LOGE(TAG, "No sensor found in at start_address :  0x%X (0x%llX)", start_address, vec_it->first_sensorkey);
 | 
			
		||||
    } else {
 | 
			
		||||
      return sensormap_.find(vec_it->first_sensorkey);
 | 
			
		||||
    }
 | 
			
		||||
    return reg_it->sensors;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // not found
 | 
			
		||||
  return std::end(sensormap_);
 | 
			
		||||
  return {};
 | 
			
		||||
}
 | 
			
		||||
void ModbusController::on_register_data(ModbusRegisterType register_type, uint16_t start_address,
 | 
			
		||||
                                        const std::vector<uint8_t> &data) {
 | 
			
		||||
  ESP_LOGV(TAG, "data for register address : 0x%X : ", start_address);
 | 
			
		||||
 | 
			
		||||
  auto map_it = find_register_(register_type, start_address);
 | 
			
		||||
  // loop through all sensors with the same start address
 | 
			
		||||
  while (map_it != sensormap_.end() && map_it->second->start_address == start_address) {
 | 
			
		||||
    if (map_it->second->register_type == register_type) {
 | 
			
		||||
      map_it->second->parse_and_publish(data);
 | 
			
		||||
    }
 | 
			
		||||
    map_it++;
 | 
			
		||||
  auto sensors = find_sensors_(register_type, start_address);
 | 
			
		||||
  for (auto sensor : sensors) {
 | 
			
		||||
    sensor->parse_and_publish(data);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -110,7 +109,7 @@ void ModbusController::queue_command(const ModbusCommandItem &command) {
 | 
			
		||||
  // not very effective but the queue is never really large
 | 
			
		||||
  for (auto &item : command_queue_) {
 | 
			
		||||
    if (item->register_address == command.register_address && item->register_count == command.register_count &&
 | 
			
		||||
        item->register_type == command.register_type) {
 | 
			
		||||
        item->register_type == command.register_type && item->function_code == command.function_code) {
 | 
			
		||||
      ESP_LOGW(TAG, "Duplicate modbus command found");
 | 
			
		||||
      // update the payload of the queued command
 | 
			
		||||
      // replaces a previous command
 | 
			
		||||
@@ -127,15 +126,16 @@ void ModbusController::update_range_(RegisterRange &r) {
 | 
			
		||||
  if (r.skip_updates_counter == 0) {
 | 
			
		||||
    // if a custom command is used the user supplied custom_data is only available in the SensorItem.
 | 
			
		||||
    if (r.register_type == ModbusRegisterType::CUSTOM) {
 | 
			
		||||
      auto it = this->find_register_(r.register_type, r.start_address);
 | 
			
		||||
      if (it != sensormap_.end()) {
 | 
			
		||||
      auto sensors = this->find_sensors_(r.register_type, r.start_address);
 | 
			
		||||
      if (!sensors.empty()) {
 | 
			
		||||
        auto sensor = sensors.cbegin();
 | 
			
		||||
        auto command_item = ModbusCommandItem::create_custom_command(
 | 
			
		||||
            this, it->second->custom_data,
 | 
			
		||||
            this, (*sensor)->custom_data,
 | 
			
		||||
            [this](ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {
 | 
			
		||||
              this->on_register_data(ModbusRegisterType::CUSTOM, start_address, data);
 | 
			
		||||
            });
 | 
			
		||||
        command_item.register_address = it->second->start_address;
 | 
			
		||||
        command_item.register_count = it->second->register_count;
 | 
			
		||||
        command_item.register_address = (*sensor)->start_address;
 | 
			
		||||
        command_item.register_count = (*sensor)->register_count;
 | 
			
		||||
        command_item.function_code = ModbusFunctionCode::CUSTOM;
 | 
			
		||||
        queue_command(command_item);
 | 
			
		||||
      }
 | 
			
		||||
@@ -164,102 +164,110 @@ void ModbusController::update() {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// walk through the sensors and determine the registerranges to read
 | 
			
		||||
// walk through the sensors and determine the register ranges to read
 | 
			
		||||
size_t ModbusController::create_register_ranges_() {
 | 
			
		||||
  register_ranges_.clear();
 | 
			
		||||
  uint8_t n = 0;
 | 
			
		||||
  if (sensormap_.empty()) {
 | 
			
		||||
  if (sensorset_.empty()) {
 | 
			
		||||
    ESP_LOGW(TAG, "No sensors registered");
 | 
			
		||||
    return 0;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  auto ix = sensormap_.begin();
 | 
			
		||||
  auto prev = ix;
 | 
			
		||||
  int total_register_count = 0;
 | 
			
		||||
  uint16_t current_start_address = ix->second->start_address;
 | 
			
		||||
  uint8_t buffer_offset = ix->second->offset;
 | 
			
		||||
  uint8_t skip_updates = ix->second->skip_updates;
 | 
			
		||||
  auto first_sensorkey = ix->second->getkey();
 | 
			
		||||
  total_register_count = 0;
 | 
			
		||||
  while (ix != sensormap_.end()) {
 | 
			
		||||
    ESP_LOGV(TAG, "Register: 0x%X %d %d  0x%llx (%d) buffer_offset = %d (0x%X) skip=%u", ix->second->start_address,
 | 
			
		||||
             ix->second->register_count, ix->second->offset, ix->second->getkey(), total_register_count, buffer_offset,
 | 
			
		||||
             buffer_offset, ix->second->skip_updates);
 | 
			
		||||
    // if this is a sequential address based on number of registers and address of previous sensor
 | 
			
		||||
    // convert to an offset to the previous sensor (address 0x101 becomes address 0x100 offset 2 bytes)
 | 
			
		||||
    if (!ix->second->force_new_range && total_register_count >= 0 &&
 | 
			
		||||
        prev->second->register_type == ix->second->register_type &&
 | 
			
		||||
        prev->second->start_address + total_register_count == ix->second->start_address &&
 | 
			
		||||
        prev->second->start_address < ix->second->start_address) {
 | 
			
		||||
      ix->second->start_address = prev->second->start_address;
 | 
			
		||||
      ix->second->offset += prev->second->offset + prev->second->get_register_size();
 | 
			
		||||
  // iterator is sorted see SensorItemsComparator for details
 | 
			
		||||
  auto ix = sensorset_.begin();
 | 
			
		||||
  RegisterRange r = {};
 | 
			
		||||
  uint8_t buffer_offset = 0;
 | 
			
		||||
  SensorItem *prev = nullptr;
 | 
			
		||||
  while (ix != sensorset_.end()) {
 | 
			
		||||
    SensorItem *curr = *ix;
 | 
			
		||||
 | 
			
		||||
      // replace entry in sensormap_
 | 
			
		||||
      auto const value = ix->second;
 | 
			
		||||
      sensormap_.erase(ix);
 | 
			
		||||
      sensormap_.insert({value->getkey(), value});
 | 
			
		||||
      // move iterator back to new element
 | 
			
		||||
      ix = sensormap_.find(value->getkey());  // next(prev, 1);
 | 
			
		||||
    }
 | 
			
		||||
    if (current_start_address != ix->second->start_address ||
 | 
			
		||||
        //  ( prev->second->start_address + prev->second->offset != ix->second->start_address) ||
 | 
			
		||||
        ix->second->register_type != prev->second->register_type) {
 | 
			
		||||
      // Difference doesn't match so we have a gap
 | 
			
		||||
      if (n > 0) {
 | 
			
		||||
        RegisterRange r;
 | 
			
		||||
        r.start_address = current_start_address;
 | 
			
		||||
        r.register_count = total_register_count;
 | 
			
		||||
        if (prev->second->register_type == ModbusRegisterType::COIL ||
 | 
			
		||||
            prev->second->register_type == ModbusRegisterType::DISCRETE_INPUT) {
 | 
			
		||||
          r.register_count = prev->second->offset + 1;
 | 
			
		||||
        }
 | 
			
		||||
        r.register_type = prev->second->register_type;
 | 
			
		||||
        r.first_sensorkey = first_sensorkey;
 | 
			
		||||
        r.skip_updates = skip_updates;
 | 
			
		||||
        r.skip_updates_counter = 0;
 | 
			
		||||
        ESP_LOGV(TAG, "Add range 0x%X %d skip:%d", r.start_address, r.register_count, r.skip_updates);
 | 
			
		||||
        register_ranges_.push_back(r);
 | 
			
		||||
      }
 | 
			
		||||
      skip_updates = ix->second->skip_updates;
 | 
			
		||||
      current_start_address = ix->second->start_address;
 | 
			
		||||
      first_sensorkey = ix->second->getkey();
 | 
			
		||||
      total_register_count = ix->second->register_count;
 | 
			
		||||
      buffer_offset = ix->second->offset;
 | 
			
		||||
      n = 1;
 | 
			
		||||
    ESP_LOGV(TAG, "Register: 0x%X %d %d %d offset=%u skip=%u addr=%p", curr->start_address, curr->register_count,
 | 
			
		||||
             curr->offset, curr->get_register_size(), curr->offset, curr->skip_updates, curr);
 | 
			
		||||
 | 
			
		||||
    if (r.register_count == 0) {
 | 
			
		||||
      // this is the first register in range
 | 
			
		||||
      r.start_address = curr->start_address;
 | 
			
		||||
      r.register_count = curr->register_count;
 | 
			
		||||
      r.register_type = curr->register_type;
 | 
			
		||||
      r.sensors.insert(curr);
 | 
			
		||||
      r.skip_updates = curr->skip_updates;
 | 
			
		||||
      r.skip_updates_counter = 0;
 | 
			
		||||
      buffer_offset = curr->get_register_size();
 | 
			
		||||
 | 
			
		||||
      ESP_LOGV(TAG, "Started new range");
 | 
			
		||||
    } else {
 | 
			
		||||
      n++;
 | 
			
		||||
      if (ix->second->offset != prev->second->offset || n == 1) {
 | 
			
		||||
        total_register_count += ix->second->register_count;
 | 
			
		||||
        buffer_offset += ix->second->get_register_size();
 | 
			
		||||
      // this is not the first register in range so it might be possible
 | 
			
		||||
      // to reuse the last register or extend the current range
 | 
			
		||||
      if (!curr->force_new_range && r.register_type == curr->register_type &&
 | 
			
		||||
          curr->register_type != ModbusRegisterType::CUSTOM) {
 | 
			
		||||
        if (curr->start_address == (r.start_address + r.register_count - prev->register_count) &&
 | 
			
		||||
            curr->register_count == prev->register_count && curr->get_register_size() == prev->get_register_size()) {
 | 
			
		||||
          // this register can re-use the data from the previous register
 | 
			
		||||
 | 
			
		||||
          // remove this sensore because start_address is changed (sort-order)
 | 
			
		||||
          ix = sensorset_.erase(ix);
 | 
			
		||||
 | 
			
		||||
          curr->start_address = r.start_address;
 | 
			
		||||
          curr->offset += prev->offset;
 | 
			
		||||
 | 
			
		||||
          sensorset_.insert(curr);
 | 
			
		||||
          // move iterator backwards because it will be incremented later
 | 
			
		||||
          ix--;
 | 
			
		||||
 | 
			
		||||
          ESP_LOGV(TAG, "Re-use previous register - change to register: 0x%X %d offset=%u", curr->start_address,
 | 
			
		||||
                   curr->register_count, curr->offset);
 | 
			
		||||
        } else if (curr->start_address == (r.start_address + r.register_count)) {
 | 
			
		||||
          // this register can extend the current range
 | 
			
		||||
 | 
			
		||||
          // remove this sensore because start_address is changed (sort-order)
 | 
			
		||||
          ix = sensorset_.erase(ix);
 | 
			
		||||
 | 
			
		||||
          curr->start_address = r.start_address;
 | 
			
		||||
          curr->offset += buffer_offset;
 | 
			
		||||
          buffer_offset += curr->get_register_size();
 | 
			
		||||
          r.register_count += curr->register_count;
 | 
			
		||||
 | 
			
		||||
          sensorset_.insert(curr);
 | 
			
		||||
          // move iterator backwards because it will be incremented later
 | 
			
		||||
          ix--;
 | 
			
		||||
 | 
			
		||||
          ESP_LOGV(TAG, "Extend range - change to register: 0x%X %d offset=%u", curr->start_address,
 | 
			
		||||
                   curr->register_count, curr->offset);
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (curr->start_address == r.start_address) {
 | 
			
		||||
      // use the lowest non zero value for the whole range
 | 
			
		||||
      // Because zero is the default value for skip_updates it is excluded from getting the min value.
 | 
			
		||||
      if (ix->second->skip_updates != 0) {
 | 
			
		||||
        if (skip_updates != 0) {
 | 
			
		||||
          skip_updates = std::min(skip_updates, ix->second->skip_updates);
 | 
			
		||||
      if (curr->skip_updates != 0) {
 | 
			
		||||
        if (r.skip_updates != 0) {
 | 
			
		||||
          r.skip_updates = std::min(r.skip_updates, curr->skip_updates);
 | 
			
		||||
        } else {
 | 
			
		||||
          skip_updates = ix->second->skip_updates;
 | 
			
		||||
          r.skip_updates = curr->skip_updates;
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      // add sensor to this range
 | 
			
		||||
      r.sensors.insert(curr);
 | 
			
		||||
 | 
			
		||||
      ix++;
 | 
			
		||||
    } else {
 | 
			
		||||
      ESP_LOGV(TAG, "Add range 0x%X %d skip:%d", r.start_address, r.register_count, r.skip_updates);
 | 
			
		||||
      register_ranges_.push_back(r);
 | 
			
		||||
      r = {};
 | 
			
		||||
      buffer_offset = 0;
 | 
			
		||||
      // do not increment the iterator here because the current sensor has to be re-evaluated
 | 
			
		||||
    }
 | 
			
		||||
    prev = ix++;
 | 
			
		||||
 | 
			
		||||
    prev = curr;
 | 
			
		||||
  }
 | 
			
		||||
  // Add the last range
 | 
			
		||||
  if (n > 0) {
 | 
			
		||||
    RegisterRange r;
 | 
			
		||||
    r.start_address = current_start_address;
 | 
			
		||||
    //    r.register_count = prev->second->offset>>1 + prev->second->get_register_size();
 | 
			
		||||
    r.register_count = total_register_count;
 | 
			
		||||
    if (prev->second->register_type == ModbusRegisterType::COIL ||
 | 
			
		||||
        prev->second->register_type == ModbusRegisterType::DISCRETE_INPUT) {
 | 
			
		||||
      r.register_count = prev->second->offset + 1;
 | 
			
		||||
    }
 | 
			
		||||
    r.register_type = prev->second->register_type;
 | 
			
		||||
    r.first_sensorkey = first_sensorkey;
 | 
			
		||||
    r.skip_updates = skip_updates;
 | 
			
		||||
    r.skip_updates_counter = 0;
 | 
			
		||||
 | 
			
		||||
  if (r.register_count > 0) {
 | 
			
		||||
    // Add the last range
 | 
			
		||||
    ESP_LOGV(TAG, "Add last range 0x%X %d skip:%d", r.start_address, r.register_count, r.skip_updates);
 | 
			
		||||
    register_ranges_.push_back(r);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return register_ranges_.size();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -268,9 +276,15 @@ void ModbusController::dump_config() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Address: 0x%02X", this->address_);
 | 
			
		||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "sensormap");
 | 
			
		||||
  for (auto &it : sensormap_) {
 | 
			
		||||
    ESP_LOGCONFIG("TAG", "  Sensor 0x%llX start=0x%X count=%d size=%d", it.second->getkey(), it.second->start_address,
 | 
			
		||||
                  it.second->register_count, it.second->get_register_size());
 | 
			
		||||
  for (auto &it : sensorset_) {
 | 
			
		||||
    ESP_LOGCONFIG(TAG, " Sensor type=%zu start=0x%X offset=0x%X count=%d size=%d",
 | 
			
		||||
                  static_cast<uint8_t>(it->register_type), it->start_address, it->offset, it->register_count,
 | 
			
		||||
                  it->get_register_size());
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "ranges");
 | 
			
		||||
  for (auto &it : register_ranges_) {
 | 
			
		||||
    ESP_LOGCONFIG(TAG, "  Range type=%zu start=0x%X count=%d skip_updates=%d", static_cast<uint8_t>(it.register_type),
 | 
			
		||||
                  it.start_address, it.register_count, it.skip_updates);
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
@@ -294,11 +308,11 @@ void ModbusController::on_write_register_response(ModbusRegisterType register_ty
 | 
			
		||||
  ESP_LOGV(TAG, "Command ACK 0x%X %d ", get_data<uint16_t>(data, 0), get_data<int16_t>(data, 1));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ModbusController::dump_sensormap_() {
 | 
			
		||||
  ESP_LOGV("modbuscontroller.h", "sensormap");
 | 
			
		||||
  for (auto &it : sensormap_) {
 | 
			
		||||
    ESP_LOGV("modbuscontroller.h", "  Sensor 0x%llX start=0x%X count=%d size=%d", it.second->getkey(),
 | 
			
		||||
             it.second->start_address, it.second->register_count, it.second->get_register_size());
 | 
			
		||||
void ModbusController::dump_sensors_() {
 | 
			
		||||
  ESP_LOGV(TAG, "sensors");
 | 
			
		||||
  for (auto &it : sensorset_) {
 | 
			
		||||
    ESP_LOGV(TAG, "  Sensor start=0x%X count=%d size=%d offset=%d", it->start_address, it->register_count,
 | 
			
		||||
             it->get_register_size(), it->offset);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -6,7 +6,7 @@
 | 
			
		||||
#include "esphome/components/modbus/modbus.h"
 | 
			
		||||
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <map>
 | 
			
		||||
#include <set>
 | 
			
		||||
#include <queue>
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
@@ -37,7 +37,7 @@ enum class ModbusFunctionCode {
 | 
			
		||||
  READ_FIFO_QUEUE = 0x18,                // not implemented
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum class ModbusRegisterType : int {
 | 
			
		||||
enum class ModbusRegisterType : uint8_t {
 | 
			
		||||
  CUSTOM = 0x0,
 | 
			
		||||
  COIL = 0x01,
 | 
			
		||||
  DISCRETE_INPUT = 0x02,
 | 
			
		||||
@@ -62,15 +62,6 @@ enum class SensorValueType : uint8_t {
 | 
			
		||||
  FP32_R = 0xD
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct RegisterRange {
 | 
			
		||||
  uint16_t start_address;
 | 
			
		||||
  ModbusRegisterType register_type;
 | 
			
		||||
  uint8_t register_count;
 | 
			
		||||
  uint8_t skip_updates;  // the config value
 | 
			
		||||
  uint64_t first_sensorkey;
 | 
			
		||||
  uint8_t skip_updates_counter;  // the running value
 | 
			
		||||
} __attribute__((packed));
 | 
			
		||||
 | 
			
		||||
inline ModbusFunctionCode modbus_register_read_function(ModbusRegisterType reg_type) {
 | 
			
		||||
  switch (reg_type) {
 | 
			
		||||
    case ModbusRegisterType::COIL:
 | 
			
		||||
@@ -108,18 +99,6 @@ inline ModbusFunctionCode modbus_register_write_function(ModbusRegisterType reg_
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/** All sensors are stored in a map
 | 
			
		||||
 * to enable binary sensors for values encoded as bits in the same register the key of each sensor
 | 
			
		||||
 * the key is a 64 bit integer that combines the register properties
 | 
			
		||||
 * sensormap_ is sorted by this key. The key ensures the correct order when creating consequtive ranges
 | 
			
		||||
 * Format:  function_code (8 bit) | start address (16 bit)| offset (8bit)| bitmask (32 bit)
 | 
			
		||||
 */
 | 
			
		||||
inline uint64_t calc_key(ModbusRegisterType register_type, uint16_t start_address, uint8_t offset = 0,
 | 
			
		||||
                         uint32_t bitmask = 0) {
 | 
			
		||||
  return uint64_t((uint16_t(register_type) << 24) + (uint32_t(start_address) << 8) + (offset & 0xFF)) << 32 | bitmask;
 | 
			
		||||
}
 | 
			
		||||
inline uint16_t register_from_key(uint64_t key) { return (key >> 40) & 0xFFFF; }
 | 
			
		||||
 | 
			
		||||
inline uint8_t c_to_hex(char c) { return (c >= 'A') ? (c >= 'a') ? (c - 'a' + 10) : (c - 'A' + 10) : (c - '0'); }
 | 
			
		||||
 | 
			
		||||
/** Get a byte from a hex string
 | 
			
		||||
@@ -250,7 +229,6 @@ class SensorItem {
 | 
			
		||||
  virtual void parse_and_publish(const std::vector<uint8_t> &data) = 0;
 | 
			
		||||
 | 
			
		||||
  void set_custom_data(const std::vector<uint8_t> &data) { custom_data = data; }
 | 
			
		||||
  uint64_t getkey() const { return calc_key(register_type, start_address, offset, bitmask); }
 | 
			
		||||
  size_t virtual get_register_size() const {
 | 
			
		||||
    if (register_type == ModbusRegisterType::COIL || register_type == ModbusRegisterType::DISCRETE_INPUT)
 | 
			
		||||
      return 1;
 | 
			
		||||
@@ -271,6 +249,48 @@ class SensorItem {
 | 
			
		||||
  bool force_new_range{false};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// ModbusController::create_register_ranges_ tries to optimize register range
 | 
			
		||||
// for this the sensors must be ordered by register_type, start_address and bitmask
 | 
			
		||||
class SensorItemsComparator {
 | 
			
		||||
 public:
 | 
			
		||||
  bool operator()(const SensorItem *lhs, const SensorItem *rhs) const {
 | 
			
		||||
    // first sort according to register type
 | 
			
		||||
    if (lhs->register_type != rhs->register_type) {
 | 
			
		||||
      return lhs->register_type < rhs->register_type;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // ensure that sensor with force_new_range set are before the others
 | 
			
		||||
    if (lhs->force_new_range != rhs->force_new_range) {
 | 
			
		||||
      return lhs->force_new_range > rhs->force_new_range;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // sort by start address
 | 
			
		||||
    if (lhs->start_address != rhs->start_address) {
 | 
			
		||||
      return lhs->start_address < rhs->start_address;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // sort by offset (ensures update of sensors in ascending order)
 | 
			
		||||
    if (lhs->offset != rhs->offset) {
 | 
			
		||||
      return lhs->offset < rhs->offset;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // The pointer to the sensor is used last to ensure that
 | 
			
		||||
    // multiple sensors with the same values can be added with a stable sort order.
 | 
			
		||||
    return lhs < rhs;
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
using SensorSet = std::set<SensorItem *, SensorItemsComparator>;
 | 
			
		||||
 | 
			
		||||
struct RegisterRange {
 | 
			
		||||
  uint16_t start_address;
 | 
			
		||||
  ModbusRegisterType register_type;
 | 
			
		||||
  uint8_t register_count;
 | 
			
		||||
  uint8_t skip_updates;          // the config value
 | 
			
		||||
  SensorSet sensors;             // all sensors of this range
 | 
			
		||||
  uint8_t skip_updates_counter;  // the running value
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class ModbusCommandItem {
 | 
			
		||||
 public:
 | 
			
		||||
  static const size_t MAX_PAYLOAD_BYTES = 240;
 | 
			
		||||
@@ -382,8 +402,8 @@ class ModbusController : public PollingComponent, public modbus::ModbusDevice {
 | 
			
		||||
  /// queues a modbus command in the send queue
 | 
			
		||||
  void queue_command(const ModbusCommandItem &command);
 | 
			
		||||
  /// Registers a sensor with the controller. Called by esphomes code generator
 | 
			
		||||
  void add_sensor_item(SensorItem *item) { sensormap_[item->getkey()] = item; }
 | 
			
		||||
  /// called when a modbus response was prased without errors
 | 
			
		||||
  void add_sensor_item(SensorItem *item) { sensorset_.insert(item); }
 | 
			
		||||
  /// called when a modbus response was parsed without errors
 | 
			
		||||
  void on_modbus_data(const std::vector<uint8_t> &data) override;
 | 
			
		||||
  /// called when a modbus error response was received
 | 
			
		||||
  void on_modbus_error(uint8_t function_code, uint8_t exception_code) override;
 | 
			
		||||
@@ -400,7 +420,7 @@ class ModbusController : public PollingComponent, public modbus::ModbusDevice {
 | 
			
		||||
  /// parse sensormap_ and create range of sequential addresses
 | 
			
		||||
  size_t create_register_ranges_();
 | 
			
		||||
  // find register in sensormap. Returns iterator with all registers having the same start address
 | 
			
		||||
  std::map<uint64_t, SensorItem *>::iterator find_register_(ModbusRegisterType register_type, uint16_t start_address);
 | 
			
		||||
  SensorSet find_sensors_(ModbusRegisterType register_type, uint16_t start_address) const;
 | 
			
		||||
  /// submit the read command for the address range to the send queue
 | 
			
		||||
  void update_range_(RegisterRange &r);
 | 
			
		||||
  /// parse incoming modbus data
 | 
			
		||||
@@ -410,10 +430,9 @@ class ModbusController : public PollingComponent, public modbus::ModbusDevice {
 | 
			
		||||
  /// get the number of queued modbus commands (should be mostly empty)
 | 
			
		||||
  size_t get_command_queue_length_() { return command_queue_.size(); }
 | 
			
		||||
  /// dump the parsed sensormap for diagnostics
 | 
			
		||||
  void dump_sensormap_();
 | 
			
		||||
  void dump_sensors_();
 | 
			
		||||
  /// Collection of all sensors for this component
 | 
			
		||||
  /// see calc_key how the key is contructed
 | 
			
		||||
  std::map<uint64_t, SensorItem *> sensormap_;
 | 
			
		||||
  SensorSet sensorset_;
 | 
			
		||||
  /// Continous range of modbus registers
 | 
			
		||||
  std::vector<RegisterRange> register_ranges_;
 | 
			
		||||
  /// Hold the pending requests to be sent
 | 
			
		||||
 
 | 
			
		||||
Some files were not shown because too many files have changed in this diff Show More
		Reference in New Issue
	
	Block a user