Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EN] Enhanced get state sentences for media_player #2230

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions responses/en/HassGetState.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: en

Check failure on line 1 in responses/en/HassGetState.yaml

View workflow job for this annotation

GitHub Actions / build

'dict object' has no attribute 'actual_state' in response 'alternate_state' (template='{{ slots.name | capitalize }} is {{ slots.actual_state }}, but it is
responses:
intents:
HassGetState:
Expand All @@ -9,6 +9,14 @@
one: |
{{ slots.name | capitalize }} is {{ state.state_with_unit }}

alternate_state: |
{{ slots.name | capitalize }} is {{ slots.actual_state }}, but it is
{% if state.state in slots.alternate_state %}
{{ slots.alternate_state[state.state] }}
{% else %}
{{ state.state }}
{% endif %}
Comment on lines +12 to +18
Copy link

@coderabbitai coderabbitai bot Jun 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new alternate_state response template enhances descriptive accuracy but may have an issue with the actual_state attribute.

- {{ slots.name | capitalize }} is {{ slots.actual_state }}, but it is
+ {{ slots.name | capitalize }} is {{ state.state_with_unit }}, but it is
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
alternate_state: |
{{ slots.name | capitalize }} is {{ slots.actual_state }}, but it is
{% if state.state in slots.alternate_state %}
{{ slots.alternate_state[state.state] }}
{% else %}
{{ state.state }}
{% endif %}
alternate_state: |
{{ slots.name | capitalize }} is {{ state.state_with_unit }}, but it is
{% if state.state in slots.alternate_state %}
{{ slots.alternate_state[state.state] }}
{% else %}
{{ state.state }}
{% endif %}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a slot being passed from sentences/en/media_player_HassGetState.yaml, so first of all it's not an attribute and second of all it is not a problem.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tetele Thank you for the clarification on the actual_state slot usage. I appreciate your insight and understanding of the implementation details. If there's anything else you need assistance with regarding this PR, feel free to let me know!


one_yesno: |
{% if query.matched %}
Yes
Expand Down
11 changes: 11 additions & 0 deletions sentences/en/_common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ lists:
out: "locked"
- in: "unlocked"
out: "unlocked"
media_player_states:
values:
- in: "[turned ]off"
out: "off"
- in: "(idle|standing by)"
out: ["on", "idle", "standby", "buffering"]
- in: "(playing|[turned ]on)"
out: "playing"
- in: "paused"
out: "paused"

# binary_sensor
bs_battery_states:
Expand Down Expand Up @@ -372,6 +382,7 @@ expansion_rules:
in: "(in|on|at|of)"
position: "{position}[[ ]%| percent]"
volume: "{volume:volume_level}[[ ]%| percent]"
media_players: "([media ]player[s]|speaker[s]stereo[s]|tv[s])"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a | between speaker and stereo. Should maybe add television? Also, this could be simplified by moving the [s] to the end.

Suggested change
media_players: "([media ]player[s]|speaker[s]stereo[s]|tv[s])"
media_players: "([media ]player|speaker|stereo|tv|television)[s]"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed regarding television, but moving the [s] at the end makes it less readable and does not optimize matching.


# Context awareness expansion rules
all: "(all|all of|every|every single|each|each and every)"
Expand Down
1 change: 1 addition & 0 deletions sentences/en/homeassistant_HassGetState.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ intents:
excludes_context:
domain:
- cover
- media_player

- sentences:
- (is|are) [there] any {on_off_domains:domain} {on_off_states:state} [in <area>]
Expand Down
75 changes: 75 additions & 0 deletions sentences/en/media_player_HassGetState.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
language: en
intents:
HassGetState:
data:
- sentences:
- is [the ][state of ]<name> [turned ]on[ in <area>]
slots:
actual_state: "on"
alternate_state:
"on": "in an unknown state"
idle: "not playing"
standby: "in standby"
Comment on lines +8 to +12
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe these should actually be metadata?

response: alternate_state
requires_context:
domain: media_player
excludes_context:
state:
- "playing"
- "off"

- sentences:
- is [the ][state of ]<name> [turned ]off[ in <area>]
slots:
state:
- "on"
- "idle"
- "paused"
- "standby"
- "buffering"
actual_state: "not off"
alternate_state:
"on": "in an unknown state"
idle: "not playing anything"
standby: "in standby"
response: alternate_state
requires_context:
domain: media_player

- sentences:
- is [the] [state of] <name> {media_player_states:state} [in <area>]
response: one_yesno
requires_context:
domain: media_player

- sentences:
- (is|are) [there] any <media_players> {media_player_states:state} [in <area>]
- (do you know|tell me) if there are any <media_players> {media_player_states:state} [in <area>]
slots:
domain: media_player
response: any
- sentences:
- (is|are) [there] any <media_players> {media_player_states:state} [in <area>]
- (do you know|tell me) if there are any <media_players> {media_player_states:state} [in <area>]
slots:
domain: media_player
response: any

- sentences:
- are all [the] <media_players> {media_player_states:state} [in <area>]
- are all [the] <media_players> in <area> {media_player_states:state}
slots:
domain: media_player
response: all

- sentences:
- "[do you know] (which|what) <media_players> (is|are) {media_player_states:state} [in <area>]"
slots:
domain: media_player
response: which

- sentences:
- "[tell me] how many <media_players> (is|are) {media_player_states:state} [in <area>]"
slots:
domain: media_player
response: how_many
6 changes: 4 additions & 2 deletions shared/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def get_matched_states(
if device_class_entity is not None:
device_class = device_class_entity.value

state_name: Optional[str] = None
state_name: Optional[str | List[str]] = None
state_entity = result.entities.get("state")
if state_entity is not None:
state_name = state_entity.value
Expand Down Expand Up @@ -163,7 +163,9 @@ def get_matched_states(

if state_name is not None:
# Match state
if state.hass_state == state_name:
if isinstance(state_name, str) and state.hass_state == state_name:
matched.append(state)
elif isinstance(state_name, list) and state.hass_state in state_name:
matched.append(state)
else:
unmatched.append(state)
Expand Down
125 changes: 125 additions & 0 deletions tests/en/media_player_HassGetState.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
language: en

Check failure on line 1 in tests/en/media_player_HassGetState.yaml

View workflow job for this annotation

GitHub Actions / build

invalid format: Expected anything but a dictionary for dictionary value @ data['tests'][0]['intent']['slots']['alternate_state']. Got {'on': 'in an unknown state', 'idle': 'not playing', 'standby': 'in standby'}
tests:
- sentences:
- "is the TV on?"
intent:
name: HassGetState
slots:
name: "TV"
actual_state: "on"
alternate_state:
"on": "in an unknown state"
idle: "not playing"
standby: "in standby"
context:
domain: media_player
volume_level: "50"
response: "Tv is on, but it is not playing"

- sentences:
- "is the TV off?"
intent:
name: HassGetState
slots:
name: "TV"
state:
- "on"
- "idle"
- "paused"
- "standby"
- "buffering"
actual_state: "not off"
alternate_state:
"on": "in an unknown state"
idle: "not playing anything"
standby: "in standby"
context:
domain: media_player
volume_level: "50"
response: "Tv is not off, but it is not playing anything"

- sentences:
- "is the TV playing?"
intent:
name: HassGetState
slots:
name: "TV"
state: "playing"
context:
domain: media_player
volume_level: "50"
response: "No, idle"

- sentences:
- "is the TV standing by?"
intent:
name: HassGetState
slots:
name: "TV"
state:
- "on"
- "idle"
- "standby"
- "buffering"
context:
domain: media_player
volume_level: "50"
response: "Yes"

- sentences:
- "are any media players on in the kitchen?"
intent:
name: HassGetState
slots:
area: "Kitchen"
domain: "media_player"
state: "playing"
response: "No"

- sentences:
- "are all media players on?"
intent:
name: HassGetState
slots:
domain: "media_player"
state: "playing"
response: "No, TV is not on"

- sentences:
- "are all media players off?"
intent:
name: HassGetState
slots:
domain: "media_player"
state: "off"
response: "No, TV is not off"

- sentences:
- "which media players are on?"
intent:
name: HassGetState
slots:
domain: "media_player"
state: "playing"
response: "Not any"

- sentences:
- "how many media players are on?"
intent:
name: HassGetState
slots:
domain: "media_player"
state: "playing"
response: "0"

- sentences:
- "are all the media players on in the living room"
- "are all media players on in the living room?"
- "are all media players in the living room on?"
intent:
name: HassGetState
slots:
domain: "media_player"
state: "playing"
area: "Living Room"
response: "No, TV is not on"
Comment on lines +1 to +125
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test scenarios are well-designed but there's an issue with the format of alternate_state.

- alternate_state:
-   "on": "in an unknown state"
-   idle: "not playing"
-   standby: "in standby"
+ alternate_state: "in an unknown state"

Committable suggestion was skipped due to low confidence.

Tools
GitHub Check: build

[failure] 1-1:
invalid format: Expected anything but a dictionary for dictionary value @ data['tests'][0]['intent']['slots']['alternate_state']. Got {'on': 'in an unknown state', 'idle': 'not playing', 'standby': 'in standby'}

Loading