2018-01-07 21:59:32 +00:00
|
|
|
"""The tests for the Dark Sky weather component."""
|
|
|
|
import re
|
|
|
|
import unittest
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
import forecastio
|
|
|
|
import requests_mock
|
|
|
|
|
|
|
|
from homeassistant.components import weather
|
|
|
|
from homeassistant.util.unit_system import METRIC_SYSTEM
|
|
|
|
from homeassistant.setup import setup_component
|
|
|
|
|
|
|
|
from tests.common import load_fixture, get_test_home_assistant
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""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"}},
|
|
|
|
)
|
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"
|