Ejemplo n.º 1
0
    def closest(self, *args):
        """Find closest entity.

        Closest to home:
          closest(states)
          closest(states.device_tracker)
          closest('group.children')
          closest(states.group.children)

        Closest to a point:
          closest(23.456, 23.456, 'group.children')
          closest('zone.school', 'group.children')
          closest(states.zone.school, 'group.children')
        """
        if len(args) == 1:
            latitude = self._hass.config.latitude
            longitude = self._hass.config.longitude
            entities = args[0]

        elif len(args) == 2:
            point_state = self._resolve_state(args[0])

            if point_state is None:
                _LOGGER.warning('Closest:Unable to find state %s', args[0])
                return None
            elif not loc_helper.has_location(point_state):
                _LOGGER.warning(
                    'Closest:State does not contain valid location: %s',
                    point_state)
                return None

            latitude = point_state.attributes.get(ATTR_LATITUDE)
            longitude = point_state.attributes.get(ATTR_LONGITUDE)

            entities = args[1]

        else:
            latitude = convert(args[0], float)
            longitude = convert(args[1], float)

            if latitude is None or longitude is None:
                _LOGGER.warning(
                    'Closest:Received invalid coordinates: %s, %s',
                    args[0], args[1])
                return None

            entities = args[2]

        if isinstance(entities, (AllStates, DomainStates)):
            states = list(entities)
        else:
            if isinstance(entities, State):
                gr_entity_id = entities.entity_id
            else:
                gr_entity_id = str(entities)

            states = [self._hass.states.get(entity_id) for entity_id
                      in group.expand_entity_ids(self._hass, [gr_entity_id])]

        return loc_helper.closest(latitude, longitude, states)
Ejemplo n.º 2
0
def closest(hass, *args):
    """Find closest entity.

    Closest to home:
        closest(states)
        closest(states.device_tracker)
        closest('group.children')
        closest(states.group.children)

    Closest to a point:
        closest(23.456, 23.456, 'group.children')
        closest('zone.school', 'group.children')
        closest(states.zone.school, 'group.children')

    As a filter:
        states | closest
        states.device_tracker | closest
        ['group.children', states.device_tracker] | closest
        'group.children' | closest(23.456, 23.456)
        states.device_tracker | closest('zone.school')
        'group.children' | closest(states.zone.school)

    """
    if len(args) == 1:
        latitude = hass.config.latitude
        longitude = hass.config.longitude
        entities = args[0]

    elif len(args) == 2:
        point_state = _resolve_state(hass, args[0])

        if point_state is None:
            _LOGGER.warning("Closest:Unable to find state %s", args[0])
            return None
        if not loc_helper.has_location(point_state):
            _LOGGER.warning(
                "Closest:State does not contain valid location: %s",
                point_state)
            return None

        latitude = point_state.attributes.get(ATTR_LATITUDE)
        longitude = point_state.attributes.get(ATTR_LONGITUDE)

        entities = args[1]

    else:
        latitude = convert(args[0], float)
        longitude = convert(args[1], float)

        if latitude is None or longitude is None:
            _LOGGER.warning("Closest:Received invalid coordinates: %s, %s",
                            args[0], args[1])
            return None

        entities = args[2]

    states = expand(hass, entities)

    # state will already be wrapped here
    return loc_helper.closest(latitude, longitude, states)
Ejemplo n.º 3
0
def test_closest_with_no_states_with_location():
    """Set up the tests."""
    state = State("light.test", "on")
    state2 = State(
        "light.test", "on", {ATTR_LATITUDE: "invalid", ATTR_LONGITUDE: 123.45}
    )
    state3 = State("light.test", "on", {ATTR_LONGITUDE: 123.45})

    assert location.closest(123.45, 123.45, [state, state2, state3]) is None
Ejemplo n.º 4
0
    def test_closest_returns_closest(self):
        state = State('light.test', 'on', {
            ATTR_LATITUDE: 124.45,
            ATTR_LONGITUDE: 124.45,
        })
        state2 = State('light.test', 'on', {
            ATTR_LATITUDE: 125.45,
            ATTR_LONGITUDE: 125.45,
        })

        self.assertEqual(
            state, location.closest(123.45, 123.45, [state, state2]))
Ejemplo n.º 5
0
    def test_closest_with_no_states_with_location(self):
        state = State('light.test', 'on')
        state2 = State('light.test', 'on', {
            ATTR_LATITUDE: 'invalid',
            ATTR_LONGITUDE: 123.45,
        })
        state3 = State('light.test', 'on', {
            ATTR_LONGITUDE: 123.45,
        })

        self.assertIsNone(
            location.closest(123.45, 123.45, [state, state2, state3]))
Ejemplo n.º 6
0
    def test_closest_returns_closest(self):
        """Test ."""
        state = State('light.test', 'on', {
            ATTR_LATITUDE: 124.45,
            ATTR_LONGITUDE: 124.45,
        })
        state2 = State('light.test', 'on', {
            ATTR_LATITUDE: 125.45,
            ATTR_LONGITUDE: 125.45,
        })

        assert state == location.closest(123.45, 123.45, [state, state2])
Ejemplo n.º 7
0
def test_closest_returns_closest():
    """Test ."""
    state = State("light.test", "on", {
        ATTR_LATITUDE: 124.45,
        ATTR_LONGITUDE: 124.45
    })
    state2 = State("light.test", "on", {
        ATTR_LATITUDE: 125.45,
        ATTR_LONGITUDE: 125.45
    })

    assert state == location.closest(123.45, 123.45, [state, state2])
Ejemplo n.º 8
0
    def test_closest_returns_closest(self):
        state = State('light.test', 'on', {
            ATTR_LATITUDE: 124.45,
            ATTR_LONGITUDE: 124.45,
        })
        state2 = State('light.test', 'on', {
            ATTR_LATITUDE: 125.45,
            ATTR_LONGITUDE: 125.45,
        })

        self.assertEqual(state,
                         location.closest(123.45, 123.45, [state, state2]))
Ejemplo n.º 9
0
    def test_closest_with_no_states_with_location(self):
        state = State('light.test', 'on')
        state2 = State('light.test', 'on', {
            ATTR_LATITUDE: 'invalid',
            ATTR_LONGITUDE: 123.45,
        })
        state3 = State('light.test', 'on', {
            ATTR_LONGITUDE: 123.45,
        })

        self.assertIsNone(
            location.closest(123.45, 123.45, [state, state2, state3]))
Ejemplo n.º 10
0
    def test_closest_returns_closest(self):
        """Test ."""
        state = State('light.test', 'on', {
            ATTR_LATITUDE: 124.45,
            ATTR_LONGITUDE: 124.45,
        })
        state2 = State('light.test', 'on', {
            ATTR_LATITUDE: 125.45,
            ATTR_LONGITUDE: 125.45,
        })

        assert state == location.closest(123.45, 123.45, [state, state2])
Ejemplo n.º 11
0
    def test_closest_with_no_states_with_location(self):
        """Set up the tests."""
        state = State('light.test', 'on')
        state2 = State('light.test', 'on', {
            ATTR_LATITUDE: 'invalid',
            ATTR_LONGITUDE: 123.45,
        })
        state3 = State('light.test', 'on', {
            ATTR_LONGITUDE: 123.45,
        })

        assert \
            location.closest(123.45, 123.45, [state, state2, state3]) is None
Ejemplo n.º 12
0
def test_closest_with_no_states_with_location():
    """Set up the tests."""
    state = State('light.test', 'on')
    state2 = State('light.test', 'on', {
        ATTR_LATITUDE: 'invalid',
        ATTR_LONGITUDE: 123.45,
    })
    state3 = State('light.test', 'on', {
        ATTR_LONGITUDE: 123.45,
    })

    assert \
        location.closest(123.45, 123.45, [state, state2, state3]) is None
Ejemplo n.º 13
0
    def closest(self, *args):
        """Find closest entity.

        Closest to home:
          closest(states)
          closest(states.device_tracker)
          closest('group.children')
          closest(states.group.children)

        Closest to a point:
          closest(23.456, 23.456, 'group.children')
          closest('zone.school', 'group.children')
          closest(states.zone.school, 'group.children')
        """
        if len(args) == 1:
            latitude = self._hass.config.latitude
            longitude = self._hass.config.longitude
            entities = args[0]

        elif len(args) == 2:
            point_state = self._resolve_state(args[0])

            if point_state is None:
                _LOGGER.warning('Closest:Unable to find state %s', args[0])
                return None
            elif not loc_helper.has_location(point_state):
                _LOGGER.warning(
                    'Closest:State does not contain valid location: %s',
                    point_state)
                return None

            latitude = point_state.attributes.get(ATTR_LATITUDE)
            longitude = point_state.attributes.get(ATTR_LONGITUDE)

            entities = args[1]

        else:
            latitude = convert(args[0], float)
            longitude = convert(args[1], float)

            if latitude is None or longitude is None:
                _LOGGER.warning('Closest:Received invalid coordinates: %s, %s',
                                args[0], args[1])
                return None

            entities = args[2]

        if isinstance(entities, (AllStates, DomainStates)):
            states = list(entities)
        else:
            if isinstance(entities, State):
                gr_entity_id = entities.entity_id
            else:
                gr_entity_id = str(entities)

            group = get_component('group')

            states = [
                self._hass.states.get(entity_id)
                for entity_id in group.expand_entity_ids(
                    self._hass, [gr_entity_id])
            ]

        return loc_helper.closest(latitude, longitude, states)