From cb563f25fae4a93ae78e199a53a60a418a7fc68d Mon Sep 17 00:00:00 2001 From: Erwin Douna Date: Fri, 21 Jun 2024 18:52:39 +0200 Subject: [PATCH] Add DSMR MQTT subscribe error handling (#118316) Add eror handling --- .../components/dsmr_reader/sensor.py | 23 ++++++++++++++++--- .../components/dsmr_reader/strings.json | 6 +++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/dsmr_reader/sensor.py b/homeassistant/components/dsmr_reader/sensor.py index 3c07ad65de6..784a4cdec51 100644 --- a/homeassistant/components/dsmr_reader/sensor.py +++ b/homeassistant/components/dsmr_reader/sensor.py @@ -6,9 +6,12 @@ from homeassistant.components import mqtt from homeassistant.components.sensor import SensorEntity from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue from homeassistant.util import slugify +from .const import DOMAIN from .definitions import SENSORS, DSMRReaderSensorEntityDescription @@ -53,6 +56,20 @@ class DSMRSensor(SensorEntity): self.async_write_ha_state() - await mqtt.async_subscribe( - self.hass, self.entity_description.key, message_received, 1 - ) + try: + await mqtt.async_subscribe( + self.hass, self.entity_description.key, message_received, 1 + ) + except HomeAssistantError: + async_create_issue( + self.hass, + DOMAIN, + f"cannot_subscribe_mqtt_topic_{self.entity_description.key}", + is_fixable=False, + severity=IssueSeverity.WARNING, + translation_key="cannot_subscribe_mqtt_topic", + translation_placeholders={ + "topic": self.entity_description.key, + "topic_title": self.entity_description.key.split("/")[-1], + }, + ) diff --git a/homeassistant/components/dsmr_reader/strings.json b/homeassistant/components/dsmr_reader/strings.json index fce274e8917..90cf0533a72 100644 --- a/homeassistant/components/dsmr_reader/strings.json +++ b/homeassistant/components/dsmr_reader/strings.json @@ -259,5 +259,11 @@ "name": "Quarter-hour peak end time" } } + }, + "issues": { + "cannot_subscribe_mqtt_topic": { + "title": "Cannot subscribe to MQTT topic {topic_title}", + "description": "The DSMR Reader integration cannot subscribe to the MQTT topic: `{topic}`. Please check the configuration of the MQTT broker and the topic.\nDSMR Reader needs to be running, before starting this integration." + } } }