Skip to content

Commit

Permalink
Fix double slash in URL sensor and missing defaults (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
firstof9 authored Feb 23, 2021
2 parents 381b7fb + 9656a0f commit 5be0d52
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
11 changes: 11 additions & 0 deletions custom_components/mail_and_packages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
CONF_PATH,
CONF_SCAN_INTERVAL,
COORDINATOR,
DEFAULT_IMAP_TIMEOUT,
DOMAIN,
ISSUE_URL,
PLATFORM,
Expand All @@ -42,6 +43,14 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
hass.data.setdefault(DOMAIN, {})
updated_config = config_entry.data.copy()

# Set amazon fwd blank if missing
if CONF_AMAZON_FWDS not in updated_config.keys():
updated_config[CONF_AMAZON_FWDS] = []

# Set default timeout if missing
if CONF_IMAP_TIMEOUT not in updated_config.keys():
updated_config[CONF_IMAP_TIMEOUT] = DEFAULT_IMAP_TIMEOUT

# Set external path off by default
if CONF_ALLOW_EXTERNAL not in config_entry.data.keys():
updated_config[CONF_ALLOW_EXTERNAL] = False
Expand Down Expand Up @@ -142,6 +151,8 @@ async def async_migrate_entry(hass, config_entry):
updated_config[CONF_AMAZON_FWDS] = [
x.strip() for x in updated_config[CONF_AMAZON_FWDS].split(",")
]
else:
updated_config[CONF_AMAZON_FWDS] = []
else:
_LOGGER.warn("Missing configuration data: %s", CONF_AMAZON_FWDS)

Expand Down
14 changes: 7 additions & 7 deletions custom_components/mail_and_packages/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def hash_file(filename: str) -> str:
return h.hexdigest()


def fetch(hass: Any, config: Any, account: Any, data: dict, sensor: str):
def fetch(hass: Any, config: Any, account: Any, data: dict, sensor: str) -> int:
"""Fetch data for a single sensor, including any sensors it depends on."""

img_out_path = f"{hass.config.path()}/{config.get(const.CONF_PATH)}"
Expand Down Expand Up @@ -289,7 +289,7 @@ def login(host, port, user, pwd):
return account


def selectfolder(account, folder):
def selectfolder(account, folder) -> None:
"""Select folder inside the mailbox"""
try:
rv, mailboxes = account.list()
Expand Down Expand Up @@ -518,7 +518,7 @@ def get_mails(
return image_count


def _generate_mp4(path: str, image_file: str):
def _generate_mp4(path: str, image_file: str) -> None:
"""
Generate mp4 from gif
use a subprocess so we don't lock up the thread
Expand Down Expand Up @@ -578,7 +578,7 @@ def resize_images(images: list, width: int, height: int) -> list:
return all_images


def copy_overlays(path: str):
def copy_overlays(path: str) -> None:
""" Copy overlay images to image output path."""

overlays = const.OVERLAY
Expand All @@ -594,7 +594,7 @@ def copy_overlays(path: str):
)


def cleanup_images(path: str, image: Optional[str] = None):
def cleanup_images(path: str, image: Optional[str] = None) -> None:
"""
Clean up image storage directory
Only supose to delete .gif and .mp4 files
Expand Down Expand Up @@ -790,7 +790,7 @@ def amazon_search(account: Any, image_path: str, hass: Any) -> int:
return count


def get_amazon_image(sdata: Any, account: Any, image_path: str, hass: Any):
def get_amazon_image(sdata: Any, account: Any, image_path: str, hass: Any) -> None:
""" Find Amazon delivery image """
_LOGGER.debug("Searching for Amazon image in emails...")
search = const.AMAZON_IMG_PATTERN
Expand Down Expand Up @@ -828,7 +828,7 @@ def get_amazon_image(sdata: Any, account: Any, image_path: str, hass: Any):
hass.add_job(download_img(img_url, image_path))


async def download_img(img_url: str, img_path: str):
async def download_img(img_url: str, img_path: str) -> None:
""" Download image from url """
filepath = img_path + "amazon_delivered.jpg"
async with aiohttp.ClientSession() as session:
Expand Down
5 changes: 3 additions & 2 deletions custom_components/mail_and_packages/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ async def async_added_to_hass(self):
self.coordinator.async_add_listener(self.async_write_ha_state)
)


class ImagePathSensors(CoordinatorEntity):
""" Represntation of a sensor """

Expand Down Expand Up @@ -159,9 +160,9 @@ def state(self) -> Optional[str]:
elif self.hass.config.external_url is None:
_LOGGER.warn("External URL not set in configuration.")
url = self.hass.config.internal_url
return f"{url.lstrip('/')}/local/mail_and_packages/{image}"
return f"{url.rstrip('/')}/local/mail_and_packages/{image}"
url = self.hass.config.external_url
return f"{url.lstrip('/')}/local/mail_and_packages/{image}"
return f"{url.rstrip('/')}/local/mail_and_packages/{image}"
else:
return None

Expand Down
17 changes: 17 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
FAKE_CONFIG_DATA_NO_RND,
)

MAIL_IMAGE_URL_ENTITY = "sensor.mail_image_url"
MAIL_IMAGE_SYSTEM_PATH = "sensor.mail_image_system_path"


async def test_unload_entry(hass, mock_update, mock_copy_overlays):
"""Test unloading entities. """
Expand Down Expand Up @@ -160,12 +163,17 @@ async def test_process_emails(
data=FAKE_CONFIG_DATA,
)

hass.config.internal_url = "http://127.0.0.1:8123/"
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

config = entry.data.copy()
assert config == FAKE_CONFIG_DATA_CORRECTED
state = hass.states.get(MAIL_IMAGE_SYSTEM_PATH)
assert "/testing_config/images/mail_and_packages/testfile.gif" in state.state
state = hass.states.get(MAIL_IMAGE_URL_ENTITY)
assert state.state == "http://127.0.0.1:8123/local/mail_and_packages/testfile.gif"
result = process_emails(hass, config)
assert result["mail_updated"] == "Sep-23-2020 10:28 AM"
assert result["zpackages_delivered"] == 0
Expand Down Expand Up @@ -194,12 +202,21 @@ async def test_process_emails_external(
data=FAKE_CONFIG_DATA_EXTERNAL,
)

hass.config.internal_url = "http://127.0.0.1:8123/"
hass.config.external_url = "http://really.fake.host.net:8123/"
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

config = entry.data.copy()
assert config == FAKE_CONFIG_DATA_CORRECTED_EXTERNAL
state = hass.states.get(MAIL_IMAGE_SYSTEM_PATH)
assert "/testing_config/www/mail_and_packages/testfile.gif" in state.state
state = hass.states.get(MAIL_IMAGE_URL_ENTITY)
assert (
state.state
== "http://really.fake.host.net:8123/local/mail_and_packages/testfile.gif"
)
result = process_emails(hass, config)
assert result["mail_updated"] == "Sep-23-2020 10:28 AM"
assert result["zpackages_delivered"] == 0
Expand Down

0 comments on commit 5be0d52

Please sign in to comment.