コード例 #1
0
async def test_active_zone_prefers_smaller_zone_if_same_distance(opp):
    """Test zone size preferences."""
    latitude = 32.880600
    longitude = -117.237561
    assert await setup.async_setup_component(
        opp,
        zone.DOMAIN,
        {
            "zone": [
                {
                    "name": "Small Zone",
                    "latitude": latitude,
                    "longitude": longitude,
                    "radius": 250,
                },
                {
                    "name": "Big Zone",
                    "latitude": latitude,
                    "longitude": longitude,
                    "radius": 500,
                },
            ]
        },
    )

    active = zone.async_active_zone(opp, latitude, longitude)
    assert active.entity_id == "zone.small_zone"
コード例 #2
0
async def test_unavailable_zone(opp):
    """Test active zone with unavailable zones."""
    assert await setup.async_setup_component(opp, DOMAIN, {"zone": {}})
    opp.states.async_set("zone.bla", "unavailable", {"restored": True})

    assert zone.async_active_zone(opp, 0.0, 0.01) is None

    assert zone.in_zone(opp.states.get("zone.bla"), 0, 0) is False
コード例 #3
0
async def test_active_zone_skips_passive_zones_2(opp):
    """Test active and passive zones."""
    assert await setup.async_setup_component(
        opp,
        zone.DOMAIN,
        {
            "zone": [{
                "name": "Active Zone",
                "latitude": 32.880800,
                "longitude": -117.237561,
                "radius": 500,
            }]
        },
    )
    await opp.async_block_till_done()
    active = zone.async_active_zone(opp, 32.880700, -117.237561)
    assert active.entity_id == "zone.active_zone"
コード例 #4
0
async def test_active_zone_skips_passive_zones(opp):
    """Test active and passive zones."""
    assert await setup.async_setup_component(
        opp,
        zone.DOMAIN,
        {
            "zone": [{
                "name": "Passive Zone",
                "latitude": 32.880600,
                "longitude": -117.237561,
                "radius": 250,
                "passive": True,
            }]
        },
    )
    await opp.async_block_till_done()
    active = zone.async_active_zone(opp, 32.880600, -117.237561)
    assert active is None
コード例 #5
0
    def state(self) -> str | None:
        """Return the state of the device."""
        if self.location_name is not None:
            return self.location_name

        if self.latitude is not None and self.longitude is not None:
            zone_state = zone.async_active_zone(
                self.opp, self.latitude, self.longitude, self.location_accuracy
            )
            if zone_state is None:
                state = STATE_NOT_HOME
            elif zone_state.entity_id == zone.ENTITY_ID_HOME:
                state = STATE_HOME
            else:
                state = zone_state.name
            return state

        return None
コード例 #6
0
    async def async_update(self):
        """Update state of entity.

        This method is a coroutine.
        """
        if not self.last_seen:
            return
        if self.location_name:
            self._state = self.location_name
        elif self.gps is not None and self.source_type == SOURCE_TYPE_GPS:
            zone_state = zone.async_active_zone(self.opp, 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.mark_stale()
        else:
            self._state = STATE_HOME
            self.last_update_home = True