Skip to content
3 changes: 2 additions & 1 deletion homeassistant/components/glances/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
"process_thread": ["Thread", "Count", "mdi:memory"],
"process_sleeping": ["Sleeping", "Count", "mdi:memory"],
"cpu_use_percent": ["CPU used", "%", "mdi:memory"],
"cpu_temp": ["CPU Temp", TEMP_CELSIUS, "mdi:thermometer"],
"docker_active": ["Containers active", "", "mdi:docker"],
"docker_cpu_use": ["Containers CPU used", "%", "mdi:docker"],
"docker_memory_use": ["Containers RAM used", "MiB", "mdi:docker"],
}
TEMP_SENSORS = {"cpu_temp": ["CPU Temp", TEMP_CELSIUS, "mdi:thermometer"]}
CPU_TEMP = "cpu_temp"

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Member

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_TYPES or use the new TEMP_SENSORS dict. The lookup is performed for the same key but for different dicts.

Copy link
Copy Markdown
Contributor Author

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 fs in case of multiple detected disks all will be added.
For sensors also all sensors are added.
For sensor_types whose key doesn't have values the sensor is not added at all.

50 changes: 27 additions & 23 deletions homeassistant/components/glances/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I can change this to sensor_temp. Or do you recommend another naming?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with sensor_temp.

)
)

async_add_entities(dev, True)
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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()

Comment thread
engrbm87 marked this conversation as resolved.
async def async_update(self):
"""Get the latest data from REST API."""
value = self.glances_data.api.data
Expand Down Expand Up @@ -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
Expand Down