Skip to content

Commit

Permalink
Fix #1677: When ecowatt request fail, it should stop scene (#1681)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles committed Jan 2, 2023
1 parent ecccc20 commit 3d2ffc8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 28 deletions.
48 changes: 26 additions & 22 deletions server/lib/scene/scene.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const utc = require('dayjs/plugin/utc');
const timezone = require('dayjs/plugin/timezone');
const { ACTIONS, DEVICE_FEATURE_CATEGORIES, DEVICE_FEATURE_TYPES } = require('../../utils/constants');
const { getDeviceFeature } = require('../../utils/device');
const { AbortScene, NotFoundError } = require('../../utils/coreErrors');
const { AbortScene } = require('../../utils/coreErrors');
const { compare } = require('../../utils/compare');
const { parseJsonIfJson } = require('../../utils/json');
const logger = require('../../utils/logger');
Expand Down Expand Up @@ -334,27 +334,31 @@ const actionsFunc = {
}
},
[ACTIONS.ECOWATT.CONDITION]: async (self, action) => {
const data = await self.gateway.getEcowattSignals();
const todayDate = dayjs.tz(dayjs(), self.timezone).format('YYYY-MM-DD');
const todayHour = dayjs.tz(dayjs(), self.timezone).hour();
const todayLiveData = data.signals.find((day) => {
const signalDate = dayjs(day.jour).format('YYYY-MM-DD');
return todayDate === signalDate;
});
if (!todayLiveData) {
throw new NotFoundError('Ecowatt: day not found');
}
const currentHourNetworkStatus = todayLiveData.values.find((hour) => hour.pas === todayHour);
if (!currentHourNetworkStatus) {
throw new NotFoundError('Ecowatt: hour not found');
}
const ECOWATT_STATUSES = {
1: 'ok',
2: 'warning',
3: 'critical',
};
if (ECOWATT_STATUSES[currentHourNetworkStatus.hvalue] !== action.ecowatt_network_status) {
throw new AbortScene('ECOWATT_DIFFERENT_STATUS');
try {
const data = await self.gateway.getEcowattSignals();
const todayDate = dayjs.tz(dayjs(), self.timezone).format('YYYY-MM-DD');
const todayHour = dayjs.tz(dayjs(), self.timezone).hour();
const todayLiveData = data.signals.find((day) => {
const signalDate = dayjs(day.jour).format('YYYY-MM-DD');
return todayDate === signalDate;
});
if (!todayLiveData) {
throw new AbortScene('Ecowatt: day not found');
}
const currentHourNetworkStatus = todayLiveData.values.find((hour) => hour.pas === todayHour);
if (!currentHourNetworkStatus) {
throw new AbortScene('Ecowatt: hour not found');
}
const ECOWATT_STATUSES = {
1: 'ok',
2: 'warning',
3: 'critical',
};
if (ECOWATT_STATUSES[currentHourNetworkStatus.hvalue] !== action.ecowatt_network_status) {
throw new AbortScene('ECOWATT_DIFFERENT_STATUS');
}
} catch (e) {
throw new AbortScene(e.message);
}
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('scene.ecowattCondition', () => {
let clock;
const timezone = 'Europe/Paris';
beforeEach(async () => {
clock = useFakeTimers(new Date('2022-12-09T05:23:57.931Z'));
clock = useFakeTimers(new Date('2022-12-09T11:00:57'));
});
afterEach(() => {
clock.restore();
Expand Down Expand Up @@ -55,12 +55,12 @@ describe('scene.ecowattCondition', () => {
);
return assert.isRejected(promise, AbortScene);
});
it('should reject, day not found, but continue scene', async () => {
it('should reject, day not found', async () => {
const gateway = {
getEcowattSignals: fake.resolves({ signals: [] }),
};
const scope = {};
await executeActions(
const promise = executeActions(
{ event, gateway, timezone },
[
[
Expand All @@ -72,14 +72,15 @@ describe('scene.ecowattCondition', () => {
],
scope,
);
await assert.isRejected(promise, AbortScene, 'Ecowatt: day not found');
});
it('should reject, hour not found, but continue scene', async () => {
it('should reject, hour not found', async () => {
const gateway = {
getEcowattSignals: fake.resolves({
signals: [
{
GenerationFichier: '2022-12-08T23:00:00+01:00',
jour: '2022-12-09T00:00:00+01:00',
jour: '2022-12-09T12:00:00',
dvalue: 1,
message: 'Pas d’alerte.',
values: [],
Expand All @@ -88,7 +89,26 @@ describe('scene.ecowattCondition', () => {
}),
};
const scope = {};
await executeActions(
const promise = executeActions(
{ event, gateway, timezone },
[
[
{
type: ACTIONS.ECOWATT.CONDITION,
ecowatt_network_status: 'warning',
},
],
],
scope,
);
await assert.isRejected(promise, AbortScene, 'Ecowatt: hour not found');
});
it('should not continue scene, ecowatt signals return error', async () => {
const gateway = {
getEcowattSignals: fake.rejects(new Error('500 - error')),
};
const scope = {};
const promise = executeActions(
{ event, gateway, timezone },
[
[
Expand All @@ -100,5 +120,6 @@ describe('scene.ecowattCondition', () => {
],
scope,
);
await assert.isRejected(promise, AbortScene, '500 - error');
});
});

0 comments on commit 3d2ffc8

Please sign in to comment.