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[ZWAVE_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_dimmer_refresh_value(mock_openzwave): """Test value changed for dimmer lights.""" node = MockNode() value = MockValue(data=0, node=node) values = MockLightValues(primary=value) device = light.get_device( node=node, values=values, node_config={zwave.CONF_REFRESH_VALUE: True, zwave.CONF_REFRESH_DELAY: 5}, ) assert not device.is_on with patch.object(light, "Timer", MagicMock()) as mock_timer: value.data = 46 value_changed(value) assert not device.is_on assert mock_timer.called assert len(mock_timer.mock_calls) == 2 timeout, callback = mock_timer.mock_calls[0][1][:2] assert timeout == 5 assert mock_timer().start.called assert len(mock_timer().start.mock_calls) == 1 with patch.object(light, "Timer", MagicMock()) as mock_timer_2: value_changed(value) assert not device.is_on assert mock_timer().cancel.called assert len(mock_timer_2.mock_calls) == 2 timeout, callback = mock_timer_2.mock_calls[0][1][:2] assert timeout == 5 assert mock_timer_2().start.called assert len(mock_timer_2().start.mock_calls) == 1 callback() assert device.is_on assert device.brightness == 118
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)
def test_switch_refresh_on_update(mock_counter, mock_openzwave): """Test value changed for refresh on update Z-Wave switch.""" mock_counter.return_value = 10 node = MockNode(manufacturer_id="013c", product_type="0001", product_id="0005") value = MockValue(data=False, node=node, instance=1) values = MockEntityValues(primary=value) device = switch.get_device(node=node, values=values, node_config={}) assert not device.is_on mock_counter.return_value = 15 value.data = True value_changed(value) assert device.is_on assert not node.request_state.called mock_counter.return_value = 45 value.data = False value_changed(value) assert not device.is_on assert node.request_state.called
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="Heat", data_items=["Off", "Cool", "Heat", "Full Power", "heat_cool"], node=node, ), fan_mode=MockValue(data="test2", data_items=[3, 4, 5], node=node), operating_state=MockValue(data="heating", node=node), fan_action=MockValue(data=7, node=node), ) device = climate.get_device(hass, node=node, values=values, node_config={}) yield device
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[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_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 == {}
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_value_discovery(hass, mock_openzwave): """Test discovery of a node.""" 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_SENSOR_BINARY) value = MockValue(data=False, node=node, index=12, instance=13, command_class=const.COMMAND_CLASS_SENSOR_BINARY, type=const.TYPE_BOOL, genre=const.GENRE_USER) hass.async_add_job(mock_receivers[0], node, value) yield from hass.async_block_till_done() assert hass.states.get( 'binary_sensor.mock_node_mock_value').state is 'off'
def device(hass, mock_openzwave): """Fixture to provide a precreated climate device.""" node = MockNode() values = MockEntityValues( primary=MockValue( data=HVAC_MODE_HEAT, data_items=[ HVAC_MODE_OFF, HVAC_MODE_HEAT, HVAC_MODE_COOL, HVAC_MODE_HEAT_COOL, ], node=node, ), setpoint_heating=MockValue(data=1, node=node), setpoint_cooling=MockValue(data=10, node=node), temperature=MockValue(data=5, node=node, units=None), fan_mode=MockValue(data="test2", data_items=[3, 4, 5], node=node), operating_state=MockValue(data=CURRENT_HVAC_HEAT, node=node), fan_action=MockValue(data=7, node=node), ) device = climate.get_device(hass, node=node, values=values, node_config={}) yield device
def device_heat_cool(hass, mock_openzwave): """Fixture to provide a precreated climate device. Test state heat only.""" node = MockNode() values = MockEntityValues( primary=MockValue(data=1, node=node), temperature=MockValue(data=5, node=node, units=None), mode=MockValue( data=HVAC_MODE_HEAT, data_items=[ HVAC_MODE_OFF, HVAC_MODE_HEAT, HVAC_MODE_COOL, "Heat Eco", "Cool Eco", ], node=node, ), fan_mode=MockValue(data="test2", data_items=[3, 4, 5], node=node), operating_state=MockValue(data="test4", node=node), fan_action=MockValue(data=7, node=node), ) device = climate.get_device(hass, node=node, values=values, node_config={}) yield device
def test_get_device_no_component_mapping(): """Test that None is returned.""" node = MockNode(manufacturer_id=" ") value = MockValue(data=0, node=node) assert workaround.get_device_component_mapping(value) is None
def test_get_device_mapping_mt(): """Test that device mapping mt is returned.""" node = MockNode(manufacturer_id="0047", product_type="5a52") value = MockValue(data=0, node=node) assert workaround.get_device_mapping(value) == "workaround_no_position"
async def test_clear_code(hass, lock_data, sent_messages, mock_openzwave, caplog): """Test refresh_codes""" entry = MockConfigEntry(domain=DOMAIN, title="frontdoor", data=CONFIG_DATA, version=2) entry.add_to_hass(hass) assert await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() # Test ZWaveIntegrationNotConfiguredError servicedata = { "entity_id": "lock.kwikset_touchpad_electronic_deadbolt_frontdoor", "code_slot": 1, } await hass.services.async_call(DOMAIN, SERVICE_CLEAR_CODE, servicedata) await hass.async_block_till_done() assert ( "A Z-Wave integration has not been configured for this Home Assistant instance" in caplog.text) # Mock using_zwave with patch("custom_components.keymaster.services.async_using_zwave", return_value=True): servicedata = { "entity_id": "lock.kwikset_touchpad_electronic_deadbolt_frontdoor", "code_slot": 1, } await hass.services.async_call(DOMAIN, SERVICE_CLEAR_CODE, servicedata) await hass.async_block_till_done() assert ( "Problem retrieving node_id from entity lock.kwikset_touchpad_electronic_deadbolt_frontdoor" in caplog.text) with patch("custom_components.keymaster.services.async_using_zwave", return_value=True), patch( "custom_components.keymaster.services.get_node_id", return_value="14"): servicedata = { "entity_id": "lock.kwikset_touchpad_electronic_deadbolt_frontdoor", "code_slot": 1, } await hass.services.async_call(DOMAIN, SERVICE_CLEAR_CODE, servicedata) await hass.async_block_till_done() assert ( "Error calling lock.set_usercode service call: Unable to find service lock.set_usercode" in caplog.text) with patch("custom_components.keymaster.services.async_using_zwave", return_value=True): # Setup zwave mock hass.data[DATA_NETWORK] = mock_openzwave node = MockNode(node_id=12) value0 = MockValue(data="12345678", node=node, index=1) value1 = MockValue(data="******", node=node, index=2) node.get_values.return_value = { value0.value_id: value0, value1.value_id: value1, } mock_openzwave.nodes = {node.node_id: node} entity = node_entity.ZWaveNodeEntity(node, mock_openzwave) # Setup the zwave integration await setup_zwave(hass, mock_openzwave) await hass.async_block_till_done() # Set the zwave network as ready hass.data[DATA_NETWORK].state = MockNetwork.STATE_READY # Create the entities hass.states.async_set( "sensor.smartcode_10_touchpad_electronic_deadbolt_alarm_level_2", 1) await hass.async_block_till_done() hass.states.async_set( "sensor.smartcode_10_touchpad_electronic_deadbolt_alarm_type_2", 22) await hass.async_block_till_done() hass.states.async_set( "lock.smartcode_10_touchpad_electronic_deadbolt_locked_2", "locked", {"node_id": 12}, ) await hass.async_block_till_done() servicedata = { "entity_id": "lock.smartcode_10_touchpad_electronic_deadbolt_locked_2", "code_slot": 1, } await hass.services.async_call(DOMAIN, SERVICE_CLEAR_CODE, servicedata) await hass.async_block_till_done() assert "Usercode at slot 1 is cleared" in caplog.text # Bring OZW up await setup_ozw(hass, fixture=lock_data) with patch("custom_components.keymaster.services.async_using_ozw", return_value=True): state = hass.states.get( "lock.smartcode_10_touchpad_electronic_deadbolt_locked") assert state is not None assert state.state == "locked" assert state.attributes["node_id"] == 14 entry = MockConfigEntry(domain=DOMAIN, title="frontdoor", data=CONFIG_DATA, version=2) entry.add_to_hass(hass) assert await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() servicedata = { "entity_id": "lock.kwikset_touchpad_electronic_deadbolt_frontdoor", "code_slot": 1, } await hass.services.async_call(DOMAIN, SERVICE_CLEAR_CODE, servicedata) await hass.async_block_till_done() assert ( "Unable to find referenced entities lock.kwikset_touchpad_electronic_deadbolt_frontdoor" in caplog.text) servicedata = { "entity_id": "lock.smartcode_10_touchpad_electronic_deadbolt_locked", "code_slot": 1, } await hass.services.async_call(DOMAIN, SERVICE_CLEAR_CODE, servicedata) await hass.async_block_till_done() assert len(sent_messages) == 4 msg = sent_messages[3] assert msg["topic"] == "OpenZWave/1/command/setvalue/" assert msg["payload"] == {"Value": 1, "ValueIDKey": 72057594287013910}
def test_get_device_component_mapping(): """Test that component is returned.""" node = MockNode(manufacturer_id='010f', product_type='0b00') value = MockValue(data=0, node=node, command_class=const.COMMAND_CLASS_SENSOR_ALARM) assert workaround.get_device_component_mapping(value) == 'binary_sensor'
def test_get_device_mapping_mtii(): """Test that device mapping mtii is returned.""" node = MockNode(manufacturer_id='013c', product_type='0002', product_id='0002') value = MockValue(data=0, node=node, index=0) assert workaround.get_device_mapping(value) == 'trigger_no_off_event'
async def test_update_usercodes_using_zwave(hass, mock_openzwave, caplog): """Test handling usercode updates using zwave""" mock_receivers = {} def mock_connect(receiver, signal, *args, **kwargs): mock_receivers[signal] = receiver with patch("pydispatch.dispatcher.connect", new=mock_connect): await async_setup_component(hass, "zwave", {"zwave": {}}) await hass.async_block_till_done() # Setup zwave mock hass.data[DATA_NETWORK] = mock_openzwave node = MockNode(node_id=12) value0 = MockValue(data="12345678", node=node, index=1) value1 = MockValue(data="******", node=node, index=2) node.get_values.return_value = { value0.value_id: value0, value1.value_id: value1, } mock_openzwave.nodes = {node.node_id: node} entity = node_entity.ZWaveNodeEntity(node, mock_openzwave) # Setup the zwave integration await setup_zwave(hass, mock_openzwave) await hass.async_block_till_done() # Set the zwave network as ready hass.data[DATA_NETWORK].state = MockNetwork.STATE_READY assert mock_receivers await hass.async_add_executor_job( mock_receivers[MockNetwork.SIGNAL_ALL_NODES_QUERIED]) # Create the entities hass.states.async_set( "sensor.smartcode_10_touchpad_electronic_deadbolt_alarm_level", 1) await hass.async_block_till_done() hass.states.async_set( "sensor.smartcode_10_touchpad_electronic_deadbolt_alarm_type", 22) await hass.async_block_till_done() hass.states.async_set( "lock.smartcode_10_touchpad_electronic_deadbolt_locked", "locked", {"node_id": 12}, ) await hass.async_block_till_done() # Load the integration with patch("custom_components.keymaster.binary_sensor.using_zwave", return_value=True), patch( "custom_components.keymaster.using_zwave", return_value=True): entry = MockConfigEntry(domain=DOMAIN, title="frontdoor", data=CONFIG_DATA_REAL, version=2) entry.add_to_hass(hass) assert await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() # Fire the event hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) await hass.async_block_till_done() assert hass.states.get(NETWORK_READY_ENTITY) assert hass.states.get(NETWORK_READY_ENTITY).state == "on" assert hass.states.get("sensor.frontdoor_code_slot_1").state == "12345678" assert "Work around code in use." in caplog.text
def test_set_config_parameter(self): """Test zwave set_config_parameter service.""" value = MockValue( index=12, command_class=const.COMMAND_CLASS_CONFIGURATION, type=const.TYPE_BYTE, ) value_list = MockValue( index=13, command_class=const.COMMAND_CLASS_CONFIGURATION, type=const.TYPE_LIST, data_items=['item1', 'item2', 'item3'], ) value_list_int = MockValue( index=15, command_class=const.COMMAND_CLASS_CONFIGURATION, type=const.TYPE_LIST, data_items=['1', '2', '3'], ) value_button = MockValue( index=14, command_class=const.COMMAND_CLASS_CONFIGURATION, type=const.TYPE_BUTTON, ) node = MockNode(node_id=14) node.get_values.return_value = {12: value, 13: value_list, 14: value_button, 15: value_list_int} self.zwave_network.nodes = {14: node} self.hass.services.call('zwave', 'set_config_parameter', { const.ATTR_NODE_ID: 14, const.ATTR_CONFIG_PARAMETER: 13, const.ATTR_CONFIG_VALUE: 'item3', }) self.hass.block_till_done() assert value_list.data == 'item3' self.hass.services.call('zwave', 'set_config_parameter', { const.ATTR_NODE_ID: 14, const.ATTR_CONFIG_PARAMETER: 15, const.ATTR_CONFIG_VALUE: 3, }) self.hass.block_till_done() assert value_list_int.data == '3' self.hass.services.call('zwave', 'set_config_parameter', { const.ATTR_NODE_ID: 14, const.ATTR_CONFIG_PARAMETER: 12, const.ATTR_CONFIG_VALUE: 7, }) self.hass.block_till_done() assert value.data == 7 self.hass.services.call('zwave', 'set_config_parameter', { const.ATTR_NODE_ID: 14, const.ATTR_CONFIG_PARAMETER: 14, const.ATTR_CONFIG_VALUE: True, }) self.hass.block_till_done() assert self.zwave_network.manager.pressButton.called assert self.zwave_network.manager.releaseButton.called self.hass.services.call('zwave', 'set_config_parameter', { const.ATTR_NODE_ID: 14, const.ATTR_CONFIG_PARAMETER: 19, const.ATTR_CONFIG_VALUE: 0x01020304, const.ATTR_CONFIG_SIZE: 4 }) self.hass.block_till_done() assert node.set_config_param.called assert len(node.set_config_param.mock_calls) == 1 assert node.set_config_param.mock_calls[0][1][0] == 19 assert node.set_config_param.mock_calls[0][1][1] == 0x01020304 assert node.set_config_param.mock_calls[0][1][2] == 4 node.set_config_param.reset_mock()