Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions components/powerhub/binary_sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@

DEPENDENCIES = ["powerhub"]

CONF_VIN_STATUS = "vin_status"

# the Top PMU button is a square shape
ICON_GESTURE_TAP_BUTTON = "mdi:gesture-tap-button"

CONFIG_SCHEMA = (
cv.Schema(
{
cv.Optional(CONF_BUTTON) : binary_sensor.binary_sensor_schema(
icon=ICON_GESTURE_TAP_BUTTON
)
cv.Optional(CONF_BUTTON): binary_sensor.binary_sensor_schema(icon=ICON_GESTURE_TAP_BUTTON).extend(
{
cv.Optional("update_interval", default=20): cv.positive_int,
}
),
cv.Optional(CONF_VIN_STATUS): binary_sensor.binary_sensor_schema(icon="mdi:power-plug").extend(
{
cv.Optional("update_interval", default=500): cv.positive_int,
}
),
}
)
.extend(BASE_SCHEMA)
Expand All @@ -28,4 +37,10 @@ async def to_code(config):

if CONF_BUTTON in config:
sens = await binary_sensor.new_binary_sensor(config[CONF_BUTTON])
cg.add(powerhub.set_button_binary_sensor(sens))
cg.add(powerhub.set_button_binary_sensor(sens))
cg.add(powerhub.set_button_update_interval(config[CONF_BUTTON]["update_interval"]))

if CONF_VIN_STATUS in config:
sens = await binary_sensor.new_binary_sensor(config[CONF_VIN_STATUS])
cg.add(powerhub.set_vin_status_binary_sensor(sens))
cg.add(powerhub.set_vin_status_update_interval(config[CONF_VIN_STATUS]["update_interval"]))
28 changes: 24 additions & 4 deletions components/powerhub/powerhub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,17 +248,23 @@ void PowerHub::setup() {

#ifdef USE_BINARY_SENSOR
// Inteval used to read PMU button status
// polling every 20ms
// polling every button_update_interval_ ms
this->button_binary_sensor_->publish_initial_state(false);
this->set_interval("pmu_button", 20, [this]() {
this->set_interval("pmu_button", this->button_update_interval_, [this]() {
this->update_pmu_button_sensor();
});

// Interval for VIN status
this->vin_status_binary_sensor_->publish_initial_state(false);
this->set_interval("vin_status", this->vin_status_update_interval_, [this]() {
this->update_vin_status_sensor();
});
#endif

#ifdef USE_TEXT_SENSOR
// Interval used to read charge/vin status
// polling every 500 ms (0.5s)
this->set_interval("charge_vin_status", 500, [this]() {
// polling every charge_status_update_interval_ ms
this->set_interval("charge_vin_status", this->charge_status_update_interval_, [this]() {
this->update_charge_vin_sensor();
});
#endif
Expand All @@ -284,6 +290,7 @@ void PowerHub::dump_config() {
#ifdef USE_BINARY_SENSOR
ESP_LOGCONFIG(TAG, "Binary Sensor:");
LOG_BINARY_SENSOR(" ", "Button", this->button_binary_sensor_);
LOG_BINARY_SENSOR(" ", "VIN Status", this->vin_status_binary_sensor_);
#endif

#ifdef USE_SENSOR
Expand Down Expand Up @@ -732,6 +739,19 @@ void PowerHub::update_pmu_button_sensor() {
#endif
}

void PowerHub::update_vin_status_sensor() {
#ifdef USE_BINARY_SENSOR
if (this->vin_status_binary_sensor_) {
uint8_t vin_status = this->read_vin_status();
bool has_input = (vin_status == 1);
if (has_input != this->last_vin_state_) {
this->last_vin_state_ = has_input;
this->vin_status_binary_sensor_->publish_state(has_input);
}
}
#endif
}

void PowerHub::set_wake_nstby(bool en) {
uint8_t val = en ? 1 : 0;
WARN_IF(this->write_register(Register::REG_NSTBY_WKUP, &val ,1));
Expand Down
18 changes: 18 additions & 0 deletions components/powerhub/powerhub.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class PowerHub : public PollingComponent, public i2c::I2CDevice {

#ifdef USE_BINARY_SENSOR
SUB_BINARY_SENSOR(button)
SUB_BINARY_SENSOR(vin_status)
#endif


Expand Down Expand Up @@ -249,6 +250,9 @@ class PowerHub : public PollingComponent, public i2c::I2CDevice {
// Because we need to get the button status fast
void update_pmu_button_sensor();

// Update VIN status binary sensor
void update_vin_status_sensor();


// RTC related
// Wakeup getters/setters
Expand Down Expand Up @@ -281,6 +285,12 @@ class PowerHub : public PollingComponent, public i2c::I2CDevice {
// i2c address
uint8_t get_i2c_addr() const { return this->i2c_address_; }

// Set update intervals
void set_button_update_interval(uint32_t interval) { this->button_update_interval_ = interval; }
void set_charging_status_update_interval(uint32_t interval) { this->vin_status_update_interval_ = interval; }
void set_charge_status_update_interval(uint32_t interval) { this->charge_status_update_interval_ = interval; }
void set_vin_status_update_interval(uint32_t interval) { this->vin_status_update_interval_ = interval; }

protected:
// Power enable
bool led_power_enabled_;
Expand All @@ -295,6 +305,9 @@ class PowerHub : public PollingComponent, public i2c::I2CDevice {
// Used to store the last PMU button state
bool last_button_state_{false};

// Used to store the last VIN state
bool last_vin_state_{false};

// USB mode
uint8_t usb_mode_;

Expand Down Expand Up @@ -323,6 +336,11 @@ class PowerHub : public PollingComponent, public i2c::I2CDevice {
uint8_t last_charge_status_{0xFF}; // Initialize to invalid value
uint8_t last_vin_status_{0xFF};

// Update intervals in milliseconds
uint32_t button_update_interval_{20};
uint32_t charge_status_update_interval_{500};
uint32_t vin_status_update_interval_{500};

// LED colors
BGR_t led_usb_c_color_;
BGR_t led_usb_a_color_;
Expand Down
17 changes: 11 additions & 6 deletions components/powerhub/text_sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@
CONFIG_SCHEMA = (
cv.Schema(
{
cv.Optional(CONF_CHARGE_STATUS) : text_sensor.text_sensor_schema(
icon=ICON_POWER_PLUG_BATTERY,
cv.Optional(CONF_CHARGE_STATUS): text_sensor.text_sensor_schema(icon=ICON_POWER_PLUG_BATTERY).extend(
{
cv.Optional("update_interval", default=500): cv.positive_int,
}
),
cv.Optional(CONF_VIN_STATUS): text_sensor.text_sensor_schema(icon=ICON_POWER_PLUG).extend(
{
cv.Optional("update_interval", default=500): cv.positive_int,
}
),
cv.Optional(CONF_VIN_STATUS) : text_sensor.text_sensor_schema(
icon=ICON_POWER_PLUG,
)
}
)
.extend(BASE_SCHEMA)
.extend(cv.polling_component_schema("10s"))
)


Expand All @@ -41,7 +44,9 @@ async def to_code(config):
if CONF_CHARGE_STATUS in config:
ts = await text_sensor.new_text_sensor(config[CONF_CHARGE_STATUS])
cg.add(powerhub.set_charge_status_text_sensor(ts))
cg.add(powerhub.set_charge_status_update_interval(config[CONF_CHARGE_STATUS]["update_interval"]))

if CONF_VIN_STATUS in config:
ts = await text_sensor.new_text_sensor(config[CONF_VIN_STATUS])
cg.add(powerhub.set_vin_status_text_sensor(ts))
cg.add(powerhub.set_vin_status_update_interval(config[CONF_VIN_STATUS]["update_interval"]))