Skip to content

Commit

Permalink
Merge pull request #169 from g4bri3lDev/main
Browse files Browse the repository at this point in the history
Added service calls for common tag actions
  • Loading branch information
g4bri3lDev authored Sep 22, 2024
2 parents 0a339c8 + 9fe9b4d commit 662a58d
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 4 deletions.
39 changes: 36 additions & 3 deletions custom_components/open_epaper_link/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from __future__ import annotations
from .imagegen import *
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, ServiceCall
from . import hub
from .const import DOMAIN
from datetime import datetime
import logging
import pprint
import time

from .util import send_tag_cmd, reboot_ap

_LOGGER = logging.getLogger(__name__)

def rgb_to_rgb332(rgb):
Expand Down Expand Up @@ -109,14 +111,45 @@ async def setled(service: ServiceCall) -> None:
result = await hass.async_add_executor_job(requests.get, url)
if result.status_code != 200:
_LOGGER.warning(result.status_code)
async def clear_pending_service(service: ServiceCall)-> None:
entity_ids = service.data.get("entity_id")

for entity_id in entity_ids:
await send_tag_cmd(hass, entity_id,"clear")

async def force_refresh_service(service: ServiceCall)-> None:
entity_ids = service.data.get("entity_id")

for entity_id in entity_ids:
await send_tag_cmd(hass, entity_id,"refresh")

async def reboot_tag_service(service: ServiceCall)-> None:
entity_ids = service.data.get("entity_id")

for entity_id in entity_ids:
await send_tag_cmd(hass, entity_id,"reboot")

async def scan_channels_service(service: ServiceCall)-> None:
entity_ids = service.data.get("entity_id")

for entity_id in entity_ids:
await send_tag_cmd(hass, entity_id,"scan")

async def reboot_ap_service(service: ServiceCall)-> None:
await reboot_ap(hass)

# register the services
# register the services
hass.services.register(DOMAIN, "dlimg", dlimg)
hass.services.register(DOMAIN, "lines5", lines5service)
hass.services.register(DOMAIN, "lines4", lines4service)
hass.services.register(DOMAIN, "drawcustom", drawcustomservice)
hass.services.register(DOMAIN, "setled", setled)
# error haneling needs to be improved
hass.services.register(DOMAIN, "clear_pending", clear_pending_service)
hass.services.register(DOMAIN, "force_refresh", force_refresh_service)
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)
# error handling needs to be improved
return True

def int_to_hex_string(number):
Expand Down
34 changes: 34 additions & 0 deletions custom_components/open_epaper_link/icons.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"services": {
"dlimg": {
"service": "mdi:download"
},
"lines4": {
"service": "mdi:text-long"
},
"lines5": {
"service": "mdi:text-long"
},
"drawcustom": {
"service": "mdi:code-braces-box"
},
"setled": {
"service": "mdi:wall-sconce-flat-variant"
},
"clear_pending": {
"service": "mdi:delete"
},
"force_refresh": {
"service": "mdi:refresh"
},
"reboot_tag": {
"service": "mdi:restart"
},
"scan_channels": {
"service": "mdi:signal"
},
"reboot_ap": {
"service": "mdi:restart"
}
}
}
27 changes: 27 additions & 0 deletions custom_components/open_epaper_link/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,30 @@ lines4:
description: Format string for the fourth row. Char 1 is for the text alignment, char 2 is for the background color, char 3 is for the border color and char 4 is for the text color
required: false
example: lrwb
clear_pending:
name: Clear Pending
description: Clears the pending status for one or more ESL tags
target:
entity:
domain: open_epaper_link
force_refresh:
name: Force Refresh
description: Forces the ESL to refresh the display
target:
entity:
domain: open_epaper_link
reboot_tag:
name: Reboot Tag
description: Reboots one or more ESL tags
target:
entity:
domain: open_epaper_link
scan_channels:
name: Scan Channels
description: Lets the ESL scan for channels
target:
entity:
domain: open_epaper_link
reboot_ap:
name: Reboot AP
description: Reboots the AP
44 changes: 43 additions & 1 deletion custom_components/open_epaper_link/util.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,50 @@
from __future__ import annotations
from .const import DOMAIN
import requests
import logging
from homeassistant.core import HomeAssistant

_LOGGER: Final = logging.getLogger(__name__)

def get_image_folder(hass):
"""Return the folder where images are stored."""
return hass.config.path("www/open_epaper_link")

def get_image_path(hass, entity_id):
"""Return the path to the image for a specific tag."""
return hass.config.path("www/open_epaper_link/open_epaper_link."+ str(entity_id).lower() + ".jpg")
return hass.config.path("www/open_epaper_link/open_epaper_link."+ str(entity_id).lower() + ".jpg")
async def send_tag_cmd(hass: HomeAssistant, entity_id: str, cmd: str) -> bool:
"""Send a command to an ESL Tag."""
ip = hass.states.get(DOMAIN + ".ip").state
mac = entity_id.split(".")[1].upper()
url = f"http://{ip}/tag_cmd"

data = {
'mac': mac,
'cmd': cmd
}

try:
result = await hass.async_add_executor_job(lambda : requests.post(url, data=data))
if result.status_code == 200:
_LOGGER.info("Sent %s command to %s", cmd, entity_id)
else:
_LOGGER.error("Failed to send %s command to %s", cmd, entity_id)
except Exception as e:
_LOGGER.error("Failed to send %s command to %s: %s", cmd, entity_id, e)
return False

async def reboot_ap(hass: HomeAssistant) -> bool:
"""Reboot the ESL Access Point."""
ip = hass.states.get(DOMAIN + ".ip").state
url = f"http://{ip}/reboot"

try:
result = await hass.async_add_executor_job(lambda : requests.post(url))
if result.status_code == 200:
_LOGGER.info("Rebooted ESL Access Point")
else:
_LOGGER.error("Failed to reboot ESL Access Point")
except Exception as e:
_LOGGER.error("Failed to reboot ESL Access Point: %s", e)
return False

0 comments on commit 662a58d

Please sign in to comment.