diff --git a/homeassistant/components/nightscout/config_flow.py b/homeassistant/components/nightscout/config_flow.py index 3000d652e46f87..2590d0cdacd5b0 100644 --- a/homeassistant/components/nightscout/config_flow.py +++ b/homeassistant/components/nightscout/config_flow.py @@ -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): diff --git a/homeassistant/components/nightscout/const.py b/homeassistant/components/nightscout/const.py index 4bb96a94c2971f..fec72b00570d22 100644 --- a/homeassistant/components/nightscout/const.py +++ b/homeassistant/components/nightscout/const.py @@ -6,3 +6,8 @@ ATTR_DATE = "date" ATTR_DELTA = "delta" ATTR_DIRECTION = "direction" + +MMOL_TO_MGDL = 18 + +MGDL = "mg/dL" +MMOL = "mmol/L" diff --git a/homeassistant/components/nightscout/sensor.py b/homeassistant/components/nightscout/sensor.py index f4ff14d7b2a7ea..fd77197bf3f880 100644 --- a/homeassistant/components/nightscout/sensor.py +++ b/homeassistant/components/nightscout/sensor.py @@ -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, + MMOL, + MMOL_TO_MGDL, +) SCAN_INTERVAL = timedelta(minutes=1) @@ -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 @@ -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 diff --git a/homeassistant/components/nightscout/strings.json b/homeassistant/components/nightscout/strings.json index 2240bcec02b571..86a223d23ab846 100644 --- a/homeassistant/components/nightscout/strings.json +++ b/homeassistant/components/nightscout/strings.json @@ -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" } } }, diff --git a/homeassistant/components/nightscout/translations/en.json b/homeassistant/components/nightscout/translations/en.json index d8b4c441283ec7..0edba02099f29d 100644 --- a/homeassistant/components/nightscout/translations/en.json +++ b/homeassistant/components/nightscout/translations/en.json @@ -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." } }