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 mmol support to nightscout #126602

Closed
2 changes: 1 addition & 1 deletion homeassistant/components/nightscout/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

_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.Required('Units'): vol.In(["mmol/l", "mg/dl"])})
danielwindit marked this conversation as resolved.
Show resolved Hide resolved


async def _validate_input(data: dict[str, Any]) -> dict[str, str]:
Expand Down
14 changes: 9 additions & 5 deletions homeassistant/components/nightscout/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ 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)

units = entry.data.get("Units")
async_add_entities([NightscoutSensor(api, "Blood Sugar", entry.unique_id, units)], True)

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

_attr_native_unit_of_measurement = "mg/dL"
_attr_icon = "mdi:cloud-question"

def __init__(self, api: NightscoutAPI, name: str, unique_id: str | None) -> None:
def __init__(self, api: NightscoutAPI, name: str, unique_id: str, units: str | None) -> None:
danielwindit marked this conversation as resolved.
Show resolved Hide resolved
"""Initialize the Nightscout sensor."""
self.api = api
self._attr_native_unit_of_measurement = units
danielwindit marked this conversation as resolved.
Show resolved Hide resolved
self._attr_unique_id = unique_id
self._attr_name = name
self._attr_extra_state_attributes: dict[str, Any] = {}
Expand All @@ -67,7 +67,11 @@ async def async_update(self) -> None:
ATTR_DELTA: value.delta,
ATTR_DIRECTION: value.direction,
}
self._attr_native_value = value.sgv
"""Convert to mmol/l and keep 2 decimals"""
if self._attr_native_unit_of_measurement == 'mmol/l':
self._attr_native_value = '{:.2f}'.format(value.sgv * 0.0555)
else:
self._attr_native_value = value.sgv
danielwindit marked this conversation as resolved.
Show resolved Hide resolved
self._attr_icon = self._parse_icon(value.direction)
else:
self._attr_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 @@ -3,10 +3,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. I.e.: https://myhomeassistant.duckdns.org:5423\n- API Key (optional): Only use if your instance is protected (auth_default_roles != readable).\n- Units: Select the unit type to display.",
"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_type": "[%key:common::config_flow::data::units%]"
}
}
},
Expand Down