core icon indicating copy to clipboard operation
core copied to clipboard

Add config flow to System Monitor

Open gjohansson-ST opened this issue 1 year ago • 9 comments

Proposed change

Add config flow to System Monitor Previous attempt #85896 was closed in the meantime

Type of change

  • [ ] Dependency upgrade
  • [ ] Bugfix (non-breaking change which fixes an issue)
  • [ ] New integration (thank you!)
  • [x] New feature (which adds functionality to an existing integration)
  • [ ] Deprecation (breaking change to happen in the future)
  • [ ] Breaking change (fix/feature causing existing functionality to break)
  • [ ] Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request: https://github.com/home-assistant/home-assistant.io/pull/30115

Checklist

  • [x] The code change is tested and works locally.
  • [x] Local tests pass. Your PR cannot be merged unless tests pass
  • [x] There is no commented out code in this PR.
  • [x] I have followed the development checklist
  • [x] I have followed the perfect PR recommendations
  • [x] The code has been formatted using Ruff (ruff format homeassistant tests)
  • [x] Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • [ ] The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • [ ] New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • [ ] For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • [ ] Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

gjohansson-ST avatar Dec 02 '23 18:12 gjohansson-ST

I am thinking if it would not be better to put the code in util.py into its own python module, just like most of the other integrations do?

autinerd avatar Dec 22 '23 17:12 autinerd

Import was good.

I'm having trouble adding/removing sensors though.

  • Its not clear what should be entered on this screen or what the options are Screenshot 2023-12-23 at 8 59 41 AM

  • Nothing showing up in the remove path Screenshot 2023-12-23 at 8 59 49 AM

bdraco avatar Dec 23 '23 19:12 bdraco

Screenshot 2023-12-23 at 8 57 09 AM Screenshot 2023-12-23 at 8 57 04 AM Screenshot 2023-12-23 at 8 57 02 AM Screenshot 2023-12-23 at 8 56 48 AM Screenshot 2023-12-23 at 8 56 16 AM

The rest looks good

bdraco avatar Dec 23 '23 19:12 bdraco

For the process sensors it may be more intuitive to have as the option flow a SelectSelector where the processes to monitor can be added and removed.

autinerd avatar Dec 23 '23 20:12 autinerd

I'll give it a retest on production shortly

bdraco avatar Dec 26 '23 05:12 bdraco

Seeing a trace but may be from earlier testing.

Will reset and try again

2023-12-25 19:29:44.653 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up systemmonitor platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 360, in _async_setup_platform
    await asyncio.shield(task)
  File "/usr/src/homeassistant/homeassistant/components/systemmonitor/sensor.py", line 391, in async_setup_entry
    legacy_resources = entry.options["resources"]
                       ~~~~~~~~~~~~~^^^^^^^^^^^^^
KeyError: 'resources'

bdraco avatar Dec 26 '23 05:12 bdraco

Worked fine after reset.

Add works well now

Maybe it shouldn't show the option to remove if there are no sensors to remove as you get stuck on the remove screen with an error on submit if there are none.

Screenshot 2023-12-25 at 7 33 44 PM Screenshot 2023-12-25 at 7 33 40 PM

bdraco avatar Dec 26 '23 05:12 bdraco

Screenshot 2023-12-25 at 7 35 05 PM

bdraco avatar Dec 26 '23 05:12 bdraco

Maybe it shouldn't show the option to remove if there are no sensors to remove as you get stuck on the remove screen with an error on submit if there are none.

There are no possibilities for dynamic menu's but I like the idea so we could add this possibility I think

gjohansson-ST avatar Dec 26 '23 14:12 gjohansson-ST

I changed the adding/removing of the sensors to be a simple multi-select instead which probably makes more sense and also simplifies the flow. Has been tested thoroughly so I'm merging once CI is happy.

gjohansson-ST avatar Dec 26 '23 16:12 gjohansson-ST

Pulled again and retested. Got this

2023-12-26 09:23:52.437 ERROR (SyncWorker_8) [roombapy.remote_client] Unable to connect to 192.168.107.20
2023-12-26 09:24:10.660 ERROR (MainThread) [aiohttp.server] Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/aiohttp/web_protocol.py", line 452, in _handle_request
    resp = await request_handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/web_app.py", line 543, in _handle
    resp = await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/aiohttp/web_middlewares.py", line 114, in impl
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/security_filter.py", line 85, in security_filter_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/forwarded.py", line 100, in forwarded_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/request_context.py", line 28, in request_context_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 80, in ban_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 233, in auth_middleware
    return await handler(request)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/headers.py", line 31, in headers_middleware
    response = await handler(request)
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 149, in handle
    result = await handler(request, **request.match_info)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/decorators.py", line 63, in with_admin
    return await func(self, request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/config/config_entries.py", line 213, in post
    return await super().post(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/http/data_validator.py", line 72, in wrapper
    result = await method(view, request, data, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/data_entry_flow.py", line 71, in post
    result = await self._flow_mgr.async_init(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 295, in async_init
    result = await self._async_handle_step(flow, flow.init_step, data)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 419, in _async_handle_step
    result: FlowResult = await getattr(flow, method)(user_input)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/schema_config_entry_flow.py", line 442, in _async_step
    result = await self._common_handler.async_step(step_id, user_input)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/schema_config_entry_flow.py", line 132, in async_step
    return await self._async_form_step(step_id, user_input)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/schema_config_entry_flow.py", line 181, in _async_form_step
    return await self._show_next_step(step_id)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/schema_config_entry_flow.py", line 243, in _show_next_step
    suggested_values = await form_step.suggested_values(self)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/systemmonitor/config_flow.py", line 108, in get_suggested_value
    processes: list[str] = sensors.get(CONF_PROCESS, [])
                           ^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'get'
Screenshot 2023-12-26 at 9 24 23 AM

I'll delete and recreate in case there was a schema change

bdraco avatar Dec 26 '23 19:12 bdraco

Yes. Should have mention that the schema/data structure changed from your last test. Thanks

gjohansson-ST avatar Dec 26 '23 19:12 gjohansson-ST

Screenshot 2023-12-26 at 9 34 44 AM Screenshot 2023-12-26 at 9 34 41 AM Screenshot 2023-12-26 at 9 34 24 AM

Retested, everything seems to work well. Much better workflow 👍

bdraco avatar Dec 26 '23 19:12 bdraco

Looks very good, especially the options flow. Thank you very much!

autinerd avatar Dec 26 '23 19:12 autinerd