Skip to content
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
13 changes: 9 additions & 4 deletions homeassistant/components/nightscout/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@
import voluptuous as vol

from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_API_KEY, CONF_URL
from homeassistant.const import CONF_API_KEY, CONF_UNIT_OF_MEASUREMENT, CONF_URL
from homeassistant.exceptions import HomeAssistantError

from .const import DOMAIN
from .const import DOMAIN, MG_DL, MMOL_L
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=MG_DL): vol.In([MG_DL, MMOL_L]),
}
)

async def _validate_input(data: dict[str, Any]) -> dict[str, str]:
"""Validate the user input allows us to connect."""
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/nightscout/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
ATTR_DEVICE = "device"
ATTR_DELTA = "delta"
ATTR_DIRECTION = "direction"
MMOL_L = "mmol/L"
MG_DL = "mg/dL"
20 changes: 14 additions & 6 deletions homeassistant/components/nightscout/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

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, MG_DL

SCAN_INTERVAL = timedelta(minutes=1)

Expand All @@ -31,18 +31,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)
unit = entry.data.get(CONF_UNIT_OF_MEASUREMENT, MG_DL)
async_add_entities(
[NightscoutSensor(api, "Blood Sugar", entry.unique_id, unit)], 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 | None, unit: str
) -> None:
"""Initialize the Nightscout sensor."""
self.api = api
self._attr_native_unit_of_measurement = unit
self._attr_unique_id = unique_id
self._attr_name = name
self._attr_extra_state_attributes: dict[str, Any] = {}
Expand All @@ -67,7 +72,10 @@ async def async_update(self) -> None:
ATTR_DELTA: value.delta,
ATTR_DIRECTION: value.direction,
}
self._attr_native_value = value.sgv
if self._attr_native_unit_of_measurement == MMOL_L:
self._attr_native_value = round(value.sgv * 0.0555, 1)
else:
self._attr_native_value = value.sgv
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_of_measurement": "[%key:common::config_flow::data::unit_of_measurement%]"
}
}
},
Expand Down