Loading files
# Copyright 2026 Scott Bezek
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from esphome import automation
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import button, i2c
# Aliased: importing the sibling `text_sensor.py` platform module binds it as an
# attribute of this package, which would shadow a plain `text_sensor` name here.
from esphome.components import text_sensor as core_text_sensor
from esphome.const import CONF_ID, CONF_MODE, CONF_NAME
from esphome.core import CORE
MULTI_CONF = True
DEPENDENCIES = ["i2c"]
AUTO_LOAD = ["button", "text_sensor"]
fader_buddy_ns = cg.esphome_ns.namespace("fader_buddy")
FaderBuddy = fader_buddy_ns.class_("FaderBuddy", cg.PollingComponent, i2c.I2CDevice)
SelfCalibrationButton = fader_buddy_ns.class_(
"SelfCalibrationButton", button.Button, cg.Parented.template(FaderBuddy)
)
# Used by platform files (e.g. text_sensor) to reference the parent hub
CONF_FADER_BUDDY_ID = "fader_buddy_id"
# Define haptic mode enum (in global namespace, shared with firmware)
HapticMode = cg.global_ns.enum("HapticMode")
HAPTIC_MODES = {
"smooth": HapticMode.HAPTIC_NO_HAPTICS,
"smooth_with_magnets": HapticMode.HAPTIC_SMOOTH_WITH_MAGNET_ENDS,
"detents": HapticMode.HAPTIC_DETENTS,
}
CONF_ON_MANUAL_MOVE = "on_manual_move"
CONF_ON_RAW_POSITION_UPDATE = "on_raw_position_update"
CONF_ON_TOUCH_CHANGE = "on_touch_change"
CONF_ON_DOUBLE_TAP = "on_double_tap"
CONF_INVERT = "invert"
CONF_LAYER_HAPTICS = "layer_haptics"
CONF_LAYER = "layer"
CONF_DETENT_COUNT = "detent_count"
CONF_DETENT_STRENGTH = "detent_strength"
CONF_POSITION = "position"
CONF_VALUE_CHANGE_MIN_INTERVAL = "value_change_min_interval"
CONF_SPEED = "speed"
CONF_DEFAULT_SPEED = "default_speed"
CONF_SERIAL_NUMBER = "serial_number"
CONF_FIRMWARE_VERSION = "firmware_version"
CONF_SELF_CALIBRATION = "self_calibration"
# Diagnostic text sensors the hub creates itself, so a bare fader_buddy block
# reports what it is without any entity yaml. Each maps to (default name
# suffix, icon). Set `internal: true` on one to keep it out of Home Assistant,
# or `disabled_by_default: true` to have HA register it but leave it off.
AUTO_TEXT_SENSORS = {
CONF_SERIAL_NUMBER: ("Serial Number", "mdi:identifier"),
CONF_FIRMWARE_VERSION: ("Firmware Version", "mdi:chip"),
}
# Buttons the hub creates itself, same deal. A press here moves the fader for
# several seconds, so these are entity_category "config" rather than controls.
AUTO_BUTTONS = {
CONF_SELF_CALIBRATION: ("Self Calibration", "mdi:tune-vertical"),
}
AUTO_ENTITIES = {**AUTO_TEXT_SENSORS, **AUTO_BUTTONS}
def _default_entity_names(config):
"""Give each auto-created text sensor a name before its schema validates.
The entity base schema rejects a sub-config carrying neither `name:` nor a
manual `id:`, and it runs while validating that sub-config - before any
validator on this schema could fill one in. So this has to be a
pre-validator, seeing the raw config.
The name is prefixed with the hub's id so several faders in one device don't
collide. A config with multiple hubs and no ids on them would; ESPHome's
auto-generated ids don't exist yet at this point.
"""
if not isinstance(config, dict):
return config
hub_id = config.get(CONF_ID)
prefix = f"{hub_id} " if isinstance(hub_id, str) else ""
for key, (label, _icon) in AUTO_ENTITIES.items():
sub = config.setdefault(key, {})
if isinstance(sub, dict) and CONF_NAME not in sub:
sub[CONF_NAME] = f"{prefix}{label}"
return config
def _claimed_by_legacy_platform(config, key):
"""True if a `text_sensor: platform: fader_buddy` block already provides this.
The deprecated platform form calls the same setter, so without this check a
config using it would get two entities for one sensor - the hub's own, left
unpublished, and the platform's. Drop the hub's in that case, so migrating
is a pure deletion of the old block.
"""
for entry in CORE.config.get("text_sensor", []):
if entry.get("platform") != "fader_buddy":
continue
if entry.get(CONF_FADER_BUDDY_ID) != config[CONF_ID]:
continue
if key in entry:
return True
return False
# Unitless move speed: 255 is full speed, 0 the slowest smooth motion
SPEED_FULL = 255
# Schema for a single layer haptic configuration
LAYER_HAPTIC_SCHEMA = cv.Schema({
cv.Required(CONF_LAYER): cv.int_range(min=0, max=7),
cv.Required(CONF_MODE): cv.enum(HAPTIC_MODES, lower=True),
cv.Optional(CONF_DETENT_COUNT, default=0): cv.int_range(min=0, max=15),
cv.Optional(CONF_DETENT_STRENGTH, default=0): cv.int_range(min=0, max=7),
cv.Optional(CONF_VALUE_CHANGE_MIN_INTERVAL, default="0ms"): cv.positive_time_period_milliseconds,
# Speed used for moves on this layer that don't name one. Kept host-side
# and sent with each move rather than stored on the fader.
cv.Optional(CONF_DEFAULT_SPEED, default=SPEED_FULL): cv.int_range(min=0, max=255),
})
CONFIG_SCHEMA = cv.All(
_default_entity_names,
cv.Schema({
cv.GenerateID(): cv.declare_id(FaderBuddy),
cv.Optional(CONF_SERIAL_NUMBER, default={}): core_text_sensor.text_sensor_schema(
entity_category="diagnostic",
icon=AUTO_TEXT_SENSORS[CONF_SERIAL_NUMBER][1],
),
cv.Optional(CONF_FIRMWARE_VERSION, default={}): core_text_sensor.text_sensor_schema(
entity_category="diagnostic",
icon=AUTO_TEXT_SENSORS[CONF_FIRMWARE_VERSION][1],
),
cv.Optional(CONF_SELF_CALIBRATION, default={}): button.button_schema(
SelfCalibrationButton,
entity_category="config",
icon=AUTO_BUTTONS[CONF_SELF_CALIBRATION][1],
),
cv.Optional(CONF_ON_MANUAL_MOVE): automation.validate_automation(single=True),
cv.Optional(CONF_ON_RAW_POSITION_UPDATE): automation.validate_automation(single=True),
cv.Optional(CONF_ON_TOUCH_CHANGE): automation.validate_automation(single=True),
cv.Optional(CONF_ON_DOUBLE_TAP): automation.validate_automation(single=True),
cv.Optional(CONF_INVERT, default=False): cv.boolean,
cv.Optional(CONF_LAYER_HAPTICS): cv.ensure_list(LAYER_HAPTIC_SCHEMA),
})
.extend(cv.polling_component_schema("50ms"))
.extend(i2c.i2c_device_schema(0x20)), # default I2C address
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
if CONF_INVERT in config:
cg.add(var.set_invert(config[CONF_INVERT]))
if not _claimed_by_legacy_platform(config, CONF_SERIAL_NUMBER):
sens = await core_text_sensor.new_text_sensor(config[CONF_SERIAL_NUMBER])
cg.add(var.set_serial_text_sensor(sens))
if not _claimed_by_legacy_platform(config, CONF_FIRMWARE_VERSION):
sens = await core_text_sensor.new_text_sensor(config[CONF_FIRMWARE_VERSION])
cg.add(var.set_firmware_text_sensor(sens))
btn = await button.new_button(config[CONF_SELF_CALIBRATION])
await cg.register_parented(btn, config[CONF_ID])
# Store initial layer haptic configurations (sent during setup)
if CONF_LAYER_HAPTICS in config:
for haptic_config in config[CONF_LAYER_HAPTICS]:
layer = haptic_config[CONF_LAYER]
mode = haptic_config[CONF_MODE]
detent_count = haptic_config[CONF_DETENT_COUNT]
detent_strength = haptic_config[CONF_DETENT_STRENGTH]
min_interval = haptic_config[CONF_VALUE_CHANGE_MIN_INTERVAL]
default_speed = haptic_config[CONF_DEFAULT_SPEED]
cg.add(var.store_initial_layer_haptic_config(
layer, mode, detent_count, detent_strength
))
cg.add(var.set_layer_value_change_min_interval(layer, min_interval))
cg.add(var.set_layer_default_speed(layer, default_speed))
if CONF_ON_MANUAL_MOVE in config:
await automation.build_automation(
var.get_on_manual_move_trigger(), [(cg.uint8, "x"), (cg.uint8, "layer")], config[CONF_ON_MANUAL_MOVE]
)
if CONF_ON_RAW_POSITION_UPDATE in config:
await automation.build_automation(
var.get_on_raw_position_update_trigger(), [(cg.uint8, "x"), (cg.uint8, "layer")], config[CONF_ON_RAW_POSITION_UPDATE]
)
if CONF_ON_TOUCH_CHANGE in config:
await automation.build_automation(
var.get_on_touch_change_trigger(), [(cg.bool_, "x"), (cg.uint8, "layer")], config[CONF_ON_TOUCH_CHANGE]
)
if CONF_ON_DOUBLE_TAP in config:
await automation.build_automation(
var.get_on_double_tap_trigger(), [(cg.uint8, "layer")], config[CONF_ON_DOUBLE_TAP]
)
# Actions
SetActiveLayerAction = fader_buddy_ns.class_("SetActiveLayerAction", automation.Action)
RemoteMoveToAction = fader_buddy_ns.class_("RemoteMoveToAction", automation.Action)
SetLayerHapticConfigAction = fader_buddy_ns.class_("SetLayerHapticConfigAction", automation.Action)
RunSelfCalibrationAction = fader_buddy_ns.class_("RunSelfCalibrationAction", automation.Action)
@automation.register_action(
"fader_buddy.set_active_layer",
SetActiveLayerAction,
cv.Schema({
cv.Required(CONF_ID): cv.use_id(FaderBuddy),
cv.Required(CONF_LAYER): cv.templatable(cv.int_range(min=0, max=7)),
})
)
async def set_active_layer_action_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
layer = await cg.templatable(config[CONF_LAYER], args, cg.uint8)
cg.add(var.set_layer(layer))
return var
@automation.register_action(
"fader_buddy.remote_move_to",
RemoteMoveToAction,
cv.Schema({
cv.Required(CONF_ID): cv.use_id(FaderBuddy),
cv.Required(CONF_POSITION): cv.templatable(cv.int_range(min=0, max=255)),
cv.Optional(CONF_LAYER, default=0): cv.templatable(cv.int_range(min=0, max=7)),
# Unitless move speed, 0 (slowest smooth motion) to 255 (full speed).
# This caps speed, it does not stretch the move to fill a duration, so
# a shorter move takes proportionally less time. Omit it to use the
# layer's default_speed.
cv.Optional(CONF_SPEED): cv.templatable(cv.int_range(min=0, max=255)),
})
)
async def remote_move_to_action_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
position = await cg.templatable(config[CONF_POSITION], args, cg.uint8)
cg.add(var.set_position(position))
layer = await cg.templatable(config[CONF_LAYER], args, cg.uint8)
cg.add(var.set_layer(layer))
if CONF_SPEED in config:
speed = await cg.templatable(config[CONF_SPEED], args, cg.uint8)
cg.add(var.set_speed(speed))
return var
@automation.register_action(
"fader_buddy.set_layer_haptic_config",
SetLayerHapticConfigAction,
cv.Schema({
cv.Required(CONF_ID): cv.use_id(FaderBuddy),
cv.Required(CONF_LAYER): cv.templatable(cv.int_range(min=0, max=7)),
cv.Required(CONF_MODE): cv.templatable(cv.enum(HAPTIC_MODES, lower=True)),
cv.Optional(CONF_DETENT_COUNT, default=0): cv.templatable(cv.int_range(min=0, max=15)),
cv.Optional(CONF_DETENT_STRENGTH, default=0): cv.templatable(cv.int_range(min=0, max=7)),
})
)
async def set_layer_haptic_config_action_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
layer = await cg.templatable(config[CONF_LAYER], args, cg.uint8)
cg.add(var.set_layer(layer))
mode = await cg.templatable(config[CONF_MODE], args, HapticMode)
cg.add(var.set_mode(mode))
detent_count = await cg.templatable(config[CONF_DETENT_COUNT], args, cg.uint8)
cg.add(var.set_detent_count(detent_count))
detent_strength = await cg.templatable(config[CONF_DETENT_STRENGTH], args, cg.uint8)
cg.add(var.set_detent_strength(detent_strength))
return var
@automation.register_action(
"fader_buddy.run_self_calibration",
RunSelfCalibrationAction,
cv.Schema({
cv.Required(CONF_ID): cv.use_id(FaderBuddy),
})
)
async def run_self_calibration_action_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
return var
392fafaadThis file is pinned to the selected release. Download the original to open it in your design tool.