mirror of
				https://github.com/esphome/esphome.git
				synced 2025-11-04 00:51:49 +00:00 
			
		
		
		
	Compare commits
	
		
			163 Commits
		
	
	
		
			2022.5.0b2
			...
			2022.6.3
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					ab86ddcf02 | ||
| 
						 | 
					bf8eddb13b | ||
| 
						 | 
					e5eaf7a3fe | ||
| 
						 | 
					50f32a3aa5 | ||
| 
						 | 
					eb878710c1 | ||
| 
						 | 
					311980e0e4 | ||
| 
						 | 
					df73170e5a | ||
| 
						 | 
					ccc13cc9e1 | ||
| 
						 | 
					020b2c05c8 | ||
| 
						 | 
					4c37c17df1 | ||
| 
						 | 
					8f67acadd8 | ||
| 
						 | 
					ca13c4c1a6 | ||
| 
						 | 
					d0c646c721 | ||
| 
						 | 
					8a055675af | ||
| 
						 | 
					f329c74a15 | ||
| 
						 | 
					7c86f3fa9e | ||
| 
						 | 
					203b8b01bf | ||
| 
						 | 
					8a1034a92f | ||
| 
						 | 
					aa0c2dedd9 | ||
| 
						 | 
					8c9948bb56 | ||
| 
						 | 
					2d1abaa68e | ||
| 
						 | 
					664a3df0b4 | ||
| 
						 | 
					9ff893881c | ||
| 
						 | 
					94f6c6861a | ||
| 
						 | 
					b1d614e6c4 | ||
| 
						 | 
					7fceb070e5 | ||
| 
						 | 
					5c7c0834c0 | ||
| 
						 | 
					f3a25de11d | ||
| 
						 | 
					041bef8bcd | ||
| 
						 | 
					6e83790308 | ||
| 
						 | 
					d2d4eb4eae | ||
| 
						 | 
					5942a3898c | ||
| 
						 | 
					93421f0fa7 | ||
| 
						 | 
					6cb5cd48c2 | ||
| 
						 | 
					746fd1122f | ||
| 
						 | 
					9663760ec5 | ||
| 
						 | 
					a3d73d1e23 | ||
| 
						 | 
					d63e14a4b6 | ||
| 
						 | 
					03944e6cd8 | ||
| 
						 | 
					0d1028be2e | ||
| 
						 | 
					6a85259e4d | ||
| 
						 | 
					ebca936b7e | ||
| 
						 | 
					31c4551890 | ||
| 
						 | 
					dd470d4197 | ||
| 
						 | 
					612822490b | ||
| 
						 | 
					f8969605e8 | ||
| 
						 | 
					dd24ffa24e | ||
| 
						 | 
					d0dda48932 | ||
| 
						 | 
					6349b5f654 | ||
| 
						 | 
					a6ff02a3cf | ||
| 
						 | 
					4f57bf786b | ||
| 
						 | 
					6221f6d47d | ||
| 
						 | 
					a922efeafa | ||
| 
						 | 
					5aa42e5e66 | ||
| 
						 | 
					708672ec7e | ||
| 
						 | 
					d2cefbf224 | ||
| 
						 | 
					adb7aa6950 | ||
| 
						 | 
					77f322166e | ||
| 
						 | 
					f3f6e54818 | ||
| 
						 | 
					fb0fec1f25 | ||
| 
						 | 
					b66af9fb4d | ||
| 
						 | 
					6617d576a7 | ||
| 
						 | 
					cd35ead890 | ||
| 
						 | 
					9dc804ee27 | ||
| 
						 | 
					a8ceeaa7b0 | ||
| 
						 | 
					7092f7663e | ||
| 
						 | 
					d9d2edeb08 | ||
| 
						 | 
					dda1ddcb26 | ||
| 
						 | 
					f0c890f160 | ||
| 
						 | 
					4f52d43347 | ||
| 
						 | 
					0ed7db979b | ||
| 
						 | 
					9c78049359 | ||
| 
						 | 
					7882105661 | ||
| 
						 | 
					c000e1d6dd | ||
| 
						 | 
					420dacb22d | ||
| 
						 | 
					ae2f6ad4d1 | ||
| 
						 | 
					2c28d79bf8 | ||
| 
						 | 
					c5069edc78 | ||
| 
						 | 
					282d9e138c | ||
| 
						 | 
					72fcf2cbe1 | ||
| 
						 | 
					6f49f5465b | ||
| 
						 | 
					17b8bd8316 | ||
| 
						 | 
					9b6b9c1fa2 | ||
| 
						 | 
					609a2ca592 | ||
| 
						 | 
					6dabf24bf3 | ||
| 
						 | 
					7e88938932 | ||
| 
						 | 
					c707e64685 | ||
| 
						 | 
					a639690716 | ||
| 
						 | 
					01222dbab7 | ||
| 
						 | 
					93e2506279 | ||
| 
						 | 
					f62d5d3b9d | ||
| 
						 | 
					0665acd190 | ||
| 
						 | 
					fea05e9d33 | ||
| 
						 | 
					7a03c7d56f | ||
| 
						 | 
					2dc2aec954 | ||
| 
						 | 
					39c6c2417a | ||
| 
						 | 
					03d5a0ec1d | ||
| 
						 | 
					1c873e0034 | ||
| 
						 | 
					bcb47c306c | ||
| 
						 | 
					01c4d3c225 | ||
| 
						 | 
					c2aaae4818 | ||
| 
						 | 
					3f678e218d | ||
| 
						 | 
					f8a1bd4e79 | ||
| 
						 | 
					993044c870 | ||
| 
						 | 
					a8c1b63edb | ||
| 
						 | 
					db7d946e1b | ||
| 
						 | 
					fc7348d46d | ||
| 
						 | 
					8be2456c7e | ||
| 
						 | 
					bb5f7249a6 | ||
| 
						 | 
					fc94a5d0ee | ||
| 
						 | 
					24029cc918 | ||
| 
						 | 
					9a9d5964ee | ||
| 
						 | 
					4e4a512107 | ||
| 
						 | 
					0729ed538e | ||
| 
						 | 
					24b75b7ed6 | ||
| 
						 | 
					ec3618ecb8 | ||
| 
						 | 
					792a24f38d | ||
| 
						 | 
					652e8a015b | ||
| 
						 | 
					1ef6fd8fb0 | ||
| 
						 | 
					942b0de7fd | ||
| 
						 | 
					859cca49d1 | ||
| 
						 | 
					8f7ff25624 | ||
| 
						 | 
					97aca8e54c | ||
| 
						 | 
					95acf19067 | ||
| 
						 | 
					3d0899aa58 | ||
| 
						 | 
					138d6e505b | ||
| 
						 | 
					2748e6ba29 | ||
| 
						 | 
					dbd4e927d8 | ||
| 
						 | 
					e73d47918f | ||
| 
						 | 
					b881bc071e | ||
| 
						 | 
					1d0395d1c7 | ||
| 
						 | 
					616c787e37 | ||
| 
						 | 
					0c4de2bc97 | ||
| 
						 | 
					c2f5ac9eba | ||
| 
						 | 
					5764c988af | ||
| 
						 | 
					ccc2fbfd67 | ||
| 
						 | 
					10b4adb8e6 | ||
| 
						 | 
					83b7181bcb | ||
| 
						 | 
					8886b7e141 | ||
| 
						 | 
					7dcc4d030b | ||
| 
						 | 
					b9398897c1 | ||
| 
						 | 
					657b1c60ae | ||
| 
						 | 
					dc54b17778 | ||
| 
						 | 
					1fb214165b | ||
| 
						 | 
					81b2fd78f5 | ||
| 
						 | 
					69002fb1e6 | ||
| 
						 | 
					75332a752d | ||
| 
						 | 
					09ed1aed93 | ||
| 
						 | 
					53d3718028 | ||
| 
						 | 
					2b5dce5232 | ||
| 
						 | 
					9ad84150aa | ||
| 
						 | 
					c0523590b4 | ||
| 
						 | 
					c7f091ab10 | ||
| 
						 | 
					7479e0aada | ||
| 
						 | 
					5bbee1a1fe | ||
| 
						 | 
					bdb9546ca3 | ||
| 
						 | 
					46af4cad6e | ||
| 
						 | 
					76a238912b | ||
| 
						 | 
					909a526967 | ||
| 
						 | 
					cd6f4fb93f | ||
| 
						 | 
					c19458696e | ||
| 
						 | 
					318b930e9f | ||
| 
						 | 
					9296a078a7 | 
@@ -88,6 +88,7 @@ esphome/components/honeywellabp/* @RubyBailey
 | 
			
		||||
esphome/components/hrxl_maxsonar_wr/* @netmikey
 | 
			
		||||
esphome/components/hydreon_rgxx/* @functionpointer
 | 
			
		||||
esphome/components/i2c/* @esphome/core
 | 
			
		||||
esphome/components/i2s_audio/* @jesserockz
 | 
			
		||||
esphome/components/improv_serial/* @esphome/core
 | 
			
		||||
esphome/components/ina260/* @MrEditor97
 | 
			
		||||
esphome/components/inkbird_ibsth1_mini/* @fkirill
 | 
			
		||||
@@ -102,6 +103,7 @@ esphome/components/lilygo_t5_47/touchscreen/* @jesserockz
 | 
			
		||||
esphome/components/lock/* @esphome/core
 | 
			
		||||
esphome/components/logger/* @esphome/core
 | 
			
		||||
esphome/components/ltr390/* @sjtrny
 | 
			
		||||
esphome/components/max31865/* @DAVe3283
 | 
			
		||||
esphome/components/max44009/* @berfenger
 | 
			
		||||
esphome/components/max7219digit/* @rspaargaren
 | 
			
		||||
esphome/components/max9611/* @mckaymatthew
 | 
			
		||||
@@ -119,6 +121,7 @@ esphome/components/mcp47a1/* @jesserockz
 | 
			
		||||
esphome/components/mcp9808/* @k7hpn
 | 
			
		||||
esphome/components/md5/* @esphome/core
 | 
			
		||||
esphome/components/mdns/* @esphome/core
 | 
			
		||||
esphome/components/media_player/* @jesserockz
 | 
			
		||||
esphome/components/midea/* @dudanov
 | 
			
		||||
esphome/components/midea_ir/* @dudanov
 | 
			
		||||
esphome/components/mitsubishi/* @RubyBailey
 | 
			
		||||
@@ -178,6 +181,7 @@ esphome/components/sen5x/* @martgras
 | 
			
		||||
esphome/components/sensirion_common/* @martgras
 | 
			
		||||
esphome/components/sensor/* @esphome/core
 | 
			
		||||
esphome/components/sgp40/* @SenexCrenshaw
 | 
			
		||||
esphome/components/sgp4x/* @SenexCrenshaw @martgras
 | 
			
		||||
esphome/components/shelly_dimmer/* @edge90 @rnauber
 | 
			
		||||
esphome/components/sht4x/* @sjtrny
 | 
			
		||||
esphome/components/shutdown/* @esphome/core @jsuanet
 | 
			
		||||
@@ -222,6 +226,7 @@ esphome/components/tsl2591/* @wjcarpenter
 | 
			
		||||
esphome/components/tuya/binary_sensor/* @jesserockz
 | 
			
		||||
esphome/components/tuya/climate/* @jesserockz
 | 
			
		||||
esphome/components/tuya/number/* @frankiboy1
 | 
			
		||||
esphome/components/tuya/select/* @bearpawmaxim
 | 
			
		||||
esphome/components/tuya/sensor/* @jesserockz
 | 
			
		||||
esphome/components/tuya/switch/* @jesserockz
 | 
			
		||||
esphome/components/tuya/text_sensor/* @dentra
 | 
			
		||||
 
 | 
			
		||||
@@ -121,7 +121,11 @@ void IRAM_ATTR HOT AcDimmerDataStore::gpio_intr() {
 | 
			
		||||
      // calculate time until enable in µs: (1.0-value)*cycle_time, but with integer arithmetic
 | 
			
		||||
      // also take into account min_power
 | 
			
		||||
      auto min_us = this->cycle_time_us * this->min_power / 1000;
 | 
			
		||||
      this->enable_time_us = std::max((uint32_t) 1, ((65535 - this->value) * (this->cycle_time_us - min_us)) / 65535);
 | 
			
		||||
      // calculate required value to provide a true RMS voltage output
 | 
			
		||||
      this->enable_time_us =
 | 
			
		||||
          std::max((uint32_t) 1, (uint32_t)((65535 - (acos(1 - (2 * this->value / 65535.0)) / 3.14159 * 65535)) *
 | 
			
		||||
                                            (this->cycle_time_us - min_us)) /
 | 
			
		||||
                                     65535);
 | 
			
		||||
      if (this->method == DIM_METHOD_LEADING_PULSE) {
 | 
			
		||||
        // Minimum pulse time should be enough for the triac to trigger when it is close to the ZC zone
 | 
			
		||||
        // this is for brightness near 99%
 | 
			
		||||
 
 | 
			
		||||
@@ -15,10 +15,21 @@ namespace esphome {
 | 
			
		||||
namespace adc {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "adc";
 | 
			
		||||
// 13 bits for S3 / 12 bit for all other esp32 variants
 | 
			
		||||
// create a const to avoid the repated cast to enum
 | 
			
		||||
 | 
			
		||||
// 13bit for S2, and 12bit for all other esp32 variants
 | 
			
		||||
#ifdef USE_ESP32
 | 
			
		||||
static const adc_bits_width_t ADC_WIDTH_MAX_SOC_BITS = static_cast<adc_bits_width_t>(ADC_WIDTH_MAX - 1);
 | 
			
		||||
 | 
			
		||||
#ifndef SOC_ADC_RTC_MAX_BITWIDTH
 | 
			
		||||
#if USE_ESP32_VARIANT_ESP32S2
 | 
			
		||||
static const int SOC_ADC_RTC_MAX_BITWIDTH = 13;
 | 
			
		||||
#else
 | 
			
		||||
static const int SOC_ADC_RTC_MAX_BITWIDTH = 12;
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
static const int ADC_MAX = (1 << SOC_ADC_RTC_MAX_BITWIDTH) - 1;    // 4095 (12 bit) or 8191 (13 bit)
 | 
			
		||||
static const int ADC_HALF = (1 << SOC_ADC_RTC_MAX_BITWIDTH) >> 1;  // 2048 (12 bit) or 4096 (13 bit)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
void ADCSensor::setup() {
 | 
			
		||||
@@ -75,16 +86,16 @@ void ADCSensor::dump_config() {
 | 
			
		||||
  } else {
 | 
			
		||||
    switch (this->attenuation_) {
 | 
			
		||||
      case ADC_ATTEN_DB_0:
 | 
			
		||||
        ESP_LOGCONFIG(TAG, " Attenuation: 0db (max 1.1V)");
 | 
			
		||||
        ESP_LOGCONFIG(TAG, " Attenuation: 0db");
 | 
			
		||||
        break;
 | 
			
		||||
      case ADC_ATTEN_DB_2_5:
 | 
			
		||||
        ESP_LOGCONFIG(TAG, " Attenuation: 2.5db (max 1.5V)");
 | 
			
		||||
        ESP_LOGCONFIG(TAG, " Attenuation: 2.5db");
 | 
			
		||||
        break;
 | 
			
		||||
      case ADC_ATTEN_DB_6:
 | 
			
		||||
        ESP_LOGCONFIG(TAG, " Attenuation: 6db (max 2.2V)");
 | 
			
		||||
        ESP_LOGCONFIG(TAG, " Attenuation: 6db");
 | 
			
		||||
        break;
 | 
			
		||||
      case ADC_ATTEN_DB_11:
 | 
			
		||||
        ESP_LOGCONFIG(TAG, " Attenuation: 11db (max 3.9V)");
 | 
			
		||||
        ESP_LOGCONFIG(TAG, " Attenuation: 11db");
 | 
			
		||||
        break;
 | 
			
		||||
      default:  // This is to satisfy the unused ADC_ATTEN_MAX
 | 
			
		||||
        break;
 | 
			
		||||
@@ -129,16 +140,16 @@ float ADCSensor::sample() {
 | 
			
		||||
    return mv / 1000.0f;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  int raw11, raw6 = 4095, raw2 = 4095, raw0 = 4095;
 | 
			
		||||
  int raw11, raw6 = ADC_MAX, raw2 = ADC_MAX, raw0 = ADC_MAX;
 | 
			
		||||
  adc1_config_channel_atten(channel_, ADC_ATTEN_DB_11);
 | 
			
		||||
  raw11 = adc1_get_raw(channel_);
 | 
			
		||||
  if (raw11 < 4095) {
 | 
			
		||||
  if (raw11 < ADC_MAX) {
 | 
			
		||||
    adc1_config_channel_atten(channel_, ADC_ATTEN_DB_6);
 | 
			
		||||
    raw6 = adc1_get_raw(channel_);
 | 
			
		||||
    if (raw6 < 4095) {
 | 
			
		||||
    if (raw6 < ADC_MAX) {
 | 
			
		||||
      adc1_config_channel_atten(channel_, ADC_ATTEN_DB_2_5);
 | 
			
		||||
      raw2 = adc1_get_raw(channel_);
 | 
			
		||||
      if (raw2 < 4095) {
 | 
			
		||||
      if (raw2 < ADC_MAX) {
 | 
			
		||||
        adc1_config_channel_atten(channel_, ADC_ATTEN_DB_0);
 | 
			
		||||
        raw0 = adc1_get_raw(channel_);
 | 
			
		||||
      }
 | 
			
		||||
@@ -154,15 +165,15 @@ float ADCSensor::sample() {
 | 
			
		||||
  uint32_t mv2 = esp_adc_cal_raw_to_voltage(raw2, &cal_characteristics_[(int) ADC_ATTEN_DB_2_5]);
 | 
			
		||||
  uint32_t mv0 = esp_adc_cal_raw_to_voltage(raw0, &cal_characteristics_[(int) ADC_ATTEN_DB_0]);
 | 
			
		||||
 | 
			
		||||
  // Contribution of each value, in range 0-2048
 | 
			
		||||
  uint32_t c11 = std::min(raw11, 2048);
 | 
			
		||||
  uint32_t c6 = 2048 - std::abs(raw6 - 2048);
 | 
			
		||||
  uint32_t c2 = 2048 - std::abs(raw2 - 2048);
 | 
			
		||||
  uint32_t c0 = std::min(4095 - raw0, 2048);
 | 
			
		||||
  // max theoretical csum value is 2048*4 = 8192
 | 
			
		||||
  // Contribution of each value, in range 0-2048 (12 bit ADC) or 0-4096 (13 bit ADC)
 | 
			
		||||
  uint32_t c11 = std::min(raw11, ADC_HALF);
 | 
			
		||||
  uint32_t c6 = ADC_HALF - std::abs(raw6 - ADC_HALF);
 | 
			
		||||
  uint32_t c2 = ADC_HALF - std::abs(raw2 - ADC_HALF);
 | 
			
		||||
  uint32_t c0 = std::min(ADC_MAX - raw0, ADC_HALF);
 | 
			
		||||
  // max theoretical csum value is 4096*4 = 16384
 | 
			
		||||
  uint32_t csum = c11 + c6 + c2 + c0;
 | 
			
		||||
 | 
			
		||||
  // each mv is max 3900; so max value is 3900*2048*4, fits in unsigned
 | 
			
		||||
  // each mv is max 3900; so max value is 3900*4096*4, fits in unsigned32
 | 
			
		||||
  uint32_t mv_scaled = (mv11 * c11) + (mv6 * c6) + (mv2 * c2) + (mv0 * c0);
 | 
			
		||||
  return mv_scaled / (float) (csum * 1000U);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -40,6 +40,8 @@ class AddressableLightDisplay : public display::DisplayBuffer, public PollingCom
 | 
			
		||||
  void setup() override;
 | 
			
		||||
  void display();
 | 
			
		||||
 | 
			
		||||
  display::DisplayType get_display_type() override { return display::DisplayType::DISPLAY_TYPE_COLOR; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  int get_width_internal() override;
 | 
			
		||||
  int get_height_internal() override;
 | 
			
		||||
 
 | 
			
		||||
@@ -42,6 +42,7 @@ service APIConnection {
 | 
			
		||||
  rpc select_command (SelectCommandRequest) returns (void) {}
 | 
			
		||||
  rpc button_command (ButtonCommandRequest) returns (void) {}
 | 
			
		||||
  rpc lock_command (LockCommandRequest) returns (void) {}
 | 
			
		||||
  rpc media_player_command (MediaPlayerCommandRequest) returns (void) {}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -991,7 +992,7 @@ message ListEntitiesLockResponse {
 | 
			
		||||
  bool supports_open = 9;
 | 
			
		||||
  bool requires_code = 10;
 | 
			
		||||
 | 
			
		||||
  # Not yet implemented:
 | 
			
		||||
  // Not yet implemented:
 | 
			
		||||
  string code_format = 11;
 | 
			
		||||
}
 | 
			
		||||
message LockStateResponse {
 | 
			
		||||
@@ -1010,7 +1011,7 @@ message LockCommandRequest {
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
  LockCommand command = 2;
 | 
			
		||||
 | 
			
		||||
  # Not yet implemented:
 | 
			
		||||
  // Not yet implemented:
 | 
			
		||||
  bool has_code = 3;
 | 
			
		||||
  string code = 4;
 | 
			
		||||
}
 | 
			
		||||
@@ -1040,3 +1041,60 @@ message ButtonCommandRequest {
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ==================== MEDIA PLAYER ====================
 | 
			
		||||
enum MediaPlayerState {
 | 
			
		||||
  MEDIA_PLAYER_STATE_NONE = 0;
 | 
			
		||||
  MEDIA_PLAYER_STATE_IDLE = 1;
 | 
			
		||||
  MEDIA_PLAYER_STATE_PLAYING = 2;
 | 
			
		||||
  MEDIA_PLAYER_STATE_PAUSED = 3;
 | 
			
		||||
}
 | 
			
		||||
enum MediaPlayerCommand {
 | 
			
		||||
  MEDIA_PLAYER_COMMAND_PLAY = 0;
 | 
			
		||||
  MEDIA_PLAYER_COMMAND_PAUSE = 1;
 | 
			
		||||
  MEDIA_PLAYER_COMMAND_STOP = 2;
 | 
			
		||||
  MEDIA_PLAYER_COMMAND_MUTE = 3;
 | 
			
		||||
  MEDIA_PLAYER_COMMAND_UNMUTE = 4;
 | 
			
		||||
}
 | 
			
		||||
message ListEntitiesMediaPlayerResponse {
 | 
			
		||||
  option (id) = 63;
 | 
			
		||||
  option (source) = SOURCE_SERVER;
 | 
			
		||||
  option (ifdef) = "USE_MEDIA_PLAYER";
 | 
			
		||||
 | 
			
		||||
  string object_id = 1;
 | 
			
		||||
  fixed32 key = 2;
 | 
			
		||||
  string name = 3;
 | 
			
		||||
  string unique_id = 4;
 | 
			
		||||
 | 
			
		||||
  string icon = 5;
 | 
			
		||||
  bool disabled_by_default = 6;
 | 
			
		||||
  EntityCategory entity_category = 7;
 | 
			
		||||
 | 
			
		||||
  bool supports_pause = 8;
 | 
			
		||||
}
 | 
			
		||||
message MediaPlayerStateResponse {
 | 
			
		||||
  option (id) = 64;
 | 
			
		||||
  option (source) = SOURCE_SERVER;
 | 
			
		||||
  option (ifdef) = "USE_MEDIA_PLAYER";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
  MediaPlayerState state = 2;
 | 
			
		||||
  float volume = 3;
 | 
			
		||||
  bool muted = 4;
 | 
			
		||||
}
 | 
			
		||||
message MediaPlayerCommandRequest {
 | 
			
		||||
  option (id) = 65;
 | 
			
		||||
  option (source) = SOURCE_CLIENT;
 | 
			
		||||
  option (ifdef) = "USE_MEDIA_PLAYER";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
 | 
			
		||||
  fixed32 key = 1;
 | 
			
		||||
 | 
			
		||||
  bool has_command = 2;
 | 
			
		||||
  MediaPlayerCommand command = 3;
 | 
			
		||||
 | 
			
		||||
  bool has_volume = 4;
 | 
			
		||||
  float volume = 5;
 | 
			
		||||
 | 
			
		||||
  bool has_media_url = 6;
 | 
			
		||||
  string media_url = 7;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -12,9 +12,6 @@
 | 
			
		||||
#ifdef USE_HOMEASSISTANT_TIME
 | 
			
		||||
#include "esphome/components/homeassistant/time/homeassistant_time.h"
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_FAN
 | 
			
		||||
#include "esphome/components/fan/fan_helpers.h"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace api {
 | 
			
		||||
@@ -253,9 +250,6 @@ void APIConnection::cover_command(const CoverCommandRequest &msg) {
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef USE_FAN
 | 
			
		||||
// Shut-up about usage of deprecated speed_level_to_enum/speed_enum_to_level functions for a bit.
 | 
			
		||||
#pragma GCC diagnostic push
 | 
			
		||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
 | 
			
		||||
bool APIConnection::send_fan_state(fan::Fan *fan) {
 | 
			
		||||
  if (!this->state_subscription_)
 | 
			
		||||
    return false;
 | 
			
		||||
@@ -268,7 +262,6 @@ bool APIConnection::send_fan_state(fan::Fan *fan) {
 | 
			
		||||
    resp.oscillating = fan->oscillating;
 | 
			
		||||
  if (traits.supports_speed()) {
 | 
			
		||||
    resp.speed_level = fan->speed;
 | 
			
		||||
    resp.speed = static_cast<enums::FanSpeed>(fan::speed_level_to_enum(fan->speed, traits.supported_speed_count()));
 | 
			
		||||
  }
 | 
			
		||||
  if (traits.supports_direction())
 | 
			
		||||
    resp.direction = static_cast<enums::FanDirection>(fan->direction);
 | 
			
		||||
@@ -295,8 +288,6 @@ void APIConnection::fan_command(const FanCommandRequest &msg) {
 | 
			
		||||
  if (fan == nullptr)
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  auto traits = fan->get_traits();
 | 
			
		||||
 | 
			
		||||
  auto call = fan->make_call();
 | 
			
		||||
  if (msg.has_state)
 | 
			
		||||
    call.set_state(msg.state);
 | 
			
		||||
@@ -305,14 +296,11 @@ void APIConnection::fan_command(const FanCommandRequest &msg) {
 | 
			
		||||
  if (msg.has_speed_level) {
 | 
			
		||||
    // Prefer level
 | 
			
		||||
    call.set_speed(msg.speed_level);
 | 
			
		||||
  } else if (msg.has_speed) {
 | 
			
		||||
    call.set_speed(fan::speed_enum_to_level(static_cast<fan::FanSpeed>(msg.speed), traits.supported_speed_count()));
 | 
			
		||||
  }
 | 
			
		||||
  if (msg.has_direction)
 | 
			
		||||
    call.set_direction(static_cast<fan::FanDirection>(msg.direction));
 | 
			
		||||
  call.perform();
 | 
			
		||||
}
 | 
			
		||||
#pragma GCC diagnostic pop
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef USE_LIGHT
 | 
			
		||||
@@ -745,6 +733,52 @@ void APIConnection::lock_command(const LockCommandRequest &msg) {
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef USE_MEDIA_PLAYER
 | 
			
		||||
bool APIConnection::send_media_player_state(media_player::MediaPlayer *media_player) {
 | 
			
		||||
  if (!this->state_subscription_)
 | 
			
		||||
    return false;
 | 
			
		||||
 | 
			
		||||
  MediaPlayerStateResponse resp{};
 | 
			
		||||
  resp.key = media_player->get_object_id_hash();
 | 
			
		||||
  resp.state = static_cast<enums::MediaPlayerState>(media_player->state);
 | 
			
		||||
  resp.volume = media_player->volume;
 | 
			
		||||
  resp.muted = media_player->is_muted();
 | 
			
		||||
  return this->send_media_player_state_response(resp);
 | 
			
		||||
}
 | 
			
		||||
bool APIConnection::send_media_player_info(media_player::MediaPlayer *media_player) {
 | 
			
		||||
  ListEntitiesMediaPlayerResponse msg;
 | 
			
		||||
  msg.key = media_player->get_object_id_hash();
 | 
			
		||||
  msg.object_id = media_player->get_object_id();
 | 
			
		||||
  msg.name = media_player->get_name();
 | 
			
		||||
  msg.unique_id = get_default_unique_id("media_player", media_player);
 | 
			
		||||
  msg.icon = media_player->get_icon();
 | 
			
		||||
  msg.disabled_by_default = media_player->is_disabled_by_default();
 | 
			
		||||
  msg.entity_category = static_cast<enums::EntityCategory>(media_player->get_entity_category());
 | 
			
		||||
 | 
			
		||||
  auto traits = media_player->get_traits();
 | 
			
		||||
  msg.supports_pause = traits.get_supports_pause();
 | 
			
		||||
 | 
			
		||||
  return this->send_list_entities_media_player_response(msg);
 | 
			
		||||
}
 | 
			
		||||
void APIConnection::media_player_command(const MediaPlayerCommandRequest &msg) {
 | 
			
		||||
  media_player::MediaPlayer *media_player = App.get_media_player_by_key(msg.key);
 | 
			
		||||
  if (media_player == nullptr)
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  auto call = media_player->make_call();
 | 
			
		||||
  if (msg.has_command) {
 | 
			
		||||
    call.set_command(static_cast<media_player::MediaPlayerCommand>(msg.command));
 | 
			
		||||
  }
 | 
			
		||||
  if (msg.has_volume) {
 | 
			
		||||
    call.set_volume(msg.volume);
 | 
			
		||||
  }
 | 
			
		||||
  if (msg.has_media_url) {
 | 
			
		||||
    call.set_media_url(msg.media_url);
 | 
			
		||||
  }
 | 
			
		||||
  call.perform();
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32_CAMERA
 | 
			
		||||
void APIConnection::send_camera_state(std::shared_ptr<esp32_camera::CameraImage> image) {
 | 
			
		||||
  if (!this->state_subscription_)
 | 
			
		||||
 
 | 
			
		||||
@@ -82,6 +82,11 @@ class APIConnection : public APIServerConnection {
 | 
			
		||||
  bool send_lock_state(lock::Lock *a_lock, lock::LockState state);
 | 
			
		||||
  bool send_lock_info(lock::Lock *a_lock);
 | 
			
		||||
  void lock_command(const LockCommandRequest &msg) override;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_MEDIA_PLAYER
 | 
			
		||||
  bool send_media_player_state(media_player::MediaPlayer *media_player);
 | 
			
		||||
  bool send_media_player_info(media_player::MediaPlayer *media_player);
 | 
			
		||||
  void media_player_command(const MediaPlayerCommandRequest &msg) override;
 | 
			
		||||
#endif
 | 
			
		||||
  bool send_log_message(int level, const char *tag, const char *line);
 | 
			
		||||
  void send_homeassistant_service_call(const HomeassistantServiceResponse &call) {
 | 
			
		||||
 
 | 
			
		||||
@@ -308,6 +308,36 @@ template<> const char *proto_enum_to_string<enums::LockCommand>(enums::LockComma
 | 
			
		||||
      return "UNKNOWN";
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
template<> const char *proto_enum_to_string<enums::MediaPlayerState>(enums::MediaPlayerState value) {
 | 
			
		||||
  switch (value) {
 | 
			
		||||
    case enums::MEDIA_PLAYER_STATE_NONE:
 | 
			
		||||
      return "MEDIA_PLAYER_STATE_NONE";
 | 
			
		||||
    case enums::MEDIA_PLAYER_STATE_IDLE:
 | 
			
		||||
      return "MEDIA_PLAYER_STATE_IDLE";
 | 
			
		||||
    case enums::MEDIA_PLAYER_STATE_PLAYING:
 | 
			
		||||
      return "MEDIA_PLAYER_STATE_PLAYING";
 | 
			
		||||
    case enums::MEDIA_PLAYER_STATE_PAUSED:
 | 
			
		||||
      return "MEDIA_PLAYER_STATE_PAUSED";
 | 
			
		||||
    default:
 | 
			
		||||
      return "UNKNOWN";
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
template<> const char *proto_enum_to_string<enums::MediaPlayerCommand>(enums::MediaPlayerCommand value) {
 | 
			
		||||
  switch (value) {
 | 
			
		||||
    case enums::MEDIA_PLAYER_COMMAND_PLAY:
 | 
			
		||||
      return "MEDIA_PLAYER_COMMAND_PLAY";
 | 
			
		||||
    case enums::MEDIA_PLAYER_COMMAND_PAUSE:
 | 
			
		||||
      return "MEDIA_PLAYER_COMMAND_PAUSE";
 | 
			
		||||
    case enums::MEDIA_PLAYER_COMMAND_STOP:
 | 
			
		||||
      return "MEDIA_PLAYER_COMMAND_STOP";
 | 
			
		||||
    case enums::MEDIA_PLAYER_COMMAND_MUTE:
 | 
			
		||||
      return "MEDIA_PLAYER_COMMAND_MUTE";
 | 
			
		||||
    case enums::MEDIA_PLAYER_COMMAND_UNMUTE:
 | 
			
		||||
      return "MEDIA_PLAYER_COMMAND_UNMUTE";
 | 
			
		||||
    default:
 | 
			
		||||
      return "UNKNOWN";
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
bool HelloRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value) {
 | 
			
		||||
  switch (field_id) {
 | 
			
		||||
    case 1: {
 | 
			
		||||
@@ -4574,6 +4604,254 @@ void ButtonCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
bool ListEntitiesMediaPlayerResponse::decode_varint(uint32_t field_id, ProtoVarInt value) {
 | 
			
		||||
  switch (field_id) {
 | 
			
		||||
    case 6: {
 | 
			
		||||
      this->disabled_by_default = value.as_bool();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 7: {
 | 
			
		||||
      this->entity_category = value.as_enum<enums::EntityCategory>();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 8: {
 | 
			
		||||
      this->supports_pause = value.as_bool();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    default:
 | 
			
		||||
      return false;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
bool ListEntitiesMediaPlayerResponse::decode_length(uint32_t field_id, ProtoLengthDelimited value) {
 | 
			
		||||
  switch (field_id) {
 | 
			
		||||
    case 1: {
 | 
			
		||||
      this->object_id = value.as_string();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 3: {
 | 
			
		||||
      this->name = value.as_string();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 4: {
 | 
			
		||||
      this->unique_id = value.as_string();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 5: {
 | 
			
		||||
      this->icon = value.as_string();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    default:
 | 
			
		||||
      return false;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
bool ListEntitiesMediaPlayerResponse::decode_32bit(uint32_t field_id, Proto32Bit value) {
 | 
			
		||||
  switch (field_id) {
 | 
			
		||||
    case 2: {
 | 
			
		||||
      this->key = value.as_fixed32();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    default:
 | 
			
		||||
      return false;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
void ListEntitiesMediaPlayerResponse::encode(ProtoWriteBuffer buffer) const {
 | 
			
		||||
  buffer.encode_string(1, this->object_id);
 | 
			
		||||
  buffer.encode_fixed32(2, this->key);
 | 
			
		||||
  buffer.encode_string(3, this->name);
 | 
			
		||||
  buffer.encode_string(4, this->unique_id);
 | 
			
		||||
  buffer.encode_string(5, this->icon);
 | 
			
		||||
  buffer.encode_bool(6, this->disabled_by_default);
 | 
			
		||||
  buffer.encode_enum<enums::EntityCategory>(7, this->entity_category);
 | 
			
		||||
  buffer.encode_bool(8, this->supports_pause);
 | 
			
		||||
}
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
void ListEntitiesMediaPlayerResponse::dump_to(std::string &out) const {
 | 
			
		||||
  __attribute__((unused)) char buffer[64];
 | 
			
		||||
  out.append("ListEntitiesMediaPlayerResponse {\n");
 | 
			
		||||
  out.append("  object_id: ");
 | 
			
		||||
  out.append("'").append(this->object_id).append("'");
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  key: ");
 | 
			
		||||
  sprintf(buffer, "%u", this->key);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  name: ");
 | 
			
		||||
  out.append("'").append(this->name).append("'");
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  unique_id: ");
 | 
			
		||||
  out.append("'").append(this->unique_id).append("'");
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  icon: ");
 | 
			
		||||
  out.append("'").append(this->icon).append("'");
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  disabled_by_default: ");
 | 
			
		||||
  out.append(YESNO(this->disabled_by_default));
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  entity_category: ");
 | 
			
		||||
  out.append(proto_enum_to_string<enums::EntityCategory>(this->entity_category));
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  supports_pause: ");
 | 
			
		||||
  out.append(YESNO(this->supports_pause));
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
bool MediaPlayerStateResponse::decode_varint(uint32_t field_id, ProtoVarInt value) {
 | 
			
		||||
  switch (field_id) {
 | 
			
		||||
    case 2: {
 | 
			
		||||
      this->state = value.as_enum<enums::MediaPlayerState>();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 4: {
 | 
			
		||||
      this->muted = value.as_bool();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    default:
 | 
			
		||||
      return false;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
bool MediaPlayerStateResponse::decode_32bit(uint32_t field_id, Proto32Bit value) {
 | 
			
		||||
  switch (field_id) {
 | 
			
		||||
    case 1: {
 | 
			
		||||
      this->key = value.as_fixed32();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 3: {
 | 
			
		||||
      this->volume = value.as_float();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    default:
 | 
			
		||||
      return false;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
void MediaPlayerStateResponse::encode(ProtoWriteBuffer buffer) const {
 | 
			
		||||
  buffer.encode_fixed32(1, this->key);
 | 
			
		||||
  buffer.encode_enum<enums::MediaPlayerState>(2, this->state);
 | 
			
		||||
  buffer.encode_float(3, this->volume);
 | 
			
		||||
  buffer.encode_bool(4, this->muted);
 | 
			
		||||
}
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
void MediaPlayerStateResponse::dump_to(std::string &out) const {
 | 
			
		||||
  __attribute__((unused)) char buffer[64];
 | 
			
		||||
  out.append("MediaPlayerStateResponse {\n");
 | 
			
		||||
  out.append("  key: ");
 | 
			
		||||
  sprintf(buffer, "%u", this->key);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  state: ");
 | 
			
		||||
  out.append(proto_enum_to_string<enums::MediaPlayerState>(this->state));
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  volume: ");
 | 
			
		||||
  sprintf(buffer, "%g", this->volume);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  muted: ");
 | 
			
		||||
  out.append(YESNO(this->muted));
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
bool MediaPlayerCommandRequest::decode_varint(uint32_t field_id, ProtoVarInt value) {
 | 
			
		||||
  switch (field_id) {
 | 
			
		||||
    case 2: {
 | 
			
		||||
      this->has_command = value.as_bool();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 3: {
 | 
			
		||||
      this->command = value.as_enum<enums::MediaPlayerCommand>();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 4: {
 | 
			
		||||
      this->has_volume = value.as_bool();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 6: {
 | 
			
		||||
      this->has_media_url = value.as_bool();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    default:
 | 
			
		||||
      return false;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
bool MediaPlayerCommandRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value) {
 | 
			
		||||
  switch (field_id) {
 | 
			
		||||
    case 7: {
 | 
			
		||||
      this->media_url = value.as_string();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    default:
 | 
			
		||||
      return false;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
bool MediaPlayerCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) {
 | 
			
		||||
  switch (field_id) {
 | 
			
		||||
    case 1: {
 | 
			
		||||
      this->key = value.as_fixed32();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    case 5: {
 | 
			
		||||
      this->volume = value.as_float();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    default:
 | 
			
		||||
      return false;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
void MediaPlayerCommandRequest::encode(ProtoWriteBuffer buffer) const {
 | 
			
		||||
  buffer.encode_fixed32(1, this->key);
 | 
			
		||||
  buffer.encode_bool(2, this->has_command);
 | 
			
		||||
  buffer.encode_enum<enums::MediaPlayerCommand>(3, this->command);
 | 
			
		||||
  buffer.encode_bool(4, this->has_volume);
 | 
			
		||||
  buffer.encode_float(5, this->volume);
 | 
			
		||||
  buffer.encode_bool(6, this->has_media_url);
 | 
			
		||||
  buffer.encode_string(7, this->media_url);
 | 
			
		||||
}
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
void MediaPlayerCommandRequest::dump_to(std::string &out) const {
 | 
			
		||||
  __attribute__((unused)) char buffer[64];
 | 
			
		||||
  out.append("MediaPlayerCommandRequest {\n");
 | 
			
		||||
  out.append("  key: ");
 | 
			
		||||
  sprintf(buffer, "%u", this->key);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  has_command: ");
 | 
			
		||||
  out.append(YESNO(this->has_command));
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  command: ");
 | 
			
		||||
  out.append(proto_enum_to_string<enums::MediaPlayerCommand>(this->command));
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  has_volume: ");
 | 
			
		||||
  out.append(YESNO(this->has_volume));
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  volume: ");
 | 
			
		||||
  sprintf(buffer, "%g", this->volume);
 | 
			
		||||
  out.append(buffer);
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  has_media_url: ");
 | 
			
		||||
  out.append(YESNO(this->has_media_url));
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
 | 
			
		||||
  out.append("  media_url: ");
 | 
			
		||||
  out.append("'").append(this->media_url).append("'");
 | 
			
		||||
  out.append("\n");
 | 
			
		||||
  out.append("}");
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
}  // namespace api
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
@@ -141,6 +141,19 @@ enum LockCommand : uint32_t {
 | 
			
		||||
  LOCK_LOCK = 1,
 | 
			
		||||
  LOCK_OPEN = 2,
 | 
			
		||||
};
 | 
			
		||||
enum MediaPlayerState : uint32_t {
 | 
			
		||||
  MEDIA_PLAYER_STATE_NONE = 0,
 | 
			
		||||
  MEDIA_PLAYER_STATE_IDLE = 1,
 | 
			
		||||
  MEDIA_PLAYER_STATE_PLAYING = 2,
 | 
			
		||||
  MEDIA_PLAYER_STATE_PAUSED = 3,
 | 
			
		||||
};
 | 
			
		||||
enum MediaPlayerCommand : uint32_t {
 | 
			
		||||
  MEDIA_PLAYER_COMMAND_PLAY = 0,
 | 
			
		||||
  MEDIA_PLAYER_COMMAND_PAUSE = 1,
 | 
			
		||||
  MEDIA_PLAYER_COMMAND_STOP = 2,
 | 
			
		||||
  MEDIA_PLAYER_COMMAND_MUTE = 3,
 | 
			
		||||
  MEDIA_PLAYER_COMMAND_UNMUTE = 4,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace enums
 | 
			
		||||
 | 
			
		||||
@@ -1146,6 +1159,60 @@ class ButtonCommandRequest : public ProtoMessage {
 | 
			
		||||
 protected:
 | 
			
		||||
  bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
 | 
			
		||||
};
 | 
			
		||||
class ListEntitiesMediaPlayerResponse : public ProtoMessage {
 | 
			
		||||
 public:
 | 
			
		||||
  std::string object_id{};
 | 
			
		||||
  uint32_t key{0};
 | 
			
		||||
  std::string name{};
 | 
			
		||||
  std::string unique_id{};
 | 
			
		||||
  std::string icon{};
 | 
			
		||||
  bool disabled_by_default{false};
 | 
			
		||||
  enums::EntityCategory entity_category{};
 | 
			
		||||
  bool supports_pause{false};
 | 
			
		||||
  void encode(ProtoWriteBuffer buffer) const override;
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
  void dump_to(std::string &out) const override;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
 | 
			
		||||
  bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
 | 
			
		||||
  bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
 | 
			
		||||
};
 | 
			
		||||
class MediaPlayerStateResponse : public ProtoMessage {
 | 
			
		||||
 public:
 | 
			
		||||
  uint32_t key{0};
 | 
			
		||||
  enums::MediaPlayerState state{};
 | 
			
		||||
  float volume{0.0f};
 | 
			
		||||
  bool muted{false};
 | 
			
		||||
  void encode(ProtoWriteBuffer buffer) const override;
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
  void dump_to(std::string &out) const override;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
 | 
			
		||||
  bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
 | 
			
		||||
};
 | 
			
		||||
class MediaPlayerCommandRequest : public ProtoMessage {
 | 
			
		||||
 public:
 | 
			
		||||
  uint32_t key{0};
 | 
			
		||||
  bool has_command{false};
 | 
			
		||||
  enums::MediaPlayerCommand command{};
 | 
			
		||||
  bool has_volume{false};
 | 
			
		||||
  float volume{0.0f};
 | 
			
		||||
  bool has_media_url{false};
 | 
			
		||||
  std::string media_url{};
 | 
			
		||||
  void encode(ProtoWriteBuffer buffer) const override;
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
  void dump_to(std::string &out) const override;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
 | 
			
		||||
  bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
 | 
			
		||||
  bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace api
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
@@ -310,6 +310,24 @@ bool APIServerConnectionBase::send_list_entities_button_response(const ListEntit
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_BUTTON
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_MEDIA_PLAYER
 | 
			
		||||
bool APIServerConnectionBase::send_list_entities_media_player_response(const ListEntitiesMediaPlayerResponse &msg) {
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
  ESP_LOGVV(TAG, "send_list_entities_media_player_response: %s", msg.dump().c_str());
 | 
			
		||||
#endif
 | 
			
		||||
  return this->send_message_<ListEntitiesMediaPlayerResponse>(msg, 63);
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_MEDIA_PLAYER
 | 
			
		||||
bool APIServerConnectionBase::send_media_player_state_response(const MediaPlayerStateResponse &msg) {
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
  ESP_LOGVV(TAG, "send_media_player_state_response: %s", msg.dump().c_str());
 | 
			
		||||
#endif
 | 
			
		||||
  return this->send_message_<MediaPlayerStateResponse>(msg, 64);
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_MEDIA_PLAYER
 | 
			
		||||
#endif
 | 
			
		||||
bool APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type, uint8_t *msg_data) {
 | 
			
		||||
  switch (msg_type) {
 | 
			
		||||
    case 1: {
 | 
			
		||||
@@ -563,6 +581,17 @@ bool APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
 | 
			
		||||
      ESP_LOGVV(TAG, "on_button_command_request: %s", msg.dump().c_str());
 | 
			
		||||
#endif
 | 
			
		||||
      this->on_button_command_request(msg);
 | 
			
		||||
#endif
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case 65: {
 | 
			
		||||
#ifdef USE_MEDIA_PLAYER
 | 
			
		||||
      MediaPlayerCommandRequest msg;
 | 
			
		||||
      msg.decode(msg_data, msg_size);
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
      ESP_LOGVV(TAG, "on_media_player_command_request: %s", msg.dump().c_str());
 | 
			
		||||
#endif
 | 
			
		||||
      this->on_media_player_command_request(msg);
 | 
			
		||||
#endif
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
@@ -813,6 +842,19 @@ void APIServerConnection::on_lock_command_request(const LockCommandRequest &msg)
 | 
			
		||||
  this->lock_command(msg);
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_MEDIA_PLAYER
 | 
			
		||||
void APIServerConnection::on_media_player_command_request(const MediaPlayerCommandRequest &msg) {
 | 
			
		||||
  if (!this->is_connection_setup()) {
 | 
			
		||||
    this->on_no_setup_connection();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (!this->is_authenticated()) {
 | 
			
		||||
    this->on_unauthenticated_access();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  this->media_player_command(msg);
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
}  // namespace api
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
@@ -144,6 +144,15 @@ class APIServerConnectionBase : public ProtoService {
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_BUTTON
 | 
			
		||||
  virtual void on_button_command_request(const ButtonCommandRequest &value){};
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_MEDIA_PLAYER
 | 
			
		||||
  bool send_list_entities_media_player_response(const ListEntitiesMediaPlayerResponse &msg);
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_MEDIA_PLAYER
 | 
			
		||||
  bool send_media_player_state_response(const MediaPlayerStateResponse &msg);
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_MEDIA_PLAYER
 | 
			
		||||
  virtual void on_media_player_command_request(const MediaPlayerCommandRequest &value){};
 | 
			
		||||
#endif
 | 
			
		||||
 protected:
 | 
			
		||||
  bool read_message(uint32_t msg_size, uint32_t msg_type, uint8_t *msg_data) override;
 | 
			
		||||
@@ -192,6 +201,9 @@ class APIServerConnection : public APIServerConnectionBase {
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_LOCK
 | 
			
		||||
  virtual void lock_command(const LockCommandRequest &msg) = 0;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_MEDIA_PLAYER
 | 
			
		||||
  virtual void media_player_command(const MediaPlayerCommandRequest &msg) = 0;
 | 
			
		||||
#endif
 | 
			
		||||
 protected:
 | 
			
		||||
  void on_hello_request(const HelloRequest &msg) override;
 | 
			
		||||
@@ -236,6 +248,9 @@ class APIServerConnection : public APIServerConnectionBase {
 | 
			
		||||
#ifdef USE_LOCK
 | 
			
		||||
  void on_lock_command_request(const LockCommandRequest &msg) override;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_MEDIA_PLAYER
 | 
			
		||||
  void on_media_player_command_request(const MediaPlayerCommandRequest &msg) override;
 | 
			
		||||
#endif
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace api
 | 
			
		||||
 
 | 
			
		||||
@@ -272,6 +272,15 @@ void APIServer::on_lock_update(lock::Lock *obj) {
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef USE_MEDIA_PLAYER
 | 
			
		||||
void APIServer::on_media_player_update(media_player::MediaPlayer *obj) {
 | 
			
		||||
  if (obj->is_internal())
 | 
			
		||||
    return;
 | 
			
		||||
  for (auto &c : this->clients_)
 | 
			
		||||
    c->send_media_player_state(obj);
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
float APIServer::get_setup_priority() const { return setup_priority::AFTER_WIFI; }
 | 
			
		||||
void APIServer::set_port(uint16_t port) { this->port_ = port; }
 | 
			
		||||
APIServer *global_api_server = nullptr;  // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
 | 
			
		||||
 
 | 
			
		||||
@@ -68,6 +68,9 @@ class APIServer : public Component, public Controller {
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_LOCK
 | 
			
		||||
  void on_lock_update(lock::Lock *obj) override;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_MEDIA_PLAYER
 | 
			
		||||
  void on_media_player_update(media_player::MediaPlayer *obj) override;
 | 
			
		||||
#endif
 | 
			
		||||
  void send_homeassistant_service_call(const HomeassistantServiceResponse &call);
 | 
			
		||||
  void register_user_service(UserServiceDescriptor *descriptor) { this->user_services_.push_back(descriptor); }
 | 
			
		||||
 
 | 
			
		||||
@@ -64,5 +64,11 @@ bool ListEntitiesIterator::on_number(number::Number *number) { return this->clie
 | 
			
		||||
bool ListEntitiesIterator::on_select(select::Select *select) { return this->client_->send_select_info(select); }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef USE_MEDIA_PLAYER
 | 
			
		||||
bool ListEntitiesIterator::on_media_player(media_player::MediaPlayer *media_player) {
 | 
			
		||||
  return this->client_->send_media_player_info(media_player);
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
}  // namespace api
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
@@ -51,6 +51,9 @@ class ListEntitiesIterator : public ComponentIterator {
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_LOCK
 | 
			
		||||
  bool on_lock(lock::Lock *a_lock) override;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_MEDIA_PLAYER
 | 
			
		||||
  bool on_media_player(media_player::MediaPlayer *media_player) override;
 | 
			
		||||
#endif
 | 
			
		||||
  bool on_end() override;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -50,6 +50,11 @@ bool InitialStateIterator::on_select(select::Select *select) {
 | 
			
		||||
#ifdef USE_LOCK
 | 
			
		||||
bool InitialStateIterator::on_lock(lock::Lock *a_lock) { return this->client_->send_lock_state(a_lock, a_lock->state); }
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_MEDIA_PLAYER
 | 
			
		||||
bool InitialStateIterator::on_media_player(media_player::MediaPlayer *media_player) {
 | 
			
		||||
  return this->client_->send_media_player_state(media_player);
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
InitialStateIterator::InitialStateIterator(APIConnection *client) : client_(client) {}
 | 
			
		||||
 | 
			
		||||
}  // namespace api
 | 
			
		||||
 
 | 
			
		||||
@@ -48,6 +48,9 @@ class InitialStateIterator : public ComponentIterator {
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_LOCK
 | 
			
		||||
  bool on_lock(lock::Lock *a_lock) override;
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_MEDIA_PLAYER
 | 
			
		||||
  bool on_media_player(media_player::MediaPlayer *media_player) override;
 | 
			
		||||
#endif
 | 
			
		||||
 protected:
 | 
			
		||||
  APIConnection *client_;
 | 
			
		||||
 
 | 
			
		||||
@@ -36,6 +36,14 @@ static uint8_t bedjet_fan_speed_to_step(const std::string &fan_step_percent) {
 | 
			
		||||
  return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static BedjetButton heat_button(BedjetHeatMode mode) {
 | 
			
		||||
  BedjetButton btn = BTN_HEAT;
 | 
			
		||||
  if (mode == HEAT_MODE_EXTENDED) {
 | 
			
		||||
    btn = BTN_EXTHT;
 | 
			
		||||
  }
 | 
			
		||||
  return btn;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Bedjet::upgrade_firmware() {
 | 
			
		||||
  auto *pkt = this->codec_->get_button_request(MAGIC_UPDATE);
 | 
			
		||||
  auto status = this->write_bedjet_packet_(pkt);
 | 
			
		||||
@@ -117,7 +125,7 @@ void Bedjet::control(const ClimateCall &call) {
 | 
			
		||||
        pkt = this->codec_->get_button_request(BTN_OFF);
 | 
			
		||||
        break;
 | 
			
		||||
      case climate::CLIMATE_MODE_HEAT:
 | 
			
		||||
        pkt = this->codec_->get_button_request(BTN_HEAT);
 | 
			
		||||
        pkt = this->codec_->get_button_request(heat_button(this->heating_mode_));
 | 
			
		||||
        break;
 | 
			
		||||
      case climate::CLIMATE_MODE_FAN_ONLY:
 | 
			
		||||
        pkt = this->codec_->get_button_request(BTN_COOL);
 | 
			
		||||
@@ -186,6 +194,8 @@ void Bedjet::control(const ClimateCall &call) {
 | 
			
		||||
      pkt = this->codec_->get_button_request(BTN_M2);
 | 
			
		||||
    } else if (preset == "M3") {
 | 
			
		||||
      pkt = this->codec_->get_button_request(BTN_M3);
 | 
			
		||||
    } else if (preset == "LTD HT") {
 | 
			
		||||
      pkt = this->codec_->get_button_request(BTN_HEAT);
 | 
			
		||||
    } else if (preset == "EXT HT") {
 | 
			
		||||
      pkt = this->codec_->get_button_request(BTN_EXTHT);
 | 
			
		||||
    } else {
 | 
			
		||||
@@ -298,7 +308,7 @@ void Bedjet::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc
 | 
			
		||||
 | 
			
		||||
#ifdef USE_TIME
 | 
			
		||||
      if (this->time_id_.has_value()) {
 | 
			
		||||
        this->send_local_time_();
 | 
			
		||||
        this->send_local_time();
 | 
			
		||||
      }
 | 
			
		||||
#endif
 | 
			
		||||
      break;
 | 
			
		||||
@@ -453,40 +463,47 @@ uint8_t Bedjet::write_notify_config_descriptor_(bool enable) {
 | 
			
		||||
 | 
			
		||||
#ifdef USE_TIME
 | 
			
		||||
/** Attempts to sync the local time (via `time_id`) to the BedJet device. */
 | 
			
		||||
void Bedjet::send_local_time_() {
 | 
			
		||||
  if (this->node_state != espbt::ClientState::ESTABLISHED) {
 | 
			
		||||
    ESP_LOGV(TAG, "[%s] Not connected, cannot send time.", this->get_name().c_str());
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  auto *time_id = *this->time_id_;
 | 
			
		||||
  time::ESPTime now = time_id->now();
 | 
			
		||||
  if (now.is_valid()) {
 | 
			
		||||
    uint8_t hour = now.hour;
 | 
			
		||||
    uint8_t minute = now.minute;
 | 
			
		||||
    BedjetPacket *pkt = this->codec_->get_set_time_request(hour, minute);
 | 
			
		||||
    auto status = this->write_bedjet_packet_(pkt);
 | 
			
		||||
    if (status) {
 | 
			
		||||
      ESP_LOGW(TAG, "Failed setting BedJet clock: %d", status);
 | 
			
		||||
    } else {
 | 
			
		||||
      ESP_LOGD(TAG, "[%s] BedJet clock set to: %d:%02d", this->get_name().c_str(), hour, minute);
 | 
			
		||||
void Bedjet::send_local_time() {
 | 
			
		||||
  if (this->time_id_.has_value()) {
 | 
			
		||||
    auto *time_id = *this->time_id_;
 | 
			
		||||
    time::ESPTime now = time_id->now();
 | 
			
		||||
    if (now.is_valid()) {
 | 
			
		||||
      this->set_clock(now.hour, now.minute);
 | 
			
		||||
      ESP_LOGD(TAG, "Using time component to set BedJet clock: %d:%02d", now.hour, now.minute);
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGI(TAG, "`time_id` is not configured: will not sync BedJet clock.");
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/** Initializes time sync callbacks to support syncing current time to the BedJet. */
 | 
			
		||||
void Bedjet::setup_time_() {
 | 
			
		||||
  if (this->time_id_.has_value()) {
 | 
			
		||||
    this->send_local_time_();
 | 
			
		||||
    this->send_local_time();
 | 
			
		||||
    auto *time_id = *this->time_id_;
 | 
			
		||||
    time_id->add_on_time_sync_callback([this] { this->send_local_time_(); });
 | 
			
		||||
    time::ESPTime now = time_id->now();
 | 
			
		||||
    ESP_LOGD(TAG, "Using time component to set BedJet clock: %d:%02d", now.hour, now.minute);
 | 
			
		||||
    time_id->add_on_time_sync_callback([this] { this->send_local_time(); });
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGI(TAG, "`time_id` is not configured: will not sync BedJet clock.");
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/** Attempt to set the BedJet device's clock to the specified time. */
 | 
			
		||||
void Bedjet::set_clock(uint8_t hour, uint8_t minute) {
 | 
			
		||||
  if (this->node_state != espbt::ClientState::ESTABLISHED) {
 | 
			
		||||
    ESP_LOGV(TAG, "[%s] Not connected, cannot send time.", this->get_name().c_str());
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  BedjetPacket *pkt = this->codec_->get_set_time_request(hour, minute);
 | 
			
		||||
  auto status = this->write_bedjet_packet_(pkt);
 | 
			
		||||
  if (status) {
 | 
			
		||||
    ESP_LOGW(TAG, "Failed setting BedJet clock: %d", status);
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGD(TAG, "[%s] BedJet clock set to: %d:%02d", this->get_name().c_str(), hour, minute);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/** Writes one BedjetPacket to the BLE client on the BEDJET_COMMAND_UUID. */
 | 
			
		||||
uint8_t Bedjet::write_bedjet_packet_(BedjetPacket *pkt) {
 | 
			
		||||
  if (this->node_state != espbt::ClientState::ESTABLISHED) {
 | 
			
		||||
@@ -557,11 +574,25 @@ bool Bedjet::update_status_() {
 | 
			
		||||
      break;
 | 
			
		||||
 | 
			
		||||
    case MODE_HEAT:
 | 
			
		||||
      this->mode = climate::CLIMATE_MODE_HEAT;
 | 
			
		||||
      this->action = climate::CLIMATE_ACTION_HEATING;
 | 
			
		||||
      this->preset.reset();
 | 
			
		||||
      if (this->heating_mode_ == HEAT_MODE_EXTENDED) {
 | 
			
		||||
        this->set_custom_preset_("LTD HT");
 | 
			
		||||
      } else {
 | 
			
		||||
        this->custom_preset.reset();
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
 | 
			
		||||
    case MODE_EXTHT:
 | 
			
		||||
      this->mode = climate::CLIMATE_MODE_HEAT;
 | 
			
		||||
      this->action = climate::CLIMATE_ACTION_HEATING;
 | 
			
		||||
      this->custom_preset.reset();
 | 
			
		||||
      this->preset.reset();
 | 
			
		||||
      if (this->heating_mode_ == HEAT_MODE_EXTENDED) {
 | 
			
		||||
        this->custom_preset.reset();
 | 
			
		||||
      } else {
 | 
			
		||||
        this->set_custom_preset_("EXT HT");
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
 | 
			
		||||
    case MODE_COOL:
 | 
			
		||||
 
 | 
			
		||||
@@ -4,6 +4,7 @@
 | 
			
		||||
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
 | 
			
		||||
#include "esphome/components/climate/climate.h"
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/core/defines.h"
 | 
			
		||||
#include "esphome/core/hal.h"
 | 
			
		||||
#include "bedjet_base.h"
 | 
			
		||||
 | 
			
		||||
@@ -37,8 +38,12 @@ class Bedjet : public climate::Climate, public esphome::ble_client::BLEClientNod
 | 
			
		||||
 | 
			
		||||
#ifdef USE_TIME
 | 
			
		||||
  void set_time_id(time::RealTimeClock *time_id) { this->time_id_ = time_id; }
 | 
			
		||||
  void send_local_time();
 | 
			
		||||
#endif
 | 
			
		||||
  void set_clock(uint8_t hour, uint8_t minute);
 | 
			
		||||
  void set_status_timeout(uint32_t timeout) { this->timeout_ = timeout; }
 | 
			
		||||
  /** Sets the default strategy to use for climate::CLIMATE_MODE_HEAT. */
 | 
			
		||||
  void set_heating_mode(BedjetHeatMode mode) { this->heating_mode_ = mode; }
 | 
			
		||||
 | 
			
		||||
  /** Attempts to check for and apply firmware updates. */
 | 
			
		||||
  void upgrade_firmware();
 | 
			
		||||
@@ -73,6 +78,11 @@ class Bedjet : public climate::Climate, public esphome::ble_client::BLEClientNod
 | 
			
		||||
        "M2",
 | 
			
		||||
        "M3",
 | 
			
		||||
    });
 | 
			
		||||
    if (this->heating_mode_ == HEAT_MODE_EXTENDED) {
 | 
			
		||||
      traits.add_supported_custom_preset("LTD HT");
 | 
			
		||||
    } else {
 | 
			
		||||
      traits.add_supported_custom_preset("EXT HT");
 | 
			
		||||
    }
 | 
			
		||||
    traits.set_visual_min_temperature(19.0);
 | 
			
		||||
    traits.set_visual_max_temperature(43.0);
 | 
			
		||||
    traits.set_visual_temperature_step(1.0);
 | 
			
		||||
@@ -84,11 +94,11 @@ class Bedjet : public climate::Climate, public esphome::ble_client::BLEClientNod
 | 
			
		||||
 | 
			
		||||
#ifdef USE_TIME
 | 
			
		||||
  void setup_time_();
 | 
			
		||||
  void send_local_time_();
 | 
			
		||||
  optional<time::RealTimeClock *> time_id_{};
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  uint32_t timeout_{DEFAULT_STATUS_TIMEOUT};
 | 
			
		||||
  BedjetHeatMode heating_mode_ = HEAT_MODE_HEAT;
 | 
			
		||||
 | 
			
		||||
  static const uint32_t MIN_NOTIFY_THROTTLE = 5000;
 | 
			
		||||
  static const uint32_t NOTIFY_WARN_THRESHOLD = 300000;
 | 
			
		||||
 
 | 
			
		||||
@@ -24,6 +24,14 @@ enum BedjetMode : uint8_t {
 | 
			
		||||
  MODE_WAIT = 6,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/** Optional heating strategies to use for climate::CLIMATE_MODE_HEAT. */
 | 
			
		||||
enum BedjetHeatMode {
 | 
			
		||||
  /// HVACMode.HEAT is handled using BTN_HEAT (default)
 | 
			
		||||
  HEAT_MODE_HEAT,
 | 
			
		||||
  /// HVACMode.HEAT is handled using BTN_EXTHT
 | 
			
		||||
  HEAT_MODE_EXTENDED,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum BedjetButton : uint8_t {
 | 
			
		||||
  /// Turn BedJet off
 | 
			
		||||
  BTN_OFF = 0x1,
 | 
			
		||||
@@ -66,8 +74,8 @@ enum BedjetCommand : uint8_t {
 | 
			
		||||
 | 
			
		||||
#define BEDJET_FAN_STEP_NAMES_ \
 | 
			
		||||
  { \
 | 
			
		||||
    "  5%", " 10%", " 15%", " 20%", " 25%", " 30%", " 35%", " 40%", " 45%", " 50%", " 55%", " 60%", " 65%", " 70%", \
 | 
			
		||||
        " 75%", " 80%", " 85%", " 90%", " 95%", "100%" \
 | 
			
		||||
    "5%", "10%", "15%", "20%", "25%", "30%", "35%", "40%", "45%", "50%", "55%", "60%", "65%", "70%", "75%", "80%", \
 | 
			
		||||
        "85%", "90%", "95%", "100%" \
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
static const char *const BEDJET_FAN_STEP_NAMES[20] = BEDJET_FAN_STEP_NAMES_;
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@ import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.components import climate, ble_client, time
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_HEAT_MODE,
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    CONF_RECEIVE_TIMEOUT,
 | 
			
		||||
    CONF_TIME_ID,
 | 
			
		||||
@@ -14,11 +15,19 @@ bedjet_ns = cg.esphome_ns.namespace("bedjet")
 | 
			
		||||
Bedjet = bedjet_ns.class_(
 | 
			
		||||
    "Bedjet", climate.Climate, ble_client.BLEClientNode, cg.PollingComponent
 | 
			
		||||
)
 | 
			
		||||
BedjetHeatMode = bedjet_ns.enum("BedjetHeatMode")
 | 
			
		||||
BEDJET_HEAT_MODES = {
 | 
			
		||||
    "heat": BedjetHeatMode.HEAT_MODE_HEAT,
 | 
			
		||||
    "extended": BedjetHeatMode.HEAT_MODE_EXTENDED,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = (
 | 
			
		||||
    climate.CLIMATE_SCHEMA.extend(
 | 
			
		||||
        {
 | 
			
		||||
            cv.GenerateID(): cv.declare_id(Bedjet),
 | 
			
		||||
            cv.Optional(CONF_HEAT_MODE, default="heat"): cv.enum(
 | 
			
		||||
                BEDJET_HEAT_MODES, lower=True
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_TIME_ID): cv.use_id(time.RealTimeClock),
 | 
			
		||||
            cv.Optional(
 | 
			
		||||
                CONF_RECEIVE_TIMEOUT, default="0s"
 | 
			
		||||
@@ -35,6 +44,7 @@ async def to_code(config):
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
    await climate.register_climate(var, config)
 | 
			
		||||
    await ble_client.register_ble_node(var, config)
 | 
			
		||||
    cg.add(var.set_heating_mode(config[CONF_HEAT_MODE]))
 | 
			
		||||
    if CONF_TIME_ID in config:
 | 
			
		||||
        time_ = await cg.get_variable(config[CONF_TIME_ID])
 | 
			
		||||
        cg.add(var.set_time_id(time_))
 | 
			
		||||
 
 | 
			
		||||
@@ -29,6 +29,7 @@ from esphome.const import (
 | 
			
		||||
    DEVICE_CLASS_EMPTY,
 | 
			
		||||
    DEVICE_CLASS_BATTERY,
 | 
			
		||||
    DEVICE_CLASS_BATTERY_CHARGING,
 | 
			
		||||
    DEVICE_CLASS_CARBON_MONOXIDE,
 | 
			
		||||
    DEVICE_CLASS_COLD,
 | 
			
		||||
    DEVICE_CLASS_CONNECTIVITY,
 | 
			
		||||
    DEVICE_CLASS_DOOR,
 | 
			
		||||
@@ -63,6 +64,7 @@ DEVICE_CLASSES = [
 | 
			
		||||
    DEVICE_CLASS_EMPTY,
 | 
			
		||||
    DEVICE_CLASS_BATTERY,
 | 
			
		||||
    DEVICE_CLASS_BATTERY_CHARGING,
 | 
			
		||||
    DEVICE_CLASS_CARBON_MONOXIDE,
 | 
			
		||||
    DEVICE_CLASS_COLD,
 | 
			
		||||
    DEVICE_CLASS_CONNECTIVITY,
 | 
			
		||||
    DEVICE_CLASS_DOOR,
 | 
			
		||||
 
 | 
			
		||||
@@ -69,7 +69,6 @@ void BinarySensor::add_filters(const std::vector<Filter *> &filters) {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
bool BinarySensor::has_state() const { return this->has_state_; }
 | 
			
		||||
uint32_t BinarySensor::hash_base() { return 1210250844UL; }
 | 
			
		||||
bool BinarySensor::is_status_binary_sensor() const { return false; }
 | 
			
		||||
 | 
			
		||||
}  // namespace binary_sensor
 | 
			
		||||
 
 | 
			
		||||
@@ -76,8 +76,6 @@ class BinarySensor : public EntityBase {
 | 
			
		||||
  virtual std::string device_class();
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  uint32_t hash_base() override;
 | 
			
		||||
 | 
			
		||||
  CallbackManager<void(bool)> state_callback_{};
 | 
			
		||||
  optional<std::string> device_class_{};  ///< Stores the override of the device class
 | 
			
		||||
  Filter *filter_list_{nullptr};
 | 
			
		||||
 
 | 
			
		||||
@@ -113,6 +113,7 @@ void BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t es
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_OPEN_EVT: {
 | 
			
		||||
      ESP_LOGV(TAG, "[%s] ESP_GATTC_OPEN_EVT", this->address_str().c_str());
 | 
			
		||||
      this->conn_id = param->open.conn_id;
 | 
			
		||||
      if (param->open.status != ESP_GATT_OK) {
 | 
			
		||||
        ESP_LOGW(TAG, "connect to %s failed, status=%d", this->address_str().c_str(), param->open.status);
 | 
			
		||||
        this->set_states_(espbt::ClientState::IDLE);
 | 
			
		||||
@@ -122,7 +123,10 @@ void BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t es
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_CONNECT_EVT: {
 | 
			
		||||
      ESP_LOGV(TAG, "[%s] ESP_GATTC_CONNECT_EVT", this->address_str().c_str());
 | 
			
		||||
      this->conn_id = param->connect.conn_id;
 | 
			
		||||
      if (this->conn_id != param->connect.conn_id) {
 | 
			
		||||
        ESP_LOGD(TAG, "[%s] Unexpected conn_id in CONNECT_EVT: param conn=%d, open conn=%d",
 | 
			
		||||
                 this->address_str().c_str(), param->connect.conn_id, this->conn_id);
 | 
			
		||||
      }
 | 
			
		||||
      auto ret = esp_ble_gattc_send_mtu_req(this->gattc_if, param->connect.conn_id);
 | 
			
		||||
      if (ret) {
 | 
			
		||||
        ESP_LOGW(TAG, "esp_ble_gattc_send_mtu_req failed, status=%x", ret);
 | 
			
		||||
@@ -183,9 +187,10 @@ void BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t es
 | 
			
		||||
                 descr->uuid.to_string().c_str());
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      uint8_t notify_en = 1;
 | 
			
		||||
      auto status = esp_ble_gattc_write_char_descr(this->gattc_if, this->conn_id, descr->handle, sizeof(notify_en),
 | 
			
		||||
                                                   ¬ify_en, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE);
 | 
			
		||||
      uint16_t notify_en = 1;
 | 
			
		||||
      auto status =
 | 
			
		||||
          esp_ble_gattc_write_char_descr(this->gattc_if, this->conn_id, descr->handle, sizeof(notify_en),
 | 
			
		||||
                                         (uint8_t *) ¬ify_en, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE);
 | 
			
		||||
      if (status) {
 | 
			
		||||
        ESP_LOGW(TAG, "esp_ble_gattc_write_char_descr error, status=%d", status);
 | 
			
		||||
      }
 | 
			
		||||
 
 | 
			
		||||
@@ -11,8 +11,6 @@ namespace ble_client {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "ble_sensor";
 | 
			
		||||
 | 
			
		||||
uint32_t BLESensor::hash_base() { return 343459825UL; }
 | 
			
		||||
 | 
			
		||||
void BLESensor::loop() {}
 | 
			
		||||
 | 
			
		||||
void BLESensor::dump_config() {
 | 
			
		||||
 
 | 
			
		||||
@@ -37,7 +37,6 @@ class BLESensor : public sensor::Sensor, public PollingComponent, public BLEClie
 | 
			
		||||
  uint16_t handle;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  uint32_t hash_base() override;
 | 
			
		||||
  float parse_data_(uint8_t *value, uint16_t value_len);
 | 
			
		||||
  optional<data_to_value_t> data_to_value_func_{};
 | 
			
		||||
  bool notify_;
 | 
			
		||||
 
 | 
			
		||||
@@ -14,8 +14,6 @@ static const char *const TAG = "ble_text_sensor";
 | 
			
		||||
 | 
			
		||||
static const std::string EMPTY = "";
 | 
			
		||||
 | 
			
		||||
uint32_t BLETextSensor::hash_base() { return 193967603UL; }
 | 
			
		||||
 | 
			
		||||
void BLETextSensor::loop() {}
 | 
			
		||||
 | 
			
		||||
void BLETextSensor::dump_config() {
 | 
			
		||||
 
 | 
			
		||||
@@ -35,7 +35,6 @@ class BLETextSensor : public text_sensor::TextSensor, public PollingComponent, p
 | 
			
		||||
  uint16_t handle;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  uint32_t hash_base() override;
 | 
			
		||||
  bool notify_;
 | 
			
		||||
  espbt::ESPBTUUID service_uuid_;
 | 
			
		||||
  espbt::ESPBTUUID char_uuid_;
 | 
			
		||||
 
 | 
			
		||||
@@ -25,7 +25,7 @@ OVERSAMPLING_OPTIONS = {
 | 
			
		||||
    "4X": Oversampling.OVERSAMPLING_X4,
 | 
			
		||||
    "8X": Oversampling.OVERSAMPLING_X8,
 | 
			
		||||
    "16X": Oversampling.OVERSAMPLING_X16,
 | 
			
		||||
    "32x": Oversampling.OVERSAMPLING_X32,
 | 
			
		||||
    "32X": Oversampling.OVERSAMPLING_X32,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
IIRFilter = bmp3xx_ns.enum("IIRFilter")
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,6 @@ void Button::press() {
 | 
			
		||||
  this->press_callback_.call();
 | 
			
		||||
}
 | 
			
		||||
void Button::add_on_press_callback(std::function<void()> &&callback) { this->press_callback_.add(std::move(callback)); }
 | 
			
		||||
uint32_t Button::hash_base() { return 1495763804UL; }
 | 
			
		||||
 | 
			
		||||
void Button::set_device_class(const std::string &device_class) { this->device_class_ = device_class; }
 | 
			
		||||
std::string Button::get_device_class() { return this->device_class_; }
 | 
			
		||||
 
 | 
			
		||||
@@ -47,8 +47,6 @@ class Button : public EntityBase {
 | 
			
		||||
   */
 | 
			
		||||
  virtual void press_action() = 0;
 | 
			
		||||
 | 
			
		||||
  uint32_t hash_base() override;
 | 
			
		||||
 | 
			
		||||
  CallbackManager<void()> press_callback_{};
 | 
			
		||||
  std::string device_class_{};
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
@@ -419,7 +419,6 @@ void Climate::publish_state() {
 | 
			
		||||
  // Save state
 | 
			
		||||
  this->save_state_();
 | 
			
		||||
}
 | 
			
		||||
uint32_t Climate::hash_base() { return 3104134496UL; }
 | 
			
		||||
 | 
			
		||||
ClimateTraits Climate::get_traits() {
 | 
			
		||||
  auto traits = this->traits();
 | 
			
		||||
 
 | 
			
		||||
@@ -282,7 +282,6 @@ class Climate : public EntityBase {
 | 
			
		||||
   */
 | 
			
		||||
  void save_state_();
 | 
			
		||||
 | 
			
		||||
  uint32_t hash_base() override;
 | 
			
		||||
  void dump_traits_(const char *tag);
 | 
			
		||||
 | 
			
		||||
  CallbackManager<void()> state_callback_{};
 | 
			
		||||
 
 | 
			
		||||
@@ -33,8 +33,6 @@ const char *cover_operation_to_str(CoverOperation op) {
 | 
			
		||||
 | 
			
		||||
Cover::Cover(const std::string &name) : EntityBase(name), position{COVER_OPEN} {}
 | 
			
		||||
 | 
			
		||||
uint32_t Cover::hash_base() { return 1727367479UL; }
 | 
			
		||||
 | 
			
		||||
CoverCall::CoverCall(Cover *parent) : parent_(parent) {}
 | 
			
		||||
CoverCall &CoverCall::set_command(const char *command) {
 | 
			
		||||
  if (strcasecmp(command, "OPEN") == 0) {
 | 
			
		||||
 
 | 
			
		||||
@@ -177,7 +177,6 @@ class Cover : public EntityBase {
 | 
			
		||||
  virtual std::string device_class();
 | 
			
		||||
 | 
			
		||||
  optional<CoverRestoreState> restore_state_();
 | 
			
		||||
  uint32_t hash_base() override;
 | 
			
		||||
 | 
			
		||||
  CallbackManager<void()> state_callback_{};
 | 
			
		||||
  optional<std::string> device_class_override_{};
 | 
			
		||||
 
 | 
			
		||||
@@ -85,6 +85,12 @@ enum ImageType {
 | 
			
		||||
  IMAGE_TYPE_RGB565 = 4,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum DisplayType {
 | 
			
		||||
  DISPLAY_TYPE_BINARY = 1,
 | 
			
		||||
  DISPLAY_TYPE_GRAYSCALE = 2,
 | 
			
		||||
  DISPLAY_TYPE_COLOR = 3,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum DisplayRotation {
 | 
			
		||||
  DISPLAY_ROTATION_0_DEGREES = 0,
 | 
			
		||||
  DISPLAY_ROTATION_90_DEGREES = 90,
 | 
			
		||||
@@ -361,6 +367,11 @@ class DisplayBuffer {
 | 
			
		||||
  virtual int get_width_internal() = 0;
 | 
			
		||||
  DisplayRotation get_rotation() const { return this->rotation_; }
 | 
			
		||||
 | 
			
		||||
  /** Get the type of display that the buffer corresponds to. In case of dynamically configurable displays,
 | 
			
		||||
   * returns the type the display is currently configured to.
 | 
			
		||||
   */
 | 
			
		||||
  virtual DisplayType get_display_type() = 0;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void vprintf_(int x, int y, Font *font, Color color, TextAlign align, const char *format, va_list arg);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -66,6 +66,9 @@ class ColorUtil {
 | 
			
		||||
    }
 | 
			
		||||
    return color_return;
 | 
			
		||||
  }
 | 
			
		||||
  static inline Color rgb332_to_color(uint8_t rgb332_color) {
 | 
			
		||||
    return to_color((uint32_t) rgb332_color, COLOR_ORDER_RGB, COLOR_BITNESS_332);
 | 
			
		||||
  }
 | 
			
		||||
  static uint8_t color_to_332(Color color, ColorOrder color_order = ColorOrder::COLOR_ORDER_RGB) {
 | 
			
		||||
    uint16_t red_color, green_color, blue_color;
 | 
			
		||||
 | 
			
		||||
@@ -100,11 +103,57 @@ class ColorUtil {
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static uint32_t color_to_grayscale4(Color color) {
 | 
			
		||||
    uint32_t gs4 = esp_scale8(color.white, 15);
 | 
			
		||||
    return gs4;
 | 
			
		||||
  }
 | 
			
		||||
  /***
 | 
			
		||||
   * Converts a Color value to an 8bit index using a 24bit 888 palette.
 | 
			
		||||
   * Uses euclidiean distance to calculate the linear distance between
 | 
			
		||||
   * two points in an RGB cube, then iterates through the full palette
 | 
			
		||||
   * returning the closest match.
 | 
			
		||||
   * @param[in] color The target color.
 | 
			
		||||
   * @param[in] palette The 256*3 byte RGB palette.
 | 
			
		||||
   * @return The 8 bit index of the closest color (e.g. for display buffer).
 | 
			
		||||
   */
 | 
			
		||||
  // static uint8_t color_to_index8_palette888(Color color, uint8_t *palette) {
 | 
			
		||||
  static uint8_t color_to_index8_palette888(Color color, const uint8_t *palette) {
 | 
			
		||||
    uint8_t closest_index = 0;
 | 
			
		||||
    uint32_t minimum_dist2 = UINT32_MAX;  // Smallest distance^2 to the target
 | 
			
		||||
                                          // so far
 | 
			
		||||
    // int8_t(*plt)[][3] = palette;
 | 
			
		||||
    int16_t tgt_r = color.r;
 | 
			
		||||
    int16_t tgt_g = color.g;
 | 
			
		||||
    int16_t tgt_b = color.b;
 | 
			
		||||
    uint16_t x, y, z;
 | 
			
		||||
    // Loop through each row of the palette
 | 
			
		||||
    for (uint16_t i = 0; i < 256; i++) {
 | 
			
		||||
      // Get the pallet rgb color
 | 
			
		||||
      int16_t plt_r = (int16_t) palette[i * 3 + 0];
 | 
			
		||||
      int16_t plt_g = (int16_t) palette[i * 3 + 1];
 | 
			
		||||
      int16_t plt_b = (int16_t) palette[i * 3 + 2];
 | 
			
		||||
      // Calculate euclidian distance (linear distance in rgb cube).
 | 
			
		||||
      x = (uint32_t) std::abs(tgt_r - plt_r);
 | 
			
		||||
      y = (uint32_t) std::abs(tgt_g - plt_g);
 | 
			
		||||
      z = (uint32_t) std::abs(tgt_b - plt_b);
 | 
			
		||||
      uint32_t dist2 = x * x + y * y + z * z;
 | 
			
		||||
      if (dist2 < minimum_dist2) {
 | 
			
		||||
        minimum_dist2 = dist2;
 | 
			
		||||
        closest_index = (uint8_t) i;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    return closest_index;
 | 
			
		||||
  }
 | 
			
		||||
  /***
 | 
			
		||||
   * Converts an 8bit palette index (e.g. from a display buffer) to a color.
 | 
			
		||||
   * @param[in] index The index to look up.
 | 
			
		||||
   * @param[in] palette The 256*3 byte RGB palette.
 | 
			
		||||
   * @return The RGBW Color object looked up by the palette.
 | 
			
		||||
   */
 | 
			
		||||
  static Color index8_to_color_palette888(uint8_t index, const uint8_t *palette) {
 | 
			
		||||
    Color color = Color(palette[index * 3 + 0], palette[index * 3 + 1], palette[index * 3 + 2], 0);
 | 
			
		||||
    return color;
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
}  // namespace display
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
@@ -79,7 +79,7 @@ async def to_code(config):
 | 
			
		||||
    cg.add(var.set_request_interval(config[CONF_REQUEST_INTERVAL].total_milliseconds))
 | 
			
		||||
    cg.add(var.set_receive_timeout(config[CONF_RECEIVE_TIMEOUT].total_milliseconds))
 | 
			
		||||
 | 
			
		||||
    cg.add_define("DSMR_GAS_MBUS_ID", config[CONF_GAS_MBUS_ID])
 | 
			
		||||
    cg.add_build_flag("-DDSMR_GAS_MBUS_ID=" + str(config[CONF_GAS_MBUS_ID]))
 | 
			
		||||
 | 
			
		||||
    # DSMR Parser
 | 
			
		||||
    cg.add_library("glmnet/Dsmr", "0.5")
 | 
			
		||||
 
 | 
			
		||||
@@ -118,12 +118,17 @@ class ESP32Preferences : public ESPPreferences {
 | 
			
		||||
    // go through vector from back to front (makes erase easier/more efficient)
 | 
			
		||||
    for (ssize_t i = s_pending_save.size() - 1; i >= 0; i--) {
 | 
			
		||||
      const auto &save = s_pending_save[i];
 | 
			
		||||
      esp_err_t err = nvs_set_blob(nvs_handle, save.key.c_str(), save.data.data(), save.data.size());
 | 
			
		||||
      if (err != 0) {
 | 
			
		||||
        ESP_LOGV(TAG, "nvs_set_blob('%s', len=%u) failed: %s", save.key.c_str(), save.data.size(),
 | 
			
		||||
                 esp_err_to_name(err));
 | 
			
		||||
        any_failed = true;
 | 
			
		||||
        continue;
 | 
			
		||||
      ESP_LOGVV(TAG, "Checking if NVS data %s has changed", save.key.c_str());
 | 
			
		||||
      if (is_changed(nvs_handle, save)) {
 | 
			
		||||
        esp_err_t err = nvs_set_blob(nvs_handle, save.key.c_str(), save.data.data(), save.data.size());
 | 
			
		||||
        if (err != 0) {
 | 
			
		||||
          ESP_LOGV(TAG, "nvs_set_blob('%s', len=%u) failed: %s", save.key.c_str(), save.data.size(),
 | 
			
		||||
                   esp_err_to_name(err));
 | 
			
		||||
          any_failed = true;
 | 
			
		||||
          continue;
 | 
			
		||||
        }
 | 
			
		||||
      } else {
 | 
			
		||||
        ESP_LOGD(TAG, "NVS data not changed skipping %s  len=%u", save.key.c_str(), save.data.size());
 | 
			
		||||
      }
 | 
			
		||||
      s_pending_save.erase(s_pending_save.begin() + i);
 | 
			
		||||
    }
 | 
			
		||||
@@ -137,6 +142,22 @@ class ESP32Preferences : public ESPPreferences {
 | 
			
		||||
 | 
			
		||||
    return !any_failed;
 | 
			
		||||
  }
 | 
			
		||||
  bool is_changed(const uint32_t nvs_handle, const NVSData &to_save) {
 | 
			
		||||
    NVSData stored_data{};
 | 
			
		||||
    size_t actual_len;
 | 
			
		||||
    esp_err_t err = nvs_get_blob(nvs_handle, to_save.key.c_str(), nullptr, &actual_len);
 | 
			
		||||
    if (err != 0) {
 | 
			
		||||
      ESP_LOGV(TAG, "nvs_get_blob('%s'): %s - the key might not be set yet", to_save.key.c_str(), esp_err_to_name(err));
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    stored_data.data.reserve(actual_len);
 | 
			
		||||
    err = nvs_get_blob(nvs_handle, to_save.key.c_str(), stored_data.data.data(), &actual_len);
 | 
			
		||||
    if (err != 0) {
 | 
			
		||||
      ESP_LOGV(TAG, "nvs_get_blob('%s') failed: %s", to_save.key.c_str(), esp_err_to_name(err));
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    return to_save.data != stored_data.data;
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
void setup_preferences() {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome import automation
 | 
			
		||||
from esphome import pins
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_FREQUENCY,
 | 
			
		||||
@@ -12,6 +13,7 @@ from esphome.const import (
 | 
			
		||||
    CONF_RESOLUTION,
 | 
			
		||||
    CONF_BRIGHTNESS,
 | 
			
		||||
    CONF_CONTRAST,
 | 
			
		||||
    CONF_TRIGGER_ID,
 | 
			
		||||
)
 | 
			
		||||
from esphome.core import CORE
 | 
			
		||||
from esphome.components.esp32 import add_idf_sdkconfig_option
 | 
			
		||||
@@ -23,7 +25,14 @@ AUTO_LOAD = ["psram"]
 | 
			
		||||
 | 
			
		||||
esp32_camera_ns = cg.esphome_ns.namespace("esp32_camera")
 | 
			
		||||
ESP32Camera = esp32_camera_ns.class_("ESP32Camera", cg.PollingComponent, cg.EntityBase)
 | 
			
		||||
 | 
			
		||||
ESP32CameraStreamStartTrigger = esp32_camera_ns.class_(
 | 
			
		||||
    "ESP32CameraStreamStartTrigger",
 | 
			
		||||
    automation.Trigger.template(),
 | 
			
		||||
)
 | 
			
		||||
ESP32CameraStreamStopTrigger = esp32_camera_ns.class_(
 | 
			
		||||
    "ESP32CameraStreamStopTrigger",
 | 
			
		||||
    automation.Trigger.template(),
 | 
			
		||||
)
 | 
			
		||||
ESP32CameraFrameSize = esp32_camera_ns.enum("ESP32CameraFrameSize")
 | 
			
		||||
FRAME_SIZES = {
 | 
			
		||||
    "160X120": ESP32CameraFrameSize.ESP32_CAMERA_SIZE_160X120,
 | 
			
		||||
@@ -111,6 +120,10 @@ CONF_TEST_PATTERN = "test_pattern"
 | 
			
		||||
CONF_MAX_FRAMERATE = "max_framerate"
 | 
			
		||||
CONF_IDLE_FRAMERATE = "idle_framerate"
 | 
			
		||||
 | 
			
		||||
# stream trigger
 | 
			
		||||
CONF_ON_STREAM_START = "on_stream_start"
 | 
			
		||||
CONF_ON_STREAM_STOP = "on_stream_stop"
 | 
			
		||||
 | 
			
		||||
camera_range_param = cv.int_range(min=-2, max=2)
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(
 | 
			
		||||
@@ -178,6 +191,20 @@ CONFIG_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(
 | 
			
		||||
        cv.Optional(CONF_IDLE_FRAMERATE, default="0.1 fps"): cv.All(
 | 
			
		||||
            cv.framerate, cv.Range(min=0, max=1)
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_ON_STREAM_START): automation.validate_automation(
 | 
			
		||||
            {
 | 
			
		||||
                cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
 | 
			
		||||
                    ESP32CameraStreamStartTrigger
 | 
			
		||||
                ),
 | 
			
		||||
            }
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_ON_STREAM_STOP): automation.validate_automation(
 | 
			
		||||
            {
 | 
			
		||||
                cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
 | 
			
		||||
                    ESP32CameraStreamStopTrigger
 | 
			
		||||
                ),
 | 
			
		||||
            }
 | 
			
		||||
        ),
 | 
			
		||||
    }
 | 
			
		||||
).extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
 | 
			
		||||
@@ -238,3 +265,11 @@ async def to_code(config):
 | 
			
		||||
    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)
 | 
			
		||||
 | 
			
		||||
    for conf in config.get(CONF_ON_STREAM_START, []):
 | 
			
		||||
        trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
 | 
			
		||||
        await automation.build_automation(trigger, [], conf)
 | 
			
		||||
 | 
			
		||||
    for conf in config.get(CONF_ON_STREAM_STOP, []):
 | 
			
		||||
        trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
 | 
			
		||||
        await automation.build_automation(trigger, [], conf)
 | 
			
		||||
 
 | 
			
		||||
@@ -282,8 +282,20 @@ void ESP32Camera::set_idle_update_interval(uint32_t idle_update_interval) {
 | 
			
		||||
void ESP32Camera::add_image_callback(std::function<void(std::shared_ptr<CameraImage>)> &&f) {
 | 
			
		||||
  this->new_image_callback_.add(std::move(f));
 | 
			
		||||
}
 | 
			
		||||
void ESP32Camera::start_stream(CameraRequester requester) { this->stream_requesters_ |= (1U << requester); }
 | 
			
		||||
void ESP32Camera::stop_stream(CameraRequester requester) { this->stream_requesters_ &= ~(1U << requester); }
 | 
			
		||||
void ESP32Camera::add_stream_start_callback(std::function<void()> &&callback) {
 | 
			
		||||
  this->stream_start_callback_.add(std::move(callback));
 | 
			
		||||
}
 | 
			
		||||
void ESP32Camera::add_stream_stop_callback(std::function<void()> &&callback) {
 | 
			
		||||
  this->stream_stop_callback_.add(std::move(callback));
 | 
			
		||||
}
 | 
			
		||||
void ESP32Camera::start_stream(CameraRequester requester) {
 | 
			
		||||
  this->stream_start_callback_.call();
 | 
			
		||||
  this->stream_requesters_ |= (1U << requester);
 | 
			
		||||
}
 | 
			
		||||
void ESP32Camera::stop_stream(CameraRequester requester) {
 | 
			
		||||
  this->stream_stop_callback_.call();
 | 
			
		||||
  this->stream_requesters_ &= ~(1U << requester);
 | 
			
		||||
}
 | 
			
		||||
void ESP32Camera::request_image(CameraRequester requester) { this->single_requesters_ |= (1U << requester); }
 | 
			
		||||
void ESP32Camera::update_camera_parameters() {
 | 
			
		||||
  sensor_t *s = esp_camera_sensor_get();
 | 
			
		||||
@@ -310,7 +322,6 @@ void ESP32Camera::update_camera_parameters() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* ---------------- Internal methods ---------------- */
 | 
			
		||||
uint32_t ESP32Camera::hash_base() { return 3010542557UL; }
 | 
			
		||||
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::framebuffer_task(void *pv) {
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/automation.h"
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/core/entity_base.h"
 | 
			
		||||
#include "esphome/core/helpers.h"
 | 
			
		||||
@@ -145,9 +146,11 @@ class ESP32Camera : public Component, public EntityBase {
 | 
			
		||||
  void request_image(CameraRequester requester);
 | 
			
		||||
  void update_camera_parameters();
 | 
			
		||||
 | 
			
		||||
  void add_stream_start_callback(std::function<void()> &&callback);
 | 
			
		||||
  void add_stream_stop_callback(std::function<void()> &&callback);
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  /* internal methods */
 | 
			
		||||
  uint32_t hash_base() override;
 | 
			
		||||
  bool has_requested_image_() const;
 | 
			
		||||
  bool can_return_image_() const;
 | 
			
		||||
 | 
			
		||||
@@ -187,6 +190,8 @@ class ESP32Camera : public Component, public EntityBase {
 | 
			
		||||
  QueueHandle_t framebuffer_get_queue_;
 | 
			
		||||
  QueueHandle_t framebuffer_return_queue_;
 | 
			
		||||
  CallbackManager<void(std::shared_ptr<CameraImage>)> new_image_callback_;
 | 
			
		||||
  CallbackManager<void()> stream_start_callback_{};
 | 
			
		||||
  CallbackManager<void()> stream_stop_callback_{};
 | 
			
		||||
 | 
			
		||||
  uint32_t last_idle_request_{0};
 | 
			
		||||
  uint32_t last_update_{0};
 | 
			
		||||
@@ -195,6 +200,23 @@ class ESP32Camera : public Component, public EntityBase {
 | 
			
		||||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
 | 
			
		||||
extern ESP32Camera *global_esp32_camera;
 | 
			
		||||
 | 
			
		||||
class ESP32CameraStreamStartTrigger : public Trigger<> {
 | 
			
		||||
 public:
 | 
			
		||||
  explicit ESP32CameraStreamStartTrigger(ESP32Camera *parent) {
 | 
			
		||||
    parent->add_stream_start_callback([this]() { this->trigger(); });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
};
 | 
			
		||||
class ESP32CameraStreamStopTrigger : public Trigger<> {
 | 
			
		||||
 public:
 | 
			
		||||
  explicit ESP32CameraStreamStopTrigger(ESP32Camera *parent) {
 | 
			
		||||
    parent->add_stream_stop_callback([this]() { this->trigger(); });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace esp32_camera
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,4 @@
 | 
			
		||||
#include "fan.h"
 | 
			
		||||
#include "fan_helpers.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
@@ -61,22 +60,6 @@ void FanCall::validate_() {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// This whole method is deprecated, don't warn about usage of deprecated methods inside of it.
 | 
			
		||||
#pragma GCC diagnostic push
 | 
			
		||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
 | 
			
		||||
FanCall &FanCall::set_speed(const char *legacy_speed) {
 | 
			
		||||
  const auto supported_speed_count = this->parent_.get_traits().supported_speed_count();
 | 
			
		||||
  if (strcasecmp(legacy_speed, "low") == 0) {
 | 
			
		||||
    this->set_speed(fan::speed_enum_to_level(FAN_SPEED_LOW, supported_speed_count));
 | 
			
		||||
  } else if (strcasecmp(legacy_speed, "medium") == 0) {
 | 
			
		||||
    this->set_speed(fan::speed_enum_to_level(FAN_SPEED_MEDIUM, supported_speed_count));
 | 
			
		||||
  } else if (strcasecmp(legacy_speed, "high") == 0) {
 | 
			
		||||
    this->set_speed(fan::speed_enum_to_level(FAN_SPEED_HIGH, supported_speed_count));
 | 
			
		||||
  }
 | 
			
		||||
  return *this;
 | 
			
		||||
}
 | 
			
		||||
#pragma GCC diagnostic pop
 | 
			
		||||
 | 
			
		||||
FanCall FanRestoreState::to_call(Fan &fan) {
 | 
			
		||||
  auto call = fan.make_call();
 | 
			
		||||
  call.set_state(this->state);
 | 
			
		||||
@@ -169,7 +152,6 @@ void Fan::dump_traits_(const char *tag, const char *prefix) {
 | 
			
		||||
  if (this->get_traits().supports_direction())
 | 
			
		||||
    ESP_LOGCONFIG(tag, "%s  Direction: YES", prefix);
 | 
			
		||||
}
 | 
			
		||||
uint32_t Fan::hash_base() { return 418001110UL; }
 | 
			
		||||
 | 
			
		||||
}  // namespace fan
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
@@ -16,13 +16,6 @@ namespace fan {
 | 
			
		||||
    (obj)->dump_traits_(TAG, prefix); \
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
/// Simple enum to represent the speed of a fan. - DEPRECATED - Will be deleted soon
 | 
			
		||||
enum ESPDEPRECATED("FanSpeed is deprecated.", "2021.9") FanSpeed {
 | 
			
		||||
  FAN_SPEED_LOW = 0,     ///< The fan is running on low speed.
 | 
			
		||||
  FAN_SPEED_MEDIUM = 1,  ///< The fan is running on medium speed.
 | 
			
		||||
  FAN_SPEED_HIGH = 2     ///< The fan is running on high/full speed.
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/// Simple enum to represent the direction of a fan.
 | 
			
		||||
enum class FanDirection { FORWARD = 0, REVERSE = 1 };
 | 
			
		||||
 | 
			
		||||
@@ -143,7 +136,6 @@ class Fan : public EntityBase {
 | 
			
		||||
  void save_state_();
 | 
			
		||||
 | 
			
		||||
  void dump_traits_(const char *tag, const char *prefix);
 | 
			
		||||
  uint32_t hash_base() override;
 | 
			
		||||
 | 
			
		||||
  CallbackManager<void()> state_callback_{};
 | 
			
		||||
  ESPPreferenceObject rtc_;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,23 +0,0 @@
 | 
			
		||||
#include <cassert>
 | 
			
		||||
#include "fan_helpers.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace fan {
 | 
			
		||||
 | 
			
		||||
// This whole file is deprecated, don't warn about usage of deprecated types in here.
 | 
			
		||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
 | 
			
		||||
 | 
			
		||||
FanSpeed speed_level_to_enum(int speed_level, int supported_speed_levels) {
 | 
			
		||||
  const auto speed_ratio = static_cast<float>(speed_level) / (supported_speed_levels + 1);
 | 
			
		||||
  const auto legacy_level = clamp<int>(static_cast<int>(ceilf(speed_ratio * 3)), 1, 3);
 | 
			
		||||
  return static_cast<FanSpeed>(legacy_level - 1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int speed_enum_to_level(FanSpeed speed, int supported_speed_levels) {
 | 
			
		||||
  const auto enum_level = static_cast<int>(speed) + 1;
 | 
			
		||||
  const auto speed_level = roundf(enum_level / 3.0f * supported_speed_levels);
 | 
			
		||||
  return static_cast<int>(speed_level);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace fan
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -1,20 +0,0 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "fan.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace fan {
 | 
			
		||||
 | 
			
		||||
// Shut-up about usage of deprecated FanSpeed for a bit.
 | 
			
		||||
#pragma GCC diagnostic push
 | 
			
		||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
 | 
			
		||||
 | 
			
		||||
ESPDEPRECATED("FanSpeed and speed_level_to_enum() are deprecated.", "2021.9")
 | 
			
		||||
FanSpeed speed_level_to_enum(int speed_level, int supported_speed_levels);
 | 
			
		||||
ESPDEPRECATED("FanSpeed and speed_enum_to_level() are deprecated.", "2021.9")
 | 
			
		||||
int speed_enum_to_level(FanSpeed speed, int supported_speed_levels);
 | 
			
		||||
 | 
			
		||||
#pragma GCC diagnostic pop
 | 
			
		||||
 | 
			
		||||
}  // namespace fan
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -1,5 +1,4 @@
 | 
			
		||||
#include "hbridge_fan.h"
 | 
			
		||||
#include "esphome/components/fan/fan_helpers.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
 
 | 
			
		||||
@@ -195,7 +195,7 @@ void HydreonRGxxComponent::process_line_() {
 | 
			
		||||
      if (n == std::string::npos) {
 | 
			
		||||
        continue;
 | 
			
		||||
      }
 | 
			
		||||
      int data = strtol(this->buffer_.substr(n + strlen(PROTOCOL_NAMES[i])).c_str(), nullptr, 10);
 | 
			
		||||
      float data = strtof(this->buffer_.substr(n + strlen(PROTOCOL_NAMES[i])).c_str(), nullptr);
 | 
			
		||||
      this->sensors_[i]->publish_state(data);
 | 
			
		||||
      ESP_LOGD(TAG, "Received %s: %f", PROTOCOL_NAMES[i], this->sensors_[i]->get_raw_state());
 | 
			
		||||
      this->sensors_received_ |= (1 << i);
 | 
			
		||||
 
 | 
			
		||||
@@ -37,7 +37,7 @@ SUPPORTED_SENSORS = {
 | 
			
		||||
PROTOCOL_NAMES = {
 | 
			
		||||
    CONF_MOISTURE: "R",
 | 
			
		||||
    CONF_ACC: "Acc",
 | 
			
		||||
    CONF_R_INT: "Rint",
 | 
			
		||||
    CONF_R_INT: "RInt",
 | 
			
		||||
    CONF_EVENT_ACC: "EventAcc",
 | 
			
		||||
    CONF_TOTAL_ACC: "TotalAcc",
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										0
									
								
								esphome/components/i2s_audio/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								esphome/components/i2s_audio/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										160
									
								
								esphome/components/i2s_audio/i2s_audio_media_player.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										160
									
								
								esphome/components/i2s_audio/i2s_audio_media_player.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,160 @@
 | 
			
		||||
#include "i2s_audio_media_player.h"
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32_FRAMEWORK_ARDUINO
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace i2s_audio {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "audio";
 | 
			
		||||
 | 
			
		||||
void I2SAudioMediaPlayer::control(const media_player::MediaPlayerCall &call) {
 | 
			
		||||
  if (call.get_media_url().has_value()) {
 | 
			
		||||
    if (this->audio_->isRunning())
 | 
			
		||||
      this->audio_->stopSong();
 | 
			
		||||
    this->high_freq_.start();
 | 
			
		||||
    this->audio_->connecttohost(call.get_media_url().value().c_str());
 | 
			
		||||
    this->state = media_player::MEDIA_PLAYER_STATE_PLAYING;
 | 
			
		||||
  }
 | 
			
		||||
  if (call.get_volume().has_value()) {
 | 
			
		||||
    this->volume = call.get_volume().value();
 | 
			
		||||
    this->set_volume_(volume);
 | 
			
		||||
    this->unmute_();
 | 
			
		||||
  }
 | 
			
		||||
  if (call.get_command().has_value()) {
 | 
			
		||||
    switch (call.get_command().value()) {
 | 
			
		||||
      case media_player::MEDIA_PLAYER_COMMAND_PLAY:
 | 
			
		||||
        if (!this->audio_->isRunning())
 | 
			
		||||
          this->audio_->pauseResume();
 | 
			
		||||
        this->state = media_player::MEDIA_PLAYER_STATE_PLAYING;
 | 
			
		||||
        break;
 | 
			
		||||
      case media_player::MEDIA_PLAYER_COMMAND_PAUSE:
 | 
			
		||||
        if (this->audio_->isRunning())
 | 
			
		||||
          this->audio_->pauseResume();
 | 
			
		||||
        this->state = media_player::MEDIA_PLAYER_STATE_PAUSED;
 | 
			
		||||
        break;
 | 
			
		||||
      case media_player::MEDIA_PLAYER_COMMAND_STOP:
 | 
			
		||||
        this->stop_();
 | 
			
		||||
        break;
 | 
			
		||||
      case media_player::MEDIA_PLAYER_COMMAND_MUTE:
 | 
			
		||||
        this->mute_();
 | 
			
		||||
        break;
 | 
			
		||||
      case media_player::MEDIA_PLAYER_COMMAND_UNMUTE:
 | 
			
		||||
        this->unmute_();
 | 
			
		||||
        break;
 | 
			
		||||
      case media_player::MEDIA_PLAYER_COMMAND_TOGGLE:
 | 
			
		||||
        this->audio_->pauseResume();
 | 
			
		||||
        if (this->audio_->isRunning()) {
 | 
			
		||||
          this->state = media_player::MEDIA_PLAYER_STATE_PLAYING;
 | 
			
		||||
        } else {
 | 
			
		||||
          this->state = media_player::MEDIA_PLAYER_STATE_PAUSED;
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
      case media_player::MEDIA_PLAYER_COMMAND_VOLUME_UP: {
 | 
			
		||||
        float new_volume = this->volume + 0.1f;
 | 
			
		||||
        if (new_volume > 1.0f)
 | 
			
		||||
          new_volume = 1.0f;
 | 
			
		||||
        this->set_volume_(new_volume);
 | 
			
		||||
        this->unmute_();
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      case media_player::MEDIA_PLAYER_COMMAND_VOLUME_DOWN: {
 | 
			
		||||
        float new_volume = this->volume - 0.1f;
 | 
			
		||||
        if (new_volume < 0.0f)
 | 
			
		||||
          new_volume = 0.0f;
 | 
			
		||||
        this->set_volume_(new_volume);
 | 
			
		||||
        this->unmute_();
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  this->publish_state();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void I2SAudioMediaPlayer::mute_() {
 | 
			
		||||
  if (this->mute_pin_ != nullptr) {
 | 
			
		||||
    this->mute_pin_->digital_write(true);
 | 
			
		||||
  } else {
 | 
			
		||||
    this->set_volume_(0.0f, false);
 | 
			
		||||
  }
 | 
			
		||||
  this->muted_ = true;
 | 
			
		||||
}
 | 
			
		||||
void I2SAudioMediaPlayer::unmute_() {
 | 
			
		||||
  if (this->mute_pin_ != nullptr) {
 | 
			
		||||
    this->mute_pin_->digital_write(false);
 | 
			
		||||
  } else {
 | 
			
		||||
    this->set_volume_(this->volume, false);
 | 
			
		||||
  }
 | 
			
		||||
  this->muted_ = false;
 | 
			
		||||
}
 | 
			
		||||
void I2SAudioMediaPlayer::set_volume_(float volume, bool publish) {
 | 
			
		||||
  this->audio_->setVolume(remap<uint8_t, float>(volume, 0.0f, 1.0f, 0, 21));
 | 
			
		||||
  if (publish)
 | 
			
		||||
    this->volume = volume;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void I2SAudioMediaPlayer::stop_() {
 | 
			
		||||
  if (this->audio_->isRunning())
 | 
			
		||||
    this->audio_->stopSong();
 | 
			
		||||
  this->high_freq_.stop();
 | 
			
		||||
  this->state = media_player::MEDIA_PLAYER_STATE_IDLE;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void I2SAudioMediaPlayer::setup() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Setting up Audio...");
 | 
			
		||||
  if (this->internal_dac_mode_ != I2S_DAC_CHANNEL_DISABLE) {
 | 
			
		||||
    this->audio_ = make_unique<Audio>(true, this->internal_dac_mode_);
 | 
			
		||||
  } else {
 | 
			
		||||
    this->audio_ = make_unique<Audio>(false);
 | 
			
		||||
    this->audio_->setPinout(this->bclk_pin_, this->lrclk_pin_, this->dout_pin_);
 | 
			
		||||
    this->audio_->forceMono(this->external_dac_channels_ == 1);
 | 
			
		||||
    if (this->mute_pin_ != nullptr) {
 | 
			
		||||
      this->mute_pin_->setup();
 | 
			
		||||
      this->mute_pin_->digital_write(false);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  this->state = media_player::MEDIA_PLAYER_STATE_IDLE;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void I2SAudioMediaPlayer::loop() {
 | 
			
		||||
  this->audio_->loop();
 | 
			
		||||
  if (this->state == media_player::MEDIA_PLAYER_STATE_PLAYING && !this->audio_->isRunning()) {
 | 
			
		||||
    this->stop_();
 | 
			
		||||
    this->publish_state();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
media_player::MediaPlayerTraits I2SAudioMediaPlayer::get_traits() {
 | 
			
		||||
  auto traits = media_player::MediaPlayerTraits();
 | 
			
		||||
  traits.set_supports_pause(true);
 | 
			
		||||
  return traits;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
void I2SAudioMediaPlayer::dump_config() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Audio:");
 | 
			
		||||
  if (this->is_failed()) {
 | 
			
		||||
    ESP_LOGCONFIG(TAG, "Audio failed to initialize!");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (this->internal_dac_mode_ != I2S_DAC_CHANNEL_DISABLE) {
 | 
			
		||||
    switch (this->internal_dac_mode_) {
 | 
			
		||||
      case I2S_DAC_CHANNEL_LEFT_EN:
 | 
			
		||||
        ESP_LOGCONFIG(TAG, "  Internal DAC mode: Left");
 | 
			
		||||
        break;
 | 
			
		||||
      case I2S_DAC_CHANNEL_RIGHT_EN:
 | 
			
		||||
        ESP_LOGCONFIG(TAG, "  Internal DAC mode: Right");
 | 
			
		||||
        break;
 | 
			
		||||
      case I2S_DAC_CHANNEL_BOTH_EN:
 | 
			
		||||
        ESP_LOGCONFIG(TAG, "  Internal DAC mode: Left & Right");
 | 
			
		||||
        break;
 | 
			
		||||
      default:
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace i2s_audio
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 | 
			
		||||
#endif  // USE_ESP32_FRAMEWORK_ARDUINO
 | 
			
		||||
							
								
								
									
										63
									
								
								esphome/components/i2s_audio/i2s_audio_media_player.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								esphome/components/i2s_audio/i2s_audio_media_player.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,63 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32_FRAMEWORK_ARDUINO
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/media_player/media_player.h"
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/core/gpio.h"
 | 
			
		||||
#include "esphome/core/helpers.h"
 | 
			
		||||
 | 
			
		||||
#include <Audio.h>
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace i2s_audio {
 | 
			
		||||
 | 
			
		||||
class I2SAudioMediaPlayer : public Component, public media_player::MediaPlayer {
 | 
			
		||||
 public:
 | 
			
		||||
  void setup() override;
 | 
			
		||||
  float get_setup_priority() const override { return esphome::setup_priority::LATE; }
 | 
			
		||||
 | 
			
		||||
  void loop() override;
 | 
			
		||||
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
 | 
			
		||||
  void set_dout_pin(uint8_t pin) { this->dout_pin_ = pin; }
 | 
			
		||||
  void set_bclk_pin(uint8_t pin) { this->bclk_pin_ = pin; }
 | 
			
		||||
  void set_lrclk_pin(uint8_t pin) { this->lrclk_pin_ = pin; }
 | 
			
		||||
  void set_mute_pin(GPIOPin *mute_pin) { this->mute_pin_ = mute_pin; }
 | 
			
		||||
  void set_internal_dac_mode(i2s_dac_mode_t mode) { this->internal_dac_mode_ = mode; }
 | 
			
		||||
  void set_external_dac_channels(uint8_t channels) { this->external_dac_channels_ = channels; }
 | 
			
		||||
 | 
			
		||||
  media_player::MediaPlayerTraits get_traits() override;
 | 
			
		||||
 | 
			
		||||
  bool is_muted() const override { return this->muted_; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void control(const media_player::MediaPlayerCall &call) override;
 | 
			
		||||
 | 
			
		||||
  void mute_();
 | 
			
		||||
  void unmute_();
 | 
			
		||||
  void set_volume_(float volume, bool publish = true);
 | 
			
		||||
  void stop_();
 | 
			
		||||
 | 
			
		||||
  std::unique_ptr<Audio> audio_;
 | 
			
		||||
 | 
			
		||||
  uint8_t dout_pin_{0};
 | 
			
		||||
  uint8_t din_pin_{0};
 | 
			
		||||
  uint8_t bclk_pin_;
 | 
			
		||||
  uint8_t lrclk_pin_;
 | 
			
		||||
 | 
			
		||||
  GPIOPin *mute_pin_{nullptr};
 | 
			
		||||
  bool muted_{false};
 | 
			
		||||
  float unmuted_volume_{0};
 | 
			
		||||
 | 
			
		||||
  i2s_dac_mode_t internal_dac_mode_{I2S_DAC_CHANNEL_DISABLE};
 | 
			
		||||
  uint8_t external_dac_channels_;
 | 
			
		||||
 | 
			
		||||
  HighFrequencyLoopRequester high_freq_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace i2s_audio
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 | 
			
		||||
#endif  // USE_ESP32_FRAMEWORK_ARDUINO
 | 
			
		||||
							
								
								
									
										94
									
								
								esphome/components/i2s_audio/media_player.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								esphome/components/i2s_audio/media_player.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,94 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
from esphome.components import media_player
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
 | 
			
		||||
from esphome import pins
 | 
			
		||||
 | 
			
		||||
from esphome.const import CONF_ID, CONF_MODE
 | 
			
		||||
from esphome.core import CORE
 | 
			
		||||
 | 
			
		||||
CODEOWNERS = ["@jesserockz"]
 | 
			
		||||
DEPENDENCIES = ["esp32"]
 | 
			
		||||
 | 
			
		||||
i2s_audio_ns = cg.esphome_ns.namespace("i2s_audio")
 | 
			
		||||
 | 
			
		||||
I2SAudioMediaPlayer = i2s_audio_ns.class_(
 | 
			
		||||
    "I2SAudioMediaPlayer", cg.Component, media_player.MediaPlayer
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
i2s_dac_mode_t = cg.global_ns.enum("i2s_dac_mode_t")
 | 
			
		||||
 | 
			
		||||
CONF_I2S_DOUT_PIN = "i2s_dout_pin"
 | 
			
		||||
CONF_I2S_BCLK_PIN = "i2s_bclk_pin"
 | 
			
		||||
CONF_I2S_LRCLK_PIN = "i2s_lrclk_pin"
 | 
			
		||||
CONF_MUTE_PIN = "mute_pin"
 | 
			
		||||
CONF_AUDIO_ID = "audio_id"
 | 
			
		||||
CONF_DAC_TYPE = "dac_type"
 | 
			
		||||
 | 
			
		||||
INTERNAL_DAC_OPTIONS = {
 | 
			
		||||
    "left": i2s_dac_mode_t.I2S_DAC_CHANNEL_LEFT_EN,
 | 
			
		||||
    "right": i2s_dac_mode_t.I2S_DAC_CHANNEL_RIGHT_EN,
 | 
			
		||||
    "stereo": i2s_dac_mode_t.I2S_DAC_CHANNEL_BOTH_EN,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
EXTERNAL_DAC_OPTIONS = ["mono", "stereo"]
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = cv.All(
 | 
			
		||||
    cv.typed_schema(
 | 
			
		||||
        {
 | 
			
		||||
            "internal": cv.Schema(
 | 
			
		||||
                {
 | 
			
		||||
                    cv.GenerateID(): cv.declare_id(I2SAudioMediaPlayer),
 | 
			
		||||
                    cv.Required(CONF_MODE): cv.enum(INTERNAL_DAC_OPTIONS, lower=True),
 | 
			
		||||
                }
 | 
			
		||||
            )
 | 
			
		||||
            .extend(media_player.MEDIA_PLAYER_SCHEMA)
 | 
			
		||||
            .extend(cv.COMPONENT_SCHEMA),
 | 
			
		||||
            "external": cv.Schema(
 | 
			
		||||
                {
 | 
			
		||||
                    cv.GenerateID(): cv.declare_id(I2SAudioMediaPlayer),
 | 
			
		||||
                    cv.Required(
 | 
			
		||||
                        CONF_I2S_DOUT_PIN
 | 
			
		||||
                    ): pins.internal_gpio_output_pin_number,
 | 
			
		||||
                    cv.Required(
 | 
			
		||||
                        CONF_I2S_BCLK_PIN
 | 
			
		||||
                    ): pins.internal_gpio_output_pin_number,
 | 
			
		||||
                    cv.Required(
 | 
			
		||||
                        CONF_I2S_LRCLK_PIN
 | 
			
		||||
                    ): pins.internal_gpio_output_pin_number,
 | 
			
		||||
                    cv.Optional(CONF_MUTE_PIN): pins.gpio_output_pin_schema,
 | 
			
		||||
                    cv.Optional(CONF_MODE, default="mono"): cv.one_of(
 | 
			
		||||
                        *EXTERNAL_DAC_OPTIONS, lower=True
 | 
			
		||||
                    ),
 | 
			
		||||
                }
 | 
			
		||||
            )
 | 
			
		||||
            .extend(media_player.MEDIA_PLAYER_SCHEMA)
 | 
			
		||||
            .extend(cv.COMPONENT_SCHEMA),
 | 
			
		||||
        },
 | 
			
		||||
        key=CONF_DAC_TYPE,
 | 
			
		||||
    ),
 | 
			
		||||
    cv.only_with_arduino,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
    await media_player.register_media_player(var, config)
 | 
			
		||||
 | 
			
		||||
    if config[CONF_DAC_TYPE] == "internal":
 | 
			
		||||
        cg.add(var.set_internal_dac_mode(config[CONF_MODE]))
 | 
			
		||||
    else:
 | 
			
		||||
        cg.add(var.set_dout_pin(config[CONF_I2S_DOUT_PIN]))
 | 
			
		||||
        cg.add(var.set_bclk_pin(config[CONF_I2S_BCLK_PIN]))
 | 
			
		||||
        cg.add(var.set_lrclk_pin(config[CONF_I2S_LRCLK_PIN]))
 | 
			
		||||
        if CONF_MUTE_PIN in config:
 | 
			
		||||
            pin = await cg.gpio_pin_expression(config[CONF_MUTE_PIN])
 | 
			
		||||
            cg.add(var.set_mute_pin(pin))
 | 
			
		||||
        cg.add(var.set_external_dac_channels(2 if config[CONF_MODE] == "stereo" else 1))
 | 
			
		||||
 | 
			
		||||
    if CORE.is_esp32:
 | 
			
		||||
        cg.add_library("WiFiClientSecure", None)
 | 
			
		||||
        cg.add_library("HTTPClient", None)
 | 
			
		||||
        cg.add_library("esphome/ESP32-audioI2S", "2.1.0")
 | 
			
		||||
        cg.add_build_flag("-DAUDIO_NO_SD_FS")
 | 
			
		||||
@@ -3,13 +3,16 @@ import esphome.config_validation as cv
 | 
			
		||||
from esphome import pins
 | 
			
		||||
from esphome.components import display, spi
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_COLOR_PALETTE,
 | 
			
		||||
    CONF_DC_PIN,
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    CONF_LAMBDA,
 | 
			
		||||
    CONF_MODEL,
 | 
			
		||||
    CONF_PAGES,
 | 
			
		||||
    CONF_RAW_DATA_ID,
 | 
			
		||||
    CONF_RESET_PIN,
 | 
			
		||||
)
 | 
			
		||||
from esphome.core import HexInt
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["spi"]
 | 
			
		||||
 | 
			
		||||
@@ -21,16 +24,21 @@ ili9341 = ili9341_ns.class_(
 | 
			
		||||
)
 | 
			
		||||
ILI9341M5Stack = ili9341_ns.class_("ILI9341M5Stack", ili9341)
 | 
			
		||||
ILI9341TFT24 = ili9341_ns.class_("ILI9341TFT24", ili9341)
 | 
			
		||||
ILI9341TFT24R = ili9341_ns.class_("ILI9341TFT24R", ili9341)
 | 
			
		||||
 | 
			
		||||
ILI9341Model = ili9341_ns.enum("ILI9341Model")
 | 
			
		||||
ILI9341ColorMode = ili9341_ns.enum("ILI9341ColorMode")
 | 
			
		||||
 | 
			
		||||
MODELS = {
 | 
			
		||||
    "M5STACK": ILI9341Model.M5STACK,
 | 
			
		||||
    "TFT_2.4": ILI9341Model.TFT_24,
 | 
			
		||||
    "TFT_2.4R": ILI9341Model.TFT_24R,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ILI9341_MODEL = cv.enum(MODELS, upper=True, space="_")
 | 
			
		||||
 | 
			
		||||
COLOR_PALETTE = cv.one_of("NONE", "GRAYSCALE")
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = cv.All(
 | 
			
		||||
    display.FULL_DISPLAY_SCHEMA.extend(
 | 
			
		||||
        {
 | 
			
		||||
@@ -39,6 +47,8 @@ CONFIG_SCHEMA = cv.All(
 | 
			
		||||
            cv.Required(CONF_DC_PIN): pins.gpio_output_pin_schema,
 | 
			
		||||
            cv.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema,
 | 
			
		||||
            cv.Optional(CONF_LED_PIN): pins.gpio_output_pin_schema,
 | 
			
		||||
            cv.Optional(CONF_COLOR_PALETTE, default="NONE"): COLOR_PALETTE,
 | 
			
		||||
            cv.GenerateID(CONF_RAW_DATA_ID): cv.declare_id(cg.uint8),
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
    .extend(cv.polling_component_schema("1s"))
 | 
			
		||||
@@ -52,6 +62,8 @@ async def to_code(config):
 | 
			
		||||
        lcd_type = ILI9341M5Stack
 | 
			
		||||
    if config[CONF_MODEL] == "TFT_2.4":
 | 
			
		||||
        lcd_type = ILI9341TFT24
 | 
			
		||||
    if config[CONF_MODEL] == "TFT_2.4R":
 | 
			
		||||
        lcd_type = ILI9341TFT24R
 | 
			
		||||
    rhs = lcd_type.new()
 | 
			
		||||
    var = cg.Pvariable(config[CONF_ID], rhs)
 | 
			
		||||
 | 
			
		||||
@@ -73,3 +85,13 @@ async def to_code(config):
 | 
			
		||||
    if CONF_LED_PIN in config:
 | 
			
		||||
        led_pin = await cg.gpio_pin_expression(config[CONF_LED_PIN])
 | 
			
		||||
        cg.add(var.set_led_pin(led_pin))
 | 
			
		||||
 | 
			
		||||
    if config[CONF_COLOR_PALETTE] == "GRAYSCALE":
 | 
			
		||||
        cg.add(var.set_buffer_color_mode(ILI9341ColorMode.BITS_8_INDEXED))
 | 
			
		||||
        rhs = []
 | 
			
		||||
        for x in range(256):
 | 
			
		||||
            rhs.extend([HexInt(x), HexInt(x), HexInt(x)])
 | 
			
		||||
        prog_arr = cg.progmem_array(config[CONF_RAW_DATA_ID], rhs)
 | 
			
		||||
        cg.add(var.set_palette(prog_arr))
 | 
			
		||||
    else:
 | 
			
		||||
        pass
 | 
			
		||||
 
 | 
			
		||||
@@ -112,29 +112,9 @@ void ILI9341Display::display_() {
 | 
			
		||||
  this->y_high_ = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint16_t ILI9341Display::convert_to_16bit_color_(uint8_t color_8bit) {
 | 
			
		||||
  int r = color_8bit >> 5;
 | 
			
		||||
  int g = (color_8bit >> 2) & 0x07;
 | 
			
		||||
  int b = color_8bit & 0x03;
 | 
			
		||||
  uint16_t color = (r * 0x04) << 11;
 | 
			
		||||
  color |= (g * 0x09) << 5;
 | 
			
		||||
  color |= (b * 0x0A);
 | 
			
		||||
 | 
			
		||||
  return color;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t ILI9341Display::convert_to_8bit_color_(uint16_t color_16bit) {
 | 
			
		||||
  // convert 16bit color to 8 bit buffer
 | 
			
		||||
  uint8_t r = color_16bit >> 11;
 | 
			
		||||
  uint8_t g = (color_16bit >> 5) & 0x3F;
 | 
			
		||||
  uint8_t b = color_16bit & 0x1F;
 | 
			
		||||
 | 
			
		||||
  return ((b / 0x0A) | ((g / 0x09) << 2) | ((r / 0x04) << 5));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ILI9341Display::fill(Color color) {
 | 
			
		||||
  auto color565 = display::ColorUtil::color_to_565(color);
 | 
			
		||||
  memset(this->buffer_, convert_to_8bit_color_(color565), this->get_buffer_length_());
 | 
			
		||||
  uint8_t color332 = display::ColorUtil::color_to_332(color, display::ColorOrder::COLOR_ORDER_RGB);
 | 
			
		||||
  memset(this->buffer_, color332, this->get_buffer_length_());
 | 
			
		||||
  this->x_low_ = 0;
 | 
			
		||||
  this->y_low_ = 0;
 | 
			
		||||
  this->x_high_ = this->get_width_internal() - 1;
 | 
			
		||||
@@ -181,8 +161,13 @@ void HOT ILI9341Display::draw_absolute_pixel_internal(int x, int y, Color color)
 | 
			
		||||
  this->y_high_ = (y > this->y_high_) ? y : this->y_high_;
 | 
			
		||||
 | 
			
		||||
  uint32_t pos = (y * width_) + x;
 | 
			
		||||
  auto color565 = display::ColorUtil::color_to_565(color);
 | 
			
		||||
  buffer_[pos] = convert_to_8bit_color_(color565);
 | 
			
		||||
  if (this->buffer_color_mode_ == BITS_8) {
 | 
			
		||||
    uint8_t color332 = display::ColorUtil::color_to_332(color, display::ColorOrder::COLOR_ORDER_RGB);
 | 
			
		||||
    buffer_[pos] = color332;
 | 
			
		||||
  } else {  // if (this->buffer_color_mode_ == BITS_8_INDEXED) {
 | 
			
		||||
    uint8_t index = display::ColorUtil::color_to_index8_palette888(color, this->palette_);
 | 
			
		||||
    buffer_[pos] = index;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// should return the total size: return this->get_width_internal() * this->get_height_internal() * 2 // 16bit color
 | 
			
		||||
@@ -247,7 +232,13 @@ uint32_t ILI9341Display::buffer_to_transfer_(uint32_t pos, uint32_t sz) {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  for (uint32_t i = 0; i < sz; ++i) {
 | 
			
		||||
    uint16_t color = convert_to_16bit_color_(*src++);
 | 
			
		||||
    uint16_t color;
 | 
			
		||||
    if (this->buffer_color_mode_ == BITS_8) {
 | 
			
		||||
      color = display::ColorUtil::color_to_565(display::ColorUtil::rgb332_to_color(*src++));
 | 
			
		||||
    } else {  //  if (this->buffer_color_mode == BITS_8_INDEXED) {
 | 
			
		||||
      Color col = display::ColorUtil::index8_to_color_palette888(*src++, this->palette_);
 | 
			
		||||
      color = display::ColorUtil::color_to_565(col);
 | 
			
		||||
    }
 | 
			
		||||
    *dst++ = (uint8_t)(color >> 8);
 | 
			
		||||
    *dst++ = (uint8_t) color;
 | 
			
		||||
  }
 | 
			
		||||
@@ -272,5 +263,13 @@ void ILI9341TFT24::initialize() {
 | 
			
		||||
  this->fill_internal_(Color::BLACK);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//   24_TFT rotated display
 | 
			
		||||
void ILI9341TFT24R::initialize() {
 | 
			
		||||
  this->init_lcd_(INITCMD_TFT);
 | 
			
		||||
  this->width_ = 320;
 | 
			
		||||
  this->height_ = 240;
 | 
			
		||||
  this->fill_internal_(Color::BLACK);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace ili9341
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
@@ -12,6 +12,12 @@ namespace ili9341 {
 | 
			
		||||
enum ILI9341Model {
 | 
			
		||||
  M5STACK = 0,
 | 
			
		||||
  TFT_24,
 | 
			
		||||
  TFT_24R,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum ILI9341ColorMode {
 | 
			
		||||
  BITS_8,
 | 
			
		||||
  BITS_8_INDEXED,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class ILI9341Display : public PollingComponent,
 | 
			
		||||
@@ -24,6 +30,8 @@ class ILI9341Display : public PollingComponent,
 | 
			
		||||
  void set_reset_pin(GPIOPin *reset) { this->reset_pin_ = reset; }
 | 
			
		||||
  void set_led_pin(GPIOPin *led) { this->led_pin_ = led; }
 | 
			
		||||
  void set_model(ILI9341Model model) { this->model_ = model; }
 | 
			
		||||
  void set_palette(const uint8_t *palette) { this->palette_ = palette; }
 | 
			
		||||
  void set_buffer_color_mode(ILI9341ColorMode color_mode) { this->buffer_color_mode_ = color_mode; }
 | 
			
		||||
 | 
			
		||||
  void command(uint8_t value);
 | 
			
		||||
  void data(uint8_t value);
 | 
			
		||||
@@ -41,6 +49,8 @@ class ILI9341Display : public PollingComponent,
 | 
			
		||||
    this->initialize();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  display::DisplayType get_display_type() override { return display::DisplayType::DISPLAY_TYPE_COLOR; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void draw_absolute_pixel_internal(int x, int y, Color color) override;
 | 
			
		||||
  void setup_pins_();
 | 
			
		||||
@@ -51,8 +61,6 @@ class ILI9341Display : public PollingComponent,
 | 
			
		||||
  void reset_();
 | 
			
		||||
  void fill_internal_(Color color);
 | 
			
		||||
  void display_();
 | 
			
		||||
  uint16_t convert_to_16bit_color_(uint8_t color_8bit);
 | 
			
		||||
  uint8_t convert_to_8bit_color_(uint16_t color_16bit);
 | 
			
		||||
 | 
			
		||||
  ILI9341Model model_;
 | 
			
		||||
  int16_t width_{320};   ///< Display width as modified by current rotation
 | 
			
		||||
@@ -61,6 +69,9 @@ class ILI9341Display : public PollingComponent,
 | 
			
		||||
  uint16_t y_low_{0};
 | 
			
		||||
  uint16_t x_high_{0};
 | 
			
		||||
  uint16_t y_high_{0};
 | 
			
		||||
  const uint8_t *palette_;
 | 
			
		||||
 | 
			
		||||
  ILI9341ColorMode buffer_color_mode_{BITS_8};
 | 
			
		||||
 | 
			
		||||
  uint32_t get_buffer_length_();
 | 
			
		||||
  int get_width_internal() override;
 | 
			
		||||
@@ -92,5 +103,12 @@ class ILI9341TFT24 : public ILI9341Display {
 | 
			
		||||
 public:
 | 
			
		||||
  void initialize() override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
//-----------   ILI9341_24_TFT rotated display --------------
 | 
			
		||||
class ILI9341TFT24R : public ILI9341Display {
 | 
			
		||||
 public:
 | 
			
		||||
  void initialize() override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace ili9341
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,8 @@
 | 
			
		||||
from esphome.const import CONF_BAUD_RATE, CONF_ID, CONF_LOGGER
 | 
			
		||||
from esphome.components.logger import USB_CDC, USB_SERIAL_JTAG
 | 
			
		||||
from esphome.const import CONF_BAUD_RATE, CONF_HARDWARE_UART, CONF_ID, CONF_LOGGER
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.core import CORE
 | 
			
		||||
import esphome.final_validate as fv
 | 
			
		||||
 | 
			
		||||
CODEOWNERS = ["@esphome/core"]
 | 
			
		||||
@@ -17,14 +19,19 @@ CONFIG_SCHEMA = cv.Schema(
 | 
			
		||||
).extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def validate_logger_baud_rate(config):
 | 
			
		||||
def validate_logger(config):
 | 
			
		||||
    logger_conf = fv.full_config.get()[CONF_LOGGER]
 | 
			
		||||
    if logger_conf[CONF_BAUD_RATE] == 0:
 | 
			
		||||
        raise cv.Invalid("improv_serial requires the logger baud_rate to be not 0")
 | 
			
		||||
    if CORE.using_esp_idf:
 | 
			
		||||
        if logger_conf[CONF_HARDWARE_UART] in [USB_SERIAL_JTAG, USB_CDC]:
 | 
			
		||||
            raise cv.Invalid(
 | 
			
		||||
                "improv_serial does not support the selected logger hardware_uart"
 | 
			
		||||
            )
 | 
			
		||||
    return config
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
FINAL_VALIDATE_SCHEMA = validate_logger_baud_rate
 | 
			
		||||
FINAL_VALIDATE_SCHEMA = validate_logger
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
 
 | 
			
		||||
@@ -55,7 +55,7 @@ bool InkbirdIbstH1Mini::parse_device(const esp32_ble_tracker::ESPBTDevice &devic
 | 
			
		||||
    ESP_LOGVV(TAG, "parse_device(): manufacturer data element length is expected to be of length 7");
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
  if (mnf_data.data[6] != 8) {
 | 
			
		||||
  if ((mnf_data.data[6] != 8) && (mnf_data.data[6] != 6)) {
 | 
			
		||||
    ESP_LOGVV(TAG, "parse_device(): unexpected data");
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -86,6 +86,10 @@ class Inkplate6 : public PollingComponent, public display::DisplayBuffer, public
 | 
			
		||||
 | 
			
		||||
  void block_partial() { this->block_partial_ = true; }
 | 
			
		||||
 | 
			
		||||
  display::DisplayType get_display_type() override {
 | 
			
		||||
    return get_greyscale() ? display::DisplayType::DISPLAY_TYPE_GRAYSCALE : display::DisplayType::DISPLAY_TYPE_BINARY;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void draw_absolute_pixel_internal(int x, int y, Color color) override;
 | 
			
		||||
  void display1b_();
 | 
			
		||||
 
 | 
			
		||||
@@ -26,21 +26,33 @@ std::string build_json(const json_build_t &f) {
 | 
			
		||||
  const size_t free_heap = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  const size_t request_size = std::min(free_heap, (size_t) 512);
 | 
			
		||||
 | 
			
		||||
  DynamicJsonDocument json_document(request_size);
 | 
			
		||||
  if (json_document.capacity() == 0) {
 | 
			
		||||
    ESP_LOGE(TAG, "Could not allocate memory for JSON document! Requested %u bytes, largest free heap block: %u bytes",
 | 
			
		||||
             request_size, free_heap);
 | 
			
		||||
    return "{}";
 | 
			
		||||
  size_t request_size = std::min(free_heap, (size_t) 512);
 | 
			
		||||
  while (true) {
 | 
			
		||||
    ESP_LOGV(TAG, "Attempting to allocate %u bytes for JSON serialization", request_size);
 | 
			
		||||
    DynamicJsonDocument json_document(request_size);
 | 
			
		||||
    if (json_document.capacity() == 0) {
 | 
			
		||||
      ESP_LOGE(TAG,
 | 
			
		||||
               "Could not allocate memory for JSON document! Requested %u bytes, largest free heap block: %u bytes",
 | 
			
		||||
               request_size, free_heap);
 | 
			
		||||
      return "{}";
 | 
			
		||||
    }
 | 
			
		||||
    JsonObject root = json_document.to<JsonObject>();
 | 
			
		||||
    f(root);
 | 
			
		||||
    if (json_document.overflowed()) {
 | 
			
		||||
      if (request_size == free_heap) {
 | 
			
		||||
        ESP_LOGE(TAG, "Could not allocate memory for JSON document! Overflowed largest free heap block: %u bytes",
 | 
			
		||||
                 free_heap);
 | 
			
		||||
        return "{}";
 | 
			
		||||
      }
 | 
			
		||||
      request_size = std::min(request_size * 2, free_heap);
 | 
			
		||||
      continue;
 | 
			
		||||
    }
 | 
			
		||||
    json_document.shrinkToFit();
 | 
			
		||||
    ESP_LOGV(TAG, "Size after shrink %u bytes", json_document.capacity());
 | 
			
		||||
    std::string output;
 | 
			
		||||
    serializeJson(json_document, output);
 | 
			
		||||
    return output;
 | 
			
		||||
  }
 | 
			
		||||
  JsonObject root = json_document.to<JsonObject>();
 | 
			
		||||
  f(root);
 | 
			
		||||
  json_document.shrinkToFit();
 | 
			
		||||
  ESP_LOGV(TAG, "Size after shrink %u bytes", json_document.capacity());
 | 
			
		||||
  std::string output;
 | 
			
		||||
  serializeJson(json_document, output);
 | 
			
		||||
  return output;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void parse_json(const std::string &data, const json_parse_t &f) {
 | 
			
		||||
 
 | 
			
		||||
@@ -145,7 +145,6 @@ void LightState::loop() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float LightState::get_setup_priority() const { return setup_priority::HARDWARE - 1.0f; }
 | 
			
		||||
uint32_t LightState::hash_base() { return 1114400283; }
 | 
			
		||||
 | 
			
		||||
void LightState::publish_state() { this->remote_values_callback_.call(); }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -150,8 +150,6 @@ class LightState : public EntityBase, public Component {
 | 
			
		||||
  friend LightCall;
 | 
			
		||||
  friend class AddressableLight;
 | 
			
		||||
 | 
			
		||||
  uint32_t hash_base() override;
 | 
			
		||||
 | 
			
		||||
  /// Internal method to start an effect with the given index
 | 
			
		||||
  void start_effect_(uint32_t effect_index);
 | 
			
		||||
  /// Internal method to get the currently active effect
 | 
			
		||||
 
 | 
			
		||||
@@ -57,7 +57,6 @@ void Lock::publish_state(LockState state) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Lock::add_on_state_callback(std::function<void()> &&callback) { this->state_callback_.add(std::move(callback)); }
 | 
			
		||||
uint32_t Lock::hash_base() { return 856245656UL; }
 | 
			
		||||
 | 
			
		||||
void LockCall::perform() {
 | 
			
		||||
  ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
 | 
			
		||||
 
 | 
			
		||||
@@ -167,8 +167,6 @@ class Lock : public EntityBase {
 | 
			
		||||
   */
 | 
			
		||||
  virtual void control(const LockCall &call) = 0;
 | 
			
		||||
 | 
			
		||||
  uint32_t hash_base() override;
 | 
			
		||||
 | 
			
		||||
  CallbackManager<void()> state_callback_{};
 | 
			
		||||
  Deduplicator<LockState> publish_dedup_;
 | 
			
		||||
  ESPPreferenceObject rtc_;
 | 
			
		||||
 
 | 
			
		||||
@@ -178,7 +178,8 @@ void Logger::pre_setup() {
 | 
			
		||||
        Serial1.setDebugOutput(ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE);
 | 
			
		||||
#endif
 | 
			
		||||
        break;
 | 
			
		||||
#if defined(USE_ESP32) && !defined(USE_ESP32_VARIANT_ESP32C3) && !defined(USE_ESP32_VARIANT_ESP32S2)
 | 
			
		||||
#if defined(USE_ESP32) && !defined(USE_ESP32_VARIANT_ESP32C3) && !defined(USE_ESP32_VARIANT_ESP32S2) && \
 | 
			
		||||
    !defined(USE_ESP32_VARIANT_ESP32S3)
 | 
			
		||||
      case UART_SELECTION_UART2:
 | 
			
		||||
        this->hw_serial_ = &Serial2;
 | 
			
		||||
        Serial2.begin(this->baud_rate_);
 | 
			
		||||
 
 | 
			
		||||
@@ -94,6 +94,14 @@ void MAX31865Sensor::read_data_() {
 | 
			
		||||
  const uint16_t rtd_resistance_register = this->read_register_16_(RTD_RESISTANCE_MSB_REG);
 | 
			
		||||
  this->write_config_(0b11000000, 0b00000000);
 | 
			
		||||
 | 
			
		||||
  // Check for bad connection
 | 
			
		||||
  if (rtd_resistance_register == 0b0000000000000000 || rtd_resistance_register == 0b1111111111111111) {
 | 
			
		||||
    ESP_LOGE(TAG, "SPI bus read all 0 or all 1 (0x%04X), check MAX31865 wiring & power.", rtd_resistance_register);
 | 
			
		||||
    this->publish_state(NAN);
 | 
			
		||||
    this->status_set_error();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Check faults
 | 
			
		||||
  const uint8_t faults = this->read_register_(FAULT_STATUS_REG);
 | 
			
		||||
  if ((has_fault_ = faults & 0b00111100)) {
 | 
			
		||||
 
 | 
			
		||||
@@ -11,6 +11,9 @@ from esphome.const import (
 | 
			
		||||
    UNIT_CELSIUS,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CODEOWNERS = ["@DAVe3283"]
 | 
			
		||||
DEPENDENCIES = ["spi"]
 | 
			
		||||
 | 
			
		||||
max31865_ns = cg.esphome_ns.namespace("max31865")
 | 
			
		||||
MAX31865Sensor = max31865_ns.class_(
 | 
			
		||||
    "MAX31865Sensor", sensor.Sensor, cg.PollingComponent, spi.SPIDevice
 | 
			
		||||
 
 | 
			
		||||
@@ -93,6 +93,8 @@ class MAX7219Component : public PollingComponent,
 | 
			
		||||
  uint8_t strftimedigit(const char *format, time::ESPTime time) __attribute__((format(strftime, 2, 0)));
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  display::DisplayType get_display_type() override { return display::DisplayType::DISPLAY_TYPE_BINARY; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void send_byte_(uint8_t a_register, uint8_t data);
 | 
			
		||||
  void send_to_all_(uint8_t a_register, uint8_t data);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										179
									
								
								esphome/components/media_player/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										179
									
								
								esphome/components/media_player/__init__.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,179 @@
 | 
			
		||||
from esphome import automation
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
 | 
			
		||||
from esphome.automation import maybe_simple_id
 | 
			
		||||
from esphome.const import CONF_ID, CONF_ON_STATE, CONF_TRIGGER_ID
 | 
			
		||||
from esphome.core import CORE
 | 
			
		||||
from esphome.coroutine import coroutine_with_priority
 | 
			
		||||
from esphome.cpp_helpers import setup_entity
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
CODEOWNERS = ["@jesserockz"]
 | 
			
		||||
 | 
			
		||||
IS_PLATFORM_COMPONENT = True
 | 
			
		||||
 | 
			
		||||
media_player_ns = cg.esphome_ns.namespace("media_player")
 | 
			
		||||
 | 
			
		||||
MediaPlayer = media_player_ns.class_("MediaPlayer")
 | 
			
		||||
 | 
			
		||||
PlayAction = media_player_ns.class_(
 | 
			
		||||
    "PlayAction", automation.Action, cg.Parented.template(MediaPlayer)
 | 
			
		||||
)
 | 
			
		||||
PlayMediaAction = media_player_ns.class_(
 | 
			
		||||
    "PlayMediaAction", automation.Action, cg.Parented.template(MediaPlayer)
 | 
			
		||||
)
 | 
			
		||||
ToggleAction = media_player_ns.class_(
 | 
			
		||||
    "ToggleAction", automation.Action, cg.Parented.template(MediaPlayer)
 | 
			
		||||
)
 | 
			
		||||
PauseAction = media_player_ns.class_(
 | 
			
		||||
    "PauseAction", automation.Action, cg.Parented.template(MediaPlayer)
 | 
			
		||||
)
 | 
			
		||||
StopAction = media_player_ns.class_(
 | 
			
		||||
    "StopAction", automation.Action, cg.Parented.template(MediaPlayer)
 | 
			
		||||
)
 | 
			
		||||
VolumeUpAction = media_player_ns.class_(
 | 
			
		||||
    "VolumeUpAction", automation.Action, cg.Parented.template(MediaPlayer)
 | 
			
		||||
)
 | 
			
		||||
VolumeDownAction = media_player_ns.class_(
 | 
			
		||||
    "VolumeDownAction", automation.Action, cg.Parented.template(MediaPlayer)
 | 
			
		||||
)
 | 
			
		||||
VolumeSetAction = media_player_ns.class_(
 | 
			
		||||
    "VolumeSetAction", automation.Action, cg.Parented.template(MediaPlayer)
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
CONF_VOLUME = "volume"
 | 
			
		||||
CONF_ON_IDLE = "on_idle"
 | 
			
		||||
CONF_ON_PLAY = "on_play"
 | 
			
		||||
CONF_ON_PAUSE = "on_pause"
 | 
			
		||||
CONF_MEDIA_URL = "media_url"
 | 
			
		||||
 | 
			
		||||
StateTrigger = media_player_ns.class_("StateTrigger", automation.Trigger.template())
 | 
			
		||||
IdleTrigger = media_player_ns.class_("IdleTrigger", automation.Trigger.template())
 | 
			
		||||
PlayTrigger = media_player_ns.class_("PlayTrigger", automation.Trigger.template())
 | 
			
		||||
PauseTrigger = media_player_ns.class_("PauseTrigger", automation.Trigger.template())
 | 
			
		||||
IsIdleCondition = media_player_ns.class_("IsIdleCondition", automation.Condition)
 | 
			
		||||
IsPlayingCondition = media_player_ns.class_("IsPlayingCondition", automation.Condition)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def setup_media_player_core_(var, config):
 | 
			
		||||
    await setup_entity(var, config)
 | 
			
		||||
    for conf in config.get(CONF_ON_STATE, []):
 | 
			
		||||
        trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
 | 
			
		||||
        await automation.build_automation(trigger, [], conf)
 | 
			
		||||
    for conf in config.get(CONF_ON_IDLE, []):
 | 
			
		||||
        trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
 | 
			
		||||
        await automation.build_automation(trigger, [], conf)
 | 
			
		||||
    for conf in config.get(CONF_ON_PLAY, []):
 | 
			
		||||
        trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
 | 
			
		||||
        await automation.build_automation(trigger, [], conf)
 | 
			
		||||
    for conf in config.get(CONF_ON_PAUSE, []):
 | 
			
		||||
        trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
 | 
			
		||||
        await automation.build_automation(trigger, [], conf)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def register_media_player(var, config):
 | 
			
		||||
    if not CORE.has_id(config[CONF_ID]):
 | 
			
		||||
        var = cg.Pvariable(config[CONF_ID], var)
 | 
			
		||||
    cg.add(cg.App.register_media_player(var))
 | 
			
		||||
    await setup_media_player_core_(var, config)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
MEDIA_PLAYER_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(
 | 
			
		||||
    {
 | 
			
		||||
        cv.Optional(CONF_ON_STATE): automation.validate_automation(
 | 
			
		||||
            {
 | 
			
		||||
                cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(StateTrigger),
 | 
			
		||||
            }
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_ON_IDLE): automation.validate_automation(
 | 
			
		||||
            {
 | 
			
		||||
                cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(IdleTrigger),
 | 
			
		||||
            }
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_ON_PLAY): automation.validate_automation(
 | 
			
		||||
            {
 | 
			
		||||
                cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(PlayTrigger),
 | 
			
		||||
            }
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_ON_PAUSE): automation.validate_automation(
 | 
			
		||||
            {
 | 
			
		||||
                cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(PauseTrigger),
 | 
			
		||||
            }
 | 
			
		||||
        ),
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
MEDIA_PLAYER_ACTION_SCHEMA = maybe_simple_id({cv.GenerateID(): cv.use_id(MediaPlayer)})
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@automation.register_action(
 | 
			
		||||
    "media_player.play_media",
 | 
			
		||||
    PlayMediaAction,
 | 
			
		||||
    cv.maybe_simple_value(
 | 
			
		||||
        {
 | 
			
		||||
            cv.GenerateID(): cv.use_id(MediaPlayer),
 | 
			
		||||
            cv.Required(CONF_MEDIA_URL): cv.templatable(cv.url),
 | 
			
		||||
        },
 | 
			
		||||
        key=CONF_MEDIA_URL,
 | 
			
		||||
    ),
 | 
			
		||||
)
 | 
			
		||||
async def media_player_play_media_action(config, action_id, template_arg, args):
 | 
			
		||||
    var = cg.new_Pvariable(action_id, template_arg)
 | 
			
		||||
    await cg.register_parented(var, config[CONF_ID])
 | 
			
		||||
    media_url = await cg.templatable(config[CONF_MEDIA_URL], args, cg.std_string)
 | 
			
		||||
    cg.add(var.set_media_url(media_url))
 | 
			
		||||
    return var
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@automation.register_action("media_player.play", PlayAction, MEDIA_PLAYER_ACTION_SCHEMA)
 | 
			
		||||
@automation.register_action(
 | 
			
		||||
    "media_player.toggle", ToggleAction, MEDIA_PLAYER_ACTION_SCHEMA
 | 
			
		||||
)
 | 
			
		||||
@automation.register_action(
 | 
			
		||||
    "media_player.pause", PauseAction, MEDIA_PLAYER_ACTION_SCHEMA
 | 
			
		||||
)
 | 
			
		||||
@automation.register_action("media_player.stop", StopAction, MEDIA_PLAYER_ACTION_SCHEMA)
 | 
			
		||||
@automation.register_action(
 | 
			
		||||
    "media_player.volume_up", VolumeUpAction, MEDIA_PLAYER_ACTION_SCHEMA
 | 
			
		||||
)
 | 
			
		||||
@automation.register_action(
 | 
			
		||||
    "media_player.volume_down", VolumeDownAction, MEDIA_PLAYER_ACTION_SCHEMA
 | 
			
		||||
)
 | 
			
		||||
@automation.register_condition(
 | 
			
		||||
    "media_player.is_idle", IsIdleCondition, MEDIA_PLAYER_ACTION_SCHEMA
 | 
			
		||||
)
 | 
			
		||||
@automation.register_condition(
 | 
			
		||||
    "media_player.is_playing", IsPlayingCondition, MEDIA_PLAYER_ACTION_SCHEMA
 | 
			
		||||
)
 | 
			
		||||
async def media_player_action(config, action_id, template_arg, args):
 | 
			
		||||
    var = cg.new_Pvariable(action_id, template_arg)
 | 
			
		||||
    await cg.register_parented(var, config[CONF_ID])
 | 
			
		||||
    return var
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@automation.register_action(
 | 
			
		||||
    "media_player.volume_set",
 | 
			
		||||
    VolumeSetAction,
 | 
			
		||||
    cv.maybe_simple_value(
 | 
			
		||||
        {
 | 
			
		||||
            cv.GenerateID(): cv.use_id(MediaPlayer),
 | 
			
		||||
            cv.Required(CONF_VOLUME): cv.templatable(cv.percentage),
 | 
			
		||||
        },
 | 
			
		||||
        key=CONF_VOLUME,
 | 
			
		||||
    ),
 | 
			
		||||
)
 | 
			
		||||
async def media_player_volume_set_action(config, action_id, template_arg, args):
 | 
			
		||||
    var = cg.new_Pvariable(action_id, template_arg)
 | 
			
		||||
    await cg.register_parented(var, config[CONF_ID])
 | 
			
		||||
    volume = await cg.templatable(config[CONF_VOLUME], args, float)
 | 
			
		||||
    cg.add(var.set_volume(volume))
 | 
			
		||||
    return var
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@coroutine_with_priority(100.0)
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    cg.add_global(media_player_ns.using)
 | 
			
		||||
    cg.add_define("USE_MEDIA_PLAYER")
 | 
			
		||||
							
								
								
									
										67
									
								
								esphome/components/media_player/automation.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								esphome/components/media_player/automation.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,67 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/automation.h"
 | 
			
		||||
#include "media_player.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
 | 
			
		||||
namespace media_player {
 | 
			
		||||
 | 
			
		||||
#define MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(ACTION_CLASS, ACTION_COMMAND) \
 | 
			
		||||
  template<typename... Ts> class ACTION_CLASS : public Action<Ts...>, public Parented<MediaPlayer> { \
 | 
			
		||||
    void play(Ts... x) override { \
 | 
			
		||||
      this->parent_->make_call().set_command(MediaPlayerCommand::MEDIA_PLAYER_COMMAND_##ACTION_COMMAND).perform(); \
 | 
			
		||||
    } \
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
#define MEDIA_PLAYER_SIMPLE_STATE_TRIGGER(TRIGGER_CLASS, TRIGGER_STATE) \
 | 
			
		||||
  class TRIGGER_CLASS : public Trigger<> { \
 | 
			
		||||
   public: \
 | 
			
		||||
    explicit TRIGGER_CLASS(MediaPlayer *player) { \
 | 
			
		||||
      player->add_on_state_callback([this, player]() { \
 | 
			
		||||
        if (player->state == MediaPlayerState::MEDIA_PLAYER_STATE_##TRIGGER_STATE) \
 | 
			
		||||
          this->trigger(); \
 | 
			
		||||
      }); \
 | 
			
		||||
    } \
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(PlayAction, PLAY)
 | 
			
		||||
MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(PauseAction, PAUSE)
 | 
			
		||||
MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(StopAction, STOP)
 | 
			
		||||
MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(ToggleAction, TOGGLE)
 | 
			
		||||
MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(VolumeUpAction, VOLUME_UP)
 | 
			
		||||
MEDIA_PLAYER_SIMPLE_COMMAND_ACTION(VolumeDownAction, VOLUME_DOWN)
 | 
			
		||||
 | 
			
		||||
template<typename... Ts> class PlayMediaAction : public Action<Ts...>, public Parented<MediaPlayer> {
 | 
			
		||||
  TEMPLATABLE_VALUE(std::string, media_url)
 | 
			
		||||
  void play(Ts... x) override { this->parent_->make_call().set_media_url(this->media_url_.value(x...)).perform(); }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
template<typename... Ts> class VolumeSetAction : public Action<Ts...>, public Parented<MediaPlayer> {
 | 
			
		||||
  TEMPLATABLE_VALUE(float, volume)
 | 
			
		||||
  void play(Ts... x) override { this->parent_->make_call().set_volume(this->volume_.value(x...)).perform(); }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class StateTrigger : public Trigger<> {
 | 
			
		||||
 public:
 | 
			
		||||
  explicit StateTrigger(MediaPlayer *player) {
 | 
			
		||||
    player->add_on_state_callback([this]() { this->trigger(); });
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
MEDIA_PLAYER_SIMPLE_STATE_TRIGGER(IdleTrigger, IDLE)
 | 
			
		||||
MEDIA_PLAYER_SIMPLE_STATE_TRIGGER(PlayTrigger, PLAYING)
 | 
			
		||||
MEDIA_PLAYER_SIMPLE_STATE_TRIGGER(PauseTrigger, PAUSED)
 | 
			
		||||
 | 
			
		||||
template<typename... Ts> class IsIdleCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
 | 
			
		||||
 public:
 | 
			
		||||
  bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_IDLE; }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
template<typename... Ts> class IsPlayingCondition : public Condition<Ts...>, public Parented<MediaPlayer> {
 | 
			
		||||
 public:
 | 
			
		||||
  bool check(Ts... x) override { return this->parent_->state == MediaPlayerState::MEDIA_PLAYER_STATE_PLAYING; }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace media_player
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										118
									
								
								esphome/components/media_player/media_player.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										118
									
								
								esphome/components/media_player/media_player.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,118 @@
 | 
			
		||||
#include "media_player.h"
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace media_player {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "media_player";
 | 
			
		||||
 | 
			
		||||
const char *media_player_state_to_string(MediaPlayerState state) {
 | 
			
		||||
  switch (state) {
 | 
			
		||||
    case MEDIA_PLAYER_STATE_IDLE:
 | 
			
		||||
      return "IDLE";
 | 
			
		||||
    case MEDIA_PLAYER_STATE_PLAYING:
 | 
			
		||||
      return "PLAYING";
 | 
			
		||||
    case MEDIA_PLAYER_STATE_PAUSED:
 | 
			
		||||
      return "PAUSED";
 | 
			
		||||
    case MEDIA_PLAYER_STATE_NONE:
 | 
			
		||||
    default:
 | 
			
		||||
      return "UNKNOWN";
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const char *media_player_command_to_string(MediaPlayerCommand command) {
 | 
			
		||||
  switch (command) {
 | 
			
		||||
    case MEDIA_PLAYER_COMMAND_PLAY:
 | 
			
		||||
      return "PLAY";
 | 
			
		||||
    case MEDIA_PLAYER_COMMAND_PAUSE:
 | 
			
		||||
      return "PAUSE";
 | 
			
		||||
    case MEDIA_PLAYER_COMMAND_STOP:
 | 
			
		||||
      return "STOP";
 | 
			
		||||
    case MEDIA_PLAYER_COMMAND_MUTE:
 | 
			
		||||
      return "MUTE";
 | 
			
		||||
    case MEDIA_PLAYER_COMMAND_UNMUTE:
 | 
			
		||||
      return "UNMUTE";
 | 
			
		||||
    case MEDIA_PLAYER_COMMAND_TOGGLE:
 | 
			
		||||
      return "TOGGLE";
 | 
			
		||||
    default:
 | 
			
		||||
      return "UNKNOWN";
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MediaPlayerCall::validate_() {
 | 
			
		||||
  if (this->media_url_.has_value()) {
 | 
			
		||||
    if (this->command_.has_value()) {
 | 
			
		||||
      ESP_LOGW(TAG, "MediaPlayerCall: Setting both command and media_url is not needed.");
 | 
			
		||||
      this->command_.reset();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  if (this->volume_.has_value()) {
 | 
			
		||||
    if (this->volume_.value() < 0.0f || this->volume_.value() > 1.0f) {
 | 
			
		||||
      ESP_LOGW(TAG, "MediaPlayerCall: Volume must be between 0.0 and 1.0.");
 | 
			
		||||
      this->volume_.reset();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MediaPlayerCall::perform() {
 | 
			
		||||
  ESP_LOGD(TAG, "'%s' - Setting", this->parent_->get_name().c_str());
 | 
			
		||||
  this->validate_();
 | 
			
		||||
  if (this->command_.has_value()) {
 | 
			
		||||
    const char *command_s = media_player_command_to_string(this->command_.value());
 | 
			
		||||
    ESP_LOGD(TAG, "  Command: %s", command_s);
 | 
			
		||||
  }
 | 
			
		||||
  if (this->media_url_.has_value()) {
 | 
			
		||||
    ESP_LOGD(TAG, "  Media URL: %s", this->media_url_.value().c_str());
 | 
			
		||||
  }
 | 
			
		||||
  if (this->volume_.has_value()) {
 | 
			
		||||
    ESP_LOGD(TAG, "  Volume: %.2f", this->volume_.value());
 | 
			
		||||
  }
 | 
			
		||||
  this->parent_->control(*this);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
MediaPlayerCall &MediaPlayerCall::set_command(MediaPlayerCommand command) {
 | 
			
		||||
  this->command_ = command;
 | 
			
		||||
  return *this;
 | 
			
		||||
}
 | 
			
		||||
MediaPlayerCall &MediaPlayerCall::set_command(optional<MediaPlayerCommand> command) {
 | 
			
		||||
  this->command_ = command;
 | 
			
		||||
  return *this;
 | 
			
		||||
}
 | 
			
		||||
MediaPlayerCall &MediaPlayerCall::set_command(const std::string &command) {
 | 
			
		||||
  if (str_equals_case_insensitive(command, "PLAY")) {
 | 
			
		||||
    this->set_command(MEDIA_PLAYER_COMMAND_PLAY);
 | 
			
		||||
  } else if (str_equals_case_insensitive(command, "PAUSE")) {
 | 
			
		||||
    this->set_command(MEDIA_PLAYER_COMMAND_PAUSE);
 | 
			
		||||
  } else if (str_equals_case_insensitive(command, "STOP")) {
 | 
			
		||||
    this->set_command(MEDIA_PLAYER_COMMAND_STOP);
 | 
			
		||||
  } else if (str_equals_case_insensitive(command, "MUTE")) {
 | 
			
		||||
    this->set_command(MEDIA_PLAYER_COMMAND_MUTE);
 | 
			
		||||
  } else if (str_equals_case_insensitive(command, "UNMUTE")) {
 | 
			
		||||
    this->set_command(MEDIA_PLAYER_COMMAND_UNMUTE);
 | 
			
		||||
  } else if (str_equals_case_insensitive(command, "TOGGLE")) {
 | 
			
		||||
    this->set_command(MEDIA_PLAYER_COMMAND_TOGGLE);
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGW(TAG, "'%s' - Unrecognized command %s", this->parent_->get_name().c_str(), command.c_str());
 | 
			
		||||
  }
 | 
			
		||||
  return *this;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
MediaPlayerCall &MediaPlayerCall::set_media_url(const std::string &media_url) {
 | 
			
		||||
  this->media_url_ = media_url;
 | 
			
		||||
  return *this;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
MediaPlayerCall &MediaPlayerCall::set_volume(float volume) {
 | 
			
		||||
  this->volume_ = volume;
 | 
			
		||||
  return *this;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MediaPlayer::add_on_state_callback(std::function<void()> &&callback) {
 | 
			
		||||
  this->state_callback_.add(std::move(callback));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MediaPlayer::publish_state() { this->state_callback_.call(); }
 | 
			
		||||
 | 
			
		||||
}  // namespace media_player
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										93
									
								
								esphome/components/media_player/media_player.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										93
									
								
								esphome/components/media_player/media_player.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,93 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/entity_base.h"
 | 
			
		||||
#include "esphome/core/helpers.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace media_player {
 | 
			
		||||
 | 
			
		||||
enum MediaPlayerState : uint8_t {
 | 
			
		||||
  MEDIA_PLAYER_STATE_NONE = 0,
 | 
			
		||||
  MEDIA_PLAYER_STATE_IDLE = 1,
 | 
			
		||||
  MEDIA_PLAYER_STATE_PLAYING = 2,
 | 
			
		||||
  MEDIA_PLAYER_STATE_PAUSED = 3
 | 
			
		||||
};
 | 
			
		||||
const char *media_player_state_to_string(MediaPlayerState state);
 | 
			
		||||
 | 
			
		||||
enum MediaPlayerCommand : uint8_t {
 | 
			
		||||
  MEDIA_PLAYER_COMMAND_PLAY = 0,
 | 
			
		||||
  MEDIA_PLAYER_COMMAND_PAUSE = 1,
 | 
			
		||||
  MEDIA_PLAYER_COMMAND_STOP = 2,
 | 
			
		||||
  MEDIA_PLAYER_COMMAND_MUTE = 3,
 | 
			
		||||
  MEDIA_PLAYER_COMMAND_UNMUTE = 4,
 | 
			
		||||
  MEDIA_PLAYER_COMMAND_TOGGLE = 5,
 | 
			
		||||
  MEDIA_PLAYER_COMMAND_VOLUME_UP = 6,
 | 
			
		||||
  MEDIA_PLAYER_COMMAND_VOLUME_DOWN = 7,
 | 
			
		||||
};
 | 
			
		||||
const char *media_player_command_to_string(MediaPlayerCommand command);
 | 
			
		||||
 | 
			
		||||
class MediaPlayer;
 | 
			
		||||
 | 
			
		||||
class MediaPlayerTraits {
 | 
			
		||||
 public:
 | 
			
		||||
  MediaPlayerTraits() = default;
 | 
			
		||||
 | 
			
		||||
  void set_supports_pause(bool supports_pause) { this->supports_pause_ = supports_pause; }
 | 
			
		||||
 | 
			
		||||
  bool get_supports_pause() const { return this->supports_pause_; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  bool supports_pause_{false};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class MediaPlayerCall {
 | 
			
		||||
 public:
 | 
			
		||||
  MediaPlayerCall(MediaPlayer *parent) : parent_(parent) {}
 | 
			
		||||
 | 
			
		||||
  MediaPlayerCall &set_command(MediaPlayerCommand command);
 | 
			
		||||
  MediaPlayerCall &set_command(optional<MediaPlayerCommand> command);
 | 
			
		||||
  MediaPlayerCall &set_command(const std::string &command);
 | 
			
		||||
 | 
			
		||||
  MediaPlayerCall &set_media_url(const std::string &url);
 | 
			
		||||
 | 
			
		||||
  MediaPlayerCall &set_volume(float volume);
 | 
			
		||||
 | 
			
		||||
  void perform();
 | 
			
		||||
 | 
			
		||||
  const optional<MediaPlayerCommand> &get_command() const { return command_; }
 | 
			
		||||
  const optional<std::string> &get_media_url() const { return media_url_; }
 | 
			
		||||
  const optional<float> &get_volume() const { return volume_; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void validate_();
 | 
			
		||||
  MediaPlayer *const parent_;
 | 
			
		||||
  optional<MediaPlayerCommand> command_;
 | 
			
		||||
  optional<std::string> media_url_;
 | 
			
		||||
  optional<float> volume_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class MediaPlayer : public EntityBase {
 | 
			
		||||
 public:
 | 
			
		||||
  MediaPlayerState state{MEDIA_PLAYER_STATE_NONE};
 | 
			
		||||
  float volume{1.0f};
 | 
			
		||||
 | 
			
		||||
  MediaPlayerCall make_call() { return MediaPlayerCall(this); }
 | 
			
		||||
 | 
			
		||||
  void publish_state();
 | 
			
		||||
 | 
			
		||||
  void add_on_state_callback(std::function<void()> &&callback);
 | 
			
		||||
 | 
			
		||||
  virtual bool is_muted() const { return false; }
 | 
			
		||||
 | 
			
		||||
  virtual MediaPlayerTraits get_traits() = 0;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  friend MediaPlayerCall;
 | 
			
		||||
 | 
			
		||||
  virtual void control(const MediaPlayerCall &call) = 0;
 | 
			
		||||
 | 
			
		||||
  CallbackManager<void()> state_callback_{};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace media_player
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -56,6 +56,11 @@ template<typename... Ts> class PowerOffAction : public MideaActionBase<Ts...> {
 | 
			
		||||
  void play(Ts... x) override { this->parent_->do_power_off(); }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
template<typename... Ts> class PowerToggleAction : public MideaActionBase<Ts...> {
 | 
			
		||||
 public:
 | 
			
		||||
  void play(Ts... x) override { this->parent_->do_power_toggle(); }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace ac
 | 
			
		||||
}  // namespace midea
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
@@ -39,6 +39,7 @@ class AirConditioner : public ApplianceBase<dudanov::midea::ac::AirConditioner>,
 | 
			
		||||
  void do_beeper_off() { this->set_beeper_feedback(false); }
 | 
			
		||||
  void do_power_on() { this->base_.setPowerState(true); }
 | 
			
		||||
  void do_power_off() { this->base_.setPowerState(false); }
 | 
			
		||||
  void do_power_toggle() { this->base_.setPowerState(this->mode == ClimateMode::CLIMATE_MODE_OFF); }
 | 
			
		||||
  void set_supported_modes(const std::set<ClimateMode> &modes) { this->supported_modes_ = modes; }
 | 
			
		||||
  void set_supported_swing_modes(const std::set<ClimateSwingMode> &modes) { this->supported_swing_modes_ = modes; }
 | 
			
		||||
  void set_supported_presets(const std::set<ClimatePreset> &presets) { this->supported_presets_ = presets; }
 | 
			
		||||
 
 | 
			
		||||
@@ -113,7 +113,7 @@ CONFIG_SCHEMA = cv.All(
 | 
			
		||||
            cv.Optional(CONF_PERIOD, default="1s"): cv.time_period,
 | 
			
		||||
            cv.Optional(CONF_TIMEOUT, default="2s"): cv.time_period,
 | 
			
		||||
            cv.Optional(CONF_NUM_ATTEMPTS, default=3): cv.int_range(min=1, max=5),
 | 
			
		||||
            cv.Optional(CONF_TRANSMITTER_ID): cv.use_id(
 | 
			
		||||
            cv.OnlyWith(CONF_TRANSMITTER_ID, "remote_transmitter"): cv.use_id(
 | 
			
		||||
                remote_transmitter.RemoteTransmitterComponent
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_BEEPER, default=False): cv.boolean,
 | 
			
		||||
@@ -163,6 +163,7 @@ BeeperOnAction = midea_ac_ns.class_("BeeperOnAction", automation.Action)
 | 
			
		||||
BeeperOffAction = midea_ac_ns.class_("BeeperOffAction", automation.Action)
 | 
			
		||||
PowerOnAction = midea_ac_ns.class_("PowerOnAction", automation.Action)
 | 
			
		||||
PowerOffAction = midea_ac_ns.class_("PowerOffAction", automation.Action)
 | 
			
		||||
PowerToggleAction = midea_ac_ns.class_("PowerToggleAction", automation.Action)
 | 
			
		||||
 | 
			
		||||
MIDEA_ACTION_BASE_SCHEMA = cv.Schema(
 | 
			
		||||
    {
 | 
			
		||||
@@ -249,6 +250,16 @@ async def power_off_to_code(var, config, args):
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# Power Toggle action
 | 
			
		||||
@register_action(
 | 
			
		||||
    "power_toggle",
 | 
			
		||||
    PowerToggleAction,
 | 
			
		||||
    cv.Schema({}),
 | 
			
		||||
)
 | 
			
		||||
async def power_inv_to_code(var, config, args):
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
 
 | 
			
		||||
@@ -68,33 +68,54 @@ 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 == 0xF || function_code == 0x10) {
 | 
			
		||||
    data_offset = 2;
 | 
			
		||||
    data_len = 4;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Error ( msb indicates error )
 | 
			
		||||
  // response format:  Byte[0] = device address, Byte[1] function code | 0x80 , Byte[2] excpetion code, Byte[3-4] crc
 | 
			
		||||
  if ((function_code & 0x80) == 0x80) {
 | 
			
		||||
    data_offset = 2;
 | 
			
		||||
    data_len = 1;
 | 
			
		||||
  }
 | 
			
		||||
  // Per https://modbus.org/docs/Modbus_Application_Protocol_V1_1b3.pdf Ch 5 User-Defined function codes
 | 
			
		||||
  if (((function_code >= 65) && (function_code <= 72)) || ((function_code >= 100) && (function_code <= 110))) {
 | 
			
		||||
    // Handle user-defined function, since we don't know how big this ought to be,
 | 
			
		||||
    // ideally we should delegate the entire length detection to whatever handler is
 | 
			
		||||
    // installed, but wait, there is the CRC, and if we get a hit there is a good
 | 
			
		||||
    // chance that this is a complete message ... admittedly there is a small chance is
 | 
			
		||||
    // isn't but that is quite small given the purpose of the CRC in the first place
 | 
			
		||||
    data_len = at;
 | 
			
		||||
    data_offset = 1;
 | 
			
		||||
 | 
			
		||||
  // Byte data_offset..data_offset+data_len-1: Data
 | 
			
		||||
  if (at < data_offset + data_len)
 | 
			
		||||
    return true;
 | 
			
		||||
    uint16_t computed_crc = crc16(raw, data_offset + data_len);
 | 
			
		||||
    uint16_t remote_crc = uint16_t(raw[data_offset + data_len]) | (uint16_t(raw[data_offset + data_len + 1]) << 8);
 | 
			
		||||
 | 
			
		||||
  // Byte 3+data_len: CRC_LO (over all bytes)
 | 
			
		||||
  if (at == data_offset + data_len)
 | 
			
		||||
    return true;
 | 
			
		||||
    if (computed_crc != remote_crc)
 | 
			
		||||
      return true;
 | 
			
		||||
 | 
			
		||||
  // Byte data_offset+len+1: CRC_HI (over all bytes)
 | 
			
		||||
  uint16_t computed_crc = crc16(raw, data_offset + data_len);
 | 
			
		||||
  uint16_t remote_crc = uint16_t(raw[data_offset + data_len]) | (uint16_t(raw[data_offset + data_len + 1]) << 8);
 | 
			
		||||
  if (computed_crc != remote_crc) {
 | 
			
		||||
    ESP_LOGW(TAG, "Modbus CRC Check failed! %02X!=%02X", computed_crc, remote_crc);
 | 
			
		||||
    return false;
 | 
			
		||||
    ESP_LOGD(TAG, "Modbus user-defined function %02X found", function_code);
 | 
			
		||||
 | 
			
		||||
  } else {
 | 
			
		||||
    // 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 == 0xF || function_code == 0x10) {
 | 
			
		||||
      data_offset = 2;
 | 
			
		||||
      data_len = 4;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Error ( msb indicates error )
 | 
			
		||||
    // response format:  Byte[0] = device address, Byte[1] function code | 0x80 , Byte[2] excpetion code, Byte[3-4] crc
 | 
			
		||||
    if ((function_code & 0x80) == 0x80) {
 | 
			
		||||
      data_offset = 2;
 | 
			
		||||
      data_len = 1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Byte data_offset..data_offset+data_len-1: Data
 | 
			
		||||
    if (at < data_offset + data_len)
 | 
			
		||||
      return true;
 | 
			
		||||
 | 
			
		||||
    // Byte 3+data_len: CRC_LO (over all bytes)
 | 
			
		||||
    if (at == data_offset + data_len)
 | 
			
		||||
      return true;
 | 
			
		||||
 | 
			
		||||
    // Byte data_offset+len+1: CRC_HI (over all bytes)
 | 
			
		||||
    uint16_t computed_crc = crc16(raw, data_offset + data_len);
 | 
			
		||||
    uint16_t remote_crc = uint16_t(raw[data_offset + data_len]) | (uint16_t(raw[data_offset + data_len + 1]) << 8);
 | 
			
		||||
    if (computed_crc != remote_crc) {
 | 
			
		||||
      ESP_LOGW(TAG, "Modbus CRC Check failed! %02X!=%02X", computed_crc, remote_crc);
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  std::vector<uint8_t> data(this->rx_buffer_.begin() + data_offset, this->rx_buffer_.begin() + data_offset + data_len);
 | 
			
		||||
  bool found = false;
 | 
			
		||||
 
 | 
			
		||||
@@ -108,8 +108,7 @@ void ModbusController::queue_command(const ModbusCommandItem &command) {
 | 
			
		||||
  // check if this commmand is already qeued.
 | 
			
		||||
  // 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->function_code == command.function_code) {
 | 
			
		||||
    if (item->is_equal(command)) {
 | 
			
		||||
      ESP_LOGW(TAG, "Duplicate modbus command found: type=0x%x address=%u count=%u",
 | 
			
		||||
               static_cast<uint8_t>(command.register_type), command.register_address, command.register_count);
 | 
			
		||||
      // update the payload of the queued command
 | 
			
		||||
@@ -489,6 +488,15 @@ bool ModbusCommandItem::send() {
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool ModbusCommandItem::is_equal(const ModbusCommandItem &other) {
 | 
			
		||||
  // for custom commands we have to check for identical payloads, since
 | 
			
		||||
  // address/count/type fields will be set to zero
 | 
			
		||||
  return this->function_code == ModbusFunctionCode::CUSTOM
 | 
			
		||||
             ? this->payload == other.payload
 | 
			
		||||
             : other.register_address == this->register_address && other.register_count == this->register_count &&
 | 
			
		||||
                   other.register_type == this->register_type && other.function_code == this->function_code;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void number_to_payload(std::vector<uint16_t> &data, int64_t value, SensorValueType value_type) {
 | 
			
		||||
  switch (value_type) {
 | 
			
		||||
    case SensorValueType::U_WORD:
 | 
			
		||||
 
 | 
			
		||||
@@ -395,6 +395,8 @@ class ModbusCommandItem {
 | 
			
		||||
      ModbusController *modbusdevice, const std::vector<uint16_t> &values,
 | 
			
		||||
      std::function<void(ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data)>
 | 
			
		||||
          &&handler = nullptr);
 | 
			
		||||
 | 
			
		||||
  bool is_equal(const ModbusCommandItem &other);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/** Modbus controller class.
 | 
			
		||||
 
 | 
			
		||||
@@ -24,6 +24,8 @@ from esphome.const import (
 | 
			
		||||
    CONF_LOG_TOPIC,
 | 
			
		||||
    CONF_ON_JSON_MESSAGE,
 | 
			
		||||
    CONF_ON_MESSAGE,
 | 
			
		||||
    CONF_ON_CONNECT,
 | 
			
		||||
    CONF_ON_DISCONNECT,
 | 
			
		||||
    CONF_PASSWORD,
 | 
			
		||||
    CONF_PAYLOAD,
 | 
			
		||||
    CONF_PAYLOAD_AVAILABLE,
 | 
			
		||||
@@ -90,6 +92,10 @@ MQTTMessageTrigger = mqtt_ns.class_(
 | 
			
		||||
MQTTJsonMessageTrigger = mqtt_ns.class_(
 | 
			
		||||
    "MQTTJsonMessageTrigger", automation.Trigger.template(cg.JsonObjectConst)
 | 
			
		||||
)
 | 
			
		||||
MQTTConnectTrigger = mqtt_ns.class_("MQTTConnectTrigger", automation.Trigger.template())
 | 
			
		||||
MQTTDisconnectTrigger = mqtt_ns.class_(
 | 
			
		||||
    "MQTTDisconnectTrigger", automation.Trigger.template()
 | 
			
		||||
)
 | 
			
		||||
MQTTComponent = mqtt_ns.class_("MQTTComponent", cg.Component)
 | 
			
		||||
MQTTConnectedCondition = mqtt_ns.class_("MQTTConnectedCondition", Condition)
 | 
			
		||||
 | 
			
		||||
@@ -212,6 +218,18 @@ CONFIG_SCHEMA = cv.All(
 | 
			
		||||
            cv.Optional(
 | 
			
		||||
                CONF_REBOOT_TIMEOUT, default="15min"
 | 
			
		||||
            ): cv.positive_time_period_milliseconds,
 | 
			
		||||
            cv.Optional(CONF_ON_CONNECT): automation.validate_automation(
 | 
			
		||||
                {
 | 
			
		||||
                    cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(MQTTConnectTrigger),
 | 
			
		||||
                }
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_ON_DISCONNECT): automation.validate_automation(
 | 
			
		||||
                {
 | 
			
		||||
                    cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(
 | 
			
		||||
                        MQTTDisconnectTrigger
 | 
			
		||||
                    ),
 | 
			
		||||
                }
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_ON_MESSAGE): automation.validate_automation(
 | 
			
		||||
                {
 | 
			
		||||
                    cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(MQTTMessageTrigger),
 | 
			
		||||
@@ -362,6 +380,14 @@ async def to_code(config):
 | 
			
		||||
        trig = cg.new_Pvariable(conf[CONF_TRIGGER_ID], conf[CONF_TOPIC], conf[CONF_QOS])
 | 
			
		||||
        await automation.build_automation(trig, [(cg.JsonObjectConst, "x")], conf)
 | 
			
		||||
 | 
			
		||||
    for conf in config.get(CONF_ON_CONNECT, []):
 | 
			
		||||
        trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
 | 
			
		||||
        await automation.build_automation(trigger, [], conf)
 | 
			
		||||
 | 
			
		||||
    for conf in config.get(CONF_ON_DISCONNECT, []):
 | 
			
		||||
        trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
 | 
			
		||||
        await automation.build_automation(trigger, [], conf)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
MQTT_PUBLISH_ACTION_SCHEMA = cv.Schema(
 | 
			
		||||
    {
 | 
			
		||||
 
 | 
			
		||||
@@ -187,7 +187,7 @@ void MQTTClientComponent::start_connect_() {
 | 
			
		||||
 | 
			
		||||
  this->mqtt_backend_.set_credentials(username, password);
 | 
			
		||||
 | 
			
		||||
  this->mqtt_backend_.set_server((uint32_t) this->ip_, this->credentials_.port);
 | 
			
		||||
  this->mqtt_backend_.set_server(this->credentials_.address.c_str(), this->credentials_.port);
 | 
			
		||||
  if (!this->last_will_.topic.empty()) {
 | 
			
		||||
    this->mqtt_backend_.set_will(this->last_will_.topic.c_str(), this->last_will_.qos, this->last_will_.retain,
 | 
			
		||||
                                 this->last_will_.payload.c_str());
 | 
			
		||||
@@ -572,6 +572,14 @@ void MQTTClientComponent::on_shutdown() {
 | 
			
		||||
  this->mqtt_backend_.disconnect();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MQTTClientComponent::set_on_connect(mqtt_on_connect_callback_t &&callback) {
 | 
			
		||||
  this->mqtt_backend_.set_on_connect(std::forward<mqtt_on_connect_callback_t>(callback));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void MQTTClientComponent::set_on_disconnect(mqtt_on_disconnect_callback_t &&callback) {
 | 
			
		||||
  this->mqtt_backend_.set_on_disconnect(std::forward<mqtt_on_disconnect_callback_t>(callback));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if ASYNC_TCP_SSL_ENABLED
 | 
			
		||||
void MQTTClientComponent::add_ssl_fingerprint(const std::array<uint8_t, SHA1_SIZE> &fingerprint) {
 | 
			
		||||
  this->mqtt_backend_.setSecure(true);
 | 
			
		||||
 
 | 
			
		||||
@@ -19,6 +19,11 @@
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace mqtt {
 | 
			
		||||
 | 
			
		||||
/** Callback for MQTT events.
 | 
			
		||||
 */
 | 
			
		||||
using mqtt_on_connect_callback_t = std::function<MQTTBackend::on_connect_callback_t>;
 | 
			
		||||
using mqtt_on_disconnect_callback_t = std::function<MQTTBackend::on_disconnect_callback_t>;
 | 
			
		||||
 | 
			
		||||
/** Callback for MQTT subscriptions.
 | 
			
		||||
 *
 | 
			
		||||
 * First parameter is the topic, the second one is the payload.
 | 
			
		||||
@@ -240,6 +245,8 @@ class MQTTClientComponent : public Component {
 | 
			
		||||
  void set_username(const std::string &username) { this->credentials_.username = username; }
 | 
			
		||||
  void set_password(const std::string &password) { this->credentials_.password = password; }
 | 
			
		||||
  void set_client_id(const std::string &client_id) { this->credentials_.client_id = client_id; }
 | 
			
		||||
  void set_on_connect(mqtt_on_connect_callback_t &&callback);
 | 
			
		||||
  void set_on_disconnect(mqtt_on_disconnect_callback_t &&callback);
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  /// Reconnect to the MQTT broker if not already connected.
 | 
			
		||||
@@ -328,6 +335,20 @@ class MQTTJsonMessageTrigger : public Trigger<JsonObjectConst> {
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class MQTTConnectTrigger : public Trigger<> {
 | 
			
		||||
 public:
 | 
			
		||||
  explicit MQTTConnectTrigger(MQTTClientComponent *&client) {
 | 
			
		||||
    client->set_on_connect([this](bool session_present) { this->trigger(); });
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class MQTTDisconnectTrigger : public Trigger<> {
 | 
			
		||||
 public:
 | 
			
		||||
  explicit MQTTDisconnectTrigger(MQTTClientComponent *&client) {
 | 
			
		||||
    client->set_on_disconnect([this](MQTTClientDisconnectReason reason) { this->trigger(); });
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
template<typename... Ts> class MQTTPublishAction : public Action<Ts...> {
 | 
			
		||||
 public:
 | 
			
		||||
  MQTTPublishAction(MQTTClientComponent *parent) : parent_(parent) {}
 | 
			
		||||
 
 | 
			
		||||
@@ -51,10 +51,9 @@ void MQTTCoverComponent::setup() {
 | 
			
		||||
void MQTTCoverComponent::dump_config() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "MQTT cover '%s':", this->cover_->get_name().c_str());
 | 
			
		||||
  auto traits = this->cover_->get_traits();
 | 
			
		||||
  // no state topic for position
 | 
			
		||||
  bool state_topic = !traits.get_supports_position();
 | 
			
		||||
  LOG_MQTT_COMPONENT(state_topic, true)
 | 
			
		||||
  if (!state_topic) {
 | 
			
		||||
  bool has_command_topic = traits.get_supports_position() || !traits.get_supports_tilt();
 | 
			
		||||
  LOG_MQTT_COMPONENT(true, has_command_topic)
 | 
			
		||||
  if (traits.get_supports_position()) {
 | 
			
		||||
    ESP_LOGCONFIG(TAG, "  Position State Topic: '%s'", this->get_position_state_topic().c_str());
 | 
			
		||||
    ESP_LOGCONFIG(TAG, "  Position Command Topic: '%s'", this->get_position_command_topic().c_str());
 | 
			
		||||
  }
 | 
			
		||||
@@ -72,7 +71,6 @@ void MQTTCoverComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryConf
 | 
			
		||||
    root[MQTT_OPTIMISTIC] = true;
 | 
			
		||||
  }
 | 
			
		||||
  if (traits.get_supports_position()) {
 | 
			
		||||
    config.state_topic = false;
 | 
			
		||||
    root[MQTT_POSITION_TOPIC] = this->get_position_state_topic();
 | 
			
		||||
    root[MQTT_SET_POSITION_TOPIC] = this->get_position_command_topic();
 | 
			
		||||
  }
 | 
			
		||||
@@ -92,17 +90,7 @@ bool MQTTCoverComponent::send_initial_state() { return this->publish_state(); }
 | 
			
		||||
bool MQTTCoverComponent::publish_state() {
 | 
			
		||||
  auto traits = this->cover_->get_traits();
 | 
			
		||||
  bool success = true;
 | 
			
		||||
  if (!traits.get_supports_position()) {
 | 
			
		||||
    const char *state_s = "unknown";
 | 
			
		||||
    if (this->cover_->position == COVER_OPEN) {
 | 
			
		||||
      state_s = "open";
 | 
			
		||||
    } else if (this->cover_->position == COVER_CLOSED) {
 | 
			
		||||
      state_s = "closed";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!this->publish(this->get_state_topic_(), state_s))
 | 
			
		||||
      success = false;
 | 
			
		||||
  } else {
 | 
			
		||||
  if (traits.get_supports_position()) {
 | 
			
		||||
    std::string pos = value_accuracy_to_string(roundf(this->cover_->position * 100), 0);
 | 
			
		||||
    if (!this->publish(this->get_position_state_topic(), pos))
 | 
			
		||||
      success = false;
 | 
			
		||||
@@ -112,6 +100,14 @@ bool MQTTCoverComponent::publish_state() {
 | 
			
		||||
    if (!this->publish(this->get_tilt_state_topic(), pos))
 | 
			
		||||
      success = false;
 | 
			
		||||
  }
 | 
			
		||||
  const char *state_s = this->cover_->current_operation == COVER_OPERATION_OPENING   ? "opening"
 | 
			
		||||
                        : this->cover_->current_operation == COVER_OPERATION_CLOSING ? "closing"
 | 
			
		||||
                        : this->cover_->position == COVER_CLOSED                     ? "closed"
 | 
			
		||||
                        : this->cover_->position == COVER_OPEN                       ? "open"
 | 
			
		||||
                        : traits.get_supports_position()                             ? "open"
 | 
			
		||||
                                                                                     : "unknown";
 | 
			
		||||
  if (!this->publish(this->get_state_topic_(), state_s))
 | 
			
		||||
    success = false;
 | 
			
		||||
  return success;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,6 @@
 | 
			
		||||
 | 
			
		||||
#ifdef USE_MQTT
 | 
			
		||||
#ifdef USE_FAN
 | 
			
		||||
#include "esphome/components/fan/fan_helpers.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace mqtt {
 | 
			
		||||
@@ -88,17 +87,6 @@ void MQTTFanComponent::setup() {
 | 
			
		||||
                    });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (this->state_->get_traits().supports_speed()) {
 | 
			
		||||
    this->subscribe(this->get_speed_command_topic(), [this](const std::string &topic, const std::string &payload) {
 | 
			
		||||
#pragma GCC diagnostic push
 | 
			
		||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
 | 
			
		||||
      this->state_->make_call()
 | 
			
		||||
          .set_speed(payload.c_str())  // NOLINT(clang-diagnostic-deprecated-declarations)
 | 
			
		||||
          .perform();
 | 
			
		||||
#pragma GCC diagnostic pop
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  auto f = std::bind(&MQTTFanComponent::publish_state, this);
 | 
			
		||||
  this->state_->add_on_state_callback([this, f]() { this->defer("send", f); });
 | 
			
		||||
}
 | 
			
		||||
@@ -113,8 +101,6 @@ void MQTTFanComponent::dump_config() {
 | 
			
		||||
  if (this->state_->get_traits().supports_speed()) {
 | 
			
		||||
    ESP_LOGCONFIG(TAG, "  Speed Level State Topic: '%s'", this->get_speed_level_state_topic().c_str());
 | 
			
		||||
    ESP_LOGCONFIG(TAG, "  Speed Level Command Topic: '%s'", this->get_speed_level_command_topic().c_str());
 | 
			
		||||
    ESP_LOGCONFIG(TAG, "  Speed State Topic: '%s'", this->get_speed_state_topic().c_str());
 | 
			
		||||
    ESP_LOGCONFIG(TAG, "  Speed Command Topic: '%s'", this->get_speed_command_topic().c_str());
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -126,10 +112,9 @@ void MQTTFanComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryConfig
 | 
			
		||||
    root[MQTT_OSCILLATION_STATE_TOPIC] = this->get_oscillation_state_topic();
 | 
			
		||||
  }
 | 
			
		||||
  if (this->state_->get_traits().supports_speed()) {
 | 
			
		||||
    root["speed_level_command_topic"] = this->get_speed_level_command_topic();
 | 
			
		||||
    root["speed_level_state_topic"] = this->get_speed_level_state_topic();
 | 
			
		||||
    root[MQTT_SPEED_COMMAND_TOPIC] = this->get_speed_command_topic();
 | 
			
		||||
    root[MQTT_SPEED_STATE_TOPIC] = this->get_speed_state_topic();
 | 
			
		||||
    root[MQTT_PERCENTAGE_COMMAND_TOPIC] = this->get_speed_level_command_topic();
 | 
			
		||||
    root[MQTT_PERCENTAGE_STATE_TOPIC] = this->get_speed_level_state_topic();
 | 
			
		||||
    root[MQTT_SPEED_RANGE_MAX] = this->state_->get_traits().supported_speed_count();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
bool MQTTFanComponent::publish_state() {
 | 
			
		||||
@@ -148,31 +133,6 @@ bool MQTTFanComponent::publish_state() {
 | 
			
		||||
    bool success = this->publish(this->get_speed_level_state_topic(), payload);
 | 
			
		||||
    failed = failed || !success;
 | 
			
		||||
  }
 | 
			
		||||
  if (traits.supports_speed()) {
 | 
			
		||||
    const char *payload;
 | 
			
		||||
#pragma GCC diagnostic push
 | 
			
		||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
 | 
			
		||||
    // NOLINTNEXTLINE(clang-diagnostic-deprecated-declarations)
 | 
			
		||||
    switch (fan::speed_level_to_enum(this->state_->speed, traits.supported_speed_count())) {
 | 
			
		||||
      case FAN_SPEED_LOW: {  // NOLINT(clang-diagnostic-deprecated-declarations)
 | 
			
		||||
        payload = "low";
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      case FAN_SPEED_MEDIUM: {  // NOLINT(clang-diagnostic-deprecated-declarations)
 | 
			
		||||
        payload = "medium";
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      default:
 | 
			
		||||
      case FAN_SPEED_HIGH: {  // NOLINT(clang-diagnostic-deprecated-declarations)
 | 
			
		||||
        payload = "high";
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
#pragma GCC diagnostic pop
 | 
			
		||||
    bool success = this->publish(this->get_speed_state_topic(), payload);
 | 
			
		||||
    failed = failed || !success;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return !failed;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -115,7 +115,7 @@ void Nextion::set_backlight_brightness(float brightness) {
 | 
			
		||||
    ESP_LOGD(TAG, "Brightness out of bounds, percentage range 0-1.0");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  this->add_no_result_to_queue_with_set("backlight_brightness", "dim", static_cast<int>(brightness * 100));
 | 
			
		||||
  this->add_no_result_to_queue_with_printf_("backlight_brightness", "dim=%d", static_cast<int>(brightness * 100));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Nextion::set_auto_wake_on_touch(bool auto_wake) {
 | 
			
		||||
 
 | 
			
		||||
@@ -17,7 +17,5 @@ void Number::add_on_state_callback(std::function<void(float)> &&callback) {
 | 
			
		||||
  this->state_callback_.add(std::move(callback));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint32_t Number::hash_base() { return 2282307003UL; }
 | 
			
		||||
 | 
			
		||||
}  // namespace number
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
@@ -52,8 +52,6 @@ class Number : public EntityBase {
 | 
			
		||||
   */
 | 
			
		||||
  virtual void control(float value) = 0;
 | 
			
		||||
 | 
			
		||||
  uint32_t hash_base() override;
 | 
			
		||||
 | 
			
		||||
  CallbackManager<void(float)> state_callback_;
 | 
			
		||||
  bool has_state_{false};
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
@@ -52,6 +52,8 @@ class PCD8544 : public PollingComponent,
 | 
			
		||||
    this->initialize();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  display::DisplayType get_display_type() override { return display::DisplayType::DISPLAY_TYPE_BINARY; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void draw_absolute_pixel_internal(int x, int y, Color color) override;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,9 @@
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
from esphome.const import CONF_ID
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    CONF_INCLUDE_INTERNAL,
 | 
			
		||||
)
 | 
			
		||||
from esphome.components.web_server_base import CONF_WEB_SERVER_BASE_ID
 | 
			
		||||
from esphome.components import web_server_base
 | 
			
		||||
 | 
			
		||||
@@ -15,6 +18,7 @@ CONFIG_SCHEMA = cv.Schema(
 | 
			
		||||
        cv.GenerateID(CONF_WEB_SERVER_BASE_ID): cv.use_id(
 | 
			
		||||
            web_server_base.WebServerBase
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_INCLUDE_INTERNAL, default=False): cv.boolean,
 | 
			
		||||
    },
 | 
			
		||||
    cv.only_with_arduino,
 | 
			
		||||
).extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
@@ -27,3 +31,5 @@ async def to_code(config):
 | 
			
		||||
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID], paren)
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
 | 
			
		||||
    cg.add(var.set_include_internal(config[CONF_INCLUDE_INTERNAL]))
 | 
			
		||||
 
 | 
			
		||||
@@ -61,7 +61,7 @@ void PrometheusHandler::sensor_type_(AsyncResponseStream *stream) {
 | 
			
		||||
  stream->print(F("#TYPE esphome_sensor_failed GAUGE\n"));
 | 
			
		||||
}
 | 
			
		||||
void PrometheusHandler::sensor_row_(AsyncResponseStream *stream, sensor::Sensor *obj) {
 | 
			
		||||
  if (obj->is_internal())
 | 
			
		||||
  if (obj->is_internal() && !this->include_internal_)
 | 
			
		||||
    return;
 | 
			
		||||
  if (!std::isnan(obj->state)) {
 | 
			
		||||
    // We have a valid value, output this value
 | 
			
		||||
@@ -98,7 +98,7 @@ void PrometheusHandler::binary_sensor_type_(AsyncResponseStream *stream) {
 | 
			
		||||
  stream->print(F("#TYPE esphome_binary_sensor_failed GAUGE\n"));
 | 
			
		||||
}
 | 
			
		||||
void PrometheusHandler::binary_sensor_row_(AsyncResponseStream *stream, binary_sensor::BinarySensor *obj) {
 | 
			
		||||
  if (obj->is_internal())
 | 
			
		||||
  if (obj->is_internal() && !this->include_internal_)
 | 
			
		||||
    return;
 | 
			
		||||
  if (obj->has_state()) {
 | 
			
		||||
    // We have a valid value, output this value
 | 
			
		||||
@@ -134,7 +134,7 @@ void PrometheusHandler::fan_type_(AsyncResponseStream *stream) {
 | 
			
		||||
  stream->print(F("#TYPE esphome_fan_oscillation GAUGE\n"));
 | 
			
		||||
}
 | 
			
		||||
void PrometheusHandler::fan_row_(AsyncResponseStream *stream, fan::Fan *obj) {
 | 
			
		||||
  if (obj->is_internal())
 | 
			
		||||
  if (obj->is_internal() && !this->include_internal_)
 | 
			
		||||
    return;
 | 
			
		||||
  stream->print(F("esphome_fan_failed{id=\""));
 | 
			
		||||
  stream->print(obj->get_object_id().c_str());
 | 
			
		||||
@@ -179,7 +179,7 @@ void PrometheusHandler::light_type_(AsyncResponseStream *stream) {
 | 
			
		||||
  stream->print(F("#TYPE esphome_light_effect_active GAUGE\n"));
 | 
			
		||||
}
 | 
			
		||||
void PrometheusHandler::light_row_(AsyncResponseStream *stream, light::LightState *obj) {
 | 
			
		||||
  if (obj->is_internal())
 | 
			
		||||
  if (obj->is_internal() && !this->include_internal_)
 | 
			
		||||
    return;
 | 
			
		||||
  // State
 | 
			
		||||
  stream->print(F("esphome_light_state{id=\""));
 | 
			
		||||
@@ -255,7 +255,7 @@ void PrometheusHandler::cover_type_(AsyncResponseStream *stream) {
 | 
			
		||||
  stream->print(F("#TYPE esphome_cover_failed GAUGE\n"));
 | 
			
		||||
}
 | 
			
		||||
void PrometheusHandler::cover_row_(AsyncResponseStream *stream, cover::Cover *obj) {
 | 
			
		||||
  if (obj->is_internal())
 | 
			
		||||
  if (obj->is_internal() && !this->include_internal_)
 | 
			
		||||
    return;
 | 
			
		||||
  if (!std::isnan(obj->position)) {
 | 
			
		||||
    // We have a valid value, output this value
 | 
			
		||||
@@ -298,7 +298,7 @@ void PrometheusHandler::switch_type_(AsyncResponseStream *stream) {
 | 
			
		||||
  stream->print(F("#TYPE esphome_switch_failed GAUGE\n"));
 | 
			
		||||
}
 | 
			
		||||
void PrometheusHandler::switch_row_(AsyncResponseStream *stream, switch_::Switch *obj) {
 | 
			
		||||
  if (obj->is_internal())
 | 
			
		||||
  if (obj->is_internal() && !this->include_internal_)
 | 
			
		||||
    return;
 | 
			
		||||
  stream->print(F("esphome_switch_failed{id=\""));
 | 
			
		||||
  stream->print(obj->get_object_id().c_str());
 | 
			
		||||
@@ -322,7 +322,7 @@ void PrometheusHandler::lock_type_(AsyncResponseStream *stream) {
 | 
			
		||||
  stream->print(F("#TYPE esphome_lock_failed GAUGE\n"));
 | 
			
		||||
}
 | 
			
		||||
void PrometheusHandler::lock_row_(AsyncResponseStream *stream, lock::Lock *obj) {
 | 
			
		||||
  if (obj->is_internal())
 | 
			
		||||
  if (obj->is_internal() && !this->include_internal_)
 | 
			
		||||
    return;
 | 
			
		||||
  stream->print(F("esphome_lock_failed{id=\""));
 | 
			
		||||
  stream->print(obj->get_object_id().c_str());
 | 
			
		||||
 
 | 
			
		||||
@@ -13,6 +13,13 @@ class PrometheusHandler : public AsyncWebHandler, public Component {
 | 
			
		||||
 public:
 | 
			
		||||
  PrometheusHandler(web_server_base::WebServerBase *base) : base_(base) {}
 | 
			
		||||
 | 
			
		||||
  /** Determine whether internal components should be exported as metrics.
 | 
			
		||||
   * Defaults to false.
 | 
			
		||||
   *
 | 
			
		||||
   * @param include_internal Whether internal components should be exported.
 | 
			
		||||
   */
 | 
			
		||||
  void set_include_internal(bool include_internal) { include_internal_ = include_internal; }
 | 
			
		||||
 | 
			
		||||
  bool canHandle(AsyncWebServerRequest *request) override {
 | 
			
		||||
    if (request->method() == HTTP_GET) {
 | 
			
		||||
      if (request->url() == "/metrics")
 | 
			
		||||
@@ -84,6 +91,7 @@ class PrometheusHandler : public AsyncWebHandler, public Component {
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  web_server_base::WebServerBase *base_;
 | 
			
		||||
  bool include_internal_{false};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace prometheus
 | 
			
		||||
 
 | 
			
		||||
@@ -728,6 +728,49 @@ async def rc5_action(var, config, args):
 | 
			
		||||
    cg.add(var.set_command(template_))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# RC6
 | 
			
		||||
RC6Data, RC6BinarySensor, RC6Trigger, RC6Action, RC6Dumper = declare_protocol("RC6")
 | 
			
		||||
RC6_SCHEMA = cv.Schema(
 | 
			
		||||
    {
 | 
			
		||||
        cv.Required(CONF_ADDRESS): cv.hex_uint8_t,
 | 
			
		||||
        cv.Required(CONF_COMMAND): cv.hex_uint8_t,
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@register_binary_sensor("rc6", RC6BinarySensor, RC6_SCHEMA)
 | 
			
		||||
def rc6_binary_sensor(var, config):
 | 
			
		||||
    cg.add(
 | 
			
		||||
        var.set_data(
 | 
			
		||||
            cg.StructInitializer(
 | 
			
		||||
                RC6Data,
 | 
			
		||||
                ("mode", 0),
 | 
			
		||||
                ("toggle", 0),
 | 
			
		||||
                ("address", config[CONF_ADDRESS]),
 | 
			
		||||
                ("command", config[CONF_COMMAND]),
 | 
			
		||||
            )
 | 
			
		||||
        )
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@register_trigger("rc6", RC6Trigger, RC6Data)
 | 
			
		||||
def rc6_trigger(var, config):
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@register_dumper("rc6", RC6Dumper)
 | 
			
		||||
def rc6_dumper(var, config):
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@register_action("rc6", RC6Action, RC6_SCHEMA)
 | 
			
		||||
async def rc6_action(var, config, args):
 | 
			
		||||
    template_ = await cg.templatable(config[CONF_ADDRESS], args, cg.uint8)
 | 
			
		||||
    cg.add(var.set_address(template_))
 | 
			
		||||
    template_ = await cg.templatable(config[CONF_COMMAND], args, cg.uint8)
 | 
			
		||||
    cg.add(var.set_command(template_))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# RC Switch Raw
 | 
			
		||||
RC_SWITCH_TIMING_SCHEMA = cv.All([cv.uint8_t], cv.Length(min=2, max=2))
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										181
									
								
								esphome/components/remote_base/rc6_protocol.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										181
									
								
								esphome/components/remote_base/rc6_protocol.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,181 @@
 | 
			
		||||
#include "rc6_protocol.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace remote_base {
 | 
			
		||||
 | 
			
		||||
static const char *const RC6_TAG = "remote.rc6";
 | 
			
		||||
 | 
			
		||||
static const uint16_t RC6_FREQ = 36000;
 | 
			
		||||
static const uint16_t RC6_UNIT = 444;
 | 
			
		||||
static const uint16_t RC6_HEADER_MARK = (6 * RC6_UNIT);
 | 
			
		||||
static const uint16_t RC6_HEADER_SPACE = (2 * RC6_UNIT);
 | 
			
		||||
static const uint16_t RC6_MODE_MASK = 0x07;
 | 
			
		||||
 | 
			
		||||
void RC6Protocol::encode(RemoteTransmitData *dst, const RC6Data &data) {
 | 
			
		||||
  dst->reserve(44);
 | 
			
		||||
  dst->set_carrier_frequency(RC6_FREQ);
 | 
			
		||||
 | 
			
		||||
  // Encode header
 | 
			
		||||
  dst->item(RC6_HEADER_MARK, RC6_HEADER_SPACE);
 | 
			
		||||
 | 
			
		||||
  int32_t next{0};
 | 
			
		||||
 | 
			
		||||
  // Encode startbit+mode
 | 
			
		||||
  uint8_t header{static_cast<uint8_t>((1 << 3) | data.mode)};
 | 
			
		||||
 | 
			
		||||
  for (uint8_t mask = 0x8; mask; mask >>= 1) {
 | 
			
		||||
    if (header & mask) {
 | 
			
		||||
      if (next < 0) {
 | 
			
		||||
        dst->space(-next);
 | 
			
		||||
        next = 0;
 | 
			
		||||
      }
 | 
			
		||||
      if (next >= 0) {
 | 
			
		||||
        next = next + RC6_UNIT;
 | 
			
		||||
        dst->mark(next);
 | 
			
		||||
        next = -RC6_UNIT;
 | 
			
		||||
      }
 | 
			
		||||
    } else {
 | 
			
		||||
      if (next > 0) {
 | 
			
		||||
        dst->mark(next);
 | 
			
		||||
        next = 0;
 | 
			
		||||
      }
 | 
			
		||||
      if (next <= 0) {
 | 
			
		||||
        next = next - RC6_UNIT;
 | 
			
		||||
        dst->space(-next);
 | 
			
		||||
        next = RC6_UNIT;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Toggle
 | 
			
		||||
  if (data.toggle) {
 | 
			
		||||
    if (next < 0) {
 | 
			
		||||
      dst->space(-next);
 | 
			
		||||
      next = 0;
 | 
			
		||||
    }
 | 
			
		||||
    if (next >= 0) {
 | 
			
		||||
      next = next + RC6_UNIT * 2;
 | 
			
		||||
      dst->mark(next);
 | 
			
		||||
      next = -RC6_UNIT * 2;
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    if (next > 0) {
 | 
			
		||||
      dst->mark(next);
 | 
			
		||||
      next = 0;
 | 
			
		||||
    }
 | 
			
		||||
    if (next <= 0) {
 | 
			
		||||
      next = next - RC6_UNIT * 2;
 | 
			
		||||
      dst->space(-next);
 | 
			
		||||
      next = RC6_UNIT * 2;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Encode data
 | 
			
		||||
  uint16_t raw{static_cast<uint16_t>((data.address << 8) | data.command)};
 | 
			
		||||
 | 
			
		||||
  for (uint16_t mask = 0x8000; mask; mask >>= 1) {
 | 
			
		||||
    if (raw & mask) {
 | 
			
		||||
      if (next < 0) {
 | 
			
		||||
        dst->space(-next);
 | 
			
		||||
        next = 0;
 | 
			
		||||
      }
 | 
			
		||||
      if (next >= 0) {
 | 
			
		||||
        next = next + RC6_UNIT;
 | 
			
		||||
        dst->mark(next);
 | 
			
		||||
        next = -RC6_UNIT;
 | 
			
		||||
      }
 | 
			
		||||
    } else {
 | 
			
		||||
      if (next > 0) {
 | 
			
		||||
        dst->mark(next);
 | 
			
		||||
        next = 0;
 | 
			
		||||
      }
 | 
			
		||||
      if (next <= 0) {
 | 
			
		||||
        next = next - RC6_UNIT;
 | 
			
		||||
        dst->space(-next);
 | 
			
		||||
        next = RC6_UNIT;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (next > 0) {
 | 
			
		||||
    dst->mark(next);
 | 
			
		||||
  } else {
 | 
			
		||||
    dst->space(-next);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
optional<RC6Data> RC6Protocol::decode(RemoteReceiveData src) {
 | 
			
		||||
  RC6Data data{
 | 
			
		||||
      .mode = 0,
 | 
			
		||||
      .toggle = 0,
 | 
			
		||||
      .address = 0,
 | 
			
		||||
      .command = 0,
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  // Check if header matches
 | 
			
		||||
  if (!src.expect_item(RC6_HEADER_MARK, RC6_HEADER_SPACE)) {
 | 
			
		||||
    return {};
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  uint8_t bit{1};
 | 
			
		||||
  uint8_t offset{0};
 | 
			
		||||
  uint8_t header{0};
 | 
			
		||||
  uint32_t buffer{0};
 | 
			
		||||
 | 
			
		||||
  // Startbit + mode
 | 
			
		||||
  while (offset < 4) {
 | 
			
		||||
    bit = src.peek() > 0;
 | 
			
		||||
    header = header + (bit << (3 - offset++));
 | 
			
		||||
    src.advance();
 | 
			
		||||
 | 
			
		||||
    if (src.peek_mark(RC6_UNIT) || src.peek_space(RC6_UNIT)) {
 | 
			
		||||
      src.advance();
 | 
			
		||||
    } else if (offset == 4) {
 | 
			
		||||
      break;
 | 
			
		||||
    } else if (!src.peek_mark(RC6_UNIT * 2) && !src.peek_space(RC6_UNIT * 2)) {
 | 
			
		||||
      return {};
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  data.mode = header & RC6_MODE_MASK;
 | 
			
		||||
 | 
			
		||||
  if (data.mode != 0) {
 | 
			
		||||
    return {};  // I dont have a device to test other modes
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Toggle
 | 
			
		||||
  data.toggle = src.peek() > 0;
 | 
			
		||||
  src.advance();
 | 
			
		||||
  if (src.peek_mark(RC6_UNIT * 2) || src.peek_space(RC6_UNIT * 2)) {
 | 
			
		||||
    src.advance();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Data
 | 
			
		||||
  offset = 0;
 | 
			
		||||
  while (offset < 16) {
 | 
			
		||||
    bit = src.peek() > 0;
 | 
			
		||||
    buffer = buffer + (bit << (15 - offset++));
 | 
			
		||||
    src.advance();
 | 
			
		||||
 | 
			
		||||
    if (offset == 16) {
 | 
			
		||||
      break;
 | 
			
		||||
    } else if (src.peek_mark(RC6_UNIT) || src.peek_space(RC6_UNIT)) {
 | 
			
		||||
      src.advance();
 | 
			
		||||
    } else if (!src.peek_mark(RC6_UNIT * 2) && !src.peek_space(RC6_UNIT * 2)) {
 | 
			
		||||
      return {};
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  data.address = (0xFF00 & buffer) >> 8;
 | 
			
		||||
  data.command = (0x00FF & buffer);
 | 
			
		||||
  return data;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void RC6Protocol::dump(const RC6Data &data) {
 | 
			
		||||
  ESP_LOGD(RC6_TAG, "Received RC6: mode=0x%X, address=0x%02X, command=0x%02X, toggle=0x%X", data.mode, data.address,
 | 
			
		||||
           data.command, data.toggle);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace remote_base
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
							
								
								
									
										46
									
								
								esphome/components/remote_base/rc6_protocol.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								esphome/components/remote_base/rc6_protocol.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "remote_base.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace remote_base {
 | 
			
		||||
 | 
			
		||||
struct RC6Data {
 | 
			
		||||
  uint8_t mode : 3;
 | 
			
		||||
  uint8_t toggle : 1;
 | 
			
		||||
  uint8_t address;
 | 
			
		||||
  uint8_t command;
 | 
			
		||||
 | 
			
		||||
  bool operator==(const RC6Data &rhs) const { return address == rhs.address && command == rhs.command; }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class RC6Protocol : public RemoteProtocol<RC6Data> {
 | 
			
		||||
 public:
 | 
			
		||||
  void encode(RemoteTransmitData *dst, const RC6Data &data) override;
 | 
			
		||||
  optional<RC6Data> decode(RemoteReceiveData src) override;
 | 
			
		||||
  void dump(const RC6Data &data) override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
DECLARE_REMOTE_PROTOCOL(RC6)
 | 
			
		||||
 | 
			
		||||
template<typename... Ts> class RC6Action : public RemoteTransmitterActionBase<Ts...> {
 | 
			
		||||
 public:
 | 
			
		||||
  TEMPLATABLE_VALUE(uint8_t, address)
 | 
			
		||||
  TEMPLATABLE_VALUE(uint8_t, command)
 | 
			
		||||
 | 
			
		||||
  void encode(RemoteTransmitData *dst, Ts... x) {
 | 
			
		||||
    RC6Data data{};
 | 
			
		||||
    data.mode = 0;
 | 
			
		||||
    data.toggle = this->toggle_;
 | 
			
		||||
    data.address = this->address_.value(x...);
 | 
			
		||||
    data.command = this->command_.value(x...);
 | 
			
		||||
    RC6Protocol().encode(dst, data);
 | 
			
		||||
    this->toggle_ = !this->toggle_;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  uint8_t toggle_{0};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace remote_base
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -103,7 +103,7 @@ void IRAM_ATTR HOT RotaryEncoderSensorStore::gpio_intr(RotaryEncoderSensorStore
 | 
			
		||||
    rotation_dir = -1;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (rotation_dir != 0) {
 | 
			
		||||
  if (rotation_dir != 0 && !arg->first_read) {
 | 
			
		||||
    auto *first_zero = std::find(arg->rotation_events.begin(), arg->rotation_events.end(), 0);  // find first zero
 | 
			
		||||
    if (first_zero == arg->rotation_events.begin()  // are we at the start (first event this loop iteration)
 | 
			
		||||
        || std::signbit(*std::prev(first_zero)) !=
 | 
			
		||||
@@ -119,6 +119,7 @@ void IRAM_ATTR HOT RotaryEncoderSensorStore::gpio_intr(RotaryEncoderSensorStore
 | 
			
		||||
      *std::prev(first_zero) += rotation_dir;  // store the rotation into the previous slot
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  arg->first_read = false;
 | 
			
		||||
 | 
			
		||||
  arg->state = new_state;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -34,6 +34,7 @@ struct RotaryEncoderSensorStore {
 | 
			
		||||
  int32_t max_value{INT32_MAX};
 | 
			
		||||
  int32_t last_read{0};
 | 
			
		||||
  uint8_t state{0};
 | 
			
		||||
  bool first_read{true};
 | 
			
		||||
 | 
			
		||||
  std::array<int8_t, 8> rotation_events{};
 | 
			
		||||
  bool rotation_events_overflow{false};
 | 
			
		||||
 
 | 
			
		||||
@@ -11,7 +11,7 @@ template<typename... Ts> class PerformForcedCalibrationAction : public Action<Ts
 | 
			
		||||
 public:
 | 
			
		||||
  void play(Ts... x) override {
 | 
			
		||||
    if (this->value_.has_value()) {
 | 
			
		||||
      this->parent_->perform_forced_calibration(value_.value());
 | 
			
		||||
      this->parent_->perform_forced_calibration(this->value_.value(x...));
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -40,7 +40,7 @@ void SDP3XComponent::setup() {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    uint16_t data[6];
 | 
			
		||||
    if (this->read_data(data, 6) != i2c::ERROR_OK) {
 | 
			
		||||
    if (!this->read_data(data, 6)) {
 | 
			
		||||
      ESP_LOGE(TAG, "Read ID SDP3X failed!");
 | 
			
		||||
      this->mark_failed();
 | 
			
		||||
      return;
 | 
			
		||||
@@ -78,8 +78,7 @@ void SDP3XComponent::setup() {
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (this->write_command(measurement_mode_ == DP_AVG ? SDP3X_START_DP_AVG : SDP3X_START_MASS_FLOW_AVG) !=
 | 
			
		||||
        i2c::ERROR_OK) {
 | 
			
		||||
    if (!this->write_command(measurement_mode_ == DP_AVG ? SDP3X_START_DP_AVG : SDP3X_START_MASS_FLOW_AVG)) {
 | 
			
		||||
      ESP_LOGE(TAG, "Start Measurements SDP3X failed!");
 | 
			
		||||
      this->mark_failed();
 | 
			
		||||
      return;
 | 
			
		||||
@@ -98,7 +97,7 @@ void SDP3XComponent::dump_config() {
 | 
			
		||||
 | 
			
		||||
void SDP3XComponent::read_pressure_() {
 | 
			
		||||
  uint16_t data[3];
 | 
			
		||||
  if (this->read_data(data, 3) != i2c::ERROR_OK) {
 | 
			
		||||
  if (!this->read_data(data, 3)) {
 | 
			
		||||
    ESP_LOGW(TAG, "Couldn't read SDP3X data!");
 | 
			
		||||
    this->status_set_warning();
 | 
			
		||||
    return;
 | 
			
		||||
 
 | 
			
		||||
@@ -58,7 +58,5 @@ optional<std::string> Select::at(size_t index) const {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint32_t Select::hash_base() { return 2812997003UL; }
 | 
			
		||||
 | 
			
		||||
}  // namespace select
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 
 | 
			
		||||
@@ -65,8 +65,6 @@ class Select : public EntityBase {
 | 
			
		||||
   */
 | 
			
		||||
  virtual void control(const std::string &value) = 0;
 | 
			
		||||
 | 
			
		||||
  uint32_t hash_base() override;
 | 
			
		||||
 | 
			
		||||
  CallbackManager<void(std::string, size_t)> state_callback_;
 | 
			
		||||
  bool has_state_{false};
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
Some files were not shown because too many files have changed in this diff Show More
		Reference in New Issue
	
	Block a user