Run socket.gethostbyname in executor in Obihai and Sonos (#91190)

* Run  in executor in Obihai and Sonos

* fix Sonos test

* fix sonos test differently (review)
This commit is contained in:
Matthias Alphart 2023-04-12 09:58:27 +02:00 committed by GitHub
parent bb15923968
commit 0ba339e56c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 18 deletions

View File

@ -66,7 +66,9 @@ class ObihaiFlowHandler(ConfigFlow, domain=DOMAIN):
if user_input is not None: if user_input is not None:
try: try:
ip = gethostbyname(user_input[CONF_HOST]) ip = await self.hass.async_add_executor_job(
gethostbyname, user_input[CONF_HOST]
)
except gaierror: except gaierror:
errors["base"] = "cannot_connect" errors["base"] = "cannot_connect"
@ -139,7 +141,7 @@ class ObihaiFlowHandler(ConfigFlow, domain=DOMAIN):
"""Handle a flow initialized by importing a config.""" """Handle a flow initialized by importing a config."""
try: try:
_ = gethostbyname(config[CONF_HOST]) _ = await self.hass.async_add_executor_job(gethostbyname, config[CONF_HOST])
except gaierror: except gaierror:
return self.async_abort(reason="cannot_connect") return self.async_abort(reason="cannot_connect")

View File

@ -48,6 +48,7 @@ from .const import (
) )
from .exception import SonosUpdateError from .exception import SonosUpdateError
from .favorites import SonosFavorites from .favorites import SonosFavorites
from .helpers import sync_get_visible_zones
from .speaker import SonosSpeaker from .speaker import SonosSpeaker
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -338,19 +339,12 @@ class SonosDiscoveryManager:
self, now: datetime.datetime | None = None self, now: datetime.datetime | None = None
) -> None: ) -> None:
"""Add and maintain Sonos devices from a manual configuration.""" """Add and maintain Sonos devices from a manual configuration."""
def get_sync_attributes(soco: SoCo) -> set[SoCo]:
"""Ensure I/O attributes are cached and return visible zones."""
_ = soco.household_id
_ = soco.uid
return soco.visible_zones
for host in self.hosts: for host in self.hosts:
ip_addr = socket.gethostbyname(host) ip_addr = await self.hass.async_add_executor_job(socket.gethostbyname, host)
soco = SoCo(ip_addr) soco = SoCo(ip_addr)
try: try:
visible_zones = await self.hass.async_add_executor_job( visible_zones = await self.hass.async_add_executor_job(
get_sync_attributes, sync_get_visible_zones,
soco, soco,
) )
except (OSError, SoCoException, Timeout) as ex: except (OSError, SoCoException, Timeout) as ex:
@ -382,7 +376,7 @@ class SonosDiscoveryManager:
break break
for host in self.hosts.copy(): for host in self.hosts.copy():
ip_addr = socket.gethostbyname(host) ip_addr = await self.hass.async_add_executor_job(socket.gethostbyname, host)
if self.is_device_invisible(ip_addr): if self.is_device_invisible(ip_addr):
_LOGGER.debug("Discarding %s from manual hosts", ip_addr) _LOGGER.debug("Discarding %s from manual hosts", ip_addr)
self.hosts.discard(ip_addr) self.hosts.discard(ip_addr)

View File

@ -117,3 +117,10 @@ def hostname_to_uid(hostname: str) -> str:
else: else:
raise ValueError(f"{hostname} is not a sonos device.") raise ValueError(f"{hostname} is not a sonos device.")
return f"{UID_PREFIX}{baseuid}{UID_POSTFIX}" return f"{UID_PREFIX}{baseuid}{UID_POSTFIX}"
def sync_get_visible_zones(soco: SoCo) -> set[SoCo]:
"""Ensure I/O attributes are cached and return visible zones."""
_ = soco.household_id
_ = soco.uid
return soco.visible_zones

View File

@ -1,6 +1,6 @@
"""Tests for the Sonos config flow.""" """Tests for the Sonos config flow."""
import logging import logging
from unittest.mock import AsyncMock, patch from unittest.mock import patch
import pytest import pytest
@ -86,16 +86,16 @@ async def test_async_poll_manual_hosts_warnings(
manager, "_async_handle_discovery_message" manager, "_async_handle_discovery_message"
), patch("homeassistant.components.sonos.async_call_later"), patch( ), patch("homeassistant.components.sonos.async_call_later"), patch(
"homeassistant.components.sonos.async_dispatcher_send" "homeassistant.components.sonos.async_dispatcher_send"
), patch.object( ), patch(
hass, "async_add_executor_job", new=AsyncMock() "homeassistant.components.sonos.sync_get_visible_zones",
) as mock_async_add_executor_job: side_effect=[
mock_async_add_executor_job.side_effect = [
OSError(), OSError(),
OSError(), OSError(),
[], [],
[], [],
OSError(), OSError(),
] ],
):
# First call fails, it should be logged as a WARNING message # First call fails, it should be logged as a WARNING message
caplog.clear() caplog.clear()
await manager.async_poll_manual_hosts() await manager.async_poll_manual_hosts()