๐Ÿ“ฆ eifinger / open_route_service

๐Ÿ“„ test_sensor.py ยท 77 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77"""Tests for open_route_service."""
import os
import json
from unittest.mock import patch

from homeassistant.const import EVENT_HOMEASSISTANT_START
from homeassistant.setup import async_setup_component


def load_fixture(filename):
    """Load a fixture."""
    path = os.path.join(os.path.dirname(__file__), "fixtures", filename)
    with open(path, encoding="utf-8") as fptr:
        return fptr.read()


async def test_sensor(hass):
    """Test that sensor works."""
    with patch(
        "openrouteservice.Client.directions",
        return_value=json.loads(load_fixture("directions_response.json")),
    ):
        with patch(
            "openrouteservice.Client.pelias_reverse",
            return_value=json.loads(load_fixture("reverse_geocode_response.json")),
        ):
            config = {
                "sensor": {
                    "platform": "open_route_service",
                    "origin_latitude": "51.222975",
                    "origin_longitude": "9.267577",
                    "destination_latitude": "51.257430",
                    "destination_longitude": "9.335892",
                    "api_key": "test",
                }
            }
            assert await async_setup_component(hass, "sensor", config)
            await hass.async_block_till_done()

            hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
            await hass.async_block_till_done()

            sensor = hass.states.get("sensor.openroute_service_travel_time")
            assert sensor.state == "4"


async def test_sensor_origin_destination_are_same(hass):
    """Test that sensor works when origin and destionation are the same."""
    with patch(
        "openrouteservice.Client.directions",
        return_value=json.loads(
            load_fixture("directions_response_origin_destination_are_same.json")
        ),
    ):
        with patch(
            "openrouteservice.Client.pelias_reverse",
            return_value=json.loads(load_fixture("reverse_geocode_response.json")),
        ):
            config = {
                "sensor": {
                    "platform": "open_route_service",
                    "origin_latitude": "51.222975",
                    "origin_longitude": "9.267577",
                    "destination_latitude": "51.257430",
                    "destination_longitude": "9.335892",
                    "api_key": "test",
                }
            }
            assert await async_setup_component(hass, "sensor", config)
            await hass.async_block_till_done()

            hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
            await hass.async_block_till_done()

            sensor = hass.states.get("sensor.openroute_service_travel_time")
            assert sensor.state == "0"