mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 14:57:09 +00:00
Redesign monitored variables for hp_ilo sensor (#7534)
* Redesign monitored variables Allow generating specific sensors without the need for template sensors * Import 3rd party library inside update method * Remove jsonpath_rw dependency * Do not interfere with value_template or ilo_data output Do not interfere with value_template or ilo_data output, this is now the responsibility of the user and should be handled in `configuration.yaml` Fix UnusedImportStatement Fix newline after function docstring * Always output results to state
This commit is contained in:
parent
b5f20c9b64
commit
e3307fb1c2
@ -11,7 +11,8 @@ import voluptuous as vol
|
|||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_HOST, CONF_PORT, CONF_USERNAME, CONF_PASSWORD, CONF_NAME,
|
CONF_HOST, CONF_PORT, CONF_USERNAME, CONF_PASSWORD, CONF_NAME,
|
||||||
CONF_MONITORED_VARIABLES, STATE_ON, STATE_OFF)
|
CONF_MONITORED_VARIABLES, CONF_VALUE_TEMPLATE, CONF_SENSOR_TYPE,
|
||||||
|
CONF_UNIT_OF_MEASUREMENT)
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.util import Throttle
|
from homeassistant.util import Throttle
|
||||||
@ -45,8 +46,14 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||||||
vol.Required(CONF_HOST): cv.string,
|
vol.Required(CONF_HOST): cv.string,
|
||||||
vol.Required(CONF_USERNAME): cv.string,
|
vol.Required(CONF_USERNAME): cv.string,
|
||||||
vol.Required(CONF_PASSWORD): cv.string,
|
vol.Required(CONF_PASSWORD): cv.string,
|
||||||
vol.Optional(CONF_MONITORED_VARIABLES, default=['server_name']):
|
vol.Optional(CONF_MONITORED_VARIABLES, default=[]):
|
||||||
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
|
vol.All(cv.ensure_list, [vol.Schema({
|
||||||
|
vol.Required(CONF_NAME): cv.string,
|
||||||
|
vol.Required(CONF_SENSOR_TYPE):
|
||||||
|
vol.All(cv.string, vol.In(SENSOR_TYPES)),
|
||||||
|
vol.Optional(CONF_UNIT_OF_MEASUREMENT, default=None): cv.string,
|
||||||
|
vol.Optional(CONF_VALUE_TEMPLATE, default=None): cv.template
|
||||||
|
})]),
|
||||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
||||||
})
|
})
|
||||||
@ -60,7 +67,6 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
login = config.get(CONF_USERNAME)
|
login = config.get(CONF_USERNAME)
|
||||||
password = config.get(CONF_PASSWORD)
|
password = config.get(CONF_PASSWORD)
|
||||||
monitored_variables = config.get(CONF_MONITORED_VARIABLES)
|
monitored_variables = config.get(CONF_MONITORED_VARIABLES)
|
||||||
name = config.get(CONF_NAME)
|
|
||||||
|
|
||||||
# Create a data fetcher to support all of the configured sensors. Then make
|
# Create a data fetcher to support all of the configured sensors. Then make
|
||||||
# the first call to init the data and confirm we can connect.
|
# the first call to init the data and confirm we can connect.
|
||||||
@ -72,10 +78,15 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
|
|
||||||
# Initialize and add all of the sensors.
|
# Initialize and add all of the sensors.
|
||||||
devices = []
|
devices = []
|
||||||
for ilo_type in monitored_variables:
|
for monitored_variable in monitored_variables:
|
||||||
new_device = HpIloSensor(
|
new_device = HpIloSensor(
|
||||||
hp_ilo_data=hp_ilo_data, sensor_type=SENSOR_TYPES.get(ilo_type),
|
hass=hass,
|
||||||
client_name=name)
|
hp_ilo_data=hp_ilo_data,
|
||||||
|
sensor_name='{} {}'.format(
|
||||||
|
config.get(CONF_NAME), monitored_variable[CONF_NAME]),
|
||||||
|
sensor_type=monitored_variable[CONF_SENSOR_TYPE],
|
||||||
|
sensor_value_template=monitored_variable[CONF_VALUE_TEMPLATE],
|
||||||
|
unit_of_measurement=monitored_variable[CONF_UNIT_OF_MEASUREMENT])
|
||||||
devices.append(new_device)
|
devices.append(new_device)
|
||||||
|
|
||||||
add_devices(devices)
|
add_devices(devices)
|
||||||
@ -84,15 +95,21 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
class HpIloSensor(Entity):
|
class HpIloSensor(Entity):
|
||||||
"""Representation of a HP ILO sensor."""
|
"""Representation of a HP ILO sensor."""
|
||||||
|
|
||||||
def __init__(self, hp_ilo_data, sensor_type, client_name):
|
def __init__(self, hass, hp_ilo_data, sensor_type, sensor_name,
|
||||||
|
sensor_value_template, unit_of_measurement):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._name = '{} {}'.format(client_name, sensor_type[0])
|
self._hass = hass
|
||||||
self._ilo_function = sensor_type[1]
|
self._name = sensor_name
|
||||||
self.client_name = client_name
|
self._unit_of_measurement = unit_of_measurement
|
||||||
|
self._ilo_function = SENSOR_TYPES[sensor_type][1]
|
||||||
self.hp_ilo_data = hp_ilo_data
|
self.hp_ilo_data = hp_ilo_data
|
||||||
|
|
||||||
|
if sensor_value_template is not None:
|
||||||
|
sensor_value_template.hass = hass
|
||||||
|
self._sensor_value_template = sensor_value_template
|
||||||
|
|
||||||
self._state = None
|
self._state = None
|
||||||
self._data = None
|
self._state_attributes = None
|
||||||
|
|
||||||
self.update()
|
self.update()
|
||||||
|
|
||||||
@ -103,6 +120,11 @@ class HpIloSensor(Entity):
|
|||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unit_of_measurement(self):
|
||||||
|
"""Return the unit of measurement of the sensor."""
|
||||||
|
return self._unit_of_measurement
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
@ -111,7 +133,7 @@ class HpIloSensor(Entity):
|
|||||||
@property
|
@property
|
||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
return self._data
|
return self._state_attributes
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data from HP ILO and updates the states."""
|
"""Get the latest data from HP ILO and updates the states."""
|
||||||
@ -121,23 +143,10 @@ class HpIloSensor(Entity):
|
|||||||
self.hp_ilo_data.update()
|
self.hp_ilo_data.update()
|
||||||
ilo_data = getattr(self.hp_ilo_data.data, self._ilo_function)()
|
ilo_data = getattr(self.hp_ilo_data.data, self._ilo_function)()
|
||||||
|
|
||||||
# Store the data received from the ILO API
|
if self._sensor_value_template is not None:
|
||||||
if isinstance(ilo_data, dict):
|
ilo_data = self._sensor_value_template.render(ilo_data=ilo_data)
|
||||||
self._data = ilo_data
|
|
||||||
else:
|
|
||||||
self._data = {'value': ilo_data}
|
|
||||||
|
|
||||||
# If the data received is an integer or string, store it as
|
self._state = ilo_data
|
||||||
# the sensor state
|
|
||||||
if isinstance(ilo_data, (str, bytes)):
|
|
||||||
states = [STATE_ON, STATE_OFF]
|
|
||||||
try:
|
|
||||||
index_element = states.index(str(ilo_data).lower())
|
|
||||||
self._state = states[index_element]
|
|
||||||
except ValueError:
|
|
||||||
self._state = ilo_data
|
|
||||||
elif isinstance(ilo_data, (int, float)):
|
|
||||||
self._state = ilo_data
|
|
||||||
|
|
||||||
|
|
||||||
class HpIloData(object):
|
class HpIloData(object):
|
||||||
|
@ -137,6 +137,7 @@ CONF_RGB = 'rgb'
|
|||||||
CONF_SCAN_INTERVAL = 'scan_interval'
|
CONF_SCAN_INTERVAL = 'scan_interval'
|
||||||
CONF_SENDER = 'sender'
|
CONF_SENDER = 'sender'
|
||||||
CONF_SENSOR_CLASS = 'sensor_class'
|
CONF_SENSOR_CLASS = 'sensor_class'
|
||||||
|
CONF_SENSOR_TYPE = 'sensor_type'
|
||||||
CONF_SENSORS = 'sensors'
|
CONF_SENSORS = 'sensors'
|
||||||
CONF_SSL = 'ssl'
|
CONF_SSL = 'ssl'
|
||||||
CONF_STATE = 'state'
|
CONF_STATE = 'state'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user