ESPHomeMarlin2/sensor.py

88 lines
3.1 KiB
Python
Raw Permalink Normal View History

2024-12-27 09:51:18 +00:00
import esphome.codegen as cg
import esphome.config_validation as cv
2024-12-27 09:55:11 +00:00
from esphome import automation
from esphome.components import uart
2024-12-27 09:51:18 +00:00
from esphome.components import sensor
2024-12-27 21:23:54 +00:00
# from esphome.components import text_sensor
2024-12-27 11:02:11 +00:00
from esphome.const import (
CONF_ID,
CONF_INDEX,
CONF_SENSORS,
CONF_HUMIDITY,
CONF_MODEL,
CONF_PIN,
CONF_TEMPERATURE,
UNIT_CELSIUS,
UNIT_PERCENT,
2024-12-27 20:32:37 +00:00
UNIT_SECOND,
STATE_CLASS_MEASUREMENT,
2024-12-27 20:41:13 +00:00
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_DURATION,
2024-12-27 11:02:11 +00:00
)
2024-12-30 09:39:19 +00:00
from . import Marlin2
2024-12-27 09:55:11 +00:00
2024-12-27 10:47:42 +00:00
CONF_BED_TEMPERATURE = "bed_temperature"
2024-12-27 20:06:46 +00:00
CONF_BED_SET_TEMPERATURE = "bed_set_temperature"
CONF_EXT_TEMPERATURE = "ext_temperature"
2024-12-27 20:06:46 +00:00
CONF_EXT_SET_TEMPERATURE = "ext_set_temperature"
2024-12-27 20:06:46 +00:00
CONF_PRINT_PROGRESS = "print_progress"
CONF_PRINT_TIME = "print_time"
CONF_PRINT_TIME_REMAINING = "print_time_remaining"
2024-12-27 10:47:42 +00:00
2024-12-30 09:39:19 +00:00
CONF_MARLIN = "marlin2"
CONFIG_SCHEMA = cv.Schema(
2024-12-27 09:51:18 +00:00
{
2024-12-30 09:39:19 +00:00
cv.GenerateID(CONF_MARLIN): cv.use_id(Marlin2),
2024-12-27 10:47:42 +00:00
cv.Optional(CONF_BED_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
2024-12-27 09:51:18 +00:00
),
2024-12-27 20:06:46 +00:00
cv.Optional(CONF_BED_SET_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_EXT_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
),
2024-12-27 20:06:46 +00:00
cv.Optional(CONF_EXT_SET_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
),
2024-12-27 20:06:46 +00:00
cv.Optional(CONF_PRINT_PROGRESS): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
accuracy_decimals=1,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_PRINT_TIME): sensor.sensor_schema(
2024-12-27 20:41:13 +00:00
unit_of_measurement=UNIT_SECOND,
2024-12-27 20:06:46 +00:00
accuracy_decimals=1,
device_class=DEVICE_CLASS_DURATION,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_PRINT_TIME_REMAINING): sensor.sensor_schema(
2024-12-27 20:41:13 +00:00
unit_of_measurement=UNIT_SECOND,
2024-12-27 20:06:46 +00:00
accuracy_decimals=1,
device_class=DEVICE_CLASS_DURATION,
state_class=STATE_CLASS_MEASUREMENT,
),
2024-12-27 09:51:18 +00:00
}
2024-12-30 09:39:19 +00:00
).extend(cv.polling_component_schema("15s"))
2024-12-27 09:55:11 +00:00
2024-12-27 09:51:18 +00:00
async def to_code(config):
2024-12-30 09:39:19 +00:00
server = await cg.get_variable(config[CONF_MARLIN])
2024-12-27 20:06:46 +00:00
for sName in [CONF_BED_TEMPERATURE, CONF_BED_SET_TEMPERATURE, CONF_EXT_TEMPERATURE, CONF_EXT_SET_TEMPERATURE, CONF_PRINT_PROGRESS, CONF_PRINT_TIME, CONF_PRINT_TIME_REMAINING]:
if sName in config:
sens = await sensor.new_sensor(config[sName])
2024-12-30 09:39:19 +00:00
cg.add(server.add_sensor(sName,sens))