Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix small bug when enabling usps mail delivered sensor #958

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions custom_components/mail_and_packages/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,10 @@ def is_on(self) -> bool:
return True
return False
if self._type in self.coordinator.data.keys():
_LOGGER.debug(
"binary_sensor: %s value: %s",
self._type,
self.coordinator.data[self._type],
)
return bool(self.coordinator.data[self._type])
return False
3 changes: 2 additions & 1 deletion custom_components/mail_and_packages/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ async def async_setup_entry(hass, entry, async_add_entities):
resources = entry.data[CONF_RESOURCES]

for variable in resources:
sensors.append(PackagesSensor(entry, SENSOR_TYPES[variable], coordinator))
if variable in SENSOR_TYPES:
sensors.append(PackagesSensor(entry, SENSOR_TYPES[variable], coordinator))

for variable, value in IMAGE_SENSORS.items():
sensors.append(ImagePathSensors(hass, entry, value, coordinator))
Expand Down
26 changes: 26 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,32 @@ def mock_imap_usps_informed_digest_no_mail():
yield mock_conn


@pytest.fixture()
def mock_imap_usps_mail_delivered():
"""Mock imap class values."""
with patch(
"custom_components.mail_and_packages.helpers.imaplib"
) as mock_imap_usps_mail_delivered:
mock_conn = mock.Mock(spec=imaplib.IMAP4_SSL)
mock_imap_usps_mail_delivered.IMAP4_SSL.return_value = mock_conn

mock_conn.login.return_value = (
"OK",
[b"user@fake.email authenticated (Success)"],
)
mock_conn.list.return_value = (
"OK",
[b'(\\HasNoChildren) "/" "INBOX"'],
)
mock_conn.search.return_value = ("OK", [b"1"])
mock_conn.uid.return_value = ("OK", [b"1"])
f = open("tests/test_emails/usps_mail_delivered.eml", "r")
email_file = f.read()
mock_conn.fetch.return_value = ("OK", [(b"", email_file.encode("utf-8"))])
mock_conn.select.return_value = ("OK", [])
yield mock_conn


@pytest.fixture()
def mock_imap_ups_out_for_delivery():
"""Mock imap class values."""
Expand Down
23 changes: 23 additions & 0 deletions tests/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,3 +898,26 @@
"username": "user@fake.email",
"verify_ssl": False,
}

FAKE_CONFIG_DATA_USPS_DELIVERED = {
"allow_external": False,
"amazon_days": 3,
"amazon_domain": "amazon.com",
"amazon_fwds": "fakeuser@fake.email, fakeuser2@fake.email",
"custom_img": False,
"folder": '"INBOX"',
"generate_mp4": False,
"gif_duration": 5,
"host": "imap.test.email",
"image_name": "mail_today.gif",
"image_path": "custom_components/mail_and_packages/images/",
"image_security": True,
"imap_security": "SSL",
"imap_timeout": 30,
"password": "suchfakemuchpassword",
"port": 993,
"resources": ["usps_mail_delivered"],
"scan_interval": 20,
"username": "user@fake.email",
"verify_ssl": False,
}
49 changes: 48 additions & 1 deletion tests/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pytest_homeassistant_custom_component.common import MockConfigEntry

from custom_components.mail_and_packages.const import DOMAIN
from tests.const import FAKE_CONFIG_DATA
from tests.const import FAKE_CONFIG_DATA, FAKE_CONFIG_DATA_USPS_DELIVERED


@pytest.mark.asyncio
Expand Down Expand Up @@ -56,6 +56,53 @@ async def test_binary_sensor_no_updates(
assert state.state == "off"


@pytest.mark.asyncio
async def test_binary_sensor_mail_delivered(
hass, mock_imap_usps_mail_delivered, entity_registry: er.EntityRegistry, caplog
):
entry = MockConfigEntry(
domain=DOMAIN,
title="imap.test.email",
data=FAKE_CONFIG_DATA_USPS_DELIVERED,
)

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

assert "mail_and_packages" in hass.config.components

state = hass.states.get("binary_sensor.usps_image_updated")
assert state
assert state.state == "off"

state = hass.states.get("binary_sensor.amazon_image_updated")
assert state
assert state.state == "off"

entity_entry = entity_registry.async_get("binary_sensor.usps_mail_delivered")

assert entity_entry
assert entity_entry.disabled
assert entity_entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION

updated_entry = entity_registry.async_update_entity(
entity_entry.entity_id, disabled_by=None
)
assert updated_entry != entity_entry
assert updated_entry.disabled is False

# reload the integration
await hass.config_entries.async_forward_entry_unload(entry, "binary_sensor")
await hass.config_entries.async_forward_entry_setups(entry, ["binary_sensor"])
await hass.async_block_till_done()

state = hass.states.get("binary_sensor.usps_mail_delivered")
assert state
assert state.state == "on"
assert "binary_sensor: usps_mail_delivered value: 1" in caplog.text


# @pytest.mark.asyncio
# async def test_binary_sensor_updated(hass, mock_update_amazon_image):
# entry = MockConfigEntry(
Expand Down
Loading
Loading