Control digital potentiometer through home assistant via esphome?

After many false starts, I finally figured it out.

First, you’ll need to setup a custom float component and override the write_state method. I also added a separate method named digitalPotWrite, but you could put it all in the write_state method.
This file needs to be put in “/config/esphome/”.

Here’s what I did (I named it “CustomDigipotOutput.h”).

#include "esphome.h"
using namespace esphome;
#include <SPI.h>

byte address = 0x00;
int CS= 5;

class CustomDigipotOutput : public Component, public FloatOutput {
 public:
    void setup() override {
    // This will be called by App.setup()
    pinMode(CS, OUTPUT);
    }

    //write to Pot
    void digitalPotWrite(int value)
    {
      SPI.begin();      
      delay(50);
      digitalWrite(CS, LOW);
      delay(50);
      SPI.transfer(address);
      SPI.transfer(value);
      digitalWrite(CS, HIGH);
    }  

    void write_state(float state) override {
    // state is the amount this output should be on, from 0.0 to 1.0

    // we need to convert it to an integer first
    int value = state * 128;
    digitalPotWrite(value);
    }
};

Next you need to add the spi definition, output component and a service API to your Device configuration in ESPHome and install it.

# SPI pins definition
spi:
  clk_pin: GPIO18
  mosi_pin: GPIO23
  miso_pin: GPIO19
output:
- platform: custom
  type: float
  lambda: |-
    auto custom_digipot_output = new CustomDigipotOutput();
    App.register_component(custom_digipot_output);
    return {custom_digipot_output};

  outputs:
    id: thermostat_digipot



api:
  services:
    - service: control_pot
      variables:
        level: float
      then:
        - output.set_level:
            id: thermostat_digipot
            level: !lambda 'return level / 100.0;'

Assuming everything installs successfully to your device, you should be able to test it out using Developer Tools Services.

I used that to set the output on digipot for all values from 0 to 100 and measured the resistance with a multimeter.

Next step is to build an automation that will set the resistance based on a temperature per the 10K thermistor chart.

I hope this is helpful for someone.