2019-04-03 15:40:03 +00:00
|
|
|
"""Sensor for Supervisord process status."""
|
2016-05-13 05:16:58 +00:00
|
|
|
import logging
|
|
|
|
import xmlrpc.client
|
|
|
|
|
2016-09-02 04:34:07 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
2016-09-02 04:34:07 +00:00
|
|
|
from homeassistant.const import CONF_URL
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-05-13 05:16:58 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_DESCRIPTION = "description"
|
|
|
|
ATTR_GROUP = "group"
|
2017-08-06 08:08:00 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_URL = "http://localhost:9001/RPC2"
|
2016-09-02 04:34:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Optional(CONF_URL, default=DEFAULT_URL): cv.url}
|
|
|
|
)
|
2016-09-02 04:34:07 +00:00
|
|
|
|
2016-05-13 05:16:58 +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 Supervisord platform."""
|
2016-09-02 04:34:07 +00:00
|
|
|
url = config.get(CONF_URL)
|
2016-05-13 05:16:58 +00:00
|
|
|
try:
|
2016-09-02 04:34:07 +00:00
|
|
|
supervisor_server = xmlrpc.client.ServerProxy(url)
|
2017-08-06 08:08:00 +00:00
|
|
|
processes = supervisor_server.supervisor.getAllProcessInfo()
|
2016-05-13 05:16:58 +00:00
|
|
|
except ConnectionRefusedError:
|
2017-05-02 16:18:47 +00:00
|
|
|
_LOGGER.error("Could not connect to Supervisord")
|
2016-09-02 04:34:07 +00:00
|
|
|
return False
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(
|
2019-07-31 19:25:30 +00:00
|
|
|
[SupervisorProcessSensor(info, supervisor_server) for info in processes], True
|
|
|
|
)
|
2016-05-13 05:16:58 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
class SupervisorProcessSensor(SensorEntity):
|
2016-09-02 04:34:07 +00:00
|
|
|
"""Representation of a supervisor-monitored process."""
|
2016-05-13 05:16:58 +00:00
|
|
|
|
|
|
|
def __init__(self, info, server):
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
self._info = info
|
|
|
|
self._server = server
|
2017-08-06 08:08:00 +00:00
|
|
|
self._available = True
|
2016-05-13 05:16:58 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self._info.get("name")
|
2016-05-13 05:16:58 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-11 16:57:50 +00:00
|
|
|
def native_value(self):
|
2016-05-13 05:16:58 +00:00
|
|
|
"""Return the state of the sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self._info.get("statename")
|
2016-05-13 05:16:58 +00:00
|
|
|
|
2017-08-06 08:08:00 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Could the device be accessed during the last update call."""
|
|
|
|
return self._available
|
2016-05-13 05:16:58 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-11 20:23:20 +00:00
|
|
|
def extra_state_attributes(self):
|
2016-05-13 05:16:58 +00:00
|
|
|
"""Return the state attributes."""
|
|
|
|
return {
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_DESCRIPTION: self._info.get("description"),
|
|
|
|
ATTR_GROUP: self._info.get("group"),
|
2016-05-13 05:16:58 +00:00
|
|
|
}
|
2017-08-06 08:08:00 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update device state."""
|
|
|
|
try:
|
2020-07-15 11:45:29 +00:00
|
|
|
self._info = self._server.supervisor.getProcessInfo(
|
|
|
|
self._info.get("group") + ":" + self._info.get("name")
|
|
|
|
)
|
2017-08-06 08:08:00 +00:00
|
|
|
self._available = True
|
|
|
|
except ConnectionRefusedError:
|
|
|
|
_LOGGER.warning("Supervisord not available")
|
|
|
|
self._available = False
|