-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
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 all 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 |
|---|---|---|
|
|
@@ -14,33 +14,81 @@ | |
| async def async_setup_entry(hass, config_entry, async_add_entities): | ||
| """Set up the Glances sensors.""" | ||
|
|
||
| glances_data = hass.data[DOMAIN][config_entry.entry_id] | ||
| client = hass.data[DOMAIN][config_entry.entry_id] | ||
| name = config_entry.data[CONF_NAME] | ||
| dev = [] | ||
| for sensor_type in SENSOR_TYPES: | ||
| dev.append( | ||
| GlancesSensor(glances_data, name, SENSOR_TYPES[sensor_type][0], sensor_type) | ||
| ) | ||
|
|
||
| for sensor_type, sensor_details in SENSOR_TYPES.items(): | ||
| if not sensor_details[0] in client.api.data: | ||
| continue | ||
| if sensor_details[0] in client.api.data: | ||
| if sensor_details[0] == "fs": | ||
| # fs will provide a list of disks attached | ||
| for disk in client.api.data[sensor_details[0]]: | ||
| dev.append( | ||
| GlancesSensor( | ||
| client, | ||
| name, | ||
| disk["mnt_point"], | ||
|
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. Can we somehow also put this in
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. I need to keep it as perfix because i use the prefix value when updating the entity in |
||
| SENSOR_TYPES[sensor_type][1], | ||
| sensor_type, | ||
| SENSOR_TYPES[sensor_type], | ||
| ) | ||
| ) | ||
| elif sensor_details[0] == "sensors": | ||
| # sensors will provide temp for different devices | ||
| for sensor in client.api.data[sensor_details[0]]: | ||
| dev.append( | ||
| GlancesSensor( | ||
| client, | ||
| name, | ||
| sensor["label"], | ||
| SENSOR_TYPES[sensor_type][1], | ||
| sensor_type, | ||
| SENSOR_TYPES[sensor_type], | ||
| ) | ||
| ) | ||
| elif client.api.data[sensor_details[0]]: | ||
| dev.append( | ||
| GlancesSensor( | ||
| client, | ||
| name, | ||
| "", | ||
| SENSOR_TYPES[sensor_type][1], | ||
| sensor_type, | ||
| SENSOR_TYPES[sensor_type], | ||
| ) | ||
| ) | ||
|
|
||
| async_add_entities(dev, True) | ||
|
|
||
|
|
||
| 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_prefix, | ||
| sensor_name_suffix, | ||
| sensor_type, | ||
| sensor_details, | ||
| ): | ||
| """Initialize the sensor.""" | ||
| self.glances_data = glances_data | ||
| self._sensor_name = sensor_name | ||
| self._sensor_name_prefix = sensor_name_prefix | ||
| self._sensor_name_suffix = sensor_name_suffix | ||
| 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): | ||
| """Return the name of the sensor.""" | ||
| return f"{self._name} {self._sensor_name}" | ||
| return f"{self._name} {self._sensor_name_prefix} {self._sensor_name_suffix}" | ||
|
|
||
| @property | ||
| def unique_id(self): | ||
|
|
@@ -50,12 +98,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[3] | ||
|
|
||
| @property | ||
| def unit_of_measurement(self): | ||
| """Return the unit the value is expressed in.""" | ||
| return self._unit_of_measurement | ||
| return self.sensor_details[2] | ||
|
|
||
| @property | ||
| def available(self): | ||
|
|
@@ -74,30 +122,48 @@ 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() | ||
| self.unsub_update = None | ||
|
|
||
|
engrbm87 marked this conversation as resolved.
|
||
| async def async_update(self): | ||
| """Get the latest data from REST API.""" | ||
| value = self.glances_data.api.data | ||
| if value is None: | ||
| return | ||
|
|
||
| if value is not None: | ||
|
engrbm87 marked this conversation as resolved.
|
||
| if self.type == "disk_use_percent": | ||
| self._state = value["fs"][0]["percent"] | ||
| elif self.type == "disk_use": | ||
| self._state = round(value["fs"][0]["used"] / 1024 ** 3, 1) | ||
| elif self.type == "disk_free": | ||
| try: | ||
| self._state = round(value["fs"][0]["free"] / 1024 ** 3, 1) | ||
| except KeyError: | ||
| self._state = round( | ||
| (value["fs"][0]["size"] - value["fs"][0]["used"]) / 1024 ** 3, 1 | ||
| ) | ||
| if self.sensor_details[0] == "fs": | ||
| for var in value["fs"]: | ||
| if var["mnt_point"] == self._sensor_name_prefix: | ||
| disk = var | ||
| break | ||
| if self.type == "disk_use_percent": | ||
| self._state = disk["percent"] | ||
| elif self.type == "disk_use": | ||
| self._state = round(disk["used"] / 1024 ** 3, 1) | ||
| elif self.type == "disk_free": | ||
| try: | ||
| self._state = round(disk["free"] / 1024 ** 3, 1) | ||
| except KeyError: | ||
| self._state = round( | ||
| (disk["size"] - disk["used"]) / 1024 ** 3, 1, | ||
| ) | ||
| elif self.type == "sensor_temp": | ||
| for sensor in value["sensors"]: | ||
| if sensor["label"] == self._sensor_name_prefix: | ||
| self._state = sensor["value"] | ||
| break | ||
| elif self.type == "memory_use_percent": | ||
| self._state = value["mem"]["percent"] | ||
| elif self.type == "memory_use": | ||
|
|
@@ -126,25 +192,6 @@ async def async_update(self): | |
| self._state = value["processcount"]["sleeping"] | ||
| elif self.type == "cpu_use_percent": | ||
| 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", | ||
| ]: | ||
| self._state = sensor["value"] | ||
| elif self.type == "docker_active": | ||
| count = 0 | ||
| try: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.