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

55 lines
1.8 KiB
Python

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
from esphome import pins
DEPENDENCIES = ["uart"]
geiger_counter_sensor_ns = cg.esphome_ns.namespace("geiger_counter_sensor")
GeigerCounterSensor = geiger_counter_sensor_ns.class_(
"GeigerCounterSensor", cg.PollingComponent, uart.UARTDevice
)
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"
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)
.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_,
})
)
async def to_code(config):
var = await sensor.new_sensor(config)
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
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]))