hass-core/homeassistant/components/linode.py
Ryan McLean 2d93285689 Linode (#9936)
* Fix: Last Played Media Title in plex would stay even when player was idle/off
     Primary Fix is in the "if self._device" portion.
     code in "if self._session" is a catch all but i'm not 100% if it is needed.

* Fixed lint issues with previous commit

* 1st Pass at refactoring plex refresh
Moved _media** into clearMedia() which is called in _init_ and
at start of refresh.

Removed redunant _media_* = None entries

Grouped TV Show and Music under single if rather than testing
seperately for now.

* Fixed invalid name for _clearMedia()
Removed another media_* = None entry

* Removed print() statements used for debug

* Removed unneeded "if" statement

* Added Base Support for Linode

* Removed unused Attr

* Corrected some Typos

* updated requirements & coveragerc

* added import to prevent var not declared errors in linter

* Fixed Typo

* Added Switch Component for Linode and corrected some errors if data was blank

* Updated api lib to hopefully remove dependancy on enum34

* Fixed Reference error

* fix pylint errors

* Update linode.py

* Update linode.py

* Update linode.py

* Update linode.py
2017-10-27 16:19:47 +02:00

98 lines
2.4 KiB
Python

"""
Support for Linode.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/linode/
"""
import logging
from datetime import timedelta
import voluptuous as vol
from homeassistant.const import CONF_ACCESS_TOKEN
from homeassistant.util import Throttle
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['linode-api==4.1.4b2']
_LOGGER = logging.getLogger(__name__)
ATTR_CREATED = 'created'
ATTR_NODE_ID = 'node_id'
ATTR_NODE_NAME = 'node_name'
ATTR_IPV4_ADDRESS = 'ipv4_address'
ATTR_IPV6_ADDRESS = 'ipv6_address'
ATTR_MEMORY = 'memory'
ATTR_REGION = 'region'
ATTR_VCPUS = 'vcpus'
CONF_NODES = 'nodes'
DATA_LINODE = 'data_li'
LINODE_PLATFORMS = ['binary_sensor', 'switch']
DOMAIN = 'linode'
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_ACCESS_TOKEN): cv.string,
}),
}, extra=vol.ALLOW_EXTRA)
def setup(hass, config):
"""Set up the Linode component."""
import linode
conf = config[DOMAIN]
access_token = conf.get(CONF_ACCESS_TOKEN)
_linode = Linode(access_token)
try:
_LOGGER.info("Linode Profile %s",
_linode.manager.get_profile().username)
except linode.errors.ApiError as _ex:
_LOGGER.error(_ex)
return False
hass.data[DATA_LINODE] = _linode
return True
class Linode(object):
"""Handle all communication with the Linode API."""
def __init__(self, access_token):
"""Initialize the Linode connection."""
import linode
self._access_token = access_token
self.data = None
self.manager = linode.LinodeClient(token=self._access_token)
def get_node_id(self, node_name):
"""Get the status of a Linode Instance."""
import linode
node_id = None
try:
all_nodes = self.manager.linode.get_instances()
for node in all_nodes:
if node_name == node.label:
node_id = node.id
except linode.errors.ApiError as _ex:
_LOGGER.error(_ex)
return node_id
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
"""Use the data from Linode API."""
import linode
try:
self.data = self.manager.linode.get_instances()
except linode.errors.ApiError as _ex:
_LOGGER.error(_ex)