Skip to content

Commit

Permalink
fix problem with sensors being added with AddEntitiesCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
ateodorescu committed Mar 13, 2024
1 parent 92ce42c commit 64b10a7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 55 deletions.
2 changes: 1 addition & 1 deletion custom_components/ipmi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def _unique_id_from_status(device_info: IpmiDeviceInfo) -> str | None:

unique_id_group = []
if product_id:
product_id = re.sub("\(.*?\)", '', product_id)
product_id = re.sub("(.*?)", '', product_id)
unique_id_group.append(product_id)
if alias:
unique_id_group.append(alias)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ipmi/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"iot_class": "local_polling",
"issue_tracker": "https://github.com/ateodorescu/home-assistant-ipmi/issues",
"requirements": ["python-ipmi"],
"version": "1.3.1",
"version": "1.4.0",
"zeroconf": ["_ipmi._tcp.local."]
}
105 changes: 52 additions & 53 deletions custom_components/ipmi/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
Expand Down Expand Up @@ -63,13 +62,62 @@ def _get_ipmi_device_info(data: IpmiServer) -> DeviceInfo:

return cast(DeviceInfo, ipmi_infos)

def create_entity_sensors(ipmi_data: object, filter: list = []) -> dict:

async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the IPMI sensors."""

server_id = config_entry.entry_id
ipmiserver = get_ipmi_server(hass, server_id)

if (ipmiserver):
coordinator = ipmiserver[COORDINATOR]
data = ipmiserver[IPMI_DATA]
unique_id = ipmiserver[IPMI_UNIQUE_ID]
async_add_entities([
IpmiSensor(
coordinator,
SensorEntityDescription(
key=KEY_STATUS,
name="State",
icon="mdi:power",
entity_registry_enabled_default=True,
),
data,
unique_id,
)
])

_LOGGER.debug("Sensors added")

@callback
async def async_new_sensors(new_entities):
"""Set up IPMI sensors."""
await create_entity_sensors(ipmiserver, new_entities, async_add_entities)

get_ipmi_data(hass)[DISPATCHERS][server_id].append(
async_dispatcher_connect(
hass,
IPMI_NEW_SENSOR_SIGNAL.format(server_id),
async_new_sensors,
)
)
_LOGGER.debug("New entity listener created")

@callback
async def create_entity_sensors(
ipmi_data: object,
filter: list,
async_add_entities: AddEntitiesCallback,
) -> None:
coordinator = ipmi_data[COORDINATOR]
data = ipmi_data[IPMI_DATA]
unique_id = ipmi_data[IPMI_UNIQUE_ID]
status = coordinator.data
entities = []
# _LOGGER.critical(status)

for id in status.sensors.get("temperature"):
if (id in filter):
Expand Down Expand Up @@ -176,57 +224,8 @@ def create_entity_sensors(ipmi_data: object, filter: list = []) -> dict:
)
)

return entities

async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the IPMI sensors."""

server_id = config_entry.entry_id
ipmiserver = get_ipmi_server(hass, server_id)
coordinator = ipmiserver[COORDINATOR]
data = ipmiserver[IPMI_DATA]
unique_id = ipmiserver[IPMI_UNIQUE_ID]
async_add_entities([
IpmiSensor(
coordinator,
SensorEntityDescription(
key=KEY_STATUS,
name="State",
icon="mdi:power",
entity_registry_enabled_default=True,
),
data,
unique_id,
)
])

_LOGGER.debug("Sensors added")

registry = er.async_get(hass)

@callback
def async_new_sensors(new_entities):
_async_add_entities(hass, registry, async_add_entities, server_id, new_entities)
async_add_entities(entities, True)

unsub = async_dispatcher_connect(
hass, IPMI_NEW_SENSOR_SIGNAL.format(server_id), async_new_sensors
)
get_ipmi_data(hass)[DISPATCHERS][server_id].append(unsub)
_LOGGER.debug("New entity listener created")

@callback
def _async_add_entities(hass, registry, async_add_entities, server_id, new_entities):
"""Set up IPMI sensors."""
ipmiserver = get_ipmi_server(hass, server_id)

if (ipmiserver):
_LOGGER.debug("New entities: %s", new_entities)
entities = create_entity_sensors(ipmiserver, new_entities)
async_add_entities(entities, True)


class IpmiSensor(CoordinatorEntity[DataUpdateCoordinator[dict[str, str]]], SensorEntity):
Expand Down

0 comments on commit 64b10a7

Please sign in to comment.