From 714d28a61a2b9d4cec82f1f2091c59b70b1e7c60 Mon Sep 17 00:00:00 2001 From: Nicholas Peters Date: Thu, 12 Mar 2020 17:25:00 -0400 Subject: [PATCH] Add TMP117 component (#992) * Create TMP117 sensor component --- esphome/components/tmp117/__init__.py | 0 esphome/components/tmp117/sensor.py | 67 +++++++++++++++++++++++ esphome/components/tmp117/tmp117.cpp | 76 +++++++++++++++++++++++++++ esphome/components/tmp117/tmp117.h | 27 ++++++++++ tests/test1.yaml | 3 ++ 5 files changed, 173 insertions(+) create mode 100644 esphome/components/tmp117/__init__.py create mode 100644 esphome/components/tmp117/sensor.py create mode 100644 esphome/components/tmp117/tmp117.cpp create mode 100644 esphome/components/tmp117/tmp117.h diff --git a/esphome/components/tmp117/__init__.py b/esphome/components/tmp117/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/esphome/components/tmp117/sensor.py b/esphome/components/tmp117/sensor.py new file mode 100644 index 0000000000..ddca3eeb64 --- /dev/null +++ b/esphome/components/tmp117/sensor.py @@ -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))) diff --git a/esphome/components/tmp117/tmp117.cpp b/esphome/components/tmp117/tmp117.cpp new file mode 100644 index 0000000000..9040d9bfed --- /dev/null +++ b/esphome/components/tmp117/tmp117.cpp @@ -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 diff --git a/esphome/components/tmp117/tmp117.h b/esphome/components/tmp117/tmp117.h new file mode 100644 index 0000000000..162dbb64db --- /dev/null +++ b/esphome/components/tmp117/tmp117.h @@ -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 diff --git a/tests/test1.yaml b/tests/test1.yaml index 93e6638330..b3b397ddb1 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -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