mirror of
https://github.com/esphome/esphome.git
synced 2025-09-14 01:02:19 +01:00
Sun support (#531)
* Sun * Add sun support * Lint * Updates * Fix elevation * Lint * Update mqtt_climate.cpp
This commit is contained in:
45
esphome/components/sun/text_sensor/__init__.py
Normal file
45
esphome/components/sun/text_sensor/__init__.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from esphome.components import text_sensor
|
||||
import esphome.config_validation as cv
|
||||
import esphome.codegen as cg
|
||||
from esphome.const import CONF_ICON, ICON_WEATHER_SUNSET_DOWN, ICON_WEATHER_SUNSET_UP, CONF_TYPE, \
|
||||
CONF_ID, CONF_FORMAT
|
||||
from .. import sun_ns, CONF_SUN_ID, Sun, CONF_ELEVATION, elevation
|
||||
|
||||
DEPENDENCIES = ['sun']
|
||||
|
||||
SunTextSensor = sun_ns.class_('SunTextSensor', text_sensor.TextSensor, cg.PollingComponent)
|
||||
SUN_TYPES = {
|
||||
'sunset': False,
|
||||
'sunrise': True,
|
||||
}
|
||||
|
||||
|
||||
def validate_optional_icon(config):
|
||||
if CONF_ICON not in config:
|
||||
config = config.copy()
|
||||
config[CONF_ICON] = {
|
||||
'sunset': ICON_WEATHER_SUNSET_DOWN,
|
||||
'sunrise': ICON_WEATHER_SUNSET_UP,
|
||||
}[config[CONF_TYPE]]
|
||||
return config
|
||||
|
||||
|
||||
CONFIG_SCHEMA = text_sensor.TEXT_SENSOR_SCHEMA.extend({
|
||||
cv.GenerateID(): cv.declare_id(SunTextSensor),
|
||||
cv.GenerateID(CONF_SUN_ID): cv.use_id(Sun),
|
||||
cv.Required(CONF_TYPE): cv.one_of(*SUN_TYPES, lower=True),
|
||||
cv.Optional(CONF_ELEVATION, default=0): elevation,
|
||||
cv.Optional(CONF_FORMAT, default='%X'): cv.string_strict,
|
||||
}).extend(cv.polling_component_schema('60s'))
|
||||
|
||||
|
||||
def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
yield cg.register_component(var, config)
|
||||
yield text_sensor.register_text_sensor(var, config)
|
||||
|
||||
paren = yield cg.get_variable(config[CONF_SUN_ID])
|
||||
cg.add(var.set_parent(paren))
|
||||
cg.add(var.set_sunrise(SUN_TYPES[config[CONF_TYPE]]))
|
||||
cg.add(var.set_elevation(config[CONF_ELEVATION]))
|
||||
cg.add(var.set_format(config[CONF_FORMAT]))
|
12
esphome/components/sun/text_sensor/sun_text_sensor.cpp
Normal file
12
esphome/components/sun/text_sensor/sun_text_sensor.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#include "sun_text_sensor.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace sun {
|
||||
|
||||
static const char *TAG = "sun.text_sensor";
|
||||
|
||||
void SunTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Sun Text Sensor", this); }
|
||||
|
||||
} // namespace sun
|
||||
} // namespace esphome
|
41
esphome/components/sun/text_sensor/sun_text_sensor.h
Normal file
41
esphome/components/sun/text_sensor/sun_text_sensor.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sun/sun.h"
|
||||
#include "esphome/components/text_sensor/text_sensor.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace sun {
|
||||
|
||||
class SunTextSensor : public text_sensor::TextSensor, public PollingComponent {
|
||||
public:
|
||||
void set_parent(Sun *parent) { parent_ = parent; }
|
||||
void set_elevation(double elevation) { elevation_ = elevation; }
|
||||
void set_sunrise(bool sunrise) { sunrise_ = sunrise; }
|
||||
void set_format(const std::string &format) { format_ = format; }
|
||||
|
||||
void update() override {
|
||||
optional<time::ESPTime> res;
|
||||
if (this->sunrise_)
|
||||
res = this->parent_->sunrise(this->elevation_);
|
||||
else
|
||||
res = this->parent_->sunset(this->elevation_);
|
||||
if (!res) {
|
||||
this->publish_state("");
|
||||
return;
|
||||
}
|
||||
|
||||
this->publish_state(res->strftime(this->format_));
|
||||
}
|
||||
|
||||
void dump_config() override;
|
||||
|
||||
protected:
|
||||
std::string format_{};
|
||||
Sun *parent_;
|
||||
double elevation_;
|
||||
bool sunrise_;
|
||||
};
|
||||
|
||||
} // namespace sun
|
||||
} // namespace esphome
|
Reference in New Issue
Block a user