Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Sensor for Supervisord process status."""
2 
3 from __future__ import annotations
4 
5 import logging
6 import xmlrpc.client
7 
8 import voluptuous as vol
9 
11  PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA,
12  SensorEntity,
13 )
14 from homeassistant.const import CONF_URL
15 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 ATTR_DESCRIPTION = "description"
23 ATTR_GROUP = "group"
24 
25 DEFAULT_URL = "http://localhost:9001/RPC2"
26 
27 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
28  {vol.Optional(CONF_URL, default=DEFAULT_URL): cv.url}
29 )
30 
31 
33  hass: HomeAssistant,
34  config: ConfigType,
35  add_entities: AddEntitiesCallback,
36  discovery_info: DiscoveryInfoType | None = None,
37 ) -> None:
38  """Set up the Supervisord platform."""
39  url = config[CONF_URL]
40  try:
41  supervisor_server = xmlrpc.client.ServerProxy(url)
42  # See this link to explain the type ignore:
43  # http://supervisord.org/api.html#supervisor.rpcinterface.SupervisorNamespaceRPCInterface.getAllProcessInfo
44  processes: list[dict] = supervisor_server.supervisor.getAllProcessInfo() # type: ignore[assignment]
45  except ConnectionRefusedError:
46  _LOGGER.error("Could not connect to Supervisord")
47  return
48 
50  [SupervisorProcessSensor(info, supervisor_server) for info in processes], True
51  )
52 
53 
55  """Representation of a supervisor-monitored process."""
56 
57  def __init__(self, info, server):
58  """Initialize the sensor."""
59  self._info_info = info
60  self._server_server = server
61  self._available_available = True
62 
63  @property
64  def name(self):
65  """Return the name of the sensor."""
66  return self._info_info.get("name")
67 
68  @property
69  def native_value(self):
70  """Return the state of the sensor."""
71  return self._info_info.get("statename")
72 
73  @property
74  def available(self):
75  """Could the device be accessed during the last update call."""
76  return self._available_available
77 
78  @property
80  """Return the state attributes."""
81  return {
82  ATTR_DESCRIPTION: self._info_info.get("description"),
83  ATTR_GROUP: self._info_info.get("group"),
84  }
85 
86  def update(self) -> None:
87  """Update device state."""
88  try:
89  self._info_info = self._server_server.supervisor.getProcessInfo(
90  self._info_info.get("group") + ":" + self._info_info.get("name")
91  )
92  self._available_available = True
93  except ConnectionRefusedError:
94  _LOGGER.warning("Supervisord not available")
95  self._available_available = False
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
def add_entities(account, async_add_entities, tracked)
Definition: sensor.py:40
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: sensor.py:37