Mariusthvdb / custom-ui

Add templates and icon_color to Home Assistant UI
163 stars 30 forks source link

HA 2023.11 breaks more info hide_attributes, open call to assist #111

Closed Mariusthvdb closed 11 months ago

Mariusthvdb commented 11 months ago

as of 2023.11, the previous fix of hiding attributes in the more-info panel has broken. Please help us out and check for any DOM path changes that could be causing below script to no longer hide those attributes:

const Name = "Custom more-info";
const Version = "20230912";
const Description = "previously custom-ui";
const Url = "https://github.com/Mariusthvdb/custom-ui";

// Log information about the custom-ui component
console.info(
  `%c  ${Name}  \n%c  Version ${Version} ${Description}`,
  "color: gold; font-weight: bold; background: black",
  "color: white; font-weight: bold; background: steelblue"
);

// Update the more-info dialog to hide certain attributes based on custom logic
function updateMoreInfo(ev) {
  if (!ev.detail.expanded) return;
  console.log("updateMoreInfo function called");
  const moreInfoInfo = document
    .querySelector("home-assistant")
    .shadowRoot.querySelector("ha-more-info-dialog")
    .shadowRoot.querySelector("ha-dialog")
    .getElementsByClassName("content")[0]
    .querySelector("ha-more-info-info");

  try {
    let t;
    {
      let moreInfoNodeName;
      const contentChild = moreInfoInfo.shadowRoot.querySelector(
        "more-info-content"
      ).childNodes;
      for (const nodeItem of contentChild) {
        if (nodeItem.nodeName.toLowerCase().startsWith("more-info-")) {
          moreInfoNodeName = nodeItem.nodeName.toLowerCase();
          break;
        }
      }
      if (moreInfoNodeName == "more-info-group") {
        let moreInfoNestedNodeName;
        const contentChildNested =
          moreInfoInfo.shadowRoot
            .querySelector("more-info-group")
            .shadowRoot.childNodes;
        for (const nodeItemNested of contentChildNested) {
          if (
            nodeItemNested.nodeName.toLowerCase().startsWith("more-info-")
          ) {
            moreInfoNestedNodeName = nodeItemNested.nodeName.toLowerCase();
            break;
          }
        }
        t = moreInfoInfo.shadowRoot
          .querySelector("more-info-group")
          .shadowRoot.querySelector(moreInfoNestedNodeName)
          .shadowRoot.querySelector("ha-attributes")
          .shadowRoot.querySelectorAll(".data-entry");
      } else {
        t = moreInfoInfo.shadowRoot
          .querySelector(moreInfoNodeName)
          .shadowRoot.querySelector("ha-attributes")
          .shadowRoot.querySelectorAll(".data-entry");
      }
    }
    if (t.length) {
      let e;
      for (const node of t) {
        const o = node.getElementsByClassName("key")[0];
        if (o.innerText.toLowerCase().trim() == "hide attributes") {
// make compatible for both 2023.8 and 2023.9
          const valueContainer = o.parentNode.getElementsByClassName("value")[0];
          const haAttributeValue = valueContainer.querySelector('ha-attribute-value');
          const text = haAttributeValue
            ? haAttributeValue.shadowRoot.textContent
            : valueContainer.innerText;
          e = text
            .split(",")
            .map((item) => item.replace("_", " ").trim());
          e.push("hide attributes");
        }
      }
      for (const node of t) {
        const o = node.getElementsByClassName("key")[0];
        if (
          e.includes(o.innerText.toLowerCase().trim()) ||
          e.includes("all")
        ) {
          o.parentNode.style.display = "none";
        }
      }
    }
  } catch (err) {}
}

window.addEventListener("expanded-changed", updateMoreInfo);

window.CUSTOM_UI_LIST = window.CUSTOM_UI_LIST || [];
window.CUSTOM_UI_LIST.push({
  name: Name,
  version: `${Version} ${Description}`,
  url: Url
});

// original, valid in 2023.8.4
//         if (o.innerText.toLowerCase().trim() == "hide attributes") {
//           e = o.parentNode
//             .getElementsByClassName("value")[0]
//             .innerText.split(",")
//             .map((item) => item.replace("_", " ").trim());
//           e.push("hide attributes");
//         }

// fix for 2023.9
// e = o.parentNode
//     .getElementsByClassName("value")[0]
//     .querySelector('ha-attribute-value')
//     .shadowRoot
//     .textContent.split(",")
//     .map((item) => item.replace("_", " ").trim());
// e.push("hide attributes");

// https://github.com/home-assistant/frontend/tree/dev/src/dialogs/more-info

// function updateMoreInfo(ev) {
//   if (!ev.detail.expanded) return;
//   // Find the element with the class "content"
//   const contentElement = document
//     .querySelector("home-assistant")
//     .shadowRoot.querySelector("ha-more-info-dialog")
//     .shadowRoot.querySelector("ha-dialog")
//     .getElementsByClassName("content")[0];
//
//   if (!contentElement) return;
//
//   // Find the ha-more-info-info element within content
//   const moreInfoInfo = contentElement.querySelector("ha-more-info-info");
//
Mariusthvdb commented 11 months ago

seems to function after all and probably a cache issue testing the other open 2023.11 issue. pardon the confusion