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

Included device (comma separated)/Excluded device (comma separated) are not working as expected #2477

Closed
danielbrunt57 opened this issue Aug 20, 2024 · 2 comments

Comments

@danielbrunt57
Copy link
Collaborator

danielbrunt57 commented Aug 20, 2024

Describe the bug

Exclude Devices is excluding more than just the device I want excluded.
I.e. specifying "Echo Auto" excludes not only "Echo Auto" but also "Echo" since device name "Echo" is in the string "Bedroom,Daniel's Fire,Echo Auto,Mach-E SYNC,Living Room,Main floor,Norton Court,Office,This Device"

To Reproduce

Amazon device names with the issue: "Echo" and "Echo Auto"
Exclude Devices string: "Bedroom,Daniel's Fire,Echo Auto,Mach-E SYNC,Living Room,Main floor,Norton Court,Office,This Device"
I tried Echo Auto, Echo%20Auto and Echo\ Auto but none of these worked.

Expected behavior

Only exclude device "Echo Auto" but not device "Echo"

System details

  • Home-assistant (version): 2024.8.2
  • alexa_media (version from const.py or HA startup): 4.12.10
  • alexapy (version from pip show alexapy or HA startup): 1.29.0
  • Amazon 2FA/2SV is enabled (y/n): Y

Logs

024-08-20 00:13:30.345 DEBUG (MainThread) [custom_components.alexa_media] dl@b**a: Existing: [] New: ['Garage Echo Dot', 'Living Room Echo Dot Right', 'Office Echo Dot Left', 'Office Echo Dot Right', 'Bedroom Echo Dot Left', 'Kitchen Echo Dot', 'Bedroom Echo Dot Right', 'Echo Show 8', 'Living Room Echo Dot Left']; Filtered out by not being in include: [] or in exclude: ['Echo', 'Echo Auto', 'Office', 'Main floor', 'Mach-E SYNC', 'Bedroom', 'Norton Court', 'Living Room', "Daniel's Fire", 'This Device']

@jleinenbach
Copy link
Contributor

With the assistance of ChatGPT, I believe I've identified the root cause of the issue in config_flow.py:

Potential Issue in config_flow.py:

There is a potential problem in the way the CONF_INCLUDE_DEVICES and CONF_EXCLUDE_DEVICES lists are handled within the _save_user_input_to_config method. Specifically, the use of reduce to convert these lists into comma-separated strings can lead to unintended behavior.

Explanation:

  • Use of reduce: When the CONF_INCLUDE_DEVICES or CONF_EXCLUDE_DEVICES lists are provided as input, the current code uses reduce to concatenate the list items into a single string, separated by commas:

    if isinstance(user_input[CONF_INCLUDE_DEVICES], list):
        self.config[CONF_INCLUDE_DEVICES] = (
            reduce(lambda x, y: f"{x},{y}", user_input[CONF_INCLUDE_DEVICES])
            if user_input[CONF_INCLUDE_DEVICES]
            else ""
        )

    This transformation turns a list like ["Echo Auto", "Bedroom"] into the string "Echo Auto,Bedroom". While this might seem convenient, it introduces a risk where the string may be incorrectly processed later, particularly in matching logic.

  • Implication: Once converted to a string, any subsequent matching logic could mistakenly identify partial matches as valid exclusions or inclusions. For example, if CONF_EXCLUDE_DEVICES contains "Echo Auto", it might be incorrectly treated as "Echo,Auto", leading to unintended exclusions like "Echo".

Recommendation:

Avoid converting lists into comma-separated strings using reduce. Instead, maintain the list structure to ensure that each device name is matched exactly as intended. This approach will prevent issues where partial names are incorrectly excluded or included due to improper string processing.

@MrPiket
Copy link

MrPiket commented Aug 20, 2024

Something that I also notice in those fields when you do the configuration via the UI is that you cannot leave that field empty. When you click on submit, it doesn't delete the value of that field it goes back with what you have before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants