Skip to content

Commit

Permalink
Add translation key for some mqtt exceptions (home-assistant#104550)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouwh authored Nov 26, 2023
1 parent b314df2 commit 8a1f7b6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
6 changes: 3 additions & 3 deletions homeassistant/components/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,11 @@ async def async_check_config_schema(
for config in config_items:
try:
schema(config)
except vol.Invalid as ex:
except vol.Invalid as exc:
integration = await async_get_integration(hass, DOMAIN)
# pylint: disable-next=protected-access
message = conf_util.format_schema_error(
hass, ex, domain, config, integration.documentation
hass, exc, domain, config, integration.documentation
)
raise ServiceValidationError(
message,
Expand All @@ -258,7 +258,7 @@ async def async_check_config_schema(
translation_placeholders={
"domain": domain,
},
) from ex
) from exc


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
Expand Down
23 changes: 16 additions & 7 deletions homeassistant/components/mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ async def async_publish(
"""Publish message to a MQTT topic."""
if not mqtt_config_entry_enabled(hass):
raise HomeAssistantError(
f"Cannot publish to topic '{topic}', MQTT is not enabled"
f"Cannot publish to topic '{topic}', MQTT is not enabled",
translation_key="mqtt_not_setup_cannot_publish",
translation_domain=DOMAIN,
translation_placeholders={"topic": topic},
)
mqtt_data = get_mqtt_data(hass)
outgoing_payload = payload
Expand Down Expand Up @@ -174,15 +177,21 @@ async def async_subscribe(
"""
if not mqtt_config_entry_enabled(hass):
raise HomeAssistantError(
f"Cannot subscribe to topic '{topic}', MQTT is not enabled"
f"Cannot subscribe to topic '{topic}', MQTT is not enabled",
translation_key="mqtt_not_setup_cannot_subscribe",
translation_domain=DOMAIN,
translation_placeholders={"topic": topic},
)
try:
mqtt_data = get_mqtt_data(hass)
except KeyError as ex:
except KeyError as exc:
raise HomeAssistantError(
f"Cannot subscribe to topic '{topic}', "
"make sure MQTT is set up correctly"
) from ex
"make sure MQTT is set up correctly",
translation_key="mqtt_not_setup_cannot_subscribe",
translation_domain=DOMAIN,
translation_placeholders={"topic": topic},
) from exc
async_remove = await mqtt_data.client.async_subscribe(
topic,
catch_log_exception(
Expand Down Expand Up @@ -606,8 +615,8 @@ def _async_untrack_subscription(self, subscription: Subscription) -> None:
del simple_subscriptions[topic]
else:
self._wildcard_subscriptions.remove(subscription)
except (KeyError, ValueError) as ex:
raise HomeAssistantError("Can't remove subscription twice") from ex
except (KeyError, ValueError) as exc:
raise HomeAssistantError("Can't remove subscription twice") from exc

@callback
def _async_queue_subscriptions(
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/mqtt/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,8 @@ def _async_setup_entities() -> None:
if TYPE_CHECKING:
assert entity_class is not None
entities.append(entity_class(hass, config, entry, None))
except vol.Invalid as ex:
error = str(ex)
except vol.Invalid as exc:
error = str(exc)
config_file = getattr(yaml_config, "__config_file__", "?")
line = getattr(yaml_config, "__line__", "?")
issue_id = hex(hash(frozenset(yaml_config)))
Expand Down
8 changes: 4 additions & 4 deletions homeassistant/components/mqtt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,15 @@ def async_render_with_possible_json_value(
payload, variables=values
)
)
except Exception as ex:
except Exception as exc:
_LOGGER.error(
"%s: %s rendering template for entity '%s', template: '%s'",
type(ex).__name__,
ex,
type(exc).__name__,
exc,
self._entity.entity_id if self._entity else "n/a",
self._value_template.template,
)
raise ex
raise exc
return rendered_payload

_LOGGER.debug(
Expand Down
8 changes: 7 additions & 1 deletion homeassistant/components/mqtt/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,13 @@
},
"exceptions": {
"invalid_platform_config": {
"message": "Reloading YAML config for manually configured MQTT `{domain}` failed. See logs for more details."
"message": "Reloading YAML config for manually configured MQTT `{domain}` item failed. See logs for more details."
},
"mqtt_not_setup_cannot_subscribe": {
"message": "Cannot subscribe to topic '{topic}', make sure MQTT is set up correctly."
},
"mqtt_not_setup_cannot_publish": {
"message": "Cannot publish to topic '{topic}', make sure MQTT is set up correctly."
}
}
}

0 comments on commit 8a1f7b6

Please sign in to comment.