def update_icloud(self, ): """Authenticate against iCloud and scan for devices.""" if self.api is not None: from pyicloud.exceptions import PyiCloudNoDevicesException try: # Loop through every device registered with the iCloud account status = self.identifier.status(DEVICESTATUSSET) dev_id = slugify(status['name'].replace(' ', '', 99)) self._devicestatuscode = status['deviceStatus'] if self._devicestatuscode == '200': self._devicestatus = 'online' elif self._devicestatuscode == '201': self._devicestatus = 'offline' elif self._devicestatuscode == '203': self._devicestatus = 'pending' elif self._devicestatuscode == '204': self._devicestatus = 'unregistered' else: self._devicestatus = 'error' self._lowpowermode = status['lowPowerMode'] self._batterystatus = status['batteryStatus'] self.update_ha_state() status = self.identifier.status(DEVICESTATUSSET) battery = status['batteryLevel'] * 100 location = status['location'] if location: see(hass=self.hass, dev_id=dev_id, host_name=status['name'], gps=(location['latitude'], location['longitude']), battery=battery, gps_accuracy=location['horizontalAccuracy']) except PyiCloudNoDevicesException: _LOGGER.error('No iCloud Devices found!')
def test_new_device_event_fired(self): """Test that the device tracker will fire an event.""" with assert_setup_component(1, device_tracker.DOMAIN): assert setup_component(self.hass, device_tracker.DOMAIN, TEST_PLATFORM) test_events = [] @callback def listener(event): """Record that our event got called.""" test_events.append(event) self.hass.bus.listen("device_tracker_new_device", listener) device_tracker.see(self.hass, 'mac_1', host_name='hello') device_tracker.see(self.hass, 'mac_1', host_name='hello') self.hass.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', }
def test_see_service(self, mock_see): """Test the see service with a unicode dev_id and NO MAC.""" with assert_setup_component(1, device_tracker.DOMAIN): assert setup_component(self.hass, device_tracker.DOMAIN, TEST_PLATFORM) params = { 'dev_id': 'some_device', 'host_name': 'example.com', 'location_name': 'Work', 'gps': [.3, .8], 'attributes': { 'test': 'test' } } device_tracker.see(self.hass, **params) self.hass.block_till_done() assert mock_see.call_count == 1 self.assertEqual(mock_see.call_count, 1) self.assertEqual(mock_see.call_args, call(**params)) mock_see.reset_mock() params['dev_id'] += chr(233) # e' acute accent from icloud device_tracker.see(self.hass, **params) self.hass.block_till_done() assert mock_see.call_count == 1 self.assertEqual(mock_see.call_count, 1) self.assertEqual(mock_see.call_args, call(**params))
def test_see_service(self, mock_see): """Test the see service with a unicode dev_id and NO MAC.""" with assert_setup_component(1, device_tracker.DOMAIN): assert setup_component(self.hass, device_tracker.DOMAIN, TEST_PLATFORM) params = { 'dev_id': 'some_device', 'host_name': 'example.com', 'location_name': 'Work', 'gps': [.3, .8], 'attributes': { 'test': 'test' } } device_tracker.see(self.hass, **params) self.hass.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 device_tracker.see(self.hass, **params) self.hass.block_till_done() assert mock_see.call_count == 1 assert mock_see.call_count == 1 assert mock_see.call_args == call(**params)
def test_new_device_event_fired(self): """Test that the device tracker will fire an event.""" with assert_setup_component(1, device_tracker.DOMAIN): assert setup_component(self.hass, device_tracker.DOMAIN, TEST_PLATFORM) test_events = [] @callback def listener(event): """Helper method that will verify our event got called.""" test_events.append(event) self.hass.bus.listen("device_tracker_new_device", listener) device_tracker.see(self.hass, 'mac_1', host_name='hello') device_tracker.see(self.hass, 'mac_1', host_name='hello') self.hass.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', 'vendor': 'unknown', }
def test_not_allow_invalid_dev_id(self): # pylint: disable=invalid-name """Test that the device tracker will not allow invalid dev ids.""" self.assertTrue(setup_component(self.hass, device_tracker.DOMAIN, TEST_PLATFORM)) device_tracker.see(self.hass, dev_id="hello-world") config = device_tracker.load_config(self.yaml_devices, self.hass, timedelta(seconds=0)) assert len(config) == 0
def test_not_allow_invalid_dev_id(self): # pylint: disable=invalid-name """Test that the device tracker will not allow invalid dev ids.""" self.assertTrue(device_tracker.setup(self.hass, TEST_PLATFORM)) device_tracker.see(self.hass, dev_id='hello-world') config = device_tracker.load_config(self.yaml_devices, self.hass, timedelta(seconds=0)) assert len(config) == 0
def test_not_allow_invalid_dev_id(self): """Test that the device tracker will not allow invalid dev ids.""" self.assertTrue(device_tracker.setup(self.hass, {})) device_tracker.see(self.hass, dev_id='hello-world') config = device_tracker.load_config(self.yaml_devices, self.hass, timedelta(seconds=0), 0) assert len(config) == 0
def test_not_allow_invalid_dev_id(self): """Test that the device tracker will not allow invalid dev ids.""" self.assertTrue(setup_component(self.hass, device_tracker.DOMAIN, TEST_PLATFORM)) device_tracker.see(self.hass, dev_id='hello-world') config = device_tracker.load_config(self.yaml_devices, self.hass, timedelta(seconds=0)) assert len(config) == 0
def test_not_allow_invalid_dev_id(self): """Test that the device tracker will not allow invalid dev ids.""" with assert_setup_component(1, device_tracker.DOMAIN): assert setup_component(self.hass, device_tracker.DOMAIN, TEST_PLATFORM) device_tracker.see(self.hass, dev_id='hello-world') config = device_tracker.load_config(self.yaml_devices, self.hass, timedelta(seconds=0)) assert len(config) == 0
def test_not_write_duplicate_yaml_keys(self): """Test that the device tracker will not generate invalid YAML.""" self.assertTrue(device_tracker.setup(self.hass, {})) device_tracker.see(self.hass, 'mac_1', host_name='hello') device_tracker.see(self.hass, 'mac_2', host_name='hello') self.hass.pool.block_till_done() config = device_tracker.load_config(self.yaml_devices, self.hass, timedelta(seconds=0), 0) assert len(config) == 2
def test_see_service_unicode_dev_id(self, mock_see): """Test the see service with a unicode dev_id and NO MAC.""" self.assertTrue(device_tracker.setup(self.hass, {})) params = { 'dev_id': chr(233), # e' acute accent from icloud 'host_name': 'example.com', 'location_name': 'Work', 'gps': [.3, .8] } device_tracker.see(self.hass, **params) self.hass.pool.block_till_done() mock_see.assert_called_once_with(**params)
def test_not_write_duplicate_yaml_keys(self): # pylint: disable=invalid-name """Test that the device tracker will not generate invalid YAML.""" self.assertTrue(setup_component(self.hass, device_tracker.DOMAIN, TEST_PLATFORM)) device_tracker.see(self.hass, "mac_1", host_name="hello") device_tracker.see(self.hass, "mac_2", host_name="hello") self.hass.block_till_done() config = device_tracker.load_config(self.yaml_devices, self.hass, timedelta(seconds=0)) assert len(config) == 2
def test_not_write_duplicate_yaml_keys(self): """Test that the device tracker will not generate invalid YAML.""" with assert_setup_component(1, device_tracker.DOMAIN): assert setup_component(self.hass, device_tracker.DOMAIN, TEST_PLATFORM) device_tracker.see(self.hass, 'mac_1', host_name='hello') device_tracker.see(self.hass, 'mac_2', host_name='hello') self.hass.block_till_done() config = device_tracker.load_config(self.yaml_devices, self.hass, timedelta(seconds=0)) assert len(config) == 2
def test_see_service(self, mock_see): self.assertTrue(device_tracker.setup(self.hass, {})) mac = 'AB:CD:EF:GH' dev_id = 'some_device' host_name = 'example.com' location_name = 'Work' gps = [.3, .8] device_tracker.see(self.hass, mac, dev_id, host_name, location_name, gps) self.hass.pool.block_till_done() mock_see.assert_called_once_with( mac=mac, dev_id=dev_id, host_name=host_name, location_name=location_name, gps=gps)
def test_see_service(self, mock_see): """Test the see service with a unicode dev_id and NO MAC.""" self.assertTrue(setup_component(self.hass, device_tracker.DOMAIN, TEST_PLATFORM)) params = {"dev_id": "some_device", "host_name": "example.com", "location_name": "Work", "gps": [0.3, 0.8]} device_tracker.see(self.hass, **params) self.hass.block_till_done() assert mock_see.call_count == 1 mock_see.assert_called_once_with(**params) mock_see.reset_mock() params["dev_id"] += chr(233) # e' acute accent from icloud device_tracker.see(self.hass, **params) self.hass.block_till_done() assert mock_see.call_count == 1 mock_see.assert_called_once_with(**params)
def test_new_device_event_fired(self): """Test that the device tracker will fire an event.""" self.assertTrue( setup_component(self.hass, device_tracker.DOMAIN, TEST_PLATFORM)) test_events = [] @callback def listener(event): """Helper method that will verify our event got called.""" test_events.append(event) self.hass.bus.listen("device_tracker_new_device", listener) device_tracker.see(self.hass, 'mac_1', host_name='hello') device_tracker.see(self.hass, 'mac_1', host_name='hello') self.hass.block_till_done() self.assertEqual(1, len(test_events))
def test_see_service(self, mock_see): self.assertTrue(device_tracker.setup(self.hass, {})) mac = 'AB:CD:EF:GH' dev_id = 'some_device' host_name = 'example.com' location_name = 'Work' gps = [.3, .8] device_tracker.see(self.hass, mac, dev_id, host_name, location_name, gps) self.hass.pool.block_till_done() mock_see.assert_called_once_with(mac=mac, dev_id=dev_id, host_name=host_name, location_name=location_name, gps=gps)
def test_see_service(self, mock_see): """Test the see service with a unicode dev_id and NO MAC.""" self.assertTrue(device_tracker.setup(self.hass, TEST_PLATFORM)) params = { 'dev_id': 'some_device', 'host_name': 'example.com', 'location_name': 'Work', 'gps': [.3, .8] } device_tracker.see(self.hass, **params) self.hass.pool.block_till_done() assert mock_see.call_count == 1 mock_see.assert_called_once_with(**params) mock_see.reset_mock() params['dev_id'] += chr(233) # e' acute accent from icloud device_tracker.see(self.hass, **params) self.hass.pool.block_till_done() assert mock_see.call_count == 1 mock_see.assert_called_once_with(**params)
def test_see_state(self): """Test device tracker see records state correctly.""" assert setup_component(self.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, }, } device_tracker.see(self.hass, **params) self.hass.block_till_done() config = device_tracker.load_config(self.yaml_devices, self.hass, timedelta(seconds=0)) assert len(config) == 1 state = self.hass.states.get('device_tracker.examplecom') attrs = state.attributes assert state.state == 'Work' assert state.object_id == 'examplecom' 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
def test_see_state(self, mock_set_vendor): """Test device tracker see records state correctly.""" self.assertTrue(setup_component(self.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, }, } device_tracker.see(self.hass, **params) self.hass.block_till_done() config = device_tracker.load_config(self.yaml_devices, self.hass, timedelta(seconds=0)) assert len(config) == 1 state = self.hass.states.get('device_tracker.examplecom') attrs = state.attributes self.assertEqual(state.state, 'Work') self.assertEqual(state.object_id, 'examplecom') self.assertEqual(state.name, 'example.com') self.assertEqual(attrs['friendly_name'], 'example.com') self.assertEqual(attrs['battery'], 100) self.assertEqual(attrs['latitude'], 0.3) self.assertEqual(attrs['longitude'], 0.8) self.assertEqual(attrs['test'], 'test') self.assertEqual(attrs['gps_accuracy'], 1) self.assertEqual(attrs['source_type'], 'gps') self.assertEqual(attrs['number'], 1)
def update_icloud(self, see): """ Authenticate against iCloud and scan for devices. """ if self.api is not None: from pyicloud.exceptions import PyiCloudNoDevicesException try: # Loop through every device registered with the iCloud account status = self.identifier.status(DEVICESTATUSSET) dev_id = re.sub(r"(\s|\W|')", '', status['name']).lower() self._devicestatuscode = status['deviceStatus'] if self._devicestatuscode == '200': self._devicestatus = 'online' elif self._devicestatuscode == '201': self._devicestatus = 'offline' elif self._devicestatuscode == '203': self._devicestatus = 'pending' elif self._devicestatuscode == '204': self._devicestatus = 'unregistered' else: self._devicestatus = 'error' self._lowPowerMode = status['lowPowerMode'] self._batteryStatus = status['batteryStatus'] self.update_ha_state() battery = status['batteryLevel']*100 location = status['location'] if location: see( hass=self.hass, dev_id=dev_id, host_name=status['name'], gps=(location['latitude'], location['longitude']), battery=battery, gps_accuracy=location['horizontalAccuracy'] ) except PyiCloudNoDevicesException: _LOGGER.error('No iCloud Devices found!')