2018-01-07 21:59:32 +00:00
|
|
|
"""The tests for the Dark Sky weather component."""
|
|
|
|
import re
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
import forecastio
|
2019-10-05 02:28:55 +00:00
|
|
|
from requests.exceptions import ConnectionError
|
2019-12-09 10:22:04 +00:00
|
|
|
import requests_mock
|
2019-10-05 02:28:55 +00:00
|
|
|
|
2018-01-07 21:59:32 +00:00
|
|
|
from homeassistant.components import weather
|
|
|
|
from homeassistant.setup import setup_component
|
2019-12-09 10:22:04 +00:00
|
|
|
from homeassistant.util.unit_system import METRIC_SYSTEM
|
2018-01-07 21:59:32 +00:00
|
|
|
|
2020-05-03 18:27:19 +00:00
|
|
|
from tests.async_mock import patch
|
2019-12-09 10:22:04 +00:00
|
|
|
from tests.common import get_test_home_assistant, load_fixture
|
2018-01-07 21:59:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestDarkSky(unittest.TestCase):
|
|
|
|
"""Test the Dark Sky weather component."""
|
|
|
|
|
|
|
|
def setUp(self):
|
2018-08-19 20:29:08 +00:00
|
|
|
"""Set up things to be run when tests are started."""
|
2018-01-07 21:59:32 +00:00
|
|
|
self.hass = get_test_home_assistant()
|
|
|
|
self.hass.config.units = METRIC_SYSTEM
|
|
|
|
self.lat = self.hass.config.latitude = 37.8267
|
|
|
|
self.lon = self.hass.config.longitude = -122.423
|
2020-06-08 19:26:40 +00:00
|
|
|
self.addCleanup(self.tear_down_cleanup)
|
2018-01-07 21:59:32 +00:00
|
|
|
|
2020-06-08 19:26:40 +00:00
|
|
|
def tear_down_cleanup(self):
|
2018-01-07 21:59:32 +00:00
|
|
|
"""Stop down everything that was started."""
|
|
|
|
self.hass.stop()
|
|
|
|
|
|
|
|
@requests_mock.Mocker()
|
2019-07-31 19:25:30 +00:00
|
|
|
@patch("forecastio.api.get_forecast", wraps=forecastio.api.get_forecast)
|
2018-01-07 21:59:32 +00:00
|
|
|
def test_setup(self, mock_req, mock_get_forecast):
|
|
|
|
"""Test for successfully setting up the forecast.io platform."""
|
2019-07-31 19:25:30 +00:00
|
|
|
uri = (
|
|
|
|
r"https://api.(darksky.net|forecast.io)\/forecast\/(\w+)\/"
|
|
|
|
r"(-?\d+\.?\d*),(-?\d+\.?\d*)"
|
|
|
|
)
|
|
|
|
mock_req.get(re.compile(uri), text=load_fixture("darksky.json"))
|
|
|
|
|
|
|
|
assert setup_component(
|
|
|
|
self.hass,
|
|
|
|
weather.DOMAIN,
|
|
|
|
{"weather": {"name": "test", "platform": "darksky", "api_key": "foo"}},
|
|
|
|
)
|
2020-06-01 05:18:30 +00:00
|
|
|
self.hass.block_till_done()
|
2018-01-07 21:59:32 +00:00
|
|
|
|
2018-10-24 10:10:05 +00:00
|
|
|
assert mock_get_forecast.called
|
|
|
|
assert mock_get_forecast.call_count == 1
|
2018-01-07 21:59:32 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("weather.test")
|
|
|
|
assert state.state == "sunny"
|
2019-10-05 02:28:55 +00:00
|
|
|
|
|
|
|
@patch("forecastio.load_forecast", side_effect=ConnectionError())
|
|
|
|
def test_failed_setup(self, mock_load_forecast):
|
|
|
|
"""Test to ensure that a network error does not break component state."""
|
|
|
|
|
|
|
|
assert setup_component(
|
|
|
|
self.hass,
|
|
|
|
weather.DOMAIN,
|
|
|
|
{"weather": {"name": "test", "platform": "darksky", "api_key": "foo"}},
|
|
|
|
)
|
2020-06-01 05:18:30 +00:00
|
|
|
self.hass.block_till_done()
|
2019-10-05 02:28:55 +00:00
|
|
|
|
|
|
|
state = self.hass.states.get("weather.test")
|
|
|
|
assert state.state == "unavailable"
|