This commit is contained in:
J. Nick Koston
2025-06-05 22:32:40 +01:00
parent a4d3849ef4
commit aa2f0e8ff9
6 changed files with 635 additions and 675 deletions

View File

@@ -260,7 +260,7 @@ void APIConnection::loop() {
resp.entity_id = it.entity_id;
resp.attribute = it.attribute.value();
resp.once = it.once;
if (this->send_subscribe_home_assistant_state_response(resp)) {
if (this->send_message(resp)) {
state_subs_at_++;
}
}
@@ -301,15 +301,14 @@ bool APIConnection::try_send_binary_sensor_state_(binary_sensor::BinarySensor *b
msg.state = state;
msg.missing_state = !binary_sensor->has_state();
msg.key = binary_sensor->get_object_id_hash();
return this->send_binary_sensor_state_response(msg);
return this->send_message(msg);
}
bool APIConnection::try_send_binary_sensor_info_(binary_sensor::BinarySensor *binary_sensor) {
ListEntitiesBinarySensorResponse msg;
msg.device_class = binary_sensor->get_device_class();
msg.is_status_binary_sensor = binary_sensor->is_status_binary_sensor();
msg.unique_id = get_default_unique_id("binary_sensor", binary_sensor);
return this->try_send_entity_info_(static_cast<EntityBase *>(binary_sensor), msg,
&APIConnection::send_list_entities_binary_sensor_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(binary_sensor), msg);
}
#endif
@@ -332,7 +331,7 @@ bool APIConnection::try_send_cover_state_(cover::Cover *cover) {
msg.tilt = cover->tilt;
msg.current_operation = static_cast<enums::CoverOperation>(cover->current_operation);
msg.key = cover->get_object_id_hash();
return this->send_cover_state_response(msg);
return this->send_message(msg);
}
bool APIConnection::try_send_cover_info_(cover::Cover *cover) {
ListEntitiesCoverResponse msg;
@@ -343,8 +342,7 @@ bool APIConnection::try_send_cover_info_(cover::Cover *cover) {
msg.supports_stop = traits.get_supports_stop();
msg.device_class = cover->get_device_class();
msg.unique_id = get_default_unique_id("cover", cover);
return this->try_send_entity_info_(static_cast<EntityBase *>(cover), msg,
&APIConnection::send_list_entities_cover_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(cover), msg);
}
void APIConnection::cover_command(const CoverCommandRequest &msg) {
cover::Cover *cover = App.get_cover_by_key(msg.key);
@@ -398,7 +396,7 @@ bool APIConnection::try_send_fan_state_(fan::Fan *fan) {
if (traits.supports_preset_modes())
msg.preset_mode = fan->preset_mode;
msg.key = fan->get_object_id_hash();
return this->send_fan_state_response(msg);
return this->send_message(msg);
}
bool APIConnection::try_send_fan_info_(fan::Fan *fan) {
ListEntitiesFanResponse msg;
@@ -410,8 +408,7 @@ bool APIConnection::try_send_fan_info_(fan::Fan *fan) {
for (auto const &preset : traits.supported_preset_modes())
msg.supported_preset_modes.push_back(preset);
msg.unique_id = get_default_unique_id("fan", fan);
return this->try_send_entity_info_(static_cast<EntityBase *>(fan), msg,
&APIConnection::send_list_entities_fan_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(fan), msg);
}
void APIConnection::fan_command(const FanCommandRequest &msg) {
fan::Fan *fan = App.get_fan_by_key(msg.key);
@@ -463,7 +460,7 @@ bool APIConnection::try_send_light_state_(light::LightState *light) {
if (light->supports_effects())
resp.effect = light->get_effect_name();
resp.key = light->get_object_id_hash();
return this->send_light_state_response(resp);
return this->send_message(resp);
}
bool APIConnection::try_send_light_info_(light::LightState *light) {
ListEntitiesLightResponse msg;
@@ -488,8 +485,7 @@ bool APIConnection::try_send_light_info_(light::LightState *light) {
}
}
msg.unique_id = get_default_unique_id("light", light);
return this->try_send_entity_info_(static_cast<EntityBase *>(light), msg,
&APIConnection::send_list_entities_light_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(light), msg);
}
void APIConnection::light_command(const LightCommandRequest &msg) {
light::LightState *light = App.get_light_by_key(msg.key);
@@ -546,7 +542,7 @@ bool APIConnection::try_send_sensor_state_(sensor::Sensor *sensor, float state)
resp.missing_state = !sensor->has_state();
resp.key = sensor->get_object_id_hash();
return this->send_sensor_state_response(resp);
return this->send_message(resp);
}
bool APIConnection::try_send_sensor_info_(sensor::Sensor *sensor) {
ListEntitiesSensorResponse msg;
@@ -558,8 +554,7 @@ bool APIConnection::try_send_sensor_info_(sensor::Sensor *sensor) {
msg.unique_id = sensor->unique_id();
if (msg.unique_id.empty())
msg.unique_id = get_default_unique_id("sensor", sensor);
return this->try_send_entity_info_(static_cast<EntityBase *>(sensor), msg,
&APIConnection::send_list_entities_sensor_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(sensor), msg);
}
#endif
@@ -580,15 +575,14 @@ bool APIConnection::try_send_switch_state_(switch_::Switch *a_switch, bool state
resp.state = state;
resp.key = a_switch->get_object_id_hash();
return this->send_switch_state_response(resp);
return this->send_message(resp);
}
bool APIConnection::try_send_switch_info_(switch_::Switch *a_switch) {
ListEntitiesSwitchResponse msg;
msg.assumed_state = a_switch->assumed_state();
msg.device_class = a_switch->get_device_class();
msg.unique_id = get_default_unique_id("switch", a_switch);
return this->try_send_entity_info_(static_cast<EntityBase *>(a_switch), msg,
&APIConnection::send_list_entities_switch_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(a_switch), msg);
}
void APIConnection::switch_command(const SwitchCommandRequest &msg) {
switch_::Switch *a_switch = App.get_switch_by_key(msg.key);
@@ -621,7 +615,7 @@ bool APIConnection::try_send_text_sensor_state_(text_sensor::TextSensor *text_se
resp.missing_state = !text_sensor->has_state();
resp.key = text_sensor->get_object_id_hash();
return this->send_text_sensor_state_response(resp);
return this->send_message(resp);
}
bool APIConnection::try_send_text_sensor_info_(text_sensor::TextSensor *text_sensor) {
ListEntitiesTextSensorResponse msg;
@@ -629,8 +623,7 @@ bool APIConnection::try_send_text_sensor_info_(text_sensor::TextSensor *text_sen
msg.unique_id = text_sensor->unique_id();
if (msg.unique_id.empty())
msg.unique_id = get_default_unique_id("text_sensor", text_sensor);
return this->try_send_entity_info_(static_cast<EntityBase *>(text_sensor), msg,
&APIConnection::send_list_entities_text_sensor_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(text_sensor), msg);
}
#endif
@@ -672,7 +665,7 @@ bool APIConnection::try_send_climate_state_(climate::Climate *climate) {
resp.current_humidity = climate->current_humidity;
if (traits.get_supports_target_humidity())
resp.target_humidity = climate->target_humidity;
return this->send_climate_state_response(resp);
return this->send_message(resp);
}
bool APIConnection::try_send_climate_info_(climate::Climate *climate) {
ListEntitiesClimateResponse msg;
@@ -702,8 +695,7 @@ bool APIConnection::try_send_climate_info_(climate::Climate *climate) {
for (auto swing_mode : traits.get_supported_swing_modes())
msg.supported_swing_modes.push_back(static_cast<enums::ClimateSwingMode>(swing_mode));
msg.unique_id = get_default_unique_id("climate", climate);
return this->try_send_entity_info_(static_cast<EntityBase *>(climate), msg,
&APIConnection::send_list_entities_climate_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(climate), msg);
}
void APIConnection::climate_command(const ClimateCommandRequest &msg) {
climate::Climate *climate = App.get_climate_by_key(msg.key);
@@ -753,7 +745,7 @@ bool APIConnection::try_send_number_state_(number::Number *number, float state)
resp.missing_state = !number->has_state();
resp.key = number->get_object_id_hash();
return this->send_number_state_response(resp);
return this->send_message(resp);
}
bool APIConnection::try_send_number_info_(number::Number *number) {
ListEntitiesNumberResponse msg;
@@ -764,8 +756,7 @@ bool APIConnection::try_send_number_info_(number::Number *number) {
msg.max_value = number->traits.get_max_value();
msg.step = number->traits.get_step();
msg.unique_id = get_default_unique_id("number", number);
return this->try_send_entity_info_(static_cast<EntityBase *>(number), msg,
&APIConnection::send_list_entities_number_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(number), msg);
}
void APIConnection::number_command(const NumberCommandRequest &msg) {
number::Number *number = App.get_number_by_key(msg.key);
@@ -795,13 +786,12 @@ bool APIConnection::try_send_date_state_(datetime::DateEntity *date) {
resp.day = date->day;
resp.key = date->get_object_id_hash();
return this->send_date_state_response(resp);
return this->send_message(resp);
}
bool APIConnection::try_send_date_info_(datetime::DateEntity *date) {
ListEntitiesDateResponse msg;
msg.unique_id = get_default_unique_id("date", date);
return this->try_send_entity_info_(static_cast<EntityBase *>(date), msg,
&APIConnection::send_list_entities_date_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(date), msg);
}
void APIConnection::date_command(const DateCommandRequest &msg) {
datetime::DateEntity *date = App.get_date_by_key(msg.key);
@@ -831,13 +821,12 @@ bool APIConnection::try_send_time_state_(datetime::TimeEntity *time) {
resp.second = time->second;
resp.key = time->get_object_id_hash();
return this->send_time_state_response(resp);
return this->send_message(resp);
}
bool APIConnection::try_send_time_info_(datetime::TimeEntity *time) {
ListEntitiesTimeResponse msg;
msg.unique_id = get_default_unique_id("time", time);
return this->try_send_entity_info_(static_cast<EntityBase *>(time), msg,
&APIConnection::send_list_entities_time_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(time), msg);
}
void APIConnection::time_command(const TimeCommandRequest &msg) {
datetime::TimeEntity *time = App.get_time_by_key(msg.key);
@@ -868,13 +857,12 @@ bool APIConnection::try_send_datetime_state_(datetime::DateTimeEntity *datetime)
}
resp.key = datetime->get_object_id_hash();
return this->send_date_time_state_response(resp);
return this->send_message(resp);
}
bool APIConnection::try_send_datetime_info_(datetime::DateTimeEntity *datetime) {
ListEntitiesDateTimeResponse msg;
msg.unique_id = get_default_unique_id("datetime", datetime);
return this->try_send_entity_info_(static_cast<EntityBase *>(datetime), msg,
&APIConnection::send_list_entities_date_time_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(datetime), msg);
}
void APIConnection::datetime_command(const DateTimeCommandRequest &msg) {
datetime::DateTimeEntity *datetime = App.get_datetime_by_key(msg.key);
@@ -903,7 +891,7 @@ bool APIConnection::try_send_text_state_(text::Text *text, std::string state) {
resp.missing_state = !text->has_state();
resp.key = text->get_object_id_hash();
return this->send_text_state_response(resp);
return this->send_message(resp);
}
bool APIConnection::try_send_text_info_(text::Text *text) {
ListEntitiesTextResponse msg;
@@ -912,8 +900,7 @@ bool APIConnection::try_send_text_info_(text::Text *text) {
msg.max_length = text->traits.get_max_length();
msg.pattern = text->traits.get_pattern();
msg.unique_id = get_default_unique_id("text", text);
return this->try_send_entity_info_(static_cast<EntityBase *>(text), msg,
&APIConnection::send_list_entities_text_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(text), msg);
}
void APIConnection::text_command(const TextCommandRequest &msg) {
text::Text *text = App.get_text_by_key(msg.key);
@@ -944,15 +931,14 @@ bool APIConnection::try_send_select_state_(select::Select *select, std::string s
resp.missing_state = !select->has_state();
resp.key = select->get_object_id_hash();
return this->send_select_state_response(resp);
return this->send_message(resp);
}
bool APIConnection::try_send_select_info_(select::Select *select) {
ListEntitiesSelectResponse msg;
for (const auto &option : select->traits.get_options())
msg.options.push_back(option);
msg.unique_id = get_default_unique_id("select", select);
return this->try_send_entity_info_(static_cast<EntityBase *>(select), msg,
&APIConnection::send_list_entities_select_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(select), msg);
}
void APIConnection::select_command(const SelectCommandRequest &msg) {
select::Select *select = App.get_select_by_key(msg.key);
@@ -974,8 +960,7 @@ bool esphome::api::APIConnection::try_send_button_info_(button::Button *button)
ListEntitiesButtonResponse msg;
msg.device_class = button->get_device_class();
msg.unique_id = get_default_unique_id("button", button);
return this->try_send_entity_info_(static_cast<EntityBase *>(button), msg,
&APIConnection::send_list_entities_button_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(button), msg);
}
void esphome::api::APIConnection::button_command(const ButtonCommandRequest &msg) {
button::Button *button = App.get_button_by_key(msg.key);
@@ -1003,7 +988,7 @@ bool APIConnection::try_send_lock_state_(lock::Lock *a_lock, lock::LockState sta
resp.state = static_cast<enums::LockState>(state);
resp.key = a_lock->get_object_id_hash();
return this->send_lock_state_response(resp);
return this->send_message(resp);
}
bool APIConnection::try_send_lock_info_(lock::Lock *a_lock) {
ListEntitiesLockResponse msg;
@@ -1011,8 +996,7 @@ bool APIConnection::try_send_lock_info_(lock::Lock *a_lock) {
msg.supports_open = a_lock->traits.get_supports_open();
msg.requires_code = a_lock->traits.get_requires_code();
msg.unique_id = get_default_unique_id("lock", a_lock);
return this->try_send_entity_info_(static_cast<EntityBase *>(a_lock), msg,
&APIConnection::send_list_entities_lock_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(a_lock), msg);
}
void APIConnection::lock_command(const LockCommandRequest &msg) {
lock::Lock *a_lock = App.get_lock_by_key(msg.key);
@@ -1048,7 +1032,7 @@ bool APIConnection::try_send_valve_state_(valve::Valve *valve) {
resp.current_operation = static_cast<enums::ValveOperation>(valve->current_operation);
resp.key = valve->get_object_id_hash();
return this->send_valve_state_response(resp);
return this->send_message(resp);
}
bool APIConnection::try_send_valve_info_(valve::Valve *valve) {
ListEntitiesValveResponse msg;
@@ -1058,8 +1042,7 @@ bool APIConnection::try_send_valve_info_(valve::Valve *valve) {
msg.supports_position = traits.get_supports_position();
msg.supports_stop = traits.get_supports_stop();
msg.unique_id = get_default_unique_id("valve", valve);
return this->try_send_entity_info_(static_cast<EntityBase *>(valve), msg,
&APIConnection::send_list_entities_valve_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(valve), msg);
}
void APIConnection::valve_command(const ValveCommandRequest &msg) {
valve::Valve *valve = App.get_valve_by_key(msg.key);
@@ -1094,7 +1077,7 @@ bool APIConnection::try_send_media_player_state_(media_player::MediaPlayer *medi
resp.muted = media_player->is_muted();
resp.key = media_player->get_object_id_hash();
return this->send_media_player_state_response(resp);
return this->send_message(resp);
}
bool APIConnection::try_send_media_player_info_(media_player::MediaPlayer *media_player) {
ListEntitiesMediaPlayerResponse msg;
@@ -1110,8 +1093,7 @@ bool APIConnection::try_send_media_player_info_(media_player::MediaPlayer *media
msg.supported_formats.push_back(media_format);
}
msg.unique_id = get_default_unique_id("media_player", media_player);
return this->try_send_entity_info_(static_cast<EntityBase *>(media_player), msg,
&APIConnection::send_list_entities_media_player_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(media_player), msg);
}
void APIConnection::media_player_command(const MediaPlayerCommandRequest &msg) {
media_player::MediaPlayer *media_player = App.get_media_player_by_key(msg.key);
@@ -1152,8 +1134,7 @@ void APIConnection::send_camera_info(esp32_camera::ESP32Camera *camera) {
bool APIConnection::try_send_camera_info_(esp32_camera::ESP32Camera *camera) {
ListEntitiesCameraResponse msg;
msg.unique_id = get_default_unique_id("camera", camera);
return this->try_send_entity_info_(static_cast<EntityBase *>(camera), msg,
&APIConnection::send_list_entities_camera_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(camera), msg);
}
void APIConnection::camera_image(const CameraImageRequest &msg) {
if (esp32_camera::global_esp32_camera == nullptr)
@@ -1196,9 +1177,9 @@ bool APIConnection::send_bluetooth_le_advertisement(const BluetoothLEAdvertiseme
manufacturer_data.legacy_data.assign(manufacturer_data.data.begin(), manufacturer_data.data.end());
manufacturer_data.data.clear();
}
return this->send_bluetooth_le_advertisement_response(resp);
return this->send_message(resp);
}
return this->send_bluetooth_le_advertisement_response(msg);
return this->send_message(msg);
}
void APIConnection::bluetooth_device_request(const BluetoothDeviceRequest &msg) {
bluetooth_proxy::global_bluetooth_proxy->bluetooth_device_request(msg);
@@ -1354,7 +1335,7 @@ bool APIConnection::try_send_alarm_control_panel_state_(alarm_control_panel::Ala
resp.state = static_cast<enums::AlarmControlPanelState>(a_alarm_control_panel->get_state());
resp.key = a_alarm_control_panel->get_object_id_hash();
return this->send_alarm_control_panel_state_response(resp);
return this->send_message(resp);
}
bool APIConnection::try_send_alarm_control_panel_info_(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel) {
ListEntitiesAlarmControlPanelResponse msg;
@@ -1362,8 +1343,7 @@ bool APIConnection::try_send_alarm_control_panel_info_(alarm_control_panel::Alar
msg.requires_code = a_alarm_control_panel->get_requires_code();
msg.requires_code_to_arm = a_alarm_control_panel->get_requires_code_to_arm();
msg.unique_id = get_default_unique_id("alarm_control_panel", a_alarm_control_panel);
return this->try_send_entity_info_(static_cast<EntityBase *>(a_alarm_control_panel), msg,
&APIConnection::send_list_entities_alarm_control_panel_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(a_alarm_control_panel), msg);
}
void APIConnection::alarm_control_panel_command(const AlarmControlPanelCommandRequest &msg) {
alarm_control_panel::AlarmControlPanel *a_alarm_control_panel = App.get_alarm_control_panel_by_key(msg.key);
@@ -1416,7 +1396,7 @@ bool APIConnection::try_send_event_(event::Event *event, std::string event_type)
resp.event_type = std::move(event_type);
resp.key = event->get_object_id_hash();
return this->send_event_response(resp);
return this->send_message(resp);
}
bool APIConnection::try_send_event_info_(event::Event *event) {
ListEntitiesEventResponse msg;
@@ -1424,8 +1404,7 @@ bool APIConnection::try_send_event_info_(event::Event *event) {
for (const auto &event_type : event->get_event_types())
msg.event_types.push_back(event_type);
msg.unique_id = get_default_unique_id("event", event);
return this->try_send_entity_info_(static_cast<EntityBase *>(event), msg,
&APIConnection::send_list_entities_event_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(event), msg);
}
#endif
@@ -1455,14 +1434,13 @@ bool APIConnection::try_send_update_state_(update::UpdateEntity *update) {
}
resp.key = update->get_object_id_hash();
return this->send_update_state_response(resp);
return this->send_message(resp);
}
bool APIConnection::try_send_update_info_(update::UpdateEntity *update) {
ListEntitiesUpdateResponse msg;
msg.device_class = update->get_device_class();
msg.unique_id = get_default_unique_id("update", update);
return this->try_send_entity_info_(static_cast<EntityBase *>(update), msg,
&APIConnection::send_list_entities_update_response);
return this->try_send_entity_info_(static_cast<EntityBase *>(update), msg);
}
void APIConnection::update_command(const UpdateCommandRequest &msg) {
update::UpdateEntity *update = App.get_update_by_key(msg.key);