mirror of
https://github.com/esphome/esphome.git
synced 2025-03-14 06:38:17 +00:00
Code cleanup
This commit is contained in:
parent
837fec7f72
commit
51b7777e91
@ -301,6 +301,7 @@ esphome/components/nfc/* @jesserockz @kbx81
|
||||
esphome/components/noblex/* @AGalfra
|
||||
esphome/components/npi19/* @bakerkj
|
||||
esphome/components/number/* @esphome/core
|
||||
esphome/components/obd/* @abstractionnl
|
||||
esphome/components/one_wire/* @ssieb
|
||||
esphome/components/online_image/* @guillempages
|
||||
esphome/components/opentherm/* @olegtarasov
|
||||
|
@ -1,13 +1,12 @@
|
||||
import esphome.config_validation as cv
|
||||
import esphome.codegen as cg
|
||||
import esphome.components.sensor as s
|
||||
import esphome.components.binary_sensor as bs
|
||||
from esphome import automation
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
CONF_INTERVAL,
|
||||
CONF_TRIGGER_ID
|
||||
)
|
||||
import esphome.codegen as cg
|
||||
import esphome.components.binary_sensor as bs
|
||||
import esphome.components.sensor as s
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ID, CONF_INTERVAL, CONF_TIMEOUT, CONF_TRIGGER_ID
|
||||
|
||||
CODEOWNERS = ["@abstractionnl"]
|
||||
DEPENDENCIES = ["canbus"]
|
||||
|
||||
obd_ns = cg.esphome_ns.namespace("obd")
|
||||
OBDComponent = obd_ns.class_("OBDComponent", cg.PollingComponent)
|
||||
@ -17,25 +16,25 @@ PIDRequest = obd_ns.class_("PIDRequest", cg.Component)
|
||||
OBDPidTrigger = obd_ns.class_(
|
||||
"OBDPidTrigger",
|
||||
automation.Trigger.template(cg.std_vector.template(cg.uint8)),
|
||||
cg.Component
|
||||
cg.Component,
|
||||
)
|
||||
|
||||
CONF_CANBUS_ID = 'canbus_id'
|
||||
CONF_ENABLED_BY_DEFAULT = 'enabled_by_default'
|
||||
CONF_OBD_ID = 'obd_id'
|
||||
CONF_CANBUS_ID = "canbus_id"
|
||||
CONF_ENABLED_BY_DEFAULT = "enabled_by_default"
|
||||
CONF_OBD_ID = "obd_id"
|
||||
CONF_PID_ID = "pid_id"
|
||||
CONF_CAN_ID = "can_id"
|
||||
CONF_RESPONSE_CAN_ID = "response_can_id"
|
||||
CONF_USE_EXTENDED_ID = "use_extended_id"
|
||||
CONF_PIDS = "pids"
|
||||
CONF_PID = "pid"
|
||||
CONF_TIMEOUT = "timeout"
|
||||
CONF_REPLY_LENGTH = "reply_length"
|
||||
CONF_ON_FRAME = "on_frame"
|
||||
CONF_MASK = "mask"
|
||||
CONF_SIGNED = "signed"
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema({
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(OBDComponent),
|
||||
cv.Required(CONF_CANBUS_ID): cv.use_id("CanbusComponent"),
|
||||
cv.Optional(CONF_ENABLED_BY_DEFAULT, default=False): cv.boolean,
|
||||
@ -46,8 +45,12 @@ CONFIG_SCHEMA = cv.Schema({
|
||||
cv.Required(CONF_PID): cv.int_range(min=0, max=0x1FFFFFFF),
|
||||
cv.Optional(CONF_RESPONSE_CAN_ID): cv.int_range(min=0, max=0x1FFFFFFF),
|
||||
cv.Optional(CONF_USE_EXTENDED_ID, default=False): cv.boolean,
|
||||
cv.Optional(CONF_INTERVAL, default="5s"): cv.positive_time_period_milliseconds,
|
||||
cv.Optional(CONF_TIMEOUT, default="500ms"): cv.positive_time_period_milliseconds,
|
||||
cv.Optional(
|
||||
CONF_INTERVAL, default="5s"
|
||||
): cv.positive_time_period_milliseconds,
|
||||
cv.Optional(
|
||||
CONF_TIMEOUT, default="500ms"
|
||||
): cv.positive_time_period_milliseconds,
|
||||
cv.Optional(CONF_REPLY_LENGTH, default=8): cv.positive_int,
|
||||
cv.Optional(CONF_ON_FRAME): automation.validate_automation(
|
||||
{
|
||||
@ -55,8 +58,10 @@ CONFIG_SCHEMA = cv.Schema({
|
||||
}
|
||||
),
|
||||
}
|
||||
)
|
||||
}).extend(cv.COMPONENT_SCHEMA)
|
||||
),
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
@ -73,7 +78,7 @@ async def to_code(config):
|
||||
use_extended_id = pid_conf[CONF_USE_EXTENDED_ID]
|
||||
response_can_id = pid_conf.get(CONF_RESPONSE_CAN_ID)
|
||||
|
||||
if (response_can_id is None):
|
||||
if response_can_id is None:
|
||||
response_can_id = can_id | 8
|
||||
|
||||
pid_request = cg.new_Pvariable(
|
||||
@ -88,6 +93,8 @@ async def to_code(config):
|
||||
for trigger_conf in pid_conf.get(CONF_ON_FRAME, []):
|
||||
trigger = cg.new_Pvariable(trigger_conf[CONF_TRIGGER_ID], pid_request)
|
||||
await cg.register_component(trigger, trigger_conf)
|
||||
await automation.build_automation(trigger, [(cg.std_vector.template(cg.uint8), "data")], trigger_conf)
|
||||
await automation.build_automation(
|
||||
trigger, [(cg.std_vector.template(cg.uint8), "data")], trigger_conf
|
||||
)
|
||||
|
||||
return var
|
||||
|
@ -1,23 +1,15 @@
|
||||
import esphome.config_validation as cv
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import binary_sensor
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_INDEX, CONF_LAMBDA
|
||||
from esphome.cpp_generator import LambdaExpression
|
||||
from esphome.const import (
|
||||
CONF_LAMBDA,
|
||||
CONF_INDEX,
|
||||
)
|
||||
from . import (
|
||||
CONF_PID_ID,
|
||||
CONF_MASK,
|
||||
OBDBinarySensor,
|
||||
PIDRequest
|
||||
)
|
||||
|
||||
from . import CONF_MASK, CONF_PID_ID, OBDBinarySensor, PIDRequest
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
binary_sensor.binary_sensor_schema(
|
||||
OBDBinarySensor,
|
||||
)
|
||||
.extend(
|
||||
).extend(
|
||||
{
|
||||
cv.Required(CONF_PID_ID): cv.use_id(PIDRequest),
|
||||
cv.Optional(CONF_LAMBDA): cv.returning_lambda,
|
||||
@ -26,16 +18,19 @@ CONFIG_SCHEMA = cv.All(
|
||||
}
|
||||
),
|
||||
cv.has_exactly_one_key(CONF_LAMBDA, CONF_INDEX),
|
||||
cv.has_none_or_all_keys(CONF_INDEX, CONF_MASK)
|
||||
cv.has_none_or_all_keys(CONF_INDEX, CONF_MASK),
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
pid_request = await cg.get_variable(config[CONF_PID_ID])
|
||||
var = await binary_sensor.new_binary_sensor(config, pid_request)
|
||||
await cg.register_component(var, config)
|
||||
|
||||
if lambda_ := config.get(CONF_LAMBDA):
|
||||
template = await cg.process_lambda(lambda_, [(cg.std_vector.template(cg.uint8), "data")], return_type=cg.bool_)
|
||||
template = await cg.process_lambda(
|
||||
lambda_, [(cg.std_vector.template(cg.uint8), "data")], return_type=cg.bool_
|
||||
)
|
||||
cg.add(var.set_template(template))
|
||||
|
||||
else:
|
||||
@ -44,6 +39,6 @@ async def to_code(config):
|
||||
template = LambdaExpression(
|
||||
f"return (data[{index}] & {mask}) == {mask};",
|
||||
[(cg.std_vector.template(cg.uint8), "data")],
|
||||
return_type=cg.bool_
|
||||
return_type=cg.bool_,
|
||||
)
|
||||
cg.add(var.set_template(template))
|
@ -15,13 +15,10 @@ void OBDComponent::call_setup() {
|
||||
}
|
||||
}
|
||||
|
||||
void OBDComponent::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "OBD Component");
|
||||
}
|
||||
void OBDComponent::dump_config() { ESP_LOGCONFIG(TAG, "OBD Component"); }
|
||||
|
||||
void OBDComponent::update() {
|
||||
if (this->current_request_ == nullptr) {
|
||||
|
||||
// No current request, find the next request to do
|
||||
for (auto *request : this->pidrequests_) {
|
||||
if (request->start()) {
|
||||
@ -162,18 +159,14 @@ void OBDSensor::update(const std::vector<uint8_t> &data) {
|
||||
this->publish_state(value);
|
||||
}
|
||||
|
||||
void OBDSensor::dump_config() {
|
||||
LOG_SENSOR("", "OBD Sensor", this);
|
||||
}
|
||||
void OBDSensor::dump_config() { LOG_SENSOR("", "OBD Sensor", this); }
|
||||
|
||||
void OBDBinarySensor::update(const std::vector<uint8_t> &data) {
|
||||
auto value = this->data_to_value_func_(data);
|
||||
this->publish_state(value);
|
||||
}
|
||||
|
||||
void OBDBinarySensor::dump_config() {
|
||||
LOG_BINARY_SENSOR("", "OBD Binary Sensor", this);
|
||||
}
|
||||
void OBDBinarySensor::dump_config() { LOG_BINARY_SENSOR("", "OBD Binary Sensor", this); }
|
||||
|
||||
}
|
||||
}
|
||||
} // namespace obd
|
||||
} // namespace esphome
|
||||
|
@ -1,34 +1,26 @@
|
||||
import esphome.config_validation as cv
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import sensor
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_INDEX, CONF_LAMBDA
|
||||
from esphome.cpp_generator import LambdaExpression
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
CONF_LAMBDA,
|
||||
CONF_INDEX,
|
||||
)
|
||||
from . import (
|
||||
CONF_PID_ID,
|
||||
CONF_SIGNED,
|
||||
OBDSensor,
|
||||
PIDRequest
|
||||
)
|
||||
|
||||
from . import CONF_PID_ID, CONF_SIGNED, OBDSensor, PIDRequest
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
sensor.sensor_schema(
|
||||
OBDSensor,
|
||||
)
|
||||
.extend(
|
||||
).extend(
|
||||
{
|
||||
cv.Required(CONF_PID_ID): cv.use_id(PIDRequest),
|
||||
cv.Optional(CONF_SIGNED, default=False): cv.boolean,
|
||||
cv.Exclusive(CONF_LAMBDA, CONF_LAMBDA): cv.returning_lambda,
|
||||
cv.Exclusive(CONF_INDEX, CONF_LAMBDA): cv.ensure_list(cv.positive_int)
|
||||
cv.Exclusive(CONF_INDEX, CONF_LAMBDA): cv.ensure_list(cv.positive_int),
|
||||
}
|
||||
),
|
||||
cv.has_exactly_one_key(CONF_LAMBDA, CONF_INDEX)
|
||||
cv.has_exactly_one_key(CONF_LAMBDA, CONF_INDEX),
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
pid_request = await cg.get_variable(config[CONF_PID_ID])
|
||||
var = await sensor.new_sensor(config, pid_request)
|
||||
@ -37,7 +29,9 @@ async def to_code(config):
|
||||
template = False
|
||||
|
||||
if lambda_ := config.get(CONF_LAMBDA):
|
||||
template = await cg.process_lambda(lambda_, [(cg.std_vector.template(cg.uint8), "data")], return_type=cg.float_)
|
||||
template = await cg.process_lambda(
|
||||
lambda_, [(cg.std_vector.template(cg.uint8), "data")], return_type=cg.float_
|
||||
)
|
||||
cg.add(var.set_template(template))
|
||||
else:
|
||||
indexes = config[CONF_INDEX]
|
||||
@ -47,29 +41,28 @@ async def to_code(config):
|
||||
template = LambdaExpression(
|
||||
f"return ({signed}data[{indexes[0]}] << 24) | (data[{indexes[1]}] << 16) | (data[{indexes[2]}] << 8) | data[{indexes[3]}];",
|
||||
[(cg.std_vector.template(cg.uint8), "data")],
|
||||
return_type=cg.float_
|
||||
return_type=cg.float_,
|
||||
)
|
||||
|
||||
if len(indexes) == 3:
|
||||
template = LambdaExpression(
|
||||
f"return ({signed}data[{indexes[0]}] << 16) | (data[{indexes[1]}] << 8) | data[{indexes[2]}];",
|
||||
[(cg.std_vector.template(cg.uint8), "data")],
|
||||
return_type=cg.float_
|
||||
return_type=cg.float_,
|
||||
)
|
||||
|
||||
if len(indexes) == 2:
|
||||
template = LambdaExpression(
|
||||
f"return ({signed}data[{indexes[0]}] << 8) | data[{indexes[1]}];",
|
||||
[(cg.std_vector.template(cg.uint8), "data")],
|
||||
return_type=cg.float_
|
||||
return_type=cg.float_,
|
||||
)
|
||||
|
||||
if len(indexes) == 1:
|
||||
template = LambdaExpression(
|
||||
f"return {signed}data[{indexes[0]}];",
|
||||
[(cg.std_vector.template(cg.uint8), "data")],
|
||||
return_type=cg.float_
|
||||
return_type=cg.float_,
|
||||
)
|
||||
|
||||
cg.add(var.set_template(template))
|
||||
|
Loading…
x
Reference in New Issue
Block a user