1
mirror of https://github.com/home-assistant/core synced 2024-07-27 18:58:57 +02:00
ha-core/homeassistant/components/supervisord/sensor.py

92 lines
2.8 KiB
Python
Raw Normal View History

"""Sensor for Supervisord process status."""
from __future__ import annotations
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
from homeassistant.core import HomeAssistant
2016-09-02 06:34:07 +02:00
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
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: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Supervisord platform."""
url = config[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)
# See this link to explain the type ignore:
# http://supervisord.org/api.html#supervisor.rpcinterface.SupervisorNamespaceRPCInterface.getAllProcessInfo
processes: list[dict] = supervisor_server.supervisor.getAllProcessInfo() # type: ignore[assignment]
2016-05-13 07:16:58 +02:00
except ConnectionRefusedError:
_LOGGER.error("Could not connect to Supervisord")
return
2016-09-02 06:34:07 +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
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) -> None:
"""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