core/tests/components/hydroquebec/test_sensor.py

106 lines
3.0 KiB
Python
Raw Normal View History

"""The test for the hydroquebec sensor platform."""
import asyncio
import logging
import sys
from unittest.mock import MagicMock
from homeassistant.bootstrap import async_setup_component
Consolidate all platforms that have tests (#22109) * Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
2019-03-19 06:07:39 +00:00
from homeassistant.components.hydroquebec import sensor as hydroquebec
from tests.common import assert_setup_component
CONTRACT = "123456789"
class HydroQuebecClientMock():
"""Fake Hydroquebec client."""
def __init__(self, username, password, contract=None, httpsession=None):
"""Fake Hydroquebec client init."""
pass
def get_data(self, contract):
"""Return fake hydroquebec data."""
return {CONTRACT: {"balance": 160.12}}
def get_contracts(self):
"""Return fake hydroquebec contracts."""
return [CONTRACT]
@asyncio.coroutine
def fetch_data(self):
"""Return fake fetching data."""
pass
class HydroQuebecClientMockError(HydroQuebecClientMock):
"""Fake Hydroquebec client error."""
def get_contracts(self):
"""Return fake hydroquebec contracts."""
return []
@asyncio.coroutine
def fetch_data(self):
"""Return fake fetching data."""
raise PyHydroQuebecErrorMock("Fake Error")
class PyHydroQuebecErrorMock(BaseException):
"""Fake PyHydroquebec Error."""
class PyHydroQuebecClientFakeModule():
"""Fake pyfido.client module."""
PyHydroQuebecError = PyHydroQuebecErrorMock
class PyHydroQuebecFakeModule():
"""Fake pyfido module."""
HydroQuebecClient = HydroQuebecClientMockError
@asyncio.coroutine
def test_hydroquebec_sensor(loop, hass):
"""Test the Hydroquebec number sensor."""
sys.modules['pyhydroquebec'] = MagicMock()
sys.modules['pyhydroquebec.client'] = MagicMock()
sys.modules['pyhydroquebec.client.PyHydroQuebecError'] = \
PyHydroQuebecErrorMock
import pyhydroquebec.client
pyhydroquebec.HydroQuebecClient = HydroQuebecClientMock
pyhydroquebec.client.PyHydroQuebecError = PyHydroQuebecErrorMock
config = {
'sensor': {
'platform': 'hydroquebec',
'name': 'hydro',
'contract': CONTRACT,
'username': 'myusername',
'password': 'password',
'monitored_variables': [
'balance',
],
}
}
with assert_setup_component(1):
yield from async_setup_component(hass, 'sensor', config)
state = hass.states.get('sensor.hydro_balance')
assert state.state == "160.12"
assert state.attributes.get('unit_of_measurement') == "CAD"
@asyncio.coroutine
def test_error(hass, caplog):
"""Test the Hydroquebec sensor errors."""
caplog.set_level(logging.ERROR)
sys.modules['pyhydroquebec'] = PyHydroQuebecFakeModule()
sys.modules['pyhydroquebec.client'] = PyHydroQuebecClientFakeModule()
config = {}
fake_async_add_entities = MagicMock()
yield from hydroquebec.async_setup_platform(hass, config,
fake_async_add_entities)
assert fake_async_add_entities.called is False