8000 whois domain lookup sensor by GenericStudent · Pull Request #10000 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

whois domain lookup sensor #10000

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

Merged
merged 12 commits into from
Oct 24, 2017
Merged

whois domain lookup sensor #10000

merged 12 commits into from
Oct 24, 2017

Conversation

GenericStudent
Copy link
Contributor
@GenericStudent GenericStudent commented Oct 20, 2017

Description:

This is at the "test the water" PR stage in that I have developed the basics of the sensor but wish to get feedback on whether this is a suitable component for HA.

I've used the pythonwhois library that comes with a wtfpl licence, and have pinned the SCAN_INTERVAL at 24 hours as we don't want to break the WHOIS TOS with high volume requests. I feel that one request per domain every 24 hours should be ok.

Is this a desirable feature and should I fully flesh it out with tests / doc PR?

Current State

Creates a sensor for each host with the following information:

  • sensor state is the number of days until expiration / Expired if <= 0
  • attributes:
  • name_servers
  • registrar
  • updated (date time)
  • expires (date time)

Any feedback much welcomed :)

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

Example entry for configuration.yaml (if applicable):

sensors:
  - platform: whois
    domain: example.net
    name: primary

Checklist:

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

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

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

def unit_of_measurement(self):
"""The unit of measurement to present the value in."""
if self._expired:
return None
Copy link
Member

Choose a reason for hiding this comment

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

This will mess-up the history.

self.hass = hass
self._hostname = hostname
self.whois = get_whois
self._state = 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.

Set it to None and let Home Assistant handle the rest.

"""Get the more info attributes."""
if self._data:
updated_formatted = self._updated_date.strftime(
'%Y-%m-%d %H:%M:%S')
Copy link
Member

Choose a reason for hiding this comment

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

Please use the ISO 8601 format.

if self._expired:
return None

return 'day{}'.format('' if self._state == 1 else 's')
Copy link
Member

Choose a reason for hiding this comment

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

The unit of measurement will change if only one day is left. This will make it hard for people who are exporting the data as the sensor will go for days to day and then to .

# every 24 hours shouldn't count as "high volume"

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): vol.All(cv.ensure_list, [cv.string]),
Copy link
Member

Choose a reason for hiding this comment

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

Please allow the user to choose a name for the sensor. Hint: CONF_NAME.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Which style should I go for, Instantiate this sensor with only a single host, such as:

sensor:
  - platform: whois
    hostname: example.net
    name: primary
  - platform: whois
    hostname: example.com
    name: secondary

or keep the list of hostnames and add the per host name config, such as:

sensor:
  - platform: whois
    hostnames:
      - url: example.net
        name: primary
      - url: example.com
        name: secondary

I can see merits in both but was curious if there was a process / design used to decide which one to go for.

Thanks for your feedback on this all, much appreciated.

Copy link
Member

Choose a reason for hiding this comment

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

I personally prefer the first style (easier for copy-&-paste) but this is pretty much up to the person who is writing the platform.

"""Initialize the sensor."""
from pythonwhois import get_whois

self.hass = hass
Copy link
Member

Choose a reason for hiding this comment

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

Not used.


def update(self):
"""Get the current WHOIS data for hostname."""
response = self.whois(self._hostname, normalized=True)
Copy link
Member

Choose a reason for hiding this comment

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

I guess that there will be an exception if the domain doesn't exist. You need to catch it.

devices = []

for hostname in hostnames:
devices.append(WhoisSensor(hass, hostname))
Copy link
Member

Choose a reason for hiding this comment

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

You should make sure that the domain exists before setting up the platform otherwise the users will end up if a non-working sensor.

- Now more resiliant to invalid hostnames
- Removed various assumed STATE_UNKOWN setting
- Upfront check for valid hostname preventing the sensor starting with dud
- Resolved unit of measurement Day, Days, None issue
- Datetime formatting now done to iso 8601 standard
- Removed all expired usage, not really that useful
- Unused hass assignment
@balloob balloob mentioned this pull request Oct 21, 2017
@pvizeli pvizeli changed the title Feature / WIP: whois domain lookup WIP: whois domain lookup Oct 22, 2017
- Resolved the dilemma with hosts / single host per sensor. Now running
single domain per sensor.
- Renamed host(s) to domain
@fabaff fabaff changed the title WIP: whois domain lookup whois domain lookup sensor Oct 23, 2017
.coveragerc Outdated
@@ -223,6 +223,8 @@ omit =
homeassistant/components/wemo.py
homeassistant/components/*/wemo.py

homeassistant/components/sensor/whois.py
Copy link
Member

Choose a reason for hiding this comment

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

Please move it down to the platforms.

name = config.get(CONF_NAME)

try:
if 'expiration_date' in get_whois(domain, normalized=True):
Copy link
Member

Choose a reason for hiding this comment

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

Especially for European domains is the expiration_date not always present. So the lookup is working but we will not get the data.

Copy link
Member

Choose a reason for hiding this comment

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

Not much we can do here. Perhaps re-phrasing the warning.

self._name_servers = self._data['nameservers']

self._expiration_date = self._data['expiration_date'][0]
self._updated_date = self._data['updated_date'][0]
Copy link
Member

Choose a reason for hiding this comment

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

With home-assistant.io I get:

  File "/home/fab/Documents/repos/home-assistant/homeassistant/components/sensor/whois.py", line 131, in update
    self._updated_date = self._data['updated_date'][0]
KeyError: 'updated_date'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems my assumption of its existence was incorrect, I've made it conditional now. Resolved the other comments too.

@fabaff fabaff merged commit 485e81d into home-assistant:dev Oct 24, 2017
@balloob balloob mentioned this pull request Nov 2, 2017
@GenericStudent GenericStudent deleted the feature/whois branch November 2, 2017 21:44
@home-assistant home-assistant locked and limited conversation to collaborators Mar 2, 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.

5 participants
0