-
-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Add Glances sensors dynamically #28639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
753aa25
9dea1c2
3b5f265
84f45f9
27af899
5934e40
d14f54e
964e0aa
83fed54
fe01ae3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ | |
| from homeassistant.helpers.dispatcher import async_dispatcher_connect | ||
| from homeassistant.helpers.entity import Entity | ||
|
|
||
| from .const import DATA_UPDATED, DOMAIN, SENSOR_TYPES | ||
| from .const import CPU_TEMP, DATA_UPDATED, DOMAIN, SENSOR_TYPES, TEMP_SENSORS | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -24,7 +24,20 @@ async def async_setup_entry(hass, config_entry, async_add_entities): | |
| dev = [] | ||
| for sensor_type in SENSOR_TYPES: | ||
| dev.append( | ||
| GlancesSensor(glances_data, name, SENSOR_TYPES[sensor_type][0], sensor_type) | ||
| GlancesSensor( | ||
| glances_data, | ||
| name, | ||
| SENSOR_TYPES[sensor_type][0], | ||
| sensor_type, | ||
| SENSOR_TYPES[sensor_type], | ||
| ) | ||
| ) | ||
|
|
||
| for sensor in glances_data.api.data["sensors"]: | ||
| dev.append( | ||
| GlancesSensor( | ||
| glances_data, name, sensor["label"], CPU_TEMP, TEMP_SENSORS[CPU_TEMP] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should adjust the naming as we are no longer only working with CPU temperatures but chipsets and other peripheral hardware.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I can change this to
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine with |
||
| ) | ||
| ) | ||
|
|
||
| async_add_entities(dev, True) | ||
|
|
@@ -33,14 +46,15 @@ async def async_setup_entry(hass, config_entry, async_add_entities): | |
| class GlancesSensor(Entity): | ||
| """Implementation of a Glances sensor.""" | ||
|
|
||
| def __init__(self, glances_data, name, sensor_name, sensor_type): | ||
| def __init__(self, glances_data, name, sensor_name, sensor_type, sensor_details): | ||
| """Initialize the sensor.""" | ||
| self.glances_data = glances_data | ||
| self._sensor_name = sensor_name | ||
| self._name = name | ||
| self.type = sensor_type | ||
| self._state = None | ||
| self._unit_of_measurement = SENSOR_TYPES[sensor_type][1] | ||
| self.sensor_details = sensor_details | ||
| self.unsub_update = None | ||
|
|
||
| @property | ||
| def name(self): | ||
|
|
@@ -55,12 +69,12 @@ def unique_id(self): | |
| @property | ||
| def icon(self): | ||
| """Icon to use in the frontend, if any.""" | ||
| return SENSOR_TYPES[self.type][2] | ||
| return self.sensor_details[2] | ||
|
|
||
| @property | ||
| def unit_of_measurement(self): | ||
| """Return the unit the value is expressed in.""" | ||
| return self._unit_of_measurement | ||
| return self.sensor_details[1] | ||
|
|
||
| @property | ||
| def available(self): | ||
|
|
@@ -79,14 +93,19 @@ def should_poll(self): | |
|
|
||
| async def async_added_to_hass(self): | ||
| """Handle entity which will be added.""" | ||
| async_dispatcher_connect( | ||
| self.unsub_update = async_dispatcher_connect( | ||
| self.hass, DATA_UPDATED, self._schedule_immediate_update | ||
| ) | ||
|
|
||
| @callback | ||
| def _schedule_immediate_update(self): | ||
| self.async_schedule_update_ha_state(True) | ||
|
|
||
| async def will_remove_from_hass(self): | ||
| """Unsubscribe from update dispatcher.""" | ||
| if self.unsub_update: | ||
| self.unsub_update() | ||
|
|
||
|
engrbm87 marked this conversation as resolved.
|
||
| async def async_update(self): | ||
| """Get the latest data from REST API.""" | ||
| value = self.glances_data.api.data | ||
|
|
@@ -133,22 +152,7 @@ async def async_update(self): | |
| self._state = value["quicklook"]["cpu"] | ||
| elif self.type == "cpu_temp": | ||
| for sensor in value["sensors"]: | ||
| if sensor["label"] in [ | ||
| "amdgpu 1", | ||
| "aml_thermal", | ||
| "Core 0", | ||
| "Core 1", | ||
| "CPU Temperature", | ||
| "CPU", | ||
| "cpu-thermal 1", | ||
| "cpu_thermal 1", | ||
| "exynos-therm 1", | ||
| "Package id 0", | ||
| "Physical id 0", | ||
| "radeon 1", | ||
| "soc-thermal 1", | ||
| "soc_thermal 1", | ||
| ]: | ||
| if self._sensor_name == sensor["label"]: | ||
| self._state = sensor["value"] | ||
| elif self.type == "docker_active": | ||
| count = 0 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see a benefit to break it out of the existing dict in a new one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I broke it out because I don't want to create only one sensor for this type. I want to create multiple sensors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are doing a lookup for every sensor that will be created thus it doesn't matter if you stick with
SENSOR_TYPESor use the newTEMP_SENSORSdict. The lookup is performed for the same key but for different dicts.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have updated the code so that sensors are added dynamically based on the collected data from glances_api.
For
fsin case of multiple detected disks all will be added.For
sensorsalso all sensors are added.For
sensor_typeswhose key doesn't have values the sensor is not added at all.