mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-31 18:18:27 +02:00
Sun support (#531)
* Sun * Add sun support * Lint * Updates * Fix elevation * Lint * Update mqtt_climate.cpp
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome import automation
|
||||
from esphome.components import time
|
||||
from esphome.const import CONF_TIME_ID, CONF_ID, CONF_TRIGGER_ID
|
||||
|
||||
sun_ns = cg.esphome_ns.namespace('sun')
|
||||
|
||||
Sun = sun_ns.class_('Sun')
|
||||
SunTrigger = sun_ns.class_('SunTrigger', cg.PollingComponent, automation.Trigger.template())
|
||||
SunCondition = sun_ns.class_('SunCondition', automation.Condition)
|
||||
|
||||
CONF_SUN_ID = 'sun_id'
|
||||
CONF_LATITUDE = 'latitude'
|
||||
CONF_LONGITUDE = 'longitude'
|
||||
CONF_ELEVATION = 'elevation'
|
||||
CONF_ON_SUNRISE = 'on_sunrise'
|
||||
CONF_ON_SUNSET = 'on_sunset'
|
||||
|
||||
ELEVATION_MAP = {
|
||||
'sunrise': 0.0,
|
||||
'sunset': 0.0,
|
||||
'civil': -6.0,
|
||||
'nautical': -12.0,
|
||||
'astronomical': -18.0,
|
||||
}
|
||||
|
||||
|
||||
def elevation(value):
|
||||
if isinstance(value, str):
|
||||
try:
|
||||
value = ELEVATION_MAP[cv.one_of(*ELEVATION_MAP, lower=True, space='_')]
|
||||
except cv.Invalid:
|
||||
pass
|
||||
value = cv.angle(value)
|
||||
return cv.float_range(min=-180, max=180)(value)
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema({
|
||||
cv.GenerateID(): cv.declare_id(Sun),
|
||||
cv.GenerateID(CONF_TIME_ID): cv.use_id(time.RealTimeClock),
|
||||
cv.Required(CONF_LATITUDE): cv.float_range(min=-90, max=90),
|
||||
cv.Required(CONF_LONGITUDE): cv.float_range(min=-180, max=180),
|
||||
|
||||
cv.Optional(CONF_ON_SUNRISE): automation.validate_automation({
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(SunTrigger),
|
||||
cv.Optional(CONF_ELEVATION, default=0.0): elevation,
|
||||
}),
|
||||
cv.Optional(CONF_ON_SUNSET): automation.validate_automation({
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(SunTrigger),
|
||||
cv.Optional(CONF_ELEVATION, default=0.0): elevation,
|
||||
}),
|
||||
})
|
||||
|
||||
|
||||
def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
time_ = yield cg.get_variable(config[CONF_TIME_ID])
|
||||
cg.add(var.set_time(time_))
|
||||
cg.add(var.set_latitude(config[CONF_LATITUDE]))
|
||||
cg.add(var.set_longitude(config[CONF_LONGITUDE]))
|
||||
|
||||
for conf in config.get(CONF_ON_SUNRISE, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID])
|
||||
yield cg.register_component(trigger, conf)
|
||||
yield cg.register_parented(trigger, var)
|
||||
cg.add(trigger.set_sunrise(True))
|
||||
cg.add(trigger.set_elevation(conf[CONF_ELEVATION]))
|
||||
yield automation.build_automation(trigger, [], conf)
|
||||
|
||||
for conf in config.get(CONF_ON_SUNSET, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID])
|
||||
yield cg.register_component(trigger, conf)
|
||||
yield cg.register_parented(trigger, var)
|
||||
cg.add(trigger.set_sunrise(False))
|
||||
cg.add(trigger.set_elevation(conf[CONF_ELEVATION]))
|
||||
yield automation.build_automation(trigger, [], conf)
|
||||
|
||||
|
||||
@automation.register_condition('sun.is_above_horizon', SunCondition, cv.Schema({
|
||||
cv.GenerateID(): cv.use_id(Sun),
|
||||
cv.Optional(CONF_ELEVATION, default=0): cv.templatable(elevation),
|
||||
}))
|
||||
def sun_above_horizon_to_code(config, condition_id, template_arg, args):
|
||||
var = cg.new_Pvariable(condition_id, template_arg)
|
||||
yield cg.register_parented(var, config[CONF_ID])
|
||||
templ = yield cg.templatable(config[CONF_ELEVATION], args, cg.double)
|
||||
cg.add(var.set_elevation(templ))
|
||||
cg.add(var.set_above(True))
|
||||
yield var
|
||||
|
||||
|
||||
@automation.register_condition('sun.is_below_horizon', SunCondition, cv.Schema({
|
||||
cv.GenerateID(): cv.use_id(Sun),
|
||||
cv.Optional(CONF_ELEVATION, default=0): cv.templatable(elevation),
|
||||
}))
|
||||
def sun_below_horizon_to_code(config, condition_id, template_arg, args):
|
||||
var = cg.new_Pvariable(condition_id, template_arg)
|
||||
yield cg.register_parented(var, config[CONF_ID])
|
||||
templ = yield cg.templatable(config[CONF_ELEVATION], args, cg.double)
|
||||
cg.add(var.set_elevation(templ))
|
||||
cg.add(var.set_above(False))
|
||||
yield var
|
||||
@@ -0,0 +1,30 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import sensor
|
||||
from esphome.const import UNIT_DEGREES, ICON_WEATHER_SUNSET, CONF_ID, CONF_TYPE
|
||||
from .. import sun_ns, CONF_SUN_ID, Sun
|
||||
|
||||
DEPENDENCIES = ['sun']
|
||||
|
||||
SunSensor = sun_ns.class_('SunSensor', sensor.Sensor, cg.PollingComponent)
|
||||
SensorType = sun_ns.enum('SensorType')
|
||||
TYPES = {
|
||||
'elevation': SensorType.SUN_SENSOR_ELEVATION,
|
||||
'azimuth': SensorType.SUN_SENSOR_AZIMUTH,
|
||||
}
|
||||
|
||||
CONFIG_SCHEMA = sensor.sensor_schema(UNIT_DEGREES, ICON_WEATHER_SUNSET, 1).extend({
|
||||
cv.GenerateID(): cv.declare_id(SunSensor),
|
||||
cv.GenerateID(CONF_SUN_ID): cv.use_id(Sun),
|
||||
cv.Required(CONF_TYPE): cv.enum(TYPES, lower=True),
|
||||
}).extend(cv.polling_component_schema('60s'))
|
||||
|
||||
|
||||
def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
yield cg.register_component(var, config)
|
||||
yield sensor.register_sensor(var, config)
|
||||
|
||||
cg.add(var.set_type(config[CONF_TYPE]))
|
||||
paren = yield cg.get_variable(config[CONF_SUN_ID])
|
||||
cg.add(var.set_parent(paren))
|
||||
@@ -0,0 +1,12 @@
|
||||
#include "sun_sensor.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace sun {
|
||||
|
||||
static const char *TAG = "sun.sensor";
|
||||
|
||||
void SunSensor::dump_config() { LOG_SENSOR("", "Sun Sensor", this); }
|
||||
|
||||
} // namespace sun
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sun/sun.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace sun {
|
||||
|
||||
enum SensorType {
|
||||
SUN_SENSOR_ELEVATION,
|
||||
SUN_SENSOR_AZIMUTH,
|
||||
};
|
||||
|
||||
class SunSensor : public sensor::Sensor, public PollingComponent {
|
||||
public:
|
||||
void set_parent(Sun *parent) { parent_ = parent; }
|
||||
void set_type(SensorType type) { type_ = type; }
|
||||
void dump_config() override;
|
||||
void update() override {
|
||||
double val;
|
||||
switch (this->type_) {
|
||||
case SUN_SENSOR_ELEVATION:
|
||||
val = this->parent_->elevation();
|
||||
break;
|
||||
case SUN_SENSOR_AZIMUTH:
|
||||
val = this->parent_->azimuth();
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
this->publish_state(val);
|
||||
}
|
||||
|
||||
protected:
|
||||
sun::Sun *parent_;
|
||||
SensorType type_;
|
||||
};
|
||||
|
||||
} // namespace sun
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,168 @@
|
||||
#include "sun.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace sun {
|
||||
|
||||
static const char *TAG = "sun";
|
||||
|
||||
#undef PI
|
||||
|
||||
/* Usually, ESPHome uses single-precision floating point values
|
||||
* because those tend to be accurate enough and are more efficient.
|
||||
*
|
||||
* However, some of the data in this class has to be quite accurate, so double is
|
||||
* used everywhere.
|
||||
*/
|
||||
static const double PI = 3.141592653589793;
|
||||
static const double TAU = 6.283185307179586;
|
||||
static const double TO_RADIANS = PI / 180.0;
|
||||
static const double TO_DEGREES = 180.0 / PI;
|
||||
static const double EARTH_TILT = 23.44 * TO_RADIANS;
|
||||
|
||||
optional<time::ESPTime> Sun::sunrise(double elevation) {
|
||||
auto time = this->time_->now();
|
||||
if (!time.is_valid())
|
||||
return {};
|
||||
double sun_time = this->sun_time_for_elevation_(time.day_of_year, elevation, true);
|
||||
if (isnan(sun_time))
|
||||
return {};
|
||||
uint32_t epoch = this->calc_epoch_(time, sun_time);
|
||||
return time::ESPTime::from_epoch_local(epoch);
|
||||
}
|
||||
optional<time::ESPTime> Sun::sunset(double elevation) {
|
||||
auto time = this->time_->now();
|
||||
if (!time.is_valid())
|
||||
return {};
|
||||
double sun_time = this->sun_time_for_elevation_(time.day_of_year, elevation, false);
|
||||
if (isnan(sun_time))
|
||||
return {};
|
||||
uint32_t epoch = this->calc_epoch_(time, sun_time);
|
||||
return time::ESPTime::from_epoch_local(epoch);
|
||||
}
|
||||
double Sun::elevation() {
|
||||
auto time = this->current_sun_time_();
|
||||
if (isnan(time))
|
||||
return NAN;
|
||||
return this->elevation_(time);
|
||||
}
|
||||
double Sun::azimuth() {
|
||||
auto time = this->current_sun_time_();
|
||||
if (isnan(time))
|
||||
return NAN;
|
||||
return this->azimuth_(time);
|
||||
}
|
||||
double Sun::sun_declination_(double sun_time) {
|
||||
double n = sun_time - 1.0;
|
||||
// maximum declination
|
||||
const double tot = -sin(EARTH_TILT);
|
||||
|
||||
// eccentricity of the earth's orbit (ellipse)
|
||||
double eccentricity = 0.0167;
|
||||
|
||||
// days since perihelion (January 3rd)
|
||||
double days_since_perihelion = n - 2;
|
||||
// days since december solstice (december 22)
|
||||
double days_since_december_solstice = n + 10;
|
||||
const double c = TAU / 365.24;
|
||||
double v = cos(c * days_since_december_solstice + 2 * eccentricity * sin(c * days_since_perihelion));
|
||||
// Make sure value is in range (double error may lead to results slightly larger than 1)
|
||||
double x = clamp(tot * v, 0, 1);
|
||||
return asin(x);
|
||||
}
|
||||
double Sun::elevation_ratio_(double sun_time) {
|
||||
double decl = this->sun_declination_(sun_time);
|
||||
double hangle = this->hour_angle_(sun_time);
|
||||
double a = sin(this->latitude_rad_()) * sin(decl);
|
||||
double b = cos(this->latitude_rad_()) * cos(decl) * cos(hangle);
|
||||
double val = clamp(a + b, -1.0, 1.0);
|
||||
return val;
|
||||
}
|
||||
double Sun::latitude_rad_() { return this->latitude_ * TO_RADIANS; }
|
||||
double Sun::hour_angle_(double sun_time) {
|
||||
double time_of_day = fmod(sun_time, 1.0) * 24.0;
|
||||
return -PI * (time_of_day - 12) / 12;
|
||||
}
|
||||
double Sun::elevation_(double sun_time) { return this->elevation_rad_(sun_time) * TO_DEGREES; }
|
||||
double Sun::elevation_rad_(double sun_time) { return asin(this->elevation_ratio_(sun_time)); }
|
||||
double Sun::zenith_rad_(double sun_time) { return acos(this->elevation_ratio_(sun_time)); }
|
||||
double Sun::azimuth_rad_(double sun_time) {
|
||||
double hangle = -this->hour_angle_(sun_time);
|
||||
double decl = this->sun_declination_(sun_time);
|
||||
double zen = this->zenith_rad_(sun_time);
|
||||
double nom = cos(zen) * sin(this->latitude_rad_()) - sin(decl);
|
||||
double denom = sin(zen) * cos(this->latitude_rad_());
|
||||
double v = clamp(nom / denom, -1.0, 1.0);
|
||||
double az = PI - acos(v);
|
||||
if (hangle > 0)
|
||||
az = -az;
|
||||
if (az < 0)
|
||||
az += TAU;
|
||||
return az;
|
||||
}
|
||||
double Sun::azimuth_(double sun_time) { return this->azimuth_rad_(sun_time) * TO_DEGREES; }
|
||||
double Sun::calc_sun_time_(const time::ESPTime &time) {
|
||||
// Time as seen at 0° longitude
|
||||
if (!time.is_valid())
|
||||
return NAN;
|
||||
|
||||
double base = (time.day_of_year + time.hour / 24.0 + time.minute / 24.0 / 60.0 + time.second / 24.0 / 60.0 / 60.0);
|
||||
// Add longitude correction
|
||||
double add = this->longitude_ / 360.0;
|
||||
return base + add;
|
||||
}
|
||||
uint32_t Sun::calc_epoch_(time::ESPTime base, double sun_time) {
|
||||
sun_time -= this->longitude_ / 360.0;
|
||||
base.day_of_year = uint32_t(floor(sun_time));
|
||||
|
||||
sun_time = (sun_time - base.day_of_year) * 24.0;
|
||||
base.hour = uint32_t(floor(sun_time));
|
||||
|
||||
sun_time = (sun_time - base.hour) * 60.0;
|
||||
base.minute = uint32_t(floor(sun_time));
|
||||
|
||||
sun_time = (sun_time - base.minute) * 60.0;
|
||||
base.second = uint32_t(floor(sun_time));
|
||||
|
||||
base.recalc_timestamp_utc(true);
|
||||
return base.timestamp;
|
||||
}
|
||||
double Sun::sun_time_for_elevation_(int32_t day_of_year, double elevation, bool rising) {
|
||||
// Use binary search, newton's method would be better but binary search already
|
||||
// converges quite well (19 cycles) and much simpler. Function is guaranteed to be
|
||||
// monotonous.
|
||||
double lo, hi;
|
||||
if (rising) {
|
||||
lo = day_of_year + 0.0;
|
||||
hi = day_of_year + 0.5;
|
||||
} else {
|
||||
lo = day_of_year + 1.0;
|
||||
hi = day_of_year + 0.5;
|
||||
}
|
||||
|
||||
double min_elevation = this->elevation_(lo);
|
||||
double max_elevation = this->elevation_(hi);
|
||||
if (elevation < min_elevation || elevation > max_elevation)
|
||||
return NAN;
|
||||
|
||||
// Accuracy: 0.1s
|
||||
const double accuracy = 1.0 / (24.0 * 60.0 * 60.0 * 10.0);
|
||||
|
||||
while (fabs(hi - lo) > accuracy) {
|
||||
double mid = (lo + hi) / 2.0;
|
||||
double value = this->elevation_(mid) - elevation;
|
||||
if (value < 0) {
|
||||
lo = mid;
|
||||
} else if (value > 0) {
|
||||
hi = mid;
|
||||
} else {
|
||||
lo = hi = mid;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return (lo + hi) / 2.0;
|
||||
}
|
||||
|
||||
} // namespace sun
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,146 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/components/time/real_time_clock.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace sun {
|
||||
|
||||
class Sun {
|
||||
public:
|
||||
void set_time(time::RealTimeClock *time) { time_ = time; }
|
||||
time::RealTimeClock *get_time() const { return time_; }
|
||||
void set_latitude(double latitude) { latitude_ = latitude; }
|
||||
void set_longitude(double longitude) { longitude_ = longitude; }
|
||||
|
||||
optional<time::ESPTime> sunrise(double elevation = 0.0);
|
||||
optional<time::ESPTime> sunset(double elevation = 0.0);
|
||||
|
||||
double elevation();
|
||||
double azimuth();
|
||||
|
||||
protected:
|
||||
double current_sun_time_() { return this->calc_sun_time_(this->time_->utcnow()); }
|
||||
|
||||
/** Calculate the declination of the sun in rad.
|
||||
*
|
||||
* See https://en.wikipedia.org/wiki/Position_of_the_Sun#Declination_of_the_Sun_as_seen_from_Earth
|
||||
*
|
||||
* Accuracy: ±0.2°
|
||||
*
|
||||
* @param sun_time The day of the year, 1 means January 1st. See calc_sun_time_.
|
||||
* @return Sun declination in degrees
|
||||
*/
|
||||
double sun_declination_(double sun_time);
|
||||
|
||||
double elevation_ratio_(double sun_time);
|
||||
|
||||
/** Calculate the hour angle based on the sun time of day in hours.
|
||||
*
|
||||
* Positive in morning, 0 at noon, negative in afternoon.
|
||||
*
|
||||
* @param sun_time Sun time, see calc_sun_time_.
|
||||
* @return Hour angle in rad.
|
||||
*/
|
||||
double hour_angle_(double sun_time);
|
||||
|
||||
double elevation_(double sun_time);
|
||||
|
||||
double elevation_rad_(double sun_time);
|
||||
|
||||
double zenith_rad_(double sun_time);
|
||||
|
||||
double azimuth_rad_(double sun_time);
|
||||
|
||||
double azimuth_(double sun_time);
|
||||
|
||||
/** Return the sun time given by the time_ object.
|
||||
*
|
||||
* Sun time is defined as doubleing point day of year.
|
||||
* Integer part encodes the day of the year (1=January 1st)
|
||||
* Decimal part encodes time of day (1/24 = 1 hour)
|
||||
*/
|
||||
double calc_sun_time_(const time::ESPTime &time);
|
||||
|
||||
uint32_t calc_epoch_(time::ESPTime base, double sun_time);
|
||||
|
||||
/** Calculate the sun time of day
|
||||
*
|
||||
* @param day_of_year
|
||||
* @param elevation
|
||||
* @param rising
|
||||
* @return
|
||||
*/
|
||||
double sun_time_for_elevation_(int32_t day_of_year, double elevation, bool rising);
|
||||
|
||||
double latitude_rad_();
|
||||
|
||||
time::RealTimeClock *time_;
|
||||
/// Latitude in degrees, range: -90 to 90.
|
||||
double latitude_;
|
||||
/// Longitude in degrees, range: -180 to 180.
|
||||
double longitude_;
|
||||
};
|
||||
|
||||
class SunTrigger : public Trigger<>, public PollingComponent, public Parented<Sun> {
|
||||
public:
|
||||
SunTrigger() : PollingComponent(1000) {}
|
||||
|
||||
void set_sunrise(bool sunrise) { sunrise_ = sunrise; }
|
||||
void set_elevation(double elevation) { elevation_ = elevation; }
|
||||
|
||||
void update() override {
|
||||
auto now = this->parent_->get_time()->utcnow();
|
||||
if (!now.is_valid())
|
||||
return;
|
||||
|
||||
if (!this->last_result_.has_value() || this->last_result_->day_of_year != now.day_of_year) {
|
||||
this->recalc_();
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->prev_check_ != -1) {
|
||||
auto res = *this->last_result_;
|
||||
// now >= sunrise > prev_check
|
||||
if (now.timestamp >= res.timestamp && res.timestamp > this->prev_check_) {
|
||||
this->trigger();
|
||||
}
|
||||
}
|
||||
this->prev_check_ = now.timestamp;
|
||||
}
|
||||
|
||||
protected:
|
||||
void recalc_() {
|
||||
if (this->sunrise_)
|
||||
this->last_result_ = this->parent_->sunrise(this->elevation_);
|
||||
else
|
||||
this->last_result_ = this->parent_->sunset(this->elevation_);
|
||||
}
|
||||
bool sunrise_;
|
||||
double elevation_;
|
||||
time_t prev_check_{-1};
|
||||
optional<time::ESPTime> last_result_{};
|
||||
};
|
||||
|
||||
template<typename... Ts> class SunCondition : public Condition<Ts...>, public Parented<Sun> {
|
||||
public:
|
||||
TEMPLATABLE_VALUE(double, elevation);
|
||||
void set_above(bool above) { above_ = above; }
|
||||
|
||||
bool check(Ts... x) override {
|
||||
double elevation = this->elevation_.value(x...);
|
||||
double current = this->parent_->elevation();
|
||||
if (this->above_)
|
||||
return current > elevation;
|
||||
else
|
||||
return current < elevation;
|
||||
}
|
||||
|
||||
protected:
|
||||
bool above_;
|
||||
};
|
||||
|
||||
} // namespace sun
|
||||
} // namespace esphome
|
||||
@@ -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]))
|
||||
@@ -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
|
||||
@@ -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