From 8a1f7b68024caddc96659fe812b99671683755b0 Mon Sep 17 00:00:00 2001 From: Jan Bouwhuis Date: Sun, 26 Nov 2023 17:33:54 +0100 Subject: [PATCH] Add translation key for some mqtt exceptions (#104550) --- homeassistant/components/mqtt/__init__.py | 6 +++--- homeassistant/components/mqtt/client.py | 23 +++++++++++++++------- homeassistant/components/mqtt/mixins.py | 4 ++-- homeassistant/components/mqtt/models.py | 8 ++++---- homeassistant/components/mqtt/strings.json | 8 +++++++- 5 files changed, 32 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index dd51b276715fb8..16f584db011b4c 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -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, @@ -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: diff --git a/homeassistant/components/mqtt/client.py b/homeassistant/components/mqtt/client.py index 2e4d49b4cd9f93..c87d4c9244a989 100644 --- a/homeassistant/components/mqtt/client.py +++ b/homeassistant/components/mqtt/client.py @@ -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 @@ -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( @@ -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( diff --git a/homeassistant/components/mqtt/mixins.py b/homeassistant/components/mqtt/mixins.py index d84f430bd8563d..76300afb97a82f 100644 --- a/homeassistant/components/mqtt/mixins.py +++ b/homeassistant/components/mqtt/mixins.py @@ -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))) diff --git a/homeassistant/components/mqtt/models.py b/homeassistant/components/mqtt/models.py index 2da2527ad7b2ff..63b8d537170f2d 100644 --- a/homeassistant/components/mqtt/models.py +++ b/homeassistant/components/mqtt/models.py @@ -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( diff --git a/homeassistant/components/mqtt/strings.json b/homeassistant/components/mqtt/strings.json index 7f8dcfedd9a427..f35cd7c2b58bdb 100644 --- a/homeassistant/components/mqtt/strings.json +++ b/homeassistant/components/mqtt/strings.json @@ -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." } } }