2019-02-13 20:21:14 +00:00
|
|
|
"""Support for interacting with Digital Ocean droplets."""
|
2016-09-30 16:30:44 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
|
2018-06-24 11:36:27 +00:00
|
|
|
from homeassistant.const import ATTR_ATTRIBUTION
|
2019-03-21 05:56:46 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
|
|
|
from . import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_CREATED_AT,
|
|
|
|
ATTR_DROPLET_ID,
|
|
|
|
ATTR_DROPLET_NAME,
|
|
|
|
ATTR_FEATURES,
|
|
|
|
ATTR_IPV4_ADDRESS,
|
|
|
|
ATTR_IPV6_ADDRESS,
|
|
|
|
ATTR_MEMORY,
|
|
|
|
ATTR_REGION,
|
|
|
|
ATTR_VCPUS,
|
|
|
|
ATTRIBUTION,
|
|
|
|
CONF_DROPLETS,
|
|
|
|
DATA_DIGITAL_OCEAN,
|
|
|
|
)
|
2016-09-30 16:30:44 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "Droplet"
|
2016-09-30 16:30:44 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Required(CONF_DROPLETS): vol.All(cv.ensure_list, [cv.string])}
|
|
|
|
)
|
2016-09-30 16:30:44 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Digital Ocean droplet switch."""
|
2017-06-30 06:46:03 +00:00
|
|
|
digital = hass.data.get(DATA_DIGITAL_OCEAN)
|
|
|
|
if not digital:
|
|
|
|
return False
|
|
|
|
|
2020-04-15 12:10:07 +00:00
|
|
|
droplets = config[CONF_DROPLETS]
|
2016-09-30 16:30:44 +00:00
|
|
|
|
|
|
|
dev = []
|
|
|
|
for droplet in droplets:
|
2017-06-30 06:46:03 +00:00
|
|
|
droplet_id = digital.get_droplet_id(droplet)
|
2017-03-16 18:59:34 +00:00
|
|
|
if droplet_id is None:
|
|
|
|
_LOGGER.error("Droplet %s is not available", droplet)
|
|
|
|
return False
|
2017-06-30 06:46:03 +00:00
|
|
|
dev.append(DigitalOceanSwitch(digital, droplet_id))
|
2016-09-30 16:30:44 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(dev, True)
|
2016-09-30 16:30:44 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class DigitalOceanSwitch(SwitchEntity):
|
2016-09-30 16:30:44 +00:00
|
|
|
"""Representation of a Digital Ocean droplet switch."""
|
|
|
|
|
|
|
|
def __init__(self, do, droplet_id):
|
|
|
|
"""Initialize a new Digital Ocean sensor."""
|
|
|
|
self._digital_ocean = do
|
|
|
|
self._droplet_id = droplet_id
|
|
|
|
self.data = None
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the switch."""
|
|
|
|
return self.data.name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if switch is on."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.data.status == "active"
|
2016-09-30 16:30:44 +00:00
|
|
|
|
|
|
|
@property
|
2017-01-23 21:21:12 +00:00
|
|
|
def device_state_attributes(self):
|
2016-09-30 16:30:44 +00:00
|
|
|
"""Return the state attributes of the Digital Ocean droplet."""
|
|
|
|
return {
|
2019-02-14 21:09:22 +00:00
|
|
|
ATTR_ATTRIBUTION: ATTRIBUTION,
|
2016-09-30 16:30:44 +00:00
|
|
|
ATTR_CREATED_AT: self.data.created_at,
|
|
|
|
ATTR_DROPLET_ID: self.data.id,
|
|
|
|
ATTR_DROPLET_NAME: self.data.name,
|
|
|
|
ATTR_FEATURES: self.data.features,
|
|
|
|
ATTR_IPV4_ADDRESS: self.data.ip_address,
|
|
|
|
ATTR_IPV6_ADDRESS: self.data.ip_v6_address,
|
|
|
|
ATTR_MEMORY: self.data.memory,
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_REGION: self.data.region["name"],
|
2016-09-30 16:30:44 +00:00
|
|
|
ATTR_VCPUS: self.data.vcpus,
|
|
|
|
}
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Boot-up the droplet."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if self.data.status != "active":
|
2016-09-30 16:30:44 +00:00
|
|
|
self.data.power_on()
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Shutdown the droplet."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if self.data.status == "active":
|
2016-09-30 16:30:44 +00:00
|
|
|
self.data.power_off()
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data from the device and update the data."""
|
|
|
|
self._digital_ocean.update()
|
|
|
|
|
|
|
|
for droplet in self._digital_ocean.data:
|
|
|
|
if droplet.id == self._droplet_id:
|
|
|
|
self.data = droplet
|