Skip to content

Commit b110300

Browse files
committed
move values to constants
1 parent 7725b6a commit b110300

File tree

3 files changed

+66
-13
lines changed

3 files changed

+66
-13
lines changed

components/ip5306/__init__.py

+33
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@
1313
CONF_CHARGER_CONNECTED = "charger_connected"
1414
CONF_CHARGER_ACTIVE = "charger_active"
1515
CONF_CHARGE_FULL = "charge_full"
16+
CONF_POWER_BOOST_ON = "power_boost_on"
17+
CONF_POWER_BOOST_SET = "power_boost_set"
18+
CONF_POWER_VIN = "power_vin"
19+
CONF_POWER_BTN = "power_btn"
20+
CONF_POWER_BOOST_KEEP_ON = "power_boost_keep_on"
21+
CONF_AUTO_BOOT_ON_LOAD = "auto_boot_on_load"
22+
CONF_LOW_POWER_SHUTDOWN_TIME = "low_power_shutdown_time"
23+
24+
25+
async def ids_to_code(config, var, types):
26+
for key in types:
27+
if key in config:
28+
conf = await cg.get_variable(config[key])
29+
cg.add(getattr(var, f"set_{key}")(conf))
1630

1731
CONFIG_SCHEMA = cv.COMPONENT_SCHEMA.extend(
1832
{
@@ -25,9 +39,26 @@
2539
cv.Optional(CONF_CHARGER_CONNECTED): binary_sensor.binary_sensor_schema(),
2640
cv.Optional(CONF_CHARGE_FULL): binary_sensor.binary_sensor_schema(),
2741
cv.Optional(CONF_CHARGER_ACTIVE): binary_sensor.binary_sensor_schema(),
42+
cv.Optional(CONF_POWER_BOOST_ON, default=True): binary_sensor.binary_sensor_schema(),
43+
cv.Optional(CONF_POWER_BOOST_SET, default=True): binary_sensor.binary_sensor_schema(),
44+
cv.Optional(CONF_POWER_VIN, default=True): binary_sensor.binary_sensor_schema(),
45+
cv.Optional(CONF_POWER_BTN, default=True): binary_sensor.binary_sensor_schema(),
46+
cv.Optional(CONF_POWER_BOOST_KEEP_ON, default=True): binary_sensor.binary_sensor_schema(),
47+
cv.Optional(CONF_AUTO_BOOT_ON_LOAD, default=False): binary_sensor.binary_sensor_schema(),
48+
cv.Optional(CONF_LOW_POWER_SHUTDOWN_TIME, default=64): cv.uint8_t,
2849
}
2950
).extend(i2c.i2c_device_schema(0x75))
3051

52+
IP5306_TYPES = {
53+
CONF_POWER_BOOST_ON,
54+
CONF_POWER_BOOST_SET,
55+
CONF_POWER_VIN,
56+
CONF_POWER_BTN,
57+
CONF_POWER_BOOST_KEEP_ON,
58+
CONF_AUTO_BOOT_ON_LOAD,
59+
CONF_LOW_POWER_SHUTDOWN_TIME
60+
}
61+
3162
async def to_code(config):
3263
var = cg.new_Pvariable(config[CONF_ID])
3364
await cg.register_component(var, config)
@@ -48,3 +79,5 @@ async def to_code(config):
4879
if CONF_CHARGE_FULL in config:
4980
sens = await binary_sensor.new_binary_sensor(config[CONF_CHARGE_FULL])
5081
cg.add(var.set_charge_full(sens))
82+
83+
await ids_to_code(config, var, IP5306_TYPES)

components/ip5306/ip5306.cpp

+16-13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ static const uint8_t IP5306_REG_CHARGE_OUT_BIT = 0x10; // charge out bit
1212

1313
static const uint8_t IP5306_REG_CHG_DIG = 0x24; // charge current
1414
static const uint8_t IP5306_REG_CHG_CTL0 = 0x20; // charge voltage
15+
static const uint8_t IP5306_REG_CHG_CTL1 = 0x21; // charge control
16+
static const uint8_t IP5306_REG_CHG_CTL2 = 0x22; // charge control
17+
static const uint8_t IP5306_REG_CHG_CTL3 = 0x23; // charge control
1518

1619
static const uint8_t IP5306_REG_SYS_CTL0 = 0x00; // initialize
1720
static const uint8_t IP5306_REG_SYS_CTL1 = 0x01; // sys control 1
@@ -67,49 +70,49 @@ void IP5306::setup() {
6770
}
6871
ESP_LOGI(TAG, "ip5306 setChargeVolt done");
6972

70-
if (!setPowerBoostOnOff(true)) {
73+
if (!setPowerBoostOnOff(powerBoostOn_)) {
7174
ESP_LOGE(TAG, "setPowerBoostOnOff failed");
7275
this->mark_failed();
7376
return;
7477
}
7578
ESP_LOGI(TAG, "ip5306 setPowerBoostOnOff done");
7679

77-
if (!setPowerBoostSet(true)) {
80+
if (!setPowerBoostSet(powerBoostSet_)) {
7881
ESP_LOGE(TAG, "setPowerBoostSet failed");
7982
this->mark_failed();
8083
return;
8184
}
8285
ESP_LOGI(TAG, "ip5306 setPowerBoostSet done");
8386

84-
if (!setPowerVin(true)) {
87+
if (!setPowerVin(powerVin_)) {
8588
ESP_LOGE(TAG, "setPowerVin failed");
8689
this->mark_failed();
8790
return;
8891
}
8992
ESP_LOGI(TAG, "ip5306 setPowerVin done");
9093

91-
if (!enablePowerBtn(true)) {
94+
if (!enablePowerBtn(enablePowerBtn_)) {
9295
ESP_LOGE(TAG, "enablePowerBtn failed");
9396
this->mark_failed();
9497
return;
9598
}
9699
ESP_LOGI(TAG, "ip5306 enablePowerBtn done");
97100

98-
if (!setPowerBoostKeepOn(true)) {
101+
if (!setPowerBoostKeepOn(powerBoostKeepOn_)) {
99102
ESP_LOGE(TAG, "setPowerBoostKeepOn failed");
100103
this->mark_failed();
101104
return;
102105
}
103106
ESP_LOGI(TAG, "ip5306 setPowerBoostKeepOn done");
104107

105-
if (!setAutoBootOnLoad(false)) {
108+
if (!setAutoBootOnLoad(autoBootOnLoad_)) {
106109
ESP_LOGE(TAG, "setAutoBootOnLoad failed");
107110
this->mark_failed();
108111
return;
109112
}
110113
ESP_LOGI(TAG, "ip5306 setAutoBootOnLoad done");
111114

112-
if (!setLowPowerShutdownTime(64)) {
115+
if (!setLowPowerShutdownTime(lowPowerShutdownTime_)) {
113116
ESP_LOGE(TAG, "setLowPowerShutdownTime failed");
114117
this->mark_failed();
115118
return;
@@ -121,25 +124,25 @@ void IP5306::completeChargingSetup() {
121124
uint8_t data[1];
122125
uint8_t value;
123126
// End charge current 200ma
124-
if (this->read_register(0x21, data, 1) == i2c::ERROR_OK) {
127+
if (this->read_register(IP5306_REG_CHG_CTL1, data, 1) == i2c::ERROR_OK) {
125128
value = (data[0] & 0x3f) | 0x00;
126-
this->write_register(0x21, &value, 1);
129+
this->write_register(IP5306_REG_CHG_CTL1, &value, 1);
127130
} else {
128131
ESP_LOGE(TAG, "completeChargingSetup read 1 failed");
129132
}
130133

131134
// Add volt 28mv
132-
if (this->read_register(0x22, data, 1) == i2c::ERROR_OK) {
135+
if (this->read_register(IP5306_REG_CHG_CTL2, data, 1) == i2c::ERROR_OK) {
133136
value = (data[0] & 0xfc) | 0x02;
134-
this->write_register(0x22, &value, 1);
137+
this->write_register(IP5306_REG_CHG_CTL2, &value, 1);
135138
} else {
136139
ESP_LOGE(TAG, "completeChargingSetup read 2 failed");
137140
}
138141

139142
// Vin charge CC
140-
if (this->read_register(0x23, data, 1) == i2c::ERROR_OK) {
143+
if (this->read_register(IP5306_REG_CHG_CTL3, data, 1) == i2c::ERROR_OK) {
141144
value = (data[0] & 0xdf) | 0x20;
142-
this->write_register(0x23, &value, 1);
145+
this->write_register(IP5306_REG_CHG_CTL3, &value, 1);
143146
} else {
144147
ESP_LOGE(TAG, "completeChargingSetup read 3 failed");
145148
}

components/ip5306/ip5306.h

+17
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ class IP5306 : public i2c::I2CDevice, public Component {
2020
void set_charger_active(binary_sensor::BinarySensor *sensor) { this->charger_active_ = sensor; }
2121
void set_charge_full(binary_sensor::BinarySensor *sensor) { this->charge_full_ = sensor; }
2222

23+
void set_power_boost_on(bool enabled) { this->powerBoostOn_ = enabled; }
24+
void set_power_boost_set(bool enabled) { this->powerBoostSet_ = enabled; }
25+
void set_power_vin(bool enabled) { this->powerVin_ = enabled; }
26+
void set_power_btn(bool enabled) { this->powerBtn_ = enabled; }
27+
void set_power_boost_keep_on(bool enabled) { this->powerBoostKeepOn_ = enabled; }
28+
void set_auto_boot_on_load(bool enabled) { this->autoBootOnLoad_ = enabled; }
29+
void set_low_power_shutdown_time(int time) { this->lowPowerShutdownTime_ = time; }
30+
2331
protected:
2432
sensor::Sensor *battery_level_{nullptr};
2533
binary_sensor::BinarySensor *charger_connected_{nullptr};
@@ -39,6 +47,15 @@ class IP5306 : public i2c::I2CDevice, public Component {
3947
bool setAutoBootOnLoad(bool enabled);
4048
bool setLowPowerShutdownTime(int time);
4149
void completeChargingSetup();
50+
51+
private:
52+
bool powerBoostOn_{false};
53+
bool powerBoostSet_{false};
54+
bool powerVin_{false};
55+
bool powerBtn_{false};
56+
bool powerBoostKeepOn_{false};
57+
bool autoBootOnLoad_{false};
58+
int lowPowerShutdownTime_{0};
4259
};
4360

4461
} // namespace ip5306

0 commit comments

Comments
 (0)