init home-assistant
This commit is contained in:
49
home-assistant/tests/components/dexcom/__init__.py
Normal file
49
home-assistant/tests/components/dexcom/__init__.py
Normal file
@@ -0,0 +1,49 @@
|
||||
"""Tests for the Dexcom integration."""
|
||||
|
||||
import json
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
from pydexcom import GlucoseReading
|
||||
|
||||
from homeassistant.components.dexcom.const import CONF_SERVER, DOMAIN, SERVER_US
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from tests.common import MockConfigEntry, load_fixture
|
||||
|
||||
CONFIG = {
|
||||
CONF_USERNAME: "test_username",
|
||||
CONF_PASSWORD: "test_password",
|
||||
CONF_SERVER: SERVER_US,
|
||||
}
|
||||
|
||||
GLUCOSE_READING = GlucoseReading(json.loads(load_fixture("data.json", "dexcom")))
|
||||
|
||||
|
||||
async def init_integration(
|
||||
hass: HomeAssistant, options: dict[str, Any] | None = None
|
||||
) -> MockConfigEntry:
|
||||
"""Set up the Dexcom integration in Home Assistant."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title="test_username",
|
||||
unique_id="test_username",
|
||||
data=CONFIG,
|
||||
options=options,
|
||||
)
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.dexcom.Dexcom.get_current_glucose_reading",
|
||||
return_value=GLUCOSE_READING,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.dexcom.Dexcom.create_session",
|
||||
return_value="test_session_id",
|
||||
),
|
||||
):
|
||||
entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
return entry
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"DT": "/Date(1587165223000+0000)/",
|
||||
"ST": "/Date(1587179623000)/",
|
||||
"Trend": 4,
|
||||
"Value": 110,
|
||||
"WT": "/Date(1587179623000)/"
|
||||
}
|
||||
101
home-assistant/tests/components/dexcom/test_config_flow.py
Normal file
101
home-assistant/tests/components/dexcom/test_config_flow.py
Normal file
@@ -0,0 +1,101 @@
|
||||
"""Test the Dexcom config flow."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from pydexcom import AccountError, SessionError
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.dexcom.const import DOMAIN
|
||||
from homeassistant.const import CONF_USERNAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
||||
from . import CONFIG
|
||||
|
||||
|
||||
async def test_form(hass: HomeAssistant) -> None:
|
||||
"""Test we get the form."""
|
||||
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
assert result["type"] is FlowResultType.FORM
|
||||
assert result["errors"] == {}
|
||||
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.dexcom.config_flow.Dexcom.create_session",
|
||||
return_value="test_session_id",
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.dexcom.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
CONFIG,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result2["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result2["title"] == CONFIG[CONF_USERNAME]
|
||||
assert result2["data"] == CONFIG
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_form_account_error(hass: HomeAssistant) -> None:
|
||||
"""Test we handle account error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.dexcom.config_flow.Dexcom",
|
||||
side_effect=AccountError,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
CONFIG,
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
assert result2["errors"] == {"base": "invalid_auth"}
|
||||
|
||||
|
||||
async def test_form_session_error(hass: HomeAssistant) -> None:
|
||||
"""Test we handle session error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.dexcom.config_flow.Dexcom",
|
||||
side_effect=SessionError,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
CONFIG,
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
assert result2["errors"] == {"base": "cannot_connect"}
|
||||
|
||||
|
||||
async def test_form_unknown_error(hass: HomeAssistant) -> None:
|
||||
"""Test we handle unknown error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||
)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.dexcom.config_flow.Dexcom",
|
||||
side_effect=Exception,
|
||||
):
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
CONFIG,
|
||||
)
|
||||
|
||||
assert result2["type"] is FlowResultType.FORM
|
||||
assert result2["errors"] == {"base": "unknown"}
|
||||
67
home-assistant/tests/components/dexcom/test_init.py
Normal file
67
home-assistant/tests/components/dexcom/test_init.py
Normal file
@@ -0,0 +1,67 @@
|
||||
"""Test the Dexcom config flow."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from pydexcom import AccountError, SessionError
|
||||
|
||||
from homeassistant.components.dexcom.const import DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from . import CONFIG, init_integration
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_setup_entry_account_error(hass: HomeAssistant) -> None:
|
||||
"""Test entry setup failed due to account error."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title="test_username",
|
||||
unique_id="test_username",
|
||||
data=CONFIG,
|
||||
options=None,
|
||||
)
|
||||
with patch(
|
||||
"homeassistant.components.dexcom.Dexcom",
|
||||
side_effect=AccountError,
|
||||
):
|
||||
entry.add_to_hass(hass)
|
||||
result = await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result is False
|
||||
|
||||
|
||||
async def test_setup_entry_session_error(hass: HomeAssistant) -> None:
|
||||
"""Test entry setup failed due to session error."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title="test_username",
|
||||
unique_id="test_username",
|
||||
data=CONFIG,
|
||||
options=None,
|
||||
)
|
||||
with patch(
|
||||
"homeassistant.components.dexcom.Dexcom",
|
||||
side_effect=SessionError,
|
||||
):
|
||||
entry.add_to_hass(hass)
|
||||
result = await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result is False
|
||||
|
||||
|
||||
async def test_unload_entry(hass: HomeAssistant) -> None:
|
||||
"""Test successful unload of entry."""
|
||||
entry = await init_integration(hass)
|
||||
|
||||
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
|
||||
assert entry.state is ConfigEntryState.LOADED
|
||||
|
||||
assert await hass.config_entries.async_unload(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert entry.state is ConfigEntryState.NOT_LOADED
|
||||
assert not hass.data.get(DOMAIN)
|
||||
55
home-assistant/tests/components/dexcom/test_sensor.py
Normal file
55
home-assistant/tests/components/dexcom/test_sensor.py
Normal file
@@ -0,0 +1,55 @@
|
||||
"""The sensor tests for the griddy platform."""
|
||||
|
||||
from unittest.mock import patch
|
||||
|
||||
from pydexcom import SessionError
|
||||
|
||||
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_component import async_update_entity
|
||||
|
||||
from . import GLUCOSE_READING, init_integration
|
||||
|
||||
|
||||
async def test_sensors(hass: HomeAssistant) -> None:
|
||||
"""Test we get sensor data."""
|
||||
await init_integration(hass)
|
||||
|
||||
test_username_glucose_value = hass.states.get("sensor.test_username_glucose_value")
|
||||
assert test_username_glucose_value.state == str(GLUCOSE_READING.value)
|
||||
test_username_glucose_trend = hass.states.get("sensor.test_username_glucose_trend")
|
||||
assert test_username_glucose_trend.state == GLUCOSE_READING.trend_description
|
||||
|
||||
|
||||
async def test_sensors_unknown(hass: HomeAssistant) -> None:
|
||||
"""Test we handle sensor state unknown."""
|
||||
await init_integration(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.dexcom.Dexcom.get_current_glucose_reading",
|
||||
return_value=None,
|
||||
):
|
||||
await async_update_entity(hass, "sensor.test_username_glucose_value")
|
||||
await async_update_entity(hass, "sensor.test_username_glucose_trend")
|
||||
|
||||
test_username_glucose_value = hass.states.get("sensor.test_username_glucose_value")
|
||||
assert test_username_glucose_value.state == STATE_UNKNOWN
|
||||
test_username_glucose_trend = hass.states.get("sensor.test_username_glucose_trend")
|
||||
assert test_username_glucose_trend.state == STATE_UNKNOWN
|
||||
|
||||
|
||||
async def test_sensors_update_failed(hass: HomeAssistant) -> None:
|
||||
"""Test we handle sensor update failed."""
|
||||
await init_integration(hass)
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.dexcom.Dexcom.get_current_glucose_reading",
|
||||
side_effect=SessionError,
|
||||
):
|
||||
await async_update_entity(hass, "sensor.test_username_glucose_value")
|
||||
await async_update_entity(hass, "sensor.test_username_glucose_trend")
|
||||
|
||||
test_username_glucose_value = hass.states.get("sensor.test_username_glucose_value")
|
||||
assert test_username_glucose_value.state == STATE_UNAVAILABLE
|
||||
test_username_glucose_trend = hass.states.get("sensor.test_username_glucose_trend")
|
||||
assert test_username_glucose_trend.state == STATE_UNAVAILABLE
|
||||
Reference in New Issue
Block a user