2019-04-03 17:40:03 +02:00
|
|
|
"""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
|
|
|
|
|
2021-03-22 19:54:14 +01:00
|
|
|
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"
|
2017-08-06 10:08:00 +02:00
|
|
|
|
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
|
|
|
|
2018-08-24 16:37:30 +02:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 18:18:47 +02:00
|
|
|
"""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)
|
2017-08-06 10:08:00 +02:00
|
|
|
processes = supervisor_server.supervisor.getAllProcessInfo()
|
2016-05-13 07:16:58 +02:00
|
|
|
except ConnectionRefusedError:
|
2017-05-02 18:18:47 +02:00
|
|
|
_LOGGER.error("Could not connect to Supervisord")
|
2016-09-02 06:34:07 +02:00
|
|
|
return False
|
|
|
|
|
2018-08-24 16:37:30 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
2021-03-22 19:54:14 +01: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
|
2017-08-06 10:08:00 +02:00
|
|
|
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
|
2021-08-11 18:57:50 +02:00
|
|
|
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
|
|
|
|
2017-08-06 10:08:00 +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
|
2021-03-11 21:23:20 +01:00
|
|
|
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
|
|
|
}
|
2017-08-06 10:08:00 +02:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update device state."""
|
|
|
|
try:
|
2020-07-15 13:45:29 +02:00
|
|
|
self._info = self._server.supervisor.getProcessInfo(
|
|
|
|
self._info.get("group") + ":" + self._info.get("name")
|
|
|
|
)
|
2017-08-06 10:08:00 +02:00
|
|
|
self._available = True
|
|
|
|
except ConnectionRefusedError:
|
|
|
|
_LOGGER.warning("Supervisord not available")
|
|
|
|
self._available = False
|