def test_dimmer_turn_on(mock_openzwave): """Test turning on a dimmable Z-Wave light.""" node = MockNode() value = MockValue(data=0, node=node) values = MockLightValues(primary=value) device = zwave.get_device(node=node, values=values, node_config={}) device.turn_on() assert node.set_dimmer.called value_id, brightness = node.set_dimmer.mock_calls[0][1] assert value_id == value.value_id assert brightness == 255 node.reset_mock() device.turn_on(**{ATTR_BRIGHTNESS: 120}) assert node.set_dimmer.called value_id, brightness = node.set_dimmer.mock_calls[0][1] assert value_id == value.value_id assert brightness == 46 # int(120 / 255 * 99) with patch.object(zwave, '_LOGGER', MagicMock()) as mock_logger: device.turn_on(**{ATTR_TRANSITION: 35}) assert mock_logger.debug.called assert node.set_dimmer.called msg, entity_id = mock_logger.debug.mock_calls[0][1] assert entity_id == device.entity_id
def test_remove_association(self): """Test zwave change_association service.""" ZWaveGroup = self.mock_openzwave.group.ZWaveGroup group = MagicMock() ZWaveGroup.return_value = group value = MockValue( index=12, command_class=const.COMMAND_CLASS_WAKE_UP, ) node = MockNode(node_id=14) node.values = {12: value} node.get_values.return_value = node.values self.zwave_network.nodes = {14: node} self.hass.services.call('zwave', 'change_association', { const.ATTR_ASSOCIATION: 'remove', const.ATTR_NODE_ID: 14, const.ATTR_TARGET_NODE_ID: 24, const.ATTR_GROUP: 3, const.ATTR_INSTANCE: 5, }) self.hass.block_till_done() assert ZWaveGroup.called assert len(ZWaveGroup.mock_calls) == 2 assert ZWaveGroup.mock_calls[0][1][0] == 3 assert ZWaveGroup.mock_calls[0][1][2] == 14 assert group.remove_association.called assert len(group.remove_association.mock_calls) == 1 assert group.remove_association.mock_calls[0][1][0] == 24 assert group.remove_association.mock_calls[0][1][1] == 5
def test_set_wakeup(self): """Test zwave set_wakeup service.""" value = MockValue( index=12, command_class=const.COMMAND_CLASS_WAKE_UP, ) node = MockNode(node_id=14) node.values = {12: value} node.get_values.return_value = node.values self.zwave_network.nodes = {14: node} self.hass.services.call('zwave', 'set_wakeup', { const.ATTR_NODE_ID: 14, const.ATTR_CONFIG_VALUE: 15, }) self.hass.block_till_done() assert value.data == 15 node.can_wake_up_value = False self.hass.services.call('zwave', 'set_wakeup', { const.ATTR_NODE_ID: 14, const.ATTR_CONFIG_VALUE: 20, }) self.hass.block_till_done() assert value.data == 15
def test_print_config_parameter(self): """Test zwave print_config_parameter service.""" value1 = MockValue( index=12, command_class=const.COMMAND_CLASS_CONFIGURATION, data=1234, ) value2 = MockValue( index=13, command_class=const.COMMAND_CLASS_CONFIGURATION, data=2345, ) node = MockNode(node_id=14) node.values = {12: value1, 13: value2} self.zwave_network.nodes = {14: node} with patch.object(zwave, '_LOGGER') as mock_logger: self.hass.services.call('zwave', 'print_config_parameter', { const.ATTR_NODE_ID: 14, const.ATTR_CONFIG_PARAMETER: 13, }) self.hass.block_till_done() assert mock_logger.info.called assert len(mock_logger.info.mock_calls) == 1 assert mock_logger.info.mock_calls[0][1][1] == 13 assert mock_logger.info.mock_calls[0][1][2] == 14 assert mock_logger.info.mock_calls[0][1][3] == 2345
async def test_get_protection_values_nonexisting_node(hass, client): """Test getting protection values on node with wrong nodeid.""" network = hass.data[DATA_NETWORK] = MagicMock() node = MockNode(node_id=18, command_classes=[const.COMMAND_CLASS_PROTECTION]) value = MockValue( value_id=123456, index=0, instance=1, command_class=const.COMMAND_CLASS_PROTECTION) value.label = 'Protection Test' value.data_items = ['Unprotected', 'Protection by Sequence', 'No Operation Possible'] value.data = 'Unprotected' network.nodes = {17: node} node.value = value resp = await client.get('/api/zwave/protection/18') assert resp.status == 404 result = await resp.json() assert not node.get_protections.called assert not node.get_protection_item.called assert not node.get_protection_items.called assert result == {'message': 'Node not found'}
async def test_set_protection_value_nonexisting_node(hass, client): """Test setting protection value on nonexisting node.""" network = hass.data[DATA_NETWORK] = MagicMock() node = MockNode(node_id=17, command_classes=[const.COMMAND_CLASS_PROTECTION]) value = MockValue( value_id=123456, index=0, instance=1, command_class=const.COMMAND_CLASS_PROTECTION) value.label = 'Protection Test' value.data_items = ['Unprotected', 'Protection by Sequence', 'No Operation Possible'] value.data = 'Unprotected' network.nodes = {17: node} node.value = value node.set_protection.return_value = False resp = await client.post( '/api/zwave/protection/18', data=json.dumps({ 'value_id': '123456', 'selection': 'Protecton by Seuence'})) assert resp.status == 404 result = await resp.json() assert not node.set_protection.called assert result == {'message': 'Node not found'}
async def test_get_protection_values(hass, client): """Test getting protection values on node.""" network = hass.data[DATA_NETWORK] = MagicMock() node = MockNode(node_id=18, command_classes=[const.COMMAND_CLASS_PROTECTION]) value = MockValue( value_id=123456, index=0, instance=1, command_class=const.COMMAND_CLASS_PROTECTION) value.label = 'Protection Test' value.data_items = ['Unprotected', 'Protection by Sequence', 'No Operation Possible'] value.data = 'Unprotected' network.nodes = {18: node} node.value = value node.get_protection_item.return_value = "Unprotected" node.get_protection_items.return_value = value.data_items node.get_protections.return_value = {value.value_id: 'Object'} resp = await client.get('/api/zwave/protection/18') assert resp.status == 200 result = await resp.json() assert node.get_protections.called assert node.get_protection_item.called assert node.get_protection_items.called assert result == { 'value_id': '123456', 'selected': 'Unprotected', 'options': ['Unprotected', 'Protection by Sequence', 'No Operation Possible'] }
def test_get_config(hass, test_client): """Test getting config on node.""" app = mock_http_component_app(hass) ZWaveNodeConfigView().register(app.router) network = hass.data[DATA_NETWORK] = MagicMock() node = MockNode(node_id=2) value = MockValue( index=12, command_class=const.COMMAND_CLASS_CONFIGURATION) value.label = 'label' value.help = 'help' value.type = 'type' value.data = 'data' value.data_items = ['item1', 'item2'] value.max = 'max' value.min = 'min' node.values = {12: value} network.nodes = {2: node} node.get_values.return_value = node.values client = yield from test_client(app) resp = yield from client.get('/api/zwave/config/2') assert resp.status == 200 result = yield from resp.json() assert result == {'12': {'data': 'data', 'data_items': ['item1', 'item2'], 'help': 'help', 'label': 'label', 'max': 'max', 'min': 'min', 'type': 'type'}}
def test_get_groups(hass, test_client): """Test getting groupdata on node.""" app = mock_http_component_app(hass) ZWaveNodeGroupView().register(app.router) network = hass.data[DATA_NETWORK] = MagicMock() node = MockNode(node_id=2) node.groups.associations = 'assoc' node.groups.associations_instances = 'inst' node.groups.label = 'the label' node.groups.max_associations = 'max' node.groups = {1: node.groups} network.nodes = {2: node} client = yield from test_client(app) resp = yield from client.get('/api/zwave/groups/2') assert resp.status == 200 result = yield from resp.json() assert result == { '1': { 'association_instances': 'inst', 'associations': 'assoc', 'label': 'the label', 'max_associations': 'max' } }
def test_get_usercodes_no_genreuser(hass, test_client): """Test getting usercodes on node missing genre user.""" app = mock_http_component_app(hass) ZWaveUserCodeView().register(app.router) network = hass.data[DATA_NETWORK] = MagicMock() node = MockNode(node_id=18, command_classes=[const.COMMAND_CLASS_USER_CODE]) value = MockValue( index=0, command_class=const.COMMAND_CLASS_USER_CODE) value.genre = const.GENRE_SYSTEM value.label = 'label' value.data = '1234' node.values = {0: value} network.nodes = {18: node} node.get_values.return_value = node.values client = yield from test_client(app) resp = yield from client.get('/api/zwave/usercodes/18') assert resp.status == 200 result = yield from resp.json() assert result == {}
def test_fan_turn_on(mock_openzwave): """Test turning on a zwave fan.""" node = MockNode() value = MockValue(data=0, node=node) values = MockEntityValues(primary=value) device = zwave.get_device(node=node, values=values, node_config={}) device.turn_on() assert node.set_dimmer.called value_id, brightness = node.set_dimmer.mock_calls[0][1] assert value_id == value.value_id assert brightness == 255 node.reset_mock() device.turn_on(speed=SPEED_OFF) assert node.set_dimmer.called value_id, brightness = node.set_dimmer.mock_calls[0][1] assert value_id == value.value_id assert brightness == 0 node.reset_mock() device.turn_on(speed=SPEED_LOW) assert node.set_dimmer.called value_id, brightness = node.set_dimmer.mock_calls[0][1] assert value_id == value.value_id assert brightness == 1 node.reset_mock() device.turn_on(speed=SPEED_MEDIUM) assert node.set_dimmer.called value_id, brightness = node.set_dimmer.mock_calls[0][1] assert value_id == value.value_id assert brightness == 50 node.reset_mock() device.turn_on(speed=SPEED_HIGH) assert node.set_dimmer.called value_id, brightness = node.set_dimmer.mock_calls[0][1] assert value_id == value.value_id assert brightness == 99
def test_rename_value(self): """Test zwave rename_value service.""" node = MockNode(node_id=14) value = MockValue(index=12, value_id=123456, label="Old Label") node.values = {123456: value} self.zwave_network.nodes = {11: node} assert value.label == "Old Label" self.hass.services.call('zwave', 'rename_value', { const.ATTR_NODE_ID: 11, const.ATTR_VALUE_ID: 123456, const.ATTR_NAME: "New Label", }) self.hass.block_till_done() assert value.label == "New Label"
def test_set_poll_intensity_disable(self): """Test zwave set_poll_intensity service, successful disable.""" node = MockNode(node_id=14) value = MockValue(index=12, value_id=123456, poll_intensity=4) node.values = {123456: value} self.zwave_network.nodes = {11: node} assert value.poll_intensity == 4 self.hass.services.call('zwave', 'set_poll_intensity', { const.ATTR_NODE_ID: 11, const.ATTR_VALUE_ID: 123456, const.ATTR_POLL_INTENSITY: 0, }) self.hass.block_till_done() disable_poll = value.disable_poll assert value.disable_poll.called assert len(disable_poll.mock_calls) == 2
async def test_get_protection_values_without_protectionclass(hass, client): """Test getting protection values on node without protectionclass.""" network = hass.data[DATA_NETWORK] = MagicMock() node = MockNode(node_id=18) value = MockValue( value_id=123456, index=0, instance=1) network.nodes = {18: node} node.value = value resp = await client.get('/api/zwave/protection/18') assert resp.status == 200 result = await resp.json() assert not node.get_protections.called assert not node.get_protection_item.called assert not node.get_protection_items.called assert result == {}
def test_set_poll_intensity_enable_failed(self): """Test zwave set_poll_intensity service, failed set.""" node = MockNode(node_id=14) value = MockValue(index=12, value_id=123456, poll_intensity=0) value.enable_poll.return_value = False node.values = {123456: value} self.zwave_network.nodes = {11: node} assert value.poll_intensity == 0 self.hass.services.call('zwave', 'set_poll_intensity', { const.ATTR_NODE_ID: 11, const.ATTR_VALUE_ID: 123456, const.ATTR_POLL_INTENSITY: 4, }) self.hass.block_till_done() enable_poll = value.enable_poll assert value.enable_poll.called assert len(enable_poll.mock_calls) == 1
def test_track_message_workaround(mock_openzwave): """Test value changed for Z-Wave lock by alarm-clearing workaround.""" node = MockNode(manufacturer_id='003B', product_id='5044', stats={'lastReceivedMessage': [0] * 6}) values = MockEntityValues( primary=MockValue(data=True, node=node), access_control=None, alarm_type=None, alarm_level=None, ) # Here we simulate an RF lock. The first zwave.get_device will call # update properties, simulating the first DoorLock report. We then trigger # a change, simulating the openzwave automatic refreshing behavior (which # is enabled for at least the lock that needs this workaround) node.stats['lastReceivedMessage'][5] = const.COMMAND_CLASS_DOOR_LOCK device = zwave.get_device(node=node, values=values) value_changed(values.primary) assert device.is_locked assert device.device_state_attributes[zwave.ATTR_NOTIFICATION] == 'RF Lock' # Simulate a keypad unlock. We trigger a value_changed() which simulates # the Alarm notification received from the lock. Then, we trigger # value_changed() to simulate the automatic refreshing behavior. values.access_control = MockValue(data=6, node=node) values.alarm_type = MockValue(data=19, node=node) values.alarm_level = MockValue(data=3, node=node) node.stats['lastReceivedMessage'][5] = const.COMMAND_CLASS_ALARM value_changed(values.access_control) node.stats['lastReceivedMessage'][5] = const.COMMAND_CLASS_DOOR_LOCK values.primary.data = False value_changed(values.primary) assert not device.is_locked assert device.device_state_attributes[zwave.ATTR_LOCK_STATUS] == \ 'Unlocked with Keypad by user 3' # Again, simulate an RF lock. device.lock() node.stats['lastReceivedMessage'][5] = const.COMMAND_CLASS_DOOR_LOCK value_changed(values.primary) assert device.is_locked assert device.device_state_attributes[zwave.ATTR_NOTIFICATION] == 'RF Lock'
async def test_set_protection_value_missing_class(hass, client): """Test setting protection value on node without protectionclass.""" network = hass.data[DATA_NETWORK] = MagicMock() node = MockNode(node_id=17) value = MockValue( value_id=123456, index=0, instance=1) network.nodes = {17: node} node.value = value node.set_protection.return_value = False resp = await client.post( '/api/zwave/protection/17', data=json.dumps({ 'value_id': '123456', 'selection': 'Protecton by Seuence'})) assert resp.status == 404 result = await resp.json() assert not node.set_protection.called assert result == {'message': 'No protection commandclass on this node'}
def test_switch_turn_on_and_off(mock_openzwave): """Test turning on a Z-Wave switch.""" node = MockNode() value = MockValue(data=0, node=node) values = MockEntityValues(primary=value) device = zwave.get_device(node=node, values=values, node_config={}) device.turn_on() assert node.set_switch.called value_id, state = node.set_switch.mock_calls[0][1] assert value_id == value.value_id assert state is True node.reset_mock() device.turn_off() assert node.set_switch.called value_id, state = node.set_switch.mock_calls[0][1] assert value_id == value.value_id assert state is False
def test_reset_node_meters(self): """Test zwave reset_node_meters service.""" value = MockValue( instance=1, index=8, data=99.5, command_class=const.COMMAND_CLASS_METER, ) reset_value = MockValue( instance=1, index=33, command_class=const.COMMAND_CLASS_METER, ) node = MockNode(node_id=14) node.values = {8: value, 33: reset_value} node.get_values.return_value = node.values self.zwave_network.nodes = {14: node} self.hass.services.call('zwave', 'reset_node_meters', { const.ATTR_NODE_ID: 14, const.ATTR_INSTANCE: 2, }) self.hass.block_till_done() assert not self.zwave_network.manager.pressButton.called assert not self.zwave_network.manager.releaseButton.called self.hass.services.call('zwave', 'reset_node_meters', { const.ATTR_NODE_ID: 14, }) self.hass.block_till_done() assert self.zwave_network.manager.pressButton.called value_id, = self.zwave_network.manager.pressButton.mock_calls.pop(0)[1] assert value_id == reset_value.value_id assert self.zwave_network.manager.releaseButton.called value_id, = ( self.zwave_network.manager.releaseButton.mock_calls.pop(0)[1]) assert value_id == reset_value.value_id
def test_dimmer_turn_on(mock_openzwave): """Test turning on a dimmable Z-Wave light.""" node = MockNode() value = MockValue(data=0, node=node) device = zwave.get_device(node, value, {}) device.turn_on() assert node.set_dimmer.called value_id, brightness = node.set_dimmer.mock_calls[0][1] assert value_id == value.value_id assert brightness == 255 node.reset_mock() device.turn_on(**{ATTR_BRIGHTNESS: 120}) assert node.set_dimmer.called value_id, brightness = node.set_dimmer.mock_calls[0][1] assert value_id == value.value_id assert brightness == 46 # int(120 / 255 * 99)
def test_get_usercodes(hass, client): """Test getting usercodes on node.""" network = hass.data[DATA_NETWORK] = MagicMock() node = MockNode(node_id=18, command_classes=[const.COMMAND_CLASS_USER_CODE]) value = MockValue( index=0, command_class=const.COMMAND_CLASS_USER_CODE) value.genre = const.GENRE_USER value.label = 'label' value.data = '1234' node.values = {0: value} network.nodes = {18: node} node.get_values.return_value = node.values resp = yield from client.get('/api/zwave/usercodes/18') assert resp.status == 200 result = yield from resp.json() assert result == {'0': {'code': '1234', 'label': 'label', 'length': 4}}
def test_multilevelsensor_value_changed_temp_celsius(mock_openzwave): """Test value changed for Z-Wave multilevel sensor for temperature.""" node = MockNode(command_classes=[ const.COMMAND_CLASS_SENSOR_MULTILEVEL, const.COMMAND_CLASS_METER, ]) value = MockValue(data=38.85555, units="C", node=node) values = MockEntityValues(primary=value) device = sensor.get_device(node=node, values=values, node_config={}) assert device.state == 38.9 assert device.unit_of_measurement == homeassistant.const.TEMP_CELSIUS value.data = 37.95555 value_changed(value) assert device.state == 38.0
def test_get_usercodes(hass, test_client): """Test getting usercodes on node.""" app = mock_http_component_app(hass) ZWaveUserCodeView().register(app.router) network = hass.data[ZWAVE_NETWORK] = MagicMock() node = MockNode(node_id=18, command_classes=[const.COMMAND_CLASS_USER_CODE]) value = MockValue(index=0, command_class=const.COMMAND_CLASS_USER_CODE) value.genre = const.GENRE_USER value.label = 'label' value.data = '1234' node.values = {0: value} network.nodes = {18: node} node.get_values.return_value = node.values client = yield from test_client(app) resp = yield from client.get('/api/zwave/usercodes/18') assert resp.status == 200 result = yield from resp.json() assert result == {'0': {'code': '1234', 'label': 'label', 'length': 4}}
def test_garage_value_changed(mock_openzwave): """Test position changed.""" node = MockNode() value = MockValue(data=False, node=node, command_class=const.COMMAND_CLASS_BARRIER_OPERATOR) values = MockEntityValues(primary=value, node=node) device = zwave.get_device(node=node, values=values, node_config={}) assert device.is_closed value.data = True value_changed(value) assert not device.is_closed
def test_set_hs_color(mock_openzwave): """Test setting zwave light color.""" node = MockNode(command_classes=[const.COMMAND_CLASS_SWITCH_COLOR]) value = MockValue(data=0, node=node) color = MockValue(data="#0000000000", node=node) # Supports RGB only color_channels = MockValue(data=0x1C, node=node) values = MockLightValues(primary=value, color=color, color_channels=color_channels) device = light.get_device(node=node, values=values, node_config={}) assert color.data == "#0000000000" device.turn_on(**{ATTR_HS_COLOR: (30, 50)}) assert color.data == "#ffbf7f0000"
def test_set_white_value(mock_openzwave): """Test setting zwave light color.""" node = MockNode(command_classes=[const.COMMAND_CLASS_SWITCH_COLOR]) value = MockValue(data=0, node=node) color = MockValue(data="#0000000000", node=node) # Supports RGBW color_channels = MockValue(data=0x1D, node=node) values = MockLightValues(primary=value, color=color, color_channels=color_channels) device = light.get_device(node=node, values=values, node_config={}) assert color.data == "#0000000000" device.turn_on(**{ATTR_WHITE_VALUE: 200}) assert color.data == "#ffffffc800"
def test_get_device_detects_battery_sensor(mock_openzwave): """Test get_device returns a Z-Wave battery sensor.""" node = MockNode(command_classes=[const.COMMAND_CLASS_BATTERY]) value = MockValue( data=0, node=node, type=const.TYPE_DECIMAL, command_class=const.COMMAND_CLASS_BATTERY, ) values = MockEntityValues(primary=value) device = sensor.get_device(node=node, values=values, node_config={}) assert isinstance(device, sensor.ZWaveBatterySensor) assert device.device_class == homeassistant.const.DEVICE_CLASS_BATTERY
def test_binary_sensor_value_changed(mock_openzwave): """Test value changed for binary sensor.""" node = MockNode() value = MockValue(data=False, node=node, command_class=const.COMMAND_CLASS_SENSOR_BINARY) values = MockEntityValues(primary=value) device = binary_sensor.get_device(node=node, values=values, node_config={}) assert not device.is_on value.data = True value_changed(value) assert device.is_on
def test_get_groups(hass, client): """Test getting groupdata on node.""" network = hass.data[DATA_NETWORK] = MagicMock() node = MockNode(node_id=2) node.groups.associations = 'assoc' node.groups.associations_instances = 'inst' node.groups.label = 'the label' node.groups.max_associations = 'max' node.groups = {1: node.groups} network.nodes = {2: node} resp = yield from client.get('/api/zwave/groups/2') assert resp.status == 200 result = yield from resp.json() assert result == { '1': { 'association_instances': 'inst', 'associations': 'assoc', 'label': 'the label', 'max_associations': 'max' } }
def test_multilevelsensor_value_changed_integer(mock_openzwave): """Test value changed for Z-Wave multilevel sensor for other units.""" node = MockNode(command_classes=[ const.COMMAND_CLASS_SENSOR_MULTILEVEL, const.COMMAND_CLASS_METER, ]) value = MockValue(data=5, units="counts", node=node) values = MockEntityValues(primary=value) device = sensor.get_device(node=node, values=values, node_config={}) assert device.state == 5 assert device.unit_of_measurement == "counts" value.data = 6 value_changed(value) assert device.state == 6
def test_multilevelsensor_value_changed_temp_fahrenheit(mock_openzwave): """Test value changed for Z-Wave multilevel sensor for temperature.""" node = MockNode(command_classes=[ const.COMMAND_CLASS_SENSOR_MULTILEVEL, const.COMMAND_CLASS_METER, ]) value = MockValue(data=190.95555, units="F", node=node) values = MockEntityValues(primary=value) device = sensor.get_device(node=node, values=values, node_config={}) assert device.state == 191.0 assert device.unit_of_measurement == openpeerpower.const.TEMP_FAHRENHEIT value.data = 197.95555 value_changed(value) assert device.state == 198.0
def test_get_device_detects_rgbw_light(mock_openzwave): """Test get_device returns a color light.""" node = MockNode(command_classes=[const.COMMAND_CLASS_SWITCH_COLOR]) value = MockValue(data=0, node=node) color = MockValue(data='#0000000000', node=node) color_channels = MockValue(data=0x1d, node=node) values = MockLightValues(primary=value, color=color, color_channels=color_channels) device = zwave.get_device(node=node, values=values, node_config={}) device.value_added() assert isinstance(device, zwave.ZwaveColorLight) assert device.supported_features == (SUPPORT_BRIGHTNESS | SUPPORT_COLOR | SUPPORT_WHITE_VALUE)
def test_get_device_detects_garagedoor_switch(hass, mock_openzwave): """Test device returns garage door.""" node = MockNode() value = MockValue(data=False, node=node, command_class=const.COMMAND_CLASS_SWITCH_BINARY) values = MockEntityValues(primary=value, node=node) device = cover.get_device(hass=hass, node=node, values=values, node_config={}) assert isinstance(device, cover.ZwaveGarageDoorSwitch) assert device.device_class == "garage" assert device.supported_features == SUPPORT_OPEN | SUPPORT_CLOSE
async def test_get_groups(opp, client): """Test getting groupdata on node.""" network = opp.data[DATA_NETWORK] = MagicMock() node = MockNode(node_id=2) node.groups.associations = "assoc" node.groups.associations_instances = "inst" node.groups.label = "the label" node.groups.max_associations = "max" node.groups = {1: node.groups} network.nodes = {2: node} resp = await client.get("/api/zwave/groups/2") assert resp.status == 200 result = await resp.json() assert result == { "1": { "association_instances": "inst", "associations": "assoc", "label": "the label", "max_associations": "max", } }
async def test_get_protection_values(hass, client): """Test getting protection values on node.""" network = hass.data[DATA_NETWORK] = MagicMock() node = MockNode(node_id=18, command_classes=[const.COMMAND_CLASS_PROTECTION]) value = MockValue(value_id=123456, index=0, instance=1, command_class=const.COMMAND_CLASS_PROTECTION) value.label = 'Protection Test' value.data_items = [ 'Unprotected', 'Protection by Sequence', 'No Operation Possible' ] value.data = 'Unprotected' network.nodes = {18: node} node.value = value node.get_protection_item.return_value = "Unprotected" node.get_protection_items.return_value = value.data_items node.get_protections.return_value = {value.value_id: 'Object'} resp = await client.get('/api/zwave/protection/18') assert resp.status == 200 result = await resp.json() assert node.get_protections.called assert node.get_protection_item.called assert node.get_protection_items.called assert result == { 'value_id': '123456', 'selected': 'Unprotected', 'options': ['Unprotected', 'Protection by Sequence', 'No Operation Possible'] }
async def test_get_protection_values(hass, client): """Test getting protection values on node.""" network = hass.data[DATA_NETWORK] = MagicMock() node = MockNode(node_id=18, command_classes=[const.COMMAND_CLASS_PROTECTION]) value = MockValue( value_id=123456, index=0, instance=1, command_class=const.COMMAND_CLASS_PROTECTION, ) value.label = "Protection Test" value.data_items = [ "Unprotected", "Protection by Sequence", "No Operation Possible", ] value.data = "Unprotected" network.nodes = {18: node} node.value = value node.get_protection_item.return_value = "Unprotected" node.get_protection_items.return_value = value.data_items node.get_protections.return_value = {value.value_id: "Object"} resp = await client.get("/api/zwave/protection/18") assert resp.status == 200 result = await resp.json() assert node.get_protections.called assert node.get_protection_item.called assert node.get_protection_items.called assert result == { "value_id": "123456", "selected": "Unprotected", "options": ["Unprotected", "Protection by Sequence", "No Operation Possible"], }
def test_get_device_detects_garagedoor_barrier(hass, mock_openzwave): """Test device returns garage door.""" node = MockNode() value = MockValue(data="Closed", node=node, command_class=const.COMMAND_CLASS_BARRIER_OPERATOR) values = MockEntityValues(primary=value, node=node) device = cover.get_device(hass=hass, node=node, values=values, node_config={}) assert isinstance(device, cover.ZwaveGarageDoorBarrier) assert device.device_class == "garage" assert device.supported_features == SUPPORT_OPEN | SUPPORT_CLOSE
def device_mapping(hass, mock_openzwave): """Fixture to provide a precreated climate device. Test state mapping.""" node = MockNode() values = MockEntityValues( primary=MockValue(data=1, node=node), temperature=MockValue(data=5, node=node, units=None), mode=MockValue(data='Off', data_items=['Off', 'Cool', 'Heat'], node=node), fan_mode=MockValue(data='test2', data_items=[3, 4, 5], node=node), operating_state=MockValue(data=6, node=node), fan_state=MockValue(data=7, node=node), ) device = climate.get_device(hass, node=node, values=values, node_config={}) yield device
def test_power_schemes(hass, mock_openzwave): """Test power attribute.""" mock_receivers = [] def mock_connect(receiver, signal, *args, **kwargs): if signal == MockNetwork.SIGNAL_VALUE_ADDED: mock_receivers.append(receiver) with patch('pydispatch.dispatcher.connect', new=mock_connect): yield from async_setup_component(hass, 'zwave', {'zwave': { 'new_entity_ids': True, }}) assert len(mock_receivers) == 1 node = MockNode(node_id=11, generic=const.GENERIC_TYPE_SWITCH_BINARY) switch = MockValue(data=True, node=node, index=12, instance=13, command_class=const.COMMAND_CLASS_SWITCH_BINARY, genre=const.GENRE_USER, type=const.TYPE_BOOL) hass.async_add_job(mock_receivers[0], node, switch) yield from hass.async_block_till_done() assert hass.states.get('switch.mock_node_mock_value').state == 'on' assert 'power_consumption' not in hass.states.get( 'switch.mock_node_mock_value').attributes def mock_update(self): self.hass.add_job(self.async_update_ha_state) with patch.object(zwave.node_entity.ZWaveBaseEntity, 'maybe_schedule_update', new=mock_update): power = MockValue(data=23.5, node=node, index=const.INDEX_SENSOR_MULTILEVEL_POWER, instance=13, command_class=const.COMMAND_CLASS_SENSOR_MULTILEVEL) hass.async_add_job(mock_receivers[0], node, power) yield from hass.async_block_till_done() assert hass.states.get( 'switch.mock_node_mock_value').attributes['power_consumption'] == 23.5
def device_zxt_120(hass, mock_openzwave): """Fixture to provide a precreated climate device.""" node = MockNode(manufacturer_id='5254', product_id='8377') values = MockEntityValues( primary=MockValue(data=1, node=node), temperature=MockValue(data=5, node=node), mode=MockValue(data=b'test1', data_items=[0, 1, 2], node=node), fan_mode=MockValue(data=b'test2', data_items=[3, 4, 5], node=node), operating_state=MockValue(data=6, node=node), fan_state=MockValue(data=7, node=node), zxt_120_swing_mode=MockValue( data=b'test3', data_items=[6, 7, 8], node=node), ) device = zwave.get_device(hass, node=node, values=values, node_config={}) yield device
def test_set_rgbw_color(mock_openzwave): """Test setting zwave light color.""" node = MockNode(command_classes=[const.COMMAND_CLASS_SWITCH_COLOR]) value = MockValue(data=0, node=node) color = MockValue(data='#0000000000', node=node) # Suppoorts RGBW color_channels = MockValue(data=0x1d, node=node) values = MockLightValues(primary=value, color=color, color_channels=color_channels) device = zwave.get_device(node=node, values=values, node_config={}) assert color.data == '#0000000000' device.turn_on(**{ATTR_RGB_COLOR: (200, 150, 100)}) assert color.data == '#c86400c800'
def test_get_device_detects_zw098(mock_openzwave): """Test get_device returns a zw098 color light.""" node = MockNode( manufacturer_id="0086", product_id="0062", command_classes=[const.COMMAND_CLASS_SWITCH_COLOR], ) value = MockValue(data=0, node=node) values = MockLightValues(primary=value) device = light.get_device(node=node, values=values, node_config={}) assert isinstance(device, light.ZwaveColorLight) assert device.color_mode == COLOR_MODE_RGB assert device.supported_features == 0 assert device.supported_color_modes == { COLOR_MODE_COLOR_TEMP, COLOR_MODE_RGB }
def test_get_device_detects_rgbw_light(mock_openzwave): """Test get_device returns a color light.""" node = MockNode(command_classes=[const.COMMAND_CLASS_SWITCH_COLOR]) value = MockValue(data=0, node=node) color = MockValue(data="#0000000000", node=node) color_channels = MockValue(data=0x1D, node=node) values = MockLightValues(primary=value, color=color, color_channels=color_channels) device = light.get_device(node=node, values=values, node_config={}) device.value_added() assert isinstance(device, light.ZwaveColorLight) assert device.color_mode == COLOR_MODE_RGBW assert device.supported_features == 0 assert device.supported_color_modes == {COLOR_MODE_RGBW}
def test_alarm_sensor_value_changed(mock_openzwave): """Test value changed for Z-Wave sensor.""" node = MockNode(command_classes=[ const.COMMAND_CLASS_ALARM, const.COMMAND_CLASS_SENSOR_ALARM ]) value = MockValue(data=12.34, node=node, units=homeassistant.const.PERCENTAGE) values = MockEntityValues(primary=value) device = sensor.get_device(node=node, values=values, node_config={}) assert device.state == 12.34 assert device.unit_of_measurement == homeassistant.const.PERCENTAGE value.data = 45.67 value_changed(value) assert device.state == 45.67
def test_rgb_value_changed(mock_openzwave): """Test value changed for rgb lights.""" node = MockNode(command_classes=[const.COMMAND_CLASS_SWITCH_COLOR]) value = MockValue(data=0, node=node) color = MockValue(data="#0000000000", node=node) # Supports RGB only color_channels = MockValue(data=0x1C, node=node) values = MockLightValues(primary=value, color=color, color_channels=color_channels) device = light.get_device(node=node, values=values, node_config={}) assert device.hs_color == (0, 0) color.data = "#ffbf800000" value_changed(color) assert device.hs_color == (29.764, 49.804)
def test_rgbcw_value_changed(mock_openzwave): """Test value changed for rgb lights.""" node = MockNode(command_classes=[const.COMMAND_CLASS_SWITCH_COLOR]) value = MockValue(data=0, node=node) color = MockValue(data='#0000000000', node=node) # Suppoorts RGB, Cold White color_channels = MockValue(data=0x1e, node=node) values = MockLightValues(primary=value, color=color, color_channels=color_channels) device = zwave.get_device(node=node, values=values, node_config={}) assert device.rgb_color == [0, 0, 0] color.data = '#c86400c800' value_changed(color) assert device.rgb_color == [200, 150, 100]
def test_switch_garage_value_changed(opp, mock_openzwave): """Test position changed.""" node = MockNode() value = MockValue(data=False, node=node, command_class=const.COMMAND_CLASS_SWITCH_BINARY) values = MockEntityValues(primary=value, node=node) device = cover.get_device(opp=opp, node=node, values=values, node_config={}) assert device.is_closed value.data = True value_changed(value) assert not device.is_closed
def test_lock_value_changed(mock_openzwave): """Test value changed for Z-Wave lock.""" node = MockNode() values = MockEntityValues( primary=MockValue(data=None, node=node), access_control=None, alarm_type=None, alarm_level=None, ) device = lock.get_device(node=node, values=values, node_config={}) assert not device.is_locked values.primary.data = True value_changed(values.primary) assert device.is_locked
def test_barrier_garage_commands(hass, mock_openzwave): """Test position changed.""" node = MockNode() value = MockValue(data="Closed", node=node, command_class=const.COMMAND_CLASS_BARRIER_OPERATOR) values = MockEntityValues(primary=value, node=node) device = zwave.get_device(hass=hass, node=node, values=values, node_config={}) assert value.data == "Closed" device.open_cover() assert value.data == "Opened" device.close_cover() assert value.data == "Closed"
def test_switch_garage_commands(hass, mock_openzwave): """Test position changed.""" node = MockNode() value = MockValue(data=False, node=node, command_class=const.COMMAND_CLASS_SWITCH_BINARY) values = MockEntityValues(primary=value, node=node) device = zwave.get_device(hass=hass, node=node, values=values, node_config={}) assert value.data is False device.open_cover() assert value.data is True device.close_cover() assert value.data is False
def test_multilevelsensor_value_changed_other_units(mock_openzwave): """Test value changed for Z-Wave multilevel sensor for other units.""" node = MockNode(command_classes=[ const.COMMAND_CLASS_SENSOR_MULTILEVEL, const.COMMAND_CLASS_METER, ]) value = MockValue(data=190.95555, units=homeassistant.const.ENERGY_KILO_WATT_HOUR, node=node) values = MockEntityValues(primary=value) device = sensor.get_device(node=node, values=values, node_config={}) assert device.state == 190.96 assert device.unit_of_measurement == homeassistant.const.ENERGY_KILO_WATT_HOUR value.data = 197.95555 value_changed(value) assert device.state == 197.96