Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronise Device Classes from Home Assistant #168

Open
wants to merge 12 commits into
base: cpl/temperbridge
Choose a base branch
from
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
secrets.yaml

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down Expand Up @@ -129,4 +131,4 @@ tests/.esphome/
sdkconfig.*
!sdkconfig.defaults

.tests/
.tests/
2 changes: 2 additions & 0 deletions esphome/components/button/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
CONF_TRIGGER_ID,
CONF_MQTT_ID,
DEVICE_CLASS_EMPTY,
DEVICE_CLASS_IDENTIFY,
DEVICE_CLASS_RESTART,
DEVICE_CLASS_UPDATE,
)
Expand All @@ -24,6 +25,7 @@

DEVICE_CLASSES = [
DEVICE_CLASS_EMPTY,
DEVICE_CLASS_IDENTIFY,
DEVICE_CLASS_RESTART,
DEVICE_CLASS_UPDATE,
]
Expand Down
6 changes: 6 additions & 0 deletions esphome/components/number/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
DEVICE_CLASS_DATA_RATE,
DEVICE_CLASS_DATA_SIZE,
DEVICE_CLASS_DISTANCE,
DEVICE_CLASS_DURATION,
DEVICE_CLASS_EMPTY,
DEVICE_CLASS_ENERGY,
DEVICE_CLASS_ENERGY_STORAGE,
Expand All @@ -42,6 +43,7 @@
DEVICE_CLASS_NITROGEN_MONOXIDE,
DEVICE_CLASS_NITROUS_OXIDE,
DEVICE_CLASS_OZONE,
DEVICE_CLASS_PH,
DEVICE_CLASS_PM1,
DEVICE_CLASS_PM10,
DEVICE_CLASS_PM25,
Expand All @@ -57,6 +59,7 @@
DEVICE_CLASS_SULPHUR_DIOXIDE,
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS,
DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS_PARTS,
DEVICE_CLASS_VOLTAGE,
DEVICE_CLASS_VOLUME,
DEVICE_CLASS_VOLUME_STORAGE,
Expand All @@ -80,6 +83,7 @@
DEVICE_CLASS_DATA_RATE,
DEVICE_CLASS_DATA_SIZE,
DEVICE_CLASS_DISTANCE,
DEVICE_CLASS_DURATION,
DEVICE_CLASS_EMPTY,
DEVICE_CLASS_ENERGY,
DEVICE_CLASS_ENERGY_STORAGE,
Expand All @@ -94,6 +98,7 @@
DEVICE_CLASS_NITROGEN_MONOXIDE,
DEVICE_CLASS_NITROUS_OXIDE,
DEVICE_CLASS_OZONE,
DEVICE_CLASS_PH,
DEVICE_CLASS_PM1,
DEVICE_CLASS_PM10,
DEVICE_CLASS_PM25,
Expand All @@ -109,6 +114,7 @@
DEVICE_CLASS_SULPHUR_DIOXIDE,
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS,
DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS_PARTS,
DEVICE_CLASS_VOLTAGE,
DEVICE_CLASS_VOLUME,
DEVICE_CLASS_VOLUME_STORAGE,
Expand Down
4 changes: 4 additions & 0 deletions esphome/components/sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
DEVICE_CLASS_NITROGEN_MONOXIDE,
DEVICE_CLASS_NITROUS_OXIDE,
DEVICE_CLASS_OZONE,
DEVICE_CLASS_PH,
DEVICE_CLASS_PM1,
DEVICE_CLASS_PM10,
DEVICE_CLASS_PM25,
Expand All @@ -73,6 +74,7 @@
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_TIMESTAMP,
DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS,
DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS_PARTS,
DEVICE_CLASS_VOLTAGE,
DEVICE_CLASS_VOLUME,
DEVICE_CLASS_VOLUME_STORAGE,
Expand Down Expand Up @@ -113,6 +115,7 @@
DEVICE_CLASS_NITROGEN_MONOXIDE,
DEVICE_CLASS_NITROUS_OXIDE,
DEVICE_CLASS_OZONE,
DEVICE_CLASS_PH,
DEVICE_CLASS_PM1,
DEVICE_CLASS_PM10,
DEVICE_CLASS_PM25,
Expand All @@ -129,6 +132,7 @@
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_TIMESTAMP,
DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS,
DEVICE_CLASS_VOLATILE_ORGANIC_COMPOUNDS_PARTS,
DEVICE_CLASS_VOLTAGE,
DEVICE_CLASS_VOLUME,
DEVICE_CLASS_VOLUME_STORAGE,
Expand Down
191 changes: 191 additions & 0 deletions esphome/components/temperbridge/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
from esphome import pins, automation
from esphome.automation import maybe_simple_id
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import spi
from esphome.const import CONF_ID, CONF_INTERRUPT_PIN

CONF_SDN_PIN = "sdn_pin"

CONF_CHANNEL = "channel"
CONF_CMD = "cmd"
CONF_TARGET = "target"
CONF_LEVEL = "level"

DEPENDENCIES = ["spi"]

temperbridge_ns = cg.esphome_ns.namespace("temperbridge")
TemperBridge = temperbridge_ns.class_(
"TemperBridgeComponent", cg.Component, spi.SPIDevice
)

temperbridge_simple_command_ns = temperbridge_ns.enum("SimpleCommand", is_class=True)

SIMPLE_COMMANDS = {
"flat": temperbridge_simple_command_ns.PRESET_FLAT,
"mode_1": temperbridge_simple_command_ns.PRESET_MODE1,
"mode_2": temperbridge_simple_command_ns.PRESET_MODE2,
"mode_3": temperbridge_simple_command_ns.PRESET_MODE3,
"mode_4": temperbridge_simple_command_ns.PRESET_MODE4,
"save_preset_mode1": temperbridge_simple_command_ns.SAVE_PRESET_MODE1,
"save_preset_mode2": temperbridge_simple_command_ns.SAVE_PRESET_MODE2,
"save_preset_mode3": temperbridge_simple_command_ns.SAVE_PRESET_MODE3,
"save_preset_mode4": temperbridge_simple_command_ns.SAVE_PRESET_MODE4,
"stop": temperbridge_simple_command_ns.STOP,
"massage_mode_1": temperbridge_simple_command_ns.MASSAGE_PRESET_MODE1,
"massage_mode_2": temperbridge_simple_command_ns.MASSAGE_PRESET_MODE2,
"massage_mode_3": temperbridge_simple_command_ns.MASSAGE_PRESET_MODE3,
"massage_mode_4": temperbridge_simple_command_ns.MASSAGE_PRESET_MODE4,
}

validate_simple_command = cv.enum(SIMPLE_COMMANDS, lower=True)

temperbridge_position_command_ns = temperbridge_ns.enum(
"PositionCommand", is_class=True
)

POSITION_COMMANDS = {
"raise_head": temperbridge_position_command_ns.RAISE_HEAD,
"raise_legs": temperbridge_position_command_ns.RAISE_LEGS,
"lower_head": temperbridge_position_command_ns.LOWER_HEAD,
"lower_legs": temperbridge_position_command_ns.LOWER_LEGS,
}

validate_position_command = cv.enum(POSITION_COMMANDS, lower=True)

temperbridge_massage_target_enum = temperbridge_ns.enum("MassageTarget", is_class=True)

MASSAGE_TARGET = {
"head": temperbridge_massage_target_enum.HEAD,
"legs": temperbridge_massage_target_enum.LEGS,
"lumbar": temperbridge_massage_target_enum.LUMBAR,
}

validate_massage_target = cv.enum(MASSAGE_TARGET, lower=True)

ExecuteSimpleCommandAction = temperbridge_ns.class_(
"ExecuteSimpleCommandAction", automation.Action
)

PositionCommandAction = temperbridge_ns.class_(
"PositionCommandAction", automation.Action
)

SetMassageIntensityAction = temperbridge_ns.class_(
"SetMassageIntensityAction", automation.Action
)

SetChannelAction = temperbridge_ns.class_("SetChannelAction", automation.Action)

CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(TemperBridge),
cv.Required(CONF_SDN_PIN): pins.gpio_output_pin_schema,
cv.Required(CONF_INTERRUPT_PIN): cv.All(
pins.internal_gpio_input_pin_schema
),
}
)
.extend(cv.COMPONENT_SCHEMA)
.extend(spi.spi_device_schema(cs_pin_required=True))
)


async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await spi.register_spi_device(var, config)

interrupt_pin = await cg.gpio_pin_expression(config[CONF_INTERRUPT_PIN])
cg.add(var.set_interrupt_pin(interrupt_pin))

sdn_pin = await cg.gpio_pin_expression(config[CONF_SDN_PIN])
cg.add(var.set_sdn_pin(sdn_pin))


@automation.register_action(
"temperbridge.position_command_2",
PositionCommandAction,
maybe_simple_id(
{
cv.GenerateID(): cv.use_id(TemperBridge),
cv.Required(CONF_CMD): cv.templatable(validate_position_command),
},
),
)
async def temperbridge_start_position_command_to_code(
config, action_id, template_arg, args
):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
cg.add(var.set_cmd(config[CONF_CMD]))
return var


@automation.register_action(
"temperbridge.execute_simple_command",
ExecuteSimpleCommandAction,
maybe_simple_id(
{
cv.GenerateID(): cv.use_id(TemperBridge),
cv.Required(CONF_CMD): cv.templatable(validate_simple_command),
},
),
)
async def temperbridge_execute_simple_command_to_code(
config, action_id, template_arg, args
):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
cg.add(var.set_cmd(config[CONF_CMD]))
return var


validate_channel = cv.All(cv.int_range(min=0, max=9999))


@automation.register_action(
"temperbridge.set_channel",
SetChannelAction,
cv.maybe_simple_value(
{
cv.GenerateID(): cv.use_id(TemperBridge),
cv.Required(CONF_CHANNEL): cv.templatable(validate_channel),
},
key=CONF_CHANNEL,
),
)
async def temperbridge_set_channel_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
template_ = await cg.templatable(config[CONF_CHANNEL], args, cg.uint16)
cg.add(var.set_channel(template_))
return var


validate_massage_level = cv.All(cv.int_range(min=0, max=10))


@automation.register_action(
"temperbridge.set_massage_intensity",
SetMassageIntensityAction,
cv.Schema(
{
cv.GenerateID(): cv.use_id(TemperBridge),
cv.Required(CONF_TARGET): cv.templatable(validate_massage_target),
cv.Required(CONF_LEVEL): cv.templatable(validate_massage_level),
}
),
)
async def temperbridge_set_massage_intensity_to_code(
config, action_id, template_arg, args
):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
template_ = await cg.templatable(config[CONF_TARGET], args, validate_massage_target)
cg.add(var.set_target(template_))

template_ = await cg.templatable(config[CONF_LEVEL], args, cg.uint8)
cg.add(var.set_level(template_))
return var
Loading