Skip to content

Commit

Permalink
Merge pull request #67 from saniho/AddEcoWattInformation
Browse files Browse the repository at this point in the history
Add eco watt information
  • Loading branch information
saniho authored Jan 3, 2023
2 parents 6e8cc8c + 9eedb55 commit 50f191c
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 0 deletions.
4 changes: 4 additions & 0 deletions content-card-linky-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ export class contentCardLinkyEditor extends LitElement {
get _showTitreLigne() {
return this._config.showTitreLigne !== false;
}
get _showEcoWatt() {
return this._config.showEcoWatt !== false;
}

get _title() {
return this._config.showTitle !== false;
Expand Down Expand Up @@ -173,6 +176,7 @@ export class contentCardLinkyEditor extends LitElement {
${this.renderSwitchOption("Show titre ligne", this._showTitreLigne, "showTitreLigne")}
${this.renderSwitchOption("Show error", this._showError, "showError")}
${this.renderSwitchOption("Show header", this._showHeader, "showHeader")}
${this.renderSwitchOption("Show echo Watt", this._showEcoWatt, "showEcoWatt")}
</ul>
<!-- -->
<paper-input
Expand Down
156 changes: 156 additions & 0 deletions content-card-linky.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ const fireEvent = (node, type, detail, options) => {
return event;
};

const ecoWattForecastValues = new Map([
["Pas de valeur", "green"],
[1, "green"],
[2, "yellow"],
[3, "red"],
]);


function hasConfigOrEntityChanged(element, changedProps) {
if (changedProps.has("config")) {
return true;
Expand Down Expand Up @@ -171,8 +179,10 @@ class ContentCardLinky extends LitElement {
</div>
${this.renderHistory(attributes.daily, attributes.unit_of_measurement, attributes.dailyweek, attributes.dailyweek_cost, attributes.dailyweek_costHC, attributes.dailyweek_costHP, attributes.dailyweek_HC, attributes.dailyweek_HP, this.config)}
${this.renderEcoWatt(attributes, this.config)}
${this.renderError(attributes.errorLastCall, this.config)}
${this.renderVersion(attributes.versionUpdateAvailable, attributes.versionGit)}
${this.renderInformation(attributes, this.config)}
</div>
</ha-card>`
}
Expand Down Expand Up @@ -283,6 +293,19 @@ class ContentCardLinky extends LitElement {
}
}
}
renderInformation(attributes, config) {
if (true) {
if ( attributes.serviceEnedis !== "myElectricalData" ){
return html `
<div class="information-msg" style="color: red">
<ha-icon id="icon" icon="mdi:alert-outline"></ha-icon>
Merci de migrer sur myElectricalData.<br>
EnedisGateway sera desactivé courant 2023.
</div>
`
}
}
}
renderVersion(versionUpdateAvailable, versionGit) {
if ( versionUpdateAvailable === true ){
return html
Expand Down Expand Up @@ -473,6 +496,84 @@ class ContentCardLinky extends LitElement {
}
}
}

getOneDayForecastTime(ecoWattForecast) {
let ecoWattForecastTimeRefBegin = new Date(ecoWattForecast.attributes["begin"]);
let ecoWattForecastTimeRefEnd = new Date(ecoWattForecast.attributes["end"]);
let ecoWattForecastStartTime = ecoWattForecastTimeRefBegin.toLocaleTimeString([], { hour: '2-digit'});
let ecoWattForecastEndTime = ecoWattForecastTimeRefEnd.toLocaleTimeString([], { hour: '2-digit' });

return [ecoWattForecastStartTime, ecoWattForecastEndTime];
}

getOneDayNextEcoWattText(ecoWattForecastEntity) {
for (let [time, value] of Object.entries(
ecoWattForecastEntity.attributes["forecast"]
)) {
if (time != undefined && ecoWattForecastValues.get(value) > 0.1) {
let timeStr = time.replace(/([345])5/g, "$10");
return value + ((time == "0 min") ? " actuellement." : " dans " + timeStr + ".");
}
}
return ""
}

getOneDayNextEcoWatt(ecoWattForecastEntity) {
let ecoWattForecastList = [];
for (let [time, value] of Object.entries(
ecoWattForecastEntity.attributes["forecast"]
)) {
if (time != undefined) {
time = time.replace("h", "").trim();
time = time.replace("min", "").trim();
ecoWattForecastList.push([time, ecoWattForecastValues.get(value), value]);
}
}

return ecoWattForecastList;
}

renderEcoWatt(attributes, config) {
if ( attributes.serviceEnedis !== "myElectricalData" ){
return html `EcoWatt : uniquement disponible avec myElectricData`;
}
if (this.config.showEcoWatt === false ){
return html ``;
}
let sensorName = config.entity + "_ecowatt" ;
const ecoWattForecast = this.hass.states[sensorName];

if (!ecoWattForecast || ecoWattForecast.length === 0) {
return html``;
}

this.numberElements++;

let [startTime, endTime] = this.getOneDayForecastTime(ecoWattForecast);

return html`
<ul class="flow-row oneHourHeader ${this.numberElements > 1 ? " spacer" : ""}">
<li> ${startTime} </li>
<li>${this.getOneDayNextEcoWattText(ecoWattForecast)}</li>
<li> ${endTime} </li>
</ul>
<ul class="flow-row oneHour">
${html`
${this.getOneDayNextEcoWatt(ecoWattForecast).map(
(forecast) => html`
<li class="ecowatt-${forecast[0]}" style="background: ${forecast[1]}" title="${forecast[1]} - ${forecast[0]}" ></li>`
)}
`}
</ul>
<ul class="flow-row oneHourLabel">
${html`
${this.getOneDayNextEcoWatt(ecoWattForecast).map(
(forecast) => html`
<li title="${forecast[0]}">${(forecast[0]%2==1) ? forecast[0] : ''}</li>`
)}
`}
</ul>`;
}

setConfig(config) {
if (!config.entity) {
Expand All @@ -494,13 +595,15 @@ class ContentCardLinky extends LitElement {
showDayHCHP: false,
showDayName: "long",
showError: true,
shoInformation: true,
showPrice: true,
showTitle: false,
showCurrentMonthRatio: true,
showMonthRatio: true,
showWeekRatio: false,
showYesterdayRatio: false,
showTitreLigne: false,
showEcoWatt: false,
titleName: "",
nbJoursAffichage: 7,
kWhPrice: undefined,
Expand Down Expand Up @@ -561,6 +664,12 @@ class ContentCardLinky extends LitElement {
position: relative;
cursor: pointer;
}
ha-card ul {
list-style: none;
padding: 0;
margin: 0;
}
.main-title {
margin: auto;
Expand Down Expand Up @@ -713,6 +822,53 @@ class ContentCardLinky extends LitElement {
visibility: visible;
opacity: 1;
}
.flow-row {
display: flex;
flex-flow: row wrap;
}
/* One Hour Forecast */
.oneHour {
height: 1em;
}
.oneHour > li {
background-color: var(--paper-item-icon-color);
border-right: 1px solid var(--lovelace-background, var(--primary-background-color));
}
.oneHour > li:first-child {
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
}
.oneHour > li:last-child {
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
border: 0;
}
/* One Hour Labels */
.ecowatt-00, .ecowatt-01, .ecowatt-02, .ecowatt-03, .ecowatt-04, .ecowatt-05, .ecowatt-06, .ecowatt-07{
flex: 2 1 0;
}
.ecowatt-08, .ecowatt-09, .ecowatt-10, .ecowatt-11, .ecowatt-12, .ecowatt-13, .ecowatt-14, .ecowatt-15 {
flex: 2 1 0;
}
.ecowatt-16, .ecowatt-17, .ecowatt-18, .ecowatt-19, .ecowatt-20, .ecowatt-21, .ecowatt-22, .ecowatt-23 {
flex: 2 1 0;
}
.oneHourLabel > li:first-child {
flex: 0.70 1 0;
}
.oneHourLabel > li {
flex: 1 1 0;
text-align: left;
}
/* One Hour Header */
.oneHourHeader {
justify-content: space-between;
}
.oneHourHeader li:last-child {
text-align: right;
}
`;
}
}
Expand Down

0 comments on commit 50f191c

Please sign in to comment.