Correct SerialException import in dsmr, firmata, landysgyr_heat_meater and rfxtrx integrations (#104889)

* Fix SerialException import in dsmr integration

* Fix imports firmata, landysgyr_heat_meter, rfxtrx
This commit is contained in:
Jan Bouwhuis 2023-12-02 19:30:04 +01:00 committed by GitHub
parent 5106dd173c
commit c9306049b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 16 additions and 16 deletions

View file

@ -116,7 +116,7 @@ class DSMRConnection:
try: try:
transport, protocol = await asyncio.create_task(reader_factory()) transport, protocol = await asyncio.create_task(reader_factory())
except (serial.serialutil.SerialException, OSError): except (serial.SerialException, OSError):
LOGGER.exception("Error connecting to DSMR") LOGGER.exception("Error connecting to DSMR")
return False return False

View file

@ -648,7 +648,7 @@ async def async_setup_entry(
# throttle reconnect attempts # throttle reconnect attempts
await asyncio.sleep(DEFAULT_RECONNECT_INTERVAL) await asyncio.sleep(DEFAULT_RECONNECT_INTERVAL)
except (serial.serialutil.SerialException, OSError): except (serial.SerialException, OSError):
# Log any error while establishing connection and drop to retry # Log any error while establishing connection and drop to retry
# connection wait # connection wait
LOGGER.exception("Error connecting to DSMR") LOGGER.exception("Error connecting to DSMR")

View file

@ -65,12 +65,12 @@ class FirmataBoard:
except RuntimeError as err: except RuntimeError as err:
_LOGGER.error("Error connecting to PyMata board %s: %s", self.name, err) _LOGGER.error("Error connecting to PyMata board %s: %s", self.name, err)
return False return False
except serial.serialutil.SerialTimeoutException as err: except serial.SerialTimeoutException as err:
_LOGGER.error( _LOGGER.error(
"Timeout writing to serial port for PyMata board %s: %s", self.name, err "Timeout writing to serial port for PyMata board %s: %s", self.name, err
) )
return False return False
except serial.serialutil.SerialException as err: except serial.SerialException as err:
_LOGGER.error( _LOGGER.error(
"Error connecting to serial port for PyMata board %s: %s", "Error connecting to serial port for PyMata board %s: %s",
self.name, self.name,

View file

@ -41,12 +41,12 @@ class FirmataFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
except RuntimeError as err: except RuntimeError as err:
_LOGGER.error("Error connecting to PyMata board %s: %s", name, err) _LOGGER.error("Error connecting to PyMata board %s: %s", name, err)
return self.async_abort(reason="cannot_connect") return self.async_abort(reason="cannot_connect")
except serial.serialutil.SerialTimeoutException as err: except serial.SerialTimeoutException as err:
_LOGGER.error( _LOGGER.error(
"Timeout writing to serial port for PyMata board %s: %s", name, err "Timeout writing to serial port for PyMata board %s: %s", name, err
) )
return self.async_abort(reason="cannot_connect") return self.async_abort(reason="cannot_connect")
except serial.serialutil.SerialException as err: except serial.SerialException as err:
_LOGGER.error( _LOGGER.error(
"Error connecting to serial port for PyMata board %s: %s", name, err "Error connecting to serial port for PyMata board %s: %s", name, err
) )

View file

@ -108,7 +108,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
# validate and retrieve the model and device number for a unique id # validate and retrieve the model and device number for a unique id
data = await self.hass.async_add_executor_job(heat_meter.read) data = await self.hass.async_add_executor_job(heat_meter.read)
except (asyncio.TimeoutError, serial.serialutil.SerialException) as err: except (asyncio.TimeoutError, serial.SerialException) as err:
_LOGGER.warning("Failed read data from: %s. %s", port, err) _LOGGER.warning("Failed read data from: %s. %s", port, err)
raise CannotConnect(f"Error communicating with device: {err}") from err raise CannotConnect(f"Error communicating with device: {err}") from err

View file

@ -33,5 +33,5 @@ class UltraheatCoordinator(DataUpdateCoordinator[HeatMeterResponse]):
try: try:
async with asyncio.timeout(ULTRAHEAT_TIMEOUT): async with asyncio.timeout(ULTRAHEAT_TIMEOUT):
return await self.hass.async_add_executor_job(self.api.read) return await self.hass.async_add_executor_job(self.api.read)
except (FileNotFoundError, serial.serialutil.SerialException) as err: except (FileNotFoundError, serial.SerialException) as err:
raise UpdateFailed(f"Error communicating with API: {err}") from err raise UpdateFailed(f"Error communicating with API: {err}") from err

View file

@ -643,7 +643,7 @@ def _test_transport(host: str | None, port: int | None, device: str | None) -> b
else: else:
try: try:
conn = rfxtrxmod.PySerialTransport(device) conn = rfxtrxmod.PySerialTransport(device)
except serial.serialutil.SerialException: except serial.SerialException:
return False return False
if conn.serial is None: if conn.serial is None:

View file

@ -335,7 +335,7 @@ async def test_setup_serial_fail(
# override the mock to have it fail the first time and succeed after # override the mock to have it fail the first time and succeed after
first_fail_connection_factory = AsyncMock( first_fail_connection_factory = AsyncMock(
return_value=(transport, protocol), return_value=(transport, protocol),
side_effect=chain([serial.serialutil.SerialException], repeat(DEFAULT)), side_effect=chain([serial.SerialException], repeat(DEFAULT)),
) )
assert result["type"] == "form" assert result["type"] == "form"

View file

@ -31,7 +31,7 @@ async def test_import_cannot_connect_serial(hass: HomeAssistant) -> None:
with patch( with patch(
"homeassistant.components.firmata.board.PymataExpress.start_aio", "homeassistant.components.firmata.board.PymataExpress.start_aio",
side_effect=serial.serialutil.SerialException, side_effect=serial.SerialException,
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
@ -48,7 +48,7 @@ async def test_import_cannot_connect_serial_timeout(hass: HomeAssistant) -> None
with patch( with patch(
"homeassistant.components.firmata.board.PymataExpress.start_aio", "homeassistant.components.firmata.board.PymataExpress.start_aio",
side_effect=serial.serialutil.SerialTimeoutException, side_effect=serial.SerialTimeoutException,
): ):
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,

View file

@ -104,7 +104,7 @@ async def test_list_entry(mock_port, mock_heat_meter, hass: HomeAssistant) -> No
async def test_manual_entry_fail(mock_heat_meter, hass: HomeAssistant) -> None: async def test_manual_entry_fail(mock_heat_meter, hass: HomeAssistant) -> None:
"""Test manual entry fails.""" """Test manual entry fails."""
mock_heat_meter().read.side_effect = serial.serialutil.SerialException mock_heat_meter().read.side_effect = serial.SerialException
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -135,7 +135,7 @@ async def test_manual_entry_fail(mock_heat_meter, hass: HomeAssistant) -> None:
async def test_list_entry_fail(mock_port, mock_heat_meter, hass: HomeAssistant) -> None: async def test_list_entry_fail(mock_port, mock_heat_meter, hass: HomeAssistant) -> None:
"""Test select from list entry fails.""" """Test select from list entry fails."""
mock_heat_meter().read.side_effect = serial.serialutil.SerialException mock_heat_meter().read.side_effect = serial.SerialException
port = mock_serial_port() port = mock_serial_port()
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(

View file

@ -150,7 +150,7 @@ async def test_exception_on_polling(
assert state.state == "123.0" assert state.state == "123.0"
# Now 'disable' the connection and wait for polling and see if it fails # Now 'disable' the connection and wait for polling and see if it fails
mock_heat_meter().read.side_effect = serial.serialutil.SerialException mock_heat_meter().read.side_effect = serial.SerialException
freezer.tick(POLLING_INTERVAL) freezer.tick(POLLING_INTERVAL)
async_fire_time_changed(hass) async_fire_time_changed(hass)
await hass.async_block_till_done() await hass.async_block_till_done()

View file

@ -216,7 +216,7 @@ async def test_setup_network_fail(transport_mock, hass: HomeAssistant) -> None:
@patch("serial.tools.list_ports.comports", return_value=[com_port()]) @patch("serial.tools.list_ports.comports", return_value=[com_port()])
@patch( @patch(
"homeassistant.components.rfxtrx.rfxtrxmod.PySerialTransport.connect", "homeassistant.components.rfxtrx.rfxtrxmod.PySerialTransport.connect",
side_effect=serial.serialutil.SerialException, side_effect=serial.SerialException,
) )
async def test_setup_serial_fail(com_mock, connect_mock, hass: HomeAssistant) -> None: async def test_setup_serial_fail(com_mock, connect_mock, hass: HomeAssistant) -> None:
"""Test setup serial failed connection.""" """Test setup serial failed connection."""