Skip to content

Commit

Permalink
Merge pull request #29 from PTST/Region-support
Browse files Browse the repository at this point in the history
Added region support
  • Loading branch information
PTST authored May 30, 2024
2 parents 494009e + c048a0a commit fca8f0d
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ ignored-modules=voluptuous,homeassistant,LibreView
disable=missing-docstring,too-few-public-methods

[SIMILARITIES]
min-similarity-lines = 8
min-similarity-lines = 25
ignore-imports = true
2 changes: 1 addition & 1 deletion custom_components/libreview/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

async def update_listener(hass: HomeAssistant, entry: ConfigEntry):
LOGGER.error("config options were changed")
LOGGER.debug("config options were changed")
await hass.config_entries.async_reload(entry.entry_id)

entry.async_on_unload(entry.add_update_listener(update_listener))
Expand Down
48 changes: 43 additions & 5 deletions custom_components/libreview/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from LibreView import LibreView

from .const import (
CONF_REGION,
CONF_SENSOR_DURATION,
CONF_SHOW_TREND_ARROW,
CONF_UOM,
Expand All @@ -29,9 +30,9 @@ async def async_step_init(
) -> FlowResult:
"""Manage the options."""
if user_input is not None:
for k, v in self.entry.data.items():
if k not in user_input.keys():
user_input[k] = v
for key, value in self.entry.data.items():
if key not in user_input.keys():
user_input[key] = value
self.hass.config_entries.async_update_entry(
self.entry, data=user_input, options=self.entry.options
)
Expand All @@ -50,6 +51,10 @@ async def async_step_init(
if self.entry.data.get(CONF_SHOW_TREND_ARROW) is not None:
default_show_trend = bool(self.entry.data.get(CONF_SHOW_TREND_ARROW))

default_region = "Standard"
if self.entry.data.get(CONF_REGION) is not None:
default_region = self.entry.data.get(CONF_REGION)

return self.async_show_form(
step_id="init",
data_schema=vol.Schema(
Expand All @@ -61,6 +66,21 @@ async def async_step_init(
vol.Required(
CONF_SHOW_TREND_ARROW, default=default_show_trend
): bool,
vol.Optional(CONF_REGION, default=default_region): vol.In(
[
"Standard",
"AE",
"AP",
"AU",
"CA",
"DE",
"EU",
"EU2",
"FR",
"JP",
"US",
]
),
}
),
)
Expand All @@ -77,6 +97,7 @@ class LibreViewConfigFlowHandler(ConfigFlow, domain=DOMAIN):
entry: ConfigEntry
password: str
connections: Dict[str, str]
region: str

async def async_step_user(
self, user_input: dict[str, Any] | None = None
Expand All @@ -91,7 +112,7 @@ async def async_step_user(
username=user_input[CONF_EMAIL], password=user_input[CONF_PASSWORD]
)
await self.hass.async_add_executor_job(libre.get_connections)
except Exception as ex: # pylint: disable=broad-exception-caught
except Exception as ex: # pylint: disable=broad-except
LOGGER.debug("Could not log in to LibreView, %s", ex)
errors["base"] = "invalid_auth"
else:
Expand All @@ -117,6 +138,7 @@ async def async_step_options(
self.uom = user_input[CONF_UOM]
self.sensor_duration = user_input[CONF_SENSOR_DURATION]
self.show_trend_icon = user_input[CONF_SHOW_TREND_ARROW]
self.region = user_input[CONF_REGION]
return self.async_create_entry(
title="LibreView",
data={
Expand All @@ -125,6 +147,7 @@ async def async_step_options(
CONF_UOM: self.uom,
CONF_SENSOR_DURATION: int(self.sensor_duration),
CONF_SHOW_TREND_ARROW: bool(self.show_trend_icon),
CONF_REGION: self.region,
},
)
return self.async_show_form(
Expand All @@ -134,6 +157,21 @@ async def async_step_options(
vol.Required(CONF_UOM): vol.In(GlucoseUnitOfMeasurement),
vol.Required(CONF_SENSOR_DURATION, default=14): int,
vol.Required(CONF_SHOW_TREND_ARROW, default=False): bool,
vol.Optional(CONF_REGION, default="Standard"): vol.In(
[
"Standard",
"AE",
"AP",
"AU",
"CA",
"DE",
"EU",
"EU2",
"FR",
"JP",
"US",
]
),
},
),
)
Expand All @@ -156,7 +194,7 @@ async def async_step_reauth_confirm(
username=user_input[CONF_EMAIL], password=user_input[CONF_PASSWORD]
)
await self.hass.async_add_executor_job(libre.get_connections)
except Exception as ex: # pylint: disable=broad-exception-caught
except Exception as ex: # pylint: disable=broad-except
LOGGER.debug("Could not log in to LibreView, %s", ex)
errors["base"] = "invalid_auth"
else:
Expand Down
1 change: 1 addition & 0 deletions custom_components/libreview/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
CONF_UOM = "uom"
CONF_SENSOR_DURATION = "sensor_duration"
CONF_SHOW_TREND_ARROW = "show_trend_arrow"
CONF_REGION = "region"


DEFAULT_ICON = "mdi:diabetes"
Expand Down
10 changes: 8 additions & 2 deletions custom_components/libreview/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,27 @@ class LibreViewCoordinator(DataUpdateCoordinator):

def __init__(self, hass: HomeAssistant, entry: ConfigEntry):
self.entry = entry
region = entry.data.get("region", None)
region = region if region and len(region) == 2 else None
self.libre = LibreView(
username=entry.data[CONF_EMAIL], password=entry.data[CONF_PASSWORD]
username=entry.data[CONF_EMAIL],
password=entry.data[CONF_PASSWORD],
region=region,
)
super().__init__(
hass, LOGGER, name=DOMAIN, update_interval=DEFAULT_SCAN_INTERVAL
)

async def _async_update_data(self) -> dict:
try:
LOGGER.debug("Updating data")
await self.hass.async_add_executor_job(self.libre.get_connections)
except Exception as ex:
LOGGER.error("Could not update status, %s", ex)
LOGGER.error(ex)
raise

# Store data in a way Home Assistant can easily consume it
LOGGER.debug(self.libre.connections_dict)
data = {
"glucose_readings": self.libre.connections_dict,
}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/libreview/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"documentation": "https://github.com/PTST/LibreView-HomeAssistant",
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/PTST/LibreView-HomeAssistant/issues",
"requirements": ["LibreView==0.1.2"],
"requirements": ["LibreView==0.1.9"],
"version": "1.0.0"
}
6 changes: 4 additions & 2 deletions custom_components/libreview/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"data": {
"uom": "Unit of measurement for blood glucose measurements",
"sensor_duration": "How many days do your Libre Sensor stay active after application",
"show_trend_arrow": "Use icon to show current trend of the glucose level"
"show_trend_arrow": "Use icon to show current trend of the glucose level",
"region": "Region of the api server to use, leave at standard to auto guess"
}
},
"reauth_confirm": {
Expand Down Expand Up @@ -43,7 +44,8 @@
"data": {
"uom": "Unit of measurement for blood glucose measurements",
"sensor_duration": "How many days do your Libre Sensor stay active after application",
"show_trend_arrow": "Use icon to show current trend of the glucose level"
"show_trend_arrow": "Use icon to show current trend of the glucose level",
"region": "Region of the api server to use, leave at standard to auto guess"
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions custom_components/libreview/translations/da.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"data": {
"uom": "Måleenhed for blodsukkermålinger",
"sensor_duration": "Hvor mange dage er din Libre sensor aktiv efter påsætning",
"show_trend_arrow": "Brug ikon til at vise glukosetendens"
"show_trend_arrow": "Brug ikon til at vise glukosetendens",
"region": "Region for API serveren, hvis sat til Standard auto findes denne"
}
},
"reauth_confirm": {
Expand Down Expand Up @@ -43,7 +44,8 @@
"data": {
"uom": "Måleenhed for blodsukkermålinger",
"sensor_duration": "Hvor mange dage er din Libre sensor aktiv efter påsætning",
"show_trend_arrow": "Brug ikon til at vise glukosetendens"
"show_trend_arrow": "Brug ikon til at vise glukosetendens",
"region": "Region for API serveren, hvis sat til Standard auto findes denne"
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions custom_components/libreview/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"data": {
"uom": "Unit of measurement for blood glucose measurements",
"sensor_duration": "How many days do your Libre Sensor stay active after application",
"show_trend_arrow": "Use icon to show current trend of the glucose level"
"show_trend_arrow": "Use icon to show current trend of the glucose level",
"region": "Region of the api server to use, leave at standard to auto guess"
}
},
"reauth_confirm": {
Expand Down Expand Up @@ -43,7 +44,8 @@
"data": {
"uom": "Unit of measurement for blood glucose measurements",
"sensor_duration": "How many days do your Libre Sensor stay active after application",
"show_trend_arrow": "Use icon to show current trend of the glucose level"
"show_trend_arrow": "Use icon to show current trend of the glucose level",
"region": "Region of the api server to use, leave at standard to auto guess"
}
}
}
Expand Down

0 comments on commit fca8f0d

Please sign in to comment.