mirror of
https://github.com/home-assistant/core.git
synced 2025-11-10 03:19:34 +00:00
Remove Vultr integration (#153560)
This commit is contained in:
@@ -1,100 +0,0 @@
|
||||
"""Support for Vultr."""
|
||||
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
from vultr import Vultr as VultrAPI
|
||||
|
||||
from homeassistant.components import persistent_notification
|
||||
from homeassistant.const import CONF_API_KEY, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
from homeassistant.util import Throttle
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ATTR_AUTO_BACKUPS = "auto_backups"
|
||||
ATTR_ALLOWED_BANDWIDTH = "allowed_bandwidth_gb"
|
||||
ATTR_COST_PER_MONTH = "cost_per_month"
|
||||
ATTR_CURRENT_BANDWIDTH_USED = "current_bandwidth_gb"
|
||||
ATTR_CREATED_AT = "created_at"
|
||||
ATTR_DISK = "disk"
|
||||
ATTR_SUBSCRIPTION_ID = "subid"
|
||||
ATTR_SUBSCRIPTION_NAME = "label"
|
||||
ATTR_IPV4_ADDRESS = "ipv4_address"
|
||||
ATTR_IPV6_ADDRESS = "ipv6_address"
|
||||
ATTR_MEMORY = "memory"
|
||||
ATTR_OS = "os"
|
||||
ATTR_PENDING_CHARGES = "pending_charges"
|
||||
ATTR_REGION = "region"
|
||||
ATTR_VCPUS = "vcpus"
|
||||
|
||||
CONF_SUBSCRIPTION = "subscription"
|
||||
|
||||
DATA_VULTR = "data_vultr"
|
||||
DOMAIN = "vultr"
|
||||
|
||||
NOTIFICATION_ID = "vultr_notification"
|
||||
NOTIFICATION_TITLE = "Vultr Setup"
|
||||
|
||||
VULTR_PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR, Platform.SWITCH]
|
||||
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema(
|
||||
{DOMAIN: vol.Schema({vol.Required(CONF_API_KEY): cv.string})}, extra=vol.ALLOW_EXTRA
|
||||
)
|
||||
|
||||
|
||||
def setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
"""Set up the Vultr component."""
|
||||
api_key = config[DOMAIN].get(CONF_API_KEY)
|
||||
|
||||
vultr = Vultr(api_key)
|
||||
|
||||
try:
|
||||
vultr.update()
|
||||
except RuntimeError as ex:
|
||||
_LOGGER.error("Failed to make update API request because: %s", ex)
|
||||
persistent_notification.create(
|
||||
hass,
|
||||
f"Error: {ex}",
|
||||
title=NOTIFICATION_TITLE,
|
||||
notification_id=NOTIFICATION_ID,
|
||||
)
|
||||
return False
|
||||
|
||||
hass.data[DATA_VULTR] = vultr
|
||||
return True
|
||||
|
||||
|
||||
class Vultr:
|
||||
"""Handle all communication with the Vultr API."""
|
||||
|
||||
def __init__(self, api_key):
|
||||
"""Initialize the Vultr connection."""
|
||||
|
||||
self._api_key = api_key
|
||||
self.data = None
|
||||
self.api = VultrAPI(self._api_key)
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
def update(self):
|
||||
"""Use the data from Vultr API."""
|
||||
self.data = self.api.server_list()
|
||||
|
||||
def _force_update(self):
|
||||
"""Use the data from Vultr API."""
|
||||
self.data = self.api.server_list()
|
||||
|
||||
def halt(self, subscription):
|
||||
"""Halt a subscription (hard power off)."""
|
||||
self.api.server_halt(subscription)
|
||||
self._force_update()
|
||||
|
||||
def start(self, subscription):
|
||||
"""Start a subscription."""
|
||||
self.api.server_start(subscription)
|
||||
self._force_update()
|
||||
@@ -1,121 +0,0 @@
|
||||
"""Support for monitoring the state of Vultr subscriptions (VPS)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
PLATFORM_SCHEMA as BINARY_SENSOR_PLATFORM_SCHEMA,
|
||||
BinarySensorDeviceClass,
|
||||
BinarySensorEntity,
|
||||
)
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from . import (
|
||||
ATTR_ALLOWED_BANDWIDTH,
|
||||
ATTR_AUTO_BACKUPS,
|
||||
ATTR_COST_PER_MONTH,
|
||||
ATTR_CREATED_AT,
|
||||
ATTR_DISK,
|
||||
ATTR_IPV4_ADDRESS,
|
||||
ATTR_IPV6_ADDRESS,
|
||||
ATTR_MEMORY,
|
||||
ATTR_OS,
|
||||
ATTR_REGION,
|
||||
ATTR_SUBSCRIPTION_ID,
|
||||
ATTR_SUBSCRIPTION_NAME,
|
||||
ATTR_VCPUS,
|
||||
CONF_SUBSCRIPTION,
|
||||
DATA_VULTR,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_NAME = "Vultr {}"
|
||||
PLATFORM_SCHEMA = BINARY_SENSOR_PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_SUBSCRIPTION): cv.string,
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def setup_platform(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
add_entities: AddEntitiesCallback,
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up the Vultr subscription (server) binary sensor."""
|
||||
vultr = hass.data[DATA_VULTR]
|
||||
|
||||
subscription = config.get(CONF_SUBSCRIPTION)
|
||||
name = config.get(CONF_NAME)
|
||||
|
||||
if subscription not in vultr.data:
|
||||
_LOGGER.error("Subscription %s not found", subscription)
|
||||
return
|
||||
|
||||
add_entities([VultrBinarySensor(vultr, subscription, name)], True)
|
||||
|
||||
|
||||
class VultrBinarySensor(BinarySensorEntity):
|
||||
"""Representation of a Vultr subscription sensor."""
|
||||
|
||||
_attr_device_class = BinarySensorDeviceClass.POWER
|
||||
|
||||
def __init__(self, vultr, subscription, name):
|
||||
"""Initialize a new Vultr binary sensor."""
|
||||
self._vultr = vultr
|
||||
self._name = name
|
||||
|
||||
self.subscription = subscription
|
||||
self.data = None
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
try:
|
||||
return self._name.format(self.data["label"])
|
||||
except (KeyError, TypeError):
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Return the icon of this server."""
|
||||
return "mdi:server" if self.is_on else "mdi:server-off"
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if the binary sensor is on."""
|
||||
return self.data["power_status"] == "running"
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self):
|
||||
"""Return the state attributes of the Vultr subscription."""
|
||||
return {
|
||||
ATTR_ALLOWED_BANDWIDTH: self.data.get("allowed_bandwidth_gb"),
|
||||
ATTR_AUTO_BACKUPS: self.data.get("auto_backups"),
|
||||
ATTR_COST_PER_MONTH: self.data.get("cost_per_month"),
|
||||
ATTR_CREATED_AT: self.data.get("date_created"),
|
||||
ATTR_DISK: self.data.get("disk"),
|
||||
ATTR_IPV4_ADDRESS: self.data.get("main_ip"),
|
||||
ATTR_IPV6_ADDRESS: self.data.get("v6_main_ip"),
|
||||
ATTR_MEMORY: self.data.get("ram"),
|
||||
ATTR_OS: self.data.get("os"),
|
||||
ATTR_REGION: self.data.get("location"),
|
||||
ATTR_SUBSCRIPTION_ID: self.data.get("SUBID"),
|
||||
ATTR_SUBSCRIPTION_NAME: self.data.get("label"),
|
||||
ATTR_VCPUS: self.data.get("vcpu_count"),
|
||||
}
|
||||
|
||||
def update(self) -> None:
|
||||
"""Update state of sensor."""
|
||||
self._vultr.update()
|
||||
self.data = self._vultr.data[self.subscription]
|
||||
@@ -1,10 +0,0 @@
|
||||
{
|
||||
"domain": "vultr",
|
||||
"name": "Vultr",
|
||||
"codeowners": [],
|
||||
"documentation": "https://www.home-assistant.io/integrations/vultr",
|
||||
"iot_class": "cloud_polling",
|
||||
"loggers": ["vultr"],
|
||||
"quality_scale": "legacy",
|
||||
"requirements": ["vultr==0.1.2"]
|
||||
}
|
||||
@@ -1,123 +0,0 @@
|
||||
"""Support for monitoring the state of Vultr Subscriptions."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA,
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
)
|
||||
from homeassistant.const import CONF_MONITORED_CONDITIONS, CONF_NAME, UnitOfInformation
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from . import (
|
||||
ATTR_CURRENT_BANDWIDTH_USED,
|
||||
ATTR_PENDING_CHARGES,
|
||||
CONF_SUBSCRIPTION,
|
||||
DATA_VULTR,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_NAME = "Vultr {} {}"
|
||||
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||
SensorEntityDescription(
|
||||
key=ATTR_CURRENT_BANDWIDTH_USED,
|
||||
name="Current Bandwidth Used",
|
||||
native_unit_of_measurement=UnitOfInformation.GIGABYTES,
|
||||
device_class=SensorDeviceClass.DATA_SIZE,
|
||||
icon="mdi:chart-histogram",
|
||||
),
|
||||
SensorEntityDescription(
|
||||
key=ATTR_PENDING_CHARGES,
|
||||
name="Pending Charges",
|
||||
native_unit_of_measurement="US$",
|
||||
icon="mdi:currency-usd",
|
||||
),
|
||||
)
|
||||
SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
|
||||
|
||||
PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_SUBSCRIPTION): cv.string,
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_MONITORED_CONDITIONS, default=SENSOR_KEYS): vol.All(
|
||||
cv.ensure_list, [vol.In(SENSOR_KEYS)]
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def setup_platform(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
add_entities: AddEntitiesCallback,
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up the Vultr subscription (server) sensor."""
|
||||
vultr = hass.data[DATA_VULTR]
|
||||
|
||||
subscription = config[CONF_SUBSCRIPTION]
|
||||
name = config[CONF_NAME]
|
||||
monitored_conditions = config[CONF_MONITORED_CONDITIONS]
|
||||
|
||||
if subscription not in vultr.data:
|
||||
_LOGGER.error("Subscription %s not found", subscription)
|
||||
return
|
||||
|
||||
entities = [
|
||||
VultrSensor(vultr, subscription, name, description)
|
||||
for description in SENSOR_TYPES
|
||||
if description.key in monitored_conditions
|
||||
]
|
||||
|
||||
add_entities(entities, True)
|
||||
|
||||
|
||||
class VultrSensor(SensorEntity):
|
||||
"""Representation of a Vultr subscription sensor."""
|
||||
|
||||
def __init__(
|
||||
self, vultr, subscription, name, description: SensorEntityDescription
|
||||
) -> None:
|
||||
"""Initialize a new Vultr sensor."""
|
||||
self.entity_description = description
|
||||
self._vultr = vultr
|
||||
self._name = name
|
||||
|
||||
self.subscription = subscription
|
||||
self.data = None
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
try:
|
||||
return self._name.format(self.entity_description.name)
|
||||
except IndexError:
|
||||
try:
|
||||
return self._name.format(
|
||||
self.data["label"], self.entity_description.name
|
||||
)
|
||||
except (KeyError, TypeError):
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def native_value(self):
|
||||
"""Return the value of this given sensor type."""
|
||||
try:
|
||||
return round(float(self.data.get(self.entity_description.key)), 2)
|
||||
except (TypeError, ValueError):
|
||||
return self.data.get(self.entity_description.key)
|
||||
|
||||
def update(self) -> None:
|
||||
"""Update state of sensor."""
|
||||
self._vultr.update()
|
||||
self.data = self._vultr.data[self.subscription]
|
||||
@@ -1,129 +0,0 @@
|
||||
"""Support for interacting with Vultr subscriptions."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.switch import (
|
||||
PLATFORM_SCHEMA as SWITCH_PLATFORM_SCHEMA,
|
||||
SwitchEntity,
|
||||
)
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
|
||||
from . import (
|
||||
ATTR_ALLOWED_BANDWIDTH,
|
||||
ATTR_AUTO_BACKUPS,
|
||||
ATTR_COST_PER_MONTH,
|
||||
ATTR_CREATED_AT,
|
||||
ATTR_DISK,
|
||||
ATTR_IPV4_ADDRESS,
|
||||
ATTR_IPV6_ADDRESS,
|
||||
ATTR_MEMORY,
|
||||
ATTR_OS,
|
||||
ATTR_REGION,
|
||||
ATTR_SUBSCRIPTION_ID,
|
||||
ATTR_SUBSCRIPTION_NAME,
|
||||
ATTR_VCPUS,
|
||||
CONF_SUBSCRIPTION,
|
||||
DATA_VULTR,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_NAME = "Vultr {}"
|
||||
PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_SUBSCRIPTION): cv.string,
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def setup_platform(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
add_entities: AddEntitiesCallback,
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up the Vultr subscription switch."""
|
||||
vultr = hass.data[DATA_VULTR]
|
||||
|
||||
subscription = config.get(CONF_SUBSCRIPTION)
|
||||
name = config.get(CONF_NAME)
|
||||
|
||||
if subscription not in vultr.data:
|
||||
_LOGGER.error("Subscription %s not found", subscription)
|
||||
return
|
||||
|
||||
add_entities([VultrSwitch(vultr, subscription, name)], True)
|
||||
|
||||
|
||||
class VultrSwitch(SwitchEntity):
|
||||
"""Representation of a Vultr subscription switch."""
|
||||
|
||||
def __init__(self, vultr, subscription, name):
|
||||
"""Initialize a new Vultr switch."""
|
||||
self._vultr = vultr
|
||||
self._name = name
|
||||
|
||||
self.subscription = subscription
|
||||
self.data = None
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the switch."""
|
||||
try:
|
||||
return self._name.format(self.data["label"])
|
||||
except (TypeError, KeyError):
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if switch is on."""
|
||||
return self.data["power_status"] == "running"
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Return the icon of this server."""
|
||||
return "mdi:server" if self.is_on else "mdi:server-off"
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self):
|
||||
"""Return the state attributes of the Vultr subscription."""
|
||||
return {
|
||||
ATTR_ALLOWED_BANDWIDTH: self.data.get("allowed_bandwidth_gb"),
|
||||
ATTR_AUTO_BACKUPS: self.data.get("auto_backups"),
|
||||
ATTR_COST_PER_MONTH: self.data.get("cost_per_month"),
|
||||
ATTR_CREATED_AT: self.data.get("date_created"),
|
||||
ATTR_DISK: self.data.get("disk"),
|
||||
ATTR_IPV4_ADDRESS: self.data.get("main_ip"),
|
||||
ATTR_IPV6_ADDRESS: self.data.get("v6_main_ip"),
|
||||
ATTR_MEMORY: self.data.get("ram"),
|
||||
ATTR_OS: self.data.get("os"),
|
||||
ATTR_REGION: self.data.get("location"),
|
||||
ATTR_SUBSCRIPTION_ID: self.data.get("SUBID"),
|
||||
ATTR_SUBSCRIPTION_NAME: self.data.get("label"),
|
||||
ATTR_VCPUS: self.data.get("vcpu_count"),
|
||||
}
|
||||
|
||||
def turn_on(self, **kwargs: Any) -> None:
|
||||
"""Boot-up the subscription."""
|
||||
if self.data["power_status"] != "running":
|
||||
self._vultr.start(self.subscription)
|
||||
|
||||
def turn_off(self, **kwargs: Any) -> None:
|
||||
"""Halt the subscription."""
|
||||
if self.data["power_status"] == "running":
|
||||
self._vultr.halt(self.subscription)
|
||||
|
||||
def update(self) -> None:
|
||||
"""Get the latest data from the device and update the data."""
|
||||
self._vultr.update()
|
||||
self.data = self._vultr.data[self.subscription]
|
||||
@@ -7411,12 +7411,6 @@
|
||||
"config_flow": true,
|
||||
"iot_class": "cloud_polling"
|
||||
},
|
||||
"vultr": {
|
||||
"name": "Vultr",
|
||||
"integration_type": "hub",
|
||||
"config_flow": false,
|
||||
"iot_class": "cloud_polling"
|
||||
},
|
||||
"w800rf32": {
|
||||
"name": "WGL Designs W800RF32",
|
||||
"integration_type": "hub",
|
||||
|
||||
3
requirements_all.txt
generated
3
requirements_all.txt
generated
@@ -3118,9 +3118,6 @@ vsure==2.6.7
|
||||
# homeassistant.components.vasttrafik
|
||||
vtjp==0.2.1
|
||||
|
||||
# homeassistant.components.vultr
|
||||
vultr==0.1.2
|
||||
|
||||
# homeassistant.components.samsungtv
|
||||
# homeassistant.components.wake_on_lan
|
||||
wakeonlan==3.1.0
|
||||
|
||||
3
requirements_test_all.txt
generated
3
requirements_test_all.txt
generated
@@ -2583,9 +2583,6 @@ volvocarsapi==0.4.2
|
||||
# homeassistant.components.verisure
|
||||
vsure==2.6.7
|
||||
|
||||
# homeassistant.components.vultr
|
||||
vultr==0.1.2
|
||||
|
||||
# homeassistant.components.samsungtv
|
||||
# homeassistant.components.wake_on_lan
|
||||
wakeonlan==3.1.0
|
||||
|
||||
@@ -1062,7 +1062,6 @@ INTEGRATIONS_WITHOUT_QUALITY_SCALE_FILE = [
|
||||
"volkszaehler",
|
||||
"volumio",
|
||||
"volvooncall",
|
||||
"vultr",
|
||||
"w800rf32",
|
||||
"wake_on_lan",
|
||||
"wallbox",
|
||||
@@ -2112,7 +2111,6 @@ INTEGRATIONS_WITHOUT_SCALE = [
|
||||
"volkszaehler",
|
||||
"volumio",
|
||||
"volvooncall",
|
||||
"vultr",
|
||||
"w800rf32",
|
||||
"wake_on_lan",
|
||||
"wallbox",
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
"""Tests for the vultr component."""
|
||||
@@ -1,30 +0,0 @@
|
||||
"""Test configuration for the Vultr tests."""
|
||||
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from requests_mock import Mocker
|
||||
|
||||
from homeassistant.components import vultr
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import VALID_CONFIG
|
||||
|
||||
from tests.common import load_fixture
|
||||
|
||||
|
||||
@pytest.fixture(name="valid_config")
|
||||
def valid_config(hass: HomeAssistant, requests_mock: Mocker) -> None:
|
||||
"""Load a valid config."""
|
||||
requests_mock.get(
|
||||
"https://api.vultr.com/v1/account/info?api_key=ABCDEFG1234567",
|
||||
text=load_fixture("account_info.json", "vultr"),
|
||||
)
|
||||
|
||||
with patch(
|
||||
"vultr.Vultr.server_list",
|
||||
return_value=json.loads(load_fixture("server_list.json", "vultr")),
|
||||
):
|
||||
# Setup hub
|
||||
vultr.setup(hass, VALID_CONFIG)
|
||||
@@ -1,3 +0,0 @@
|
||||
"""Constants for the Vultr tests."""
|
||||
|
||||
VALID_CONFIG = {"vultr": {"api_key": "ABCDEFG1234567"}}
|
||||
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"balance": "-123.00",
|
||||
"pending_charges": "3.38",
|
||||
"last_payment_date": "2017-08-11 15:04:04",
|
||||
"last_payment_amount": "-10.00"
|
||||
}
|
||||
@@ -1,122 +0,0 @@
|
||||
{
|
||||
"576965": {
|
||||
"SUBID": "576965",
|
||||
"os": "CentOS 6 x64",
|
||||
"ram": "4096 MB",
|
||||
"disk": "Virtual 60 GB",
|
||||
"main_ip": "123.123.123.123",
|
||||
"vcpu_count": "2",
|
||||
"location": "New Jersey",
|
||||
"DCID": "1",
|
||||
"default_password": "nreqnusibni",
|
||||
"date_created": "2013-12-19 14:45:41",
|
||||
"pending_charges": "46.67",
|
||||
"status": "active",
|
||||
"cost_per_month": "10.05",
|
||||
"current_bandwidth_gb": 131.512,
|
||||
"allowed_bandwidth_gb": "1000",
|
||||
"netmask_v4": "255.255.255.248",
|
||||
"gateway_v4": "123.123.123.1",
|
||||
"power_status": "running",
|
||||
"server_state": "ok",
|
||||
"VPSPLANID": "28",
|
||||
"v6_network": "2001:DB8:1000::",
|
||||
"v6_main_ip": "2001:DB8:1000::100",
|
||||
"v6_network_size": "64",
|
||||
"v6_networks": [
|
||||
{
|
||||
"v6_network": "2001:DB8:1000::",
|
||||
"v6_main_ip": "2001:DB8:1000::100",
|
||||
"v6_network_size": "64"
|
||||
}
|
||||
],
|
||||
"label": "my new server",
|
||||
"internal_ip": "10.99.0.10",
|
||||
"kvm_url": "https://my.vultr.com/subs/novnc/api.php?data=eawxFVZw2mXnhGUV",
|
||||
"auto_backups": "yes",
|
||||
"tag": "mytag",
|
||||
"OSID": "127",
|
||||
"APPID": "0",
|
||||
"FIREWALLGROUPID": "0"
|
||||
},
|
||||
"123456": {
|
||||
"SUBID": "123456",
|
||||
"os": "CentOS 6 x64",
|
||||
"ram": "4096 MB",
|
||||
"disk": "Virtual 60 GB",
|
||||
"main_ip": "192.168.100.50",
|
||||
"vcpu_count": "2",
|
||||
"location": "New Jersey",
|
||||
"DCID": "1",
|
||||
"default_password": "nreqnusibni",
|
||||
"date_created": "2014-10-13 14:45:41",
|
||||
"pending_charges": "3.72",
|
||||
"status": "active",
|
||||
"cost_per_month": "73.25",
|
||||
"current_bandwidth_gb": 957.457,
|
||||
"allowed_bandwidth_gb": "1000",
|
||||
"netmask_v4": "255.255.255.248",
|
||||
"gateway_v4": "123.123.123.1",
|
||||
"power_status": "halted",
|
||||
"server_state": "ok",
|
||||
"VPSPLANID": "28",
|
||||
"v6_network": "2001:DB8:1000::",
|
||||
"v6_main_ip": "2001:DB8:1000::100",
|
||||
"v6_network_size": "64",
|
||||
"v6_networks": [
|
||||
{
|
||||
"v6_network": "2001:DB8:1000::",
|
||||
"v6_main_ip": "2001:DB8:1000::100",
|
||||
"v6_network_size": "64"
|
||||
}
|
||||
],
|
||||
"label": "my failed server",
|
||||
"internal_ip": "10.99.0.10",
|
||||
"kvm_url": "https://my.vultr.com/subs/novnc/api.php?data=eawxFVZw2mXnhGUV",
|
||||
"auto_backups": "no",
|
||||
"tag": "mytag",
|
||||
"OSID": "127",
|
||||
"APPID": "0",
|
||||
"FIREWALLGROUPID": "0"
|
||||
},
|
||||
"555555": {
|
||||
"SUBID": "555555",
|
||||
"os": "CentOS 7 x64",
|
||||
"ram": "1024 MB",
|
||||
"disk": "Virtual 30 GB",
|
||||
"main_ip": "192.168.250.50",
|
||||
"vcpu_count": "1",
|
||||
"location": "London",
|
||||
"DCID": "7",
|
||||
"default_password": "password",
|
||||
"date_created": "2014-10-15 14:45:41",
|
||||
"pending_charges": "5.45",
|
||||
"status": "active",
|
||||
"cost_per_month": "73.25",
|
||||
"current_bandwidth_gb": 57.457,
|
||||
"allowed_bandwidth_gb": "100",
|
||||
"netmask_v4": "255.255.255.248",
|
||||
"gateway_v4": "123.123.123.1",
|
||||
"power_status": "halted",
|
||||
"server_state": "ok",
|
||||
"VPSPLANID": "28",
|
||||
"v6_network": "2001:DB8:1000::",
|
||||
"v6_main_ip": "2001:DB8:1000::100",
|
||||
"v6_network_size": "64",
|
||||
"v6_networks": [
|
||||
{
|
||||
"v6_network": "2001:DB8:1000::",
|
||||
"v6_main_ip": "2001:DB8:1000::100",
|
||||
"v6_network_size": "64"
|
||||
}
|
||||
],
|
||||
"label": "Another Server",
|
||||
"internal_ip": "10.99.0.10",
|
||||
"kvm_url": "https://my.vultr.com/subs/novnc/api.php?data=eawxFVZw2mXnhGUV",
|
||||
"auto_backups": "no",
|
||||
"tag": "mytag",
|
||||
"OSID": "127",
|
||||
"APPID": "0",
|
||||
"FIREWALLGROUPID": "0"
|
||||
}
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
"""Test the Vultr binary sensor platform."""
|
||||
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import vultr as base_vultr
|
||||
from homeassistant.components.vultr import (
|
||||
ATTR_ALLOWED_BANDWIDTH,
|
||||
ATTR_AUTO_BACKUPS,
|
||||
ATTR_COST_PER_MONTH,
|
||||
ATTR_CREATED_AT,
|
||||
ATTR_IPV4_ADDRESS,
|
||||
ATTR_SUBSCRIPTION_ID,
|
||||
CONF_SUBSCRIPTION,
|
||||
binary_sensor as vultr,
|
||||
)
|
||||
from homeassistant.const import CONF_NAME, CONF_PLATFORM
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
CONFIGS = [
|
||||
{CONF_SUBSCRIPTION: "576965", CONF_NAME: "A Server"},
|
||||
{CONF_SUBSCRIPTION: "123456", CONF_NAME: "Failed Server"},
|
||||
{CONF_SUBSCRIPTION: "555555", CONF_NAME: vultr.DEFAULT_NAME},
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("valid_config")
|
||||
def test_binary_sensor(hass: HomeAssistant) -> None:
|
||||
"""Test successful instance."""
|
||||
hass_devices = []
|
||||
|
||||
def add_entities(devices, action):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
device.hass = hass
|
||||
hass_devices.append(device)
|
||||
|
||||
# Setup each of our test configs
|
||||
for config in CONFIGS:
|
||||
vultr.setup_platform(hass, config, add_entities, None)
|
||||
|
||||
assert len(hass_devices) == 3
|
||||
|
||||
for device in hass_devices:
|
||||
# Test pre data retrieval
|
||||
if device.subscription == "555555":
|
||||
assert device.name == "Vultr {}"
|
||||
|
||||
device.update()
|
||||
device_attrs = device.extra_state_attributes
|
||||
|
||||
if device.subscription == "555555":
|
||||
assert device.name == "Vultr Another Server"
|
||||
|
||||
if device.name == "A Server":
|
||||
assert device.is_on is True
|
||||
assert device.device_class == "power"
|
||||
assert device.state == "on"
|
||||
assert device.icon == "mdi:server"
|
||||
assert device_attrs[ATTR_ALLOWED_BANDWIDTH] == "1000"
|
||||
assert device_attrs[ATTR_AUTO_BACKUPS] == "yes"
|
||||
assert device_attrs[ATTR_IPV4_ADDRESS] == "123.123.123.123"
|
||||
assert device_attrs[ATTR_COST_PER_MONTH] == "10.05"
|
||||
assert device_attrs[ATTR_CREATED_AT] == "2013-12-19 14:45:41"
|
||||
assert device_attrs[ATTR_SUBSCRIPTION_ID] == "576965"
|
||||
elif device.name == "Failed Server":
|
||||
assert device.is_on is False
|
||||
assert device.state == "off"
|
||||
assert device.icon == "mdi:server-off"
|
||||
assert device_attrs[ATTR_ALLOWED_BANDWIDTH] == "1000"
|
||||
assert device_attrs[ATTR_AUTO_BACKUPS] == "no"
|
||||
assert device_attrs[ATTR_IPV4_ADDRESS] == "192.168.100.50"
|
||||
assert device_attrs[ATTR_COST_PER_MONTH] == "73.25"
|
||||
assert device_attrs[ATTR_CREATED_AT] == "2014-10-13 14:45:41"
|
||||
assert device_attrs[ATTR_SUBSCRIPTION_ID] == "123456"
|
||||
|
||||
|
||||
def test_invalid_sensor_config() -> None:
|
||||
"""Test config type failures."""
|
||||
with pytest.raises(vol.Invalid): # No subs
|
||||
vultr.PLATFORM_SCHEMA({CONF_PLATFORM: base_vultr.DOMAIN})
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("valid_config")
|
||||
def test_invalid_sensors(hass: HomeAssistant) -> None:
|
||||
"""Test the VultrBinarySensor fails."""
|
||||
hass_devices = []
|
||||
|
||||
def add_entities(devices, action):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
device.hass = hass
|
||||
hass_devices.append(device)
|
||||
|
||||
bad_conf = {} # No subscription
|
||||
|
||||
vultr.setup_platform(hass, bad_conf, add_entities, None)
|
||||
|
||||
bad_conf = {
|
||||
CONF_NAME: "Missing Server",
|
||||
CONF_SUBSCRIPTION: "555555",
|
||||
} # Sub not associated with API key (not in server_list)
|
||||
|
||||
vultr.setup_platform(hass, bad_conf, add_entities, None)
|
||||
@@ -1,30 +0,0 @@
|
||||
"""The tests for the Vultr component."""
|
||||
|
||||
from copy import deepcopy
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant import setup
|
||||
from homeassistant.components import vultr
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import VALID_CONFIG
|
||||
|
||||
from tests.common import load_fixture
|
||||
|
||||
|
||||
def test_setup(hass: HomeAssistant) -> None:
|
||||
"""Test successful setup."""
|
||||
with patch(
|
||||
"vultr.Vultr.server_list",
|
||||
return_value=json.loads(load_fixture("server_list.json", "vultr")),
|
||||
):
|
||||
response = vultr.setup(hass, VALID_CONFIG)
|
||||
assert response
|
||||
|
||||
|
||||
async def test_setup_no_api_key(hass: HomeAssistant) -> None:
|
||||
"""Test failed setup with missing API Key."""
|
||||
conf = deepcopy(VALID_CONFIG)
|
||||
del conf["vultr"]["api_key"]
|
||||
assert not await setup.async_setup_component(hass, vultr.DOMAIN, conf)
|
||||
@@ -1,134 +0,0 @@
|
||||
"""The tests for the Vultr sensor platform."""
|
||||
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import vultr as base_vultr
|
||||
from homeassistant.components.vultr import CONF_SUBSCRIPTION, sensor as vultr
|
||||
from homeassistant.const import (
|
||||
CONF_MONITORED_CONDITIONS,
|
||||
CONF_NAME,
|
||||
CONF_PLATFORM,
|
||||
UnitOfInformation,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
CONFIGS = [
|
||||
{
|
||||
CONF_NAME: vultr.DEFAULT_NAME,
|
||||
CONF_SUBSCRIPTION: "576965",
|
||||
CONF_MONITORED_CONDITIONS: vultr.SENSOR_KEYS,
|
||||
},
|
||||
{
|
||||
CONF_NAME: "Server {}",
|
||||
CONF_SUBSCRIPTION: "123456",
|
||||
CONF_MONITORED_CONDITIONS: vultr.SENSOR_KEYS,
|
||||
},
|
||||
{
|
||||
CONF_NAME: "VPS Charges",
|
||||
CONF_SUBSCRIPTION: "555555",
|
||||
CONF_MONITORED_CONDITIONS: ["pending_charges"],
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("valid_config")
|
||||
def test_sensor(hass: HomeAssistant) -> None:
|
||||
"""Test the Vultr sensor class and methods."""
|
||||
hass_devices = []
|
||||
|
||||
def add_entities(devices, action):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
device.hass = hass
|
||||
hass_devices.append(device)
|
||||
|
||||
for config in CONFIGS:
|
||||
vultr.setup_platform(hass, config, add_entities, None)
|
||||
|
||||
assert len(hass_devices) == 5
|
||||
|
||||
tested = 0
|
||||
|
||||
for device in hass_devices:
|
||||
# Test pre update
|
||||
if device.subscription == "576965":
|
||||
assert device.name == vultr.DEFAULT_NAME
|
||||
|
||||
device.update()
|
||||
|
||||
if (
|
||||
device.unit_of_measurement == UnitOfInformation.GIGABYTES
|
||||
): # Test Bandwidth Used
|
||||
if device.subscription == "576965":
|
||||
assert device.name == "Vultr my new server Current Bandwidth Used"
|
||||
assert device.icon == "mdi:chart-histogram"
|
||||
assert device.state == 131.51
|
||||
assert device.icon == "mdi:chart-histogram"
|
||||
tested += 1
|
||||
|
||||
elif device.subscription == "123456":
|
||||
assert device.name == "Server Current Bandwidth Used"
|
||||
assert device.state == 957.46
|
||||
tested += 1
|
||||
|
||||
elif device.unit_of_measurement == "US$": # Test Pending Charges
|
||||
if device.subscription == "576965": # Default 'Vultr {} {}'
|
||||
assert device.name == "Vultr my new server Pending Charges"
|
||||
assert device.icon == "mdi:currency-usd"
|
||||
assert device.state == 46.67
|
||||
assert device.icon == "mdi:currency-usd"
|
||||
tested += 1
|
||||
|
||||
elif device.subscription == "123456": # Custom name with 1 {}
|
||||
assert device.name == "Server Pending Charges"
|
||||
assert device.state == 3.72
|
||||
tested += 1
|
||||
|
||||
elif device.subscription == "555555": # No {} in name
|
||||
assert device.name == "VPS Charges"
|
||||
assert device.state == 5.45
|
||||
tested += 1
|
||||
|
||||
assert tested == 5
|
||||
|
||||
|
||||
def test_invalid_sensor_config() -> None:
|
||||
"""Test config type failures."""
|
||||
with pytest.raises(vol.Invalid): # No subscription
|
||||
vultr.PLATFORM_SCHEMA(
|
||||
{
|
||||
CONF_PLATFORM: base_vultr.DOMAIN,
|
||||
CONF_MONITORED_CONDITIONS: vultr.SENSOR_KEYS,
|
||||
}
|
||||
)
|
||||
with pytest.raises(vol.Invalid): # Bad monitored_conditions
|
||||
vultr.PLATFORM_SCHEMA(
|
||||
{
|
||||
CONF_PLATFORM: base_vultr.DOMAIN,
|
||||
CONF_SUBSCRIPTION: "123456",
|
||||
CONF_MONITORED_CONDITIONS: ["non-existent-condition"],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("valid_config")
|
||||
def test_invalid_sensors(hass: HomeAssistant) -> None:
|
||||
"""Test the VultrSensor fails."""
|
||||
hass_devices = []
|
||||
|
||||
def add_entities(devices, action):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
device.hass = hass
|
||||
hass_devices.append(device)
|
||||
|
||||
bad_conf = {
|
||||
CONF_NAME: "Vultr {} {}",
|
||||
CONF_SUBSCRIPTION: "",
|
||||
CONF_MONITORED_CONDITIONS: vultr.SENSOR_KEYS,
|
||||
} # No subs at all
|
||||
|
||||
vultr.setup_platform(hass, bad_conf, add_entities, None)
|
||||
|
||||
assert len(hass_devices) == 0
|
||||
@@ -1,161 +0,0 @@
|
||||
"""Test the Vultr switch platform."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import vultr as base_vultr
|
||||
from homeassistant.components.vultr import (
|
||||
ATTR_ALLOWED_BANDWIDTH,
|
||||
ATTR_AUTO_BACKUPS,
|
||||
ATTR_COST_PER_MONTH,
|
||||
ATTR_CREATED_AT,
|
||||
ATTR_IPV4_ADDRESS,
|
||||
ATTR_SUBSCRIPTION_ID,
|
||||
CONF_SUBSCRIPTION,
|
||||
switch as vultr,
|
||||
)
|
||||
from homeassistant.const import CONF_NAME, CONF_PLATFORM
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import load_fixture
|
||||
|
||||
CONFIGS = [
|
||||
{CONF_SUBSCRIPTION: "576965", CONF_NAME: "A Server"},
|
||||
{CONF_SUBSCRIPTION: "123456", CONF_NAME: "Failed Server"},
|
||||
{CONF_SUBSCRIPTION: "555555", CONF_NAME: vultr.DEFAULT_NAME},
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture(name="hass_devices")
|
||||
def load_hass_devices(hass: HomeAssistant):
|
||||
"""Load a valid config."""
|
||||
hass_devices = []
|
||||
|
||||
def add_entities(devices, action):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
device.hass = hass
|
||||
hass_devices.append(device)
|
||||
|
||||
# Setup each of our test configs
|
||||
for config in CONFIGS:
|
||||
vultr.setup_platform(hass, config, add_entities, None)
|
||||
|
||||
return hass_devices
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("valid_config")
|
||||
def test_switch(hass: HomeAssistant, hass_devices: list[vultr.VultrSwitch]) -> None:
|
||||
"""Test successful instance."""
|
||||
|
||||
assert len(hass_devices) == 3
|
||||
|
||||
tested = 0
|
||||
|
||||
for device in hass_devices:
|
||||
if device.subscription == "555555":
|
||||
assert device.name == "Vultr {}"
|
||||
tested += 1
|
||||
|
||||
device.update()
|
||||
device_attrs = device.extra_state_attributes
|
||||
|
||||
if device.subscription == "555555":
|
||||
assert device.name == "Vultr Another Server"
|
||||
tested += 1
|
||||
|
||||
if device.name == "A Server":
|
||||
assert device.is_on is True
|
||||
assert device.state == "on"
|
||||
assert device.icon == "mdi:server"
|
||||
assert device_attrs[ATTR_ALLOWED_BANDWIDTH] == "1000"
|
||||
assert device_attrs[ATTR_AUTO_BACKUPS] == "yes"
|
||||
assert device_attrs[ATTR_IPV4_ADDRESS] == "123.123.123.123"
|
||||
assert device_attrs[ATTR_COST_PER_MONTH] == "10.05"
|
||||
assert device_attrs[ATTR_CREATED_AT] == "2013-12-19 14:45:41"
|
||||
assert device_attrs[ATTR_SUBSCRIPTION_ID] == "576965"
|
||||
tested += 1
|
||||
|
||||
elif device.name == "Failed Server":
|
||||
assert device.is_on is False
|
||||
assert device.state == "off"
|
||||
assert device.icon == "mdi:server-off"
|
||||
assert device_attrs[ATTR_ALLOWED_BANDWIDTH] == "1000"
|
||||
assert device_attrs[ATTR_AUTO_BACKUPS] == "no"
|
||||
assert device_attrs[ATTR_IPV4_ADDRESS] == "192.168.100.50"
|
||||
assert device_attrs[ATTR_COST_PER_MONTH] == "73.25"
|
||||
assert device_attrs[ATTR_CREATED_AT] == "2014-10-13 14:45:41"
|
||||
assert device_attrs[ATTR_SUBSCRIPTION_ID] == "123456"
|
||||
tested += 1
|
||||
|
||||
assert tested == 4
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("valid_config")
|
||||
def test_turn_on(hass: HomeAssistant, hass_devices: list[vultr.VultrSwitch]) -> None:
|
||||
"""Test turning a subscription on."""
|
||||
with (
|
||||
patch(
|
||||
"vultr.Vultr.server_list",
|
||||
return_value=json.loads(load_fixture("server_list.json", "vultr")),
|
||||
),
|
||||
patch("vultr.Vultr.server_start") as mock_start,
|
||||
):
|
||||
for device in hass_devices:
|
||||
if device.name == "Failed Server":
|
||||
device.update()
|
||||
device.turn_on()
|
||||
|
||||
# Turn on
|
||||
assert mock_start.call_count == 1
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("valid_config")
|
||||
def test_turn_off(hass: HomeAssistant, hass_devices: list[vultr.VultrSwitch]) -> None:
|
||||
"""Test turning a subscription off."""
|
||||
with (
|
||||
patch(
|
||||
"vultr.Vultr.server_list",
|
||||
return_value=json.loads(load_fixture("server_list.json", "vultr")),
|
||||
),
|
||||
patch("vultr.Vultr.server_halt") as mock_halt,
|
||||
):
|
||||
for device in hass_devices:
|
||||
if device.name == "A Server":
|
||||
device.update()
|
||||
device.turn_off()
|
||||
|
||||
# Turn off
|
||||
assert mock_halt.call_count == 1
|
||||
|
||||
|
||||
def test_invalid_switch_config() -> None:
|
||||
"""Test config type failures."""
|
||||
with pytest.raises(vol.Invalid): # No subscription
|
||||
vultr.PLATFORM_SCHEMA({CONF_PLATFORM: base_vultr.DOMAIN})
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("valid_config")
|
||||
def test_invalid_switches(hass: HomeAssistant) -> None:
|
||||
"""Test the VultrSwitch fails."""
|
||||
hass_devices = []
|
||||
|
||||
def add_entities(devices, action):
|
||||
"""Mock add devices."""
|
||||
hass_devices.extend(devices)
|
||||
|
||||
bad_conf = {} # No subscription
|
||||
|
||||
vultr.setup_platform(hass, bad_conf, add_entities, None)
|
||||
|
||||
bad_conf = {
|
||||
CONF_NAME: "Missing Server",
|
||||
CONF_SUBSCRIPTION: "665544",
|
||||
} # Sub not associated with API key (not in server_list)
|
||||
|
||||
vultr.setup_platform(hass, bad_conf, add_entities, None)
|
||||
Reference in New Issue
Block a user