Move imports to top for nsw_fuel_station (#29538)

* Move imports to top for nsw_fuel_station

* Correct patch path in test_sensor.py

* Fix tests by removing the unused argument mock_nsw_fuel
This commit is contained in:
springstan 2019-12-09 11:29:36 +01:00 committed by Franck Nijhof
parent 425a1814d9
commit 202522fbca
2 changed files with 13 additions and 10 deletions

View File

@ -3,11 +3,12 @@ import datetime
import logging import logging
from typing import Optional from typing import Optional
from nsw_fuel import FuelCheckClient, FuelCheckError
import voluptuous as vol import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import ATTR_ATTRIBUTION from homeassistant.const import ATTR_ATTRIBUTION
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle from homeassistant.util import Throttle
@ -52,7 +53,6 @@ NOTIFICATION_TITLE = "NSW Fuel Station Sensor Setup"
def setup_platform(hass, config, add_entities, discovery_info=None): def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the NSW Fuel Station sensor.""" """Set up the NSW Fuel Station sensor."""
from nsw_fuel import FuelCheckClient
station_id = config[CONF_STATION_ID] station_id = config[CONF_STATION_ID]
fuel_types = config[CONF_FUEL_TYPES] fuel_types = config[CONF_FUEL_TYPES]
@ -97,7 +97,6 @@ class StationPriceData:
@Throttle(MIN_TIME_BETWEEN_UPDATES) @Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self): def update(self):
"""Update the internal data using the API client.""" """Update the internal data using the API client."""
from nsw_fuel import FuelCheckError
if self._reference_data is None: if self._reference_data is None:
try: try:

View File

@ -4,7 +4,7 @@ from unittest.mock import patch
from homeassistant.components import sensor from homeassistant.components import sensor
from homeassistant.setup import setup_component from homeassistant.setup import setup_component
from tests.common import get_test_home_assistant, assert_setup_component, MockDependency from tests.common import get_test_home_assistant, assert_setup_component
VALID_CONFIG = { VALID_CONFIG = {
"platform": "nsw_fuel_station", "platform": "nsw_fuel_station",
@ -83,9 +83,11 @@ class TestNSWFuelStation(unittest.TestCase):
"""Stop everything that was started.""" """Stop everything that was started."""
self.hass.stop() self.hass.stop()
@MockDependency("nsw_fuel") @patch(
@patch("nsw_fuel.FuelCheckClient", new=FuelCheckClientMock) "homeassistant.components.nsw_fuel_station.sensor.FuelCheckClient",
def test_setup(self, mock_nsw_fuel): new=FuelCheckClientMock,
)
def test_setup(self):
"""Test the setup with custom settings.""" """Test the setup with custom settings."""
with assert_setup_component(1, sensor.DOMAIN): with assert_setup_component(1, sensor.DOMAIN):
assert setup_component(self.hass, sensor.DOMAIN, {"sensor": VALID_CONFIG}) assert setup_component(self.hass, sensor.DOMAIN, {"sensor": VALID_CONFIG})
@ -96,9 +98,11 @@ class TestNSWFuelStation(unittest.TestCase):
state = self.hass.states.get("sensor.{}".format(entity_id)) state = self.hass.states.get("sensor.{}".format(entity_id))
assert state is not None assert state is not None
@MockDependency("nsw_fuel") @patch(
@patch("nsw_fuel.FuelCheckClient", new=FuelCheckClientMock) "homeassistant.components.nsw_fuel_station.sensor.FuelCheckClient",
def test_sensor_values(self, mock_nsw_fuel): new=FuelCheckClientMock,
)
def test_sensor_values(self):
"""Test retrieval of sensor values.""" """Test retrieval of sensor values."""
assert setup_component(self.hass, sensor.DOMAIN, {"sensor": VALID_CONFIG}) assert setup_component(self.hass, sensor.DOMAIN, {"sensor": VALID_CONFIG})