import re
|
|
import esphome.codegen as cg
|
|
import esphome.config_validation as cv
|
|
from esphome.components import binary_sensor
|
|
from esphome.const import CONF_ID, CONF_FOR
|
|
from .. import (
|
|
bslamp2_ns, CODEOWNERS,
|
|
CONF_FRONT_PANEL_HAL_ID, FrontPanelHAL
|
|
)
|
|
|
|
AUTO_LOAD = ["xiaomi_bslamp2"]
|
|
|
|
CONF_PART = "part"
|
|
|
|
# The identifier values match the bit values of the events as defined
|
|
# in ../front_panel_hal.h.
|
|
PARTS = {
|
|
"POWER_BUTTON" : 0b001 << 1,
|
|
"POWER" : 0b001 << 1,
|
|
"COLOR_BUTTON" : 0b010 << 1,
|
|
"COLOR" : 0b010 << 1,
|
|
"SLIDER" : 0b100 << 1,
|
|
}
|
|
|
|
XiaomiBslamp2TouchBinarySensor = bslamp2_ns.class_(
|
|
"XiaomiBslamp2TouchBinarySensor", binary_sensor.BinarySensor, cg.Component)
|
|
|
|
def get_part_id(value):
|
|
normalized = re.sub('\s+', '_', value.upper())
|
|
try:
|
|
return PARTS[normalized]
|
|
except KeyError:
|
|
raise cv.Invalid(f"[{value}] is not a valid part identifier")
|
|
|
|
def validate_for(value):
|
|
parts = set()
|
|
if isinstance(value, str):
|
|
parts.add(get_part_id(value))
|
|
elif isinstance(value, list):
|
|
for x in value:
|
|
parts.add(get_part_id(x))
|
|
else:
|
|
cv.Invalid("The value must be a single part identifier or a list of identifiers.")
|
|
return list(parts)
|
|
|
|
def validate_part(value):
|
|
return [ get_part_id(value) ]
|
|
|
|
def validate_binary_sensor(conf):
|
|
if CONF_PART in conf and CONF_FOR in conf:
|
|
raise cv.Invalid("Specify only one of [part] or [for]")
|
|
if CONF_PART in conf and not CONF_FOR in conf:
|
|
conf[CONF_FOR] = conf[CONF_PART]
|
|
if CONF_FOR not in conf:
|
|
raise cv.Invalid("'for' is a required option for [binary_sensor.xiaomi_bslamp2]")
|
|
return conf
|
|
|
|
CONFIG_SCHEMA = cv.All(
|
|
binary_sensor.BINARY_SENSOR_SCHEMA.extend(
|
|
{
|
|
cv.GenerateID(): cv.declare_id(XiaomiBslamp2TouchBinarySensor),
|
|
cv.GenerateID(CONF_FRONT_PANEL_HAL_ID): cv.use_id(FrontPanelHAL),
|
|
# This option is not advertised in the documentation. It must be
|
|
# considered deprecated. I'm not announcing it as such yet. Not sure
|
|
# if it's useful to do so.
|
|
cv.Optional(CONF_PART): validate_part,
|
|
cv.Optional(CONF_FOR): validate_for,
|
|
}
|
|
).extend(cv.COMPONENT_SCHEMA),
|
|
validate_binary_sensor,
|
|
)
|
|
|
|
def to_code(config):
|
|
var = cg.new_Pvariable(config[CONF_ID])
|
|
yield cg.register_component(var, config)
|
|
yield binary_sensor.register_binary_sensor(var, config)
|
|
|
|
front_panel_hal_var = yield cg.get_variable(config[CONF_FRONT_PANEL_HAL_ID])
|
|
cg.add(var.set_parent(front_panel_hal_var))
|
|
for part_id in config[CONF_FOR]:
|
|
cg.add(var.include_part(part_id))
|