Exemplo n.º 1
0
    def test_active_zone_skips_passive_zones(self):
        assert zone.setup(self.hass, {
            'zone': [
                {
                    'name': 'Passive Zone',
                    'latitude': 32.880600,
                    'longitude': -117.237561,
                    'radius': 250,
                    'passive': True
                },
            ]
        })

        active = zone.active_zone(self.hass, 32.880600, -117.237561)
        assert active is None

        assert zone.setup(self.hass, {
            'zone': [
                {
                    'name': 'Active Zone',
                    'latitude': 32.880800,
                    'longitude': -117.237561,
                    'radius': 500,
                },
            ]
        })

        active = zone.active_zone(self.hass, 32.880700, -117.237561)
        assert 'zone.active_zone' == active.entity_id
Exemplo n.º 2
0
    def test_active_zone_skips_passive_zones(self):
        """Test active and passive zones."""
        assert bootstrap.setup_component(self.hass, zone.DOMAIN, {
            'zone': [
                {
                    'name': 'Passive Zone',
                    'latitude': 32.880600,
                    'longitude': -117.237561,
                    'radius': 250,
                    'passive': True
                },
            ]
        })

        active = zone.active_zone(self.hass, 32.880600, -117.237561)
        assert active is None

        self.hass.config.components.remove('zone')
        assert bootstrap.setup_component(self.hass, zone.DOMAIN, {
            'zone': [
                {
                    'name': 'Active Zone',
                    'latitude': 32.880800,
                    'longitude': -117.237561,
                    'radius': 500,
                },
            ]
        })

        active = zone.active_zone(self.hass, 32.880700, -117.237561)
        assert 'zone.active_zone' == active.entity_id
Exemplo n.º 3
0
    def test_active_zone_skips_passive_zones(self):
        """Test active and passive zones."""
        assert bootstrap.setup_component(
            self.hass, zone.DOMAIN, {
                'zone': [
                    {
                        'name': 'Passive Zone',
                        'latitude': 32.880600,
                        'longitude': -117.237561,
                        'radius': 250,
                        'passive': True
                    },
                ]
            })

        active = zone.active_zone(self.hass, 32.880600, -117.237561)
        assert active is None

        self.hass.config.components.remove('zone')
        assert bootstrap.setup_component(
            self.hass, zone.DOMAIN, {
                'zone': [
                    {
                        'name': 'Active Zone',
                        'latitude': 32.880800,
                        'longitude': -117.237561,
                        'radius': 500,
                    },
                ]
            })

        active = zone.active_zone(self.hass, 32.880700, -117.237561)
        assert 'zone.active_zone' == active.entity_id
Exemplo n.º 4
0
    def determine_interval(self, devicename, latitude, longitude, battery):
        """Calculate new interval."""
        distancefromhome = None
        zone_state = self.hass.states.get('zone.home')
        zone_state_lat = zone_state.attributes['latitude']
        zone_state_long = zone_state.attributes['longitude']
        distancefromhome = distance(
            latitude, longitude, zone_state_lat, zone_state_long)
        distancefromhome = round(distancefromhome / 1000, 1)

        currentzone = active_zone(self.hass, latitude, longitude)

        if ((currentzone is not None and
             currentzone == self._overridestates.get(devicename)) or
                (currentzone is None and
                 self._overridestates.get(devicename) == 'away')):
            return

        self._overridestates[devicename] = None

        if currentzone is not None:
            self._intervals[devicename] = 30
            return

        if distancefromhome is None:
            return
        if distancefromhome > 25:
            self._intervals[devicename] = round(distancefromhome / 2, 0)
        elif distancefromhome > 10:
            self._intervals[devicename] = 5
        else:
            self._intervals[devicename] = 1
        if battery is not None and battery <= 33 and distancefromhome > 3:
            self._intervals[devicename] = self._intervals[devicename] * 2
Exemplo n.º 5
0
    def determine_interval(self, devicename, latitude, longitude, battery):
        """Calculate new interval."""
        distancefromhome = None
        zone_state = self.hass.states.get('zone.home')
        zone_state_lat = zone_state.attributes['latitude']
        zone_state_long = zone_state.attributes['longitude']
        distancefromhome = distance(latitude, longitude, zone_state_lat,
                                    zone_state_long)
        distancefromhome = round(distancefromhome / 1000, 1)

        currentzone = active_zone(self.hass, latitude, longitude)

        if ((currentzone is not None
             and currentzone == self._overridestates.get(devicename))
                or (currentzone is None
                    and self._overridestates.get(devicename) == 'away')):
            return

        self._overridestates[devicename] = None

        if currentzone is not None:
            self._intervals[devicename] = 30
            return

        if distancefromhome is None:
            return
        if distancefromhome > 25:
            self._intervals[devicename] = round(distancefromhome / 2, 0)
        elif distancefromhome > 10:
            self._intervals[devicename] = 5
        else:
            self._intervals[devicename] = 1
        if battery is not None and battery <= 33 and distancefromhome > 3:
            self._intervals[devicename] = self._intervals[devicename] * 2
Exemplo n.º 6
0
    def test_active_zone_prefers_smaller_zone_if_same_distance(self):
        """Test zone size preferences."""
        latitude = 32.880600
        longitude = -117.237561
        assert bootstrap.setup_component(
            self.hass, zone.DOMAIN, {
                'zone': [
                    {
                        'name': 'Small Zone',
                        'latitude': latitude,
                        'longitude': longitude,
                        'radius': 250,
                    },
                    {
                        'name': 'Big Zone',
                        'latitude': latitude,
                        'longitude': longitude,
                        'radius': 500,
                    },
                ]
            })

        active = zone.active_zone(self.hass, latitude, longitude)
        assert 'zone.small_zone' == active.entity_id

        self.hass.config.components.remove('zone')
        assert bootstrap.setup_component(
            self.hass, zone.DOMAIN, {
                'zone': [
                    {
                        'name': 'Smallest Zone',
                        'latitude': latitude,
                        'longitude': longitude,
                        'radius': 50,
                    },
                ]
            })

        active = zone.active_zone(self.hass, latitude, longitude)
        assert 'zone.smallest_zone' == active.entity_id
Exemplo n.º 7
0
    def determine_interval(self, devicename, latitude, longitude, battery):
        """Calculate new interval."""
        currentzone = active_zone(self.hass, latitude, longitude)

        if ((currentzone is not None
             and currentzone == self._overridestates.get(devicename))
                or (currentzone is None
                    and self._overridestates.get(devicename) == 'away')):
            return

        zones = (self.hass.states.get(entity_id)
                 for entity_id in sorted(self.hass.states.entity_ids('zone')))

        distances = []
        for zone_state in zones:
            zone_state_lat = zone_state.attributes['latitude']
            zone_state_long = zone_state.attributes['longitude']
            zone_distance = distance(latitude, longitude, zone_state_lat,
                                     zone_state_long)
            distances.append(round(zone_distance / 1000, 1))

        if distances:
            mindistance = min(distances)
        else:
            mindistance = None

        self._overridestates[devicename] = None

        if currentzone is not None:
            self._intervals[devicename] = 30
            return

        if mindistance is None:
            return

        # Calculate out how long it would take for the device to drive to the
        # nearest zone at 120 km/h:
        interval = round(mindistance / 2, 0)

        # Never poll more than once per minute
        interval = max(interval, 1)

        if interval > 180:
            # Three hour drive?  This is far enough that they might be flying
            # home - check every half hour
            interval = 30

        if battery is not None and battery <= 33 and mindistance > 3:
            # Low battery - let's check half as often
            interval = interval * 2

        self._intervals[devicename] = interval
Exemplo n.º 8
0
    def determine_interval(self, devicename, latitude, longitude, battery):
        """Calculate new interval."""
        currentzone = active_zone(self.hass, latitude, longitude)

        if ((currentzone is not None and
             currentzone == self._overridestates.get(devicename)) or
                (currentzone is None and
                 self._overridestates.get(devicename) == 'away')):
            return

        zones = (self.hass.states.get(entity_id) for entity_id
                 in sorted(self.hass.states.entity_ids('zone')))

        distances = []
        for zone_state in zones:
            zone_state_lat = zone_state.attributes['latitude']
            zone_state_long = zone_state.attributes['longitude']
            zone_distance = distance(
                latitude, longitude, zone_state_lat, zone_state_long)
            distances.append(round(zone_distance / 1000, 1))

        if distances:
            mindistance = min(distances)
        else:
            mindistance = None

        self._overridestates[devicename] = None

        if currentzone is not None:
            self._intervals[devicename] = 30
            return

        if mindistance is None:
            return

        # Calculate out how long it would take for the device to drive to the
        # nearest zone at 120 km/h:
        interval = round(mindistance / 2, 0)

        # Never poll more than once per minute
        interval = max(interval, 1)

        if interval > 180:
            # Three hour drive?  This is far enough that they might be flying
            # home - check every half hour
            interval = 30

        if battery is not None and battery <= 33 and mindistance > 3:
            # Low battery - let's check half as often
            interval = interval * 2

        self._intervals[devicename] = interval
Exemplo n.º 9
0
    def test_active_zone_prefers_smaller_zone_if_same_distance(self):
        """Test zone size preferences."""
        latitude = 32.880600
        longitude = -117.237561
        assert zone.setup(
            self.hass, {
                'zone': [
                    {
                        'name': 'Small Zone',
                        'latitude': latitude,
                        'longitude': longitude,
                        'radius': 250,
                    },
                    {
                        'name': 'Big Zone',
                        'latitude': latitude,
                        'longitude': longitude,
                        'radius': 500,
                    },
                ]
            })

        active = zone.active_zone(self.hass, latitude, longitude)
        assert 'zone.small_zone' == active.entity_id

        assert zone.setup(
            self.hass, {
                'zone': [
                    {
                        'name': 'Smallest Zone',
                        'latitude': latitude,
                        'longitude': longitude,
                        'radius': 50,
                    },
                ]
            })

        active = zone.active_zone(self.hass, latitude, longitude)
        assert 'zone.smallest_zone' == active.entity_id
Exemplo n.º 10
0
    def test_active_zone_prefers_smaller_zone_if_same_distance(self):
        """Test zone size preferences."""
        latitude = 32.880600
        longitude = -117.237561
        assert bootstrap.setup_component(self.hass, zone.DOMAIN, {
            'zone': [
                {
                    'name': 'Small Zone',
                    'latitude': latitude,
                    'longitude': longitude,
                    'radius': 250,
                },
                {
                    'name': 'Big Zone',
                    'latitude': latitude,
                    'longitude': longitude,
                    'radius': 500,
                },
            ]
        })

        active = zone.active_zone(self.hass, latitude, longitude)
        assert 'zone.small_zone' == active.entity_id

        self.hass.config.components.remove('zone')
        assert bootstrap.setup_component(self.hass, zone.DOMAIN, {
            'zone': [
                {
                    'name': 'Smallest Zone',
                    'latitude': latitude,
                    'longitude': longitude,
                    'radius': 50,
                },
            ]
        })

        active = zone.active_zone(self.hass, latitude, longitude)
        assert 'zone.smallest_zone' == active.entity_id
Exemplo n.º 11
0
    def test_active_zone_prefers_smaller_zone_if_same_distance(self):
        """Test zone size preferences."""
        latitude = 32.880600
        longitude = -117.237561
        assert zone.setup(self.hass, {
            'zone': [
                {
                    'name': 'Small Zone',
                    'latitude': latitude,
                    'longitude': longitude,
                    'radius': 250,
                },
                {
                    'name': 'Big Zone',
                    'latitude': latitude,
                    'longitude': longitude,
                    'radius': 500,
                },
            ]
        })

        active = zone.active_zone(self.hass, latitude, longitude)
        assert 'zone.small_zone' == active.entity_id

        assert zone.setup(self.hass, {
            'zone': [
                {
                    'name': 'Smallest Zone',
                    'latitude': latitude,
                    'longitude': longitude,
                    'radius': 50,
                },
            ]
        })

        active = zone.active_zone(self.hass, latitude, longitude)
        assert 'zone.smallest_zone' == active.entity_id
Exemplo n.º 12
0
 def test_active_zone_skips_passive_zones_2(self):
     """Test active and passive zones."""
     assert setup.setup_component(self.hass, zone.DOMAIN, {
         'zone': [
             {
                 'name': 'Active Zone',
                 'latitude': 32.880800,
                 'longitude': -117.237561,
                 'radius': 500,
             },
         ]
     })
     self.hass.block_till_done()
     active = zone.active_zone(self.hass, 32.880700, -117.237561)
     assert 'zone.active_zone' == active.entity_id
Exemplo n.º 13
0
 def test_active_zone_skips_passive_zones(self):
     """Test active and passive zones."""
     assert setup.setup_component(self.hass, zone.DOMAIN, {
         'zone': [
             {
                 'name': 'Passive Zone',
                 'latitude': 32.880600,
                 'longitude': -117.237561,
                 'radius': 250,
                 'passive': True
             },
         ]
     })
     self.hass.block_till_done()
     active = zone.active_zone(self.hass, 32.880600, -117.237561)
     assert active is None
Exemplo n.º 14
0
 def setinterval(self, interval=None, devicename=None):
     """Set the interval of the given devices."""
     devs = [devicename] if devicename else self.devices
     for device in devs:
         devid = DOMAIN + '.' + device
         devicestate = self.hass.states.get(devid)
         if interval is not None:
             if devicestate is not None:
                 self._overridestates[device] = active_zone(
                     self.hass,
                     float(devicestate.attributes.get('latitude', 0)),
                     float(devicestate.attributes.get('longitude', 0)))
                 if self._overridestates[device] is None:
                     self._overridestates[device] = 'away'
             self._intervals[device] = interval
         else:
             self._overridestates[device] = None
         self.update_device(device)
Exemplo n.º 15
0
 def setinterval(self, interval=None, devicename=None):
     """Set the interval of the given devices."""
     devs = [devicename] if devicename else self.devices
     for device in devs:
         devid = '{}.{}'.format(DOMAIN, device)
         devicestate = self.hass.states.get(devid)
         if interval is not None:
             if devicestate is not None:
                 self._overridestates[device] = active_zone(
                     self.hass,
                     float(devicestate.attributes.get('latitude', 0)),
                     float(devicestate.attributes.get('longitude', 0)))
                 if self._overridestates[device] is None:
                     self._overridestates[device] = 'away'
             self._intervals[device] = interval
         else:
             self._overridestates[device] = None
         self.update_device(device)
Exemplo n.º 16
0
 def setinterval(self, interval=None, devicename=None):
     """Set the interval of the given devices."""
     devs = [devicename] if devicename else self.devices
     for device in devs:
         devid = DOMAIN + "." + device
         devicestate = self.hass.states.get(devid)
         if interval is not None:
             if devicestate is not None:
                 self._overridestates[device] = active_zone(
                     self.hass,
                     float(devicestate.attributes.get("latitude", 0)),
                     float(devicestate.attributes.get("longitude", 0)),
                 )
                 if self._overridestates[device] is None:
                     self._overridestates[device] = "away"
             self._intervals[device] = interval
         else:
             self._overridestates[device] = None
         self.update_device(device)
Exemplo n.º 17
0
    def update(self):
        """Update state of entity."""
        if not self.last_seen:
            return
        elif self.location_name:
            self._state = self.location_name
        elif self.gps is not None:
            zone_state = zone.active_zone(self.hass, self.gps[0], self.gps[1], self.gps_accuracy)
            if zone_state is None:
                self._state = STATE_NOT_HOME
            elif zone_state.entity_id == zone.ENTITY_ID_HOME:
                self._state = STATE_HOME
            else:
                self._state = zone_state.name

        elif self.stale():
            self._state = STATE_NOT_HOME
            self.last_update_home = False
        else:
            self._state = STATE_HOME
            self.last_update_home = True
Exemplo n.º 18
0
    def update(self):
        """Update state of entity."""
        if not self.last_seen:
            return
        elif self.location_name:
            self._state = self.location_name
        elif self.gps is not None:
            zone_state = zone.active_zone(self.hass, self.gps[0], self.gps[1],
                                          self.gps_accuracy)
            if zone_state is None:
                self._state = STATE_NOT_HOME
            elif zone_state.entity_id == zone.ENTITY_ID_HOME:
                self._state = STATE_HOME
            else:
                self._state = zone_state.name

        elif self.stale():
            self._state = STATE_NOT_HOME
            self.last_update_home = False
        else:
            self._state = STATE_HOME
            self.last_update_home = True