2018-05-08 17:35:55 +00:00
|
|
|
"""The tests for the BOM Weather sensor platform."""
|
2018-05-19 15:14:53 +00:00
|
|
|
import json
|
2018-05-08 17:35:55 +00:00
|
|
|
import re
|
|
|
|
import unittest
|
|
|
|
from unittest.mock import patch
|
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
2018-05-19 15:14:53 +00:00
|
|
|
import requests
|
|
|
|
|
|
|
|
from homeassistant.components import sensor
|
2019-03-19 06:07:39 +00:00
|
|
|
from homeassistant.components.bom.sensor import BOMCurrentData
|
2018-05-19 15:14:53 +00:00
|
|
|
from homeassistant.setup import setup_component
|
2019-07-31 19:25:30 +00:00
|
|
|
from tests.common import assert_setup_component, get_test_home_assistant, load_fixture
|
2018-05-08 17:35:55 +00:00
|
|
|
|
|
|
|
VALID_CONFIG = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"platform": "bom",
|
|
|
|
"station": "IDN60901.94767",
|
|
|
|
"name": "Fake",
|
|
|
|
"monitored_conditions": ["apparent_t", "press", "weather"],
|
2018-05-08 17:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def mocked_requests(*args, **kwargs):
|
|
|
|
"""Mock requests.get invocations."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-05-08 17:35:55 +00:00
|
|
|
class MockResponse:
|
|
|
|
"""Class to represent a mocked response."""
|
|
|
|
|
|
|
|
def __init__(self, json_data, status_code):
|
|
|
|
"""Initialize the mock response class."""
|
|
|
|
self.json_data = json_data
|
|
|
|
self.status_code = status_code
|
|
|
|
|
|
|
|
def json(self):
|
|
|
|
"""Return the json of the response."""
|
|
|
|
return self.json_data
|
|
|
|
|
|
|
|
@property
|
|
|
|
def content(self):
|
|
|
|
"""Return the content of the response."""
|
|
|
|
return self.json()
|
|
|
|
|
|
|
|
def raise_for_status(self):
|
|
|
|
"""Raise an HTTPError if status is not 200."""
|
|
|
|
if self.status_code != 200:
|
|
|
|
raise requests.HTTPError(self.status_code)
|
|
|
|
|
|
|
|
url = urlparse(args[0])
|
2019-07-31 19:25:30 +00:00
|
|
|
if re.match(r"^/fwo/[\w]+/[\w.]+\.json", url.path):
|
|
|
|
return MockResponse(json.loads(load_fixture("bom_weather.json")), 200)
|
2018-05-08 17:35:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
raise NotImplementedError("Unknown route {}".format(url.path))
|
2018-05-08 17:35:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestBOMWeatherSensor(unittest.TestCase):
|
|
|
|
"""Test the BOM Weather sensor."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Set up things to be run when tests are started."""
|
|
|
|
self.hass = get_test_home_assistant()
|
|
|
|
self.config = VALID_CONFIG
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""Stop everything that was started."""
|
|
|
|
self.hass.stop()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@patch("requests.get", side_effect=mocked_requests)
|
2018-05-08 17:35:55 +00:00
|
|
|
def test_setup(self, mock_get):
|
|
|
|
"""Test the setup with custom settings."""
|
|
|
|
with assert_setup_component(1, sensor.DOMAIN):
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup_component(self.hass, sensor.DOMAIN, {"sensor": VALID_CONFIG})
|
2018-05-08 17:35:55 +00:00
|
|
|
|
|
|
|
fake_entities = [
|
2019-07-31 19:25:30 +00:00
|
|
|
"bom_fake_feels_like_c",
|
|
|
|
"bom_fake_pressure_mb",
|
|
|
|
"bom_fake_weather",
|
|
|
|
]
|
2018-05-08 17:35:55 +00:00
|
|
|
|
|
|
|
for entity_id in fake_entities:
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.hass.states.get("sensor.{}".format(entity_id))
|
2018-10-24 10:10:05 +00:00
|
|
|
assert state is not None
|
2018-05-08 17:35:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
@patch("requests.get", side_effect=mocked_requests)
|
2018-05-08 17:35:55 +00:00
|
|
|
def test_sensor_values(self, mock_get):
|
|
|
|
"""Test retrieval of sensor values."""
|
2019-07-31 19:25:30 +00:00
|
|
|
assert setup_component(self.hass, sensor.DOMAIN, {"sensor": VALID_CONFIG})
|
2018-05-08 17:35:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
weather = self.hass.states.get("sensor.bom_fake_weather").state
|
|
|
|
assert "Fine" == weather
|
2018-05-19 15:14:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
pressure = self.hass.states.get("sensor.bom_fake_pressure_mb").state
|
|
|
|
assert "1021.7" == pressure
|
2018-05-19 15:14:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
feels_like = self.hass.states.get("sensor.bom_fake_feels_like_c").state
|
|
|
|
assert "25.0" == feels_like
|
2018-11-22 01:41:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestBOMCurrentData(unittest.TestCase):
|
|
|
|
"""Test the BOM data container."""
|
|
|
|
|
|
|
|
def test_should_update_initial(self):
|
|
|
|
"""Test that the first update always occurs."""
|
2019-07-31 19:25:30 +00:00
|
|
|
bom_data = BOMCurrentData("IDN60901.94767")
|
2018-11-22 01:41:53 +00:00
|
|
|
assert bom_data.should_update() is True
|