Skip to content

Commit

Permalink
Put the name of the server into the name of each entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Breina committed Feb 22, 2024
1 parent d906064 commit 09b8fce
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 38 deletions.
4 changes: 3 additions & 1 deletion custom_components/idrac_power/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e
)

async_add_entities([
IdracStatusBinarySensor(hass, rest_client, device_info, f"{serial}_{model}_status", f"{model}_status"),
IdracStatusBinarySensor(hass, rest_client, device_info, f"{serial}_{name}_status",
f"{name} status"
)
])


Expand Down
20 changes: 5 additions & 15 deletions custom_components/idrac_power/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e
)

async_add_entities([
IdracPowerONButton(hass, rest_client, device_info, f"{serial}_{model}_status", name),
IdracRefreshButton(hass, rest_client, device_info, f"{serial}_{model}_refresh", name)
IdracPowerONButton(hass, rest_client, device_info, f"{serial}_{name}_status", name),
IdracRefreshButton(hass, rest_client, device_info, f"{serial}_{name}_refresh", name)
])


Expand All @@ -50,7 +50,7 @@ def __init__(self, hass, rest: IdracRest, device_info, unique_id, name):

self.entity_description = ButtonEntityDescription(
key='power_on',
name=name,
name=f"Power on {name}",
icon='mdi:power',
device_class=ButtonDeviceClass.UPDATE,
)
Expand All @@ -60,12 +60,7 @@ def __init__(self, hass, rest: IdracRest, device_info, unique_id, name):
self._attr_has_entity_name = True

async def async_press(self) -> None:
result = await self.hass.async_add_executor_job(self.rest.power_on)

@property
def name(self):
"""Name of the entity."""
return "Power On"
await self.hass.async_add_executor_job(self.rest.power_on)


class IdracRefreshButton(ButtonEntity):
Expand All @@ -76,7 +71,7 @@ def __init__(self, hass, rest: IdracRest, device_info, unique_id, name):

self.entity_description = ButtonEntityDescription(
key='power_on',
name=name,
name=f"Refresh {name}",
icon='mdi:power',
device_class=ButtonDeviceClass.UPDATE,
)
Expand All @@ -90,8 +85,3 @@ async def async_press(self) -> None:
await self.hass.async_add_executor_job(self.rest.update_thermals)
await self.hass.async_add_executor_job(self.rest.update_status)
await self.hass.async_add_executor_job(self.rest.update_power_usage)

@property
def name(self):
"""Name of the entity."""
return "Refresh Values"
30 changes: 8 additions & 22 deletions custom_components/idrac_power/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from __future__ import annotations

import asyncio
import functools
import logging

from homeassistant.components.sensor import SensorEntity, SensorEntityDescription, SensorStateClass, SensorDeviceClass
Expand Down Expand Up @@ -44,15 +43,19 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e
serial_number=serial
)

entities = [IdracCurrentPowerSensor(hass, rest_client, device_info, f"{serial}_{model}_current", name)]
entities = [IdracCurrentPowerSensor(hass, rest_client, device_info, f"{serial}_{name}_current", name)]

for i, fan in enumerate(thermal_info['Fans']):
_LOGGER.info("Adding fan %s : %s", i, fan["FanName"])
entities.append(IdracFanSensor(hass, rest_client, device_info, f"{serial}_{model}_fan_{i}", fan["FanName"], i))
entities.append(IdracFanSensor(hass, rest_client, device_info, f"{serial}_{name}_fan_{i}",
f"{name} {fan['FanName']}", i
))

for i, temp in enumerate(thermal_info['Temperatures']):
_LOGGER.info("Adding temp %s : %s", i, temp["Name"])
entities.append(IdracTempSensor(hass, rest_client, device_info, f"{serial}_{model}_temp_{i}", temp["Name"], i))
entities.append(IdracTempSensor(hass, rest_client, device_info, f"{serial}_{name}_temp_{i}",
f"{name} {temp['Name']}", i
))

async_add_entities(entities)

Expand All @@ -76,7 +79,7 @@ def __init__(self, hass, rest: IdracRest, device_info, unique_id, name):

self.entity_description = SensorEntityDescription(
key='current_power_usage',
name=name,
name=f"{name} power usage",
icon='mdi:lightning-bolt',
native_unit_of_measurement='W',
device_class=SensorDeviceClass.POWER,
Expand All @@ -91,11 +94,6 @@ def __init__(self, hass, rest: IdracRest, device_info, unique_id, name):

self.rest.register_callback_power_usage(self.update_value)

@property
def name(self):
"""Name of the entity."""
return "Power Usage"

def update_value(self, new_value: int):
self._attr_native_value = new_value
self.async_schedule_update_ha_state()
Expand All @@ -116,7 +114,6 @@ def __init__(self, hass, rest: IdracRest, device_info, unique_id, name, id):
state_class=SensorStateClass.MEASUREMENT
)

self.custom_name = name
self._attr_device_info = device_info
self._attr_unique_id = unique_id
self._attr_has_entity_name = True
Expand All @@ -126,11 +123,6 @@ def __init__(self, hass, rest: IdracRest, device_info, unique_id, name, id):

self.rest.register_callback_thermals(self.update_value)

@property
def name(self):
"""Name of the entity."""
return self.custom_name

def update_value(self, thermal: dict):
self._attr_native_value = thermal['Fans'][self.id]['Reading']
self.async_schedule_update_ha_state()
Expand All @@ -151,7 +143,6 @@ def __init__(self, hass, rest: IdracRest, device_info, unique_id, name, id):
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement='°C',
)
self.custom_name = name

self._attr_device_info = device_info
self._attr_unique_id = unique_id
Expand All @@ -161,11 +152,6 @@ def __init__(self, hass, rest: IdracRest, device_info, unique_id, name, id):

self.rest.register_callback_thermals(self.update_value)

@property
def name(self):
"""Name of the entity."""
return self.custom_name

def update_value(self, thermal: dict):
self._attr_native_value = thermal['Temperatures'][self.id]['ReadingCelsius']
self.async_schedule_update_ha_state()

0 comments on commit 09b8fce

Please sign in to comment.