Skip to content

Commit

Permalink
Allow for custom device names
Browse files Browse the repository at this point in the history
  • Loading branch information
gadget-man committed Jul 9, 2021
1 parent c8dea75 commit fd92a7a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export class BoilerplateCardEditor extends LitElement implements LovelaceCardEdi
const device = await this.getDeviceDetails(this.hass, value);
this._config = {
...this._config,
"device_name": device.name,
"device_name": device.name_by_user ? device.name_by_user?.replace(/ |-/g,"_").toLowerCase() : device.name?.replace(/ |-/g,"_").toLowerCase(),
};
}
this._config = {
Expand Down
35 changes: 20 additions & 15 deletions src/iparcelbox-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,18 +399,19 @@ export class IParcelBoxCard extends LitElement {
getEntities(): any {
const newEntities: EntityList = [];
const deviceId = this.config.device_name;
const mac = deviceId.split("-")[1]
// console.log("Got Device ID: " + deviceId);
// const mac = deviceId //.split("-")[1]

const e = {
type: "sensor",
entity: "sensor.iparcelbox_" + mac + "_boxstatus"
entity: "sensor." + deviceId + "_boxstatus"
}
newEntities.push(e);

Object.keys(this.config.status).forEach(key => {
const e = {
type: this.config.status[key]["type"],
entity: this.config.status[key]["type"] + ".iparcelbox_" + mac + "_" + this.config.status[key]["key"],
entity: this.config.status[key]["type"] + "." + deviceId + "_" + this.config.status[key]["key"],
};
newEntities.push(e)
});
Expand All @@ -419,7 +420,7 @@ export class IParcelBoxCard extends LitElement {
Object.keys(this.config.attributes.right).forEach(key => {
const e = {
type: this.config.attributes.right[key]["type"],
entity: this.config.attributes.right[key]["type"] + ".iparcelbox_" + mac + "_" + this.config.attributes.right[key]["key"],
entity: this.config.attributes.right[key]["type"] + "." + deviceId + "_" + this.config.attributes.right[key]["key"],
};
newEntities.push(e)
});
Expand All @@ -431,8 +432,10 @@ export class IParcelBoxCard extends LitElement {

set hass(hass) {
const deviceId = this.config.device_name;
const mac = deviceId.split("-")
const statusSensor = "sensor.iparcelbox_" + mac[1] + "_boxstatus"
// const mac = deviceId.split("-")
// const statusSensor = "sensor.iparcelbox_" + mac[1] + "_boxstatus"

const statusSensor = "sensor." + deviceId + "_boxstatus"

if (hass && this.config) {
this.stateObj = statusSensor in hass.states ? hass.states[statusSensor] : null;
Expand Down Expand Up @@ -545,11 +548,11 @@ export class IParcelBoxCard extends LitElement {

// https://lit-element.polymer-project.org/guide/styles
renderLabel(data): any {
console.log("RenderLabel: " + JSON.stringify(data))
// console.log("RenderLabel: " + JSON.stringify(data))
const deviceId = this.config.device_name;
const mac = deviceId.split("-")
const sensor = data.type + ".iparcelbox_" + mac[1] + "_" + data.key
console.log("Render label for sensor: " + sensor);
// const mac = deviceId.split("-")
const sensor = data.type + "." + deviceId + "_" + data.key
// console.log("Render label for sensor: " + sensor);
const value = this._hass.states[sensor].state
// console.log("Label: " + sensor + " (" + value + ")")
// console.log("LabelData: " + JSON.stringify(this._hass.states[sensor]))
Expand All @@ -563,9 +566,9 @@ export class IParcelBoxCard extends LitElement {
renderAttribute(data): any {
// console.log("RenderAttribute: " + JSON.stringify(data))
const deviceId = this.config.device_name;
const mac = deviceId.split("-")
const sensor = data.type + ".iparcelbox_" + mac[1] + "_" + data.key
const sensor2 = data.key2 ? data.type2 + ".iparcelbox_" + mac[1] + "_" + data.key2 : null
// const mac = deviceId.split("-")
const sensor = data.type + "." + deviceId + "_" + data.key
const sensor2 = data.key2 ? data.type2 + "." + deviceId + "_" + data.key2 : null
const isBattery = data.key == 'battery' ? true : false
const isSleep = data.key == 'asleep' ? true : false

Expand Down Expand Up @@ -630,8 +633,10 @@ export class IParcelBoxCard extends LitElement {

renderStatus(data): any {
const deviceId = this.config.device_name;
const mac = deviceId.split("-")[1]
const sensorId = data.type + ".iparcelbox_" + mac + "_" + data.key
// const mac = deviceId.split("-")[1]
// const sensorId = data.type + ".iparcelbox_" + mac + "_" + data.key

const sensorId = data.type + "." + deviceId + "_" + data.key

const computeFunc = data.compute || (v => v);

Expand Down

0 comments on commit fd92a7a

Please sign in to comment.