Skip to content

Commit

Permalink
Fix metering data type (#666)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Jackson <chris@cd-jackson.com>
  • Loading branch information
cdjackson committed Aug 6, 2021
1 parent 742a2d0 commit 0172809
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void attributeUpdated(ZclAttribute attribute, Object val) {
logger.debug("{}: ZigBee attribute reports {}", endpoint.getIeeeAddress(), attribute);
if (attribute.getCluster() == ZclClusterType.METERING
&& attribute.getId() == ZclMeteringCluster.ATTR_CURRENTSUMMATIONDELIVERED) {
Integer value = (Integer) val;
Integer value = ((Long) val).intValue();
BigDecimal valueCalibrated = BigDecimal.valueOf(value * multiplier / divisor);
updateChannelState(new DecimalType(valueCalibrated));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void attributeUpdated(ZclAttribute attribute, Object val) {
logger.debug("{}: ZigBee attribute reports {}", endpoint.getIeeeAddress(), attribute);
if (attribute.getCluster() == ZclClusterType.METERING
&& attribute.getId() == ZclMeteringCluster.ATTR_CURRENTSUMMATIONRECEIVED) {
Integer value = (Integer) val;
Integer value = ((Long) val).intValue();
BigDecimal valueCalibrated = BigDecimal.valueOf(value * multiplier / divisor);
updateChannelState(new DecimalType(valueCalibrated));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.zigbee.internal.converter;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.openhab.binding.zigbee.handler.ZigBeeCoordinatorHandler;
import org.openhab.binding.zigbee.handler.ZigBeeThingHandler;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.binding.builder.ChannelBuilder;
import org.openhab.core.types.State;

import com.zsmartsystems.zigbee.IeeeAddress;
import com.zsmartsystems.zigbee.ZigBeeEndpoint;
import com.zsmartsystems.zigbee.zcl.ZclAttribute;
import com.zsmartsystems.zigbee.zcl.clusters.ZclMeteringCluster;
import com.zsmartsystems.zigbee.zcl.protocol.ZclDataType;

/**
* Tests for metering converter
*
* @author Chris Jackson
*
*/
public class ZigBeeConverterMeteringSummationDeliveredTest {

@Test
public void testAttributeUpdated() {
ZigBeeEndpoint endpoint = Mockito.mock(ZigBeeEndpoint.class);
ZigBeeCoordinatorHandler coordinatorHandler = Mockito.mock(ZigBeeCoordinatorHandler.class);
ZclMeteringCluster cluster = Mockito.mock(ZclMeteringCluster.class);
ZclAttribute initAttribute = Mockito.mock(ZclAttribute.class);
Mockito.when(cluster.getAttribute(ZclMeteringCluster.ATTR_CURRENTSUMMATIONDELIVERED)).thenReturn(initAttribute);
Mockito.when(cluster.getAttribute(ZclMeteringCluster.ATTR_DIVISOR)).thenReturn(initAttribute);
Mockito.when(cluster.getAttribute(ZclMeteringCluster.ATTR_MULTIPLIER)).thenReturn(initAttribute);
Mockito.when(initAttribute.readValue(Long.MAX_VALUE)).thenReturn(1);

Mockito.when(coordinatorHandler.getEndpoint(ArgumentMatchers.any(IeeeAddress.class), ArgumentMatchers.anyInt()))
.thenReturn(endpoint);
Mockito.when(endpoint.getInputCluster(ZclMeteringCluster.CLUSTER_ID)).thenReturn(cluster);

ZigBeeConverterMeteringSummationDelivered converter = new ZigBeeConverterMeteringSummationDelivered();
ArgumentCaptor<ChannelUID> channelCapture = ArgumentCaptor.forClass(ChannelUID.class);
ArgumentCaptor<State> stateCapture = ArgumentCaptor.forClass(State.class);
ZigBeeThingHandler thingHandler = Mockito.mock(ZigBeeThingHandler.class);
Channel channel = ChannelBuilder.create(new ChannelUID("a:b:c:d"), "").build();
converter.initialize(channel, coordinatorHandler, new IeeeAddress("1234567890ABCDEF"), 1);
converter.initializeConverter(thingHandler);

ZclAttribute attribute = new ZclAttribute(new ZclMeteringCluster(endpoint),
ZclMeteringCluster.ATTR_CURRENTSUMMATIONDELIVERED, "Summation", ZclDataType.SIGNED_48_BIT_INTEGER,
false, false, false, false);

attribute.updateValue(1L);
converter.attributeUpdated(attribute, attribute.getLastValue());
Mockito.verify(thingHandler, Mockito.times(1)).setChannelState(channelCapture.capture(),
stateCapture.capture());
assertEquals(new ChannelUID("a:b:c:d"), channelCapture.getValue());
assertEquals(DecimalType.valueOf("1"), stateCapture.getValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.zigbee.internal.converter;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.openhab.binding.zigbee.handler.ZigBeeCoordinatorHandler;
import org.openhab.binding.zigbee.handler.ZigBeeThingHandler;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.binding.builder.ChannelBuilder;
import org.openhab.core.types.State;

import com.zsmartsystems.zigbee.IeeeAddress;
import com.zsmartsystems.zigbee.ZigBeeEndpoint;
import com.zsmartsystems.zigbee.zcl.ZclAttribute;
import com.zsmartsystems.zigbee.zcl.clusters.ZclMeteringCluster;
import com.zsmartsystems.zigbee.zcl.protocol.ZclDataType;

/**
* Tests for metering converter
*
* @author Chris Jackson
*
*/
public class ZigBeeConverterMeteringSummationReceivedTest {

@Test
public void testAttributeUpdated() {
ZigBeeEndpoint endpoint = Mockito.mock(ZigBeeEndpoint.class);
ZigBeeCoordinatorHandler coordinatorHandler = Mockito.mock(ZigBeeCoordinatorHandler.class);
ZclMeteringCluster cluster = Mockito.mock(ZclMeteringCluster.class);
ZclAttribute initAttribute = Mockito.mock(ZclAttribute.class);
Mockito.when(cluster.getAttribute(ZclMeteringCluster.ATTR_CURRENTSUMMATIONRECEIVED)).thenReturn(initAttribute);
Mockito.when(cluster.getAttribute(ZclMeteringCluster.ATTR_DIVISOR)).thenReturn(initAttribute);
Mockito.when(cluster.getAttribute(ZclMeteringCluster.ATTR_MULTIPLIER)).thenReturn(initAttribute);
Mockito.when(initAttribute.readValue(Long.MAX_VALUE)).thenReturn(1);

Mockito.when(coordinatorHandler.getEndpoint(ArgumentMatchers.any(IeeeAddress.class), ArgumentMatchers.anyInt()))
.thenReturn(endpoint);
Mockito.when(endpoint.getInputCluster(ZclMeteringCluster.CLUSTER_ID)).thenReturn(cluster);

ZigBeeConverterMeteringSummationReceived converter = new ZigBeeConverterMeteringSummationReceived();
ArgumentCaptor<ChannelUID> channelCapture = ArgumentCaptor.forClass(ChannelUID.class);
ArgumentCaptor<State> stateCapture = ArgumentCaptor.forClass(State.class);
ZigBeeThingHandler thingHandler = Mockito.mock(ZigBeeThingHandler.class);
Channel channel = ChannelBuilder.create(new ChannelUID("a:b:c:d"), "").build();
converter.initialize(channel, coordinatorHandler, new IeeeAddress("1234567890ABCDEF"), 1);
converter.initializeConverter(thingHandler);

ZclAttribute attribute = new ZclAttribute(new ZclMeteringCluster(endpoint),
ZclMeteringCluster.ATTR_CURRENTSUMMATIONRECEIVED, "Received", ZclDataType.SIGNED_48_BIT_INTEGER, false,
false, false, false);

attribute.updateValue(1L);
converter.attributeUpdated(attribute, attribute.getLastValue());
Mockito.verify(thingHandler, Mockito.times(1)).setChannelState(channelCapture.capture(),
stateCapture.capture());
assertEquals(new ChannelUID("a:b:c:d"), channelCapture.getValue());
assertEquals(DecimalType.valueOf("1"), stateCapture.getValue());
}
}

0 comments on commit 0172809

Please sign in to comment.