mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 13:57:10 +00:00
Increase timeouts in Minecraft Server (#101784)
This commit is contained in:
parent
c7d2499a52
commit
54bcd70878
@ -39,7 +39,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
)
|
)
|
||||||
except MinecraftServerAddressError as error:
|
except MinecraftServerAddressError as error:
|
||||||
raise ConfigEntryError(
|
raise ConfigEntryError(
|
||||||
f"Server address in configuration entry is invalid (error: {error})"
|
f"Server address in configuration entry is invalid: {error}"
|
||||||
) from error
|
) from error
|
||||||
|
|
||||||
# Create coordinator instance.
|
# Create coordinator instance.
|
||||||
@ -109,7 +109,7 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
|
|||||||
except MinecraftServerAddressError as error:
|
except MinecraftServerAddressError as error:
|
||||||
host_only_lookup_success = False
|
host_only_lookup_success = False
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
"Hostname (without port) cannot be parsed (error: %s), trying again with port",
|
"Hostname (without port) cannot be parsed, trying again with port: %s",
|
||||||
error,
|
error,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -119,7 +119,7 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
|
|||||||
MinecraftServer(MinecraftServerType.JAVA_EDITION, address)
|
MinecraftServer(MinecraftServerType.JAVA_EDITION, address)
|
||||||
except MinecraftServerAddressError as error:
|
except MinecraftServerAddressError as error:
|
||||||
_LOGGER.exception(
|
_LOGGER.exception(
|
||||||
"Can't migrate configuration entry due to error while parsing server address (error: %s), try again later",
|
"Can't migrate configuration entry due to error while parsing server address, try again later: %s",
|
||||||
error,
|
error,
|
||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
|
@ -11,6 +11,10 @@ from mcstatus.status_response import BedrockStatusResponse, JavaStatusResponse
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
LOOKUP_TIMEOUT: float = 10
|
||||||
|
DATA_UPDATE_TIMEOUT: float = 10
|
||||||
|
DATA_UPDATE_RETRIES: int = 3
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class MinecraftServerData:
|
class MinecraftServerData:
|
||||||
@ -57,14 +61,17 @@ class MinecraftServer:
|
|||||||
"""Initialize server instance."""
|
"""Initialize server instance."""
|
||||||
try:
|
try:
|
||||||
if server_type == MinecraftServerType.JAVA_EDITION:
|
if server_type == MinecraftServerType.JAVA_EDITION:
|
||||||
self._server = JavaServer.lookup(address)
|
self._server = JavaServer.lookup(address, timeout=LOOKUP_TIMEOUT)
|
||||||
else:
|
else:
|
||||||
self._server = BedrockServer.lookup(address)
|
self._server = BedrockServer.lookup(address, timeout=LOOKUP_TIMEOUT)
|
||||||
except (ValueError, LifetimeTimeout) as error:
|
except (ValueError, LifetimeTimeout) as error:
|
||||||
raise MinecraftServerAddressError(
|
raise MinecraftServerAddressError(
|
||||||
f"{server_type} server address '{address}' is invalid (error: {error})"
|
f"Lookup of '{address}' failed: {self._get_error_message(error)}"
|
||||||
) from error
|
) from error
|
||||||
|
|
||||||
|
self._server.timeout = DATA_UPDATE_TIMEOUT
|
||||||
|
self._address = address
|
||||||
|
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
"%s server instance created with address '%s'", server_type, address
|
"%s server instance created with address '%s'", server_type, address
|
||||||
)
|
)
|
||||||
@ -83,10 +90,10 @@ class MinecraftServer:
|
|||||||
status_response: BedrockStatusResponse | JavaStatusResponse
|
status_response: BedrockStatusResponse | JavaStatusResponse
|
||||||
|
|
||||||
try:
|
try:
|
||||||
status_response = await self._server.async_status()
|
status_response = await self._server.async_status(tries=DATA_UPDATE_RETRIES)
|
||||||
except OSError as error:
|
except OSError as error:
|
||||||
raise MinecraftServerConnectionError(
|
raise MinecraftServerConnectionError(
|
||||||
f"Fetching data from the server failed (error: {error})"
|
f"Status request to '{self._address}' failed: {self._get_error_message(error)}"
|
||||||
) from error
|
) from error
|
||||||
|
|
||||||
if isinstance(status_response, JavaStatusResponse):
|
if isinstance(status_response, JavaStatusResponse):
|
||||||
@ -132,3 +139,11 @@ class MinecraftServer:
|
|||||||
game_mode=status_response.gamemode,
|
game_mode=status_response.gamemode,
|
||||||
map_name=status_response.map_name,
|
map_name=status_response.map_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _get_error_message(self, error: BaseException) -> str:
|
||||||
|
"""Get error message of an exception."""
|
||||||
|
if not str(error):
|
||||||
|
# Fallback to error type in case of an empty error message.
|
||||||
|
return repr(error)
|
||||||
|
|
||||||
|
return str(error)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user