Skip to content

Commit

Permalink
Fix prettier.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerivec committed Oct 7, 2024
1 parent 087fe3e commit 538beef
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 13 deletions.
70 changes: 60 additions & 10 deletions test/availability.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,18 @@ describe('Availability', () => {
});

it('Should publish availability on startup for device where it is enabled for', async () => {
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability', stringify({state: 'online'}), {retain: true, qos: 1}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/remote/availability', stringify({state: 'online'}), {retain: true, qos: 1}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bulb_color/availability',
stringify({state: 'online'}),
{retain: true, qos: 1},
expect.any(Function),
);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/remote/availability',
stringify({state: 'online'}),
{retain: true, qos: 1},
expect.any(Function),
);
expect(MQTT.publish).not.toHaveBeenCalledWith(
'zigbee2mqtt/bulb_color_2/availability',
stringify({state: 'online'}),
Expand Down Expand Up @@ -109,7 +119,12 @@ describe('Availability', () => {
await setTimeAndAdvanceTimers(utils.minutes(7));
expect(devices.bulb_color.ping).toHaveBeenCalledTimes(1);
expect(devices.bulb_color.ping).toHaveBeenNthCalledWith(1, true);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability', stringify({state: 'offline'}), {retain: true, qos: 1}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bulb_color/availability',
stringify({state: 'offline'}),
{retain: true, qos: 1},
expect.any(Function),
);
});

it('Shouldnt do anything for a device when availability: false is set for device', async () => {
Expand All @@ -123,7 +138,12 @@ describe('Availability', () => {
MQTT.publish.mockClear();
await setTimeAndAdvanceTimers(utils.hours(26));
expect(devices.remote.ping).toHaveBeenCalledTimes(0);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/remote/availability', stringify({state: 'offline'}), {retain: true, qos: 1}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/remote/availability',
stringify({state: 'offline'}),
{retain: true, qos: 1},
expect.any(Function),
);
});

it('Should reset ping timer when device last seen changes for active device', async () => {
Expand All @@ -133,7 +153,12 @@ describe('Availability', () => {
expect(devices.bulb_color.ping).toHaveBeenCalledTimes(0);

await zigbeeHerdsman.events.lastSeenChanged({device: devices.bulb_color});
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability', stringify({state: 'offline'}), {retain: true, qos: 1}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bulb_color/availability',
stringify({state: 'offline'}),
{retain: true, qos: 1},
expect.any(Function),
);

await setTimeAndAdvanceTimers(utils.minutes(7));
expect(devices.bulb_color.ping).toHaveBeenCalledTimes(0);
Expand Down Expand Up @@ -165,7 +190,12 @@ describe('Availability', () => {
expect(devices.remote.ping).toHaveBeenCalledTimes(0);

await zigbeeHerdsman.events.lastSeenChanged({device: devices.remote});
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/remote/availability', stringify({state: 'offline'}), {retain: true, qos: 1}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/remote/availability',
stringify({state: 'offline'}),
{retain: true, qos: 1},
expect.any(Function),
);

await setTimeAndAdvanceTimers(utils.hours(25));
expect(devices.remote.ping).toHaveBeenCalledTimes(0);
Expand All @@ -178,12 +208,22 @@ describe('Availability', () => {
MQTT.publish.mockClear();

await setTimeAndAdvanceTimers(utils.minutes(15));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability', stringify({state: 'offline'}), {retain: true, qos: 1}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bulb_color/availability',
stringify({state: 'offline'}),
{retain: true, qos: 1},
expect.any(Function),
);

devices.bulb_color.lastSeen = Date.now();
await zigbeeHerdsman.events.lastSeenChanged({device: devices.bulb_color});
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability', stringify({state: 'online'}), {retain: true, qos: 1}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bulb_color/availability',
stringify({state: 'online'}),
{retain: true, qos: 1},
expect.any(Function),
);
});

it('Should allow to change availability timeout via device options', async () => {
Expand Down Expand Up @@ -299,9 +339,19 @@ describe('Availability', () => {
await flushPromises();

expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability', '', {retain: true, qos: 1}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_new_name/availability', stringify({state: 'online'}), {retain: true, qos: 1}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bulb_new_name/availability',
stringify({state: 'online'}),
{retain: true, qos: 1},
expect.any(Function),
);
await setTimeAndAdvanceTimers(utils.hours(12));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_new_name/availability', stringify({state: 'offline'}), {retain: true, qos: 1}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bulb_new_name/availability',
stringify({state: 'offline'}),
{retain: true, qos: 1},
expect.any(Function),
);
});

it('Should publish availability payload in JSON format', async () => {
Expand Down
15 changes: 12 additions & 3 deletions test/homeassistant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,10 @@ describe('HomeAssistant extension', () => {
via_device: 'zigbee2mqtt_bridge_0x00124b00120144ae',
},
availability_mode: 'all',
availability: [{topic: 'zigbee2mqtt/bridge/state', value_template: '{{ value_json.state }}'}, {topic: 'zigbee2mqtt/weather_sensor/availability', value_template: '{{ value_json.state }}'}],
availability: [
{topic: 'zigbee2mqtt/bridge/state', value_template: '{{ value_json.state }}'},
{topic: 'zigbee2mqtt/weather_sensor/availability', value_template: '{{ value_json.state }}'},
],
};

expect(MQTT.publish).toHaveBeenCalledWith(
Expand Down Expand Up @@ -2015,7 +2018,10 @@ describe('HomeAssistant extension', () => {

// Existing group with old topic structure (1.20.0) -> clear
MQTT.publish.mockClear();
await MQTT.events.message('homeassistant/light/9/light/config', stringify({availability: [{topic: 'zigbee2mqtt/bridge/state', value_template: '{{ value_json.state }}'}]}));
await MQTT.events.message(
'homeassistant/light/9/light/config',
stringify({availability: [{topic: 'zigbee2mqtt/bridge/state', value_template: '{{ value_json.state }}'}]}),
);
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledTimes(1);
expect(MQTT.publish).toHaveBeenCalledWith('homeassistant/light/9/light/config', '', {qos: 1, retain: true}, expect.any(Function));
Expand All @@ -2037,7 +2043,10 @@ describe('HomeAssistant extension', () => {

// Non-existing device -> clear
MQTT.publish.mockClear();
await MQTT.events.message('homeassistant/sensor/0x123/temperature/config', stringify({availability: [{topic: 'zigbee2mqtt/bridge/state', value_template: '{{ value_json.state }}'}]}));
await MQTT.events.message(
'homeassistant/sensor/0x123/temperature/config',
stringify({availability: [{topic: 'zigbee2mqtt/bridge/state', value_template: '{{ value_json.state }}'}]}),
);
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledTimes(1);
expect(MQTT.publish).toHaveBeenCalledWith('homeassistant/sensor/0x123/temperature/config', '', {qos: 1, retain: true}, expect.any(Function));
Expand Down

0 comments on commit 538beef

Please sign in to comment.