1
mirror of https://github.com/home-assistant/core synced 2024-07-30 21:18:57 +02:00

Support case-insensitive matching

This commit is contained in:
puddly 2022-11-03 18:01:25 -04:00
parent 9c85d22bab
commit 0fdb2aa6bc
2 changed files with 8 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import dataclasses
import fnmatch
import logging
import os
import re
import sys
from typing import TYPE_CHECKING, Any
@ -125,11 +126,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
return True
def _fnmatch_lower(name: str | None, pattern: str) -> bool:
"""Match a lowercase version of the name."""
def _fnmatch_case_insensitive(name: str | None, pattern: str) -> bool:
"""Match a name case-insensitively."""
if name is None:
return False
return fnmatch.fnmatch(name.lower(), pattern)
return re.match(fnmatch.translate(pattern), name, re.IGNORECASE) is not None
def _is_matching(device: USBDevice, matcher: USBMatcher | USBCallbackMatcher) -> bool:
@ -138,15 +139,15 @@ def _is_matching(device: USBDevice, matcher: USBMatcher | USBCallbackMatcher) ->
return False
if "pid" in matcher and device.pid != matcher["pid"]:
return False
if "serial_number" in matcher and not _fnmatch_lower(
if "serial_number" in matcher and not _fnmatch_case_insensitive(
device.serial_number, matcher["serial_number"]
):
return False
if "manufacturer" in matcher and not _fnmatch_lower(
if "manufacturer" in matcher and not _fnmatch_case_insensitive(
device.manufacturer, matcher["manufacturer"]
):
return False
if "description" in matcher and not _fnmatch_lower(
if "description" in matcher and not _fnmatch_case_insensitive(
device.description, matcher["description"]
):
return False

View File

@ -403,6 +403,7 @@ async def test_discovered_by_websocket_scan_limited_by_manufacturer_matcher(
"domain": "test1",
"vid": "3039",
"pid": "3039",
"description": "*ConBee*",
"manufacturer": "dresden elektronik ingenieurtechnik*",
}
]