mirror of
https://github.com/home-assistant/core.git
synced 2026-02-15 07:36:16 +00:00
Co-authored-by: Robert Resch <robert@resch.dev> Co-authored-by: Abílio Costa <abmantis@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Joostlek <joostlek@outlook.com>
101 lines
2.8 KiB
Python
101 lines
2.8 KiB
Python
"""Binary sensor to read Proxmox VE data."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from homeassistant.components.binary_sensor import (
|
|
BinarySensorDeviceClass,
|
|
BinarySensorEntity,
|
|
)
|
|
from homeassistant.const import CONF_HOST
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
|
|
|
from . import ProxmoxConfigEntry
|
|
from .const import CONF_CONTAINERS, CONF_NODE, CONF_NODES, CONF_VMS
|
|
from .entity import ProxmoxEntity
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
entry: ProxmoxConfigEntry,
|
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
|
) -> None:
|
|
"""Set up binary sensors."""
|
|
sensors = []
|
|
|
|
host_name = entry.data[CONF_HOST]
|
|
host_name_coordinators = entry.runtime_data[host_name]
|
|
|
|
for node_config in entry.data[CONF_NODES]:
|
|
node_name = node_config[CONF_NODE]
|
|
|
|
for dev_id in node_config[CONF_VMS] + node_config[CONF_CONTAINERS]:
|
|
coordinator = host_name_coordinators[node_name][dev_id]
|
|
|
|
if TYPE_CHECKING:
|
|
assert coordinator.data is not None
|
|
name = coordinator.data["name"]
|
|
sensor = create_binary_sensor(
|
|
coordinator, host_name, node_name, dev_id, name
|
|
)
|
|
sensors.append(sensor)
|
|
|
|
async_add_entities(sensors)
|
|
|
|
|
|
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."""
|
|
return ProxmoxBinarySensor(
|
|
coordinator=coordinator,
|
|
unique_id=f"proxmox_{node_name}_{vm_id}_running",
|
|
name=f"{node_name}_{name}",
|
|
icon="",
|
|
host_name=host_name,
|
|
node_name=node_name,
|
|
vm_id=vm_id,
|
|
)
|
|
|
|
|
|
class ProxmoxBinarySensor(ProxmoxEntity, BinarySensorEntity):
|
|
"""A binary sensor for reading Proxmox VE data."""
|
|
|
|
_attr_device_class = BinarySensorDeviceClass.RUNNING
|
|
|
|
def __init__(
|
|
self,
|
|
coordinator: DataUpdateCoordinator,
|
|
unique_id: str,
|
|
name: str,
|
|
icon: str,
|
|
host_name: str,
|
|
node_name: str,
|
|
vm_id: int,
|
|
) -> None:
|
|
"""Create the binary sensor for vms or containers."""
|
|
super().__init__(
|
|
coordinator, unique_id, name, icon, host_name, node_name, vm_id
|
|
)
|
|
|
|
@property
|
|
def is_on(self) -> bool | None:
|
|
"""Return the state of the binary sensor."""
|
|
if (data := self.coordinator.data) is None:
|
|
return None
|
|
|
|
return data["status"] == "running"
|
|
|
|
@property
|
|
def available(self) -> bool:
|
|
"""Return sensor availability."""
|
|
|
|
return super().available and self.coordinator.data is not None
|