Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to a different unit of measurement(mmol/L) on Nightscout #58921

9 changes: 7 additions & 2 deletions homeassistant/components/nightscout/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
from py_nightscout import Api as NightscoutAPI

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_KEY, CONF_URL
from homeassistant.const import CONF_API_KEY, CONF_UNIT_OF_MEASUREMENT, CONF_URL
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.entity import SLOW_UPDATE_WARNING

from .const import DOMAIN
from .const import DOMAIN, MG_DL

PLATFORMS = ["sensor"]
_API_TIMEOUT = SLOW_UPDATE_WARNING - 1
Expand All @@ -29,6 +29,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
except (ClientError, AsyncIOTimeoutError, OSError) as error:
raise ConfigEntryNotReady from error

if not entry.options:
hass.config_entries.async_update_entry(
entry, options={CONF_UNIT_OF_MEASUREMENT: MG_DL}
)

hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = api

Expand Down
36 changes: 34 additions & 2 deletions homeassistant/components/nightscout/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
import voluptuous as vol

from homeassistant import config_entries, exceptions
from homeassistant.const import CONF_API_KEY, CONF_URL
from homeassistant.const import CONF_API_KEY, CONF_UNIT_OF_MEASUREMENT, CONF_URL
from homeassistant.core import callback

from .const import DOMAIN
from .const import DOMAIN, MG_DL, MMOL_L
from .utils import hash_from_url

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -62,6 +63,37 @@ async def async_step_user(self, user_input=None):
step_id="user", data_schema=DATA_SCHEMA, errors=errors
)

@staticmethod
@callback
def async_get_options_flow(config_entry):
"""Get the options flow for this handler."""
return NightscoutOptionsFlowHandler(config_entry)


class NightscoutOptionsFlowHandler(config_entries.OptionsFlow):
"""Handle a option flow for Nightscout."""

def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize options flow."""
self.config_entry = config_entry

async def async_step_init(self, user_input=None):
"""Handle options flow."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)

data_schema = vol.Schema(
{
vol.Optional(
CONF_UNIT_OF_MEASUREMENT,
default=self.config_entry.options.get(
CONF_UNIT_OF_MEASUREMENT, MG_DL
),
): vol.In({MG_DL, MMOL_L}),
}
)
return self.async_show_form(step_id="init", data_schema=data_schema)


class InputValidationError(exceptions.HomeAssistantError):
"""Error to indicate we cannot proceed due to invalid input."""
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/nightscout/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

DOMAIN = "nightscout"

MMOL_L = "mmol/L"
MG_DL = "mg/dL"

ATTR_DEVICE = "device"
ATTR_SGV = "sgv"
ATTR_DELTA = "delta"
ATTR_DIRECTION = "direction"
2 changes: 1 addition & 1 deletion homeassistant/components/nightscout/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Nightscout",
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/nightscout",
"requirements": ["py-nightscout==1.2.2"],
"requirements": ["py-nightscout==1.3.2"],
"codeowners": ["@marciogranzotto"],
"quality_scale": "platinum",
"iot_class": "cloud_polling"
Expand Down
21 changes: 13 additions & 8 deletions homeassistant/components/nightscout/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@

from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_DATE
from homeassistant.const import ATTR_DATE, CONF_UNIT_OF_MEASUREMENT
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import ATTR_DELTA, ATTR_DEVICE, ATTR_DIRECTION, DOMAIN
from .const import ATTR_DELTA, ATTR_DEVICE, ATTR_DIRECTION, DOMAIN, MMOL_L

SCAN_INTERVAL = timedelta(minutes=1)

_LOGGER = logging.getLogger(__name__)

DEFAULT_NAME = "Blood Glucose"
MMOL_CONVERSION_FACTOR = 18


async def async_setup_entry(
Expand All @@ -30,20 +31,20 @@ async def async_setup_entry(
) -> None:
"""Set up the Glucose Sensor."""
api = hass.data[DOMAIN][entry.entry_id]
async_add_entities([NightscoutSensor(api, "Blood Sugar", entry.unique_id)], True)
async_add_entities([NightscoutSensor(api, "Blood Sugar", entry)], True)


class NightscoutSensor(SensorEntity):
"""Implementation of a Nightscout sensor."""

def __init__(self, api: NightscoutAPI, name, unique_id):
def __init__(self, api: NightscoutAPI, name, entry):
"""Initialize the Nightscout sensor."""
self.api = api
self._unique_id = unique_id
self._unique_id = entry.unique_id
self._name = name
self._state = None
self._attributes = None
self._unit_of_measurement = "mg/dL"
self._unit_of_measurement = entry.options[CONF_UNIT_OF_MEASUREMENT]
self._icon = "mdi:cloud-question"
self._available = False

Expand Down Expand Up @@ -94,10 +95,14 @@ async def async_update(self):
self._attributes = {
ATTR_DEVICE: value.device,
ATTR_DATE: value.date,
ATTR_DELTA: value.delta,
ATTR_DIRECTION: value.direction,
}
self._state = value.sgv
if self._unit_of_measurement == MMOL_L:
self._state = value.sgv_mmol
self._attributes[ATTR_DELTA] = value.delta_mmol
else:
self._state = value.sgv
self._attributes[ATTR_DELTA] = value.delta
self._icon = self._parse_icon()
else:
self._available = False
Expand Down
11 changes: 10 additions & 1 deletion homeassistant/components/nightscout/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"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).",
"description": "- URL: the address of your nightscout instance. e.g.: https://myhomeassistant.duckdns.org:5423\n- API Key/Access Token (optional): Only use if your instance is protected (auth_default_roles != readable).\nIt is recommended NOT to use your API_SECRET but instead generate an access token. See https://nightscout.github.io/nightscout/security/#create-authentication-tokens-for-users for details.\nA 'subject' (person/device) with the 'readable' permission is sufficient for this integration.",
"data": {
"url": "[%key:common::config_flow::data::url%]",
"api_key": "[%key:common::config_flow::data::api_key%]"
Expand All @@ -18,5 +18,14 @@
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
}
},
"options": {
"step": {
"init": {
"data": {
"unit_of_measurement": "Unit of measurement"
}
}
}
}
}
13 changes: 11 additions & 2 deletions homeassistant/components/nightscout/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@
"api_key": "API Key",
"url": "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).",
"description": "- URL: the address of your nightscout instance. e.g.: https://myhomeassistant.duckdns.org:5423\n- API Key/Access Token (optional): Only use if your instance is protected (auth_default_roles != readable).\nIt is recommended NOT to use your API_SECRET but instead generate an access token. See https://nightscout.github.io/nightscout/security/#create-authentication-tokens-for-users for details.\nA 'subject' (person/device) with the 'readable' permission is sufficient for this integration.",
"title": "Enter your Nightscout server information."
}
}
},
"options": {
"step": {
"init": {
"data": {
"unit_of_measurement": "Unit of measurement"
}
}
}
}
}
}
11 changes: 10 additions & 1 deletion homeassistant/components/nightscout/translations/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,14 @@
}
}
}
}
},
"options": {
"step": {
"init": {
"data": {
"unit_of_measurement": "Unidade de medida"
}
}
}
}
}
11 changes: 10 additions & 1 deletion homeassistant/components/nightscout/translations/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,14 @@
}
}
}
}
},
"options": {
"step": {
"init": {
"data": {
"unit_of_measurement": "Unidade de medida"
}
}
}
}
}
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ py-cpuinfo==8.0.0
py-melissa-climate==2.1.4

# homeassistant.components.nightscout
py-nightscout==1.2.2
py-nightscout==1.3.2

# homeassistant.components.schluter
py-schluter==0.1.7
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ py-canary==0.5.1
py-melissa-climate==2.1.4

# homeassistant.components.nightscout
py-nightscout==1.2.2
py-nightscout==1.3.2

# homeassistant.components.synology_dsm
py-synologydsm-api==1.0.4
Expand Down
19 changes: 16 additions & 3 deletions tests/components/nightscout/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
from aiohttp import ClientConnectionError
from py_nightscout.models import SGV, ServerStatus

from homeassistant.components.nightscout.const import DOMAIN
from homeassistant.const import CONF_URL
from homeassistant.components.nightscout.const import (
ATTR_DELTA,
ATTR_SGV,
DOMAIN,
MG_DL,
)
from homeassistant.const import CONF_UNIT_OF_MEASUREMENT, CONF_URL

from tests.common import MockConfigEntry

Expand All @@ -17,6 +22,9 @@
)
)
]

CONVERTED_MMOL_VALUES = {ATTR_SGV: 9.4, ATTR_DELTA: -0.3}

SERVER_STATUS = ServerStatus.new_from_json_dict(
json.loads(
'{"status":"ok","name":"nightscout","version":"13.0.1","serverTime":"2020-08-05T18:14:02.032Z","serverTimeEpoch":1596651242032,"apiEnabled":true,"careportalEnabled":true,"boluscalcEnabled":true,"settings":{},"extendedSettings":{},"authorized":null}'
Expand All @@ -29,11 +37,16 @@
)


async def init_integration(hass) -> MockConfigEntry:
async def init_integration(hass, unit_of_measurement=MG_DL) -> MockConfigEntry:
"""Set up the Nightscout integration in Home Assistant."""
options = {}
if unit_of_measurement:
options[CONF_UNIT_OF_MEASUREMENT] = unit_of_measurement

entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_URL: "https://some.url:1234"},
options=options,
)
with patch(
"homeassistant.components.nightscout.NightscoutAPI.get_sgvs",
Expand Down
52 changes: 50 additions & 2 deletions tests/components/nightscout/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from aiohttp import ClientConnectionError, ClientResponseError

from homeassistant import config_entries, data_entry_flow
from homeassistant.components.nightscout.const import DOMAIN
from homeassistant.components.nightscout.const import DOMAIN, MG_DL, MMOL_L
from homeassistant.components.nightscout.utils import hash_from_url
from homeassistant.const import CONF_URL
from homeassistant.const import CONF_UNIT_OF_MEASUREMENT, CONF_URL

from tests.common import MockConfigEntry
from tests.components.nightscout import (
Expand Down Expand Up @@ -116,6 +116,54 @@ async def test_user_form_duplicate(hass):
assert result["reason"] == "already_configured"


async def test_option_flow_default(hass):
"""Test config flow options."""
entry = MockConfigEntry(
domain=DOMAIN,
data=CONFIG,
options=None,
)
entry.add_to_hass(hass)

result = await hass.config_entries.options.async_init(entry.entry_id)

assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "init"

result2 = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={},
)
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result2["data"] == {
CONF_UNIT_OF_MEASUREMENT: MG_DL,
}


async def test_option_flow(hass):
"""Test config flow options."""
entry = MockConfigEntry(
domain=DOMAIN,
data=CONFIG,
options={CONF_UNIT_OF_MEASUREMENT: MG_DL},
)
entry.add_to_hass(hass)

result = await hass.config_entries.options.async_init(entry.entry_id)

assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "init"

result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={CONF_UNIT_OF_MEASUREMENT: MMOL_L},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["data"] == {
CONF_UNIT_OF_MEASUREMENT: MMOL_L,
}


def _patch_async_setup_entry():
return patch(
"homeassistant.components.nightscout.async_setup_entry",
Expand Down
Loading