1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-29 22:24:26 +00:00
* Add black

Update pre commit

Update pre commit

add empty line

* Format with black
This commit is contained in:
Guillermo Ruffino
2021-03-07 16:03:16 -03:00
committed by GitHub
parent 2b60b0f1fa
commit 69879920eb
398 changed files with 21624 additions and 12644 deletions

View File

@@ -4,58 +4,67 @@ from esphome import automation
from esphome.components import time
from esphome.const import CONF_TIME_ID, CONF_ID, CONF_TRIGGER_ID
CODEOWNERS = ['@OttoWinter']
sun_ns = cg.esphome_ns.namespace('sun')
CODEOWNERS = ["@OttoWinter"]
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)
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'
CONF_SUN_ID = "sun_id"
CONF_LATITUDE = "latitude"
CONF_LONGITUDE = "longitude"
CONF_ELEVATION = "elevation"
CONF_ON_SUNRISE = "on_sunrise"
CONF_ON_SUNSET = "on_sunset"
# Default sun elevation is a bit below horizon because sunset
# means time when the entire sun disk is below the horizon
DEFAULT_ELEVATION = -0.883
ELEVATION_MAP = {
'sunrise': 0.0,
'sunset': 0.0,
'civil': -6.0,
'nautical': -12.0,
'astronomical': -18.0,
"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='_')(value)]
value = ELEVATION_MAP[
cv.one_of(*ELEVATION_MAP, lower=True, space="_")(value)
]
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=DEFAULT_ELEVATION): elevation,
}),
cv.Optional(CONF_ON_SUNSET): automation.validate_automation({
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(SunTrigger),
cv.Optional(CONF_ELEVATION, default=DEFAULT_ELEVATION): elevation,
}),
})
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=DEFAULT_ELEVATION): elevation,
}
),
cv.Optional(CONF_ON_SUNSET): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(SunTrigger),
cv.Optional(CONF_ELEVATION, default=DEFAULT_ELEVATION): elevation,
}
),
}
)
def to_code(config):
@@ -82,10 +91,18 @@ def to_code(config):
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=DEFAULT_ELEVATION): cv.templatable(elevation),
}))
@automation.register_condition(
"sun.is_above_horizon",
SunCondition,
cv.Schema(
{
cv.GenerateID(): cv.use_id(Sun),
cv.Optional(CONF_ELEVATION, default=DEFAULT_ELEVATION): 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])
@@ -95,10 +112,18 @@ def sun_above_horizon_to_code(config, condition_id, template_arg, args):
yield var
@automation.register_condition('sun.is_below_horizon', SunCondition, cv.Schema({
cv.GenerateID(): cv.use_id(Sun),
cv.Optional(CONF_ELEVATION, default=DEFAULT_ELEVATION): cv.templatable(elevation),
}))
@automation.register_condition(
"sun.is_below_horizon",
SunCondition,
cv.Schema(
{
cv.GenerateID(): cv.use_id(Sun),
cv.Optional(CONF_ELEVATION, default=DEFAULT_ELEVATION): 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])

View File

@@ -1,25 +1,35 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor
from esphome.const import DEVICE_CLASS_EMPTY, UNIT_DEGREES, ICON_WEATHER_SUNSET, CONF_ID, CONF_TYPE
from esphome.const import (
DEVICE_CLASS_EMPTY,
UNIT_DEGREES,
ICON_WEATHER_SUNSET,
CONF_ID,
CONF_TYPE,
)
from .. import sun_ns, CONF_SUN_ID, Sun
DEPENDENCIES = ['sun']
DEPENDENCIES = ["sun"]
SunSensor = sun_ns.class_('SunSensor', sensor.Sensor, cg.PollingComponent)
SensorType = sun_ns.enum('SensorType')
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,
"elevation": SensorType.SUN_SENSOR_ELEVATION,
"azimuth": SensorType.SUN_SENSOR_AZIMUTH,
}
CONFIG_SCHEMA = sensor.sensor_schema(
UNIT_DEGREES, ICON_WEATHER_SUNSET, 1, DEVICE_CLASS_EMPTY
).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'))
CONFIG_SCHEMA = (
sensor.sensor_schema(UNIT_DEGREES, ICON_WEATHER_SUNSET, 1, DEVICE_CLASS_EMPTY)
.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):

View File

@@ -1,16 +1,24 @@
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 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, DEFAULT_ELEVATION
DEPENDENCIES = ['sun']
DEPENDENCIES = ["sun"]
SunTextSensor = sun_ns.class_('SunTextSensor', text_sensor.TextSensor, cg.PollingComponent)
SunTextSensor = sun_ns.class_(
"SunTextSensor", text_sensor.TextSensor, cg.PollingComponent
)
SUN_TYPES = {
'sunset': False,
'sunrise': True,
"sunset": False,
"sunrise": True,
}
@@ -18,19 +26,24 @@ 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,
"sunset": ICON_WEATHER_SUNSET_DOWN,
"sunrise": ICON_WEATHER_SUNSET_UP,
}[config[CONF_TYPE]]
return config
CONFIG_SCHEMA = cv.All(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=DEFAULT_ELEVATION): elevation,
cv.Optional(CONF_FORMAT, default='%X'): cv.string_strict,
}).extend(cv.polling_component_schema('60s')), validate_optional_icon)
CONFIG_SCHEMA = cv.All(
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=DEFAULT_ELEVATION): elevation,
cv.Optional(CONF_FORMAT, default="%X"): cv.string_strict,
}
).extend(cv.polling_component_schema("60s")),
validate_optional_icon,
)
def to_code(config):