2020-05-17 10:15:06 +00:00
|
|
|
"""Helper functions for Acmeda Pulse."""
|
2022-01-03 18:21:48 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2022-05-18 09:55:08 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr
|
2022-01-03 18:21:48 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-05-17 10:15:06 +00:00
|
|
|
|
|
|
|
from .const import DOMAIN, LOGGER
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_add_acmeda_entities(
|
2022-01-03 18:21:48 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
entity_class: type,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
current: set[int],
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
2020-05-17 10:15:06 +00:00
|
|
|
):
|
|
|
|
"""Add any new entities."""
|
|
|
|
hub = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
LOGGER.debug("Looking for new %s on: %s", entity_class.__name__, hub.host)
|
|
|
|
|
|
|
|
api = hub.api.rollers
|
|
|
|
|
|
|
|
new_items = []
|
|
|
|
for unique_id, roller in api.items():
|
|
|
|
if unique_id not in current:
|
|
|
|
LOGGER.debug("New %s %s", entity_class.__name__, unique_id)
|
|
|
|
new_item = entity_class(roller)
|
|
|
|
current.add(unique_id)
|
|
|
|
new_items.append(new_item)
|
|
|
|
|
|
|
|
async_add_entities(new_items)
|
|
|
|
|
|
|
|
|
2022-01-03 18:21:48 +00:00
|
|
|
async def update_devices(hass: HomeAssistant, config_entry: ConfigEntry, api):
|
2020-05-17 10:15:06 +00:00
|
|
|
"""Tell hass that device info has been updated."""
|
2022-05-18 09:55:08 +00:00
|
|
|
dev_registry = dr.async_get(hass)
|
2020-05-17 10:15:06 +00:00
|
|
|
|
|
|
|
for api_item in api.values():
|
|
|
|
# Update Device name
|
2021-01-07 12:49:45 +00:00
|
|
|
device = dev_registry.async_get_device(identifiers={(DOMAIN, api_item.id)})
|
2020-05-17 10:15:06 +00:00
|
|
|
if device is not None:
|
|
|
|
dev_registry.async_update_device(
|
2020-08-27 11:56:20 +00:00
|
|
|
device.id,
|
|
|
|
name=api_item.name,
|
2020-05-17 10:15:06 +00:00
|
|
|
)
|