def test_setup(self):
        """ Test setup method. """
        # Bogus config
        self.assertFalse(device_tracker.setup(self.hass, {}))

        self.assertFalse(
            device_tracker.setup(self.hass, {device_tracker.DOMAIN: {}}))

        # Test with non-existing component
        self.assertFalse(
            device_tracker.setup(
                self.hass,
                {device_tracker.DOMAIN: {
                    CONF_PLATFORM: 'nonexisting'
                }}))

        # Test with a bad known device file around
        with open(self.known_dev_path, 'w') as fil:
            fil.write("bad data\nbad data\n")

        self.assertFalse(
            device_tracker.setup(
                self.hass, {device_tracker.DOMAIN: {
                    CONF_PLATFORM: 'test'
                }}))
示例#2
0
 def test_config_failure(self, mock_ex):
     """Test that the device tracker see failures."""
     device_tracker.setup(
         self.hass,
         {device_tracker.DOMAIN: {
             device_tracker.CONF_CONSIDER_HOME: -1
         }})
     assert mock_ex.call_count == 1
示例#3
0
    def setup_method(self, method):
        """ Init needed objects. """
        self.hass = get_test_home_assistant()
        mock_mqtt_component(self.hass)
        self.assertTrue(
            device_tracker.setup(
                self.hass,
                {device_tracker.DOMAIN: {
                    CONF_PLATFORM: 'owntracks'
                }}))

        self.hass.states.set('zone.inner', 'zoning', {
            'name': 'zone',
            'latitude': 2.1,
            'longitude': 1.1,
            'radius': 10
        })

        self.hass.states.set('zone.inner_2', 'zoning', {
            'name': 'zone',
            'latitude': 2.1,
            'longitude': 1.1,
            'radius': 10
        })

        self.hass.states.set('zone.outer', 'zoning', {
            'name': 'zone',
            'latitude': 2.0,
            'longitude': 1.0,
            'radius': 100000
        })
        # Clear state between teste
        self.hass.states.set(DEVICE_TRACKER_STATE, None)
        owntracks.REGIONS_ENTERED = defaultdict(list)
        owntracks.MOBILE_BEACONS_ACTIVE = defaultdict(list)
示例#4
0
    def test_update_stale(self):
        """Test stalled update."""
        scanner = get_component('device_tracker.test').SCANNER
        scanner.reset()
        scanner.come_home('DEV1')

        register_time = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC)
        scan_time = datetime(2015, 9, 15, 23, 1, tzinfo=dt_util.UTC)

        with patch('homeassistant.components.device_tracker.dt_util.utcnow',
                   return_value=register_time):
            self.assertTrue(
                device_tracker.setup(
                    self.hass, {
                        device_tracker.DOMAIN: {
                            CONF_PLATFORM: 'test',
                            device_tracker.CONF_CONSIDER_HOME: 59,
                        }
                    }))

        self.assertEqual(STATE_HOME,
                         self.hass.states.get('device_tracker.dev1').state)

        scanner.leave_home('DEV1')

        with patch('homeassistant.components.device_tracker.dt_util.utcnow',
                   return_value=scan_time):
            fire_time_changed(self.hass, scan_time)
            self.hass.pool.block_till_done()

        self.assertEqual(STATE_NOT_HOME,
                         self.hass.states.get('device_tracker.dev1').state)
示例#5
0
    def test_group_all_devices(self):
        dev_id = 'test_entity'
        entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)
        device = device_tracker.Device(self.hass,
                                       timedelta(seconds=180),
                                       0,
                                       True,
                                       dev_id,
                                       None,
                                       away_hide=True)
        device_tracker.update_config(self.yaml_devices, dev_id, device)

        scanner = get_component('device_tracker.test').SCANNER
        scanner.reset()

        self.assertTrue(
            device_tracker.setup(
                self.hass, {device_tracker.DOMAIN: {
                    CONF_PLATFORM: 'test'
                }}))

        state = self.hass.states.get(device_tracker.ENTITY_ID_ALL_DEVICES)
        self.assertIsNotNone(state)
        self.assertEqual(STATE_NOT_HOME, state.state)
        self.assertSequenceEqual((entity_id, ),
                                 state.attributes.get(ATTR_ENTITY_ID))
示例#6
0
    def test_update_stale(self):
        """Test stalled update."""
        scanner = get_component('device_tracker.test').SCANNER
        scanner.reset()
        scanner.come_home('DEV1')

        register_time = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC)
        scan_time = datetime(2015, 9, 15, 23, 1, tzinfo=dt_util.UTC)

        with patch('homeassistant.components.device_tracker.dt_util.utcnow',
                   return_value=register_time):
            self.assertTrue(device_tracker.setup(self.hass, {
                'device_tracker': {
                    'platform': 'test',
                    'consider_home': 59,
                }}))

        self.assertEqual(STATE_HOME,
                         self.hass.states.get('device_tracker.dev1').state)

        scanner.leave_home('DEV1')

        with patch('homeassistant.components.device_tracker.dt_util.utcnow',
                   return_value=scan_time):
            fire_time_changed(self.hass, scan_time)
            self.hass.pool.block_till_done()

        self.assertEqual(STATE_NOT_HOME,
                         self.hass.states.get('device_tracker.dev1').state)
示例#7
0
    def setUp(self):  # pylint: disable=invalid-name
        self.hass = get_test_home_assistant()

        self.scanner = loader.get_component('device_tracker.test').get_scanner(
            None, None)

        self.scanner.reset()
        self.scanner.come_home('DEV1')

        loader.get_component('light.test').init()

        self.assertTrue(
            device_tracker.setup(
                self.hass, {device_tracker.DOMAIN: {
                    CONF_PLATFORM: 'test'
                }}))

        self.assertTrue(
            light.setup(self.hass, {light.DOMAIN: {
                CONF_PLATFORM: 'test'
            }}))

        self.assertTrue(
            sun.setup(self.hass, {sun.DOMAIN: {
                sun.CONF_ELEVATION: 0
            }}))
示例#8
0
    def test_update_stale(self):
        scanner = get_component('device_tracker.test').SCANNER
        scanner.reset()
        scanner.come_home('DEV1')

        register_time = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC)
        scan_time = datetime(2015, 9, 15, 23, 1, tzinfo=dt_util.UTC)

        with patch('homeassistant.components.device_tracker.dt_util.utcnow',
                   return_value=register_time):
            self.assertTrue(
                device_tracker.setup(self.hass, {
                    'device_tracker': {
                        'platform': 'test',
                        'consider_home': 59,
                    }
                }))

        self.assertEqual(STATE_HOME,
                         self.hass.states.get('device_tracker.dev1').state)

        scanner.leave_home('DEV1')

        with patch('homeassistant.components.device_tracker.dt_util.utcnow',
                   return_value=scan_time):
            fire_time_changed(self.hass, scan_time)
            self.hass.pool.block_till_done()

        self.assertEqual(STATE_NOT_HOME,
                         self.hass.states.get('device_tracker.dev1').state)
 def test_encrypted_payload_wrong_key(self):
     self.assertTrue(
         device_tracker.setup(
             self.hass, {device_tracker.DOMAIN: {CONF_PLATFORM: "owntracks", CONF_SECRET: "wrong key"}}
         )
     )
     self.send_message(LOCATION_TOPIC, MOCK_ENCRYPTED_LOCATION_MESSAGE)
     self.assert_location_latitude(None)
示例#10
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
示例#11
0
    def test_discovery(self):
        scanner = get_component('device_tracker.test').SCANNER

        with patch.dict(device_tracker.DISCOVERY_PLATFORMS, {'test': 'test'}):
            with patch.object(scanner, 'scan_devices') as mock_scan:
                self.assertTrue(device_tracker.setup(self.hass, {
                    device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}}))
                fire_service_discovered(self.hass, 'test', {})
                self.assertTrue(mock_scan.called)
示例#12
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
示例#13
0
    def test_discovery(self):
        """Test discovery."""
        scanner = get_component('device_tracker.test').SCANNER

        with patch.dict(device_tracker.DISCOVERY_PLATFORMS, {'test': 'test'}):
            with patch.object(scanner, 'scan_devices') as mock_scan:
                self.assertTrue(device_tracker.setup(self.hass, TEST_PLATFORM))
                fire_service_discovered(self.hass, 'test', {})
                self.assertTrue(mock_scan.called)
示例#14
0
 def test_encrypted_payload_no_key(self):
     """Test encrypted payload with no key."""
     self.assertTrue(device_tracker.setup(self.hass, {
         device_tracker.DOMAIN: {
             CONF_PLATFORM: 'owntracks',
             # key missing
         }}))
     self.send_message(LOCATION_TOPIC, MOCK_ENCRYPTED_LOCATION_MESSAGE)
     self.assert_location_latitude(None)
示例#15
0
 def test_encrypted_payload(self):
     """Test encrypted payload."""
     self.assertTrue(device_tracker.setup(self.hass, {
         device_tracker.DOMAIN: {
             CONF_PLATFORM: 'owntracks',
             CONF_SECRET: SECRET_KEY,
         }}))
     self.send_message(LOCATION_TOPIC, MOCK_ENCRYPTED_LOCATION_MESSAGE)
     self.assert_location_latitude(2.0)
示例#16
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
示例#17
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
示例#18
0
 def test_encrypted_payload_topic_key(self):
     self.assertTrue(
         device_tracker.setup(
             self.hass,
             {device_tracker.DOMAIN: {CONF_PLATFORM: "owntracks", CONF_SECRET: {LOCATION_TOPIC: SECRET_KEY}}},
         )
     )
     self.send_message(LOCATION_TOPIC, MOCK_ENCRYPTED_LOCATION_MESSAGE)
     self.assert_location_latitude(2.0)
示例#19
0
 def test_encrypted_payload_wrong_topic_key(self):
     """Test encrypted payload with wrong  topic key."""
     self.assertTrue(device_tracker.setup(self.hass, {
         device_tracker.DOMAIN: {
             CONF_PLATFORM: 'owntracks',
             CONF_SECRET: {
                 LOCATION_TOPIC: 'wrong key'
             }}}))
     self.send_message(LOCATION_TOPIC, MOCK_ENCRYPTED_LOCATION_MESSAGE)
     self.assert_location_latitude(None)
示例#20
0
    def test_encrypted_payload_libsodium(self):
        """Test sending encrypted message payload."""
        self.assertTrue(
            device_tracker.setup(
                self.hass, {device_tracker.DOMAIN: {CONF_PLATFORM: "owntracks", CONF_SECRET: SECRET_KEY}}
            )
        )

        self.send_message(LOCATION_TOPIC, ENCRYPTED_LOCATION_MESSAGE)
        self.assert_location_latitude(2.0)
示例#21
0
 def test_encrypted_payload_no_topic_key(self):
     """Test encrypted payload with no topic key."""
     self.assertTrue(device_tracker.setup(self.hass, {
         device_tracker.DOMAIN: {
             CONF_PLATFORM: 'owntracks',
             CONF_SECRET: {
                 'owntracks/{}/{}'.format(USER, 'otherdevice'): 'foobar'
             }}}))
     self.send_message(LOCATION_TOPIC, MOCK_ENCRYPTED_LOCATION_MESSAGE)
     self.assert_location_latitude(None)
示例#22
0
 def test_adding_unknown_device_to_config(self):
     scanner = get_component('device_tracker.test').SCANNER
     scanner.reset()
     scanner.come_home('DEV1')
     self.assertTrue(device_tracker.setup(self.hass, {
         device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}}))
     config = device_tracker.load_config(self.yaml_devices, self.hass,
                                         timedelta(seconds=0))[0]
     self.assertEqual('dev1', config.dev_id)
     self.assertEqual(True, config.track)
    def setUp(self):  # pylint: disable=invalid-name
        self.hass = get_test_home_assistant()

        self.scanner = loader.get_component(
            'device_tracker.test').get_scanner(None, None)

        self.scanner.reset()
        self.scanner.come_home('DEV1')

        loader.get_component('light.test').init()

        device_tracker.setup(self.hass, {
            device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}
        })

        light.setup(self.hass, {
            light.DOMAIN: {CONF_PLATFORM: 'test'}
        })

        sun.setup(self.hass, {})
    def test_setup(self):
        """ Test setup method. """
        # Bogus config
        self.assertFalse(device_tracker.setup(self.hass, {}))

        self.assertFalse(
            device_tracker.setup(self.hass, {device_tracker.DOMAIN: {}}))

        # Test with non-existing component
        self.assertFalse(device_tracker.setup(
            self.hass, {device_tracker.DOMAIN: {CONF_PLATFORM: 'nonexisting'}}
        ))

        # Test with a bad known device file around
        with open(self.known_dev_path, 'w') as fil:
            fil.write("bad data\nbad data\n")

        self.assertFalse(device_tracker.setup(self.hass, {
            device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}
        }))
示例#25
0
    def test_adding_unknown_device_to_config(self):
        scanner = get_component('device_tracker.test').SCANNER
        scanner.reset()
        scanner.come_home('DEV1')

        self.assertTrue(device_tracker.setup(self.hass, {
            device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}}))
        config = device_tracker.load_config(self.yaml_devices, self.hass,
                                            timedelta(seconds=0), 0)
        assert len(config) == 1
        assert config[0].dev_id == 'dev1'
        assert config[0].track
 def test_encrypted_payload_wrong_key(self):
     """Test encrypted payload with wrong key."""
     self.assertTrue(
         device_tracker.setup(
             self.hass, {
                 device_tracker.DOMAIN: {
                     CONF_PLATFORM: 'owntracks',
                     CONF_SECRET: 'wrong key',
                 }
             }))
     self.send_message(LOCATION_TOPIC, MOCK_ENCRYPTED_LOCATION_MESSAGE)
     self.assert_location_latitude(None)
示例#27
0
 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)
示例#28
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
示例#29
0
 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)
示例#30
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
示例#31
0
 def test_adding_unknown_device_to_config(self):
     scanner = get_component('device_tracker.test').SCANNER
     scanner.reset()
     scanner.come_home('DEV1')
     self.assertTrue(
         device_tracker.setup(
             self.hass, {device_tracker.DOMAIN: {
                 CONF_PLATFORM: 'test'
             }}))
     config = device_tracker.load_config(self.yaml_devices, self.hass,
                                         timedelta(seconds=0), 0)[0]
     self.assertEqual('dev1', config.dev_id)
     self.assertEqual(True, config.track)
    def test_encrypted_payload_libsodium(self):
        """Test sending encrypted message payload."""
        self.assertTrue(
            device_tracker.setup(
                self.hass, {
                    device_tracker.DOMAIN: {
                        CONF_PLATFORM: 'owntracks',
                        CONF_SECRET: SECRET_KEY,
                    }
                }))

        self.send_message(LOCATION_TOPIC, ENCRYPTED_LOCATION_MESSAGE)
        self.assert_location_latitude(2.0)
示例#33
0
 def test_encrypted_payload_no_topic_key(self):
     self.assertTrue(
         device_tracker.setup(
             self.hass,
             {
                 device_tracker.DOMAIN: {
                     CONF_PLATFORM: "owntracks",
                     CONF_SECRET: {"owntracks/{}/{}".format(USER, "otherdevice"): "foobar"},
                 }
             },
         )
     )
     self.send_message(LOCATION_TOPIC, MOCK_ENCRYPTED_LOCATION_MESSAGE)
     self.assert_location_latitude(None)
示例#34
0
    def test_adding_unknown_device_to_config(self): \
            # pylint: disable=invalid-name
        """Test the adding of unknown devices to configuration file."""
        scanner = get_component('device_tracker.test').SCANNER
        scanner.reset()
        scanner.come_home('DEV1')

        self.assertTrue(device_tracker.setup(self.hass, {
            device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}}))
        config = device_tracker.load_config(self.yaml_devices, self.hass,
                                            timedelta(seconds=0))
        assert len(config) == 1
        assert config[0].dev_id == 'dev1'
        assert config[0].track
示例#35
0
    def test_new_message(self):
        dev_id = 'paulus'
        enttiy_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)
        topic = '/location/paulus'
        location = 'work'

        self.assertTrue(device_tracker.setup(self.hass, {
            device_tracker.DOMAIN: {
                CONF_PLATFORM: 'mqtt',
                'devices': {dev_id: topic}
            }}))
        fire_mqtt_message(self.hass, topic, location)
        self.hass.pool.block_till_done()
        self.assertEqual(location, self.hass.states.get(enttiy_id).state)
示例#36
0
    def setup_method(self, method):
        """Setup things to be run when tests are started."""
        self.hass = get_test_home_assistant()
        mock_mqtt_component(self.hass)
        self.assertTrue(device_tracker.setup(self.hass, {
            device_tracker.DOMAIN: {
                CONF_PLATFORM: 'owntracks',
                CONF_MAX_GPS_ACCURACY: 200
            }}))

        self.hass.states.set(
            'zone.inner', 'zoning',
            {
                'name': 'zone',
                'latitude': 2.1,
                'longitude': 1.1,
                'radius': 10
            })

        self.hass.states.set(
            'zone.inner_2', 'zoning',
            {
                'name': 'zone',
                'latitude': 2.1,
                'longitude': 1.1,
                'radius': 10
            })

        self.hass.states.set(
            'zone.outer', 'zoning',
            {
                'name': 'zone',
                'latitude': 2.0,
                'longitude': 1.0,
                'radius': 100000
            })

        self.hass.states.set(
            'zone.passive', 'zoning',
            {
                'name': 'zone',
                'latitude': 3.0,
                'longitude': 1.0,
                'radius': 10,
                'passive': True
            })
        # Clear state between teste
        self.hass.states.set(DEVICE_TRACKER_STATE, None)
        owntracks.REGIONS_ENTERED = defaultdict(list)
        owntracks.MOBILE_BEACONS_ACTIVE = defaultdict(list)
示例#37
0
 def test_reading_yaml_config(self):
     dev_id = 'test'
     device = device_tracker.Device(
         self.hass, timedelta(seconds=180), True, dev_id, 'AB:CD:EF:GH:IJ',
         'Test name', 'http://test.picture', True)
     device_tracker.update_config(self.yaml_devices, dev_id, device)
     self.assertTrue(device_tracker.setup(self.hass, {}))
     config = device_tracker.load_config(self.yaml_devices, self.hass,
                                         device.consider_home)[0]
     self.assertEqual(device.dev_id, config.dev_id)
     self.assertEqual(device.track, config.track)
     self.assertEqual(device.mac, config.mac)
     self.assertEqual(device.config_picture, config.config_picture)
     self.assertEqual(device.away_hide, config.away_hide)
     self.assertEqual(device.consider_home, config.consider_home)
 def test_encrypted_payload_no_topic_key(self):
     """Test encrypted payload with no topic key."""
     self.assertTrue(
         device_tracker.setup(
             self.hass, {
                 device_tracker.DOMAIN: {
                     CONF_PLATFORM: 'owntracks',
                     CONF_SECRET: {
                         'owntracks/{}/{}'.format(USER, 'otherdevice'):
                         'foobar'
                     }
                 }
             }))
     self.send_message(LOCATION_TOPIC, MOCK_ENCRYPTED_LOCATION_MESSAGE)
     self.assert_location_latitude(None)
示例#39
0
 def test_reading_yaml_config(self):
     dev_id = 'test'
     device = device_tracker.Device(self.hass, timedelta(seconds=180), True,
                                    dev_id, 'AB:CD:EF:GH:IJ', 'Test name',
                                    'http://test.picture', True)
     device_tracker.update_config(self.yaml_devices, dev_id, device)
     self.assertTrue(device_tracker.setup(self.hass, {}))
     config = device_tracker.load_config(self.yaml_devices, self.hass,
                                         device.consider_home)[0]
     self.assertEqual(device.dev_id, config.dev_id)
     self.assertEqual(device.track, config.track)
     self.assertEqual(device.mac, config.mac)
     self.assertEqual(device.config_picture, config.config_picture)
     self.assertEqual(device.away_hide, config.away_hide)
     self.assertEqual(device.consider_home, config.consider_home)
示例#40
0
    def test_migrating_config(self):
        csv_devices = self.hass.config.path(device_tracker.CSV_DEVICES)

        self.assertFalse(os.path.isfile(csv_devices))
        self.assertFalse(os.path.isfile(self.yaml_devices))

        person1 = {
            'mac': 'AB:CD:EF:GH:IJ:KL',
            'name': 'Paulus',
            'track': True,
            'picture': 'http://placehold.it/200x200',
        }
        person2 = {
            'mac': 'MN:OP:QR:ST:UV:WX:YZ',
            'name': '',
            'track': False,
            'picture': None,
        }

        try:
            with open(csv_devices, 'w') as fil:
                fil.write('device,name,track,picture\n')
                for pers in (person1, person2):
                    fil.write('{},{},{},{}\n'.format(
                        pers['mac'], pers['name'],
                        '1' if pers['track'] else '0', pers['picture'] or ''))

            self.assertTrue(device_tracker.setup(self.hass, {}))
            self.assertFalse(os.path.isfile(csv_devices))
            self.assertTrue(os.path.isfile(self.yaml_devices))

            yaml_config = load_yaml_config_file(self.yaml_devices)

            self.assertEqual(2, len(yaml_config))

            for pers, yaml_pers in zip((person1, person2),
                                       sorted(yaml_config.values(),
                                              key=lambda pers: pers['mac'])):
                for key, value in pers.items():
                    if key == 'name' and value == '':
                        value = DEVICE_DEFAULT_NAME
                    self.assertEqual(value, yaml_pers.get(key))

        finally:
            try:
                os.remove(csv_devices)
            except FileNotFoundError:
                pass
示例#41
0
    def test_migrating_config(self):
        csv_devices = self.hass.config.path(device_tracker.CSV_DEVICES)

        self.assertFalse(os.path.isfile(csv_devices))
        self.assertFalse(os.path.isfile(self.yaml_devices))

        person1 = {
            'mac': 'AB:CD:EF:GH:IJ:KL',
            'name': 'Paulus',
            'track': True,
            'picture': 'http://placehold.it/200x200',
        }
        person2 = {
            'mac': 'MN:OP:QR:ST:UV:WX:YZ',
            'name': '',
            'track': False,
            'picture': None,
        }

        try:
            with open(csv_devices, 'w') as fil:
                fil.write('device,name,track,picture\n')
                for pers in (person1, person2):
                    fil.write('{},{},{},{}\n'.format(
                        pers['mac'], pers['name'],
                        '1' if pers['track'] else '0', pers['picture'] or ''))

            self.assertTrue(device_tracker.setup(self.hass, {}))
            self.assertFalse(os.path.isfile(csv_devices))
            self.assertTrue(os.path.isfile(self.yaml_devices))

            yaml_config = load_yaml_config_file(self.yaml_devices)

            self.assertEqual(2, len(yaml_config))

            for pers, yaml_pers in zip(
                (person1, person2), sorted(yaml_config.values(),
                                           key=lambda pers: pers['mac'])):
                for key, value in pers.items():
                    if key == 'name' and value == '':
                        value = DEVICE_DEFAULT_NAME
                    self.assertEqual(value, yaml_pers.get(key))

        finally:
            try:
                os.remove(csv_devices)
            except FileNotFoundError:
                pass
示例#42
0
    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)
示例#43
0
    def test_adding_unknown_device_to_config(self):
        """Test the adding of unknown devices to configuration file."""
        scanner = get_component('device_tracker.test').SCANNER
        scanner.reset()
        scanner.come_home('DEV1')

        self.assertTrue(
            device_tracker.setup(
                self.hass, {device_tracker.DOMAIN: {
                    CONF_PLATFORM: 'test'
                }}))
        config = device_tracker.load_config(self.yaml_devices, self.hass,
                                            timedelta(seconds=0), 0)
        assert len(config) == 1
        assert config[0].dev_id == 'dev1'
        assert config[0].track
示例#44
0
    def test_device_hidden(self):
        """Test hidden devices."""
        dev_id = 'test_entity'
        entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)
        device = device_tracker.Device(
            self.hass, timedelta(seconds=180), True, dev_id, None,
            away_hide=True)
        device_tracker.update_config(self.yaml_devices, dev_id, device)

        scanner = get_component('device_tracker.test').SCANNER
        scanner.reset()

        self.assertTrue(device_tracker.setup(self.hass, TEST_PLATFORM))

        self.assertTrue(self.hass.states.get(entity_id)
                        .attributes.get(ATTR_HIDDEN))
示例#45
0
    def setup_method(self, method):
        """Setup things to be run when tests are started."""
        self.hass = get_test_home_assistant()
        mock_mqtt_component(self.hass)
        self.assertTrue(
            device_tracker.setup(
                self.hass, {
                    device_tracker.DOMAIN: {
                        CONF_PLATFORM: 'owntracks',
                        CONF_MAX_GPS_ACCURACY: 200
                    }
                }))

        self.hass.states.set('zone.inner', 'zoning', {
            'name': 'zone',
            'latitude': 2.1,
            'longitude': 1.1,
            'radius': 10
        })

        self.hass.states.set('zone.inner_2', 'zoning', {
            'name': 'zone',
            'latitude': 2.1,
            'longitude': 1.1,
            'radius': 10
        })

        self.hass.states.set('zone.outer', 'zoning', {
            'name': 'zone',
            'latitude': 2.0,
            'longitude': 1.0,
            'radius': 100000
        })

        self.hass.states.set(
            'zone.passive', 'zoning', {
                'name': 'zone',
                'latitude': 3.0,
                'longitude': 1.0,
                'radius': 10,
                'passive': True
            })
        # Clear state between teste
        self.hass.states.set(DEVICE_TRACKER_STATE, None)
        owntracks.REGIONS_ENTERED = defaultdict(list)
        owntracks.MOBILE_BEACONS_ACTIVE = defaultdict(list)
示例#46
0
 def test_reading_yaml_config(self):
     """Test the rendering of the YAML configuration."""
     dev_id = 'test'
     device = device_tracker.Device(
         self.hass, timedelta(seconds=180), True, dev_id,
         'AB:CD:EF:GH:IJ', 'Test name', picture='http://test.picture',
         away_hide=True)
     device_tracker.update_config(self.yaml_devices, dev_id, device)
     self.assertTrue(device_tracker.setup(self.hass, TEST_PLATFORM))
     config = device_tracker.load_config(self.yaml_devices, self.hass,
                                         device.consider_home)[0]
     self.assertEqual(device.dev_id, config.dev_id)
     self.assertEqual(device.track, config.track)
     self.assertEqual(device.mac, config.mac)
     self.assertEqual(device.config_picture, config.config_picture)
     self.assertEqual(device.away_hide, config.away_hide)
     self.assertEqual(device.consider_home, config.consider_home)
示例#47
0
    def test_entity_attributes(self):
        dev_id = 'test_entity'
        entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)
        friendly_name = 'Paulus'
        picture = 'http://placehold.it/200x200'

        device = device_tracker.Device(
            self.hass, timedelta(seconds=180), True, dev_id, None,
            friendly_name, picture, away_hide=True)
        device_tracker.update_config(self.yaml_devices, dev_id, device)

        self.assertTrue(device_tracker.setup(self.hass, {}))

        attrs = self.hass.states.get(entity_id).attributes

        self.assertEqual(friendly_name, attrs.get(ATTR_FRIENDLY_NAME))
        self.assertEqual(picture, attrs.get(ATTR_ENTITY_PICTURE))
示例#48
0
    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)
示例#49
0
    def test_device_hidden(self):
        """Test hidden devices."""
        dev_id = 'test_entity'
        entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)
        device = device_tracker.Device(self.hass,
                                       timedelta(seconds=180),
                                       True,
                                       dev_id,
                                       None,
                                       away_hide=True)
        device_tracker.update_config(self.yaml_devices, dev_id, device)

        scanner = get_component('device_tracker.test').SCANNER
        scanner.reset()

        self.assertTrue(device_tracker.setup(self.hass, TEST_PLATFORM))

        self.assertTrue(
            self.hass.states.get(entity_id).attributes.get(ATTR_HIDDEN))
示例#50
0
    def test_group_all_devices(self):
        """Test grouping of devices."""
        dev_id = 'test_entity'
        entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)
        device = device_tracker.Device(
            self.hass, timedelta(seconds=180), True, dev_id, None,
            away_hide=True)
        device_tracker.update_config(self.yaml_devices, dev_id, device)

        scanner = get_component('device_tracker.test').SCANNER
        scanner.reset()

        self.assertTrue(device_tracker.setup(self.hass, TEST_PLATFORM))

        state = self.hass.states.get(device_tracker.ENTITY_ID_ALL_DEVICES)
        self.assertIsNotNone(state)
        self.assertEqual(STATE_NOT_HOME, state.state)
        self.assertSequenceEqual((entity_id,),
                                 state.attributes.get(ATTR_ENTITY_ID))
示例#51
0
    def test_new_message(self):
        dev_id = 'paulus'
        enttiy_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)
        topic = '/location/paulus'
        location = 'work'

        self.assertTrue(
            device_tracker.setup(
                self.hass, {
                    device_tracker.DOMAIN: {
                        CONF_PLATFORM: 'mqtt',
                        'devices': {
                            dev_id: topic
                        }
                    }
                }))
        fire_mqtt_message(self.hass, topic, location)
        self.hass.pool.block_till_done()
        self.assertEqual(location, self.hass.states.get(enttiy_id).state)
示例#52
0
    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)
示例#53
0
    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)
示例#54
0
 def test_reading_yaml_config(self):
     """Test the rendering of the YAML configuration."""
     dev_id = 'test'
     device = device_tracker.Device(self.hass,
                                    timedelta(seconds=180),
                                    True,
                                    dev_id,
                                    'AB:CD:EF:GH:IJ',
                                    'Test name',
                                    picture='http://test.picture',
                                    away_hide=True)
     device_tracker.update_config(self.yaml_devices, dev_id, device)
     self.assertTrue(device_tracker.setup(self.hass, TEST_PLATFORM))
     config = device_tracker.load_config(self.yaml_devices, self.hass,
                                         device.consider_home)[0]
     self.assertEqual(device.dev_id, config.dev_id)
     self.assertEqual(device.track, config.track)
     self.assertEqual(device.mac, config.mac)
     self.assertEqual(device.config_picture, config.config_picture)
     self.assertEqual(device.away_hide, config.away_hide)
     self.assertEqual(device.consider_home, config.consider_home)
示例#55
0
    def setup_method(self, method):
        """ Init needed objects. """
        self.hass = get_test_home_assistant()
        mock_mqtt_component(self.hass)
        self.assertTrue(device_tracker.setup(self.hass, {
            device_tracker.DOMAIN: {
                CONF_PLATFORM: 'owntracks'
            }}))

        self.hass.states.set(
            'zone.inner', 'zoning',
            {
                'name': 'zone',
                'latitude': 2.1,
                'longitude': 1.1,
                'radius': 10
            })

        self.hass.states.set(
            'zone.inner_2', 'zoning',
            {
                'name': 'zone',
                'latitude': 2.1,
                'longitude': 1.1,
                'radius': 10
            })

        self.hass.states.set(
            'zone.outer', 'zoning',
            {
                'name': 'zone',
                'latitude': 2.0,
                'longitude': 1.0,
                'radius': 100000
            })
        # Clear state between teste
        self.hass.states.set(DEVICE_TRACKER_STATE, None)
        owntracks.REGIONS_ENTERED = defaultdict(list)
        owntracks.MOBILE_BEACONS_ACTIVE = defaultdict(list)
示例#56
0
    def test_entity_attributes(self):
        dev_id = 'test_entity'
        entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)
        friendly_name = 'Paulus'
        picture = 'http://placehold.it/200x200'

        device = device_tracker.Device(self.hass,
                                       timedelta(seconds=180),
                                       True,
                                       dev_id,
                                       None,
                                       friendly_name,
                                       picture,
                                       away_hide=True)
        device_tracker.update_config(self.yaml_devices, dev_id, device)

        self.assertTrue(device_tracker.setup(self.hass, {}))

        attrs = self.hass.states.get(entity_id).attributes

        self.assertEqual(friendly_name, attrs.get(ATTR_FRIENDLY_NAME))
        self.assertEqual(picture, attrs.get(ATTR_ENTITY_PICTURE))
    def setUp(self):  # pylint: disable=invalid-name
        """Setup things to be run when tests are started."""
        self.hass = get_test_home_assistant()
        event_decorators.HASS = self.hass

        self.scanner = loader.get_component(
            'device_tracker.test').get_scanner(None, None)

        self.scanner.reset()
        self.scanner.come_home('DEV1')

        loader.get_component('light.test').init()

        self.assertTrue(device_tracker.setup(self.hass, {
            device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}
        }))

        self.assertTrue(light.setup(self.hass, {
            light.DOMAIN: {CONF_PLATFORM: 'test'}
        }))

        self.assertTrue(sun.setup(
            self.hass, {sun.DOMAIN: {sun.CONF_ELEVATION: 0}}))
示例#58
0
 def test_setup_without_yaml_file(self):
     """Test with no YAML file."""
     self.assertTrue(device_tracker.setup(self.hass, TEST_PLATFORM))
示例#59
0
 def test_setup_without_yaml_file(self):
     self.assertTrue(device_tracker.setup(self.hass, {}))
    def test_writing_known_devices_file(self):
        """ Test the device tracker class. """
        scanner = loader.get_component('device_tracker.test').get_scanner(
            None, None)

        scanner.come_home('DEV1')
        scanner.come_home('DEV2')

        self.assertTrue(
            device_tracker.setup(
                self.hass, {device_tracker.DOMAIN: {
                    CONF_PLATFORM: 'test'
                }}))

        # Ensure a new known devices file has been created.
        # Since the device_tracker uses a set internally we cannot
        # know what the order of the devices in the known devices file is.
        # To ensure all the three expected lines are there, we sort the file
        with open(self.known_dev_path) as fil:
            self.assertEqual([
                'DEV1,unknown device,0,\n', 'DEV2,dev2,0,\n',
                'device,name,track,picture\n'
            ], sorted(fil))

        # Write one where we track dev1, dev2
        with open(self.known_dev_path, 'w') as fil:
            fil.write('device,name,track,picture\n')
            fil.write('DEV1,device 1,1,http://example.com/dev1.jpg\n')
            fil.write('DEV2,device 2,1,http://example.com/dev2.jpg\n')

        scanner.leave_home('DEV1')
        scanner.come_home('DEV3')

        self.hass.services.call(device_tracker.DOMAIN,
                                device_tracker.SERVICE_DEVICE_TRACKER_RELOAD)

        self.hass.pool.block_till_done()

        dev1 = device_tracker.ENTITY_ID_FORMAT.format('device_1')
        dev2 = device_tracker.ENTITY_ID_FORMAT.format('device_2')
        dev3 = device_tracker.ENTITY_ID_FORMAT.format('DEV3')

        now = datetime.now()

        # Device scanner scans every 12 seconds. We need to sync our times to
        # be every 12 seconds or else the time_changed event will be ignored.
        nowAlmostMinimumGone = now + device_tracker.TIME_DEVICE_NOT_FOUND
        nowAlmostMinimumGone -= timedelta(seconds=12 +
                                          (nowAlmostMinimumGone.second % 12))

        nowMinimumGone = now + device_tracker.TIME_DEVICE_NOT_FOUND
        nowMinimumGone += timedelta(seconds=12 - (nowMinimumGone.second % 12))

        # Test initial is correct
        self.assertTrue(device_tracker.is_on(self.hass))
        self.assertFalse(device_tracker.is_on(self.hass, dev1))
        self.assertTrue(device_tracker.is_on(self.hass, dev2))
        self.assertIsNone(self.hass.states.get(dev3))

        self.assertEqual(
            'http://example.com/dev1.jpg',
            self.hass.states.get(dev1).attributes.get(ATTR_ENTITY_PICTURE))
        self.assertEqual(
            'http://example.com/dev2.jpg',
            self.hass.states.get(dev2).attributes.get(ATTR_ENTITY_PICTURE))

        # Test if dev3 got added to known dev file
        with open(self.known_dev_path) as fil:
            self.assertEqual('DEV3,dev3,0,\n', list(fil)[-1])

        # Change dev3 to track
        with open(self.known_dev_path, 'w') as fil:
            fil.write("device,name,track,picture\n")
            fil.write('DEV1,Device 1,1,http://example.com/picture.jpg\n')
            fil.write('DEV2,Device 2,1,http://example.com/picture.jpg\n')
            fil.write('DEV3,DEV3,1,\n')

        scanner.come_home('DEV1')
        scanner.leave_home('DEV2')

        # reload dev file
        self.hass.services.call(device_tracker.DOMAIN,
                                device_tracker.SERVICE_DEVICE_TRACKER_RELOAD)

        self.hass.pool.block_till_done()

        # Test what happens if a device comes home and another leaves
        self.assertTrue(device_tracker.is_on(self.hass))
        self.assertTrue(device_tracker.is_on(self.hass, dev1))
        # Dev2 will still be home because of the error margin on time
        self.assertTrue(device_tracker.is_on(self.hass, dev2))
        # dev3 should be tracked now after we reload the known devices
        self.assertTrue(device_tracker.is_on(self.hass, dev3))

        self.assertIsNone(
            self.hass.states.get(dev3).attributes.get(ATTR_ENTITY_PICTURE))

        # Test if device leaves what happens, test the time span
        self.hass.bus.fire(ha.EVENT_TIME_CHANGED,
                           {ha.ATTR_NOW: nowAlmostMinimumGone})

        self.hass.pool.block_till_done()

        self.assertTrue(device_tracker.is_on(self.hass))
        self.assertTrue(device_tracker.is_on(self.hass, dev1))
        # Dev2 will still be home because of the error time
        self.assertTrue(device_tracker.is_on(self.hass, dev2))
        self.assertTrue(device_tracker.is_on(self.hass, dev3))

        # Now test if gone for longer then error margin
        self.hass.bus.fire(ha.EVENT_TIME_CHANGED,
                           {ha.ATTR_NOW: nowMinimumGone})

        self.hass.pool.block_till_done()

        self.assertTrue(device_tracker.is_on(self.hass))
        self.assertTrue(device_tracker.is_on(self.hass, dev1))
        self.assertFalse(device_tracker.is_on(self.hass, dev2))
        self.assertTrue(device_tracker.is_on(self.hass, dev3))