Skip to content

Commit 0804314

Browse files
authored
fix: handle connection errors with common exception (#201)
related to #196
1 parent 55c7933 commit 0804314

3 files changed

Lines changed: 8 additions & 6 deletions

File tree

custom_components/solakon_one/config_flow.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SCAN_INTERVAL, UnitOfTime
1212
from homeassistant.core import HomeAssistant, callback
1313
from homeassistant.data_entry_flow import FlowResult
14-
from homeassistant.exceptions import HomeAssistantError
1514
from homeassistant.helpers import config_validation as cv, selector
1615

1716
from .const import (
@@ -22,6 +21,7 @@
2221
DEFAULT_SCAN_INTERVAL,
2322
DOMAIN,
2423
)
24+
from .exceptions import CannotConnect
2525
from .modbus import get_modbus_hub
2626

2727
_LOGGER = logging.getLogger(__name__)
@@ -152,7 +152,3 @@ async def async_step_init(
152152
self._config_entry.options or self._config_entry.data,
153153
),
154154
)
155-
156-
157-
class CannotConnect(HomeAssistantError):
158-
"""Error to indicate we cannot connect."""
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from homeassistant.exceptions import HomeAssistantError
2+
3+
4+
class CannotConnect(HomeAssistantError):
5+
"""Error to indicate we cannot connect."""

custom_components/solakon_one/modbus.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
DEFAULT_SCAN_INTERVAL,
2222
REGISTERS,
2323
)
24+
from .exceptions import CannotConnect
2425

2526
_LOGGER = logging.getLogger(__name__)
2627

@@ -95,7 +96,7 @@ async def async_setup(self) -> None:
9596
_LOGGER.warning(f"Test read exception: {e}")
9697
else:
9798
_LOGGER.error(f"Failed to connect to {self._host}:{self._port}")
98-
raise ConnectionError(f"Failed to connect to {self._host}:{self._port}")
99+
raise CannotConnect(f"Failed to connect to {self._host}:{self._port}")
99100

100101
except Exception as err:
101102
_LOGGER.error(f"Connection setup error: {err}")

0 commit comments

Comments
 (0)