Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add platform and sensors for Vultr VPS #9928

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 32 commits into from
Nov 5, 2017
Merged

Add platform and sensors for Vultr VPS #9928

merged 32 commits into from
Nov 5, 2017

Conversation

GenericStudent
Copy link
Contributor

@GenericStudent GenericStudent commented Oct 17, 2017

Description:

Allows the Vultr VPS provider to be used as a hub, binary sensor (representing currently running or not of each subscription (vps), attributes being everything static about the vps), sensor (two types for the moment representing current bandwidth usage this period in GB and the current pending charges in US$), switch (allow the VPS to be started or stopped, also contains all the attributes the binary sensor does)

This is my first platform / sensor creation so I would love any feedback. It was stitched together with reference code from the DigitalOcean platform and various other components.

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

Example entry for configuration.yaml (if applicable):

vultr:
  api_key: ABCDEF123456

binary_sensor:
  - platform: vultr
    name: My Amazing Server
    subscription: 123456

sensor:
  - platform: vultr
    subscription: 123456
    monitored_conditions:
      - current_bandwidth_used
      - pending_charges

switch:
  - platform: vultr
    name: Web Server 17
    subscription: 123456

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.


self.assertFalse(setup)

with pytest.raises(vol.Invalid):

Choose a reason for hiding this comment

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

undefined name 'pytest'
undefined name 'vol'

def test_invalid_sensor(self, mock):
"""Test the Vultr sensor class and methods."""
mock.get('https://api.vultr.com/v1/account/info',
text="{}")

Choose a reason for hiding this comment

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

continuation line under-indented for visual indent


self.assertFalse(setup)

with pytest.raises(vol.Invalid):

Choose a reason for hiding this comment

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

undefined name 'pytest'
undefined name 'vol'

def test_invalid_sensor(self, mock):
"""Test the Vultr sensor class and methods."""
mock.get('https://api.vultr.com/v1/account/info',
text="{}")

Choose a reason for hiding this comment

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

continuation line under-indented for visual indent

device_attrs[ATTR_CREATED_AT])
self.assertEqual('123456',
device_attrs[ATTR_SUBSCRIPTION_ID])

Choose a reason for hiding this comment

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

blank line at end of file

add_devices([VultrBinarySensor(vultr, subscription, name)], True)
else:
_LOGGER.error(
"Subscription %s not found. Perhaps API key issue?",
Copy link
Member

Choose a reason for hiding this comment

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

Leave Perhaps API key issue? out because I could also be that the subscription doesn't exist.

sensors = []

for condition in monitored_conditions:
if condition not in 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.

This will never happen as the schema validation only allows entries in MONITORED_CONDITIONS.

name))
else:
_LOGGER.error(
"Subscription %s not found. Perhaps API key issue?",
Copy link
Member

Choose a reason for hiding this comment

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

Dito

self._vultr = vultr
self._subscription = subscription
self._var_id = variable
self.data = self._vultr.data[subscription]
Copy link
Member

@fabaff fabaff Nov 2, 2017

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 update set the state.

subscription = config.get(CONF_SUBSCRIPTION)
name = config.get(CONF_NAME)

if subscription in vultr.data:
Copy link
Member

@fabaff fabaff Nov 2, 2017

Choose a reason for hiding this comment

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

Go for not in and log the error and return false as a guard clause. Now the else: can be avoided.

subscription = config.get(CONF_SUBSCRIPTION)
name = config.get(CONF_NAME)

if subscription in vultr.data:
Copy link
Member

Choose a reason for hiding this comment

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

Dito

self._vultr = vultr
self._subscription = subscription
self.data = self._vultr.data[subscription]
self._state = None
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.

"""Initialize a new Vultr switch."""
self._vultr = vultr
self._subscription = subscription
self.data = self._vultr.data[subscription]
Copy link
Member

Choose a reason for hiding this comment

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

Dito.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With the switch component, I use this self.data immediately to format the name, allowing the default name of Vultr {} to be populated with the subscription's label. How do you think I should handle this? I could not use the label at all and default to Vultr subscription or something. Could move the name setting into update method. Could set data to None, but access the self._vultr.data[subscription]['label'] directly in this init method.

Thanks for you assistance with my PRs, it's always much appreciated and makes me proud to contribute to such a cool project.

Copy link
Member

Choose a reason for hiding this comment

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

Now I see the idea. As far as I can tell, is it required that the user provide the right string at the moment. Only something like MyName {} will work. If MyName is set then there will be an IndexError. My guess is that something like this could work:

    [...]
     self._name = name

@property
def name(self):
"""Return the name of the switch."""
    try:
        return self._name.format(self.data['label'])
    except IndexError:
        return self._name



class VultrSwitch(SwitchDevice):
"""Representation of a Vultr subsciption switch."""
Copy link
Member

Choose a reason for hiding this comment

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

Typo.

return self._name

@property
def icon(self):
Copy link
Member

Choose a reason for hiding this comment

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

Use def device_class() for binary sensors.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking through the device_class types, the only one that jumps out to me is power, I notice the digitalocean binary sensor has moving. What would you suggest to pin it as?

Copy link
Member

Choose a reason for hiding this comment

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

If you prefer power then go with that. User can always overwrite it.

Have a working Vultr hub and binary sensor which pulls down the
following attributes of your VPS:
 - Date created
 - Subscription id (server id)
 - Cost per month (in US$)
 - Operating System installed
 - IPv4 address
 - label (human readable name)
 - region
 - number of vcpus
 - which storage package chosen
 - IPV6 address (if applicable)
 - RAM amount

Working next on sensor and then testing / coverage.
Copy link
Member

@fabaff fabaff left a comment

Choose a reason for hiding this comment

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

Thanks 🐦

@fabaff fabaff merged commit 72ce9ec into home-assistant:dev Nov 5, 2017
@balloob balloob mentioned this pull request Nov 17, 2017
@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