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

Introduced Iperf3 client sensor #14213

Merged
merged 11 commits into from
May 24, 2018
Merged

Conversation

tchellomello
Copy link
Contributor

@tchellomello tchellomello commented May 1, 2018

Description:

Introducing Iperf3 client sensor. With this sensor is possible to use iperf3 to test your network connection against a local or remote Iperf3 server.

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#5282

Pull request in home-assistant/hassio-build: home-assistant/hassio-build#77

Example entry for configuration.yaml (if applicable):

sensor:
  - platform: iperf3
    host: 192.168.169.6
    duration: 10 
    monitored_conditions:
      - download
      - upload
  • Download
    image

  • Upload
    image

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

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

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

  • New dependencies have been added to the REQUIREMENTS variable ([example][ex-requir]).
  • New dependencies are only imported inside functions that use them ([example][ex-import]).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

@tchellomello tchellomello changed the title Introduced Iperf3 client sensor **WIP** Introduced Iperf3 client sensor May 1, 2018
hass, self.update, second=config.get(CONF_SECOND),
minute=config.get(CONF_MINUTE), hour=config.get(CONF_HOUR),
day=config.get(CONF_DAY))
@property

Choose a reason for hiding this comment

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

expected 1 blank line, found 0

@property
def service_name(self):
"""Return the service name of the sensor."""
return slugify("{} {}".format('update_iperf3', self.iperf3_client.server))

Choose a reason for hiding this comment

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

line too long (82 > 79 characters)

ATTR_TEST_STATUS = 'Test Status'
ATTR_VERSION = 'Version'

CONF_ATTRIBUTION = 'Data retrived using Iperf3'
Copy link
Member

Choose a reason for hiding this comment

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

Typo: retrieved

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! 👍

@syssi
Copy link
Member

syssi commented May 1, 2018

LGTM just the config schema should be improved. What about a list of time periods instead of the bunch of config keys (hour, minute, second)?

@tchellomello
Copy link
Contributor Author

@syssi I decided to follow this format as it is already in use in Fastcom and Speedtest sensors. Maybe we can simplify it and then change in all related components. I'm planning to do it in the future. Thanks for your review.

CONF_MANUAL = 'manual'
CONF_DURATION = 'duration'
CONF_SERVER = 'server'
CONF_PORT = 'port'
Copy link
Member

Choose a reason for hiding this comment

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

Can you please check if some of those constants are present already in const.py?

vol.All(cv.ensure_list, [vol.All(vol.Coerce(int), vol.Range(0, 59))]),
vol.Optional(CONF_MINUTE, default=[0]):
vol.All(cv.ensure_list, [vol.All(vol.Coerce(int), vol.Range(0, 59))]),
vol.Optional(CONF_HOUR):
Copy link
Member

Choose a reason for hiding this comment

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

Please remove this. For platforms Home Assistant already allows users to override a scan interval by passing scan_interval: <duration> to the platform config. This is defined in PLATFORM_SCHEMA which you extend. To set a default, define a SCAN_INTERVAL = timedelta(minutes=30)

sensor.update()

for sensor in dev:
hass.services.register(DOMAIN, sensor.service_name, update)
Copy link
Member

Choose a reason for hiding this comment

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

All services need to be always prefixed with the platform name.

_LOGGER.error("Iperf3 sensor error: %s", result.error)

self.data = {
'download': STATE_UNKNOWN,
Copy link
Member

Choose a reason for hiding this comment

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

Don't use STATE_UNKNOWN, use None instead.

ATTR_PROTOCOL: STATE_UNKNOWN,
ATTR_REMOTE_HOST: STATE_UNKNOWN,
ATTR_REMOTE_PORT: STATE_UNKNOWN,
ATTR_VERSION: STATE_UNKNOWN,
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 not store static info in the attributes.

return ICON


class Iperf3Data(object):
Copy link
Member

Choose a reason for hiding this comment

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

Since there is only 1 per entity, why extract it into its own class?

@tchellomello
Copy link
Contributor Author

@balloob @fabaff guys, PR updated.

Just one observation is to create the service_name I used the slugfy function to use the host to append to the service name. This way if the user has more than 1 instance of the iperf3 sensor, it will allow the user to select which sensor to trigger manually.

image

I noticed that we have this issue when using the fastdotcom and speedtest components. Only the last instance processed will have its service register. If this approach gets accepted by the team, I'll submit 2 new PR's to fix this behavior on these 2 other platforms too.

Thanks guys

@syssi
Copy link
Member

syssi commented May 22, 2018

If the component has an entity_id (like yours) a service call should look like this:

service: sensor.iperf3_update
entity_id: sensor.iperf3_localhost

If you want to update all iperf3 sensors you could call sensor.iperf3_update without an entity_id. This is an example of the needed service handler: https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/light/lifx.py#L222-L234

Please prefix the service name with iperf3. (update_iperf3 -> iperf3_update).

self._state = round(self.result.sent_Mbps, 2)

@asyncio.coroutine
def async_added_to_hass(self):
Copy link
Member

@balloob balloob May 23, 2018

Choose a reason for hiding this comment

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

We should not restore state from the database since it can be queried from the device

@tchellomello
Copy link
Contributor Author

@syssi @balloob guys, thanks for the code review.

The service now has the correct name and also supports to pass an entity_id in case the user wants to update just a particular entity. I also removed the state restore option too.

@syssi
Copy link
Member

syssi commented May 24, 2018

@pvizeli This component requires the iperf3 binary > 3.0.6. What's needed to provide this dependency on hass.io?

@tchellomello
Copy link
Contributor Author

@syssi Thanks for your code review. I have actually already a PR ready to be submitted to add support to https://github.com/home-assistant/home-assistant/blob/dev/Dockerfile and I'm working on a separate PR too to HASS.io. I was waiting that to be merged so then I submit the next 2 ones.

@tchellomello
Copy link
Contributor Author

@syssi Updated this PR to make sure Iperf3 will add support to Home assistant docker image [1]. I'll submit a PR to HASS soon too.

[1] - https://hub.docker.com/r/homeassistant/home-assistant/

@tchellomello
Copy link
Contributor Author

@syssi @pvizeli PR for hassio-build added at home-assistant/hassio-build#77

@syssi syssi merged commit 36da82a into home-assistant:dev May 24, 2018
@tchellomello tchellomello deleted the iperf_dev branch May 24, 2018 07:26
@michaelarnauts
Copy link
Contributor

Actually, if you just need to install a package without compiling or other repositories, you should just add the package in the setup_prereqs file.

The separate scripts are for packages that need more work to install.

@tchellomello
Copy link
Contributor Author

Thanks @michaelarnauts I'll make sure on the next PRs to do it this way! 👍

@balloob balloob mentioned this pull request Jun 8, 2018
girlpunk pushed a commit to girlpunk/home-assistant that referenced this pull request Sep 4, 2018
@home-assistant home-assistant locked and limited conversation to collaborators Sep 5, 2018
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.

7 participants