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

Add sensor for CPU and memory utilization for unifi device #114986

Merged

Conversation

kimdv
Copy link
Contributor

@kimdv kimdv commented Apr 5, 2024

Breaking change

This should only be additive.

Proposed change

I saw that this pr was stalled and never merged: #74025
As I was setting up my new home assistant, I tought it would be nice with this information on my dashboard. So why not contributing 🤷‍♂️

So this PR aims to add system_stats sensors. The uptime is already added, but bot trough the system stats tough.
I've added a sensor for CPU and memory utilization.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • 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:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • 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:

Copy link

@home-assistant home-assistant bot left a comment

Choose a reason for hiding this comment

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

Hi @kimdv

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant
Copy link

home-assistant bot commented Apr 5, 2024

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant
Copy link

home-assistant bot commented Apr 5, 2024

Hey there @Kane610, mind taking a look at this pull request as it has been labeled with an integration (unifi) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of unifi can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign unifi Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@kimdv kimdv marked this pull request as ready for review April 5, 2024 22:38
@kimdv kimdv force-pushed the kimdv/add-system_states-to-device-sensor branch from a7c06f3 to f33e062 Compare April 6, 2024 07:25
Copy link
Member

@Kane610 Kane610 left a comment

Choose a reason for hiding this comment

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

Awesome!

homeassistant/components/unifi/sensor.py Outdated Show resolved Hide resolved
homeassistant/components/unifi/sensor.py Outdated Show resolved Hide resolved
@home-assistant home-assistant bot marked this pull request as draft April 6, 2024 15:21
@kimdv kimdv force-pushed the kimdv/add-system_states-to-device-sensor branch 2 times, most recently from 335eeab to 519598a Compare April 6, 2024 18:56
@kimdv kimdv marked this pull request as ready for review April 6, 2024 18:57
@home-assistant home-assistant bot requested a review from Kane610 April 6, 2024 18:57
Copy link
Member

@Kane610 Kane610 left a comment

Choose a reason for hiding this comment

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

Looks really great, one thing left

homeassistant/components/unifi/sensor.py Outdated Show resolved Hide resolved
homeassistant/components/unifi/sensor.py Outdated Show resolved Hide resolved
@home-assistant home-assistant bot marked this pull request as draft April 6, 2024 23:39
@kimdv kimdv force-pushed the kimdv/add-system_states-to-device-sensor branch 2 times, most recently from 632f54f to 5766857 Compare April 7, 2024 09:23
@kimdv kimdv marked this pull request as ready for review April 7, 2024 09:26
@home-assistant home-assistant bot requested a review from Kane610 April 7, 2024 09:27
@kimdv
Copy link
Contributor Author

kimdv commented Apr 7, 2024

@Kane610 I see that unifi/test_switch.py have a OUTLET_UP1 where system-stats = {}

So should we either add the missing data or should we expend "system-stats" in hub.api.devices[obj_id].raw to also check if cpu and mem is in?

@Kane610
Copy link
Member

Kane610 commented Apr 7, 2024

@Kane610 I see that unifi/test_switch.py have a OUTLET_UP1 where system-stats = {}

So should we either add the missing data or should we expend "system-stats" in hub.api.devices[obj_id].raw to also check if cpu and mem is in?

I think we need to replace the lambda with a method here to be a bit smarter with supported_fn. It should take device and index or something and still validate the existence of system stats but also check for the expected value if it is "" the method should return false

@kimdv kimdv force-pushed the kimdv/add-system_states-to-device-sensor branch 2 times, most recently from ebfe095 to 55aa868 Compare April 7, 2024 19:29
@kimdv
Copy link
Contributor Author

kimdv commented Apr 7, 2024

I think we need to replace the lambda with a method here to be a bit smarter with supported_fn. It should take device and index or something and still validate the existence of system stats but also check for the expected value if it is "" the method should return false

Do you think on something like this @Kane610 ?

@Kane610
Copy link
Member

Kane610 commented Apr 7, 2024

I think we need to replace the lambda with a method here to be a bit smarter with supported_fn. It should take device and index or something and still validate the existence of system stats but also check for the expected value if it is "" the method should return false

Do you think on something like this @Kane610 ?

If you add index as well we can combine them to one method and you can use partial to define index to look for when instantiating the entity description. If that makes sense

@kimdv
Copy link
Contributor Author

kimdv commented Apr 8, 2024

If you add index as well we can combine them to one method and you can use partial to define index to look for when instantiating the entity description. If that makes sense

Do you think on something like this

def device_system_stats_supported_fn(
    hub: UnifiHub, obj_id: str, stat_index: int
) -> bool:
    """Determine if a device supports reading item at index in system stats."""
    return (
        "system-stats" in hub.api.devices[obj_id].raw
        and hub.api.devices[obj_id].system_stats[stat_index] != ""
    )
        supported_fn=lambda hub, obj_id: device_system_stats_supported_fn(
            hub, obj_id, 0
        ),

@Kane610
Copy link
Member

Kane610 commented Apr 8, 2024

If you add index as well we can combine them to one method and you can use partial to define index to look for when instantiating the entity description. If that makes sense

Do you think on something like this

def device_system_stats_supported_fn(
    hub: UnifiHub, obj_id: str, stat_index: int
) -> bool:
    """Determine if a device supports reading item at index in system stats."""
    return (
        "system-stats" in hub.api.devices[obj_id].raw
        and hub.api.devices[obj_id].system_stats[stat_index] != ""
    )
       supported_fn=lambda hub, obj_id: device_system_stats_supported_fn(
           hub, obj_id, 0
       ),

Exactly, I think you might need to put the stat index first and you can skip the lambda and do partial which is shorter

supported_fn=partial(device_system_stats_supported_fn, 0),

@kimdv kimdv force-pushed the kimdv/add-system_states-to-device-sensor branch from 55aa868 to e72f8f9 Compare April 8, 2024 08:10
@kimdv kimdv force-pushed the kimdv/add-system_states-to-device-sensor branch from e72f8f9 to 7c39f76 Compare April 8, 2024 08:13
@kimdv
Copy link
Contributor Author

kimdv commented Apr 8, 2024

Exactly, I think you might need to put the stat index first and you can skip the lambda and do partial which is shorter

supported_fn=partial(device_system_stats_supported_fn, 0),

Didn't know that! Nice.
I've done it now and run pre-commit locally. Also unifi tests are ✅ locally

@Kane610
Copy link
Member

Kane610 commented Apr 8, 2024

Exactly, I think you might need to put the stat index first and you can skip the lambda and do partial which is shorter

supported_fn=partial(device_system_stats_supported_fn, 0),

Didn't know that! Nice. I've done it now and run pre-commit locally. Also unifi tests are ✅ locally

Skimmed through and it looked OK. Will review tonight

Copy link
Member

@Kane610 Kane610 left a comment

Choose a reason for hiding this comment

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

Awesome! Thanks

@Kane610 Kane610 merged commit f23e48f into home-assistant:dev Apr 8, 2024
19 of 20 checks passed
@kimdv kimdv deleted the kimdv/add-system_states-to-device-sensor branch April 8, 2024 18:10
@kimdv
Copy link
Contributor Author

kimdv commented Apr 8, 2024

Awesome! Thanks

It's me that say thanks!

@Kane610 I've considered to look into if I could add the WAN tx/rx, so it's possible to see the usage of all connected devices.
Is that se same way as cpu and mem here? And is it possible?

@Kane610
Copy link
Member

Kane610 commented Apr 8, 2024

Awesome! Thanks

It's me that say thanks!

@Kane610 I've considered to look into if I could add the WAN tx/rx, so it's possible to see the usage of all connected devices. Is that se same way as cpu and mem here? And is it possible?

You're welcome to do it! Not exactly sure where, you probably need to figure out which device is the main gateway and maybe there it will have a port which represents WAN rx/tx

@github-actions github-actions bot locked and limited conversation to collaborators Apr 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants