mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Improve Proxmox VE type hints (#90359)
* Improves some type hints in Proxmox VE * update * update] * fix isort * Fix vm_id type * Fix vm_id type * Update homeassistant/components/proxmoxve/__init__.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/proxmoxve/__init__.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Change initialization of _proxmox * Move definition of _proxmox to class level --------- Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
parent
c51ed4b328
commit
190393c6bb
@ -2,6 +2,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from proxmoxer import AuthenticationError, ProxmoxAPI
|
from proxmoxer import AuthenticationError, ProxmoxAPI
|
||||||
from proxmoxer.core import ResourceException
|
from proxmoxer.core import ResourceException
|
||||||
@ -185,14 +186,19 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
|
|
||||||
|
|
||||||
def create_coordinator_container_vm(
|
def create_coordinator_container_vm(
|
||||||
hass, proxmox, host_name, node_name, vm_id, vm_type
|
hass: HomeAssistant,
|
||||||
):
|
proxmox: ProxmoxAPI,
|
||||||
|
host_name: str,
|
||||||
|
node_name: str,
|
||||||
|
vm_id: int,
|
||||||
|
vm_type: int,
|
||||||
|
) -> DataUpdateCoordinator[dict[str, Any] | None]:
|
||||||
"""Create and return a DataUpdateCoordinator for a vm/container."""
|
"""Create and return a DataUpdateCoordinator for a vm/container."""
|
||||||
|
|
||||||
async def async_update_data():
|
async def async_update_data() -> dict[str, Any] | None:
|
||||||
"""Call the api and handle the response."""
|
"""Call the api and handle the response."""
|
||||||
|
|
||||||
def poll_api():
|
def poll_api() -> dict[str, Any] | None:
|
||||||
"""Call the api."""
|
"""Call the api."""
|
||||||
vm_status = call_api_container_vm(proxmox, node_name, vm_id, vm_type)
|
vm_status = call_api_container_vm(proxmox, node_name, vm_id, vm_type)
|
||||||
return vm_status
|
return vm_status
|
||||||
@ -216,7 +222,7 @@ def create_coordinator_container_vm(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def parse_api_container_vm(status):
|
def parse_api_container_vm(status: dict[str, Any]) -> dict[str, Any]:
|
||||||
"""Get the container or vm api data and return it formatted in a dictionary.
|
"""Get the container or vm api data and return it formatted in a dictionary.
|
||||||
|
|
||||||
It is implemented in this way to allow for more data to be added for sensors
|
It is implemented in this way to allow for more data to be added for sensors
|
||||||
@ -226,7 +232,12 @@ def parse_api_container_vm(status):
|
|||||||
return {"status": status["status"], "name": status["name"]}
|
return {"status": status["status"], "name": status["name"]}
|
||||||
|
|
||||||
|
|
||||||
def call_api_container_vm(proxmox, node_name, vm_id, machine_type):
|
def call_api_container_vm(
|
||||||
|
proxmox: ProxmoxAPI,
|
||||||
|
node_name: str,
|
||||||
|
vm_id: int,
|
||||||
|
machine_type: int,
|
||||||
|
) -> dict[str, Any] | None:
|
||||||
"""Make proper api calls."""
|
"""Make proper api calls."""
|
||||||
status = None
|
status = None
|
||||||
|
|
||||||
@ -247,12 +258,12 @@ class ProxmoxEntity(CoordinatorEntity):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: DataUpdateCoordinator,
|
coordinator: DataUpdateCoordinator,
|
||||||
unique_id,
|
unique_id: str,
|
||||||
name,
|
name: str,
|
||||||
icon,
|
icon: str,
|
||||||
host_name,
|
host_name: str,
|
||||||
node_name,
|
node_name: str,
|
||||||
vm_id=None,
|
vm_id: int | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the Proxmox entity."""
|
"""Initialize the Proxmox entity."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
@ -292,7 +303,17 @@ class ProxmoxEntity(CoordinatorEntity):
|
|||||||
class ProxmoxClient:
|
class ProxmoxClient:
|
||||||
"""A wrapper for the proxmoxer ProxmoxAPI client."""
|
"""A wrapper for the proxmoxer ProxmoxAPI client."""
|
||||||
|
|
||||||
def __init__(self, host, port, user, realm, password, verify_ssl):
|
_proxmox: ProxmoxAPI
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
host: str,
|
||||||
|
port: int,
|
||||||
|
user: str,
|
||||||
|
realm: str,
|
||||||
|
password: str,
|
||||||
|
verify_ssl: bool,
|
||||||
|
) -> None:
|
||||||
"""Initialize the ProxmoxClient."""
|
"""Initialize the ProxmoxClient."""
|
||||||
|
|
||||||
self._host = host
|
self._host = host
|
||||||
@ -302,10 +323,7 @@ class ProxmoxClient:
|
|||||||
self._password = password
|
self._password = password
|
||||||
self._verify_ssl = verify_ssl
|
self._verify_ssl = verify_ssl
|
||||||
|
|
||||||
self._proxmox = None
|
def build_client(self) -> None:
|
||||||
self._connection_start_time = None
|
|
||||||
|
|
||||||
def build_client(self):
|
|
||||||
"""Construct the ProxmoxAPI client.
|
"""Construct the ProxmoxAPI client.
|
||||||
|
|
||||||
Allows inserting the realm within the `user` value.
|
Allows inserting the realm within the `user` value.
|
||||||
@ -324,6 +342,6 @@ class ProxmoxClient:
|
|||||||
verify_ssl=self._verify_ssl,
|
verify_ssl=self._verify_ssl,
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_api_client(self):
|
def get_api_client(self) -> ProxmoxAPI:
|
||||||
"""Return the ProxmoxAPI client."""
|
"""Return the ProxmoxAPI client."""
|
||||||
return self._proxmox
|
return self._proxmox
|
||||||
|
@ -51,7 +51,13 @@ async def async_setup_platform(
|
|||||||
add_entities(sensors)
|
add_entities(sensors)
|
||||||
|
|
||||||
|
|
||||||
def create_binary_sensor(coordinator, host_name, node_name, vm_id, name):
|
def create_binary_sensor(
|
||||||
|
coordinator,
|
||||||
|
host_name: str,
|
||||||
|
node_name: str,
|
||||||
|
vm_id: int,
|
||||||
|
name: str,
|
||||||
|
) -> ProxmoxBinarySensor:
|
||||||
"""Create a binary sensor based on the given data."""
|
"""Create a binary sensor based on the given data."""
|
||||||
return ProxmoxBinarySensor(
|
return ProxmoxBinarySensor(
|
||||||
coordinator=coordinator,
|
coordinator=coordinator,
|
||||||
@ -72,12 +78,12 @@ class ProxmoxBinarySensor(ProxmoxEntity, BinarySensorEntity):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: DataUpdateCoordinator,
|
coordinator: DataUpdateCoordinator,
|
||||||
unique_id,
|
unique_id: str,
|
||||||
name,
|
name: str,
|
||||||
icon,
|
icon: str,
|
||||||
host_name,
|
host_name: str,
|
||||||
node_name,
|
node_name: str,
|
||||||
vm_id,
|
vm_id: int,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Create the binary sensor for vms or containers."""
|
"""Create the binary sensor for vms or containers."""
|
||||||
super().__init__(
|
super().__init__(
|
||||||
@ -85,7 +91,7 @@ class ProxmoxBinarySensor(ProxmoxEntity, BinarySensorEntity):
|
|||||||
)
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self) -> bool | None:
|
||||||
"""Return the state of the binary sensor."""
|
"""Return the state of the binary sensor."""
|
||||||
if (data := self.coordinator.data) is None:
|
if (data := self.coordinator.data) is None:
|
||||||
return None
|
return None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user