-
-
Notifications
You must be signed in to change notification settings - Fork 34.1k
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
Add platform and sensors for Vultr VPS #9928
Conversation
|
||
self.assertFalse(setup) | ||
|
||
with pytest.raises(vol.Invalid): |
There was a problem hiding this comment.
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="{}") |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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="{}") |
There was a problem hiding this comment.
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]) | ||
|
There was a problem hiding this comment.
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?", |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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?", |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dito.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.""" |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
…red binary_sensor and hub too
- Refactored sensor, binary_sensor, and switch to reference one subscription - Renamed CURRENT_BANDWIDTH_GB monitored condition to CURRENT_BANDWIDTH_USED - Improved test coverage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 🐦
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):Checklist:
If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
tox
run successfully. Your PR cannot be merged unless tests passREQUIREMENTS
variable (example).requirements_all.txt
by runningscript/gen_requirements_all.py
.