From 07545fcff2e850f87842b0e9078574aa514e931e Mon Sep 17 00:00:00 2001 From: Chris <1105672+firstof9@users.noreply.github.com> Date: Wed, 24 Jul 2024 17:10:41 -0700 Subject: [PATCH] fix: fix small bug when enabling usps mail delivered sensor (#958) --- .../mail_and_packages/binary_sensor.py | 5 + custom_components/mail_and_packages/sensor.py | 3 +- tests/conftest.py | 26 + tests/const.py | 23 + tests/test_binary_sensor.py | 49 +- tests/test_emails/usps_mail_delivered.eml | 1208 +++++++++++++++++ 6 files changed, 1312 insertions(+), 2 deletions(-) create mode 100644 tests/test_emails/usps_mail_delivered.eml diff --git a/custom_components/mail_and_packages/binary_sensor.py b/custom_components/mail_and_packages/binary_sensor.py index 081f695f..efdb63b2 100644 --- a/custom_components/mail_and_packages/binary_sensor.py +++ b/custom_components/mail_and_packages/binary_sensor.py @@ -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 diff --git a/custom_components/mail_and_packages/sensor.py b/custom_components/mail_and_packages/sensor.py index 20c226b7..b7a8a94f 100644 --- a/custom_components/mail_and_packages/sensor.py +++ b/custom_components/mail_and_packages/sensor.py @@ -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)) diff --git a/tests/conftest.py b/tests/conftest.py index 8f7461f2..0385ffcb 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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.""" diff --git a/tests/const.py b/tests/const.py index 0f3e3567..cc46efc6 100644 --- a/tests/const.py +++ b/tests/const.py @@ -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, +} diff --git a/tests/test_binary_sensor.py b/tests/test_binary_sensor.py index 0c0acda1..70064bee 100644 --- a/tests/test_binary_sensor.py +++ b/tests/test_binary_sensor.py @@ -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 @@ -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( diff --git a/tests/test_emails/usps_mail_delivered.eml b/tests/test_emails/usps_mail_delivered.eml new file mode 100644 index 00000000..0809759d --- /dev/null +++ b/tests/test_emails/usps_mail_delivered.eml @@ -0,0 +1,1208 @@ +Delivered-To: usps.emailuser@gmail.com +Received: by 2002:a05:7010:fa15:b0:3e7:8f91:c410 with SMTP id qv21csp3277846mdb; + Wed, 24 Jul 2024 12:06:53 -0700 (PDT) +X-Google-Smtp-Source: AGHT+IHxvIdIYIxU4880Kpv0oSrVhmI4VVYX4uiiSRTt9LXnM4z6w6jwUHCqcqbwLOP0KjoJGyAa +X-Received: by 2002:a05:620a:24d6:b0:79d:9ccd:9421 with SMTP id af79cd13be357-7a1d4514712mr74976285a.48.1721848013699; + Wed, 24 Jul 2024 12:06:53 -0700 (PDT) +ARC-Seal: i=1; a=rsa-sha256; t=1721848013; cv=none; + d=google.com; s=arc-20160816; + b=BINgNllVhBVD3tf5d3ir901xOAXbJBdxNAvr3o3mE8edQgAKPIDDzFo7KP8Tz22XUp + bb9xaFn6BLPhsMMArXiaq795FGNmgit9fTZnloIHI6M1RBWT0XC7SE7VBEb3+lQ5Z6j9 + /suZJ90H7mwiWf+QJmaXuwqPWpjkdWXMlj3hEZvYeRT5Kv2pzo1FKjd5p+PcSZTy+1pl + aTHdAVpexJ/7lGa42l7JiuXp91dLKbPVsiL8l5vehD1SxfZ5FFvTv50hWqrySpTVPmm5 + PICfc4tyNXi6NHMuv5WASwYMQkqXZF+T6GmYDB+gkEF/F3EZp1Jh1KMtXGRYUhwbchWE + Fu8A== +ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; + h=content-transfer-encoding:message-id:to:from:subject:mime-version + :date:sender:dkim-signature:dkim-signature; + bh=F2slSVT/hDvTuNwbnA+63+abakRuaMkGOmkiBvfOD3U=; + fh=jrMwuYj9j2Sb1Uz+WBKF6Gdf9DFOShvhdLJmLv2yLyM=; + b=tape357FNnVjcLcgZvMOzSXpECe6iSeNLJDqr9VHrA5o6y5e5rDFhxgnrPwc/Qz/kL + o7fIvyOTMdmR7pKcTEUjFCzjP0SiuL+tjs1mHICUnMMpdUdA9ZhkPJGKk9KrFHmftNGA + BrgX9vAT+w3gXAEWfR+URUDJATJtmnGWGKqi7eLHh/5dY2iBZ++/Exp+RzP6LRt/XyFt + e2ITqurFgJT3AcDYRFzD1e8GoZ7xHuo6OgpV5dF9L0gHXQSflNpn7nmVnzyRzCTkWUzf + rYdIeXjsCMx2SMdN7CoWb0gjp1ukrv6I5u/kEnHm8pIXLk4JCD6Vb9p0yIbamsS7PMOf + RvmQ==; + dara=google.com +ARC-Authentication-Results: i=1; mx.google.com; + dkim=pass header.i=@email.informeddelivery.usps.com header.s=krs header.b=VpMFDPjZ; + dkim=pass header.i=@mailgun.org header.s=mg header.b=IzzvhGDb; + spf=pass (google.com: domain of bounce+1097e0.f7c326-usps.emailuser=gmail.com@email.informeddelivery.usps.com designates 159.112.244.229 as permitted sender) smtp.mailfrom="bounce+1097e0.f7c326-usps.emailuser=gmail.com@email.informeddelivery.usps.com"; + dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=usps.com +Return-Path: +Received: from mg229.informeddelivery.usps.com (mg229.informeddelivery.usps.com. [159.112.244.229]) + by mx.google.com with UTF8SMTPS id af79cd13be357-7a1990b2b3dsi1341201585a.697.2024.07.24.12.06.53 + for + (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); + Wed, 24 Jul 2024 12:06:53 -0700 (PDT) +Received-SPF: pass (google.com: domain of bounce+1097e0.f7c326-usps.emailuser=gmail.com@email.informeddelivery.usps.com designates 159.112.244.229 as permitted sender) client-ip=159.112.244.229; +Authentication-Results: mx.google.com; + dkim=pass header.i=@email.informeddelivery.usps.com header.s=krs header.b=VpMFDPjZ; + dkim=pass header.i=@mailgun.org header.s=mg header.b=IzzvhGDb; + spf=pass (google.com: domain of bounce+1097e0.f7c326-usps.emailuser=gmail.com@email.informeddelivery.usps.com designates 159.112.244.229 as permitted sender) smtp.mailfrom="bounce+1097e0.f7c326-usps.emailuser=gmail.com@email.informeddelivery.usps.com"; + dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=usps.com +DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=email.informeddelivery.usps.com; q=dns/txt; s=krs; t=1721848013; x=1721855213; h=Content-Type: Content-Transfer-Encoding: Message-Id: To: To: From: From: Subject: Subject: Mime-Version: Date: Sender: Sender: X-Feedback-Id; bh=F2slSVT/hDvTuNwbnA+63+abakRuaMkGOmkiBvfOD3U=; b=VpMFDPjZEk2z2+ilhJY2ppeoqkZ98T6fRtEehX4g6Lcjs1l2XpVHTbJdcVqJFs5YsXLNaqfBumpvylSXssJYaLENT88wPpBcdr4lqXQkG5XivYUN9xvgAFmtkqPKoAthpLevL8GegX3sfVjN6eKVPDl1umrii93kwXBD8ZDiR3bQic5ex/aasPFZcxkaCmNfYhuFzE63NMbTbPqzmnf/EY1GZnKCYXZfRi0H9pFCGYSJeO04T7LJjmLrkKko9oRRCdo2W2r+p+3YX/YTqVtE19U9BVTFn1Y0lK2MHl7iuJ1HQQLqhM5GkXfiD/H9vDuU/qaNrDP24MYEsQ58JA0pFQ== +DKIM-Signature: a=rsa-sha256; v=1; c=relaxed/relaxed; d=mailgun.org; q=dns/txt; s=mg; t=1721848013; x=1721855213; h=Content-Type: Content-Transfer-Encoding: Message-Id: To: To: From: From: Subject: Subject: Mime-Version: Date: Sender: Sender: X-Feedback-Id; bh=F2slSVT/hDvTuNwbnA+63+abakRuaMkGOmkiBvfOD3U=; b=IzzvhGDbewrk20ga+aLoAIz+PRTYaG+KEzaniqlR3u+jZflmdXre1OqCNKKIzK/BCLRIEIA441JBWfkxwrF5jgMvg8+ra3u7njQ5wOrORfobE1DYYaG+O0uQk9YVzcYEEcYa3EkfZ09pdWGtMvV0iWxn6jVeK7+iozd/76y7sNQ= +X-Feedback-Id: USPSInformeddelivery@email.informeddelivery.usps.com:MDN:628d49d589e2a96a9951e974:mailgun +X-Mailgun-Sending-Ip: 159.112.244.229 +X-Mailgun-Sid: WyI1ZjdkMCIsImdhcnJldHQucGVhdnkrdXNwcy5jb21AZ21haWwuY29tIiwiZjdjMzI2Il0= +Received: from ( []) by 817214700d08 with HTTP id 66a14fa253311c018e638c6a; Wed, 24 Jul 2024 19:01:54 GMT +Sender: USPSInformeddelivery@email.informeddelivery.usps.com +Date: Wed, 24 Jul 2024 19:01:54 +0000 +Mime-Version: 1.0 +Subject: Your Mail Was Delivered Wed, Jul 24 +From: USPS Informed Delivery +To: usps.emailuser@gmail.com +X-Mailgun-Tag: MDN +X-Mailgun-Drop-Message: false +X-Mailgun-Deliver-By: Wed, 24 Jul 2024 19:06:53 +0000 +Message-Id: <20240724190154.b758d0d2c0cd8936@email.informeddelivery.usps.com> +Content-Transfer-Encoding: quoted-printable +Content-Type: text/html; charset="utf-8" + + +=20 + =20 + =20 + =20 + =20 + =20 + Mail Delivery Notification=20 + =20 + =20 + =20 + =20 +
=20 + =20 +
=20 +


3D"" 3D""

=20 + + \ No newline at end of file