esphome-geiger-counter/components/geiger_counter_sensor/sensor.py

55 lines
1.8 KiB
Python
Raw Normal View History

2024-09-13 18:22:15 -06:00
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import uart, sensor
from esphome.const import ICON_RADIATOR, UNIT_EMPTY
2024-09-15 21:57:37 -06:00
from esphome import pins
2024-09-13 18:22:15 -06:00
DEPENDENCIES = ["uart"]
geiger_counter_sensor_ns = cg.esphome_ns.namespace("geiger_counter_sensor")
GeigerCounterSensor = geiger_counter_sensor_ns.class_(
"GeigerCounterSensor", cg.PollingComponent, uart.UARTDevice
)
2024-09-15 21:57:37 -06:00
CONF_GREEN_LED_PIN = "green_led_pin"
CONF_YELLOW_LED_PIN = "yellow_led_pin"
CONF_RED_LED_PIN = "red_led_pin"
CONF_SAFE_LEVEL = "safe_level"
CONF_WARNING_LEVEL = "warning_level"
2024-09-13 18:22:15 -06:00
CONFIG_SCHEMA = (
sensor.sensor_schema(
GeigerCounterSensor,
unit_of_measurement="μSv",
icon='mdi:radioactive-circle',
accuracy_decimals=2,
)
.extend(cv.polling_component_schema("60s"))
.extend(uart.UART_DEVICE_SCHEMA)
2024-09-15 21:57:37 -06:00
.extend({
cv.Required(CONF_GREEN_LED_PIN): pins.gpio_output_pin_schema,
cv.Required(CONF_YELLOW_LED_PIN): pins.gpio_output_pin_schema,
cv.Required(CONF_RED_LED_PIN): pins.gpio_output_pin_schema,
cv.Optional(CONF_SAFE_LEVEL, default=0.35): cv.float_,
cv.Optional(CONF_WARNING_LEVEL, default=0.7): cv.float_,
})
2024-09-13 18:22:15 -06:00
)
async def to_code(config):
var = await sensor.new_sensor(config)
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
2024-09-15 21:57:37 -06:00
green_led_pin = await cg.gpio_pin_expression(config[CONF_GREEN_LED_PIN])
cg.add(var.set_green_led_pin(green_led_pin))
yellow_led_pin = await cg.gpio_pin_expression(config[CONF_YELLOW_LED_PIN])
cg.add(var.set_yellow_led_pin(yellow_led_pin))
red_led_pin = await cg.gpio_pin_expression(config[CONF_RED_LED_PIN])
cg.add(var.set_red_led_pin(red_led_pin))
cg.add(var.set_safe_level(config[CONF_SAFE_LEVEL]))
cg.add(var.set_warning_level(config[CONF_WARNING_LEVEL]))