Skip to content

Commit

Permalink
Merge pull request #567 from canton7/feature/issue_registry
Browse files Browse the repository at this point in the history
Use the issue registry to report connection errors
  • Loading branch information
canton7 authored Mar 17, 2024
2 parents 1133804 + 558c42a commit 78825e8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
21 changes: 21 additions & 0 deletions custom_components/foxess_modbus/modbus_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

from homeassistant.components.logbook import async_log_entry
from homeassistant.core import HomeAssistant
from homeassistant.helpers import issue_registry
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.helpers.issue_registry import IssueSeverity
from pymodbus.exceptions import ConnectionException

from .client.modbus_client import ModbusClient
Expand All @@ -29,6 +31,7 @@
from .common.types import RegisterType
from .common.unload_controller import UnloadController
from .const import DOMAIN
from .const import ENTITY_ID_PREFIX
from .const import FRIENDLY_NAME
from .const import INVERTER_MODEL
from .const import MAX_READ
Expand Down Expand Up @@ -316,6 +319,11 @@ async def _refresh(self, _time: datetime) -> None:
self._connection_state = ConnectionState.CONNECTED
self._current_connection_error = None
self._log_message("Connection restored")
issue_registry.async_delete_issue(
self._hass,
domain=DOMAIN,
issue_id=f"connection_error_{self.inverter_details[ENTITY_ID_PREFIX]}",
)
await self._notify_is_connected_changed(is_connected=True)
elif self._connection_state != ConnectionState.DISCONNECTED:
self._num_failed_poll_attempts += 1
Expand All @@ -330,6 +338,19 @@ async def _refresh(self, _time: datetime) -> None:
self._connection_state = ConnectionState.DISCONNECTED
self._current_connection_error = str(exception)
self._log_message(f"Connection error: {exception}")
issue_registry.async_create_issue(
self._hass,
domain=DOMAIN,
issue_id=f"connection_error_{self.inverter_details[ENTITY_ID_PREFIX]}",
is_fixable=False,
is_persistent=False,
severity=IssueSeverity.ERROR,
translation_key="connection_error",
translation_placeholders={
"friendly_name": self.inverter_details[FRIENDLY_NAME],
"error": str(exception),
},
)
await self._notify_is_connected_changed(is_connected=False)

if self._remote_control_manager is not None:
Expand Down
6 changes: 6 additions & 0 deletions custom_components/foxess_modbus/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,5 +189,11 @@
"network_other": "Other"
}
}
},
"issues": {
"connection_error": {
"title": "Connection Error",
"description": "Error connecting to inverter (friendly name: '{friendly_name}'). {error}"
}
}
}

0 comments on commit 78825e8

Please sign in to comment.