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

Introduce conversion to mmol/L in Nightscout #41382

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions homeassistant/components/nightscout/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@
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 .const import DOMAIN # pylint:disable=unused-import
from .const import DOMAIN, MGDL, MMOL # pylint:disable=unused-import
from .utils import hash_from_url

_LOGGER = logging.getLogger(__name__)

DATA_SCHEMA = vol.Schema({vol.Required(CONF_URL): str, vol.Optional(CONF_API_KEY): str})
DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_URL): str,
vol.Optional(CONF_API_KEY): str,
vol.Optional(CONF_UNIT_OF_MEASUREMENT, default=MGDL): vol.In([MGDL, MMOL]),
}
)


async def _validate_input(data):
Expand Down
5 changes: 5 additions & 0 deletions homeassistant/components/nightscout/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@
ATTR_DATE = "date"
ATTR_DELTA = "delta"
ATTR_DIRECTION = "direction"

MMOL_TO_MGDL = 18

MGDL = "mg/dL"
MMOL = "mmol/L"
29 changes: 24 additions & 5 deletions homeassistant/components/nightscout/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@
from py_nightscout import Api as NightscoutAPI

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_UNIT_OF_MEASUREMENT
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity

from .const import ATTR_DATE, ATTR_DELTA, ATTR_DEVICE, ATTR_DIRECTION, DOMAIN
from .const import (
ATTR_DATE,
ATTR_DELTA,
ATTR_DEVICE,
ATTR_DIRECTION,
DOMAIN,
MGDL,
boc-the-git marked this conversation as resolved.
Show resolved Hide resolved
MMOL,
MMOL_TO_MGDL,
)

SCAN_INTERVAL = timedelta(minutes=1)

Expand All @@ -27,20 +37,23 @@ 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)
uom = entry.data.get(CONF_UNIT_OF_MEASUREMENT, MGDL)
async_add_entities(
[NightscoutSensor(api, "Blood Sugar", entry.unique_id, uom)], True
)


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

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

Expand Down Expand Up @@ -94,7 +107,13 @@ async def async_update(self):
ATTR_DELTA: value.delta,
ATTR_DIRECTION: value.direction,
}
self._state = value.sgv
if self._unit_of_measurement == MMOL:
self._state = "%.1f" % (value.sgv / MMOL_TO_MGDL)
self._attributes[ATTR_DELTA] = "%.1f" % (
self._attributes[ATTR_DELTA] / MMOL_TO_MGDL
)
else:
self._state = value.sgv
self._icon = self._parse_icon()
else:
self._available = False
Expand Down
5 changes: 3 additions & 2 deletions homeassistant/components/nightscout/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
"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 (Optional): Only use if your instance is protected (auth_default_roles != readable).\n- Unit of Measurement: Choose between mg/dL or mmol/L.",
"data": {
"url": "[%key:common::config_flow::data::url%]",
"api_key": "[%key:common::config_flow::data::api_key%]"
"api_key": "[%key:common::config_flow::data::api_key%]",
"unit_of_measurement": "Unit of Measurement"
}
}
},
Expand Down
5 changes: 3 additions & 2 deletions homeassistant/components/nightscout/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
"user": {
"data": {
"api_key": "API Key",
"url": "URL"
"url": "URL",
"unit_of_measurement": "Unit of Measurement"
},
"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 (Optional): Only use if your instance is protected (auth_default_roles != readable).\n- Unit of Measurement: Choose between mg/dL or mmol/L.",
"title": "Enter your Nightscout server information."
}
}
Expand Down