From 8f093823672a49d63d9276c4072d2a3189d194d6 Mon Sep 17 00:00:00 2001
From: Philippe Wechsler <29612400+MadMonkey87@users.noreply.github.com>
Date: Wed, 14 Aug 2024 04:25:45 +0200
Subject: [PATCH] support illuminance for airthings wave plus device (#5203)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
---
 .../airthings_wave_plus/airthings_wave_plus.cpp      |  7 +++++--
 .../airthings_wave_plus/airthings_wave_plus.h        |  2 ++
 esphome/components/airthings_wave_plus/sensor.py     | 12 ++++++++++++
 3 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/esphome/components/airthings_wave_plus/airthings_wave_plus.cpp b/esphome/components/airthings_wave_plus/airthings_wave_plus.cpp
index a32128e992..8c8c514fdb 100644
--- a/esphome/components/airthings_wave_plus/airthings_wave_plus.cpp
+++ b/esphome/components/airthings_wave_plus/airthings_wave_plus.cpp
@@ -14,8 +14,6 @@ void AirthingsWavePlus::read_sensors(uint8_t *raw_value, uint16_t value_len) {
     ESP_LOGD(TAG, "version = %d", value->version);
 
     if (value->version == 1) {
-      ESP_LOGD(TAG, "ambient light = %d", value->ambientLight);
-
       if (this->humidity_sensor_ != nullptr) {
         this->humidity_sensor_->publish_state(value->humidity / 2.0f);
       }
@@ -43,6 +41,10 @@ void AirthingsWavePlus::read_sensors(uint8_t *raw_value, uint16_t value_len) {
       if ((this->tvoc_sensor_ != nullptr) && this->is_valid_voc_value_(value->voc)) {
         this->tvoc_sensor_->publish_state(value->voc);
       }
+
+      if (this->illuminance_sensor_ != nullptr) {
+        this->illuminance_sensor_->publish_state(value->ambientLight);
+      }
     } else {
       ESP_LOGE(TAG, "Invalid payload version (%d != 1, newer version or not a Wave Plus?)", value->version);
     }
@@ -68,6 +70,7 @@ void AirthingsWavePlus::dump_config() {
   LOG_SENSOR("  ", "Radon", this->radon_sensor_);
   LOG_SENSOR("  ", "Radon Long Term", this->radon_long_term_sensor_);
   LOG_SENSOR("  ", "CO2", this->co2_sensor_);
+  LOG_SENSOR("  ", "Illuminance", this->illuminance_sensor_);
 }
 
 AirthingsWavePlus::AirthingsWavePlus() {
diff --git a/esphome/components/airthings_wave_plus/airthings_wave_plus.h b/esphome/components/airthings_wave_plus/airthings_wave_plus.h
index 23c8cbb166..bd7a40ef8b 100644
--- a/esphome/components/airthings_wave_plus/airthings_wave_plus.h
+++ b/esphome/components/airthings_wave_plus/airthings_wave_plus.h
@@ -22,6 +22,7 @@ class AirthingsWavePlus : public airthings_wave_base::AirthingsWaveBase {
   void set_radon(sensor::Sensor *radon) { radon_sensor_ = radon; }
   void set_radon_long_term(sensor::Sensor *radon_long_term) { radon_long_term_sensor_ = radon_long_term; }
   void set_co2(sensor::Sensor *co2) { co2_sensor_ = co2; }
+  void set_illuminance(sensor::Sensor *illuminance) { illuminance_sensor_ = illuminance; }
 
  protected:
   bool is_valid_radon_value_(uint16_t radon);
@@ -32,6 +33,7 @@ class AirthingsWavePlus : public airthings_wave_base::AirthingsWaveBase {
   sensor::Sensor *radon_sensor_{nullptr};
   sensor::Sensor *radon_long_term_sensor_{nullptr};
   sensor::Sensor *co2_sensor_{nullptr};
+  sensor::Sensor *illuminance_sensor_{nullptr};
 
   struct WavePlusReadings {
     uint8_t version;
diff --git a/esphome/components/airthings_wave_plus/sensor.py b/esphome/components/airthings_wave_plus/sensor.py
index 643a2bfb68..d28c7e2abc 100644
--- a/esphome/components/airthings_wave_plus/sensor.py
+++ b/esphome/components/airthings_wave_plus/sensor.py
@@ -12,6 +12,9 @@ from esphome.const import (
     CONF_CO2,
     UNIT_BECQUEREL_PER_CUBIC_METER,
     UNIT_PARTS_PER_MILLION,
+    CONF_ILLUMINANCE,
+    UNIT_LUX,
+    DEVICE_CLASS_ILLUMINANCE,
 )
 
 DEPENDENCIES = airthings_wave_base.DEPENDENCIES
@@ -45,6 +48,12 @@ CONFIG_SCHEMA = airthings_wave_base.BASE_SCHEMA.extend(
             device_class=DEVICE_CLASS_CARBON_DIOXIDE,
             state_class=STATE_CLASS_MEASUREMENT,
         ),
+        cv.Optional(CONF_ILLUMINANCE): sensor.sensor_schema(
+            unit_of_measurement=UNIT_LUX,
+            accuracy_decimals=0,
+            device_class=DEVICE_CLASS_ILLUMINANCE,
+            state_class=STATE_CLASS_MEASUREMENT,
+        ),
     }
 )
 
@@ -62,3 +71,6 @@ async def to_code(config):
     if config_co2 := config.get(CONF_CO2):
         sens = await sensor.new_sensor(config_co2)
         cg.add(var.set_co2(sens))
+    if config_illuminance := config.get(CONF_ILLUMINANCE):
+        sens = await sensor.new_sensor(config_illuminance)
+        cg.add(var.set_illuminance(sens))