Rewrite london_air tests in pytest style (#41165)
This commit is contained in:
parent
1f850f6374
commit
dab5e5ebc9
2 changed files with 52 additions and 33 deletions
|
@ -128,19 +128,21 @@ class AirSensor(Entity):
|
||||||
"""Return other details about the sensor state."""
|
"""Return other details about the sensor state."""
|
||||||
attrs = {}
|
attrs = {}
|
||||||
attrs["updated"] = self._updated
|
attrs["updated"] = self._updated
|
||||||
attrs["sites"] = len(self._site_data)
|
attrs["sites"] = len(self._site_data) if self._site_data is not None else 0
|
||||||
attrs["data"] = self._site_data
|
attrs["data"] = self._site_data
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update the sensor."""
|
"""Update the sensor."""
|
||||||
self._api_data.update()
|
|
||||||
self._site_data = self._api_data.data[self._name]
|
|
||||||
self._updated = self._site_data[0]["updated"]
|
|
||||||
sites_status = []
|
sites_status = []
|
||||||
for site in self._site_data:
|
self._api_data.update()
|
||||||
if site["pollutants_status"] != "no_species_data":
|
if self._api_data.data:
|
||||||
sites_status.append(site["pollutants_status"])
|
self._site_data = self._api_data.data[self._name]
|
||||||
|
self._updated = self._site_data[0]["updated"]
|
||||||
|
for site in self._site_data:
|
||||||
|
if site["pollutants_status"] != "no_species_data":
|
||||||
|
sites_status.append(site["pollutants_status"])
|
||||||
|
|
||||||
if sites_status:
|
if sites_status:
|
||||||
self._state = max(set(sites_status), key=sites_status.count)
|
self._state = max(set(sites_status), key=sites_status.count)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -1,34 +1,51 @@
|
||||||
"""The tests for the tube_state platform."""
|
"""The tests for the london_air platform."""
|
||||||
import unittest
|
|
||||||
|
|
||||||
import requests_mock
|
|
||||||
|
|
||||||
from homeassistant.components.london_air.sensor import CONF_LOCATIONS, URL
|
from homeassistant.components.london_air.sensor import CONF_LOCATIONS, URL
|
||||||
from homeassistant.setup import setup_component
|
from homeassistant.const import HTTP_OK, HTTP_SERVICE_UNAVAILABLE
|
||||||
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from tests.common import get_test_home_assistant, load_fixture
|
from tests.common import load_fixture
|
||||||
|
|
||||||
VALID_CONFIG = {"platform": "london_air", CONF_LOCATIONS: ["Merton"]}
|
VALID_CONFIG = {"sensor": {"platform": "london_air", CONF_LOCATIONS: ["Merton"]}}
|
||||||
|
|
||||||
|
|
||||||
class TestLondonAirSensor(unittest.TestCase):
|
async def test_valid_state(hass, requests_mock):
|
||||||
"""Test the tube_state platform."""
|
"""Test for operational london_air sensor with proper attributes."""
|
||||||
|
requests_mock.get(URL, text=load_fixture("london_air.json"), status_code=HTTP_OK)
|
||||||
|
assert await async_setup_component(hass, "sensor", VALID_CONFIG)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
def setUp(self):
|
state = hass.states.get("sensor.merton")
|
||||||
"""Initialize values for this testcase class."""
|
assert state is not None
|
||||||
self.hass = get_test_home_assistant()
|
assert state.state == "Low"
|
||||||
self.config = VALID_CONFIG
|
assert state.attributes["icon"] == "mdi:cloud-outline"
|
||||||
self.addCleanup(self.hass.stop)
|
assert state.attributes["updated"] == "2017-08-03 03:00:00"
|
||||||
|
assert state.attributes["sites"] == 2
|
||||||
|
assert state.attributes["friendly_name"] == "Merton"
|
||||||
|
|
||||||
@requests_mock.Mocker()
|
sites = state.attributes["data"]
|
||||||
def test_setup(self, mock_req):
|
assert sites is not None
|
||||||
"""Test for operational tube_state sensor with proper attributes."""
|
assert len(sites) == 2
|
||||||
mock_req.get(URL, text=load_fixture("london_air.json"))
|
assert sites[0]["site_code"] == "ME2"
|
||||||
assert setup_component(self.hass, "sensor", {"sensor": self.config})
|
assert sites[0]["site_type"] == "Roadside"
|
||||||
self.hass.block_till_done()
|
assert sites[0]["site_name"] == "Merton Road"
|
||||||
|
assert sites[0]["pollutants_status"] == "Low"
|
||||||
|
|
||||||
state = self.hass.states.get("sensor.merton")
|
pollutants = sites[0]["pollutants"]
|
||||||
assert state.state == "Low"
|
assert pollutants is not None
|
||||||
assert state.attributes.get("updated") == "2017-08-03 03:00:00"
|
assert len(pollutants) == 1
|
||||||
assert state.attributes.get("sites") == 2
|
assert pollutants[0]["code"] == "PM10"
|
||||||
assert state.attributes.get("data")[0]["site_code"] == "ME2"
|
assert pollutants[0]["quality"] == "Low"
|
||||||
|
assert int(pollutants[0]["index"]) == 2
|
||||||
|
assert pollutants[0]["summary"] == "PM10 is Low"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_api_failure(hass, requests_mock):
|
||||||
|
"""Test for failure in the API."""
|
||||||
|
requests_mock.get(URL, status_code=HTTP_SERVICE_UNAVAILABLE)
|
||||||
|
assert await async_setup_component(hass, "sensor", VALID_CONFIG)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
state = hass.states.get("sensor.merton")
|
||||||
|
assert state is not None
|
||||||
|
assert state.attributes["updated"] is None
|
||||||
|
assert state.attributes["sites"] == 0
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue