Skip to content

Commit

Permalink
fix: sensors getting mixed up after reconnecting
Browse files Browse the repository at this point in the history
  • Loading branch information
PocketMiner82 committed Jun 24, 2024
1 parent 07b38f1 commit e757d6e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
2 changes: 2 additions & 0 deletions custom_components/idrac_power/idrac_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ def update_thermals(self) -> dict:
_LOGGER.debug(f"Couldn't update {self.host} thermals: {e}")
new_thermals = None

_LOGGER.warning(new_thermals)

if new_thermals != self.thermal_values:
self.thermal_values = new_thermals
for callback in self.callback_thermals:
Expand Down
28 changes: 18 additions & 10 deletions custom_components/idrac_power/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e
entities = [IdracCurrentPowerSensor(hass, rest_client, device_info, f"{serial}_{name}_power", name)]

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

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

async_add_entities(entities)
Expand Down Expand Up @@ -150,7 +152,7 @@ def update_value(self, new_value: int | None):


class IdracFanSensor(SensorEntity):
def __init__(self, hass, rest: IdracRest, device_info, unique_id, name, index):
def __init__(self, hass, rest: IdracRest, device_info, unique_id, name, member_id):
self.hass = hass
self.rest = rest

Expand All @@ -167,21 +169,24 @@ def __init__(self, hass, rest: IdracRest, device_info, unique_id, name, index):
self._attr_has_entity_name = True

self._attr_native_value = None
self.index = index
self.member_id = member_id

self.rest.register_callback_thermals(self.update_value)

def update_value(self, thermal: dict | None):
if thermal:
self._attr_native_value = thermal['Fans'][self.index]['Reading']
for fan in thermal['Fans']:
if fan['MemberId'] == self.member_id:
self._attr_native_value = fan['Reading']
break
self._attr_available = True
else:
self._attr_available = False
self.schedule_update_ha_state()


class IdracTempSensor(SensorEntity):
def __init__(self, hass, rest: IdracRest, device_info, unique_id, name, index):
def __init__(self, hass, rest: IdracRest, device_info, unique_id, name, member_id):
self.hass = hass
self.rest = rest

Expand All @@ -198,13 +203,16 @@ def __init__(self, hass, rest: IdracRest, device_info, unique_id, name, index):
self._attr_unique_id = unique_id
self._attr_has_entity_name = True
self._attr_native_value = None
self.index = index
self.member_id = member_id

self.rest.register_callback_thermals(self.update_value)

def update_value(self, thermal: dict | None):
if thermal:
self._attr_native_value = thermal['Temperatures'][self.index]['ReadingCelsius']
for temp in thermal['Temperatures']:
if temp['MemberId'] == self.member_id:
self._attr_native_value = temp['ReadingCelsius']
break
self._attr_available = True
else:
self._attr_available = False
Expand Down

0 comments on commit e757d6e

Please sign in to comment.