Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Linode."""
2 
3 from datetime import timedelta
4 import logging
5 
6 import linode
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 = "created"
18 ATTR_NODE_ID = "node_id"
19 ATTR_NODE_NAME = "node_name"
20 ATTR_IPV4_ADDRESS = "ipv4_address"
21 ATTR_IPV6_ADDRESS = "ipv6_address"
22 ATTR_MEMORY = "memory"
23 ATTR_REGION = "region"
24 ATTR_VCPUS = "vcpus"
25 
26 CONF_NODES = "nodes"
27 
28 DATA_LINODE = "data_li"
29 LINODE_PLATFORMS = [Platform.BINARY_SENSOR, Platform.SWITCH]
30 DOMAIN = "linode"
31 
32 MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
33 
34 CONFIG_SCHEMA = vol.Schema(
35  {DOMAIN: vol.Schema({vol.Required(CONF_ACCESS_TOKEN): cv.string})},
36  extra=vol.ALLOW_EXTRA,
37 )
38 
39 
40 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
41  """Set up the Linode component."""
42  conf = config[DOMAIN]
43  access_token = conf.get(CONF_ACCESS_TOKEN)
44 
45  _linode = Linode(access_token)
46 
47  try:
48  _LOGGER.debug("Linode Profile %s", _linode.manager.get_profile().username)
49  except linode.errors.ApiError as _ex:
50  _LOGGER.error(_ex)
51  return False
52 
53  hass.data[DATA_LINODE] = _linode
54 
55  return True
56 
57 
58 class Linode:
59  """Handle all communication with the Linode API."""
60 
61  def __init__(self, access_token):
62  """Initialize the Linode connection."""
63  self._access_token_access_token = access_token
64  self.datadata = None
65  self.managermanager = linode.LinodeClient(token=self._access_token_access_token)
66 
67  def get_node_id(self, node_name):
68  """Get the status of a Linode Instance."""
69  node_id = None
70 
71  try:
72  all_nodes = self.managermanager.linode.get_instances()
73  for node in all_nodes:
74  if node_name == node.label:
75  node_id = node.id
76  except linode.errors.ApiError as _ex:
77  _LOGGER.error(_ex)
78 
79  return node_id
80 
81  @Throttle(MIN_TIME_BETWEEN_UPDATES)
82  def update(self):
83  """Use the data from Linode API."""
84  try:
85  self.datadata = self.managermanager.linode.get_instances()
86  except linode.errors.ApiError as _ex:
87  _LOGGER.error(_ex)
def __init__(self, access_token)
Definition: __init__.py:61
def get_node_id(self, node_name)
Definition: __init__.py:67
bool setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:40