1 """Support for Open Hardware Monitor Sensor Platform."""
3 from __future__
import annotations
5 from datetime
import timedelta
9 import voluptuous
as vol
12 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
25 _LOGGER = logging.getLogger(__name__)
27 STATE_MIN_VALUE =
"minimal_value"
28 STATE_MAX_VALUE =
"maximum_value"
30 STATE_OBJECT =
"object"
31 CONF_INTERVAL =
"interval"
40 OHM_CHILDREN =
"Children"
43 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
44 {vol.Required(CONF_HOST): cv.string, vol.Optional(CONF_PORT, default=8085): cv.port}
51 add_entities: AddEntitiesCallback,
52 discovery_info: DiscoveryInfoType |
None =
None,
54 """Set up the Open Hardware Monitor platform."""
57 raise PlatformNotReady
62 """Device used to display information from OpenHardwareMonitor."""
64 _attr_state_class = SensorStateClass.MEASUREMENT
66 def __init__(self, data, name, path, unit_of_measurement):
67 """Initialize an OpenHardwareMonitor sensor."""
78 """Return the name of the device."""
79 return self.
_name_name
83 """Return the unit of measurement."""
88 """Return the state of the device."""
89 if self.
valuevalue ==
"-":
91 return self.
valuevalue
95 """Return the state attributes of the entity."""
100 """In some locales a decimal numbers uses ',' instead of '.'."""
101 return string.replace(
",",
".")
104 """Update the device from a new JSON object."""
107 array = self.
_data_data.data[OHM_CHILDREN]
110 for path_index, path_number
in enumerate(self.
pathpath):
111 values = array[path_number]
113 if path_index == len(self.
pathpath) - 1:
117 "name": values[OHM_NAME],
119 values[OHM_MIN].split(
" ")[0]
122 values[OHM_MAX].split(
" ")[0]
129 array = array[path_number][OHM_CHILDREN]
130 _attributes.update({f
"level_{path_index}": values[OHM_NAME]})
134 """Class used to pull data from OHM and create sensors."""
137 """Initialize the Open Hardware Monitor data-handler."""
144 @Throttle(MIN_TIME_BETWEEN_UPDATES)
146 """Hit by the timer with the configured interval."""
147 if self.
datadata
is None:
153 """Download and parse JSON from OHM."""
155 f
"http://{self._config.get(CONF_HOST)}:"
156 f
"{self._config.get(CONF_PORT)}/data.json"
160 response = requests.get(data_url, timeout=30)
161 self.
datadata = response.json()
162 except requests.exceptions.ConnectionError:
163 _LOGGER.debug(
"ConnectionError: Is OpenHardwareMonitor running?")
166 """Parse of the sensors and adding of devices."""
169 if self.
datadata
is None:
175 """Recursively loop through child objects, finding the values."""
176 result = devices.copy()
178 if json[OHM_CHILDREN]:
179 for child_index
in range(len(json[OHM_CHILDREN])):
180 child_path = path.copy()
181 child_path.append(child_index)
183 child_names = names.copy()
185 child_names.append(json[OHM_NAME])
187 obj = json[OHM_CHILDREN][child_index]
190 obj, devices, child_path, child_names
193 result = result + added_devices
196 if json[OHM_VALUE].find(
" ") == -1:
199 unit_of_measurement = json[OHM_VALUE].split(
" ")[1]
200 child_names = names.copy()
201 child_names.append(json[OHM_NAME])
202 fullname =
" ".join(child_names)
def initialize(self, now)
def __init__(self, config, hass)
def parse_children(self, json, devices, path, names)
def parse_number(cls, string)
def __init__(self, data, name, path, unit_of_measurement)
def native_unit_of_measurement(self)
def extra_state_attributes(self)
def add_entities(account, async_add_entities, tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)