import re
import html
import httpx

def obtener_inventario(host, auth):
    respuesta = httpx.get(
        host.rstrip("/") + "/config/lmp",
        headers={"Authorization": auth},
        timeout=15
    )
    respuesta.raise_for_status()

    patron = r'id="name_lmp_([^"]+)"[^>]*>(.*?)</a>'

    coincidencias = re.findall(
        patron,
        respuesta.text,
        flags=re.IGNORECASE | re.DOTALL
    )

    canales = {}

    for identificador, contenido in coincidencias:
        nombre = re.sub(r"<[^>]+>", "", contenido)
        nombre = html.unescape(nombre).strip()

        if identificador and nombre:
            canales[identificador] = nombre

    if not canales:
        raise ValueError("Inventario LMP vacio")

    return canales
