Add authentication support to Nightscout (#40602)

* Add API Key to the Nightscout integration config

* Add tests for nightscout config changes

* Apply suggestions from code review

Co-authored-by: springstan <46536646+springstan@users.noreply.github.com>
Co-authored-by: Chris Talkington <chris@talkingtontech.com>

* Update homeassistant/components/nightscout/__init__.py

* Run translations script to fix en.json

Co-authored-by: springstan <46536646+springstan@users.noreply.github.com>
Co-authored-by: Chris Talkington <chris@talkingtontech.com>
This commit is contained in:
authored and GitHub committed 2020-09-27 10:22:28 -05:00
1 parent 6028953eca
commit 3fba4274f5
9 files changed
+63 -18

No files matched your search

@@ -7,7 +7,7 @@ from aiohttp import ClientError
from py_nightscout import Api as NightscoutAPI
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_URL
from homeassistant.const import CONF_API_KEY, CONF_URL
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr
@@ -30,8 +30,9 @@ async def async_setup(hass: HomeAssistant, config: dict):
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up Nightscout from a config entry."""
server_url = entry.data[CONF_URL]
api_key = entry.data.get(CONF_API_KEY)
session = async_get_clientsession(hass)
api = NightscoutAPI(server_url, session=session)
api = NightscoutAPI(server_url, session=session, api_secret=api_key)
try:
status = await api.get_server_status()
except (ClientError, AsyncIOTimeoutError, OSError) as error:
@@ -2,27 +2,32 @@
from asyncio import TimeoutError as AsyncIOTimeoutError
import logging
from aiohttp import ClientError
from aiohttp import ClientError, ClientResponseError
from py_nightscout import Api as NightscoutAPI
import voluptuous as vol
from homeassistant import config_entries, exceptions
from homeassistant.const import CONF_URL
from homeassistant.const import CONF_API_KEY, CONF_URL
from .const import DOMAIN # pylint:disable=unused-import
from .utils import hash_from_url
_LOGGER = logging.getLogger(__name__)
DATA_SCHEMA = vol.Schema({vol.Required(CONF_URL): str})
DATA_SCHEMA = vol.Schema({vol.Required(CONF_URL): str, vol.Optional(CONF_API_KEY): str})
async def _validate_input(data):
"""Validate the user input allows us to connect."""
url = data[CONF_URL]
api_key = data.get(CONF_API_KEY)
try:
api = NightscoutAPI(url)
api = NightscoutAPI(url, api_secret=api_key)
status = await api.get_server_status()
if status.settings.get("authDefaultRoles") == "status-only":
await api.get_sgvs()
except ClientResponseError as error:
raise InputValidationError("invalid_auth") from error
except (ClientError, AsyncIOTimeoutError, OSError) as error:
raise InputValidationError("cannot_connect") from error
@@ -4,7 +4,7 @@
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/nightscout",
"requirements": [
"py-nightscout==1.2.1"
"py-nightscout==1.2.2"
],
"codeowners": [
"@marciogranzotto"
@@ -3,12 +3,16 @@
"flow_title": "Nightscout",
"step": {
"user": {
"title": "Enter your Nightscout server information.",
"description": "- URL: the address of your nightscout instance. I.e.: https://myhomeassistant.duckdns.org:5423\n- API Key (Optional): Only use if your instance is protected (auth_default_roles != readable).",
"data": {
"url": "URL"
"url": "[%key:common::config_flow::data::url%]",
"api_key": "[%key:common::config_flow::data::api_key%]"
}
}
},
"error": {
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"unknown": "[%key:common::config_flow::error::unknown%]"
},
@@ -16,4 +20,4 @@
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
}
}
}
}
@@ -1,18 +1,22 @@
{
"config": {
"abort": {
"already_configured": "Device is already configured"
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
},
"error": {
"cannot_connect": "Failed to connect",
"unknown": "Unexpected error"
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
"unknown": "[%key:common::config_flow::error::unknown%]"
},
"flow_title": "Nightscout",
"step": {
"user": {
"data": {
"url": "URL"
}
"api_key": "[%key:common::config_flow::data::api_key%]",
"url": "[%key:common::config_flow::data::url%]"
},
"description": "- URL: the address of your nightscout instance. I.e.: https://myhomeassistant.duckdns.org:5423\n- API Key (Optional): Only use if your instance is protected (auth_default_roles != readable).",
"title": "Enter your Nightscout server information."
}
}
}