mirror of
https://github.com/home-assistant/core.git
synced 2025-08-05 13:45:12 +02:00
Support case-insensitive matching
This commit is contained in:
@@ -6,6 +6,7 @@ import dataclasses
|
|||||||
import fnmatch
|
import fnmatch
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
from typing import TYPE_CHECKING, Any
|
from typing import TYPE_CHECKING, Any
|
||||||
|
|
||||||
@@ -125,11 +126,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def _fnmatch_lower(name: str | None, pattern: str) -> bool:
|
def _fnmatch_case_insensitive(name: str | None, pattern: str) -> bool:
|
||||||
"""Match a lowercase version of the name."""
|
"""Match a name case-insensitively."""
|
||||||
if name is None:
|
if name is None:
|
||||||
return False
|
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:
|
def _is_matching(device: USBDevice, matcher: USBMatcher | USBCallbackMatcher) -> bool:
|
||||||
@@ -138,15 +139,15 @@ def _is_matching(device: USBDevice, matcher: USBMatcher | USBCallbackMatcher) ->
|
|||||||
return False
|
return False
|
||||||
if "pid" in matcher and device.pid != matcher["pid"]:
|
if "pid" in matcher and device.pid != matcher["pid"]:
|
||||||
return False
|
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"]
|
device.serial_number, matcher["serial_number"]
|
||||||
):
|
):
|
||||||
return False
|
return False
|
||||||
if "manufacturer" in matcher and not _fnmatch_lower(
|
if "manufacturer" in matcher and not _fnmatch_case_insensitive(
|
||||||
device.manufacturer, matcher["manufacturer"]
|
device.manufacturer, matcher["manufacturer"]
|
||||||
):
|
):
|
||||||
return False
|
return False
|
||||||
if "description" in matcher and not _fnmatch_lower(
|
if "description" in matcher and not _fnmatch_case_insensitive(
|
||||||
device.description, matcher["description"]
|
device.description, matcher["description"]
|
||||||
):
|
):
|
||||||
return False
|
return False
|
||||||
|
@@ -403,6 +403,7 @@ async def test_discovered_by_websocket_scan_limited_by_manufacturer_matcher(
|
|||||||
"domain": "test1",
|
"domain": "test1",
|
||||||
"vid": "3039",
|
"vid": "3039",
|
||||||
"pid": "3039",
|
"pid": "3039",
|
||||||
|
"description": "*ConBee*",
|
||||||
"manufacturer": "dresden elektronik ingenieurtechnik*",
|
"manufacturer": "dresden elektronik ingenieurtechnik*",
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
Reference in New Issue
Block a user