Files
core/homeassistant/components/linode/binary_sensor.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

94 lines
2.6 KiB
Python
Raw Normal View History

2019-02-14 05:35:12 +01:00
"""Support for monitoring the state of Linode Nodes."""
2022-01-05 13:30:37 +01:00
from __future__ import annotations
2017-10-27 15:19:47 +01:00
import logging
import voluptuous as vol
from homeassistant.components.binary_sensor import (
PLATFORM_SCHEMA as BINARY_SENSOR_PLATFORM_SCHEMA,
2021-12-20 14:17:23 +01:00
BinarySensorDeviceClass,
BinarySensorEntity,
)
2022-01-05 13:30:37 +01:00
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
2022-01-05 13:30:37 +01:00
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import (
2019-02-14 05:35:12 +01: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 15:19:47 +01:00
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = "Node"
PLATFORM_SCHEMA = BINARY_SENSOR_PLATFORM_SCHEMA.extend(
2017-10-27 15:19:47 +01:00
{vol.Required(CONF_NODES): vol.All(cv.ensure_list, [cv.string])}
)
2022-01-05 13:30:37 +01:00
def setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
2017-10-27 15:19:47 +01:00
"""Set up the Linode droplet sensor."""
2022-01-05 13:30:37 +01:00
linode = hass.data[DATA_LINODE]
nodes = config[CONF_NODES]
2017-10-27 15:19:47 +01:00
dev = []
for node in nodes:
2021-10-31 18:56:25 +01:00
if (node_id := linode.get_node_id(node)) is None:
2017-10-27 15:19:47 +01:00
_LOGGER.error("Node %s is not available", node)
return
dev.append(LinodeBinarySensor(linode, node_id))
2018-08-24 16:37:30 +02:00
add_entities(dev, True)
2017-10-27 15:19:47 +01:00
class LinodeBinarySensor(BinarySensorEntity):
2017-10-27 15:19:47 +01:00
"""Representation of a Linode droplet sensor."""
2021-12-20 14:17:23 +01:00
_attr_device_class = BinarySensorDeviceClass.MOVING
def __init__(self, li, node_id):
2017-10-27 15:19:47 +01:00
"""Initialize a new Linode sensor."""
self._linode = li
self._node_id = node_id
2021-12-20 14:17:23 +01:00
self._attr_extra_state_attributes = {}
self._attr_name = None
2018-06-18 15:21:41 +02:00
2022-09-01 14:14:31 +02:00
def update(self) -> None:
2018-06-18 15:21:41 +02:00
"""Update state of sensor."""
2021-12-20 14:17:23 +01:00
data = None
2018-06-18 15:21:41 +02:00
self._linode.update()
if self._linode.data is not None:
for node in self._linode.data:
if node.id == self._node_id:
2021-12-20 14:17:23 +01:00
data = node
if data is not None:
self._attr_is_on = data.status == "running"
self._attr_extra_state_attributes = {
ATTR_CREATED: data.created,
ATTR_NODE_ID: data.id,
ATTR_NODE_NAME: data.label,
ATTR_IPV4_ADDRESS: data.ipv4,
ATTR_IPV6_ADDRESS: data.ipv6,
ATTR_MEMORY: data.specs.memory,
ATTR_REGION: data.region.country,
ATTR_VCPUS: data.specs.vcpus,
2017-10-27 15:19:47 +01:00
}
2021-12-20 14:17:23 +01:00
self._attr_name = data.label