From dc5c2ce9f4592b394e9e30eb05a68041c5caa0db Mon Sep 17 00:00:00 2001 From: boc-the-git Date: Wed, 7 Oct 2020 09:19:31 +1100 Subject: [PATCH 1/5] Convert mg/dL to mmol/L --- .../components/nightscout/config_flow.py | 12 ++++++-- homeassistant/components/nightscout/const.py | 5 ++++ homeassistant/components/nightscout/sensor.py | 28 +++++++++++++++---- .../components/nightscout/strings.json | 5 ++-- .../nightscout/translations/en.json | 5 ++-- 5 files changed, 43 insertions(+), 12 deletions(-) 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..c998a839ee3273 100644 --- a/homeassistant/components/nightscout/sensor.py +++ b/homeassistant/components/nightscout/sensor.py @@ -8,10 +8,19 @@ 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_TO_MGDL, +) SCAN_INTERVAL = timedelta(minutes=1) @@ -27,20 +36,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) + 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 +106,13 @@ async def async_update(self): ATTR_DELTA: value.delta, ATTR_DIRECTION: value.direction, } - self._state = value.sgv + if self._unit_of_measurement == MGDL: + self._state = value.sgv + else: + self._state = value.sgv / MMOL_TO_MGDL + self._attributes[ATTR_DELTA] = ( + self._attributes[ATTR_DELTA] / MMOL_TO_MGDL + ) 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." } } From 247c2f3d5e59fa511b417c017af4aae060e06123 Mon Sep 17 00:00:00 2001 From: boc-the-git Date: Wed, 7 Oct 2020 18:15:55 +1100 Subject: [PATCH 2/5] Round mmol values --- homeassistant/components/nightscout/sensor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/nightscout/sensor.py b/homeassistant/components/nightscout/sensor.py index c998a839ee3273..e34858b6b9f815 100644 --- a/homeassistant/components/nightscout/sensor.py +++ b/homeassistant/components/nightscout/sensor.py @@ -109,8 +109,8 @@ async def async_update(self): if self._unit_of_measurement == MGDL: self._state = value.sgv else: - self._state = value.sgv / MMOL_TO_MGDL - self._attributes[ATTR_DELTA] = ( + self._state = "%.1f" % (value.sgv / MMOL_TO_MGDL) + self._attributes[ATTR_DELTA] = "%.1f" % ( self._attributes[ATTR_DELTA] / MMOL_TO_MGDL ) self._icon = self._parse_icon() From 99210835831f34ec0cceb874680e77af9338d108 Mon Sep 17 00:00:00 2001 From: boc-the-git <3479092+boc-the-git@users.noreply.github.com> Date: Thu, 8 Oct 2020 08:01:06 +1100 Subject: [PATCH 3/5] Update homeassistant/components/nightscout/sensor.py Co-authored-by: Marcio Granzotto Rodrigues --- homeassistant/components/nightscout/sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/nightscout/sensor.py b/homeassistant/components/nightscout/sensor.py index e34858b6b9f815..666d72b705a8bb 100644 --- a/homeassistant/components/nightscout/sensor.py +++ b/homeassistant/components/nightscout/sensor.py @@ -36,7 +36,7 @@ async def async_setup_entry( ) -> None: """Set up the Glucose Sensor.""" api = hass.data[DOMAIN][entry.entry_id] - uom = entry.data.get(CONF_UNIT_OF_MEASUREMENT) + uom = entry.data.get(CONF_UNIT_OF_MEASUREMENT, MGDL) async_add_entities( [NightscoutSensor(api, "Blood Sugar", entry.unique_id, uom)], True ) From 8c4cb9c9d34cb775f14d58c8f03c3e68fb9466eb Mon Sep 17 00:00:00 2001 From: boc-the-git <3479092+boc-the-git@users.noreply.github.com> Date: Thu, 8 Oct 2020 08:01:19 +1100 Subject: [PATCH 4/5] Update homeassistant/components/nightscout/sensor.py Co-authored-by: Marcio Granzotto Rodrigues --- homeassistant/components/nightscout/sensor.py | 1 + 1 file changed, 1 insertion(+) diff --git a/homeassistant/components/nightscout/sensor.py b/homeassistant/components/nightscout/sensor.py index 666d72b705a8bb..eee8c45d062288 100644 --- a/homeassistant/components/nightscout/sensor.py +++ b/homeassistant/components/nightscout/sensor.py @@ -19,6 +19,7 @@ ATTR_DIRECTION, DOMAIN, MGDL, + MMOL, MMOL_TO_MGDL, ) From 87770a92da1f31be92034c9d4e6b447555d591dd Mon Sep 17 00:00:00 2001 From: boc-the-git <3479092+boc-the-git@users.noreply.github.com> Date: Thu, 8 Oct 2020 08:04:25 +1100 Subject: [PATCH 5/5] Update homeassistant/components/nightscout/sensor.py Co-authored-by: Marcio Granzotto Rodrigues --- homeassistant/components/nightscout/sensor.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/nightscout/sensor.py b/homeassistant/components/nightscout/sensor.py index eee8c45d062288..fd77197bf3f880 100644 --- a/homeassistant/components/nightscout/sensor.py +++ b/homeassistant/components/nightscout/sensor.py @@ -107,13 +107,13 @@ async def async_update(self): ATTR_DELTA: value.delta, ATTR_DIRECTION: value.direction, } - if self._unit_of_measurement == MGDL: - self._state = value.sgv - else: + 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