From f21fe35a1f7e16b2f515a478d0ca6e5e226b4d98 Mon Sep 17 00:00:00 2001 From: Chris <1105672+firstof9@users.noreply.github.com> Date: Thu, 19 Dec 2024 10:26:32 -0700 Subject: [PATCH] fix: selectable binary_sensors processed (#1038) --- custom_components/mail_and_packages/const.py | 14 +-- custom_components/mail_and_packages/entity.py | 14 +++ .../mail_and_packages/helpers.py | 12 ++- pylintrc | 1 + tests/test_helpers.py | 87 +++++++++++++++++++ 5 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 custom_components/mail_and_packages/entity.py diff --git a/custom_components/mail_and_packages/const.py b/custom_components/mail_and_packages/const.py index d1a4bafc..01b75bbf 100644 --- a/custom_components/mail_and_packages/const.py +++ b/custom_components/mail_and_packages/const.py @@ -6,11 +6,12 @@ from homeassistant.components.binary_sensor import ( BinarySensorDeviceClass, - BinarySensorEntityDescription, ) from homeassistant.components.sensor import SensorDeviceClass, SensorEntityDescription from homeassistant.helpers.entity import EntityCategory +from .entity import MailandPackagesBinarySensorEntityDescription + DOMAIN = "mail_and_packages" DOMAIN_DATA = f"{DOMAIN}_data" VERSION = "0.0.0-dev" # Now updated by release workflow @@ -1211,21 +1212,24 @@ ), } -BINARY_SENSORS: Final[dict[str, BinarySensorEntityDescription]] = { - "usps_update": BinarySensorEntityDescription( +BINARY_SENSORS: Final[dict[str, MailandPackagesBinarySensorEntityDescription]] = { + "usps_update": MailandPackagesBinarySensorEntityDescription( name="USPS Image Updated", key="usps_update", device_class=BinarySensorDeviceClass.UPDATE, + selectable=False, ), - "amazon_update": BinarySensorEntityDescription( + "amazon_update": MailandPackagesBinarySensorEntityDescription( name="Amazon Image Updated", key="amazon_update", device_class=BinarySensorDeviceClass.UPDATE, + selectable=False, ), - "usps_mail_delivered": BinarySensorEntityDescription( + "usps_mail_delivered": MailandPackagesBinarySensorEntityDescription( name="USPS Mail Delivered", key="usps_mail_delivered", entity_registry_enabled_default=False, + selectable=True, ), } diff --git a/custom_components/mail_and_packages/entity.py b/custom_components/mail_and_packages/entity.py new file mode 100644 index 00000000..3e3bf5cc --- /dev/null +++ b/custom_components/mail_and_packages/entity.py @@ -0,0 +1,14 @@ +"""Support for Mail and Packages entities.""" + +from __future__ import annotations + +from dataclasses import dataclass + +from homeassistant.components.binary_sensor import BinarySensorEntityDescription + + +@dataclass +class MailandPackagesBinarySensorEntityDescription(BinarySensorEntityDescription): + """Class describing Mail and Packages binary sensor entities.""" + + selectable: bool = False diff --git a/custom_components/mail_and_packages/helpers.py b/custom_components/mail_and_packages/helpers.py index 56a3f2eb..68ac1af0 100644 --- a/custom_components/mail_and_packages/helpers.py +++ b/custom_components/mail_and_packages/helpers.py @@ -68,6 +68,7 @@ ATTR_SUBJECT, ATTR_TRACKING, ATTR_USPS_MAIL, + BINARY_SENSORS, CONF_ALLOW_EXTERNAL, CONF_AMAZON_DAYS, CONF_AMAZON_DOMAIN, @@ -102,7 +103,16 @@ def get_resources() -> dict: sensor_id: sensor.name for sensor_id, sensor in SENSOR_TYPES.items() } - return known_available_resources + # append binary sensors that have selectable set to true + additional_resources = { + sensor_id: sensor.name + for sensor_id, sensor in BINARY_SENSORS.items() + if sensor.selectable + } + + known_available_resources.update(additional_resources) + + return dict(sorted(known_available_resources.items())) async def _check_ffmpeg() -> bool: diff --git a/pylintrc b/pylintrc index 72133b4f..80a57417 100644 --- a/pylintrc +++ b/pylintrc @@ -33,6 +33,7 @@ disable= too-many-locals, abstract-method, too-many-positional-arguments, + unexpected-keyword-arg, [REFACTORING] diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 6c5c4249..c62aa6e6 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -27,6 +27,7 @@ get_formatted_date, get_items, get_mails, + get_resources, hash_file, image_file_name, login, @@ -1155,3 +1156,89 @@ async def test_amazon_shipped_fwd(hass, mock_imap_amazon_fwd, caplog): async def test_amazon_otp(hass, mock_imap_amazon_otp, caplog): result = amazon_otp(mock_imap_amazon_otp, "order") assert result == {"code": ["671314"]} + + +@pytest.mark.asyncio +async def test_get_resourcs(hass): + """Test get_resources helper function.""" + result = get_resources() + assert result == { + "amazon_delivered": "Mail Amazon Packages Delivered", + "amazon_exception": "Mail Amazon Exception", + "amazon_hub": "Mail Amazon Hub Packages", + "amazon_otp": "Mail Amazon OTP Code", + "amazon_packages": "Mail Amazon Packages", + "auspost_delivered": "Mail AusPost Delivered", + "auspost_delivering": "Mail AusPost Delivering", + "auspost_packages": "Mail AusPost Packages", + "bonshaw_distribution_network_delivered": "Mail Bonshaw Distribution Network Delivered", + "bonshaw_distribution_network_delivering": "Mail Bonshaw Distribution Network Delivering", + "bonshaw_distribution_network_packages": "Mail Bonshaw Distribution Network Packages", + "buildinglink_delivered": "Mail BuildingLink Delivered", + "capost_delivered": "Mail Canada Post Delivered", + "capost_delivering": "Mail Canada Post Delivering", + "capost_packages": "Mail Canada Post Packages", + "dhl_delivered": "Mail DHL Delivered", + "dhl_delivering": "Mail DHL Delivering", + "dhl_packages": "Mail DHL Packages", + "dhl_parcel_nl_delivered": "DHL Parcel NL Delivered", + "dhl_parcel_nl_delivering": "DHL Parcel NL Delivering", + "dhl_parcel_nl_packages": "DHL Parcel NL Packages", + "dpd_com_pl_delivered": "Mail DPD.com.pl Delivered", + "dpd_com_pl_delivering": "Mail DPD.com.pl Delivering", + "dpd_com_pl_packages": "Mail DPD.com.pl Packages", + "dpd_delivered": "Mail DPD Delivered", + "dpd_delivering": "Mail DPD Delivering", + "dpd_packages": "Mail DPD Packages", + "evri_delivered": "Mail Evri Delivered", + "evri_delivering": "Mail Evri Delivering", + "evri_packages": "Mail Evri Packages", + "fedex_delivered": "Mail FedEx Delivered", + "fedex_delivering": "Mail FedEx Delivering", + "fedex_packages": "Mail FedEx Packages", + "gls_delivered": "Mail GLS Delivered", + "gls_delivering": "Mail GLS Delivering", + "gls_packages": "Mail GLS Packages", + "hermes_delivered": "Mail Hermes Delivered", + "hermes_delivering": "Mail Hermes Delivering", + "hermes_packages": "Mail Hermes Packages", + "inpost_pl_delivered": "Mail InPost.pl Delivered", + "inpost_pl_delivering": "Mail InPost.pl Delivering", + "inpost_pl_packages": "Mail InPost.pl Packages", + "intelcom_delivered": "Mail Intelcom Delivered", + "intelcom_delivering": "Mail Intelcom Delivering", + "intelcom_packages": "Mail Intelcom Packages", + "mail_updated": "Mail Updated", + "poczta_polska_delivering": "Mail Poczta Polska Delivering", + "poczta_polska_packages": "Mail Poczta Polska Packages", + "post_at_delivered": "Post AT Delivered", + "post_at_delivering": "Post AT Delivering", + "post_at_packages": "Post AT Packages", + "post_de_delivering": "Post DE Delivering", + "post_de_packages": "Post DE Packages", + "post_nl_delivered": "Post NL Delivered", + "post_nl_delivering": "Post NL Delivering", + "post_nl_exception": "Post NL Missed Delivery", + "post_nl_packages": "Post NL Packages", + "purolator_delivered": "Mail Purolator Delivered", + "purolator_delivering": "Mail Purolator Delivering", + "purolator_packages": "Mail Purolator Packages", + "royal_delivered": "Mail Royal Mail Delivered", + "royal_delivering": "Mail Royal Mail Delivering", + "royal_packages": "Mail Royal Mail Packages", + "ups_delivered": "Mail UPS Delivered", + "ups_delivering": "Mail UPS Delivering", + "ups_exception": "Mail UPS Exception", + "ups_packages": "Mail UPS Packages", + "usps_delivered": "Mail USPS Delivered", + "usps_delivering": "Mail USPS Delivering", + "usps_exception": "Mail USPS Exception", + "usps_mail": "Mail USPS Mail", + "usps_mail_delivered": "USPS Mail Delivered", + "usps_packages": "Mail USPS Packages", + "walmart_delivered": "Mail Walmart Delivered", + "walmart_delivering": "Mail Walmart Delivering", + "walmart_exception": "Mail Walmart Exception", + "zpackages_delivered": "Mail Packages Delivered", + "zpackages_transit": "Mail Packages In Transit", + }