Move imports to top for fido (#29557)

* Move imports to top for fido

* Fix tests for fido by using patch
This commit is contained in:
springstan 2019-12-08 12:20:53 +01:00 committed by Paulus Schoutsen
parent 6de8072e8a
commit d752fe3033
2 changed files with 26 additions and 30 deletions

View File

@ -7,21 +7,23 @@ https://www.fido.ca/pages/#/my-account/wireless
For more details about this platform, please refer to the documentation at For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.fido/ https://home-assistant.io/components/sensor.fido/
""" """
import logging
from datetime import timedelta from datetime import timedelta
import logging
from pyfido import FidoClient
from pyfido.client import PyFidoError
import voluptuous as vol import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import ( from homeassistant.const import (
CONF_USERNAME,
CONF_PASSWORD,
CONF_NAME,
CONF_MONITORED_VARIABLES, CONF_MONITORED_VARIABLES,
CONF_NAME,
CONF_PASSWORD,
CONF_USERNAME,
) )
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
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -147,7 +149,6 @@ class FidoData:
def __init__(self, username, password, httpsession): def __init__(self, username, password, httpsession):
"""Initialize the data object.""" """Initialize the data object."""
from pyfido import FidoClient
self.client = FidoClient(username, password, REQUESTS_TIMEOUT, httpsession) self.client = FidoClient(username, password, REQUESTS_TIMEOUT, httpsession)
self.data = {} self.data = {}
@ -155,7 +156,6 @@ class FidoData:
@Throttle(MIN_TIME_BETWEEN_UPDATES) @Throttle(MIN_TIME_BETWEEN_UPDATES)
async def async_update(self): async def async_update(self):
"""Get the latest data from Fido.""" """Get the latest data from Fido."""
from pyfido.client import PyFidoError
try: try:
await self.client.fetch_data() await self.client.fetch_data()

View File

@ -2,7 +2,7 @@
import asyncio import asyncio
import logging import logging
import sys import sys
from unittest.mock import MagicMock from unittest.mock import MagicMock, patch
from homeassistant.bootstrap import async_setup_component from homeassistant.bootstrap import async_setup_component
from homeassistant.components.fido import sensor as fido from homeassistant.components.fido import sensor as fido
@ -66,29 +66,25 @@ def fake_async_add_entities(component, update_before_add=False):
@asyncio.coroutine @asyncio.coroutine
def test_fido_sensor(loop, hass): def test_fido_sensor(loop, hass):
"""Test the Fido number sensor.""" """Test the Fido number sensor."""
sys.modules["pyfido"] = MagicMock() with patch(
sys.modules["pyfido.client"] = MagicMock() "homeassistant.components.fido.sensor.FidoClient", new=FidoClientMock
sys.modules["pyfido.client.PyFidoError"] = PyFidoErrorMock ), patch("homeassistant.components.fido.sensor.PyFidoError", new=PyFidoErrorMock):
import pyfido.client config = {
"sensor": {
pyfido.FidoClient = FidoClientMock "platform": "fido",
pyfido.client.PyFidoError = PyFidoErrorMock "name": "fido",
config = { "username": "myusername",
"sensor": { "password": "password",
"platform": "fido", "monitored_variables": ["balance", "data_remaining"],
"name": "fido", }
"username": "myusername",
"password": "password",
"monitored_variables": ["balance", "data_remaining"],
} }
} with assert_setup_component(1):
with assert_setup_component(1): yield from async_setup_component(hass, "sensor", config)
yield from async_setup_component(hass, "sensor", config) state = hass.states.get("sensor.fido_1112223344_balance")
state = hass.states.get("sensor.fido_1112223344_balance") assert state.state == "160.12"
assert state.state == "160.12" assert state.attributes.get("number") == "1112223344"
assert state.attributes.get("number") == "1112223344" state = hass.states.get("sensor.fido_1112223344_data_remaining")
state = hass.states.get("sensor.fido_1112223344_data_remaining") assert state.state == "100.33"
assert state.state == "100.33"
@asyncio.coroutine @asyncio.coroutine