Skip to content

Commit

Permalink
V2025.01.03b1
Browse files Browse the repository at this point in the history
To work around issue #103
- Add persistent notification providing the yaml to create the the equivalent of the custom card using a manual card.
petergridge authored Jan 9, 2025
1 parent 58fefbb commit 4d702d3
Showing 3 changed files with 197 additions and 26 deletions.
2 changes: 1 addition & 1 deletion custom_components/irrigationprogram/manifest.json
Original file line number Diff line number Diff line change
@@ -8,5 +8,5 @@
"iot_class": "local_polling",
"issue_tracker": "https://github.com/petergridge/irrigation-v5/issues",
"requirements": [],
"version": "V2025.01.02b60"
"version": "V2025.01.03b1"
}
173 changes: 173 additions & 0 deletions custom_components/irrigationprogram/program.py
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import logging
from zoneinfo import ZoneInfo

from homeassistant.components.persistent_notification import async_create
from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchEntity
from homeassistant.const import EVENT_HOMEASSISTANT_STOP, MATCH_ALL
from homeassistant.core import HomeAssistant, callback
@@ -83,6 +84,178 @@ def __init__(

self._localtimezone = ZoneInfo(self._hass.config.time_zone)

# def generate_card (self):
"""Create card config."""

def add_entity(object,conditions,simple=False):
if object:
data = ""
data += "- type: conditional" + chr(10)
data += " conditions:" + chr(10)
for condition in conditions:
x = 1
for k, v in condition.items():
if x == 1:
data += " - "
x=2
else:
data += " "
data += k + ": " + v + chr(10)
data += " row:" + chr(10)
if simple:
data += " type: " + "simple-entity" + chr(10)
data += " entity: " + object.entity_id + chr(10)

return data
return ""

def add_entity_2(object,conditions,simple=False):
if object:
data = ""
data += "- type: conditional" + chr(10)
data += " conditions:" + chr(10)
for condition in conditions:
x = 1
for k, v in condition.items():
if x == 1:
data += " - "
x=2
else:
data += " "
data += k + ": " + v + chr(10)
data += " row:" + chr(10)
if simple:
data += " type: " + "simple-entity" + chr(10)
data += " entity: " + object + chr(10)

return data
return ""

card:str = "### Copy into manual card"+chr(10)
card += "```"+chr(10)
card += "type: entities" + chr(10)
card += "entities:" + chr(10)
card += "- type: conditional" + chr(10)
card += " conditions:" + chr(10)
card += " - entity: " + self._device_id + chr(10)
card += " state: off" + chr(10)
card += " row:" + chr(10)
card += " type: buttons" + chr(10)
card += " entities: " + chr(10)
card += " - entity: " + self._device_id+ chr(10)
card += " show_name: true" + chr(10)
card += " - entity: " + self._program.config.entity_id + chr(10)
card += " show_name: true" + chr(10)
card += "- type: conditional" + chr(10)
card += " conditions:" + chr(10)
card += " - entity: " + self._device_id + chr(10)
card += " state: on" + chr(10)
card += " row:" + chr(10)
card += " type: buttons" + chr(10)
card += " entities: " + chr(10)
card += " - entity: " + self._device_id + chr(10)
card += " show_name: true" + chr(10)
card += " - entity: " + self._program.config.entity_id + chr(10)
card += " show_name: true" + chr(10)
card += " - entity: " + self._program.pause.entity_id + chr(10)
card += " show_name: true" + chr(10)

condition = [{ "entity": self._program.config.entity_id, "state_not": "on" },{ "entity": "showconfig", "state_not": "on" }]
card += add_entity(self._program.start_time,condition,True)

condition = [{ "entity": self._program.config.entity_id, "state": "on" }]
if self._program.sunrise_offset or self._program.sunset_offset:
card += add_entity(self._program.start_time,condition,True)
else:
card += add_entity(self._program.start_time,condition)
card += add_entity(self._program.sunrise_offset,condition)
card += add_entity(self._program.sunset_offset,condition)

condition = [{ "entity": self._device_id, "state": "on" }]
card += add_entity(self._program.remaining_time,condition)

condition = [{ "entity": self._program.config.entity_id, "state": "on" }]
card += add_entity(self._program.enabled,condition)
card += add_entity(self._program.frequency,condition)
card += add_entity(self._program.inter_zone_delay,condition)

#now process the zones
for zone in self._zones:
card += "- type: section" + chr(10)
card += " label: ''" + chr(10)

card += "- type: conditional" + chr(10)
card += " conditions:" + chr(10)
card += " - entity: " + zone.zone + chr(10)
card += " state: off" + chr(10)
card += " row:" + chr(10)
card += " type: buttons" + chr(10)
card += " entities: " + chr(10)
card += " - entity: " + zone.zone + chr(10)
card += " show_name: true" + chr(10)
card += " show_icon: true" + chr(10)
card += " tap_action: " + chr(10)
card += " action: call-service" + chr(10)
card += " service: switch.toggle" + chr(10)
card += " service_data:" + chr(10)
card += " entity_id: " + zone.zone + chr(10)
card += " - entity: " + zone.config.entity_id + chr(10)
card += " show_name: true" + chr(10)


card += "- type: conditional" + chr(10)
card += " conditions:" + chr(10)
card += " - entity: " + zone.zone + chr(10)
card += " state_not: off" + chr(10)
card += " row:" + chr(10)
card += " type: buttons" + chr(10)
card += " entities: " + chr(10)
card += " - entity: " + zone.zone + chr(10)
card += " show_name: true" + chr(10)
card += " show_icon: true" + chr(10)
card += " tap_action: " + chr(10)
card += " action: call-service" + chr(10)
card += " service: switch.toggle" + chr(10)
card += " service_data:" + chr(10)
card += " entity_id: " + zone.zone + chr(10)
card += " - entity: " + zone.config.entity_id + chr(10)
card += " show_name: true" + chr(10)
card += " - entity: " + zone.status.entity_id + chr(10)
card += " show_name: false" + chr(10)

condition = [{ "entity": "zonestatus", "state": '["off"]'} ]
card += add_entity(zone.next_run,condition)

condition = [{ "entity": "zonestatus", "state_not": '["off", "on", "pending", "eco"]'} ]
card += add_entity(zone.status,condition)

condition = [{ "entity": "zonestatus", "state": '["on","eco","pending"]'} ]
card += add_entity(zone.switch,condition)

condition = [{ "entity": "zonestatus", "state_not": '["on", "eco", "pending"]' },{ "entity": zone.config.entity_id, "state": "on" }]
card += add_entity(zone.last_ran,condition)

condition = [{ "entity": zone.config.entity_id, "state": "on" }]
card += add_entity(zone.enabled,condition)
card += add_entity(zone.frequency,condition)
card += add_entity(zone.water,condition)
card += add_entity(zone.wait,condition)
card += add_entity(zone.repeat,condition)
card += add_entity_2(zone.flow_sensor,condition)
card += add_entity_2(zone.adjustment,condition)
card += add_entity_2(zone.rain_sensor,condition)
card += add_entity_2(zone.water_source,condition)
card += add_entity(zone.ignore_sensors,condition)

card += "```" + chr(10)

#create the persistent notification
async_create(
hass,
message=card,
title="Irrigation Controller"
)

async def async_will_remove_from_hass(self) -> None:
"""Cancel next update."""
if self._unsub_point_in_time:
48 changes: 23 additions & 25 deletions custom_components/irrigationprogram/www/irrigation-card.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
class IrrigationCard extends HTMLElement {

setConfig(config) {
if (this.lastElementChild) this.removeChild(this.lastElementChild);
const cardConfig = Object.assign({}, config);
if (!cardConfig.card) cardConfig.card = {};
if (!cardConfig.card.type) cardConfig.card.type = "entities";
if (!cardConfig.entities_vars)
cardConfig.entities_vars = { type: "entity" };
const element = document.createElement('hui-entities-card');
this._config = JSON.parse(JSON.stringify(cardConfig));
customElements.whenDefined("card-mod").then(() => {
customElements
.get("card-mod")
.applyToElement(element, "card-mod-card", this._config.card_mod.style);
});
this.appendChild(element);
}

set hass(hass) {
const config = this._config;
config.card.title = config.title;
// https://www.home-assistant.io/lovelace/header-footer/
// config.card.header = config.header;
// config.card.footer = config.footer;
// config.card.icon = config.icon;
// config.card.theme = config.theme;
config.card.header = config.header;
config.card.footer = config.footer;
config.card.icon = config.icon;
config.card.theme = config.theme;
config.card.show_header_toggle = false;
config.card.state_color = true;
let doErrors = [];
@@ -16,8 +33,6 @@ class IrrigationCard extends HTMLElement {
let zones = [];
let entities = [];

console.log("card:constructor()");

const x = hass.states[config.program];
if (!x) {
validconfig == "invalid";
@@ -27,7 +42,7 @@ class IrrigationCard extends HTMLElement {
});
} else {
validconfig = "valid";
console.log("valide program");
console.log("valid program");
}

if (validconfig === "valid") {
@@ -278,23 +293,6 @@ class IrrigationCard extends HTMLElement {

}

setConfig(config) {
if (this.lastElementChild) this.removeChild(this.lastElementChild);
const cardConfig = Object.assign({}, config);
if (!cardConfig.card) cardConfig.card = {};
if (!cardConfig.card.type) cardConfig.card.type = "entities";
if (!cardConfig.entities_vars)
cardConfig.entities_vars = { type: "entity" };
const element = document.createElement('hui-entities-card');
this._config = JSON.parse(JSON.stringify(cardConfig));
customElements.whenDefined("card-mod").then(() => {
customElements
.get("card-mod")
.applyToElement(element, "card-mod-card", this._config.card_mod.style);
});
this.appendChild(element);
}

static getConfigElement() {
return document.createElement("irrigation-card-editor");
}
@@ -516,6 +514,6 @@ window.customCards = window.customCards || [];
window.customCards.push({
type: "irrigation-card",
name: "Irrigation Card",
preview: true, // Optional - defaults to false
preview: false, // Optional - defaults to false
description: "Custom card companion to Irrigation Custom Component", // Optional
});

0 comments on commit 4d702d3

Please sign in to comment.