Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Vultr."""
2 
3 from datetime import timedelta
4 import logging
5 
6 import voluptuous as vol
7 from vultr import Vultr as VultrAPI
8 
9 from homeassistant.components import persistent_notification
10 from homeassistant.const import CONF_API_KEY, Platform
11 from homeassistant.core import HomeAssistant
13 from homeassistant.helpers.typing import ConfigType
14 from homeassistant.util import Throttle
15 
16 _LOGGER = logging.getLogger(__name__)
17 
18 ATTR_AUTO_BACKUPS = "auto_backups"
19 ATTR_ALLOWED_BANDWIDTH = "allowed_bandwidth_gb"
20 ATTR_COST_PER_MONTH = "cost_per_month"
21 ATTR_CURRENT_BANDWIDTH_USED = "current_bandwidth_gb"
22 ATTR_CREATED_AT = "created_at"
23 ATTR_DISK = "disk"
24 ATTR_SUBSCRIPTION_ID = "subid"
25 ATTR_SUBSCRIPTION_NAME = "label"
26 ATTR_IPV4_ADDRESS = "ipv4_address"
27 ATTR_IPV6_ADDRESS = "ipv6_address"
28 ATTR_MEMORY = "memory"
29 ATTR_OS = "os"
30 ATTR_PENDING_CHARGES = "pending_charges"
31 ATTR_REGION = "region"
32 ATTR_VCPUS = "vcpus"
33 
34 CONF_SUBSCRIPTION = "subscription"
35 
36 DATA_VULTR = "data_vultr"
37 DOMAIN = "vultr"
38 
39 NOTIFICATION_ID = "vultr_notification"
40 NOTIFICATION_TITLE = "Vultr Setup"
41 
42 VULTR_PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR, Platform.SWITCH]
43 
44 MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
45 
46 CONFIG_SCHEMA = vol.Schema(
47  {DOMAIN: vol.Schema({vol.Required(CONF_API_KEY): cv.string})}, extra=vol.ALLOW_EXTRA
48 )
49 
50 
51 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
52  """Set up the Vultr component."""
53  api_key = config[DOMAIN].get(CONF_API_KEY)
54 
55  vultr = Vultr(api_key)
56 
57  try:
58  vultr.update()
59  except RuntimeError as ex:
60  _LOGGER.error("Failed to make update API request because: %s", ex)
61  persistent_notification.create(
62  hass,
63  f"Error: {ex}",
64  title=NOTIFICATION_TITLE,
65  notification_id=NOTIFICATION_ID,
66  )
67  return False
68 
69  hass.data[DATA_VULTR] = vultr
70  return True
71 
72 
73 class Vultr:
74  """Handle all communication with the Vultr API."""
75 
76  def __init__(self, api_key):
77  """Initialize the Vultr connection."""
78 
79  self._api_key_api_key = api_key
80  self.datadata = None
81  self.apiapi = VultrAPI(self._api_key_api_key)
82 
83  @Throttle(MIN_TIME_BETWEEN_UPDATES)
84  def update(self):
85  """Use the data from Vultr API."""
86  self.datadata = self.apiapi.server_list()
87 
88  def _force_update(self):
89  """Use the data from Vultr API."""
90  self.datadata = self.apiapi.server_list()
91 
92  def halt(self, subscription):
93  """Halt a subscription (hard power off)."""
94  self.apiapi.server_halt(subscription)
95  self._force_update_force_update()
96 
97  def start(self, subscription):
98  """Start a subscription."""
99  self.apiapi.server_start(subscription)
100  self._force_update_force_update()
def start(self, subscription)
Definition: __init__.py:97
def halt(self, subscription)
Definition: __init__.py:92
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
bool setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:51