Use new subscribe mixin for the sidebar to show counts of repairs (#13282)

* Use new subscribe mixin for the sidebar

* spelling
This commit is contained in:
Zack Barett 2022-07-26 15:43:46 -05:00 committed by GitHub
parent b443ec0af5
commit adf3fa6a0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 7 deletions

View File

@ -21,6 +21,7 @@ import "@polymer/paper-item/paper-icon-item";
import type { PaperIconItemElement } from "@polymer/paper-item/paper-icon-item"; import type { PaperIconItemElement } from "@polymer/paper-item/paper-icon-item";
import "@polymer/paper-item/paper-item"; import "@polymer/paper-item/paper-item";
import "@polymer/paper-listbox/paper-listbox"; import "@polymer/paper-listbox/paper-listbox";
import { UnsubscribeFunc } from "home-assistant-js-websocket";
import { import {
css, css,
CSSResult, CSSResult,
@ -44,7 +45,9 @@ import {
PersistentNotification, PersistentNotification,
subscribeNotifications, subscribeNotifications,
} from "../data/persistent_notification"; } from "../data/persistent_notification";
import { subscribeRepairsIssueRegistry } from "../data/repairs";
import { updateCanInstall, UpdateEntity } from "../data/update"; import { updateCanInstall, UpdateEntity } from "../data/update";
import { SubscribeMixin } from "../mixins/subscribe-mixin";
import { actionHandler } from "../panels/lovelace/common/directives/action-handler-directive"; import { actionHandler } from "../panels/lovelace/common/directives/action-handler-directive";
import { haStyleScrollbar } from "../resources/styles"; import { haStyleScrollbar } from "../resources/styles";
import type { HomeAssistant, PanelInfo, Route } from "../types"; import type { HomeAssistant, PanelInfo, Route } from "../types";
@ -177,7 +180,7 @@ const computePanels = memoizeOne(
let Sortable; let Sortable;
@customElement("ha-sidebar") @customElement("ha-sidebar")
class HaSidebar extends LitElement { class HaSidebar extends SubscribeMixin(LitElement) {
@property({ attribute: false }) public hass!: HomeAssistant; @property({ attribute: false }) public hass!: HomeAssistant;
@property({ type: Boolean, reflect: true }) public narrow!: boolean; @property({ type: Boolean, reflect: true }) public narrow!: boolean;
@ -192,6 +195,8 @@ class HaSidebar extends LitElement {
@state() private _updatesCount = 0; @state() private _updatesCount = 0;
@state() private _issuesCount = 0;
@state() private _renderEmptySortable = false; @state() private _renderEmptySortable = false;
private _mouseLeaveTimeout?: number; private _mouseLeaveTimeout?: number;
@ -214,6 +219,16 @@ class HaSidebar extends LitElement {
private _sortable?; private _sortable?;
public hassSubscribe(): UnsubscribeFunc[] {
return [
subscribeRepairsIssueRegistry(this.hass.connection!, (repairs) => {
this._issuesCount = repairs.issues.filter(
(issue) => !issue.ignored
).length;
}),
];
}
protected render() { protected render() {
if (!this.hass) { if (!this.hass) {
return html``; return html``;
@ -238,6 +253,7 @@ class HaSidebar extends LitElement {
changedProps.has("alwaysExpand") || changedProps.has("alwaysExpand") ||
changedProps.has("_externalConfig") || changedProps.has("_externalConfig") ||
changedProps.has("_updatesCount") || changedProps.has("_updatesCount") ||
changedProps.has("_issuesCount") ||
changedProps.has("_notifications") || changedProps.has("_notifications") ||
changedProps.has("editMode") || changedProps.has("editMode") ||
changedProps.has("_renderEmptySortable") || changedProps.has("_renderEmptySortable") ||
@ -511,17 +527,20 @@ class HaSidebar extends LitElement {
> >
<paper-icon-item class="configuration" role="option"> <paper-icon-item class="configuration" role="option">
<ha-svg-icon slot="item-icon" .path=${mdiCog}></ha-svg-icon> <ha-svg-icon slot="item-icon" .path=${mdiCog}></ha-svg-icon>
${!this.alwaysExpand && this._updatesCount > 0 ${!this.alwaysExpand &&
(this._updatesCount > 0 || this._issuesCount > 0)
? html` ? html`
<span class="configuration-badge" slot="item-icon"> <span class="configuration-badge" slot="item-icon">
${this._updatesCount} ${this._updatesCount + this._issuesCount}
</span> </span>
` `
: ""} : ""}
<span class="item-text">${title}</span> <span class="item-text">${title}</span>
${this.alwaysExpand && this._updatesCount > 0 ${this.alwaysExpand && (this._updatesCount > 0 || this._issuesCount > 0)
? html` ? html`
<span class="configuration-badge">${this._updatesCount}</span> <span class="configuration-badge"
>${this._updatesCount + this._issuesCount}</span
>
` `
: ""} : ""}
</paper-icon-item> </paper-icon-item>

View File

@ -1,5 +1,9 @@
import type { Connection } from "home-assistant-js-websocket";
import { createCollection } from "home-assistant-js-websocket";
import type { Store } from "home-assistant-js-websocket/dist/store";
import { debounce } from "../common/util/debounce";
import type { HomeAssistant } from "../types"; import type { HomeAssistant } from "../types";
import { DataEntryFlowStep } from "./data_entry_flow"; import type { DataEntryFlowStep } from "./data_entry_flow";
export interface RepairsIssue { export interface RepairsIssue {
domain: string; domain: string;
@ -61,3 +65,36 @@ export const handleRepairsFlowStep = (
export const deleteRepairsFlow = (hass: HomeAssistant, flowId: string) => export const deleteRepairsFlow = (hass: HomeAssistant, flowId: string) =>
hass.callApi("DELETE", `repairs/issues/fix/${flowId}`); hass.callApi("DELETE", `repairs/issues/fix/${flowId}`);
export const fetchRepairsIssueRegistry = (conn: Connection) =>
conn.sendMessagePromise<{ issues: RepairsIssue[] }>({
type: "repairs/list_issues",
});
const subscribeRepairsIssueUpdates = (
conn: Connection,
store: Store<{ issues: RepairsIssue[] }>
) =>
conn.subscribeEvents(
debounce(
() =>
fetchRepairsIssueRegistry(conn).then((repairs) =>
store.setState(repairs, true)
),
500,
true
),
"repairs_issue_registry_updated"
);
export const subscribeRepairsIssueRegistry = (
conn: Connection,
onChange: (repairs: { issues: RepairsIssue[] }) => void
) =>
createCollection<{ issues: RepairsIssue[] }>(
"_repairsIssueRegistry",
fetchRepairsIssueRegistry,
subscribeRepairsIssueUpdates,
conn,
onChange
);