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 Synology SRM sensor to get information from router #31523

Closed
wants to merge 7 commits into from

Conversation

i00
Copy link
Contributor

@i00 i00 commented Feb 6, 2020

Proposed change

The current SRM integration is used for obtaining devices for tracking only ...
This adds sensor support so that additional information can be obtained from the router (that is not contained in SNMP).

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)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example entry for configuration.yaml:

# Example configuration.yaml
sensor:
  - platform: synology_srm
    host: HOSTNAME
    password: PASSWORD

    #Optional defaults are below:
    #name: synology_srm
    #port: 8001
    #username: admin
    #ssl: true
    #verify_ssl: false
    #monitored_conditions:
    #  - core.ddns_extip
    ##  OTHER POSSIBLE VALUES FOR MONITORED CONDTIONS:
    ##  - base.encryption
    ##  - base.info
    ##  - core.ddns_extip
    ##  - core.ddns_record
    ##  - core.system_utilization
    ##  - core.network_nsm_device
    ##  - mesh.network_wanstatus
    ##  - mesh.network_wifidevice
    ##  - mesh.system_info

Additional information

Information is returned in attributes, the state is the external IP address.

Here is an example of how to use this to implement entities for current WAN traffic:

sensor:
  - platform: synology_srm
    host: SynologyRouter.local
    password: !secret SynologyRouter
    monitored_conditions:
      - core.ddns_extip
      - core.system_utilization
  - platform: template
    sensors:
      wan_download:
        value_template: "{% set i = namespace() %}{% set i.i = 0 %}{% for item in states.sensor.synology_srm.attributes.core_system_utilization.network if item.device|regex_match('^(usbnet|ppp)', ignorecase=true) %}{% set i.i = i.i + item.rx %}{% endfor %}{{ ((i.i / 1024 / 1024) * 8)|round(2) }}"
        friendly_name: Download
        unit_of_measurement: Mbit/s
        icon_template: 'mdi:download'
      wan_upload:
        value_template: "{% set i = namespace() %}{% set i.i = 0 %}{% for item in states.sensor.synology_srm.attributes.core_system_utilization.network if item.device|regex_match('^(usbnet|ppp)', ignorecase=true) %}{% set i.i = i.i + item.tx %}{% endfor %}{{ ((i.i / 1024 / 1024) * 8)|round(2) }}"
        friendly_name: Upload
        unit_of_measurement: Mbit/s
        icon_template: 'mdi:upload'

Also ... I am not sure how to submit corresponding documentation along with this as it appears to be in another repo.

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
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:
Will do this up on the week end

If the code communicates with devices, web services, or third-party tools:
Does not 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.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

How do we get this scored? It isn't apparent in the docs

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

@homeassistant
Copy link
Contributor

Hi @i00,

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!

@probot-home-assistant
Copy link

Hey there @aerialls, mind taking a look at this pull request as its been labeled with a integration (synology_srm) you are listed as a codeowner for? Thanks!

@MartinHjelmare MartinHjelmare changed the title Add a sensor to Synology SRM so that it can get information from the router Add Synology SRM sensor to get information from router Feb 6, 2020
@i00
Copy link
Contributor Author

i00 commented Feb 7, 2020

I signed the CLA

... I also added an example of how to get WAN Traffic (mainly what I use it for)

@i00
Copy link
Contributor Author

i00 commented Feb 13, 2020

@frenck ... sorry internet has been down since last weekend ... got hit by heavy storms and only got it back yesterday ... will do the documentation this weekend ...

I may require some help later with the failing checks.

Thanks

@i00
Copy link
Contributor Author

i00 commented Feb 16, 2020

I created the documentation and updated the pull request
... sorry a little stuck on what to do now?
Thanks

@frenck
Copy link
Member

frenck commented Feb 16, 2020

@i00 Removed the docs-missing, thanks for addressing that.

For the other build issues:

  • CheckFormat: Format your code using Black: black --fast homeassistant tests
  • Lint: Check the output of the builds, to see what issues our linting has found, and address them.

@i00
Copy link
Contributor Author

i00 commented Feb 19, 2020

@frenck ... thanks will have a look at it this weekend :)

@i00
Copy link
Contributor Author

i00 commented Feb 26, 2020

@i00
Copy link
Contributor Author

i00 commented Mar 8, 2020

Shameful bump..
It builds fine and passed all of the pre-commit checks..
Why is the build failing on the CI?

@frenck
Copy link
Member

frenck commented Mar 9, 2020

Well, we can't see anymore, since the build logs are removed after some time. Please push an empty commit to re-trigger the build (or rebase the PR onto the latest dev branch).

@i00
Copy link
Contributor Author

i00 commented Mar 9, 2020

@frenck ... sorry didn't realize that the logs got nuked ... just did the empty commit :) ... still failing though ... any ideas?

Thanks a million 👍

@frenck
Copy link
Member

frenck commented Mar 9, 2020

You haven't updated the requirements.

See: https://dev.azure.com/home-assistant/Core/_build/results?buildId=30832&view=logs&j=f36f3843-8e6d-51cb-0e3a-71707ab37859&t=e02bf7dd-8cde-5cf1-60a6-6b38a38544a6&l=36

The message is pretty clear at the end:

Please run python3 -m script.gen_requirements_all

@i00
Copy link
Contributor Author

i00 commented Mar 9, 2020

Thanks @frenck ...
When I run that I get the following error:

root@HomeAssistantDev:/home-assistant# python3 -m script.gen_requirements_all
Traceback (most recent call last):
  File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home-assistant/script/gen_requirements_all.py", line 11, in <module>
    from script.hassfest.model import Integration
  File "/home-assistant/script/hassfest/model.py", line 7, in <module>
    import attr
ModuleNotFoundError: No module named 'attr'

Copy link
Member

frenck commented Mar 11, 2020

Yes, it needs to run from within the venv.

@i00
Copy link
Contributor Author

i00 commented Mar 11, 2020

Thanks for confirming that ... will do that next time :)
... and thanks for all your help with regards to this.

@i00
Copy link
Contributor Author

i00 commented Mar 26, 2020

@frenck .. sorry but any updates on this?
Thanks

@frenck
Copy link
Member

frenck commented Mar 26, 2020

Any update on what @i00? I don't believe you had a followup question?

If you are pinging me for a review, I would really want to advise you not to ping people for that. There are a lot of PRs open for review, so please be patient.

@i00
Copy link
Contributor Author

i00 commented Mar 26, 2020

Ok thanks, just wasn't sure if this was forgotten about :)

"live",
]

PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend(
Copy link
Member

Choose a reason for hiding this comment

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

We no longer accept additions or changes to the yaml config of integrations. See: https://github.com/home-assistant/architecture/blob/master/adr/0010-integration-configuration.md

This integration needs to be refactored to use a config flow and config entries.

https://developers.home-assistant.io/docs/config_entries_config_flow_handler

Copy link
Member

Choose a reason for hiding this comment

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

I agree, should move this platform based copmponent to an integration based.

Will avoid double config for one device.

Dev automation moved this from Needs review to Review in progress Apr 16, 2020
@MartinHjelmare MartinHjelmare self-assigned this Apr 19, 2020
@ahkaroly
Copy link

Hello,

I’m a beginner, but if there’s going to be an Installable Beta, I’ll be happy to help with the test with a 2600AC and a 2200AC router.

Thank you for your work.

@i00
Copy link
Contributor Author

i00 commented Apr 22, 2020

@ahkaroly, would be great; but at the moment the current version of it does not conform to the guidelines that were changed last week so the test would have to wait until I refactor it to accommodate this (will do when I take holidays next month).

However; it will still work with the current release; so if you want it now so you can use the integration right away you can do so by using this:
synology_srm_router.zip
I modified that to use the current version of synology_srm that is in Home Assistant.
Extract the files into "config\custom_components" so the files are in the subdirectory: "synology_srm_router"
You will also need to use: platform: synology_srm_router instead of "synology_srm" for this one to work.

I get most of the following out of it:
image
Let me know if you want to know how to do anything above ;).

Kris

@ahkaroly
Copy link

@i00

Thank you very much. I see it works. I will test and I will if I find any problems. But I can't find one thing. Does your image show traffic and Benchmark, the total amount of data used per day? Where can I find this? Is this calculated by the home assistant, or can the sensor also transfer router statistics?

@i00
Copy link
Contributor Author

i00 commented Apr 24, 2020

@i00

Thank you very much. I see it works. I will test and I will if I find any problems. But I can't find one thing. Does your image show traffic and Benchmark, the total amount of data used per day? Where can I find this? Is this calculated by the home assistant, or can the sensor also transfer router statistics?

No, it is the live traffic; see the Additional information in the OP that details how to create the wan_download & wan_upload sensors for this. I had to strip out the historical traffic as it is not supported in the current version in HA.
The benchmarks are from the Speedtest.net integration.

@ahkaroly
Copy link

@i00
Thank you very much. I see it works. I will test and I will if I find any problems. But I can't find one thing. Does your image show traffic and Benchmark, the total amount of data used per day? Where can I find this? Is this calculated by the home assistant, or can the sensor also transfer router statistics?

No, it is the live traffic; see the Additional information in the OP that details how to create the wan_download & wan_upload sensors for this. I had to strip out the historical traffic as it is not supported in the current version in HA.
The benchmarks are from the Speedtest.net integration.

Sorry, I watched the speedtest as day traffic.

The current traffic is working perfectly :)

Can't read monthly internet traffic with SNMP?

@i00
Copy link
Contributor Author

i00 commented Apr 25, 2020

Can't read monthly internet traffic with SNMP?

Nope ... can basically read noting useful with SNMP with Synology SRM... Seems to be geared around general status rather than actual good routing information.

@HummelsTech
Copy link

HummelsTech commented May 3, 2020

@i00

Thanks for the great Job.
Can you please tell me how you extract all info in your lovelace,
I have now 1 sensor with all attributes but I've no idea how can I extract them.
Yes I saw the template but how can I template to extract core.network_nsm_device for example

Thanks for your help

@i00
Copy link
Contributor Author

i00 commented May 4, 2020

@AlwinHummels ; I take it you are talking about the device list on the left?
I updated the SRM device tracker to include this additional information; it was released in in 0.108+.
... so my config looks something like this:

device_tracker:
  - platform: synology_srm
    host: SynologyRouter.local
    password: !secret SynologyRouter
    consider_home: 60 # default 180
    new_device_defaults:
      track_new_devices: true

In Lovelace I use this for the list of devices (bit messy but it does the job):

card:
  show_header_toggle: false
  title: Devices
  type: entities
filter:
  include:
    - attributes:
        scanner: SynologySrmDeviceScanner
      entity_id: device_tracker.*
      options:
        entities:
          - attribute: mac
          - attribute: ip_addr
          - icon: 'mdi:ethernet'
          - icon: 'mdi:wifi-strength-1'
          - icon: 'mdi:wifi-strength-2'
          - icon: 'mdi:wifi-strength-4'
          - icon: 'mdi:gamepad-variant-outline'
          - icon: 'mdi:cellphone-android'
          - icon: 'mdi:television'
          - icon: 'mdi:dlna'
          - icon: 'mdi:desktop-tower'
          - icon: 'mdi:nas'
          - icon: 'mdi:laptop-chromebook'
          - icon: 'mdi:tablet-android'
          - icon: 'mdi:cctv'
          - icon: 'mdi:printer'
          - icon: 'mdi:router-wireless'
        icon: 'mdi:devices'
        secondary_info:
          attribute: band
        show_state: false
        style: >
          div.entity:nth-child(1),div.entity:nth-child(2) {
            margin-right:0;
          } div.entity:nth-child({% if state_attr(config.entity,
          'ip_addr') == '' %}1{% else %}2{% endif %}) {
            display: none !important;
            visibility: hidden !important;
            position: absolute !important;
          } .secondary {
            padding-left:16px;
          } {% if state_attr(config.entity, 'is_wireless') == false %}
          .secondary {
            visibility: hidden;
          } .secondary:before {
            content: 'Ethernet';
            visibility: visible;
          } {% else %} .secondary:after {
            content: ' (Level {% if state_attr(config.entity, 'mesh_node_id') == 0 %}1{% elif state_attr(config.entity, 'mesh_node_id') == 1 %}3{% elif state_attr(config.entity, 'mesh_node_id') == 3 %}4{% else %}?{% endif %})';
          } {% endif %}


          div.entity:nth-child(4),div.entity:nth-child(5),div.entity:nth-child(6),div.entity:nth-child(7)
          {
            --iron-icon-height: 16px;
            position: absolute;
            top: 8px;
            left: 50px;
            visibility: hidden;
          } div.entity:nth-child({% if state_attr(config.entity,
          'is_wireless') == false %}4{% elif state_attr(config.entity,
          'rate_quality') == 'low' %}5{% elif state_attr(config.entity,
          'rate_quality') == 'middle' %}6{% elif
          state_attr(config.entity, 'rate_quality') == 'high' %}7{%
          endif %}) {
              visibility: visible;
          } :host>state-badge {
            {% if state_attr(config.entity, 'dev_type') != 'others' and state_attr(config.entity, 'dev_type') != 'default' %}
              visibility: hidden;
            {% endif %}
          } div.entity:nth-child(n+8) {
            position: absolute;
            top: 0px;
            left: 8px;
            visibility: hidden;
          } div.entity:nth-child({% if state_attr(config.entity,
          'dev_type') == 'gamebox' %}8{% elif state_attr(config.entity,
          'dev_type') == 'phone' %}9{% elif state_attr(config.entity,
          'dev_type') == 'tv' %}10{% elif state_attr(config.entity,
          'dev_type') == 'dlna' %}11{% elif state_attr(config.entity,
          'dev_type') == 'computer' %}12{% elif
          state_attr(config.entity, 'dev_type') == 'nas' %}13{% elif
          state_attr(config.entity, 'dev_type') == 'notebook' %}14{%
          elif state_attr(config.entity, 'dev_type') == 'tablet' %}15{%
          elif state_attr(config.entity, 'dev_type') == 'ipcam' %}16{%
          elif state_attr(config.entity, 'dev_type') == 'printer' %}17{%
          elif state_attr(config.entity, 'dev_type') == 'router' %}18{%
          endif %}) {
              visibility: visible;
          }
        type: 'custom:multiple-entity-row'
      state: home
sort:
  ignore_case: true
  method: name
type: 'custom:auto-entities'

The Lovelace stuff requires the following:

Note that I have hard-coded the mesh IDs to the name (Level 1,3,4) ... so you may need to fiddle with that ... search for Level in my Lovelace config.

Hope that helps,
Kris

EDIT: Also I am not sure if the version that I have here is compatible with the current srm version that I had merged in to the current version of HA ... if there are issues let me know ... I have a version that works with it.

@ghost
Copy link

ghost commented May 4, 2020

@ahkaroly, would be great; but at the moment the current version of it does not conform to the guidelines that were changed last week so the test would have to wait until I refactor it to accommodate this (will do when I take holidays next month).

However; it will still work with the current release; so if you want it now so you can use the integration right away you can do so by using this:
synology_srm_router.zip
I modified that to use the current version of synology_srm that is in Home Assistant.
Extract the files into "config\custom_components" so the files are in the subdirectory: "synology_srm_router"
You will also need to use: platform: synology_srm_router instead of "synology_srm" for this one to work.

I get most of the following out of it:
image
Let me know if you want to know how to do anything above ;).

Kris

Hi
Can you write step by step how add this as ypou show on screenshot ?

Are you planing to add it to official integration of Home Assistant ?

@HummelsTech
Copy link

@i00 Thanks a lot for your help, can you please share the code for the other cards too.
It looks very great.

@i00 i00 mentioned this pull request May 6, 2020
@i00
Copy link
Contributor Author

i00 commented May 6, 2020

@Pepeel @AlwinHummels ... I feel this is a little off-topic ... so I have started a chatter thread here ... feel free to leave any questions there.

Also I have time off coming up from the 8th of June ... so will probably finish this push off then.

Copy link
Member

@Quentame Quentame left a comment

Choose a reason for hiding this comment

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

I think we should focus on #35412 first.

Then update this PR accordingly.

),
vol.Optional(
CONF_MONITORED_CONDITIONS, default=DEFAULT_MONITORED_CONDITIONS
): vol.All(cv.ensure_list, [vol.In(POSSIBLE_MONITORED_CONDITIONS)]),
Copy link
Member

Choose a reason for hiding this comment

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

We should avoid monitored_contitions from https://github.com/home-assistant/architecture/blob/master/adr/0003-monitor-condition-and-data-selectors.md

Is your different conditions are making a specific fetch ?

Or maybe you can use entity_registry_enabled_default https://developers.home-assistant.io/docs/core/entity#advanced-properties

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes ... they make separate requests ... so I think this should remain.


def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Synology SRM Sensor."""
add_devices([SynologySrm(config)])
Copy link
Member

Choose a reason for hiding this comment

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

This does not allow to automatically add a new SynologySrm sensor entity automatically when a new device connects to the router.

See #30334 freebox/device_tracker or #35412 synology_srm/device_tracker

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not a device tracker; this is to obtain information from the router; the existing SRM integration can be used for device tracking.

Copy link
Member

Choose a reason for hiding this comment

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

I know this is not a device_tracker, but the behavior is the same.
You can actually see it in icloud/sensor.

But since when you have a new device connected to your router, you have to create a sensor, this should work like this.

"live",
]

PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend(
Copy link
Member

Choose a reason for hiding this comment

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

I agree, should move this platform based copmponent to an integration based.

Will avoid double config for one device.

@Quentame
Copy link
Member

Just reviewed #35412

@stale
Copy link

stale bot commented Jul 3, 2020

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
Thank you for your contributions.

@stale stale bot added the stale label Jul 3, 2020
@stale stale bot closed this Jul 11, 2020
Dev automation moved this from Review in progress to Cancelled Jul 11, 2020
@Quentame Quentame mentioned this pull request Aug 3, 2020
20 tasks
@romanchuvilin86
Copy link

Can anyone help with the download/upload speed sensors config?
The value was suggested:

value_template: "{% set i = namespace() %}{% set i.i = 0 %}{% for item in states.sensor.synology_srm.attributes.core_system_utilization.network if item.device|regex_match('^(usbnet|ppp)', ignorecase=true) %}{% set i.i = i.i + item.rx %}{% endfor %}{{ ((i.i / 1024 / 1024) * 8)|round(2) }}"

From my sensor.synology_srm respond i see the WAN load load under "device: lbr0". How can i get it separately from other numbers?

core_get_system_utilization:
network:

  • device: total
    rx: 40836
    tx: 69326
  • device: bwlan0
    rx: 1408
    tx: 2305
  • device: bwlan1
    rx: 2827
    tx: 31845
  • device: eth0
    rx: 30108
    tx: 3185
  • device: lbr0
    rx: 6246
    tx: 31914
  • device: usbnet0
    rx: 247
    tx: 77
    time: 1597242312

@MartinHjelmare
Copy link
Member

MartinHjelmare commented Aug 12, 2020

Please open an issue if you suspect a bug. If you need help please use our help channels:
https://home-assistant.io/help/#communication-channels

PRs should not be used for support or bug reports. Thanks!

@home-assistant home-assistant locked and limited conversation to collaborators Aug 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Dev
  
Cancelled
Development

Successfully merging this pull request may close these issues.

None yet

8 participants