async def test_new_device_event_fired(hass, mock_device_tracker_conf, enable_custom_integrations): """Test that the device tracker will fire an event.""" with assert_setup_component(1, device_tracker.DOMAIN): assert await async_setup_component(hass, device_tracker.DOMAIN, TEST_PLATFORM) test_events = [] @callback def listener(event): """Record that our event got called.""" test_events.append(event) hass.bus.async_listen("device_tracker_new_device", listener) common.async_see(hass, "mac_1", host_name="hello") common.async_see(hass, "mac_1", host_name="hello") await hass.async_block_till_done() assert len(test_events) == 1 # Assert we can serialize the event json.dumps(test_events[0].as_dict(), cls=JSONEncoder) assert test_events[0].data == { "entity_id": "device_tracker.hello", "host_name": "hello", "mac": "MAC_1", }
async def test_new_device_event_fired(hass): """Test that the device tracker will fire an event.""" with assert_setup_component(1, device_tracker.DOMAIN): assert await async_setup_component(hass, device_tracker.DOMAIN, TEST_PLATFORM) test_events = [] @callback def listener(event): """Record that our event got called.""" test_events.append(event) hass.bus.async_listen("device_tracker_new_device", listener) common.async_see(hass, 'mac_1', host_name='hello') common.async_see(hass, 'mac_1', host_name='hello') await hass.async_block_till_done() assert len(test_events) == 1 # Assert we can serialize the event json.dumps(test_events[0].as_dict(), cls=JSONEncoder) assert test_events[0].data == { 'entity_id': 'device_tracker.hello', 'host_name': 'hello', 'mac': 'MAC_1', }
async def test_see_service(mock_see, hass): """Test the see service with a unicode dev_id and NO MAC.""" with assert_setup_component(1, device_tracker.DOMAIN): assert await async_setup_component(hass, device_tracker.DOMAIN, TEST_PLATFORM) params = { 'dev_id': 'some_device', 'host_name': 'example.com', 'location_name': 'Work', 'gps': [.3, .8], 'attributes': { 'test': 'test' } } common.async_see(hass, **params) await hass.async_block_till_done() assert mock_see.call_count == 1 assert mock_see.call_count == 1 assert mock_see.call_args == call(**params) mock_see.reset_mock() params['dev_id'] += chr(233) # e' acute accent from icloud common.async_see(hass, **params) await hass.async_block_till_done() assert mock_see.call_count == 1 assert mock_see.call_count == 1 assert mock_see.call_args == call(**params)
async def test_see_service(mock_see, hass, enable_custom_integrations): """Test the see service with a unicode dev_id and NO MAC.""" with assert_setup_component(1, device_tracker.DOMAIN): assert await async_setup_component(hass, device_tracker.DOMAIN, TEST_PLATFORM) params = { "dev_id": "some_device", "host_name": "example.com", "location_name": "Work", "gps": [0.3, 0.8], "attributes": { "test": "test" }, } common.async_see(hass, **params) await hass.async_block_till_done() assert mock_see.call_count == 1 assert mock_see.call_count == 1 assert mock_see.call_args == call(**params) mock_see.reset_mock() params["dev_id"] += chr(233) # e' acute accent from icloud common.async_see(hass, **params) await hass.async_block_till_done() assert mock_see.call_count == 1 assert mock_see.call_count == 1 assert mock_see.call_args == call(**params)
async def test_invalid_dev_id(hass, mock_device_tracker_conf): """Test that the device tracker will not allow invalid dev ids.""" devices = mock_device_tracker_conf with assert_setup_component(1, device_tracker.DOMAIN): assert await async_setup_component(hass, device_tracker.DOMAIN, TEST_PLATFORM) common.async_see(hass, dev_id="hello-world") await hass.async_block_till_done() assert not devices
async def test_not_allow_invalid_dev_id(hass, yaml_devices): """Test that the device tracker will not allow invalid dev ids.""" with assert_setup_component(1, device_tracker.DOMAIN): assert await async_setup_component(hass, device_tracker.DOMAIN, TEST_PLATFORM) common.async_see(hass, dev_id='hello-world') config = await device_tracker.async_load_config(yaml_devices, hass, timedelta(seconds=0)) assert len(config) == 0
async def test_invalid_dev_id(opp, mock_device_tracker_conf, enable_custom_integrations): """Test that the device tracker will not allow invalid dev ids.""" devices = mock_device_tracker_conf with assert_setup_component(1, device_tracker.DOMAIN): assert await async_setup_component(opp, device_tracker.DOMAIN, TEST_PLATFORM) common.async_see(opp, dev_id="hello-world") await opp.async_block_till_done() assert not devices
async def test_duplicate_yaml_keys(hass, mock_device_tracker_conf): """Test that the device tracker will not generate invalid YAML.""" devices = mock_device_tracker_conf with assert_setup_component(1, device_tracker.DOMAIN): assert await async_setup_component(hass, device_tracker.DOMAIN, TEST_PLATFORM) common.async_see(hass, "mac_1", host_name="hello") common.async_see(hass, "mac_2", host_name="hello") await hass.async_block_till_done() assert len(devices) == 2 assert devices[0].dev_id != devices[1].dev_id
async def test_not_write_duplicate_yaml_keys(hass, yaml_devices): """Test that the device tracker will not generate invalid YAML.""" with assert_setup_component(1, device_tracker.DOMAIN): assert await async_setup_component(hass, device_tracker.DOMAIN, TEST_PLATFORM) common.async_see(hass, 'mac_1', host_name='hello') common.async_see(hass, 'mac_2', host_name='hello') await hass.async_block_till_done() config = await device_tracker.async_load_config(yaml_devices, hass, timedelta(seconds=0)) assert len(config) == 2
async def test_see_service_guard_config_entry(hass, mock_device_tracker_conf): """Test the guard if the device is registered in the entity registry.""" mock_entry = Mock() dev_id = "test" entity_id = f"{const.DOMAIN}.{dev_id}" mock_registry(hass, {entity_id: mock_entry}) devices = mock_device_tracker_conf assert await async_setup_component(hass, device_tracker.DOMAIN, TEST_PLATFORM) params = {"dev_id": dev_id, "gps": [0.3, 0.8]} common.async_see(hass, **params) await hass.async_block_till_done() assert not devices
async def test_see_service_guard_config_entry(opp, mock_device_tracker_conf): """Test the guard if the device is registered in the entity registry.""" mock_entry = Mock() dev_id = "test" entity_id = const.ENTITY_ID_FORMAT.format(dev_id) mock_registry(opp, {entity_id: mock_entry}) devices = mock_device_tracker_conf assert await async_setup_component(opp, device_tracker.DOMAIN, TEST_PLATFORM) params = {"dev_id": dev_id, "gps": [0.3, 0.8]} common.async_see(opp, **params) await opp.async_block_till_done() assert not devices
async def test_see_service_guard_config_entry(hass, mock_device_tracker_conf): """Test the guard if the device is registered in the entity registry.""" mock_entry = Mock() dev_id = 'test' entity_id = const.ENTITY_ID_FORMAT.format(dev_id) mock_registry(hass, {entity_id: mock_entry}) devices = mock_device_tracker_conf assert await async_setup_component(hass, device_tracker.DOMAIN, TEST_PLATFORM) params = { 'dev_id': dev_id, 'gps': [.3, .8], } common.async_see(hass, **params) await hass.async_block_till_done() assert not devices
async def test_see_state(hass, yaml_devices): """Test device tracker see records state correctly.""" assert await async_setup_component(hass, device_tracker.DOMAIN, TEST_PLATFORM) params = { 'mac': 'AA:BB:CC:DD:EE:FF', 'dev_id': 'some_device', 'host_name': 'example.com', 'location_name': 'Work', 'gps': [.3, .8], 'gps_accuracy': 1, 'battery': 100, 'attributes': { 'test': 'test', 'number': 1, }, } common.async_see(hass, **params) await hass.async_block_till_done() config = await legacy.async_load_config(yaml_devices, hass, timedelta(seconds=0)) assert len(config) == 1 state = hass.states.get('device_tracker.example_com') attrs = state.attributes assert state.state == 'Work' assert state.object_id == 'example_com' assert state.name == 'example.com' assert attrs['friendly_name'] == 'example.com' assert attrs['battery'] == 100 assert attrs['latitude'] == 0.3 assert attrs['longitude'] == 0.8 assert attrs['test'] == 'test' assert attrs['gps_accuracy'] == 1 assert attrs['source_type'] == 'gps' assert attrs['number'] == 1
async def test_see_state(hass, yaml_devices): """Test device tracker see records state correctly.""" assert await async_setup_component(hass, device_tracker.DOMAIN, TEST_PLATFORM) params = { "mac": "AA:BB:CC:DD:EE:FF", "dev_id": "some_device", "host_name": "example.com", "location_name": "Work", "gps": [0.3, 0.8], "gps_accuracy": 1, "battery": 100, "attributes": { "test": "test", "number": 1 }, } common.async_see(hass, **params) await hass.async_block_till_done() config = await legacy.async_load_config(yaml_devices, hass, timedelta(seconds=0)) assert len(config) == 1 state = hass.states.get("device_tracker.example_com") attrs = state.attributes assert state.state == "Work" assert state.object_id == "example_com" assert state.name == "example.com" assert attrs["friendly_name"] == "example.com" assert attrs["battery"] == 100 assert attrs["latitude"] == 0.3 assert attrs["longitude"] == 0.8 assert attrs["test"] == "test" assert attrs["gps_accuracy"] == 1 assert attrs["source_type"] == "gps" assert attrs["number"] == 1
async def test_see_state(hass, yaml_devices): """Test device tracker see records state correctly.""" assert await async_setup_component(hass, device_tracker.DOMAIN, TEST_PLATFORM) params = { 'mac': 'AA:BB:CC:DD:EE:FF', 'dev_id': 'some_device', 'host_name': 'example.com', 'location_name': 'Work', 'gps': [.3, .8], 'gps_accuracy': 1, 'battery': 100, 'attributes': { 'test': 'test', 'number': 1, }, } common.async_see(hass, **params) await hass.async_block_till_done() config = await device_tracker.async_load_config(yaml_devices, hass, timedelta(seconds=0)) assert len(config) == 1 state = hass.states.get('device_tracker.example_com') attrs = state.attributes assert state.state == 'Work' assert state.object_id == 'example_com' assert state.name == 'example.com' assert attrs['friendly_name'] == 'example.com' assert attrs['battery'] == 100 assert attrs['latitude'] == 0.3 assert attrs['longitude'] == 0.8 assert attrs['test'] == 'test' assert attrs['gps_accuracy'] == 1 assert attrs['source_type'] == 'gps' assert attrs['number'] == 1