-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Correctly allow mqtt topics to be none so ESPHome does not sub/pub to them #5213
Conversation
This PR seems like a more natural solution than #5195 for disabling MQTT for particular entities, especially since it doesn't require changes to the core. One suggestion is to add a default option. One possible way is if the base MQTT configuration has However, this PR doesn't handle the other case where the entity should only publish via MQTT and not via the API. Perhaps the MQTT component's Here is how I use that case: I use MQTT to collect long-term statistics in a separate database independent of HomeAssistant. I use the data sent to HA for quick informational purposes/minor automations that do not require frequent updates. I have configured it so that the entities update over the API much less frequently than entities updating via MQTT. I would prefer not to expose the large number of more frequently updated MQTT entities to HA, even if they are If people like these ideas (or some variation), I can implement them after this PR is merged if you don't want to add them yourself. |
Hi @kahrendt thanks for the feedback
Yes this would be fine. This PR will actually allow it to become blank ie:
I think this would be an acceptable change |
I was able to test this out today, and it works as intended with Edit: After further testing, it seems like it is attempting to repeatedly send the discovery message every loop when the topic is set to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you modify the is_internal()
function as follows, it fixes the issues I mentioned before. It won't send a discovery message if the state_topic or command_topic is null. It also allows the component to be internal with regard to the API, but it will still publish to custom topics if they are explicitly defined. This approach seems like the easiest solution to do all these things, but there may be a better way!
bool MQTTComponent::is_internal() {
if ((this->get_state_topic_().empty()) && (this->get_command_topic_().empty())) {
// If both state_topic and command_topic are empty, then the entity is internal to mqtt
return true;
}
if (this->has_custom_state_topic_ || this->has_custom_command_topic_) {
// If a custom state_topic or command_topic is set, then the entity is not internal to mqtt
return false;
}
// Use ESPHome's entity internal state
return this->get_entity()->is_internal();
}
I dont use mqtt at all, so relying on your testing for this =) |
What does this implement/fix?
This allows disabling of sensor publishing via mqtt while not forcing that sensor to be
internal
(if the user is using both api and mqtt in config).Replaces and closes #5195
Types of changes
Related issue or feature (if applicable): fixes esphome/feature-requests#375
Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#
Test Environment
Example entry for
config.yaml
:Checklist:
tests/
folder).If user exposed functionality or configuration variables are added/changed: