core/tests/components/bom/test_sensor.py

105 lines
3.3 KiB
Python
Raw Normal View History

2018-05-08 17:35:55 +00:00
"""The tests for the BOM Weather sensor platform."""
import json
2018-05-08 17:35:55 +00:00
import re
import unittest
from unittest.mock import patch
from urllib.parse import urlparse
import requests
from homeassistant.components import sensor
Consolidate all platforms that have tests (#22109) * Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
2019-03-19 06:07:39 +00:00
from homeassistant.components.bom.sensor import BOMCurrentData
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))
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
2019-07-31 19:25:30 +00:00
pressure = self.hass.states.get("sensor.bom_fake_pressure_mb").state
assert "1021.7" == pressure
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
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")
assert bom_data.should_update() is True