1
0
mirror of https://github.com/esphome/esphome.git synced 2025-08-15 10:39:35 +01:00
Files
.devcontainer
.github
.vscode
docker
esphome
api
components
a4988
ac_dimmer
adalight
adc
addressable_light
ade7953
ads1115
aht10
am2320
animation
apds9960
api
as3935
as3935_i2c
as3935_spi
async_tcp
atc_mithermometer
atm90e32
b_parasite
bang_bang
bh1750
binary
binary_sensor
binary_sensor_map
ble_client
ble_presence
ble_rssi
ble_scanner
bme280
bme680
bme680_bsec
bmp085
bmp280
canbus
captive_portal
ccs811
climate
climate_ir
climate_ir_lg
color
coolix
cover
cs5460a
cse7766
ct_clamp
custom
custom_component
cwww
daikin
dallas
debug
deep_sleep
dfplayer
dht
dht12
display
ds1307
duty_cycle
e131
endstop
esp32_ble
esp32_ble_beacon
esp32_ble_tracker
esp32_camera
esp32_dac
esp32_hall
esp32_improv
esp32_touch
esp8266_pwm
ethernet
exposure_notifications
external_components
ezo
fan
fastled_base
fastled_clockless
fastled_spi
fingerprint_grow
font
fujitsu_general
globals
gpio
gps
hbridge
hdc1080
hitachi_ac344
hlw8012
hm3301
hmc5883l
homeassistant
http_request
__init__.py
http_request.cpp
http_request.h
htu21d
hx711
i2c
ili9341
image
improv
ina219
ina226
ina3221
inkbird_ibsth1_mini
inkplate6
integration
interval
json
lcd_base
lcd_gpio
lcd_pcf8574
ledc
light
logger
max31855
max31856
max31865
max6675
max7219
max7219digit
mcp23008
mcp23016
mcp23017
mcp23s08
mcp23s17
mcp23x08_base
mcp23x17_base
mcp23xxx_base
mcp2515
mcp3008
mcp4725
mcp9808
mhz19
midea_ac
midea_dongle
mitsubishi
modbus
monochromatic
mpr121
mpu6050
mqtt
mqtt_subscribe
ms5611
my9231
neopixelbus
network
nextion
nfc
ntc
ota
output
packages
partition
pca9685
pcd8544
pcf8574
pid
pmsx003
pn532
pn532_i2c
pn532_spi
power_supply
prometheus
pulse_counter
pulse_meter
pulse_width
pzem004t
pzemac
pzemdc
qmc5883l
rc522
rc522_i2c
rc522_spi
rdm6300
remote_base
remote_receiver
remote_transmitter
resistance
restart
rf_bridge
rgb
rgbw
rgbww
rotary_encoder
rtttl
ruuvi_ble
ruuvitag
scd30
script
sdm_meter
sds011
senseair
sensor
servo
sgp30
sgp40
sht3xd
sht4x
shtcx
shutdown
sim800l
slow_pwm
sm16716
sm2135
sm300d2
sn74hc595
sntp
speed
spi
sps30
ssd1306_base
ssd1306_i2c
ssd1306_spi
ssd1322_base
ssd1322_spi
ssd1325_base
ssd1325_spi
ssd1327_base
ssd1327_i2c
ssd1327_spi
ssd1331_base
ssd1331_spi
ssd1351_base
ssd1351_spi
st7735
st7789v
status
status_led
stepper
sts3x
substitutions
sun
switch
sx1509
tca9548a
tcl112
tcs34725
teleinfo
template
text_sensor
thermostat
time
time_based
tlc59208f
tm1637
tm1651
tmp102
tmp117
tof10120
toshiba
total_daily_energy
tsl2561
ttp229_bsf
ttp229_lsf
tuya
tx20
uart
uln2003
ultrasonic
uptime
version
vl53l0x
voltage_sampler
waveshare_epaper
web_server
web_server_base
whirlpool
wifi
wifi_info
wifi_signal
wled
xiaomi_ble
xiaomi_cgd1
xiaomi_cgdk2
xiaomi_cgg1
xiaomi_gcls002
xiaomi_hhccjcy01
xiaomi_hhccpot002
xiaomi_jqjcy01ym
xiaomi_lywsd02
xiaomi_lywsd03mmc
xiaomi_lywsdcgq
xiaomi_mhoc401
xiaomi_miflora
xiaomi_mijia
xiaomi_miscale
xiaomi_miscale2
xiaomi_mjyd02yla
xiaomi_mue4094rt
xiaomi_wx08zm
xpt2046
yashima
zyaura
__init__.py
core
dashboard
__init__.py
__main__.py
automation.py
codegen.py
config.py
config_helpers.py
config_validation.py
const.py
coroutine.py
cpp_generator.py
cpp_helpers.py
cpp_types.py
espota2.py
helpers.py
jsonschema.py
legacy.py
loader.py
log.py
mqtt.py
pins.py
platformio_api.py
storage_json.py
util.py
voluptuous_schema.py
vscode.py
wizard.py
writer.py
yaml_util.py
zeroconf.py
script
tests
.clang-format
.clang-tidy
.coveragerc
.dockerignore
.editorconfig
.gitignore
.gitpod.yml
.pre-commit-config.yaml
CODEOWNERS
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE
MANIFEST.in
README.md
platformio.ini
pylintrc
pyproject.toml
pytest.ini
requirements.txt
requirements_optional.txt
requirements_test.txt
setup.cfg
setup.py
esphome/esphome/components/http_request/http_request.h
2021-06-10 13:04:40 +02:00

134 lines
4.4 KiB
C++

#pragma once
#include "esphome/components/json/json_util.h"
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include <list>
#include <map>
#include <utility>
#ifdef ARDUINO_ARCH_ESP32
#include <HTTPClient.h>
#endif
#ifdef ARDUINO_ARCH_ESP8266
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h>
#endif
namespace esphome {
namespace http_request {
struct Header {
const char *name;
const char *value;
};
class HttpRequestResponseTrigger;
class HttpRequestComponent : public Component {
public:
void dump_config() override;
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
void set_url(std::string url);
void set_method(const char *method) { this->method_ = method; }
void set_useragent(const char *useragent) { this->useragent_ = useragent; }
void set_timeout(uint16_t timeout) { this->timeout_ = timeout; }
void set_body(std::string body) { this->body_ = std::move(body); }
void set_headers(std::list<Header> headers) { this->headers_ = std::move(headers); }
void send(const std::vector<HttpRequestResponseTrigger *> &response_triggers);
void close();
const char *get_string();
protected:
HTTPClient client_{};
std::string url_;
std::string last_url_;
const char *method_;
const char *useragent_{nullptr};
bool secure_;
uint16_t timeout_{5000};
std::string body_;
std::list<Header> headers_;
#ifdef ARDUINO_ARCH_ESP8266
WiFiClient *wifi_client_{nullptr};
BearSSL::WiFiClientSecure *wifi_client_secure_{nullptr};
WiFiClient *get_wifi_client_();
#endif
};
template<typename... Ts> class HttpRequestSendAction : public Action<Ts...> {
public:
HttpRequestSendAction(HttpRequestComponent *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(std::string, url)
TEMPLATABLE_VALUE(const char *, method)
TEMPLATABLE_VALUE(std::string, body)
TEMPLATABLE_VALUE(const char *, useragent)
TEMPLATABLE_VALUE(uint16_t, timeout)
void add_header(const char *key, TemplatableValue<const char *, Ts...> value) { this->headers_.insert({key, value}); }
void add_json(const char *key, TemplatableValue<std::string, Ts...> value) { this->json_.insert({key, value}); }
void set_json(std::function<void(Ts..., JsonObject &)> json_func) { this->json_func_ = json_func; }
void register_response_trigger(HttpRequestResponseTrigger *trigger) { this->response_triggers_.push_back(trigger); }
void play(Ts... x) override {
this->parent_->set_url(this->url_.value(x...));
this->parent_->set_method(this->method_.value(x...));
if (this->body_.has_value()) {
this->parent_->set_body(this->body_.value(x...));
}
if (!this->json_.empty()) {
auto f = std::bind(&HttpRequestSendAction<Ts...>::encode_json_, this, x..., std::placeholders::_1);
this->parent_->set_body(json::build_json(f));
}
if (this->json_func_ != nullptr) {
auto f = std::bind(&HttpRequestSendAction<Ts...>::encode_json_func_, this, x..., std::placeholders::_1);
this->parent_->set_body(json::build_json(f));
}
if (this->useragent_.has_value()) {
this->parent_->set_useragent(this->useragent_.value(x...));
}
if (this->timeout_.has_value()) {
this->parent_->set_timeout(this->timeout_.value(x...));
}
if (!this->headers_.empty()) {
std::list<Header> headers;
for (const auto &item : this->headers_) {
auto val = item.second;
Header header;
header.name = item.first;
header.value = val.value(x...);
headers.push_back(header);
}
this->parent_->set_headers(headers);
}
this->parent_->send(this->response_triggers_);
this->parent_->close();
}
protected:
void encode_json_(Ts... x, JsonObject &root) {
for (const auto &item : this->json_) {
auto val = item.second;
root[item.first] = val.value(x...);
}
}
void encode_json_func_(Ts... x, JsonObject &root) { this->json_func_(x..., root); }
HttpRequestComponent *parent_;
std::map<const char *, TemplatableValue<const char *, Ts...>> headers_{};
std::map<const char *, TemplatableValue<std::string, Ts...>> json_{};
std::function<void(Ts..., JsonObject &)> json_func_{nullptr};
std::vector<HttpRequestResponseTrigger *> response_triggers_;
};
class HttpRequestResponseTrigger : public Trigger<int> {
public:
void process(int status_code) { this->trigger(status_code); }
};
} // namespace http_request
} // namespace esphome