Skip to content

Commit 9b25230

Browse files
committed
price indexes bugfix
1 parent 2d971f1 commit 9b25230

File tree

6 files changed

+326
-212
lines changed

6 files changed

+326
-212
lines changed

.homeycompose/capabilities/spot_price_update_status.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"en": "Spot Price Update Status"
66
},
77
"getable": true,
8-
"setable": false,
8+
"setable": true,
99
"uiComponent": null,
1010
"insights": true,
1111
"insightsTitleTrue": {

app.json

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,7 +1285,7 @@
12851285
],
12861286
"getable": true,
12871287
"setable": false,
1288-
"uiComponent": null,
1288+
"uiComponent": "sensor",
12891289
"icon": "/assets/images/Index_icon.svg"
12901290
},
12911291
"hour_price_index_1": {
@@ -1321,7 +1321,7 @@
13211321
],
13221322
"getable": true,
13231323
"setable": false,
1324-
"uiComponent": null,
1324+
"uiComponent": "sensor",
13251325
"icon": "/assets/images/Index_icon.svg"
13261326
},
13271327
"hour_price_index_10": {
@@ -1357,7 +1357,7 @@
13571357
],
13581358
"getable": true,
13591359
"setable": false,
1360-
"uiComponent": null,
1360+
"uiComponent": "sensor",
13611361
"icon": "/assets/images/Index_icon.svg"
13621362
},
13631363
"hour_price_index_11": {
@@ -1393,7 +1393,7 @@
13931393
],
13941394
"getable": true,
13951395
"setable": false,
1396-
"uiComponent": null,
1396+
"uiComponent": "sensor",
13971397
"icon": "/assets/images/Index_icon.svg"
13981398
},
13991399
"hour_price_index_12": {
@@ -1429,7 +1429,7 @@
14291429
],
14301430
"getable": true,
14311431
"setable": false,
1432-
"uiComponent": null,
1432+
"uiComponent": "sensor",
14331433
"icon": "/assets/images/Index_icon.svg"
14341434
},
14351435
"hour_price_index_13": {
@@ -1465,7 +1465,7 @@
14651465
],
14661466
"getable": true,
14671467
"setable": false,
1468-
"uiComponent": null,
1468+
"uiComponent": "sensor",
14691469
"icon": "/assets/images/Index_icon.svg"
14701470
},
14711471
"hour_price_index_14": {
@@ -1501,7 +1501,7 @@
15011501
],
15021502
"getable": true,
15031503
"setable": false,
1504-
"uiComponent": null,
1504+
"uiComponent": "sensor",
15051505
"icon": "/assets/images/Index_icon.svg"
15061506
},
15071507
"hour_price_index_15": {
@@ -1537,7 +1537,7 @@
15371537
],
15381538
"getable": true,
15391539
"setable": false,
1540-
"uiComponent": null,
1540+
"uiComponent": "sensor",
15411541
"icon": "/assets/images/Index_icon.svg"
15421542
},
15431543
"hour_price_index_16": {
@@ -1573,7 +1573,7 @@
15731573
],
15741574
"getable": true,
15751575
"setable": false,
1576-
"uiComponent": null,
1576+
"uiComponent": "sensor",
15771577
"icon": "/assets/images/Index_icon.svg"
15781578
},
15791579
"hour_price_index_17": {
@@ -1609,7 +1609,7 @@
16091609
],
16101610
"getable": true,
16111611
"setable": false,
1612-
"uiComponent": null,
1612+
"uiComponent": "sensor",
16131613
"icon": "/assets/images/Index_icon.svg"
16141614
},
16151615
"hour_price_index_18": {
@@ -1645,7 +1645,7 @@
16451645
],
16461646
"getable": true,
16471647
"setable": false,
1648-
"uiComponent": null,
1648+
"uiComponent": "sensor",
16491649
"icon": "/assets/images/Index_icon.svg"
16501650
},
16511651
"hour_price_index_19": {
@@ -1681,7 +1681,7 @@
16811681
],
16821682
"getable": true,
16831683
"setable": false,
1684-
"uiComponent": null,
1684+
"uiComponent": "sensor",
16851685
"icon": "/assets/images/Index_icon.svg"
16861686
},
16871687
"hour_price_index_2": {
@@ -1717,7 +1717,7 @@
17171717
],
17181718
"getable": true,
17191719
"setable": false,
1720-
"uiComponent": null,
1720+
"uiComponent": "sensor",
17211721
"icon": "/assets/images/Index_icon.svg"
17221722
},
17231723
"hour_price_index_20": {
@@ -1753,7 +1753,7 @@
17531753
],
17541754
"getable": true,
17551755
"setable": false,
1756-
"uiComponent": null,
1756+
"uiComponent": "sensor",
17571757
"icon": "/assets/images/Index_icon.svg"
17581758
},
17591759
"hour_price_index_21": {
@@ -1789,7 +1789,7 @@
17891789
],
17901790
"getable": true,
17911791
"setable": false,
1792-
"uiComponent": null,
1792+
"uiComponent": "sensor",
17931793
"icon": "/assets/images/Index_icon.svg"
17941794
},
17951795
"hour_price_index_22": {
@@ -1825,7 +1825,7 @@
18251825
],
18261826
"getable": true,
18271827
"setable": false,
1828-
"uiComponent": null,
1828+
"uiComponent": "sensor",
18291829
"icon": "/assets/images/Index_icon.svg"
18301830
},
18311831
"hour_price_index_23": {
@@ -1861,7 +1861,7 @@
18611861
],
18621862
"getable": true,
18631863
"setable": false,
1864-
"uiComponent": null,
1864+
"uiComponent": "sensor",
18651865
"icon": "/assets/images/Index_icon.svg"
18661866
},
18671867
"hour_price_index_3": {
@@ -1897,7 +1897,7 @@
18971897
],
18981898
"getable": true,
18991899
"setable": false,
1900-
"uiComponent": null,
1900+
"uiComponent": "sensor",
19011901
"icon": "/assets/images/Index_icon.svg"
19021902
},
19031903
"hour_price_index_4": {
@@ -1933,7 +1933,7 @@
19331933
],
19341934
"getable": true,
19351935
"setable": false,
1936-
"uiComponent": null,
1936+
"uiComponent": "sensor",
19371937
"icon": "/assets/images/Index_icon.svg"
19381938
},
19391939
"hour_price_index_5": {
@@ -1969,7 +1969,7 @@
19691969
],
19701970
"getable": true,
19711971
"setable": false,
1972-
"uiComponent": null,
1972+
"uiComponent": "sensor",
19731973
"icon": "/assets/images/Index_icon.svg"
19741974
},
19751975
"hour_price_index_6": {
@@ -2005,7 +2005,7 @@
20052005
],
20062006
"getable": true,
20072007
"setable": false,
2008-
"uiComponent": null,
2008+
"uiComponent": "sensor",
20092009
"icon": "/assets/images/Index_icon.svg"
20102010
},
20112011
"hour_price_index_7": {
@@ -2041,7 +2041,7 @@
20412041
],
20422042
"getable": true,
20432043
"setable": false,
2044-
"uiComponent": null,
2044+
"uiComponent": "sensor",
20452045
"icon": "/assets/images/Index_icon.svg"
20462046
},
20472047
"hour_price_index_8": {
@@ -2077,7 +2077,7 @@
20772077
],
20782078
"getable": true,
20792079
"setable": false,
2080-
"uiComponent": null,
2080+
"uiComponent": "sensor",
20812081
"icon": "/assets/images/Index_icon.svg"
20822082
},
20832083
"hour_price_index_9": {
@@ -2113,7 +2113,7 @@
21132113
],
21142114
"getable": true,
21152115
"setable": false,
2116-
"uiComponent": null,
2116+
"uiComponent": "sensor",
21172117
"icon": "/assets/images/Index_icon.svg"
21182118
},
21192119
"measure_current_spot_index": {
@@ -2189,7 +2189,7 @@
21892189
"en": "Spot Price Update Status"
21902190
},
21912191
"getable": true,
2192-
"setable": false,
2192+
"setable": true,
21932193
"uiComponent": null,
21942194
"insights": true,
21952195
"insightsTitleTrue": {

drivers/cz-spot-prices/api.js

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ class SpotPriceAPI {
120120
const errorMessage = 'Neplatná device instance pro getDailyPrices';
121121
if (this.logger) {
122122
this.logger.error(errorMessage, new Error(errorMessage));
123-
} else {
124-
this.homey.error(errorMessage);
125123
}
126124
throw new Error(errorMessage);
127125
}
@@ -131,42 +129,36 @@ class SpotPriceAPI {
131129
let oteError = null;
132130

133131
try {
134-
// Nastavení příznaku pokusu o primární API
135-
await device.setCapabilityValue('primary_api_fail', true);
132+
// Výchozí stav - normálně získáváme data z primárního API
133+
await device.setCapabilityValue('primary_api_fail', false);
136134

137135
if (this.logger) {
138136
this.logger.debug('Pokus o získání dat z primárního API (spotovaelektrina.cz)');
139137
}
140138

141-
// Pokus o získání dat z primárního API
142-
const data = await this._fetchFromPrimaryAPI(timeoutMs);
139+
const rawData = await this._fetchFromPrimaryAPI(timeoutMs);
143140

144-
// Logování vrácených dat z `_fetchFromPrimaryAPI`
145-
if (this.logger) {
146-
this.logger.debug('Výsledná data vrácená z _fetchFromPrimaryAPI', { data });
147-
}
148-
149-
// Ověříme, že `data` je správně definována, a logujeme
150-
if (!data) {
141+
if (!rawData) {
151142
this.logger.error('Chyba: data jsou undefined po volání _fetchFromPrimaryAPI');
152143
throw new Error('Data z primárního API jsou undefined');
153144
}
154145

155-
// Logování dat před validací
156-
if (this.logger) {
157-
this.logger.debug('Data předaná do validatePriceData', { hoursToday: data });
158-
}
146+
// Transformace dat - pouze hour a priceCZK
147+
const data = rawData.map(hourData => ({
148+
hour: hourData.hour,
149+
priceCZK: hourData.priceCZK
150+
// Úmyslně vynecháváme level z API
151+
}));
159152

160-
// Validace dat z primárního API
161153
if (!this.priceCalculator.validatePriceData(data)) {
162154
throw new Error('Neplatný formát dat z primárního API');
163155
}
164156

165-
// Úspěch - resetujeme příznak selhání
166-
await device.setCapabilityValue('primary_api_fail', false);
167-
168157
if (this.logger) {
169-
this.logger.log('Data úspěšně získána z primárního API', { source: 'Primary API' });
158+
this.logger.log('Data úspěšně získána z primárního API', {
159+
source: 'Primary API',
160+
sampleData: data[0] // Log prvního záznamu pro kontrolu
161+
});
170162
}
171163

172164
return data;
@@ -181,22 +173,27 @@ class SpotPriceAPI {
181173
});
182174
}
183175

176+
// Primární API selhalo - přepneme na záložní API
184177
await device.setCapabilityValue('primary_api_fail', true);
185178

186179
try {
187180
if (this.logger) {
188181
this.logger.debug('Pokus o získání dat ze záložního API (ote.cr)');
189182
}
190183

191-
// Pokus o získání záložních dat
192-
const backupData = await this.getBackupDailyPrices(device);
184+
const rawBackupData = await this.getBackupDailyPrices(device);
185+
186+
// Transformace záložních dat - opět pouze hour a priceCZK
187+
const backupData = rawBackupData.map(hourData => ({
188+
hour: hourData.hour,
189+
priceCZK: hourData.priceCZK
190+
// Opět vynecháváme jakékoliv levely
191+
}));
193192

194-
// Validace záložních dat
195193
if (!this.priceCalculator.validatePriceData(backupData)) {
196194
throw new Error('Neplatný formát dat ze záložního API');
197195
}
198196

199-
// Spustíme trigger s informací o záloze
200197
await device.triggerAPIFailure({
201198
primaryAPI: this.getErrorMessage(spotElektrinaError),
202199
backupAPI: 'Záložní API úspěšné',
@@ -206,9 +203,13 @@ class SpotPriceAPI {
206203
});
207204

208205
if (this.logger) {
209-
this.logger.log('Data úspěšně získána ze záložního API', { source: 'Backup API' });
206+
this.logger.log('Data úspěšně získána ze záložního API', {
207+
source: 'Backup API',
208+
sampleData: backupData[0] // Log prvního záznamu pro kontrolu
209+
});
210210
}
211211

212+
// Ponecháme primary_api_fail na true, protože stále používáme záložní API
212213
return backupData;
213214

214215
} catch (backupError) {
@@ -221,14 +222,14 @@ class SpotPriceAPI {
221222
});
222223
}
223224

224-
// Spuštění triggeru pro selhání obou API
225225
await device.triggerAPIFailure({
226226
primaryAPI: this.getErrorMessage(spotElektrinaError),
227227
backupAPI: this.getErrorMessage(oteError),
228228
willRetry: false,
229229
maxRetriesReached: true
230230
});
231231

232+
// Ponecháme primary_api_fail na true, protože primární API stále nefunguje
232233
throw new Error(`Selhání obou API: spotovaelektrina.cz: ${this.getErrorMessage(spotElektrinaError)}, ote.cr: ${this.getErrorMessage(oteError)}`);
233234
}
234235
}

0 commit comments

Comments
 (0)