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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add switch platform for hp ilo integration #32209

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions homeassistant/components/hp_ilo/switch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""Support for powering on/off server with HP iLO."""
import logging

import hpilo
Copy link
Member

Choose a reason for hiding this comment

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

Only import the login function and maybe rename it to something more self-explanatory.

import voluptuous as vol

from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
from homeassistant.const import (
CONF_HOST,
CONF_NAME,
CONF_PASSWORD,
CONF_PORT,
CONF_USERNAME,
STATE_OFF,
STATE_ON,
)
import homeassistant.helpers.config_validation as cv

_LOGGER = logging.getLogger(__name__)

DEFAULT_NAME = "HP ILO"
DEFAULT_PORT = 443

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
}
)


def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the HP iLO switch."""
hostname = config.get(CONF_HOST)
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 dict[key] for required config keys as well.

port = config.get(CONF_PORT)
login = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
name = config[CONF_NAME]

try:
hp_ilo = hpilo.Ilo(hostname=hostname, login=login, password=password, port=port)
except ValueError as error:
_LOGGER.error(error)
return

add_entities([HpIloSwitch(hass, name, hp_ilo)], True)


class HpIloSwitch(SwitchDevice):
"""Representation of a HP iLO switch."""

def __init__(self, hass, name, hp_ilo):
"""Initialize the HP iLO switch."""
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.

Self.hass is not used

self._name = name
self.hp_ilo = hp_ilo
Copy link
Member

Choose a reason for hiding this comment

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

Please rename this to client or server since it resembles the name of the library too much IMHO.

self._state = None

@property
def is_on(self):
"""Return true if switch is on."""
return self._state

@property
def name(self):
"""Return the name of the switch."""
return self._name

def turn_on(self, **kwargs):
"""Turn the device on."""
self.hp_ilo.set_host_power(True)

def turn_off(self, **kwargs):
"""Turn the device off."""
self.hp_ilo.set_host_power(False)

def update(self):
"""Update the server's power state."""
pwr_status = self.hp_ilo.get_host_power_status().lower()

if pwr_status in (STATE_ON, STATE_OFF):
self._state = pwr_status
else:
self._state = None