Skip to content

Commit

Permalink
Restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
AzonInc committed Oct 27, 2024
1 parent 8e49f28 commit 6611f4c
Show file tree
Hide file tree
Showing 20 changed files with 519 additions and 338 deletions.
180 changes: 26 additions & 154 deletions components/tc_bus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import text_sensor, binary_sensor, select, number
from esphome import pins, automation
from esphome.const import CONF_ID, ENTITY_CATEGORY_DIAGNOSTIC, ENTITY_CATEGORY_CONFIG, CONF_TRIGGER_ID, CONF_TYPE, CONF_VALUE

AUTO_LOAD = ["binary_sensor", "text_sensor", "select", "number"]

CODEOWNERS = ["@azoninc"]

tc_bus_ns = cg.esphome_ns.namespace("tc_bus")
Expand All @@ -30,13 +27,6 @@
CommandData = tc_bus_ns.struct(f"CommandData")
SettingData = tc_bus_ns.struct(f"SettingData")

IntercomModelSelect = tc_bus_ns.class_("IntercomModelSelect", select.Select, cg.Component)
IntercomRingtoneDoorCallSelect = tc_bus_ns.class_("IntercomRingtoneDoorCallSelect", select.Select, cg.Component)
IntercomRingtoneFloorCallSelect = tc_bus_ns.class_("IntercomRingtoneFloorCallSelect", select.Select, cg.Component)
IntercomRingtoneInternalCallSelect = tc_bus_ns.class_("IntercomRingtoneInternalCallSelect", select.Select, cg.Component)
IntercomVolumeHandsetNumber = tc_bus_ns.class_("IntercomVolumeHandsetNumber", number.Number, cg.Component)
IntercomVolumeRingtoneNumber = tc_bus_ns.class_("IntercomVolumeRingtoneNumber", number.Number, cg.Component)

ReadMemoryCompleteTrigger = tc_bus_ns.class_("ReadMemoryCompleteTrigger", automation.Trigger.template())
ReadMemoryTimeoutTrigger = tc_bus_ns.class_("ReadMemoryTimeoutTrigger", automation.Trigger.template())
ReceivedCommandTrigger = tc_bus_ns.class_("ReceivedCommandTrigger", automation.Trigger.template())
Expand Down Expand Up @@ -118,29 +108,15 @@

CONF_RX_PIN = "rx_pin"
CONF_TX_PIN = "tx_pin"

CONF_EVENT = "event"

CONF_COMMAND = "command"
CONF_SERIAL_NUMBER = "serial_number"
CONF_SERIAL_NUMBER_LAMBDA = "serial_number_lambda"
CONF_COMMAND = "command"
CONF_ADDRESS = "address"
CONF_ADDRESS_LAMBDA = "address_lambda"
CONF_PAYLOAD = "payload"
CONF_PAYLOAD_LAMBDA = "payload_lambda"

CONF_MODEL = "model"

CONF_RINGTONE_DOOR_CALL = "ringtone_door_call"
CONF_RINGTONE_FLOOR_CALL = "ringtone_floor_call"
CONF_RINGTONE_INTERNAL_CALL = "ringtone_internal_call"
CONF_VOLUME_HANDSET = "volume_handset"
CONF_VOLUME_RINGTONE = "volume_ringtone"

CONF_BUS_COMMAND = "bus_command"
CONF_HARDWARE_VERSION = "hardware_version"
CONF_DOOR_READINESS = "door_readiness"

CONF_ON_COMMAND = "on_command"
CONF_ON_MEMORY = "on_read_memory_complete"
CONF_ON_MEMORY_TIMEOUT = "on_read_memory_timeout"
Expand All @@ -151,69 +127,32 @@
def validate_config(config):
return config

CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(TCBus),
cv.Optional(CONF_RX_PIN, default=9): pins.internal_gpio_input_pin_schema,
cv.Optional(CONF_TX_PIN, default=8): pins.internal_gpio_output_pin_schema,
cv.Optional(CONF_EVENT, default="tc"): cv.string,
cv.Optional(CONF_ON_COMMAND): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ReceivedCommandTrigger),
}
),
cv.Optional(CONF_ON_MEMORY): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ReadMemoryCompleteTrigger),
}
),
cv.Optional(CONF_ON_MEMORY_TIMEOUT): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ReadMemoryTimeoutTrigger),
}
),
}
)

CONFIG_SCHEMA = cv.All(
cv.Schema(
{
cv.GenerateID(): cv.declare_id(TCBus),
cv.Optional(CONF_RX_PIN, default=9): pins.internal_gpio_input_pin_schema,
cv.Optional(CONF_TX_PIN, default=8): pins.internal_gpio_output_pin_schema,
cv.Optional(CONF_EVENT, default="tc"): cv.string,
cv.Optional(CONF_SERIAL_NUMBER, default=0): cv.hex_uint32_t,
cv.Optional(CONF_SERIAL_NUMBER_LAMBDA): cv.returning_lambda,
cv.Optional(CONF_MODEL): select.select_schema(
IntercomModelSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
),
cv.Optional(CONF_RINGTONE_DOOR_CALL): select.select_schema(
IntercomRingtoneDoorCallSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
),
cv.Optional(CONF_RINGTONE_FLOOR_CALL): select.select_schema(
IntercomRingtoneFloorCallSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
),
cv.Optional(CONF_RINGTONE_INTERNAL_CALL): select.select_schema(
IntercomRingtoneInternalCallSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
),
cv.Optional(CONF_VOLUME_HANDSET): number.number_schema(
IntercomVolumeHandsetNumber,
entity_category=ENTITY_CATEGORY_CONFIG,
),
cv.Optional(CONF_VOLUME_RINGTONE): number.number_schema(
IntercomVolumeRingtoneNumber,
entity_category=ENTITY_CATEGORY_CONFIG,
),
cv.Optional(CONF_BUS_COMMAND): text_sensor.text_sensor_schema(
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
icon="mdi:console-network",
),
cv.Optional(CONF_HARDWARE_VERSION): text_sensor.text_sensor_schema(
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
icon="mdi:router-network-wireless",
),
cv.Optional(CONF_DOOR_READINESS): binary_sensor.binary_sensor_schema(
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
icon="mdi:router-network-wireless",
),
cv.Optional(CONF_ON_COMMAND): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ReceivedCommandTrigger),
}
),
cv.Optional(CONF_ON_MEMORY): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ReadMemoryCompleteTrigger),
}
),
cv.Optional(CONF_ON_MEMORY_TIMEOUT): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(ReadMemoryTimeoutTrigger),
}
),
}
)
.extend(cv.COMPONENT_SCHEMA),
CONFIG_SCHEMA.extend(cv.COMPONENT_SCHEMA),
validate_config,
)

Expand All @@ -230,73 +169,6 @@ async def to_code(config):

cg.add(var.set_event("esphome." + config[CONF_EVENT]))

if CONF_SERIAL_NUMBER in config:
cg.add(var.set_sn(config[CONF_SERIAL_NUMBER]))

if CONF_SERIAL_NUMBER_LAMBDA in config:
template_ = await cg.process_lambda(
config[CONF_SERIAL_NUMBER_LAMBDA], [], return_type=cg.optional.template(cg.uint32)
)
cg.add(var.set_sn_lambda(template_))

if model := config.get(CONF_MODEL):
sel = await select.new_select(
model,
options=[CONF_MODELS],
)
await cg.register_parented(sel, config[CONF_ID])
cg.add(var.set_intercom_model_select(sel))

if ringtone_door_call := config.get(CONF_RINGTONE_DOOR_CALL):
sel = await select.new_select(
ringtone_door_call,
options=[CONF_RINGTONES],
)
await cg.register_parented(sel, config[CONF_ID])
cg.add(var.set_intercom_ringtone_door_call_select(sel))

if ringtone_floor_call := config.get(CONF_RINGTONE_FLOOR_CALL):
sel = await select.new_select(
ringtone_floor_call,
options=[CONF_RINGTONES],
)
await cg.register_parented(sel, config[CONF_ID])
cg.add(var.set_intercom_ringtone_floor_call_select(sel))

if ringtone_internal_call := config.get(CONF_RINGTONE_INTERNAL_CALL):
sel = await select.new_select(
ringtone_internal_call,
options=[CONF_RINGTONES],
)
await cg.register_parented(sel, config[CONF_ID])
cg.add(var.set_intercom_ringtone_internal_call_select(sel))

if volume_handset := config.get(CONF_VOLUME_HANDSET):
n = await number.new_number(
volume_handset, min_value=0, max_value=7, step=1
)
await cg.register_parented(n, config[CONF_ID])
cg.add(var.set_intercom_volume_handset_number(n))

if volume_ringtone := config.get(CONF_VOLUME_RINGTONE):
n = await number.new_number(
volume_ringtone, min_value=0, max_value=7, step=1
)
await cg.register_parented(n, config[CONF_ID])
cg.add(var.set_intercom_volume_ringtone_number(n))

if CONF_BUS_COMMAND in config:
sens = await text_sensor.new_text_sensor(config[CONF_BUS_COMMAND])
cg.add(var.set_bus_command_text_sensor(sens))

if CONF_HARDWARE_VERSION in config:
sens = await text_sensor.new_text_sensor(config[CONF_HARDWARE_VERSION])
cg.add(var.set_hardware_version_text_sensor(sens))

if CONF_DOOR_READINESS in config:
sens = await binary_sensor.new_binary_sensor(config[CONF_DOOR_READINESS])
cg.add(var.set_door_readiness_binary_sensor(sens))

for conf in config.get(CONF_ON_COMMAND, []):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
await automation.build_automation(trigger, [(CommandData, "x")], conf)
Expand Down
59 changes: 59 additions & 0 deletions components/tc_bus/number/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import esphome.codegen as cg
from esphome.components import number
import esphome.config_validation as cv
from esphome.const import (
CONF_ID,
ENTITY_CATEGORY_CONFIG,
)
from .. import CONF_TC_ID, TCBus, tc_bus_ns

SerialNumberNumber = tc_bus_ns.class_("SerialNumberNumber", number.Number, cg.Component)
VolumeHandsetNumber = tc_bus_ns.class_("VolumeHandsetNumber", number.Number, cg.Component)
VolumeRingtoneNumber = tc_bus_ns.class_("VolumeRingtoneNumber", number.Number, cg.Component)

CONF_SERIAL_NUMBER = "serial_number"
CONF_VOLUME_HANDSET = "volume_handset"
CONF_VOLUME_RINGTONE = "volume_ringtone"

CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_TC_ID): cv.use_id(TCBus),
cv.Optional(CONF_SERIAL_NUMBER): number.number_schema(
SerialNumberNumber,
entity_category=ENTITY_CATEGORY_CONFIG,
),
cv.Optional(CONF_VOLUME_HANDSET): number.number_schema(
VolumeHandsetNumber,
entity_category=ENTITY_CATEGORY_CONFIG,
),
cv.Optional(CONF_VOLUME_RINGTONE): number.number_schema(
VolumeRingtoneNumber,
entity_category=ENTITY_CATEGORY_CONFIG,
),
}
)


async def to_code(config):
tc_bus_component = await cg.get_variable(config[CONF_TC_ID])

if serial_number := config.get(CONF_SERIAL_NUMBER):
n = await number.new_number(
serial_number, min_value=0, max_value=1048575, step=0
)
await cg.register_parented(n, config[CONF_TC_ID])
cg.add(tc_bus_component.set_serial_number_number(n))

if volume_handset := config.get(CONF_VOLUME_HANDSET):
n = await number.new_number(
volume_handset, min_value=0, max_value=7, step=1
)
await cg.register_parented(n, config[CONF_TC_ID])
cg.add(tc_bus_component.set_volume_handset_number(n))

if volume_ringtone := config.get(CONF_VOLUME_RINGTONE):
n = await number.new_number(
volume_ringtone, min_value=0, max_value=7, step=1
)
await cg.register_parented(n, config[CONF_TC_ID])
cg.add(tc_bus_component.set_volume_ringtone_number(n))
13 changes: 13 additions & 0 deletions components/tc_bus/number/serial_number_number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "serial_number_number.h"

namespace esphome {
namespace tc_bus {

void SerialNumberNumber::control(float value)
{
this->publish_state(value);
this->parent_->set_serial_number(value);
}

} // namespace tc_bus
} // namespace esphome
18 changes: 18 additions & 0 deletions components/tc_bus/number/serial_number_number.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "esphome/components/number/number.h"
#include "../tc_bus.h"

namespace esphome {
namespace tc_bus {

class SerialNumberNumber : public number::Number, public Parented<TCBusComponent> {
public:
SerialNumberNumber() = default;

protected:
void control(float value) override;
};

} // namespace tc_bus
} // namespace esphome
13 changes: 13 additions & 0 deletions components/tc_bus/number/volume_handset_number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "volume_handset_number.h"

namespace esphome {
namespace tc_bus {

void VolumeHandsetNumber::control(float value)
{
this->publish_state(value);
this->parent_->update_setting(SETTING_VOLUME_HANDSET, value, 0);
}

} // namespace tc_bus
} // namespace esphome
18 changes: 18 additions & 0 deletions components/tc_bus/number/volume_handset_number.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "esphome/components/number/number.h"
#include "../tc_bus.h"

namespace esphome {
namespace tc_bus {

class VolumeHandsetNumber : public number::Number, public Parented<TCBusComponent> {
public:
VolumeHandsetNumber() = default;

protected:
void control(float value) override;
};

} // namespace tc_bus
} // namespace esphome
13 changes: 13 additions & 0 deletions components/tc_bus/number/volume_ringtone_number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "volume_ringtone_number.h"

namespace esphome {
namespace tc_bus {

void VolumeRingtoneNumber::control(float value)
{
this->publish_state(value);
this->parent_->update_setting(SETTING_VOLUME_RINGTONE, value, 0);
}

} // namespace tc_bus
} // namespace esphome
18 changes: 18 additions & 0 deletions components/tc_bus/number/volume_ringtone_number.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "esphome/components/number/number.h"
#include "../tc_bus.h"

namespace esphome {
namespace tc_bus {

class VolumeRingtoneNumber : public number::Number, public Parented<TCBusComponent> {
public:
VolumeRingtoneNumber() = default;

protected:
void control(float value) override;
};

} // namespace tc_bus
} // namespace esphome
Loading

0 comments on commit 6611f4c

Please sign in to comment.