Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Digital Ocean."""
2 
3 from datetime import timedelta
4 import logging
5 
6 import digitalocean
7 import voluptuous as vol
8 
9 from homeassistant.const import CONF_ACCESS_TOKEN, Platform
10 from homeassistant.core import HomeAssistant
12 from homeassistant.helpers.typing import ConfigType
13 from homeassistant.util import Throttle
14 
15 _LOGGER = logging.getLogger(__name__)
16 
17 ATTR_CREATED_AT = "created_at"
18 ATTR_DROPLET_ID = "droplet_id"
19 ATTR_DROPLET_NAME = "droplet_name"
20 ATTR_FEATURES = "features"
21 ATTR_IPV4_ADDRESS = "ipv4_address"
22 ATTR_IPV6_ADDRESS = "ipv6_address"
23 ATTR_MEMORY = "memory"
24 ATTR_REGION = "region"
25 ATTR_VCPUS = "vcpus"
26 
27 ATTRIBUTION = "Data provided by Digital Ocean"
28 
29 CONF_DROPLETS = "droplets"
30 
31 DATA_DIGITAL_OCEAN = "data_do"
32 DIGITAL_OCEAN_PLATFORMS = [Platform.SWITCH, Platform.BINARY_SENSOR]
33 DOMAIN = "digital_ocean"
34 
35 MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
36 
37 CONFIG_SCHEMA = vol.Schema(
38  {DOMAIN: vol.Schema({vol.Required(CONF_ACCESS_TOKEN): cv.string})},
39  extra=vol.ALLOW_EXTRA,
40 )
41 
42 
43 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
44  """Set up the Digital Ocean component."""
45 
46  conf = config[DOMAIN]
47  access_token = conf[CONF_ACCESS_TOKEN]
48 
49  digital = DigitalOcean(access_token)
50 
51  try:
52  if not digital.manager.get_account():
53  _LOGGER.error("No account found for the given API token")
54  return False
55  except digitalocean.baseapi.DataReadError:
56  _LOGGER.error("API token not valid for authentication")
57  return False
58 
59  hass.data[DATA_DIGITAL_OCEAN] = digital
60 
61  return True
62 
63 
65  """Handle all communication with the Digital Ocean API."""
66 
67  def __init__(self, access_token):
68  """Initialize the Digital Ocean connection."""
69 
70  self._access_token_access_token = access_token
71  self.datadata = None
72  self.managermanager = digitalocean.Manager(token=self._access_token_access_token)
73 
74  def get_droplet_id(self, droplet_name):
75  """Get the status of a Digital Ocean droplet."""
76  droplet_id = None
77 
78  all_droplets = self.managermanager.get_all_droplets()
79  for droplet in all_droplets:
80  if droplet_name == droplet.name:
81  droplet_id = droplet.id
82 
83  return droplet_id
84 
85  @Throttle(MIN_TIME_BETWEEN_UPDATES)
86  def update(self):
87  """Use the data from Digital Ocean API."""
88  self.datadata = self.managermanager.get_all_droplets()
bool setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:43