mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 01:08:12 +00:00
Add Pterodactyl integration (#141197)
* Add Pterodactyl integration * Remove translation for unavailable platform sensor, use constant for host * Improve data descriptions * Replace index based handling of data (list) with dict[str, PterodactylData] * Replace CONF_HOST with CONF_URL * Parse URL with YARL * Set proper availability in binary sensor * Remove storage of data within api.py * Fix some review findings * Use better unique ID for binary_sensor * Fix more review findings * Fix remaining review findings * Add wrapper for server and util API, use underscore in unique ID * Reuse result in config flow tests * Patch async_setup_entry in config_flow tests * Move patching of library APIs to the fixture mock_pterodactyl
This commit is contained in:
parent
4cd4201a31
commit
e853df4fb0
2
CODEOWNERS
generated
2
CODEOWNERS
generated
@ -1183,6 +1183,8 @@ build.json @home-assistant/supervisor
|
||||
/tests/components/prusalink/ @balloob
|
||||
/homeassistant/components/ps4/ @ktnrg45
|
||||
/tests/components/ps4/ @ktnrg45
|
||||
/homeassistant/components/pterodactyl/ @elmurato
|
||||
/tests/components/pterodactyl/ @elmurato
|
||||
/homeassistant/components/pure_energie/ @klaasnicolaas
|
||||
/tests/components/pure_energie/ @klaasnicolaas
|
||||
/homeassistant/components/purpleair/ @bachya
|
||||
|
27
homeassistant/components/pterodactyl/__init__.py
Normal file
27
homeassistant/components/pterodactyl/__init__.py
Normal file
@ -0,0 +1,27 @@
|
||||
"""The Pterodactyl integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .coordinator import PterodactylConfigEntry, PterodactylCoordinator
|
||||
|
||||
_PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: PterodactylConfigEntry) -> bool:
|
||||
"""Set up Pterodactyl from a config entry."""
|
||||
coordinator = PterodactylCoordinator(hass, entry)
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
entry.runtime_data = coordinator
|
||||
await hass.config_entries.async_forward_entry_setups(entry, _PLATFORMS)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(
|
||||
hass: HomeAssistant, entry: PterodactylConfigEntry
|
||||
) -> bool:
|
||||
"""Unload a Pterodactyl config entry."""
|
||||
return await hass.config_entries.async_unload_platforms(entry, _PLATFORMS)
|
120
homeassistant/components/pterodactyl/api.py
Normal file
120
homeassistant/components/pterodactyl/api.py
Normal file
@ -0,0 +1,120 @@
|
||||
"""API module of the Pterodactyl integration."""
|
||||
|
||||
from dataclasses import dataclass
|
||||
import logging
|
||||
|
||||
from pydactyl import PterodactylClient
|
||||
from pydactyl.exceptions import (
|
||||
BadRequestError,
|
||||
ClientConfigError,
|
||||
PterodactylApiError,
|
||||
PydactylError,
|
||||
)
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PterodactylConfigurationError(Exception):
|
||||
"""Raised when the configuration is invalid."""
|
||||
|
||||
|
||||
class PterodactylConnectionError(Exception):
|
||||
"""Raised when no data can be fechted from the server."""
|
||||
|
||||
|
||||
@dataclass
|
||||
class PterodactylData:
|
||||
"""Data for the Pterodactyl server."""
|
||||
|
||||
name: str
|
||||
uuid: str
|
||||
identifier: str
|
||||
state: str
|
||||
memory_utilization: int
|
||||
cpu_utilization: float
|
||||
disk_utilization: int
|
||||
network_rx_utilization: int
|
||||
network_tx_utilization: int
|
||||
uptime: int
|
||||
|
||||
|
||||
class PterodactylAPI:
|
||||
"""Wrapper for Pterodactyl's API."""
|
||||
|
||||
pterodactyl: PterodactylClient | None
|
||||
identifiers: list[str]
|
||||
|
||||
def __init__(self, hass: HomeAssistant, host: str, api_key: str) -> None:
|
||||
"""Initialize the Pterodactyl API."""
|
||||
self.hass = hass
|
||||
self.host = host
|
||||
self.api_key = api_key
|
||||
self.pterodactyl = None
|
||||
self.identifiers = []
|
||||
|
||||
async def async_init(self):
|
||||
"""Initialize the Pterodactyl API."""
|
||||
self.pterodactyl = PterodactylClient(self.host, self.api_key)
|
||||
|
||||
try:
|
||||
paginated_response = await self.hass.async_add_executor_job(
|
||||
self.pterodactyl.client.servers.list_servers
|
||||
)
|
||||
except ClientConfigError as error:
|
||||
raise PterodactylConfigurationError(error) from error
|
||||
except (
|
||||
PydactylError,
|
||||
BadRequestError,
|
||||
PterodactylApiError,
|
||||
) as error:
|
||||
raise PterodactylConnectionError(error) from error
|
||||
else:
|
||||
game_servers = paginated_response.collect()
|
||||
for game_server in game_servers:
|
||||
self.identifiers.append(game_server["attributes"]["identifier"])
|
||||
|
||||
_LOGGER.debug("Identifiers of Pterodactyl servers: %s", self.identifiers)
|
||||
|
||||
def get_server_data(self, identifier: str) -> tuple[dict, dict]:
|
||||
"""Get all data from the Pterodactyl server."""
|
||||
server = self.pterodactyl.client.servers.get_server(identifier) # type: ignore[union-attr]
|
||||
utilization = self.pterodactyl.client.servers.get_server_utilization( # type: ignore[union-attr]
|
||||
identifier
|
||||
)
|
||||
|
||||
return server, utilization
|
||||
|
||||
async def async_get_data(self) -> dict[str, PterodactylData]:
|
||||
"""Update the data from all Pterodactyl servers."""
|
||||
data = {}
|
||||
|
||||
for identifier in self.identifiers:
|
||||
try:
|
||||
server, utilization = await self.hass.async_add_executor_job(
|
||||
self.get_server_data, identifier
|
||||
)
|
||||
except (
|
||||
PydactylError,
|
||||
BadRequestError,
|
||||
PterodactylApiError,
|
||||
) as error:
|
||||
raise PterodactylConnectionError(error) from error
|
||||
else:
|
||||
data[identifier] = PterodactylData(
|
||||
name=server["name"],
|
||||
uuid=server["uuid"],
|
||||
identifier=identifier,
|
||||
state=utilization["current_state"],
|
||||
cpu_utilization=utilization["resources"]["cpu_absolute"],
|
||||
memory_utilization=utilization["resources"]["memory_bytes"],
|
||||
disk_utilization=utilization["resources"]["disk_bytes"],
|
||||
network_rx_utilization=utilization["resources"]["network_rx_bytes"],
|
||||
network_tx_utilization=utilization["resources"]["network_tx_bytes"],
|
||||
uptime=utilization["resources"]["uptime"],
|
||||
)
|
||||
|
||||
_LOGGER.debug("%s", data[identifier])
|
||||
|
||||
return data
|
64
homeassistant/components/pterodactyl/binary_sensor.py
Normal file
64
homeassistant/components/pterodactyl/binary_sensor.py
Normal file
@ -0,0 +1,64 @@
|
||||
"""Binary sensor platform of the Pterodactyl integration."""
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorDeviceClass,
|
||||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .coordinator import PterodactylConfigEntry, PterodactylCoordinator
|
||||
from .entity import PterodactylEntity
|
||||
|
||||
KEY_STATUS = "status"
|
||||
|
||||
|
||||
BINARY_SENSOR_DESCRIPTIONS = [
|
||||
BinarySensorEntityDescription(
|
||||
key=KEY_STATUS,
|
||||
translation_key=KEY_STATUS,
|
||||
device_class=BinarySensorDeviceClass.RUNNING,
|
||||
),
|
||||
]
|
||||
|
||||
# Coordinator is used to centralize the data updates.
|
||||
PARALLEL_UPDATES = 0
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: PterodactylConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the Pterodactyl binary sensor platform."""
|
||||
coordinator = config_entry.runtime_data
|
||||
|
||||
async_add_entities(
|
||||
PterodactylBinarySensorEntity(
|
||||
coordinator, identifier, description, config_entry
|
||||
)
|
||||
for identifier in coordinator.api.identifiers
|
||||
for description in BINARY_SENSOR_DESCRIPTIONS
|
||||
)
|
||||
|
||||
|
||||
class PterodactylBinarySensorEntity(PterodactylEntity, BinarySensorEntity):
|
||||
"""Representation of a Pterodactyl binary sensor base entity."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: PterodactylCoordinator,
|
||||
identifier: str,
|
||||
description: BinarySensorEntityDescription,
|
||||
config_entry: PterodactylConfigEntry,
|
||||
) -> None:
|
||||
"""Initialize binary sensor base entity."""
|
||||
super().__init__(coordinator, identifier, config_entry)
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = f"{self.game_server_data.uuid}_{description.key}"
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return binary sensor state."""
|
||||
return self.game_server_data.state == "running"
|
62
homeassistant/components/pterodactyl/config_flow.py
Normal file
62
homeassistant/components/pterodactyl/config_flow.py
Normal file
@ -0,0 +1,62 @@
|
||||
"""Config flow for the Pterodactyl integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import voluptuous as vol
|
||||
from yarl import URL
|
||||
|
||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
||||
from homeassistant.const import CONF_API_KEY, CONF_URL
|
||||
|
||||
from .api import (
|
||||
PterodactylAPI,
|
||||
PterodactylConfigurationError,
|
||||
PterodactylConnectionError,
|
||||
)
|
||||
from .const import DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_URL = "http://localhost:8080"
|
||||
|
||||
STEP_USER_DATA_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_URL, default=DEFAULT_URL): str,
|
||||
vol.Required(CONF_API_KEY): str,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class PterodactylConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
"""Handle a config flow for Pterodactyl."""
|
||||
|
||||
VERSION = 1
|
||||
|
||||
async def async_step_user(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> ConfigFlowResult:
|
||||
"""Handle the initial step."""
|
||||
errors: dict[str, str] = {}
|
||||
if user_input is not None:
|
||||
url = URL(user_input[CONF_URL]).human_repr()
|
||||
api_key = user_input[CONF_API_KEY]
|
||||
|
||||
self._async_abort_entries_match({CONF_URL: url})
|
||||
api = PterodactylAPI(self.hass, url, api_key)
|
||||
|
||||
try:
|
||||
await api.async_init()
|
||||
except (PterodactylConfigurationError, PterodactylConnectionError):
|
||||
errors["base"] = "cannot_connect"
|
||||
except Exception:
|
||||
_LOGGER.exception("Unexpected exception occurred during config flow")
|
||||
errors["base"] = "unknown"
|
||||
else:
|
||||
return self.async_create_entry(title=url, data=user_input)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
|
||||
)
|
3
homeassistant/components/pterodactyl/const.py
Normal file
3
homeassistant/components/pterodactyl/const.py
Normal file
@ -0,0 +1,3 @@
|
||||
"""Constants for the Pterodactyl integration."""
|
||||
|
||||
DOMAIN = "pterodactyl"
|
66
homeassistant/components/pterodactyl/coordinator.py
Normal file
66
homeassistant/components/pterodactyl/coordinator.py
Normal file
@ -0,0 +1,66 @@
|
||||
"""Data update coordinator of the Pterodactyl integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_API_KEY, CONF_URL
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
from .api import (
|
||||
PterodactylAPI,
|
||||
PterodactylConfigurationError,
|
||||
PterodactylConnectionError,
|
||||
PterodactylData,
|
||||
)
|
||||
|
||||
SCAN_INTERVAL = timedelta(seconds=60)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
type PterodactylConfigEntry = ConfigEntry[PterodactylCoordinator]
|
||||
|
||||
|
||||
class PterodactylCoordinator(DataUpdateCoordinator[dict[str, PterodactylData]]):
|
||||
"""Pterodactyl data update coordinator."""
|
||||
|
||||
config_entry: PterodactylConfigEntry
|
||||
api: PterodactylAPI
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
config_entry: PterodactylConfigEntry,
|
||||
) -> None:
|
||||
"""Initialize coordinator instance."""
|
||||
|
||||
super().__init__(
|
||||
hass=hass,
|
||||
name=config_entry.data[CONF_URL],
|
||||
config_entry=config_entry,
|
||||
logger=_LOGGER,
|
||||
update_interval=SCAN_INTERVAL,
|
||||
)
|
||||
|
||||
async def _async_setup(self) -> None:
|
||||
"""Set up the Pterodactyl data coordinator."""
|
||||
self.api = PterodactylAPI(
|
||||
hass=self.hass,
|
||||
host=self.config_entry.data[CONF_URL],
|
||||
api_key=self.config_entry.data[CONF_API_KEY],
|
||||
)
|
||||
|
||||
try:
|
||||
await self.api.async_init()
|
||||
except PterodactylConfigurationError as error:
|
||||
raise UpdateFailed(error) from error
|
||||
|
||||
async def _async_update_data(self) -> dict[str, PterodactylData]:
|
||||
"""Get updated data from the Pterodactyl server."""
|
||||
try:
|
||||
return await self.api.async_get_data()
|
||||
except PterodactylConnectionError as error:
|
||||
raise UpdateFailed(error) from error
|
47
homeassistant/components/pterodactyl/entity.py
Normal file
47
homeassistant/components/pterodactyl/entity.py
Normal file
@ -0,0 +1,47 @@
|
||||
"""Base entity for the Pterodactyl integration."""
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_URL
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .api import PterodactylData
|
||||
from .const import DOMAIN
|
||||
from .coordinator import PterodactylCoordinator
|
||||
|
||||
MANUFACTURER = "Pterodactyl"
|
||||
|
||||
|
||||
class PterodactylEntity(CoordinatorEntity[PterodactylCoordinator]):
|
||||
"""Representation of a Pterodactyl base entity."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: PterodactylCoordinator,
|
||||
identifier: str,
|
||||
config_entry: ConfigEntry,
|
||||
) -> None:
|
||||
"""Initialize base entity."""
|
||||
super().__init__(coordinator)
|
||||
|
||||
self.identifier = identifier
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, identifier)},
|
||||
manufacturer=MANUFACTURER,
|
||||
name=self.game_server_data.name,
|
||||
model=self.game_server_data.name,
|
||||
model_id=self.game_server_data.uuid,
|
||||
configuration_url=f"{config_entry.data[CONF_URL]}/server/{identifier}",
|
||||
)
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return binary sensor availability."""
|
||||
return super().available and self.identifier in self.coordinator.data
|
||||
|
||||
@property
|
||||
def game_server_data(self) -> PterodactylData:
|
||||
"""Return game server data."""
|
||||
return self.coordinator.data[self.identifier]
|
10
homeassistant/components/pterodactyl/manifest.json
Normal file
10
homeassistant/components/pterodactyl/manifest.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"domain": "pterodactyl",
|
||||
"name": "Pterodactyl",
|
||||
"codeowners": ["@elmurato"],
|
||||
"config_flow": true,
|
||||
"documentation": "https://www.home-assistant.io/integrations/pterodactyl",
|
||||
"iot_class": "local_polling",
|
||||
"quality_scale": "bronze",
|
||||
"requirements": ["py-dactyl==2.0.4"]
|
||||
}
|
93
homeassistant/components/pterodactyl/quality_scale.yaml
Normal file
93
homeassistant/components/pterodactyl/quality_scale.yaml
Normal file
@ -0,0 +1,93 @@
|
||||
rules:
|
||||
# Bronze
|
||||
action-setup:
|
||||
status: exempt
|
||||
comment: Integration doesn't provide any service actions.
|
||||
appropriate-polling: done
|
||||
brands: done
|
||||
common-modules: done
|
||||
config-flow: done
|
||||
config-flow-test-coverage: done
|
||||
dependency-transparency: done
|
||||
docs-actions:
|
||||
status: exempt
|
||||
comment: Integration doesn't provide any service actions.
|
||||
docs-high-level-description: done
|
||||
docs-installation-instructions: done
|
||||
docs-removal-instructions: done
|
||||
entity-event-setup:
|
||||
status: done
|
||||
comment: Handled by coordinator.
|
||||
entity-unique-id:
|
||||
status: done
|
||||
comment: Using confid entry ID as the dependency pydactyl doesn't provide a unique information.
|
||||
has-entity-name: done
|
||||
runtime-data: done
|
||||
test-before-configure: done
|
||||
test-before-setup:
|
||||
status: done
|
||||
comment: |
|
||||
Raising ConfigEntryNotReady, if the initialization isn't successful.
|
||||
unique-config-entry:
|
||||
status: done
|
||||
comment: |
|
||||
As there is no unique information available from the dependency pydactyl,
|
||||
the server host is used to identify that the same service is already configured.
|
||||
|
||||
# Silver
|
||||
action-exceptions:
|
||||
status: exempt
|
||||
comment: Integration doesn't provide any service actions.
|
||||
config-entry-unloading: done
|
||||
docs-configuration-parameters:
|
||||
status: exempt
|
||||
comment: Integration doesn't support any configuration parameters.
|
||||
docs-installation-parameters: todo
|
||||
entity-unavailable:
|
||||
status: done
|
||||
comment: Handled by coordinator.
|
||||
integration-owner: done
|
||||
log-when-unavailable:
|
||||
status: done
|
||||
comment: Handled by coordinator.
|
||||
parallel-updates: done
|
||||
reauthentication-flow: todo
|
||||
test-coverage: todo
|
||||
|
||||
# Gold
|
||||
devices: done
|
||||
diagnostics: todo
|
||||
discovery:
|
||||
status: exempt
|
||||
comment: No discovery possible.
|
||||
discovery-update-info:
|
||||
status: exempt
|
||||
comment: |
|
||||
No discovery possible. Users can use the (local or public) hostname instead of an IP address,
|
||||
if static IP addresses cannot be configured.
|
||||
docs-data-update: todo
|
||||
docs-examples: todo
|
||||
docs-known-limitations: todo
|
||||
docs-supported-devices: todo
|
||||
docs-supported-functions: todo
|
||||
docs-troubleshooting: todo
|
||||
docs-use-cases: todo
|
||||
dynamic-devices: todo
|
||||
entity-category: done
|
||||
entity-device-class: done
|
||||
entity-disabled-by-default: todo
|
||||
entity-translations: done
|
||||
exception-translations: todo
|
||||
icon-translations: todo
|
||||
reconfiguration-flow: todo
|
||||
repair-issues:
|
||||
status: exempt
|
||||
comment: No repair use-cases for this integration.
|
||||
stale-devices: todo
|
||||
|
||||
# Platinum
|
||||
async-dependency: todo
|
||||
inject-websession:
|
||||
status: exempt
|
||||
comment: Integration isn't making any HTTP requests.
|
||||
strict-typing: todo
|
30
homeassistant/components/pterodactyl/strings.json
Normal file
30
homeassistant/components/pterodactyl/strings.json
Normal file
@ -0,0 +1,30 @@
|
||||
{
|
||||
"config": {
|
||||
"step": {
|
||||
"user": {
|
||||
"data": {
|
||||
"url": "[%key:common::config_flow::data::url%]",
|
||||
"api_key": "[%key:common::config_flow::data::api_key%]"
|
||||
},
|
||||
"data_description": {
|
||||
"url": "The URL of your Pterodactyl server, including the protocol (http:// or https://) and optionally the port number.",
|
||||
"api_key": "The account API key for accessing your Pterodactyl server."
|
||||
}
|
||||
}
|
||||
},
|
||||
"error": {
|
||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
||||
"unknown": "[%key:common::config_flow::error::unknown%]"
|
||||
},
|
||||
"abort": {
|
||||
"already_configured": "[%key:common::config_flow::abort::already_configured_service%]"
|
||||
}
|
||||
},
|
||||
"entity": {
|
||||
"binary_sensor": {
|
||||
"status": {
|
||||
"name": "Status"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
1
homeassistant/generated/config_flows.py
generated
1
homeassistant/generated/config_flows.py
generated
@ -488,6 +488,7 @@ FLOWS = {
|
||||
"proximity",
|
||||
"prusalink",
|
||||
"ps4",
|
||||
"pterodactyl",
|
||||
"pure_energie",
|
||||
"purpleair",
|
||||
"pushbullet",
|
||||
|
@ -5021,6 +5021,12 @@
|
||||
"integration_type": "virtual",
|
||||
"supported_by": "opower"
|
||||
},
|
||||
"pterodactyl": {
|
||||
"name": "Pterodactyl",
|
||||
"integration_type": "hub",
|
||||
"config_flow": true,
|
||||
"iot_class": "local_polling"
|
||||
},
|
||||
"pulseaudio_loopback": {
|
||||
"name": "PulseAudio Loopback",
|
||||
"integration_type": "hub",
|
||||
|
3
requirements_all.txt
generated
3
requirements_all.txt
generated
@ -1733,6 +1733,9 @@ py-ccm15==0.0.9
|
||||
# homeassistant.components.cpuspeed
|
||||
py-cpuinfo==9.0.0
|
||||
|
||||
# homeassistant.components.pterodactyl
|
||||
py-dactyl==2.0.4
|
||||
|
||||
# homeassistant.components.dormakaba_dkey
|
||||
py-dormakaba-dkey==1.0.5
|
||||
|
||||
|
3
requirements_test_all.txt
generated
3
requirements_test_all.txt
generated
@ -1432,6 +1432,9 @@ py-ccm15==0.0.9
|
||||
# homeassistant.components.cpuspeed
|
||||
py-cpuinfo==9.0.0
|
||||
|
||||
# homeassistant.components.pterodactyl
|
||||
py-dactyl==2.0.4
|
||||
|
||||
# homeassistant.components.dormakaba_dkey
|
||||
py-dormakaba-dkey==1.0.5
|
||||
|
||||
|
1
tests/components/pterodactyl/__init__.py
Normal file
1
tests/components/pterodactyl/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
"""Tests for the Pterodactyl integration."""
|
155
tests/components/pterodactyl/conftest.py
Normal file
155
tests/components/pterodactyl/conftest.py
Normal file
@ -0,0 +1,155 @@
|
||||
"""Common fixtures for the Pterodactyl tests."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from pydactyl.responses import PaginatedResponse
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.pterodactyl.const import DOMAIN
|
||||
from homeassistant.const import CONF_API_KEY, CONF_URL
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
TEST_URL = "https://192.168.0.1:8080/"
|
||||
TEST_API_KEY = "TestClientApiKey"
|
||||
TEST_USER_INPUT = {
|
||||
CONF_URL: TEST_URL,
|
||||
CONF_API_KEY: TEST_API_KEY,
|
||||
}
|
||||
TEST_SERVER_LIST_DATA = {
|
||||
"meta": {"pagination": {"total": 2, "count": 2, "per_page": 50, "current_page": 1}},
|
||||
"data": [
|
||||
{
|
||||
"object": "server",
|
||||
"attributes": {
|
||||
"server_owner": True,
|
||||
"identifier": "1",
|
||||
"internal_id": 1,
|
||||
"uuid": "1-1-1-1-1",
|
||||
"name": "Test Server 1",
|
||||
"node": "default_node",
|
||||
"description": "Description of Test Server 1",
|
||||
"limits": {
|
||||
"memory": 2048,
|
||||
"swap": 1024,
|
||||
"disk": 10240,
|
||||
"io": 500,
|
||||
"cpu": 100,
|
||||
"threads": None,
|
||||
"oom_disabled": True,
|
||||
},
|
||||
"invocation": "java -jar test_server1.jar",
|
||||
"docker_image": "test_docker_image_1",
|
||||
"egg_features": ["java_version"],
|
||||
},
|
||||
},
|
||||
{
|
||||
"object": "server",
|
||||
"attributes": {
|
||||
"server_owner": True,
|
||||
"identifier": "2",
|
||||
"internal_id": 2,
|
||||
"uuid": "2-2-2-2-2",
|
||||
"name": "Test Server 2",
|
||||
"node": "default_node",
|
||||
"description": "Description of Test Server 2",
|
||||
"limits": {
|
||||
"memory": 2048,
|
||||
"swap": 1024,
|
||||
"disk": 10240,
|
||||
"io": 500,
|
||||
"cpu": 100,
|
||||
"threads": None,
|
||||
"oom_disabled": True,
|
||||
},
|
||||
"invocation": "java -jar test_server_2.jar",
|
||||
"docker_image": "test_docker_image2",
|
||||
"egg_features": ["java_version"],
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
TEST_SERVER = {
|
||||
"server_owner": True,
|
||||
"identifier": "1",
|
||||
"internal_id": 1,
|
||||
"uuid": "1-1-1-1-1",
|
||||
"name": "Test Server 1",
|
||||
"node": "default_node",
|
||||
"is_node_under_maintenance": False,
|
||||
"sftp_details": {"ip": "192.168.0.1", "port": 2022},
|
||||
"description": "",
|
||||
"limits": {
|
||||
"memory": 2048,
|
||||
"swap": 1024,
|
||||
"disk": 10240,
|
||||
"io": 500,
|
||||
"cpu": 100,
|
||||
"threads": None,
|
||||
"oom_disabled": True,
|
||||
},
|
||||
"invocation": "java -jar test.jar",
|
||||
"docker_image": "test_docker_image",
|
||||
"egg_features": ["eula", "java_version", "pid_limit"],
|
||||
"feature_limits": {"databases": 0, "allocations": 0, "backups": 3},
|
||||
"status": None,
|
||||
"is_suspended": False,
|
||||
"is_installing": False,
|
||||
"is_transferring": False,
|
||||
"relationships": {"allocations": {...}, "variables": {...}},
|
||||
}
|
||||
TEST_SERVER_UTILIZATION = {
|
||||
"current_state": "running",
|
||||
"is_suspended": False,
|
||||
"resources": {
|
||||
"memory_bytes": 1111,
|
||||
"cpu_absolute": 22,
|
||||
"disk_bytes": 3333,
|
||||
"network_rx_bytes": 44,
|
||||
"network_tx_bytes": 55,
|
||||
"uptime": 6666,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_setup_entry() -> Generator[AsyncMock]:
|
||||
"""Override async_setup_entry."""
|
||||
with patch(
|
||||
"homeassistant.components.pterodactyl.async_setup_entry", return_value=True
|
||||
) as mock_setup_entry:
|
||||
yield mock_setup_entry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config_entry() -> MockConfigEntry:
|
||||
"""Create Pterodactyl mock config entry."""
|
||||
return MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
unique_id=None,
|
||||
entry_id="01234567890123456789012345678901",
|
||||
title=TEST_URL,
|
||||
data={
|
||||
CONF_URL: TEST_URL,
|
||||
CONF_API_KEY: TEST_API_KEY,
|
||||
},
|
||||
version=1,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_pterodactyl():
|
||||
"""Mock the Pterodactyl API."""
|
||||
with patch(
|
||||
"homeassistant.components.pterodactyl.api.PterodactylClient", autospec=True
|
||||
) as mock:
|
||||
mock.return_value.client.servers.list_servers.return_value = PaginatedResponse(
|
||||
mock.return_value, "client", TEST_SERVER_LIST_DATA
|
||||
)
|
||||
mock.return_value.client.servers.get_server.return_value = TEST_SERVER
|
||||
mock.return_value.client.servers.get_server_utilization.return_value = (
|
||||
TEST_SERVER_UTILIZATION
|
||||
)
|
||||
|
||||
yield mock.return_value
|
129
tests/components/pterodactyl/test_config_flow.py
Normal file
129
tests/components/pterodactyl/test_config_flow.py
Normal file
@ -0,0 +1,129 @@
|
||||
"""Test the Pterodactyl config flow."""
|
||||
|
||||
from pydactyl import PterodactylClient
|
||||
from pydactyl.exceptions import ClientConfigError, PterodactylApiError
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.pterodactyl.const import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from .conftest import TEST_URL, TEST_USER_INPUT
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_pterodactyl", "mock_setup_entry")
|
||||
async def test_full_flow(hass: HomeAssistant) -> None:
|
||||
"""Test full flow without errors."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
flow_id=result["flow_id"],
|
||||
user_input=TEST_USER_INPUT,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == TEST_URL
|
||||
assert result["data"] == TEST_USER_INPUT
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_setup_entry")
|
||||
@pytest.mark.parametrize(
|
||||
"exception_type",
|
||||
[
|
||||
ClientConfigError,
|
||||
PterodactylApiError,
|
||||
],
|
||||
)
|
||||
async def test_recovery_after_api_error(
|
||||
hass: HomeAssistant,
|
||||
exception_type,
|
||||
mock_pterodactyl: PterodactylClient,
|
||||
) -> None:
|
||||
"""Test recovery after an API error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
mock_pterodactyl.client.servers.list_servers.side_effect = exception_type
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
flow_id=result["flow_id"],
|
||||
user_input=TEST_USER_INPUT,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
mock_pterodactyl.reset_mock(side_effect=True)
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
flow_id=result["flow_id"], user_input=TEST_USER_INPUT
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == TEST_URL
|
||||
assert result["data"] == TEST_USER_INPUT
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_setup_entry")
|
||||
async def test_recovery_after_unknown_error(
|
||||
hass: HomeAssistant,
|
||||
mock_pterodactyl: PterodactylClient,
|
||||
) -> None:
|
||||
"""Test recovery after an API error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
mock_pterodactyl.client.servers.list_servers.side_effect = Exception
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
flow_id=result["flow_id"],
|
||||
user_input=TEST_USER_INPUT,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {"base": "unknown"}
|
||||
|
||||
mock_pterodactyl.reset_mock(side_effect=True)
|
||||
|
||||
result = await hass.config_entries.flow.async_configure(
|
||||
flow_id=result["flow_id"], user_input=TEST_USER_INPUT
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == TEST_URL
|
||||
assert result["data"] == TEST_USER_INPUT
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_setup_entry")
|
||||
async def test_service_already_configured(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_pterodactyl: PterodactylClient,
|
||||
) -> None:
|
||||
"""Test config flow abort if the Pterodactyl server is already configured."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=TEST_USER_INPUT
|
||||
)
|
||||
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "already_configured"
|
Loading…
x
Reference in New Issue
Block a user