Rewrite london_air tests in pytest style (#41165)
parent
1f850f6374
commit
dab5e5ebc9
|
@ -128,19 +128,21 @@ class AirSensor(Entity):
|
|||
"""Return other details about the sensor state."""
|
||||
attrs = {}
|
||||
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
|
||||
return attrs
|
||||
|
||||
def update(self):
|
||||
"""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 = []
|
||||
for site in self._site_data:
|
||||
if site["pollutants_status"] != "no_species_data":
|
||||
sites_status.append(site["pollutants_status"])
|
||||
self._api_data.update()
|
||||
if self._api_data.data:
|
||||
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:
|
||||
self._state = max(set(sites_status), key=sites_status.count)
|
||||
else:
|
||||
|
|
|
@ -1,34 +1,51 @@
|
|||
"""The tests for the tube_state platform."""
|
||||
import unittest
|
||||
|
||||
import requests_mock
|
||||
|
||||
"""The tests for the london_air platform."""
|
||||
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):
|
||||
"""Test the tube_state platform."""
|
||||
async def test_valid_state(hass, requests_mock):
|
||||
"""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):
|
||||
"""Initialize values for this testcase class."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.config = VALID_CONFIG
|
||||
self.addCleanup(self.hass.stop)
|
||||
state = hass.states.get("sensor.merton")
|
||||
assert state is not None
|
||||
assert state.state == "Low"
|
||||
assert state.attributes["icon"] == "mdi:cloud-outline"
|
||||
assert state.attributes["updated"] == "2017-08-03 03:00:00"
|
||||
assert state.attributes["sites"] == 2
|
||||
assert state.attributes["friendly_name"] == "Merton"
|
||||
|
||||
@requests_mock.Mocker()
|
||||
def test_setup(self, mock_req):
|
||||
"""Test for operational tube_state sensor with proper attributes."""
|
||||
mock_req.get(URL, text=load_fixture("london_air.json"))
|
||||
assert setup_component(self.hass, "sensor", {"sensor": self.config})
|
||||
self.hass.block_till_done()
|
||||
sites = state.attributes["data"]
|
||||
assert sites is not None
|
||||
assert len(sites) == 2
|
||||
assert sites[0]["site_code"] == "ME2"
|
||||
assert sites[0]["site_type"] == "Roadside"
|
||||
assert sites[0]["site_name"] == "Merton Road"
|
||||
assert sites[0]["pollutants_status"] == "Low"
|
||||
|
||||
state = self.hass.states.get("sensor.merton")
|
||||
assert state.state == "Low"
|
||||
assert state.attributes.get("updated") == "2017-08-03 03:00:00"
|
||||
assert state.attributes.get("sites") == 2
|
||||
assert state.attributes.get("data")[0]["site_code"] == "ME2"
|
||||
pollutants = sites[0]["pollutants"]
|
||||
assert pollutants is not None
|
||||
assert len(pollutants) == 1
|
||||
assert pollutants[0]["code"] == "PM10"
|
||||
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…
Reference in New Issue