Nightscout PR fixes (#38737)

* Don't allow duplicate nightscout configs

* Fix nightscout translations

* Remove unnecessary should_poll method

* Remove SVG attribute, as it was duplicating the state

* Use aiohttp client session from HA

* Move validate_input outside the config class

* Use the entry unique_id on the sensor

* Move create entity logic

* Handle unexpected exception on Nightscout config
This commit is contained in:
Marcio Granzotto Rodrigues authored and GitHub committed 2020-08-13 07:46:07 -04:00
1 parent 86aa758ecd
commit 52a9921ed3
21 files changed
+74 -215

No files matched your search

+33 -10
View File
@@ -3,9 +3,11 @@ from aiohttp import ClientConnectionError
from homeassistant import config_entries, data_entry_flow, setup
from homeassistant.components.nightscout.const import DOMAIN
from homeassistant.components.nightscout.utils import hash_from_url
from homeassistant.const import CONF_URL
from tests.async_mock import patch
from tests.common import MockConfigEntry
from tests.components.nightscout import GLUCOSE_READINGS, SERVER_STATUS
CONFIG = {CONF_URL: "https://some.url:1234"}
@@ -20,13 +22,7 @@ async def test_form(hass):
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"] == {}
with patch(
"homeassistant.components.nightscout.NightscoutAPI.get_sgvs",
return_value=GLUCOSE_READINGS,
), patch(
"homeassistant.components.nightscout.NightscoutAPI.get_server_status",
return_value=SERVER_STATUS,
), _patch_async_setup() as mock_setup, _patch_async_setup_entry() as mock_setup_entry:
with _patch_glucose_readings(), _patch_server_status(), _patch_async_setup() as mock_setup, _patch_async_setup_entry() as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], CONFIG,
)
@@ -53,12 +49,12 @@ async def test_user_form_cannot_connect(hass):
result["flow_id"], {CONF_URL: "https://some.url:1234"},
)
assert result2["type"] == "form"
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result2["errors"] == {"base": "cannot_connect"}
async def test_user_form_unexpected_exception(hass):
"""Test we handle cannot connect error."""
"""Test we handle unexpected exception."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
@@ -71,10 +67,23 @@ async def test_user_form_unexpected_exception(hass):
result["flow_id"], {CONF_URL: "https://some.url:1234"},
)
assert result2["type"] == "form"
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result2["errors"] == {"base": "unknown"}
async def test_user_form_duplicate(hass):
"""Test duplicate entries."""
with _patch_glucose_readings(), _patch_server_status():
unique_id = hash_from_url(CONFIG[CONF_URL])
entry = MockConfigEntry(domain=DOMAIN, unique_id=unique_id)
await hass.config_entries.async_add(entry)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}, data=CONFIG,
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"
def _patch_async_setup():
return patch("homeassistant.components.nightscout.async_setup", return_value=True)
@@ -83,3 +92,17 @@ def _patch_async_setup_entry():
return patch(
"homeassistant.components.nightscout.async_setup_entry", return_value=True,
)
def _patch_glucose_readings():
return patch(
"homeassistant.components.nightscout.NightscoutAPI.get_sgvs",
return_value=GLUCOSE_READINGS,
)
def _patch_server_status():
return patch(
"homeassistant.components.nightscout.NightscoutAPI.get_server_status",
return_value=SERVER_STATUS,
)
@@ -5,7 +5,6 @@ from homeassistant.components.nightscout.const import (
ATTR_DELTA,
ATTR_DEVICE,
ATTR_DIRECTION,
ATTR_SVG,
)
from homeassistant.const import ATTR_ICON, STATE_UNAVAILABLE
@@ -56,5 +55,4 @@ async def test_sensor_attributes(hass):
assert attr[ATTR_DELTA] == reading.delta # pylint: disable=maybe-no-member
assert attr[ATTR_DEVICE] == reading.device # pylint: disable=maybe-no-member
assert attr[ATTR_DIRECTION] == reading.direction # pylint: disable=maybe-no-member
assert attr[ATTR_SVG] == reading.sgv # pylint: disable=maybe-no-member
assert attr[ATTR_ICON] == "mdi:arrow-bottom-right"