mirror of
https://github.com/esphome/esphome.git
synced 2024-11-25 00:18:11 +01:00
parent
c60989a7be
commit
714d28a61a
5 changed files with 173 additions and 0 deletions
0
esphome/components/tmp117/__init__.py
Normal file
0
esphome/components/tmp117/__init__.py
Normal file
67
esphome/components/tmp117/sensor.py
Normal file
67
esphome/components/tmp117/sensor.py
Normal file
|
@ -0,0 +1,67 @@
|
|||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import i2c, sensor
|
||||
from esphome.const import CONF_ID, CONF_UPDATE_INTERVAL, \
|
||||
UNIT_CELSIUS, ICON_THERMOMETER
|
||||
|
||||
DEPENDENCIES = ['i2c']
|
||||
|
||||
tmp117_ns = cg.esphome_ns.namespace('tmp117')
|
||||
TMP117Component = tmp117_ns.class_('TMP117Component',
|
||||
cg.PollingComponent, i2c.I2CDevice, sensor.Sensor)
|
||||
|
||||
CONFIG_SCHEMA = cv.All(sensor.sensor_schema(UNIT_CELSIUS, ICON_THERMOMETER, 1).extend({
|
||||
cv.GenerateID(): cv.declare_id(TMP117Component),
|
||||
}).extend(cv.polling_component_schema('60s')).extend(i2c.i2c_device_schema(0x48)))
|
||||
|
||||
|
||||
def determine_config_register(polling_period):
|
||||
if polling_period >= 16.0:
|
||||
# 64 averaged conversions, max conversion time
|
||||
# 0000 00 111 11 00000
|
||||
# 0000 0011 1110 0000
|
||||
return 0x03E0
|
||||
if polling_period >= 8.0:
|
||||
# 64 averaged conversions, high conversion time
|
||||
# 0000 00 110 11 00000
|
||||
# 0000 0011 0110 0000
|
||||
return 0x0360
|
||||
if polling_period >= 4.0:
|
||||
# 64 averaged conversions, mid conversion time
|
||||
# 0000 00 101 11 00000
|
||||
# 0000 0010 1110 0000
|
||||
return 0x02E0
|
||||
if polling_period >= 1.0:
|
||||
# 64 averaged conversions, min conversion time
|
||||
# 0000 00 000 11 00000
|
||||
# 0000 0000 0110 0000
|
||||
return 0x0060
|
||||
if polling_period >= 0.5:
|
||||
# 32 averaged conversions, min conversion time
|
||||
# 0000 00 000 10 00000
|
||||
# 0000 0000 0100 0000
|
||||
return 0x0040
|
||||
if polling_period >= 0.25:
|
||||
# 8 averaged conversions, mid conversion time
|
||||
# 0000 00 010 01 00000
|
||||
# 0000 0001 0010 0000
|
||||
return 0x0120
|
||||
if polling_period >= 0.125:
|
||||
# 8 averaged conversions, min conversion time
|
||||
# 0000 00 000 01 00000
|
||||
# 0000 0000 0010 0000
|
||||
return 0x0020
|
||||
# 1 averaged conversions, min conversion time
|
||||
# 0000 00 000 00 00000
|
||||
# 0000 0000 0000 0000
|
||||
return 0x0000
|
||||
|
||||
|
||||
def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
yield cg.register_component(var, config)
|
||||
yield i2c.register_i2c_device(var, config)
|
||||
yield sensor.register_sensor(var, config)
|
||||
|
||||
update_period = config[CONF_UPDATE_INTERVAL].total_seconds
|
||||
cg.add(var.set_config(determine_config_register(update_period)))
|
76
esphome/components/tmp117/tmp117.cpp
Normal file
76
esphome/components/tmp117/tmp117.cpp
Normal file
|
@ -0,0 +1,76 @@
|
|||
// Implementation based on:
|
||||
// - DHT 12 Component
|
||||
|
||||
#include "tmp117.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace tmp117 {
|
||||
|
||||
static const char *TAG = "tmp117";
|
||||
|
||||
void TMP117Component::update() {
|
||||
int16_t data;
|
||||
if (!this->read_data_(&data)) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
if ((uint16_t) data != 0x8000) {
|
||||
float temperature = data * 0.0078125f;
|
||||
|
||||
ESP_LOGD(TAG, "Got temperature=%.2f°C", temperature);
|
||||
this->publish_state(temperature);
|
||||
this->status_clear_warning();
|
||||
} else {
|
||||
ESP_LOGD(TAG, "TMP117 not ready");
|
||||
}
|
||||
}
|
||||
void TMP117Component::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up TMP117...");
|
||||
|
||||
if (!this->write_config_(this->config_)) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
int16_t data;
|
||||
if (!this->read_data_(&data)) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
}
|
||||
void TMP117Component::dump_config() {
|
||||
ESP_LOGD(TAG, "TMP117:");
|
||||
LOG_I2C_DEVICE(this);
|
||||
if (this->is_failed()) {
|
||||
ESP_LOGE(TAG, "Communication with TMP117 failed!");
|
||||
}
|
||||
LOG_SENSOR(" ", "Temperature", this);
|
||||
}
|
||||
float TMP117Component::get_setup_priority() const { return setup_priority::DATA; }
|
||||
bool TMP117Component::read_data_(int16_t *data) {
|
||||
if (!this->read_byte_16(0, (uint16_t *) data)) {
|
||||
ESP_LOGW(TAG, "Updating TMP117 failed!");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TMP117Component::read_config_(uint16_t *config) {
|
||||
if (!this->read_byte_16(1, (uint16_t *) config)) {
|
||||
ESP_LOGW(TAG, "Reading TMP117 config failed!");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TMP117Component::write_config_(uint16_t config) {
|
||||
if (!this->write_byte_16(1, config)) {
|
||||
ESP_LOGE(TAG, "Writing TMP117 config failed!");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace tmp117
|
||||
} // namespace esphome
|
27
esphome/components/tmp117/tmp117.h
Normal file
27
esphome/components/tmp117/tmp117.h
Normal file
|
@ -0,0 +1,27 @@
|
|||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace tmp117 {
|
||||
|
||||
class TMP117Component : public PollingComponent, public i2c::I2CDevice, public sensor::Sensor {
|
||||
public:
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override;
|
||||
void update() override;
|
||||
void set_config(uint16_t config) { config_ = config; };
|
||||
|
||||
protected:
|
||||
bool read_data_(int16_t *data);
|
||||
bool read_config_(uint16_t *config);
|
||||
bool write_config_(uint16_t config);
|
||||
|
||||
uint16_t config_;
|
||||
};
|
||||
|
||||
} // namespace tmp117
|
||||
} // namespace esphome
|
|
@ -716,6 +716,9 @@ sensor:
|
|||
name: "Lightning Energy"
|
||||
distance:
|
||||
name: "Distance Storm"
|
||||
- platform: tmp117
|
||||
name: "TMP117 Temperature"
|
||||
update_interval: 5s
|
||||
|
||||
esp32_touch:
|
||||
setup_mode: False
|
||||
|
|
Loading…
Reference in a new issue