2020-03-31 18:58:44 +00:00
|
|
|
"""Tests for the myq integration."""
|
|
|
|
import json
|
2021-02-10 20:30:52 +00:00
|
|
|
import logging
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
2020-03-31 18:58:44 +00:00
|
|
|
|
2021-02-10 20:30:52 +00:00
|
|
|
from pymyq.const import ACCOUNTS_ENDPOINT, DEVICES_ENDPOINT
|
|
|
|
|
2020-03-31 18:58:44 +00:00
|
|
|
from homeassistant.components.myq.const import DOMAIN
|
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
|
|
|
from tests.common import MockConfigEntry, load_fixture
|
|
|
|
|
2021-02-10 20:30:52 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-03-31 18:58:44 +00:00
|
|
|
|
|
|
|
async def async_init_integration(
|
2020-08-27 11:56:20 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
skip_setup: bool = False,
|
2020-03-31 18:58:44 +00:00
|
|
|
) -> MockConfigEntry:
|
|
|
|
"""Set up the myq integration in Home Assistant."""
|
|
|
|
|
|
|
|
devices_fixture = "myq/devices.json"
|
|
|
|
devices_json = load_fixture(devices_fixture)
|
|
|
|
devices_dict = json.loads(devices_json)
|
|
|
|
|
2021-02-10 20:30:52 +00:00
|
|
|
def _handle_mock_api_oauth_authenticate():
|
|
|
|
return 1234, 1800
|
|
|
|
|
|
|
|
def _handle_mock_api_request(method, returns, url, **kwargs):
|
|
|
|
_LOGGER.debug("URL: %s", url)
|
|
|
|
if url == ACCOUNTS_ENDPOINT:
|
|
|
|
_LOGGER.debug("Accounts")
|
|
|
|
return None, {"accounts": [{"id": 1, "name": "mock"}]}
|
|
|
|
if url == DEVICES_ENDPOINT.format(account_id=1):
|
|
|
|
_LOGGER.debug("Devices")
|
|
|
|
return None, devices_dict
|
|
|
|
_LOGGER.debug("Something else")
|
|
|
|
return None, {}
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"pymyq.api.API._oauth_authenticate",
|
|
|
|
side_effect=_handle_mock_api_oauth_authenticate,
|
|
|
|
), patch("pymyq.api.API.request", side_effect=_handle_mock_api_request):
|
2020-03-31 18:58:44 +00:00
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=DOMAIN, data={CONF_USERNAME: "mock", CONF_PASSWORD: "mock"}
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
if not skip_setup:
|
|
|
|
await hass.config_entries.async_setup(entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
return entry
|