2019-09-09 10:48:09 -07:00
const data = require ( './stub/data' ) ;
const logger = require ( './stub/logger' ) ;
const zigbeeHerdsman = require ( './stub/zigbeeHerdsman' ) ;
2020-05-24 06:57:29 -07:00
const zigbeeHerdsmanConverters = require ( 'zigbee-herdsman-converters' ) ;
2019-09-09 10:48:09 -07:00
zigbeeHerdsman . returnDevices . push ( '0x00124b00120144ae' ) ;
zigbeeHerdsman . returnDevices . push ( '0x000b57fffec6a5b3' ) ;
zigbeeHerdsman . returnDevices . push ( '0x000b57fffec6a5b2' ) ;
zigbeeHerdsman . returnDevices . push ( '0x0017880104e45542' ) ;
const MQTT = require ( './stub/mqtt' ) ;
const Controller = require ( '../lib/controller' ) ;
const flushPromises = ( ) => new Promise ( setImmediate ) ;
2019-05-29 11:11:40 -07:00
const settings = require ( '../lib/util/settings' ) ;
2019-04-29 11:38:40 -07:00
describe ( 'Groups' , ( ) => {
2019-09-09 10:48:09 -07:00
let controller ;
2019-04-29 11:38:40 -07:00
2019-09-09 10:48:09 -07:00
beforeEach ( ( ) => {
2020-03-02 11:11:43 -07:00
data . writeEmptyState ( ) ;
2020-07-13 12:45:44 -07:00
settings . set ( [ 'experimental' , 'new_api' ] , true ) ;
2019-09-09 10:48:09 -07:00
controller = new Controller ( ) ;
Object . values ( zigbeeHerdsman . groups ) . forEach ( ( g ) => g . members = [ ] ) ;
data . writeDefaultConfiguration ( ) ;
settings . _reRead ( ) ;
2019-09-17 10:01:57 -07:00
MQTT . publish . mockClear ( ) ;
2020-05-24 06:57:29 -07:00
zigbeeHerdsmanConverters . toZigbeeConverters . _ _clearStore _ _ ( ) ;
2019-09-09 10:48:09 -07:00
} )
2019-04-29 11:38:40 -07:00
it ( 'Apply group updates add' , async ( ) => {
2019-09-25 03:08:39 -07:00
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ 'bulb' , 'bulb_color' ] } } ) ;
2019-09-09 10:48:09 -07:00
zigbeeHerdsman . groups . group _1 . members . push ( zigbeeHerdsman . devices . bulb . getEndpoint ( 1 ) )
await controller . start ( ) ;
await flushPromises ( ) ;
expect ( zigbeeHerdsman . groups . group _1 . members ) . toStrictEqual ( [
zigbeeHerdsman . devices . bulb . getEndpoint ( 1 ) ,
zigbeeHerdsman . devices . bulb _color . getEndpoint ( 1 )
] ) ;
2019-04-29 11:38:40 -07:00
} ) ;
it ( 'Apply group updates remove' , async ( ) => {
2019-09-09 10:48:09 -07:00
const endpoint = zigbeeHerdsman . devices . bulb _color . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint ) ;
2019-09-25 03:08:39 -07:00
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , } } ) ;
2019-09-09 10:48:09 -07:00
await controller . start ( ) ;
await flushPromises ( ) ;
expect ( zigbeeHerdsman . groups . group _1 . members ) . toStrictEqual ( [ ] ) ;
} ) ;
2020-07-26 01:11:58 -07:00
it ( 'Apply group updates remove handle fail' , async ( ) => {
const endpoint = zigbeeHerdsman . devices . bulb _color . getEndpoint ( 1 ) ;
endpoint . removeFromGroup . mockImplementationOnce ( ( ) => { throw new Error ( "failed!" ) } ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint ) ;
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , } } ) ;
logger . error . mockClear ( ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
expect ( logger . error ) . toHaveBeenCalledWith ( ` Failed to remove 'bulb_color' from 'group_1' ` ) ;
expect ( zigbeeHerdsman . groups . group _1 . members ) . toStrictEqual ( [ endpoint ] ) ;
} ) ;
2019-09-09 10:48:09 -07:00
it ( 'Move to non existing group' , async ( ) => {
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint ) ;
2019-09-25 03:08:39 -07:00
settings . set ( [ 'groups' ] , { '3' : { friendly _name : 'group_3' , retain : false , devices : [ device . ieeeAddr ] } } ) ;
2019-09-09 10:48:09 -07:00
await controller . start ( ) ;
await flushPromises ( ) ;
expect ( zigbeeHerdsman . groups . group _1 . members ) . toStrictEqual ( [ ] ) ;
} ) ;
it ( 'Add non standard endpoint to group with name' , async ( ) => {
const QBKG03LM = zigbeeHerdsman . devices . QBKG03LM ;
2019-09-25 03:08:39 -07:00
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ '0x0017880104e45542/right' ] } } ) ;
2019-09-09 10:48:09 -07:00
await controller . start ( ) ;
await flushPromises ( ) ;
expect ( zigbeeHerdsman . groups . group _1 . members ) . toStrictEqual ( [ QBKG03LM . getEndpoint ( 3 ) ] ) ;
} ) ;
it ( 'Add non standard endpoint to group with number' , async ( ) => {
const QBKG03LM = zigbeeHerdsman . devices . QBKG03LM ;
2019-09-25 03:08:39 -07:00
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ 'wall_switch_double/2' ] } } ) ;
2019-09-09 10:48:09 -07:00
await controller . start ( ) ;
await flushPromises ( ) ;
expect ( zigbeeHerdsman . groups . group _1 . members ) . toStrictEqual ( [ QBKG03LM . getEndpoint ( 2 ) ] ) ;
} ) ;
it ( 'Shouldnt crash on non-existing devices' , async ( ) => {
logger . error . mockClear ( ) ;
2019-09-25 03:08:39 -07:00
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ 'not_existing_bla' ] } } ) ;
2019-09-09 10:48:09 -07:00
await controller . start ( ) ;
await flushPromises ( ) ;
expect ( zigbeeHerdsman . groups . group _1 . members ) . toStrictEqual ( [ ] ) ;
expect ( logger . error ) . toHaveBeenCalledWith ( "Cannot find 'not_existing_bla' of group 'group_1'" ) ;
2019-05-29 11:11:40 -07:00
} ) ;
2020-07-12 14:50:15 -07:00
it ( 'Legacy api: Add to group via MQTT' , async ( ) => {
2019-09-09 10:48:09 -07:00
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
2019-09-25 03:08:39 -07:00
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ ] } } ) ;
2019-09-09 10:48:09 -07:00
expect ( group . members . length ) . toBe ( 0 ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
MQTT . events . message ( 'zigbee2mqtt/bridge/group/group_1/add' , 'bulb_color' ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ endpoint ] ) ;
expect ( settings . getGroup ( 'group_1' ) . devices ) . toStrictEqual ( [ ` ${ device . ieeeAddr } /1 ` ] ) ;
2019-09-15 00:13:02 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/bridge/log" , '{"type":"device_group_add","message":{"friendly_name":"bulb_color","group":"group_1"}}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
2019-05-29 11:11:40 -07:00
} ) ;
2020-07-12 14:50:15 -07:00
it ( 'Legacy api: Add to group with slashes via MQTT' , async ( ) => {
2019-10-25 09:40:37 -07:00
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups [ "group/with/slashes" ] ;
settings . set ( [ 'groups' ] , { '99' : { friendly _name : 'group/with/slashes' , retain : false , devices : [ ] } } ) ;
expect ( group . members . length ) . toBe ( 0 ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
MQTT . events . message ( 'zigbee2mqtt/bridge/group/group/with/slashes/add' , 'bulb_color' ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ endpoint ] ) ;
expect ( settings . getGroup ( 'group/with/slashes' ) . devices ) . toStrictEqual ( [ ` ${ device . ieeeAddr } /1 ` ] ) ;
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/bridge/log" , '{"type":"device_group_add","message":{"friendly_name":"bulb_color","group":"group/with/slashes"}}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
2020-03-11 13:24:01 -07:00
// Test if subscribed to topics with slashes
expect ( MQTT . subscribe ) . toHaveBeenCalledWith ( 'zigbee2mqtt/bridge/group/+/remove' ) ;
expect ( MQTT . subscribe ) . toHaveBeenCalledWith ( 'zigbee2mqtt/bridge/group/+/+/remove' ) ;
expect ( MQTT . subscribe ) . toHaveBeenCalledWith ( 'zigbee2mqtt/bridge/group/+/+/+/+/+/remove' ) ;
expect ( MQTT . subscribe ) . toHaveBeenCalledWith ( 'zigbee2mqtt/bridge/group/+/+/+/+/+/add' ) ;
2019-10-25 09:40:37 -07:00
} ) ;
2020-07-12 14:50:15 -07:00
it ( 'Legacy api: Add to group via MQTT with postfix' , async ( ) => {
2019-09-09 10:48:09 -07:00
const device = zigbeeHerdsman . devices . QBKG03LM ;
const endpoint = device . getEndpoint ( 3 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
expect ( group . members . length ) . toBe ( 0 ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
await MQTT . events . message ( 'zigbee2mqtt/bridge/group/group_1/add' , 'wall_switch_double/right' ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ endpoint ] ) ;
expect ( settings . getGroup ( 'group_1' ) . devices ) . toStrictEqual ( [ ` ${ device . ieeeAddr } / ${ endpoint . ID } ` ] ) ;
} ) ;
2020-07-12 14:50:15 -07:00
it ( 'Legacy api: Add to group via MQTT with postfix shouldnt add it twice' , async ( ) => {
2019-09-09 10:48:09 -07:00
const device = zigbeeHerdsman . devices . QBKG03LM ;
const endpoint = device . getEndpoint ( 3 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
expect ( group . members . length ) . toBe ( 0 ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
await MQTT . events . message ( 'zigbee2mqtt/bridge/group/group_1/add' , 'wall_switch_double/right' ) ;
await flushPromises ( ) ;
await MQTT . events . message ( 'zigbee2mqtt/bridge/group/group_1/add' , '0x0017880104e45542/3' ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ endpoint ] ) ;
expect ( settings . getGroup ( 'group_1' ) . devices ) . toStrictEqual ( [ ` ${ device . ieeeAddr } / ${ endpoint . ID } ` ] ) ;
} ) ;
2020-07-12 14:50:15 -07:00
it ( 'Legacy api: Remove from group via MQTT' , async ( ) => {
2019-09-09 10:48:09 -07:00
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint ) ;
2019-09-25 03:08:39 -07:00
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ device . ieeeAddr ] } } ) ;
2019-09-09 10:48:09 -07:00
await controller . start ( ) ;
await flushPromises ( ) ;
await MQTT . events . message ( 'zigbee2mqtt/bridge/group/group_1/remove' , 'bulb_color' ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ ] ) ;
expect ( settings . getGroup ( 'group_1' ) . devices ) . toStrictEqual ( [ ] ) ;
2019-09-15 00:13:02 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/bridge/log" , '{"type":"device_group_remove","message":{"friendly_name":"bulb_color","group":"group_1"}}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
2019-09-09 10:48:09 -07:00
} ) ;
2020-07-12 14:50:15 -07:00
it ( 'Legacy api: Remove from group via MQTT when in zigbee but not in settings' , async ( ) => {
2019-09-09 10:48:09 -07:00
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint ) ;
2019-09-25 03:08:39 -07:00
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ 'dummy' ] } } ) ;
2019-09-09 10:48:09 -07:00
await controller . start ( ) ;
await flushPromises ( ) ;
await MQTT . events . message ( 'zigbee2mqtt/bridge/group/group_1/remove' , 'bulb_color' ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ ] ) ;
expect ( settings . getGroup ( 'group_1' ) . devices ) . toStrictEqual ( [ 'dummy' ] ) ;
} ) ;
2020-07-12 14:50:15 -07:00
it ( 'Legacy api: Remove from group via MQTT with postfix variant 1' , async ( ) => {
2019-09-09 10:48:09 -07:00
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint ) ;
2019-09-25 03:08:39 -07:00
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ ` wall_switch_double/right ` ] } } ) ;
2019-09-09 10:48:09 -07:00
await controller . start ( ) ;
await flushPromises ( ) ;
await MQTT . events . message ( 'zigbee2mqtt/bridge/group/group_1/remove' , '0x0017880104e45542/3' ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ ] ) ;
expect ( settings . getGroup ( 'group_1' ) . devices ) . toStrictEqual ( [ ] ) ;
} ) ;
2020-07-12 14:50:15 -07:00
it ( 'Legacy api: Remove from group via MQTT with postfix variant 2' , async ( ) => {
2019-09-09 10:48:09 -07:00
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint ) ;
2019-09-25 03:08:39 -07:00
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ ` 0x0017880104e45542/right ` ] } } ) ;
2019-09-09 10:48:09 -07:00
await controller . start ( ) ;
await flushPromises ( ) ;
await MQTT . events . message ( 'zigbee2mqtt/bridge/group/group_1/remove' , 'wall_switch_double/3' ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ ] ) ;
expect ( settings . getGroup ( 'group_1' ) . devices ) . toStrictEqual ( [ ] ) ;
} ) ;
2020-07-12 14:50:15 -07:00
it ( 'Legacy api: Remove from group via MQTT with postfix variant 3' , async ( ) => {
2019-09-09 10:48:09 -07:00
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint ) ;
2019-09-25 03:08:39 -07:00
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ ` wall_switch_double/3 ` ] } } ) ;
2019-09-09 10:48:09 -07:00
await controller . start ( ) ;
await flushPromises ( ) ;
await MQTT . events . message ( 'zigbee2mqtt/bridge/group/group_1/remove' , '0x0017880104e45542/right' ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ ] ) ;
expect ( settings . getGroup ( 'group_1' ) . devices ) . toStrictEqual ( [ ] ) ;
} ) ;
2020-07-12 14:50:15 -07:00
it ( 'Legacy api: Remove from group all' , async ( ) => {
2019-09-09 10:48:09 -07:00
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint ) ;
2019-09-25 03:08:39 -07:00
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ ` wall_switch_double/3 ` ] } } ) ;
2019-09-09 10:48:09 -07:00
await controller . start ( ) ;
await flushPromises ( ) ;
await MQTT . events . message ( 'zigbee2mqtt/bridge/group/remove_all' , '0x0017880104e45542/right' ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ ] ) ;
expect ( settings . getGroup ( 'group_1' ) . devices ) . toStrictEqual ( [ ] ) ;
2019-09-15 00:13:02 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/bridge/log" , '{"type":"device_group_remove_all","message":{"friendly_name":"wall_switch_double"}}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
2019-09-09 10:48:09 -07:00
} ) ;
it ( 'Remove from group all deprecated' , async ( ) => {
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint ) ;
2019-09-25 03:08:39 -07:00
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ ` wall_switch_double/3 ` ] } } ) ;
2019-09-09 10:48:09 -07:00
await controller . start ( ) ;
await flushPromises ( ) ;
await MQTT . events . message ( 'zigbee2mqtt/bridge/group/group_1/remove_all' , '0x0017880104e45542/right' ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ ] ) ;
expect ( settings . getGroup ( 'group_1' ) . devices ) . toStrictEqual ( [ ] ) ;
} ) ;
2020-07-12 14:50:15 -07:00
it ( 'Legacy api: Log when adding to non-existing group' , async ( ) => {
2019-09-09 10:48:09 -07:00
await controller . start ( ) ;
await flushPromises ( ) ;
logger . error . mockClear ( ) ;
MQTT . events . message ( 'zigbee2mqtt/bridge/group/group_1_not_existing/add' , 'bulb_color' ) ;
await flushPromises ( ) ;
expect ( logger . error ) . toHaveBeenCalledWith ( "Group 'group_1_not_existing' does not exist" ) ;
} ) ;
2020-07-12 14:50:15 -07:00
it ( 'Legacy api: Log when adding to non-existing device' , async ( ) => {
2019-09-09 10:48:09 -07:00
await controller . start ( ) ;
await flushPromises ( ) ;
logger . error . mockClear ( ) ;
MQTT . events . message ( 'zigbee2mqtt/bridge/group/group_1/add' , 'bulb_color_not_existing' ) ;
await flushPromises ( ) ;
expect ( logger . error ) . toHaveBeenCalledWith ( "Device 'bulb_color_not_existing' does not exist" ) ;
2019-06-17 12:17:29 -07:00
} ) ;
2019-09-17 09:33:27 -07:00
it ( 'Should publish group state change when a device in it changes state' , async ( ) => {
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint ) ;
2019-09-25 03:08:39 -07:00
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ device . ieeeAddr ] } } ) ;
2019-09-17 09:33:27 -07:00
await controller . start ( ) ;
await flushPromises ( ) ;
2019-09-29 05:35:05 -07:00
MQTT . publish . mockClear ( ) ;
2019-09-17 09:33:27 -07:00
const payload = { data : { onOff : 1 } , cluster : 'genOnOff' , device , endpoint , type : 'attributeReport' , linkquality : 10 } ;
await zigbeeHerdsman . events . message ( payload ) ;
await flushPromises ( ) ;
2019-09-29 05:35:05 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledTimes ( 2 ) ;
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/bulb_color" , '{"state":"ON","linkquality":10}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
2019-09-17 09:33:27 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/group_1" , '{"state":"ON"}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
} ) ;
2019-09-17 10:01:57 -07:00
2019-09-29 05:35:05 -07:00
it ( 'Should publish state change of all members when a group changes its state' , async ( ) => {
2019-09-17 10:01:57 -07:00
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint ) ;
2019-09-29 05:35:05 -07:00
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ device . ieeeAddr ] } } ) ;
2019-09-17 10:01:57 -07:00
await controller . start ( ) ;
await flushPromises ( ) ;
2019-09-25 03:51:17 -07:00
MQTT . publish . mockClear ( ) ;
2019-09-29 05:35:05 -07:00
await MQTT . events . message ( 'zigbee2mqtt/group_1/set' , JSON . stringify ( { state : 'ON' } ) ) ;
2019-09-17 10:01:57 -07:00
await flushPromises ( ) ;
2019-09-29 05:35:05 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledTimes ( 2 ) ;
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/bulb_color" , '{"state":"ON"}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/group_1" , '{"state":"ON"}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
2019-09-25 03:51:17 -07:00
} ) ;
2020-03-02 12:08:51 -07:00
it ( 'Should publish state change of all members when a group changes its state, filtered' , async ( ) => {
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint ) ;
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , filtered _attributes : [ 'brightness' ] , devices : [ device . ieeeAddr ] } } ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
MQTT . publish . mockClear ( ) ;
await MQTT . events . message ( 'zigbee2mqtt/group_1/set' , JSON . stringify ( { state : 'ON' , brightness : 100 } ) ) ;
await flushPromises ( ) ;
expect ( MQTT . publish ) . toHaveBeenCalledTimes ( 2 ) ;
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/bulb_color" , '{"state":"ON","brightness":100}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/group_1" , '{"state":"ON"}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
} ) ;
2019-09-29 05:35:05 -07:00
it ( 'Shouldnt publish group state change when a group is not optimistic' , async ( ) => {
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
2019-09-25 03:51:17 -07:00
const group = zigbeeHerdsman . groups . group _1 ;
2019-09-29 05:35:05 -07:00
group . members . push ( endpoint ) ;
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , devices : [ device . ieeeAddr ] , optimistic : false , retain : false } } ) ;
2019-09-25 03:51:17 -07:00
await controller . start ( ) ;
await flushPromises ( ) ;
MQTT . publish . mockClear ( ) ;
2019-09-29 05:35:05 -07:00
const payload = { data : { onOff : 1 } , cluster : 'genOnOff' , device , endpoint , type : 'attributeReport' , linkquality : 10 } ;
2019-09-25 03:51:17 -07:00
await zigbeeHerdsman . events . message ( payload ) ;
await flushPromises ( ) ;
2019-09-29 05:35:05 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledTimes ( 1 ) ;
2019-09-25 03:51:17 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/bulb_color" , '{"state":"ON","linkquality":10}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
2019-09-17 10:01:57 -07:00
} ) ;
2019-09-30 12:18:07 -07:00
it ( 'Should publish state change of another group with shared device when a group changes its state' , async ( ) => {
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint ) ;
settings . set ( [ 'groups' ] , {
'1' : { friendly _name : 'group_1' , retain : false , devices : [ device . ieeeAddr ] } ,
'2' : { friendly _name : 'group_2' , retain : false , devices : [ device . ieeeAddr ] } ,
'3' : { friendly _name : 'group_3' , retain : false , devices : [ ] }
} ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
MQTT . publish . mockClear ( ) ;
await MQTT . events . message ( 'zigbee2mqtt/group_1/set' , JSON . stringify ( { state : 'ON' } ) ) ;
await flushPromises ( ) ;
expect ( MQTT . publish ) . toHaveBeenCalledTimes ( 3 ) ;
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/bulb_color" , '{"state":"ON"}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/group_1" , '{"state":"ON"}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/group_2" , '{"state":"ON"}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
} ) ;
2019-10-03 08:41:36 -07:00
2019-10-14 09:04:04 -07:00
it ( 'Should not publish state change off if any lights within are still on when changed via device' , async ( ) => {
2019-10-03 08:41:36 -07:00
const device _1 = zigbeeHerdsman . devices . bulb _color ;
const device _2 = zigbeeHerdsman . devices . bulb ;
const endpoint _1 = device _1 . getEndpoint ( 1 ) ;
const endpoint _2 = device _2 . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint _1 ) ;
group . members . push ( endpoint _2 ) ;
settings . set ( [ 'groups' ] , {
'1' : { friendly _name : 'group_1' , devices : [ device _1 . ieeeAddr , device _2 . ieeeAddr ] , retain : false }
} ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
await MQTT . events . message ( 'zigbee2mqtt/group_1/set' , JSON . stringify ( { state : 'ON' } ) ) ;
await flushPromises ( ) ;
MQTT . publish . mockClear ( ) ;
await MQTT . events . message ( 'zigbee2mqtt/bulb_color/set' , JSON . stringify ( { state : 'OFF' } ) ) ;
await flushPromises ( ) ;
expect ( MQTT . publish ) . toHaveBeenCalledTimes ( 1 ) ;
2020-05-24 06:57:29 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/bulb_color" , '{"state":"OFF","brightness":0}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
2019-10-14 09:04:04 -07:00
} ) ;
it ( 'Should not publish state change off if any lights within are still on when changed via shared group' , async ( ) => {
const device _1 = zigbeeHerdsman . devices . bulb _color ;
const device _2 = zigbeeHerdsman . devices . bulb ;
const endpoint _1 = device _1 . getEndpoint ( 1 ) ;
const endpoint _2 = device _2 . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint _1 ) ;
group . members . push ( endpoint _2 ) ;
settings . set ( [ 'groups' ] , {
'1' : { friendly _name : 'group_1' , devices : [ device _1 . ieeeAddr , device _2 . ieeeAddr ] , retain : false } ,
'2' : { friendly _name : 'group_2' , retain : false , devices : [ device _1 . ieeeAddr ] } ,
} ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
await MQTT . events . message ( 'zigbee2mqtt/group_1/set' , JSON . stringify ( { state : 'ON' } ) ) ;
await flushPromises ( ) ;
MQTT . publish . mockClear ( ) ;
await MQTT . events . message ( 'zigbee2mqtt/group_2/set' , JSON . stringify ( { state : 'OFF' } ) ) ;
await flushPromises ( ) ;
expect ( MQTT . publish ) . toHaveBeenCalledTimes ( 2 ) ;
2020-05-24 06:57:29 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/group_2" , '{"state":"OFF","brightness":0}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/bulb_color" , '{"state":"OFF","brightness":0}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
2019-10-03 08:41:36 -07:00
} ) ;
it ( 'Should publish state change off if all lights within turn off' , async ( ) => {
const device _1 = zigbeeHerdsman . devices . bulb _color ;
const device _2 = zigbeeHerdsman . devices . bulb ;
const endpoint _1 = device _1 . getEndpoint ( 1 ) ;
const endpoint _2 = device _2 . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint _1 ) ;
group . members . push ( endpoint _2 ) ;
settings . set ( [ 'groups' ] , {
'1' : { friendly _name : 'group_1' , devices : [ device _1 . ieeeAddr , device _2 . ieeeAddr ] , retain : false }
} ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
await MQTT . events . message ( 'zigbee2mqtt/group_1/set' , JSON . stringify ( { state : 'ON' } ) ) ;
await flushPromises ( ) ;
MQTT . publish . mockClear ( ) ;
await MQTT . events . message ( 'zigbee2mqtt/bulb_color/set' , JSON . stringify ( { state : 'OFF' } ) ) ;
await MQTT . events . message ( 'zigbee2mqtt/bulb/set' , JSON . stringify ( { state : 'OFF' } ) ) ;
await flushPromises ( ) ;
expect ( MQTT . publish ) . toHaveBeenCalledTimes ( 3 ) ;
2020-05-24 06:57:29 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/bulb_color" , '{"state":"OFF","brightness":0}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/bulb" , '{"state":"OFF","brightness":0}' , { "retain" : true , qos : 0 } , expect . any ( Function ) ) ;
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/group_1" , '{"state":"OFF","brightness":0}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
2019-10-03 08:41:36 -07:00
} ) ;
it ( 'Should publish state change off even when missing current state' , async ( ) => {
const device _1 = zigbeeHerdsman . devices . bulb _color ;
const device _2 = zigbeeHerdsman . devices . bulb ;
const endpoint _1 = device _1 . getEndpoint ( 1 ) ;
const endpoint _2 = device _2 . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint _1 ) ;
group . members . push ( endpoint _2 ) ;
settings . set ( [ 'groups' ] , {
'1' : { friendly _name : 'group_1' , devices : [ device _1 . ieeeAddr , device _2 . ieeeAddr ] , retain : false }
} ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
await MQTT . events . message ( 'zigbee2mqtt/group_1/set' , JSON . stringify ( { state : 'ON' } ) ) ;
await flushPromises ( ) ;
MQTT . publish . mockClear ( ) ;
controller . state . state = { } ;
await MQTT . events . message ( 'zigbee2mqtt/bulb_color/set' , JSON . stringify ( { state : 'OFF' } ) ) ;
await flushPromises ( ) ;
expect ( MQTT . publish ) . toHaveBeenCalledTimes ( 2 ) ;
2020-05-24 06:57:29 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/bulb_color" , '{"state":"OFF","brightness":0}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
expect ( MQTT . publish ) . toHaveBeenCalledWith ( "zigbee2mqtt/group_1" , '{"state":"OFF","brightness":0}' , { "retain" : false , qos : 0 } , expect . any ( Function ) ) ;
2019-10-03 08:41:36 -07:00
} ) ;
2020-07-12 14:50:15 -07:00
it ( 'Add to group via MQTT' , async ( ) => {
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ ] } } ) ;
expect ( group . members . length ) . toBe ( 0 ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
2020-07-13 12:45:44 -07:00
MQTT . publish . mockClear ( ) ;
2020-07-12 14:50:15 -07:00
MQTT . events . message ( 'zigbee2mqtt/bridge/request/group/members/add' , JSON . stringify ( { group : 'group_1' , device : 'bulb_color' } ) ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ endpoint ] ) ;
expect ( settings . getGroup ( 'group_1' ) . devices ) . toStrictEqual ( [ ` ${ device . ieeeAddr } /1 ` ] ) ;
2020-07-13 12:45:44 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith ( 'zigbee2mqtt/bridge/groups' , expect . any ( String ) , expect . any ( Object ) , expect . any ( Function ) ) ;
2020-07-12 14:50:15 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith (
'zigbee2mqtt/bridge/response/group/members/add' ,
JSON . stringify ( { "data" : { "device" : "bulb_color" , "group" : "group_1" } , "status" : "ok" } ) ,
{ retain : false , qos : 0 } , expect . any ( Function )
) ;
} ) ;
it ( 'Add to group via MQTT fails' , async ( ) => {
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ ] } } ) ;
expect ( group . members . length ) . toBe ( 0 ) ;
await controller . start ( ) ;
endpoint . addToGroup . mockImplementationOnce ( ( ) => { throw new Error ( 'timeout' ) } ) ;
await flushPromises ( ) ;
2020-07-13 12:45:44 -07:00
MQTT . publish . mockClear ( ) ;
2020-07-12 14:50:15 -07:00
MQTT . events . message ( 'zigbee2mqtt/bridge/request/group/members/add' , JSON . stringify ( { group : 'group_1' , device : 'bulb_color' } ) ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ ] ) ;
expect ( settings . getGroup ( 'group_1' ) . devices ) . toStrictEqual ( [ ] ) ;
2020-07-13 12:45:44 -07:00
expect ( MQTT . publish ) . not . toHaveBeenCalledWith ( 'zigbee2mqtt/bridge/groups' , expect . any ( String ) , expect . any ( Object ) , expect . any ( Function ) ) ;
2020-07-12 14:50:15 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith (
'zigbee2mqtt/bridge/response/group/members/add' ,
JSON . stringify ( { "data" : { "device" : "bulb_color" , "group" : "group_1" } , "status" : "error" , "error" : "Failed to add from group (timeout)" } ) ,
{ retain : false , qos : 0 } , expect . any ( Function )
) ;
} ) ;
it ( 'Add to group with slashes via MQTT' , async ( ) => {
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups [ "group/with/slashes" ] ;
settings . set ( [ 'groups' ] , { '99' : { friendly _name : 'group/with/slashes' , retain : false , devices : [ ] } } ) ;
expect ( group . members . length ) . toBe ( 0 ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
2020-07-13 12:45:44 -07:00
MQTT . publish . mockClear ( ) ;
2020-07-12 14:50:15 -07:00
MQTT . events . message ( 'zigbee2mqtt/bridge/request/group/members/add' , JSON . stringify ( { group : 'group/with/slashes' , device : 'bulb_color' } ) ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ endpoint ] ) ;
expect ( settings . getGroup ( 'group/with/slashes' ) . devices ) . toStrictEqual ( [ ` ${ device . ieeeAddr } /1 ` ] ) ;
2020-07-13 12:45:44 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith ( 'zigbee2mqtt/bridge/groups' , expect . any ( String ) , expect . any ( Object ) , expect . any ( Function ) ) ;
2020-07-12 14:50:15 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith (
'zigbee2mqtt/bridge/response/group/members/add' ,
JSON . stringify ( { "data" : { "device" : "bulb_color" , "group" : "group/with/slashes" } , "status" : "ok" } ) ,
{ retain : false , qos : 0 } , expect . any ( Function )
) ;
} ) ;
it ( 'Add to group via MQTT with postfix' , async ( ) => {
const device = zigbeeHerdsman . devices . QBKG03LM ;
const endpoint = device . getEndpoint ( 3 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
expect ( group . members . length ) . toBe ( 0 ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
2020-07-13 12:45:44 -07:00
MQTT . publish . mockClear ( ) ;
2020-07-12 14:50:15 -07:00
MQTT . events . message ( 'zigbee2mqtt/bridge/request/group/members/add' , JSON . stringify ( { group : 'group_1' , device : 'wall_switch_double/right' } ) ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ endpoint ] ) ;
expect ( settings . getGroup ( 'group_1' ) . devices ) . toStrictEqual ( [ ` ${ device . ieeeAddr } / ${ endpoint . ID } ` ] ) ;
2020-07-13 12:45:44 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith ( 'zigbee2mqtt/bridge/groups' , expect . any ( String ) , expect . any ( Object ) , expect . any ( Function ) ) ;
2020-07-12 14:50:15 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith (
'zigbee2mqtt/bridge/response/group/members/add' ,
JSON . stringify ( { "data" : { "device" : "wall_switch_double/right" , "group" : "group_1" } , "status" : "ok" } ) ,
{ retain : false , qos : 0 } , expect . any ( Function )
) ;
} ) ;
it ( 'Add to group via MQTT with postfix shouldnt add it twice' , async ( ) => {
const device = zigbeeHerdsman . devices . QBKG03LM ;
const endpoint = device . getEndpoint ( 3 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
expect ( group . members . length ) . toBe ( 0 ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
2020-07-13 12:45:44 -07:00
MQTT . publish . mockClear ( ) ;
2020-07-12 14:50:15 -07:00
MQTT . events . message ( 'zigbee2mqtt/bridge/request/group/members/add' , JSON . stringify ( { group : 'group_1' , device : 'wall_switch_double/right' } ) ) ;
await flushPromises ( ) ;
MQTT . events . message ( 'zigbee2mqtt/bridge/request/group/members/add' , JSON . stringify ( { group : 'group_1' , device : '0x0017880104e45542/3' } ) ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ endpoint ] ) ;
expect ( settings . getGroup ( 'group_1' ) . devices ) . toStrictEqual ( [ ` ${ device . ieeeAddr } / ${ endpoint . ID } ` ] ) ;
2020-07-13 12:45:44 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith ( 'zigbee2mqtt/bridge/groups' , expect . any ( String ) , expect . any ( Object ) , expect . any ( Function ) ) ;
2020-07-12 14:50:15 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith (
'zigbee2mqtt/bridge/response/group/members/add' ,
JSON . stringify ( { "data" : { "device" : "wall_switch_double/right" , "group" : "group_1" } , "status" : "ok" } ) ,
{ retain : false , qos : 0 } , expect . any ( Function )
) ;
} ) ;
it ( 'Remove from group via MQTT' , async ( ) => {
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint ) ;
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ device . ieeeAddr ] } } ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
2020-07-13 12:45:44 -07:00
MQTT . publish . mockClear ( ) ;
2020-07-12 14:50:15 -07:00
MQTT . events . message ( 'zigbee2mqtt/bridge/request/group/members/remove' , JSON . stringify ( { group : 'group_1' , device : 'bulb_color' } ) ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ ] ) ;
expect ( settings . getGroup ( 'group_1' ) . devices ) . toStrictEqual ( [ ] ) ;
2020-07-13 12:45:44 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith ( 'zigbee2mqtt/bridge/groups' , expect . any ( String ) , expect . any ( Object ) , expect . any ( Function ) ) ;
2020-07-12 14:50:15 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith (
'zigbee2mqtt/bridge/response/group/members/remove' ,
JSON . stringify ( { "data" : { "device" : "bulb_color" , "group" : "group_1" } , "status" : "ok" } ) ,
{ retain : false , qos : 0 } , expect . any ( Function )
) ;
} ) ;
it ( 'Remove from group via MQTT when in zigbee but not in settings' , async ( ) => {
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint ) ;
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ 'dummy' ] } } ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
2020-07-13 12:45:44 -07:00
MQTT . publish . mockClear ( ) ;
2020-07-12 14:50:15 -07:00
MQTT . events . message ( 'zigbee2mqtt/bridge/request/group/members/remove' , JSON . stringify ( { group : 'group_1' , device : 'bulb_color' } ) ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ ] ) ;
expect ( settings . getGroup ( 'group_1' ) . devices ) . toStrictEqual ( [ 'dummy' ] ) ;
2020-07-13 12:45:44 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith ( 'zigbee2mqtt/bridge/groups' , expect . any ( String ) , expect . any ( Object ) , expect . any ( Function ) ) ;
2020-07-12 14:50:15 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith (
'zigbee2mqtt/bridge/response/group/members/remove' ,
JSON . stringify ( { "data" : { "device" : "bulb_color" , "group" : "group_1" } , "status" : "ok" } ) ,
{ retain : false , qos : 0 } , expect . any ( Function )
) ;
} ) ;
it ( 'Remove from group via MQTT with postfix variant 1' , async ( ) => {
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint ) ;
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ ` wall_switch_double/right ` ] } } ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
2020-07-13 12:45:44 -07:00
MQTT . publish . mockClear ( ) ;
2020-07-12 14:50:15 -07:00
MQTT . events . message ( 'zigbee2mqtt/bridge/request/group/members/remove' , JSON . stringify ( { group : 'group_1' , device : '0x0017880104e45542/3' } ) ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ ] ) ;
expect ( settings . getGroup ( 'group_1' ) . devices ) . toStrictEqual ( [ ] ) ;
2020-07-13 12:45:44 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith ( 'zigbee2mqtt/bridge/groups' , expect . any ( String ) , expect . any ( Object ) , expect . any ( Function ) ) ;
2020-07-12 14:50:15 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith (
'zigbee2mqtt/bridge/response/group/members/remove' ,
JSON . stringify ( { "data" : { "device" : "0x0017880104e45542/3" , "group" : "group_1" } , "status" : "ok" } ) ,
{ retain : false , qos : 0 } , expect . any ( Function )
) ;
} ) ;
it ( 'Remove from group via MQTT with postfix variant 2' , async ( ) => {
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint ) ;
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ ` 0x0017880104e45542/right ` ] } } ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
2020-07-13 12:45:44 -07:00
MQTT . publish . mockClear ( ) ;
2020-07-12 14:50:15 -07:00
MQTT . events . message ( 'zigbee2mqtt/bridge/request/group/members/remove' , JSON . stringify ( { group : 'group_1' , device : 'wall_switch_double/3' } ) ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ ] ) ;
expect ( settings . getGroup ( 'group_1' ) . devices ) . toStrictEqual ( [ ] ) ;
2020-07-13 12:45:44 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith ( 'zigbee2mqtt/bridge/groups' , expect . any ( String ) , expect . any ( Object ) , expect . any ( Function ) ) ;
2020-07-12 14:50:15 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith (
'zigbee2mqtt/bridge/response/group/members/remove' ,
JSON . stringify ( { "data" : { "device" : "wall_switch_double/3" , "group" : "group_1" } , "status" : "ok" } ) ,
{ retain : false , qos : 0 } , expect . any ( Function )
) ;
} ) ;
it ( 'Remove from group via MQTT with postfix variant 3' , async ( ) => {
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint ) ;
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ ` wall_switch_double/3 ` ] } } ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
2020-07-13 12:45:44 -07:00
MQTT . publish . mockClear ( ) ;
2020-07-12 14:50:15 -07:00
MQTT . events . message ( 'zigbee2mqtt/bridge/request/group/members/remove' , JSON . stringify ( { group : 'group_1' , device : '0x0017880104e45542/right' } ) ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ ] ) ;
expect ( settings . getGroup ( 'group_1' ) . devices ) . toStrictEqual ( [ ] ) ;
2020-07-13 12:45:44 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith ( 'zigbee2mqtt/bridge/groups' , expect . any ( String ) , expect . any ( Object ) , expect . any ( Function ) ) ;
2020-07-12 14:50:15 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith (
'zigbee2mqtt/bridge/response/group/members/remove' ,
JSON . stringify ( { "data" : { "device" : "0x0017880104e45542/right" , "group" : "group_1" } , "status" : "ok" } ) ,
{ retain : false , qos : 0 } , expect . any ( Function )
) ;
} ) ;
it ( 'Remove from group all' , async ( ) => {
const device = zigbeeHerdsman . devices . bulb _color ;
const endpoint = device . getEndpoint ( 1 ) ;
const group = zigbeeHerdsman . groups . group _1 ;
group . members . push ( endpoint ) ;
settings . set ( [ 'groups' ] , { '1' : { friendly _name : 'group_1' , retain : false , devices : [ ` wall_switch_double/3 ` ] } } ) ;
await controller . start ( ) ;
await flushPromises ( ) ;
2020-07-13 12:45:44 -07:00
MQTT . publish . mockClear ( ) ;
2020-07-12 14:50:15 -07:00
MQTT . events . message ( 'zigbee2mqtt/bridge/request/group/members/remove_all' , JSON . stringify ( { device : '0x0017880104e45542/right' } ) ) ;
await flushPromises ( ) ;
expect ( group . members ) . toStrictEqual ( [ ] ) ;
expect ( settings . getGroup ( 'group_1' ) . devices ) . toStrictEqual ( [ ] ) ;
2020-07-13 12:45:44 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith ( 'zigbee2mqtt/bridge/groups' , expect . any ( String ) , expect . any ( Object ) , expect . any ( Function ) ) ;
2020-07-12 14:50:15 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith (
'zigbee2mqtt/bridge/response/group/members/remove_all' ,
JSON . stringify ( { "data" : { "device" : "0x0017880104e45542/right" } , "status" : "ok" } ) ,
{ retain : false , qos : 0 } , expect . any ( Function )
) ;
} ) ;
it ( 'Error when adding to non-existing group' , async ( ) => {
await controller . start ( ) ;
await flushPromises ( ) ;
logger . error . mockClear ( ) ;
2020-07-13 12:45:44 -07:00
MQTT . publish . mockClear ( ) ;
2020-07-12 14:50:15 -07:00
MQTT . events . message ( 'zigbee2mqtt/bridge/request/group/members/remove' , JSON . stringify ( { group : 'group_1_not_existing' , device : 'bulb_color' } ) ) ;
await flushPromises ( ) ;
2020-07-13 12:45:44 -07:00
expect ( MQTT . publish ) . not . toHaveBeenCalledWith ( 'zigbee2mqtt/bridge/groups' , expect . any ( String ) , expect . any ( Object ) , expect . any ( Function ) ) ;
2020-07-12 14:50:15 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith (
'zigbee2mqtt/bridge/response/group/members/remove' ,
JSON . stringify ( { "data" : { "device" : "bulb_color" , "group" : "group_1_not_existing" } , "status" : "error" , "error" : "Group 'group_1_not_existing' does not exist" } ) ,
{ retain : false , qos : 0 } , expect . any ( Function )
) ;
} ) ;
it ( 'Error when adding to non-existing device' , async ( ) => {
await controller . start ( ) ;
await flushPromises ( ) ;
logger . error . mockClear ( ) ;
2020-07-13 12:45:44 -07:00
MQTT . publish . mockClear ( ) ;
2020-07-12 14:50:15 -07:00
MQTT . events . message ( 'zigbee2mqtt/bridge/request/group/members/add' , JSON . stringify ( { group : 'group_1' , device : 'bulb_color_not_existing' } ) ) ;
await flushPromises ( ) ;
2020-07-13 12:45:44 -07:00
expect ( MQTT . publish ) . not . toHaveBeenCalledWith ( 'zigbee2mqtt/bridge/groups' , expect . any ( String ) , expect . any ( Object ) , expect . any ( Function ) ) ;
2020-07-12 14:50:15 -07:00
expect ( MQTT . publish ) . toHaveBeenCalledWith (
'zigbee2mqtt/bridge/response/group/members/add' ,
JSON . stringify ( { "data" : { "device" : "bulb_color_not_existing" , "group" : "group_1" } , "status" : "error" , "error" : "Device 'bulb_color_not_existing' does not exist" } ) ,
{ retain : false , qos : 0 } , expect . any ( Function )
) ;
} ) ;
2020-03-02 11:11:43 -07:00
} ) ;