-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(BusMonitor): Add first working version of KNX Bus Monitor
- Loading branch information
Showing
10 changed files
with
277 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
"""Constants for KNX Panel.""" | ||
from typing import Final | ||
from typing import Awaitable, Callable, Final | ||
|
||
from xknx.telegram import Telegram | ||
|
||
KNX_DOMAIN: Final = "knx" | ||
|
||
AsyncMessageCallbackType = Callable[[Telegram], Awaitable[None]] | ||
MessageCallbackType = Callable[[Telegram], None] |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
import { KnxInfo } from "@typing/websocket"; | ||
import { KNXInfo, KNXTelegram } from "@typing/websocket"; | ||
import { HomeAssistant } from "custom-card-helpers"; | ||
|
||
export const getKnxInfo = (hass: HomeAssistant): Promise<KnxInfo> => | ||
export const getKnxInfo = (hass: HomeAssistant): Promise<KNXInfo> => | ||
hass.callWS({ | ||
type: "knx_panel/info", | ||
}); | ||
|
||
export const subscribeKnxTelegrams = ( | ||
hass: HomeAssistant, | ||
callback: (telegram: KNXTelegram) => void | ||
) => | ||
hass.connection.subscribeMessage<KNXTelegram>(callback, { | ||
type: "knx_panel/subscribe_telegrams", | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
export interface KnxInfo { | ||
export interface KNXInfo { | ||
version: string; | ||
connected: boolean; | ||
current_address: string; | ||
} | ||
|
||
export interface KNXTelegram { | ||
destination_address: string; | ||
source_address: string; | ||
payload: any; | ||
direction: string; | ||
timestamp: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { subscribeKnxTelegrams } from "@services/websocket.service"; | ||
import { KNXTelegram } from "@typing/websocket"; | ||
import { HomeAssistant } from "custom-card-helpers"; | ||
import { css, html, LitElement, TemplateResult } from "lit"; | ||
import { state } from "lit-element"; | ||
import { customElement, property } from "lit/decorators.js"; | ||
|
||
@customElement("knx-bus-monitor") | ||
export class KNXBusMonitor extends LitElement { | ||
@property({ type: Object }) public hass!: HomeAssistant; | ||
@property({ type: Boolean, reflect: true }) public narrow!: boolean; | ||
@state() private telegrams: KNXTelegram[] = []; | ||
@state() private subscribed?: () => void; | ||
|
||
public disconnectedCallback() { | ||
super.disconnectedCallback(); | ||
if (this.subscribed) { | ||
this.subscribed(); | ||
this.subscribed = undefined; | ||
} | ||
} | ||
|
||
protected async firstUpdated() { | ||
if (!this.subscribed) { | ||
this.subscribed = await subscribeKnxTelegrams(this.hass, (message) => | ||
this.telegram_callback(message) | ||
); | ||
this.telegrams = []; | ||
} | ||
} | ||
|
||
protected telegram_callback(telegram: KNXTelegram): void { | ||
this.telegrams.push(telegram); | ||
this.requestUpdate(); | ||
} | ||
|
||
protected render(): TemplateResult | void { | ||
return html` | ||
<ha-card class="knx-info" header="KNX Bus Monitor"> | ||
${this.telegrams.map( | ||
(telegram) => html` | ||
<div class="telegram"> | ||
<div>${telegram.destination_address}</div> | ||
<div>${telegram.source_address}</div> | ||
<div>${telegram.payload}</div> | ||
<div>${telegram.direction}</div> | ||
<div>${telegram.timestamp}</div> | ||
</div> | ||
` | ||
)} | ||
</ha-card> | ||
`; | ||
} | ||
|
||
static get styles() { | ||
return css` | ||
.telegram { | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: space-between; | ||
} | ||
`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters