Move upcloud base entity to separate module (#126533)
This commit is contained in:
parent
9fafbbff81
commit
225266b687
5 changed files with 119 additions and 105 deletions
|
@ -5,7 +5,6 @@ from __future__ import annotations
|
||||||
import dataclasses
|
import dataclasses
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
import requests.exceptions
|
import requests.exceptions
|
||||||
import upcloud_api
|
import upcloud_api
|
||||||
|
@ -15,44 +14,26 @@ from homeassistant.const import (
|
||||||
CONF_PASSWORD,
|
CONF_PASSWORD,
|
||||||
CONF_SCAN_INTERVAL,
|
CONF_SCAN_INTERVAL,
|
||||||
CONF_USERNAME,
|
CONF_USERNAME,
|
||||||
STATE_OFF,
|
|
||||||
STATE_ON,
|
|
||||||
STATE_PROBLEM,
|
|
||||||
Platform,
|
Platform,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
|
||||||
from homeassistant.helpers.dispatcher import (
|
from homeassistant.helpers.dispatcher import (
|
||||||
async_dispatcher_connect,
|
async_dispatcher_connect,
|
||||||
async_dispatcher_send,
|
async_dispatcher_send,
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
||||||
|
|
||||||
from .const import CONFIG_ENTRY_UPDATE_SIGNAL_TEMPLATE, DEFAULT_SCAN_INTERVAL, DOMAIN
|
from .const import (
|
||||||
|
CONFIG_ENTRY_UPDATE_SIGNAL_TEMPLATE,
|
||||||
|
DATA_UPCLOUD,
|
||||||
|
DEFAULT_SCAN_INTERVAL,
|
||||||
|
)
|
||||||
from .coordinator import UpCloudDataUpdateCoordinator
|
from .coordinator import UpCloudDataUpdateCoordinator
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
ATTR_CORE_NUMBER = "core_number"
|
|
||||||
ATTR_HOSTNAME = "hostname"
|
|
||||||
ATTR_MEMORY_AMOUNT = "memory_amount"
|
|
||||||
ATTR_TITLE = "title"
|
|
||||||
ATTR_UUID = "uuid"
|
|
||||||
ATTR_ZONE = "zone"
|
|
||||||
|
|
||||||
CONF_SERVERS = "servers"
|
|
||||||
|
|
||||||
DATA_UPCLOUD = "data_upcloud"
|
|
||||||
|
|
||||||
DEFAULT_COMPONENT_NAME = "UpCloud {}"
|
|
||||||
|
|
||||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SWITCH]
|
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SWITCH]
|
||||||
|
|
||||||
SIGNAL_UPDATE_UPCLOUD = "upcloud_update"
|
|
||||||
|
|
||||||
STATE_MAP = {"error": STATE_PROBLEM, "started": STATE_ON, "stopped": STATE_OFF}
|
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass
|
||||||
class UpCloudHassData:
|
class UpCloudHassData:
|
||||||
|
@ -136,82 +117,3 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
|
||||||
hass.data[DATA_UPCLOUD].coordinators.pop(config_entry.data[CONF_USERNAME])
|
hass.data[DATA_UPCLOUD].coordinators.pop(config_entry.data[CONF_USERNAME])
|
||||||
|
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
|
||||||
|
|
||||||
class UpCloudServerEntity(CoordinatorEntity[UpCloudDataUpdateCoordinator]):
|
|
||||||
"""Entity class for UpCloud servers."""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
coordinator: UpCloudDataUpdateCoordinator,
|
|
||||||
uuid: str,
|
|
||||||
) -> None:
|
|
||||||
"""Initialize the UpCloud server entity."""
|
|
||||||
super().__init__(coordinator)
|
|
||||||
self.uuid = uuid
|
|
||||||
|
|
||||||
@property
|
|
||||||
def _server(self) -> upcloud_api.Server:
|
|
||||||
return self.coordinator.data[self.uuid]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self) -> str:
|
|
||||||
"""Return unique ID for the entity."""
|
|
||||||
return self.uuid
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self) -> str:
|
|
||||||
"""Return the name of the component."""
|
|
||||||
try:
|
|
||||||
return DEFAULT_COMPONENT_NAME.format(self._server.title)
|
|
||||||
except (AttributeError, KeyError, TypeError):
|
|
||||||
return DEFAULT_COMPONENT_NAME.format(self.uuid)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self) -> str:
|
|
||||||
"""Return the icon of this server."""
|
|
||||||
return "mdi:server" if self.is_on else "mdi:server-off"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def is_on(self) -> bool:
|
|
||||||
"""Return true if the server is on."""
|
|
||||||
try:
|
|
||||||
return STATE_MAP.get(self._server.state, self._server.state) == STATE_ON # type: ignore[no-any-return]
|
|
||||||
except AttributeError:
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
|
||||||
def available(self) -> bool:
|
|
||||||
"""Return True if entity is available."""
|
|
||||||
return super().available and STATE_MAP.get(
|
|
||||||
self._server.state, self._server.state
|
|
||||||
) in (STATE_ON, STATE_OFF)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def extra_state_attributes(self) -> dict[str, Any]:
|
|
||||||
"""Return the state attributes of the UpCloud server."""
|
|
||||||
return {
|
|
||||||
x: getattr(self._server, x, None)
|
|
||||||
for x in (
|
|
||||||
ATTR_UUID,
|
|
||||||
ATTR_TITLE,
|
|
||||||
ATTR_HOSTNAME,
|
|
||||||
ATTR_ZONE,
|
|
||||||
ATTR_CORE_NUMBER,
|
|
||||||
ATTR_MEMORY_AMOUNT,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_info(self) -> DeviceInfo:
|
|
||||||
"""Return info for device registry."""
|
|
||||||
assert self.coordinator.config_entry is not None
|
|
||||||
return DeviceInfo(
|
|
||||||
configuration_url="https://hub.upcloud.com",
|
|
||||||
model="Control Panel",
|
|
||||||
entry_type=DeviceEntryType.SERVICE,
|
|
||||||
identifiers={
|
|
||||||
(DOMAIN, f"{self.coordinator.config_entry.data[CONF_USERNAME]}@hub")
|
|
||||||
},
|
|
||||||
manufacturer="UpCloud Ltd",
|
|
||||||
)
|
|
||||||
|
|
|
@ -9,7 +9,8 @@ from homeassistant.const import CONF_USERNAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import DATA_UPCLOUD, UpCloudServerEntity
|
from .const import DATA_UPCLOUD
|
||||||
|
from .entity import UpCloudServerEntity
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
|
|
|
@ -3,5 +3,6 @@
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
DOMAIN = "upcloud"
|
DOMAIN = "upcloud"
|
||||||
|
DATA_UPCLOUD = "data_upcloud"
|
||||||
DEFAULT_SCAN_INTERVAL = timedelta(seconds=60)
|
DEFAULT_SCAN_INTERVAL = timedelta(seconds=60)
|
||||||
CONFIG_ENTRY_UPDATE_SIGNAL_TEMPLATE = f"{DOMAIN}_config_entry_update:{{}}"
|
CONFIG_ENTRY_UPDATE_SIGNAL_TEMPLATE = f"{DOMAIN}_config_entry_update:{{}}"
|
||||||
|
|
107
homeassistant/components/upcloud/entity.py
Normal file
107
homeassistant/components/upcloud/entity.py
Normal file
|
@ -0,0 +1,107 @@
|
||||||
|
"""Support for UpCloud."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
import upcloud_api
|
||||||
|
|
||||||
|
from homeassistant.const import CONF_USERNAME, STATE_OFF, STATE_ON, STATE_PROBLEM
|
||||||
|
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||||
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
|
from .const import DOMAIN
|
||||||
|
from .coordinator import UpCloudDataUpdateCoordinator
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
ATTR_CORE_NUMBER = "core_number"
|
||||||
|
ATTR_HOSTNAME = "hostname"
|
||||||
|
ATTR_MEMORY_AMOUNT = "memory_amount"
|
||||||
|
ATTR_TITLE = "title"
|
||||||
|
ATTR_UUID = "uuid"
|
||||||
|
ATTR_ZONE = "zone"
|
||||||
|
|
||||||
|
DEFAULT_COMPONENT_NAME = "UpCloud {}"
|
||||||
|
|
||||||
|
STATE_MAP = {"error": STATE_PROBLEM, "started": STATE_ON, "stopped": STATE_OFF}
|
||||||
|
|
||||||
|
|
||||||
|
class UpCloudServerEntity(CoordinatorEntity[UpCloudDataUpdateCoordinator]):
|
||||||
|
"""Entity class for UpCloud servers."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: UpCloudDataUpdateCoordinator,
|
||||||
|
uuid: str,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the UpCloud server entity."""
|
||||||
|
super().__init__(coordinator)
|
||||||
|
self.uuid = uuid
|
||||||
|
|
||||||
|
@property
|
||||||
|
def _server(self) -> upcloud_api.Server:
|
||||||
|
return self.coordinator.data[self.uuid]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self) -> str:
|
||||||
|
"""Return unique ID for the entity."""
|
||||||
|
return self.uuid
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self) -> str:
|
||||||
|
"""Return the name of the component."""
|
||||||
|
try:
|
||||||
|
return DEFAULT_COMPONENT_NAME.format(self._server.title)
|
||||||
|
except (AttributeError, KeyError, TypeError):
|
||||||
|
return DEFAULT_COMPONENT_NAME.format(self.uuid)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def icon(self) -> str:
|
||||||
|
"""Return the icon of this server."""
|
||||||
|
return "mdi:server" if self.is_on else "mdi:server-off"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self) -> bool:
|
||||||
|
"""Return true if the server is on."""
|
||||||
|
try:
|
||||||
|
return STATE_MAP.get(self._server.state, self._server.state) == STATE_ON # type: ignore[no-any-return]
|
||||||
|
except AttributeError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self) -> bool:
|
||||||
|
"""Return True if entity is available."""
|
||||||
|
return super().available and STATE_MAP.get(
|
||||||
|
self._server.state, self._server.state
|
||||||
|
) in (STATE_ON, STATE_OFF)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def extra_state_attributes(self) -> dict[str, Any]:
|
||||||
|
"""Return the state attributes of the UpCloud server."""
|
||||||
|
return {
|
||||||
|
x: getattr(self._server, x, None)
|
||||||
|
for x in (
|
||||||
|
ATTR_UUID,
|
||||||
|
ATTR_TITLE,
|
||||||
|
ATTR_HOSTNAME,
|
||||||
|
ATTR_ZONE,
|
||||||
|
ATTR_CORE_NUMBER,
|
||||||
|
ATTR_MEMORY_AMOUNT,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def device_info(self) -> DeviceInfo:
|
||||||
|
"""Return info for device registry."""
|
||||||
|
assert self.coordinator.config_entry is not None
|
||||||
|
return DeviceInfo(
|
||||||
|
configuration_url="https://hub.upcloud.com",
|
||||||
|
model="Control Panel",
|
||||||
|
entry_type=DeviceEntryType.SERVICE,
|
||||||
|
identifiers={
|
||||||
|
(DOMAIN, f"{self.coordinator.config_entry.data[CONF_USERNAME]}@hub")
|
||||||
|
},
|
||||||
|
manufacturer="UpCloud Ltd",
|
||||||
|
)
|
|
@ -9,7 +9,10 @@ from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.dispatcher import dispatcher_send
|
from homeassistant.helpers.dispatcher import dispatcher_send
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import DATA_UPCLOUD, SIGNAL_UPDATE_UPCLOUD, UpCloudServerEntity
|
from .const import DATA_UPCLOUD
|
||||||
|
from .entity import UpCloudServerEntity
|
||||||
|
|
||||||
|
SIGNAL_UPDATE_UPCLOUD = "upcloud_update"
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue