Skip to content

Commit

Permalink
Add information on plate MAC address and reformats code
Browse files Browse the repository at this point in the history
  • Loading branch information
dgomes committed Sep 20, 2024
1 parent 6563948 commit 4218153
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
33 changes: 21 additions & 12 deletions custom_components/openhasp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import pathlib
import re

from homeassistant.helpers.device_registry import (
CONNECTION_NETWORK_MAC,
format_mac,
)
from homeassistant.components.mqtt import async_subscribe, async_publish
import homeassistant.components.mqtt as mqtt
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
Expand Down Expand Up @@ -151,7 +155,7 @@ def hasp_object(value):

HASP_PAGE_SCHEMA = vol.Schema(vol.All(vol.Coerce(int), vol.Range(min=0, max=12)))

PUSH_IMAGE_SCHEMA = cv.make_entity_service_schema(
PUSH_IMAGE_SCHEMA = cv.make_entity_service_schema(
{
vol.Required(ATTR_IMAGE): vol.Any(cv.url, cv.isfile),
vol.Required(ATTR_OBJECT): hasp_object,
Expand Down Expand Up @@ -262,6 +266,7 @@ async def async_setup_entry(hass, entry) -> bool:
sw_version=entry.data[DISCOVERED_VERSION],
configuration_url=entry.data.get(DISCOVERED_URL),
name=plate,
connections={(CONNECTION_NETWORK_MAC, format_mac(entry.data[CONF_HWID]))},
)

# Add entity to component
Expand All @@ -271,7 +276,7 @@ async def async_setup_entry(hass, entry) -> bool:
hass.data[DOMAIN][CONF_PLATE][plate] = plate_entity

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

listener = entry.add_update_listener(async_update_options)
entry.async_on_unload(listener)

Expand Down Expand Up @@ -380,7 +385,9 @@ async def async_added_to_hass(self):
"""Run when entity about to be added."""
await super().async_added_to_hass()

schema_file_contents = await self.hass.async_add_executor_job(self._read_file, pathlib.Path(__file__).parent.joinpath("pages_schema.json"))
schema_file_contents = await self.hass.async_add_executor_job(
self._read_file, pathlib.Path(__file__).parent.joinpath("pages_schema.json")
)
self.json_schema = json.loads(schema_file_contents)

state = await self.async_get_last_state()
Expand Down Expand Up @@ -560,7 +567,11 @@ async def async_change_page(self, page):
if self._statusupdate:
num_pages = self._statusupdate[HASP_NUM_PAGES]

if isinstance(page, int) and isinstance(num_pages, int) and (page <= 0 or page > num_pages):
if (
isinstance(page, int)
and isinstance(num_pages, int)
and (page <= 0 or page > num_pages)
):
_LOGGER.error(
"Can't change to %s, available pages are 1 to %s", page, num_pages
)
Expand Down Expand Up @@ -597,7 +608,9 @@ async def async_push_image(
):
"""Update object image."""

image_id = hashlib.md5(image.encode("utf-8") + self._entry.data[CONF_NAME].encode('utf-8')).hexdigest()
image_id = hashlib.md5(
image.encode("utf-8") + self._entry.data[CONF_NAME].encode("utf-8")
).hexdigest()

rgb_image = await self.hass.async_add_executor_job(
image_to_rgb565, image, (width, height), fitscreen
Expand All @@ -608,14 +621,10 @@ async def async_push_image(
cmd_topic = f"{self._topic}/command/{obj}.src"

if http_proxy:
rgb_image_url = (
f"{http_proxy}/api/openhasp/serve/{image_id}"
)
rgb_image_url = f"{http_proxy}/api/openhasp/serve/{image_id}"
else:
rgb_image_url = (
f"{get_url(self.hass, allow_external=False)}/api/openhasp/serve/{image_id}"
)
#self._entry.data
rgb_image_url = f"{get_url(self.hass, allow_external=False)}/api/openhasp/serve/{image_id}"
# self._entry.data
_LOGGER.debug("Push %s with %s", cmd_topic, rgb_image_url)

await async_publish(self.hass, cmd_topic, rgb_image_url, qos=0, retain=False)
Expand Down
5 changes: 3 additions & 2 deletions custom_components/openhasp/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ def image_to_rgb565(in_image, size, fitscreen):
height = min(h for h in [height, original_height] if h is not None and h > 0)
im.thumbnail((width, height), Image.LANCZOS)
else:
im = ImageOps.fit(im, (width, height), method = 3,
bleed = 0.0, centering =(0.5, 0.5))
im = ImageOps.fit(
im, (width, height), method=3, bleed=0.0, centering=(0.5, 0.5)
)

width, height = im.size # actual size after resize

Expand Down

0 comments on commit 4218153

Please sign in to comment.