From 20fdcbadff35891de6dc1965cf81b3954fe71bab Mon Sep 17 00:00:00 2001 From: springstan <46536646+springstan@users.noreply.github.com> Date: Thu, 5 Dec 2019 21:56:42 +0100 Subject: [PATCH] Move imports to top for nextbus (#29520) * Move imports to top for nextbus * Fix test_sensor.py failed tests --- homeassistant/components/nextbus/sensor.py | 10 ++++----- tests/components/nextbus/test_sensor.py | 24 +++++++++++++--------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/nextbus/sensor.py b/homeassistant/components/nextbus/sensor.py index 7622bd133f0..983c2272adc 100644 --- a/homeassistant/components/nextbus/sensor.py +++ b/homeassistant/components/nextbus/sensor.py @@ -1,13 +1,13 @@ """NextBus sensor.""" -import logging from itertools import chain +import logging +from py_nextbus import NextBusClient import voluptuous as vol -import homeassistant.helpers.config_validation as cv from homeassistant.components.sensor import PLATFORM_SCHEMA -from homeassistant.const import CONF_NAME -from homeassistant.const import DEVICE_CLASS_TIMESTAMP +from homeassistant.const import CONF_NAME, DEVICE_CLASS_TIMESTAMP +import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity from homeassistant.util.dt import utc_from_timestamp @@ -94,8 +94,6 @@ def setup_platform(hass, config, add_entities, discovery_info=None): stop = config[CONF_STOP] name = config.get(CONF_NAME) - from py_nextbus import NextBusClient - client = NextBusClient(output_format="json") # Ensures that the tags provided are valid, also logs out valid values diff --git a/tests/components/nextbus/test_sensor.py b/tests/components/nextbus/test_sensor.py index d7c1919dff0..4bac317102a 100644 --- a/tests/components/nextbus/test_sensor.py +++ b/tests/components/nextbus/test_sensor.py @@ -2,11 +2,12 @@ from copy import deepcopy import pytest +from unittest.mock import patch import homeassistant.components.sensor as sensor import homeassistant.components.nextbus.sensor as nextbus -from tests.common import assert_setup_component, async_setup_component, MockDependency +from tests.common import assert_setup_component, async_setup_component VALID_AGENCY = "sf-muni" @@ -54,14 +55,16 @@ async def assert_setup_sensor(hass, config, count=1): @pytest.fixture def mock_nextbus(): """Create a mock py_nextbus module.""" - with MockDependency("py_nextbus") as py_nextbus: - yield py_nextbus + with patch( + "homeassistant.components.nextbus.sensor.NextBusClient" + ) as NextBusClient: + yield NextBusClient @pytest.fixture def mock_nextbus_predictions(mock_nextbus): """Create a mock of NextBusClient predictions.""" - instance = mock_nextbus.NextBusClient.return_value + instance = mock_nextbus.return_value instance.get_predictions_for_multi_stops.return_value = BASIC_RESULTS yield instance.get_predictions_for_multi_stops @@ -70,7 +73,7 @@ def mock_nextbus_predictions(mock_nextbus): @pytest.fixture def mock_nextbus_lists(mock_nextbus): """Mock all list functions in nextbus to test validate logic.""" - instance = mock_nextbus.NextBusClient.return_value + instance = mock_nextbus.return_value instance.get_agency_list.return_value = { "agency": [{"tag": "sf-muni", "title": "San Francisco Muni"}] } @@ -94,17 +97,18 @@ async def test_invalid_config(hass, mock_nextbus, mock_nextbus_lists): async def test_validate_tags(hass, mock_nextbus, mock_nextbus_lists): """Test that additional validation against the API is successful.""" - client = mock_nextbus.NextBusClient() # with self.subTest('Valid everything'): - assert nextbus.validate_tags(client, VALID_AGENCY, VALID_ROUTE, VALID_STOP) + assert nextbus.validate_tags(mock_nextbus(), VALID_AGENCY, VALID_ROUTE, VALID_STOP) # with self.subTest('Invalid agency'): - assert not nextbus.validate_tags(client, "not-valid", VALID_ROUTE, VALID_STOP) + assert not nextbus.validate_tags( + mock_nextbus(), "not-valid", VALID_ROUTE, VALID_STOP + ) # with self.subTest('Invalid route'): - assert not nextbus.validate_tags(client, VALID_AGENCY, "0", VALID_STOP) + assert not nextbus.validate_tags(mock_nextbus(), VALID_AGENCY, "0", VALID_STOP) # with self.subTest('Invalid stop'): - assert not nextbus.validate_tags(client, VALID_AGENCY, VALID_ROUTE, 0) + assert not nextbus.validate_tags(mock_nextbus(), VALID_AGENCY, VALID_ROUTE, 0) async def test_verify_valid_state(