mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Fix when 2 states match with same name (#12771)
This commit is contained in:
parent
e2a2fe36fc
commit
f7e9215f5e
@ -97,12 +97,12 @@ def async_match_state(hass, name, states=None):
|
|||||||
if states is None:
|
if states is None:
|
||||||
states = hass.states.async_all()
|
states = hass.states.async_all()
|
||||||
|
|
||||||
entity = _fuzzymatch(name, states, lambda state: state.name)
|
state = _fuzzymatch(name, states, lambda state: state.name)
|
||||||
|
|
||||||
if entity is None:
|
if state is None:
|
||||||
raise IntentHandleError('Unable to find entity {}'.format(name))
|
raise IntentHandleError('Unable to find entity {}'.format(name))
|
||||||
|
|
||||||
return entity
|
return state
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
@ -154,12 +154,13 @@ def _fuzzymatch(name, items, key):
|
|||||||
matches = []
|
matches = []
|
||||||
pattern = '.*?'.join(name)
|
pattern = '.*?'.join(name)
|
||||||
regex = re.compile(pattern, re.IGNORECASE)
|
regex = re.compile(pattern, re.IGNORECASE)
|
||||||
for item in items:
|
for idx, item in enumerate(items):
|
||||||
match = regex.search(key(item))
|
match = regex.search(key(item))
|
||||||
if match:
|
if match:
|
||||||
matches.append((len(match.group()), match.start(), item))
|
# Add index so we pick first match in case same group and start
|
||||||
|
matches.append((len(match.group()), match.start(), idx, item))
|
||||||
|
|
||||||
return sorted(matches)[0][2] if matches else None
|
return sorted(matches)[0][3] if matches else None
|
||||||
|
|
||||||
|
|
||||||
class ServiceIntentHandler(IntentHandler):
|
class ServiceIntentHandler(IntentHandler):
|
||||||
|
12
tests/helpers/test_intent.py
Normal file
12
tests/helpers/test_intent.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
"""Tests for the intent helpers."""
|
||||||
|
from homeassistant.core import State
|
||||||
|
from homeassistant.helpers import intent
|
||||||
|
|
||||||
|
|
||||||
|
def test_async_match_state():
|
||||||
|
"""Test async_match_state helper."""
|
||||||
|
state1 = State('light.kitchen', 'on')
|
||||||
|
state2 = State('switch.kitchen', 'on')
|
||||||
|
|
||||||
|
state = intent.async_match_state(None, 'kitch', [state1, state2])
|
||||||
|
assert state is state1
|
Loading…
x
Reference in New Issue
Block a user