diff --git a/.gitignore b/.gitignore index 47180ab..56649e0 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,15 @@ # Whitelist subdirectory yaml files !*/*.yaml !*/*.yml +!*/*.md +!*/*.sh +!*/*.js* +!*/*.txt* +!*/*.json* +!*/*.ui.yaml +!*/*.h +!*/*.py + # Whitelist specific folders (uncomment as needed) !automations/ @@ -24,7 +33,10 @@ !esphome/ha-remote/ !esphome/ha-remote/components/ !esphome/ha-remote/components/** -!custom_components/ +!esphome/ha-remote/components/max17043/ +!esphome/components/** +!esphome/components/max17043/** +!custom_components/** # !packages/ # !themes/ # !node-red/ diff --git a/esphome/MAX17048_component.h b/esphome/MAX17048_component.h new file mode 100644 index 0000000..99f278a --- /dev/null +++ b/esphome/MAX17048_component.h @@ -0,0 +1,47 @@ +#include "esphome.h" + +#define MAX17048_ADDRESS 0x36 +#define MAX17048_VCELL 0x02 // voltage +#define MAX17048_SOC 0x04 // percentage +#define MAX17048_MODE 0x06 +#define MAX17048_VERSION 0x08 +#define MAX17048_CONFIG 0x0c +#define MAX17048_COMMAND 0xfe + +class MAX17048Sensor : public PollingComponent, public Sensor { + public: + Sensor *voltage_sensor = new Sensor(); + Sensor *percentage_sensor = new Sensor(); + + MAX17048Sensor() : PollingComponent(10000) {} + + void setup() override { + // Initialize the device here. Usually Wire.begin() will be called in here, + // though that call is unnecessary if you have an 'i2c:' entry in your config + ESP_LOGD("custom", "Starting up MAX17048 sensor"); + + Wire.begin(); + } + + uint16_t read16(uint8_t reg) { + uint16_t temp; + Wire.begin(); + Wire.beginTransmission(MAX17048_ADDRESS); + Wire.write(reg); + Wire.endTransmission(); + Wire.requestFrom(MAX17048_ADDRESS, 2); + temp = (uint16_t)Wire.read() << 8; + temp |= (uint16_t)Wire.read(); + Wire.endTransmission(); + return temp; + } + + void update() override { + float voltage = (float)(read16(MAX17048_VCELL)) * 78.125 / 1000000; + voltage_sensor->publish_state(voltage); + + uint16_t percentage_tmp = read16(MAX17048_SOC); + float percentage = (float)(percentage_tmp) / 256; + percentage_sensor->publish_state(percentage); + } +}; \ No newline at end of file