Skip to content

Commit

Permalink
Merge pull request #189 from g4bri3lDev/fetch_tagtypes_from_main_repo
Browse files Browse the repository at this point in the history
Removed hw_map.py
  • Loading branch information
g4bri3lDev authored Oct 27, 2024
2 parents 713885d + 78785c4 commit 1065e38
Show file tree
Hide file tree
Showing 8 changed files with 643 additions and 184 deletions.
20 changes: 20 additions & 0 deletions custom_components/open_epaper_link/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pprint
import time

from .tag_types import get_tag_types_manager
from .util import send_tag_cmd, reboot_ap

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -138,6 +139,24 @@ async def scan_channels_service(service: ServiceCall)-> None:
async def reboot_ap_service(service: ServiceCall)-> None:
await reboot_ap(hass)

async def refresh_tag_types_service(service: ServiceCall) -> None:
"""Service to force refresh of tag types."""
manager = await get_tag_types_manager(hass)
# Force a refresh by clearing the last update timestamp
manager._last_update = None
await manager.ensure_types_loaded()
tag_types_len = len(manager.get_all_types())
message = f"Successfully refreshed {tag_types_len} tag types from GitHub"
await hass.services.async_call(
"persistent_notification",
"create",
{
"title": "Tag Types Refreshed",
"message:": message,
"notification_id": "tag_types_refresh_notification",
},
)

# register the services
hass.services.register(DOMAIN, "dlimg", dlimg)
hass.services.register(DOMAIN, "lines5", lines5service)
Expand All @@ -149,6 +168,7 @@ async def reboot_ap_service(service: ServiceCall)-> None:
hass.services.register(DOMAIN, "reboot_tag", reboot_tag_service)
hass.services.register(DOMAIN, "scan_channels", scan_channels_service)
hass.services.register(DOMAIN, "reboot_ap", reboot_ap_service)
hass.services.register(DOMAIN, "refresh_tag_types", refresh_tag_types_service)
# error handling needs to be improved
return True

Expand Down
43 changes: 41 additions & 2 deletions custom_components/open_epaper_link/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import json
import logging

from .hw_map import get_hw_dimensions
from .tag_types import get_hw_dimensions, get_tag_types_manager
from .util import send_tag_cmd, reboot_ap
from .const import DOMAIN

Expand All @@ -23,6 +23,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e
buttons.append(ScanChannelsButton(hass, tag_mac, hub))
buttons.append(IdentifyTagButton(hass,tag_mac,hub))
buttons.append(RebootAPButton(hass, hub))
buttons.append(RefreshTagTypesButton(hass))
async_add_entities(buttons)

class ClearPendingTagButton(ButtonEntity):
Expand Down Expand Up @@ -195,4 +196,42 @@ async def async_press(self) -> None:
if response.status_code == 200:
_LOGGER.info(f"Sent identify command to tag {self._tag_mac}")
except requests.RequestException as err:
_LOGGER.error(f"Failed to send identify command to tag {self._tag_mac}: {err}")
_LOGGER.error(f"Failed to send identify command to tag {self._tag_mac}: {err}")

class RefreshTagTypesButton(ButtonEntity):
"""Button to manually refresh tag types from GitHub."""

def __init__(self, hass: HomeAssistant) -> None:
self._hass = hass
self._attr_unique_id = "refresh_tag_types"
self._attr_name = "Refresh Tag Types"
self._attr_entity_category = EntityCategory.DIAGNOSTIC
self._attr_icon = "mdi:refresh"

@property
def device_info(self):
"""Return device info."""
return {
"identifiers": {(DOMAIN, "ap")},
"name": "OpenEPaperLink AP",
"model": "esp32",
"manufacturer": "OpenEPaperLink",
}

async def async_press(self) -> None:
"""Trigger a manual refresh of tag types."""
manager = await get_tag_types_manager(self._hass)
# Force a refresh by clearing the last update timestamp
manager._last_update = None
await manager.ensure_types_loaded()
tag_types_len = len(manager.get_all_types())
message = f"Successfully refreshed {tag_types_len} tag types from GitHub"
await self.hass.services.async_call(
"persistent_notification",
"create",
{
"title": "Tag Types Refreshed",
"message": message,
"notification_id": "tag_types_refresh_notification",
},
)
Loading

0 comments on commit 1065e38

Please sign in to comment.