Skip to content

Commit

Permalink
Custom library url (#2416)
Browse files Browse the repository at this point in the history
* Add custom library url

* Update library url
  • Loading branch information
andrew-codechimp authored Nov 20, 2024
1 parent 1fbd507 commit b6e76c2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
7 changes: 7 additions & 0 deletions custom_components/battery_notes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
CONF_ENABLE_AUTODISCOVERY,
CONF_ENABLE_REPLACED,
CONF_HIDE_BATTERY,
CONF_LIBRARY_URL,
CONF_ROUND_BATTERY,
CONF_SHOW_ALL_DEVICES,
CONF_USER_LIBRARY,
Expand All @@ -39,6 +40,7 @@
DATA_STORE,
DEFAULT_BATTERY_INCREASE_THRESHOLD,
DEFAULT_BATTERY_LOW_THRESHOLD,
DEFAULT_LIBRARY_URL,
DOMAIN,
DOMAIN_CONFIG,
MIN_HA_VERSION,
Expand Down Expand Up @@ -75,6 +77,10 @@
CONF_BATTERY_INCREASE_THRESHOLD,
default=DEFAULT_BATTERY_INCREASE_THRESHOLD,
): cv.positive_int,
vol.Optional(
CONF_LIBRARY_URL,
default=DEFAULT_LIBRARY_URL,
): cv.string,
},
),
),
Expand Down Expand Up @@ -111,6 +117,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
CONF_ROUND_BATTERY: False,
CONF_DEFAULT_BATTERY_LOW_THRESHOLD: DEFAULT_BATTERY_LOW_THRESHOLD,
CONF_BATTERY_INCREASE_THRESHOLD: DEFAULT_BATTERY_INCREASE_THRESHOLD,
CONF_LIBRARY_URL: DEFAULT_LIBRARY_URL,
}

hass.data[DOMAIN] = {
Expand Down
3 changes: 2 additions & 1 deletion custom_components/battery_notes/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

DEFAULT_BATTERY_LOW_THRESHOLD = 10
DEFAULT_BATTERY_INCREASE_THRESHOLD = 25
DEFAULT_LIBRARY_URL = "https://raw.githubusercontent.com/andrew-codechimp/HA-Battery-Notes/main/custom_components/battery_notes/data/library.json" # pylint: disable=line-too-long

CONF_SOURCE_ENTITY_ID = "source_entity_id"
CONF_BATTERY_TYPE = "battery_type"
Expand All @@ -42,7 +43,7 @@
CONF_MODEL_ID = "model_id"
CONF_MANUFACTURER = "manufacturer"
CONF_DEVICE_NAME = "device_name"
CONF_LIBRARY_URL = "https://raw.githubusercontent.com/andrew-codechimp/HA-Battery-Notes/main/custom_components/battery_notes/data/library.json" # pylint: disable=line-too-long
CONF_LIBRARY_URL = "library_url"
CONF_SHOW_ALL_DEVICES = "show_all_devices"
CONF_ENABLE_REPLACED = "enable_replaced"
CONF_DEFAULT_BATTERY_LOW_THRESHOLD = "default_battery_low_threshold"
Expand Down
20 changes: 15 additions & 5 deletions custom_components/battery_notes/library_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
CONF_ENABLE_AUTODISCOVERY,
CONF_LIBRARY_URL,
DATA_LIBRARY_LAST_UPDATE,
DEFAULT_LIBRARY_URL,
DOMAIN,
DOMAIN_CONFIG,
)
Expand All @@ -44,7 +45,14 @@ class LibraryUpdater:
def __init__(self, hass: HomeAssistant):
"""Initialize the library updater."""
self.hass = hass
self._client = LibraryUpdaterClient(session=async_get_clientsession(hass))

if DOMAIN_CONFIG in self.hass.data[DOMAIN]:
domain_config: dict = self.hass.data[DOMAIN][DOMAIN_CONFIG]
url = domain_config.get(CONF_LIBRARY_URL, DEFAULT_LIBRARY_URL)
else:
url = DEFAULT_LIBRARY_URL

self._client = LibraryUpdaterClient(library_url=url, session=async_get_clientsession(hass))

# Fire the library check every 24 hours from now
async_track_utc_time_change(
Expand Down Expand Up @@ -103,8 +111,7 @@ def _update_library_json(library_file: str, content: str) -> dict[str, Any]:

except LibraryUpdaterClientError:
_LOGGER.warning(
"Unable to update library, this could be a GitHub or internet "
"connectivity issue, will retry later."
"Unable to update library, will retry later."
)

async def time_to_update_library(self) -> bool:
Expand Down Expand Up @@ -146,14 +153,17 @@ class LibraryUpdaterClient:

def __init__(
self,
library_url: str,
session: aiohttp.ClientSession,
) -> None:
"""Client to get latest library file from GitHub."""
self._library_url = library_url
self._session = session

async def async_get_data(self) -> any:
"""Get data from the API."""
return await self._api_wrapper(method="get", url=CONF_LIBRARY_URL)
"""Get data from the hosted library."""
_LOGGER.debug(f"Updating library from {self._library_url}")
return await self._api_wrapper(method="get", url=self._library_url)

async def _api_wrapper(
self,
Expand Down

0 comments on commit b6e76c2

Please sign in to comment.