Skip to content

Commit

Permalink
fix: selectable binary_sensors processed (#1038)
Browse files Browse the repository at this point in the history
  • Loading branch information
firstof9 authored Dec 19, 2024
1 parent 9972855 commit f21fe35
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 6 deletions.
14 changes: 9 additions & 5 deletions custom_components/mail_and_packages/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
),
}

Expand Down
14 changes: 14 additions & 0 deletions custom_components/mail_and_packages/entity.py
Original file line number Diff line number Diff line change
@@ -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
12 changes: 11 additions & 1 deletion custom_components/mail_and_packages/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
ATTR_SUBJECT,
ATTR_TRACKING,
ATTR_USPS_MAIL,
BINARY_SENSORS,
CONF_ALLOW_EXTERNAL,
CONF_AMAZON_DAYS,
CONF_AMAZON_DOMAIN,
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ disable=
too-many-locals,
abstract-method,
too-many-positional-arguments,
unexpected-keyword-arg,

[REFACTORING]

Expand Down
87 changes: 87 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
get_formatted_date,
get_items,
get_mails,
get_resources,
hash_file,
image_file_name,
login,
Expand Down Expand Up @@ -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",
}

0 comments on commit f21fe35

Please sign in to comment.