1
mirror of https://github.com/home-assistant/core synced 2024-09-06 10:29:55 +02:00
ha-core/homeassistant/components/supervisord/sensor.py

80 lines
2.3 KiB
Python
Raw Normal View History

"""Sensor for Supervisord process status."""
2016-05-13 07:16:58 +02:00
import logging
import xmlrpc.client
2016-09-02 06:34:07 +02:00
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
2016-09-02 06:34:07 +02:00
from homeassistant.const import CONF_URL
import homeassistant.helpers.config_validation as cv
2016-05-13 07:16:58 +02:00
_LOGGER = logging.getLogger(__name__)
2019-07-31 21:25:30 +02:00
ATTR_DESCRIPTION = "description"
ATTR_GROUP = "group"
2019-07-31 21:25:30 +02:00
DEFAULT_URL = "http://localhost:9001/RPC2"
2016-09-02 06:34:07 +02:00
2019-07-31 21:25:30 +02:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Optional(CONF_URL, default=DEFAULT_URL): cv.url}
)
2016-09-02 06:34:07 +02:00
2016-05-13 07:16:58 +02:00
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Supervisord platform."""
2016-09-02 06:34:07 +02:00
url = config.get(CONF_URL)
2016-05-13 07:16:58 +02:00
try:
2016-09-02 06:34:07 +02:00
supervisor_server = xmlrpc.client.ServerProxy(url)
processes = supervisor_server.supervisor.getAllProcessInfo()
2016-05-13 07:16:58 +02:00
except ConnectionRefusedError:
_LOGGER.error("Could not connect to Supervisord")
2016-09-02 06:34:07 +02:00
return False
add_entities(
2019-07-31 21:25:30 +02:00
[SupervisorProcessSensor(info, supervisor_server) for info in processes], True
)
2016-05-13 07:16:58 +02:00
class SupervisorProcessSensor(SensorEntity):
2016-09-02 06:34:07 +02:00
"""Representation of a supervisor-monitored process."""
2016-05-13 07:16:58 +02:00
def __init__(self, info, server):
"""Initialize the sensor."""
self._info = info
self._server = server
self._available = True
2016-05-13 07:16:58 +02:00
@property
def name(self):
"""Return the name of the sensor."""
2019-07-31 21:25:30 +02:00
return self._info.get("name")
2016-05-13 07:16:58 +02:00
@property
def native_value(self):
2016-05-13 07:16:58 +02:00
"""Return the state of the sensor."""
2019-07-31 21:25:30 +02:00
return self._info.get("statename")
2016-05-13 07:16:58 +02:00
@property
def available(self):
"""Could the device be accessed during the last update call."""
return self._available
2016-05-13 07:16:58 +02:00
@property
def extra_state_attributes(self):
2016-05-13 07:16:58 +02:00
"""Return the state attributes."""
return {
2019-07-31 21:25:30 +02:00
ATTR_DESCRIPTION: self._info.get("description"),
ATTR_GROUP: self._info.get("group"),
2016-05-13 07:16:58 +02:00
}
def update(self):
"""Update device state."""
try:
self._info = self._server.supervisor.getProcessInfo(
self._info.get("group") + ":" + self._info.get("name")
)
self._available = True
except ConnectionRefusedError:
_LOGGER.warning("Supervisord not available")
self._available = False