2019-02-14 04:35:12 +00:00
|
|
|
"""Support for interacting with Linode nodes."""
|
2022-01-05 12:30:37 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-10-27 14:19:47 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
|
2022-01-05 12:30:37 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-03-21 05:56:46 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-05 12:30:37 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2019-03-21 05:56:46 +00:00
|
|
|
|
|
|
|
from . import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_CREATED,
|
|
|
|
ATTR_IPV4_ADDRESS,
|
|
|
|
ATTR_IPV6_ADDRESS,
|
|
|
|
ATTR_MEMORY,
|
|
|
|
ATTR_NODE_ID,
|
|
|
|
ATTR_NODE_NAME,
|
|
|
|
ATTR_REGION,
|
|
|
|
ATTR_VCPUS,
|
|
|
|
CONF_NODES,
|
|
|
|
DATA_LINODE,
|
|
|
|
)
|
2017-10-27 14:19:47 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "Node"
|
2017-10-27 14:19:47 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Required(CONF_NODES): vol.All(cv.ensure_list, [cv.string])}
|
|
|
|
)
|
2017-10-27 14:19:47 +00:00
|
|
|
|
|
|
|
|
2022-01-05 12:30:37 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-10-27 14:19:47 +00:00
|
|
|
"""Set up the Linode Node switch."""
|
2022-01-05 12:30:37 +00:00
|
|
|
linode = hass.data[DATA_LINODE]
|
|
|
|
nodes = config[CONF_NODES]
|
2017-10-27 14:19:47 +00:00
|
|
|
|
|
|
|
dev = []
|
|
|
|
for node in nodes:
|
2021-10-31 17:56:25 +00:00
|
|
|
if (node_id := linode.get_node_id(node)) is None:
|
2017-10-27 14:19:47 +00:00
|
|
|
_LOGGER.error("Node %s is not available", node)
|
|
|
|
return
|
|
|
|
dev.append(LinodeSwitch(linode, node_id))
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(dev, True)
|
2017-10-27 14:19:47 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class LinodeSwitch(SwitchEntity):
|
2017-10-27 14:19:47 +00:00
|
|
|
"""Representation of a Linode Node switch."""
|
|
|
|
|
2021-11-25 23:13:27 +00:00
|
|
|
def __init__(self, li, node_id): # pylint: disable=invalid-name
|
2017-10-27 14:19:47 +00:00
|
|
|
"""Initialize a new Linode sensor."""
|
|
|
|
self._linode = li
|
|
|
|
self._node_id = node_id
|
|
|
|
self.data = None
|
2021-12-20 13:17:23 +00:00
|
|
|
self._attr_extra_state_attributes = {}
|
2017-10-27 14:19:47 +00:00
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Boot-up the Node."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if self.data.status != "running":
|
2017-10-27 14:19:47 +00:00
|
|
|
self.data.boot()
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Shutdown the nodes."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if self.data.status == "running":
|
2017-10-27 14:19:47 +00:00
|
|
|
self.data.shutdown()
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data from the device and update the data."""
|
|
|
|
self._linode.update()
|
|
|
|
if self._linode.data is not None:
|
|
|
|
for node in self._linode.data:
|
|
|
|
if node.id == self._node_id:
|
|
|
|
self.data = node
|
2018-06-18 13:21:41 +00:00
|
|
|
if self.data is not None:
|
2021-12-20 13:17:23 +00:00
|
|
|
self._attr_is_on = self.data.status == "running"
|
|
|
|
self._attr_extra_state_attributes = {
|
2018-06-18 13:21:41 +00:00
|
|
|
ATTR_CREATED: self.data.created,
|
|
|
|
ATTR_NODE_ID: self.data.id,
|
|
|
|
ATTR_NODE_NAME: self.data.label,
|
|
|
|
ATTR_IPV4_ADDRESS: self.data.ipv4,
|
|
|
|
ATTR_IPV6_ADDRESS: self.data.ipv6,
|
|
|
|
ATTR_MEMORY: self.data.specs.memory,
|
|
|
|
ATTR_REGION: self.data.region.country,
|
|
|
|
ATTR_VCPUS: self.data.specs.vcpus,
|
|
|
|
}
|
2021-12-20 13:17:23 +00:00
|
|
|
self._attr_name = self.data.label
|