Skip to content

Commit

Permalink
fix: UPS tracking and improvements (#498)
Browse files Browse the repository at this point in the history
  • Loading branch information
firstof9 authored Aug 19, 2021
2 parents d0908f0 + 41ebd92 commit 4430994
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 42 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pytest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
python -m pip install --upgrade pip
pip install -r requirements_test.txt
sudo apt-get update
sudo apt-get -y install language-pack-it
- name: Generate coverage report
run: |
Expand Down
5 changes: 5 additions & 0 deletions custom_components/mail_and_packages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_RESOURCES
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import (
Expand Down Expand Up @@ -94,6 +95,10 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
# Fetch initial data so we have data when entities subscribe
await coordinator.async_refresh()

# Raise ConfEntryNotReady if coordinator didn't update
if not coordinator.last_update_success:
raise ConfigEntryNotReady

hass.data[DOMAIN][config_entry.entry_id] = {
COORDINATOR: coordinator,
}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/mail_and_packages/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
"ups_packages": {},
"ups_tracking": {
"pattern": [
"(1Z ?[0-9A-Z]{3} ?[0-9A-Z]{3} ?[0-9A-Z]{2} ?[0-9A-Z]{4} ?[0-9A-Z]{3} ?[0-9A-Z]|[\\dT]\\d\\d\\d ?\\d\\d\\d\\d ?\\d\\d\\d)$"
"1Z?[0-9A-Z]{3}?[0-9A-Z]{3}?[0-9A-Z]{2}?[0-9A-Z]{4}?[0-9A-Z]{3}?[0-9A-Z]|[\\dT]\\d\\d\\d?\\d\\d\\d\\d?\\d\\d\\d"
]
},
"fedex_delivered": {
Expand Down
18 changes: 6 additions & 12 deletions custom_components/mail_and_packages/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def __init__(self, config, sensor_type, coordinator, unique_id):
self._config = config
self._name = const.SENSOR_TYPES[sensor_type][const.SENSOR_NAME]
self._icon = const.SENSOR_TYPES[sensor_type][const.SENSOR_ICON]
self._unit_of_measurement = const.SENSOR_TYPES[sensor_type][const.SENSOR_UNIT]
self._attr_unit_of_measurement = const.SENSOR_TYPES[sensor_type][
const.SENSOR_UNIT
]
self.type = sensor_type
self._host = config.data[CONF_HOST]
self._unique_id = unique_id
Expand All @@ -68,11 +70,6 @@ def state(self) -> Optional[int]:
value = None
return value

@property
def unit_of_measurement(self) -> Optional[str]:
"""Return the unit of measurement of this entity, if any."""
return self._unit_of_measurement

@property
def icon(self) -> str:
"""Return the unit of measurement."""
Expand Down Expand Up @@ -123,7 +120,9 @@ def __init__(self, hass, config, sensor_type, coordinator, unique_id):
self._config = config
self._name = const.IMAGE_SENSORS[sensor_type][const.SENSOR_NAME]
self._icon = const.IMAGE_SENSORS[sensor_type][const.SENSOR_ICON]
self._unit_of_measurement = const.IMAGE_SENSORS[sensor_type][const.SENSOR_UNIT]
self._attr_unit_of_measurement = const.IMAGE_SENSORS[sensor_type][
const.SENSOR_UNIT
]
self.type = sensor_type
self._host = config.data[CONF_HOST]
self._unique_id = unique_id
Expand Down Expand Up @@ -167,11 +166,6 @@ def state(self) -> Optional[str]:
the_path = f"{url.rstrip('/')}/local/mail_and_packages/{image}"
return the_path

@property
def unit_of_measurement(self) -> Optional[str]:
"""Return the unit of measurement of this entity, if any."""
return self._unit_of_measurement

@property
def icon(self) -> str:
"""Return the unit of measurement."""
Expand Down
4 changes: 4 additions & 0 deletions tests/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ async def test_check_file_path_access(
mock_copy_overlays,
mock_hash_file,
mock_getctime_today,
mock_update,
caplog,
):
"""Test check_file_path_access function."""
Expand Down Expand Up @@ -133,6 +134,7 @@ async def test_async_camera_image(
mock_copy_overlays,
mock_hash_file,
mock_getctime_today,
mock_update,
):
"""Test async_camera_image function."""

Expand Down Expand Up @@ -172,6 +174,7 @@ async def test_async_camera_image_file_error(
mock_copy_overlays,
mock_hash_file,
mock_getctime_today,
mock_update,
caplog,
):
"""Test async_camera_image function."""
Expand Down Expand Up @@ -208,6 +211,7 @@ async def test_async_on_demand_update(
mock_copy_overlays,
mock_hash_file,
mock_getctime_today,
mock_update,
):
"""Test async_camera_image function."""

Expand Down
3 changes: 3 additions & 0 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,7 @@ async def test_options_flow(
data,
hass,
mock_imap,
mock_update,
):
"""Test config flow options."""
entry = MockConfigEntry(
Expand Down Expand Up @@ -1127,6 +1128,7 @@ async def test_options_flow_invalid_custom_img_path(
data,
hass,
mock_imap,
mock_update,
):
"""Test config flow options."""
entry = MockConfigEntry(
Expand Down Expand Up @@ -1852,6 +1854,7 @@ async def test_options_flow_bad(
data,
hass,
mock_imap,
mock_update,
):
"""Test config flow options."""
entry = MockConfigEntry(
Expand Down
32 changes: 3 additions & 29 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ async def test_process_emails_copytree_error(
assert "Problem copying files from" in caplog.text


async def test_process_emails_bad(hass, mock_imap_no_email):
async def test_process_emails_bad(hass, mock_imap_no_email, mock_update):
entry = MockConfigEntry(
domain=DOMAIN,
title="imap.test.email",
Expand Down Expand Up @@ -690,7 +690,7 @@ async def test_ups_out_for_delivery(hass, mock_imap_ups_out_for_delivery):
result = get_count(
mock_imap_ups_out_for_delivery, "ups_delivering", True, "./", hass
)
assert result["count"] == 2
assert result["count"] == 1
# assert result["tracking"] == ["1Z2345YY0678901234"]


Expand Down Expand Up @@ -726,32 +726,6 @@ async def test_royal_out_for_delivery(hass, mock_imap_royal_out_for_delivery):
assert result["tracking"] == ["MA038501234GB"]


async def test_amazon_fwds(
hass,
mock_imap_no_email,
mock_osremove,
mock_osmakedir,
mock_listdir,
mock_update_time,
mock_hash_file,
mock_getctime_today,
caplog,
):
"""Test settting up entities."""
entry = MockConfigEntry(
domain=DOMAIN,
title="imap.test.email",
data=FAKE_CONFIG_DATA,
)

entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

assert "Amazon email adding [email protected] to list" in caplog.text
assert "Amazon email adding [email protected] to list" in caplog.text


async def test_amazon_shipped_count(hass, mock_imap_amazon_shipped):
with patch("datetime.date") as mock_date:
mock_date.today.return_value = date(2020, 9, 11)
Expand Down Expand Up @@ -965,7 +939,7 @@ async def test_image_file_name_path_error(hass, caplog):


async def test_image_file_name_amazon(
hass, mock_listdir_nogif, mock_getctime_today, mock_hash_file, caplog
hass, mock_listdir_nogif, mock_getctime_today, mock_hash_file, mock_copyfile, caplog
):
config = FAKE_CONFIG_DATA_CORRECTED

Expand Down
5 changes: 5 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ async def test_setup_entry(
mock_copy_overlays,
mock_hash_file,
mock_getctime_today,
mock_update,
):
"""Test settting up entities."""
entry = MockConfigEntry(
Expand Down Expand Up @@ -79,6 +80,7 @@ async def test_no_path_no_sec(
mock_copy_overlays,
mock_hash_file,
mock_getctime_today,
mock_update,
):
"""Test settting up entities."""
entry = MockConfigEntry(
Expand All @@ -104,6 +106,7 @@ async def test_missing_imap_timeout(
mock_copy_overlays,
mock_hash_file,
mock_getctime_today,
mock_update,
):
"""Test settting up entities."""
entry = MockConfigEntry(
Expand Down Expand Up @@ -132,6 +135,7 @@ async def test_amazon_fwds_string(
mock_copy_overlays,
mock_hash_file,
mock_getctime_today,
mock_update,
):
"""Test settting up entities."""
entry = MockConfigEntry(
Expand Down Expand Up @@ -160,6 +164,7 @@ async def test_custom_img(
mock_copy_overlays,
mock_hash_file,
mock_getctime_today,
mock_update,
):
"""Test settting up entities."""
entry = MockConfigEntry(
Expand Down

0 comments on commit 4430994

Please sign in to comment.