Add Obihai reboot button (#88849)

* Obihai: Add reboot service

* Switch to button

* Remove button.py from coverage

* Update homeassistant/components/obihai/const.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update homeassistant/components/obihai/button.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update homeassistant/components/obihai/button.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* PR Feedback

* Cleanup some typehints

* As a class attr

---------

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
Emory Penney 2023-03-02 22:31:56 -08:00 committed by GitHub
parent a689ce7283
commit 1cb1dfa456
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 3 deletions

View File

@ -809,6 +809,7 @@ omit =
homeassistant/components/nx584/alarm_control_panel.py
homeassistant/components/oasa_telematics/sensor.py
homeassistant/components/obihai/__init__.py
homeassistant/components/obihai/button.py
homeassistant/components/obihai/connectivity.py
homeassistant/components/obihai/sensor.py
homeassistant/components/octoprint/__init__.py

View File

@ -0,0 +1,59 @@
"""Obihai button module."""
from __future__ import annotations
from pyobihai import PyObihai
from homeassistant.components.button import (
ButtonDeviceClass,
ButtonEntity,
ButtonEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_platform
from .connectivity import ObihaiConnection
from .const import OBIHAI
BUTTON_DESCRIPTION = ButtonEntityDescription(
key="reboot",
name=f"{OBIHAI} Reboot",
device_class=ButtonDeviceClass.RESTART,
entity_category=EntityCategory.CONFIG,
)
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: entity_platform.AddEntitiesCallback,
) -> None:
"""Set up the Obihai sensor entries."""
username = entry.data[CONF_USERNAME]
password = entry.data[CONF_PASSWORD]
host = entry.data[CONF_HOST]
requester = ObihaiConnection(host, username, password)
await hass.async_add_executor_job(requester.update)
buttons = [ObihaiButton(requester.pyobihai, requester.serial)]
async_add_entities(buttons, update_before_add=True)
class ObihaiButton(ButtonEntity):
"""Obihai Reboot button."""
entity_description = BUTTON_DESCRIPTION
def __init__(self, pyobihai: PyObihai, serial: str) -> None:
"""Initialize monitor sensor."""
self._pyobihai = pyobihai
self._attr_unique_id = f"{serial}-reboot"
def press(self) -> None:
"""Press button."""
if not self._pyobihai.call_reboot():
raise HomeAssistantError("Reboot failed!")

View File

@ -45,7 +45,7 @@ class ObihaiConnection:
self.host = host
self.username = username
self.password = password
self.serial: list = []
self.serial: str
self.services: list = []
self.line_services: list = []
self.call_direction: list = []

View File

@ -12,4 +12,4 @@ OBIHAI = "Obihai"
LOGGER = logging.getLogger(__package__)
PLATFORMS: Final = [Platform.SENSOR]
PLATFORMS: Final = [Platform.BUTTON, Platform.SENSOR]

View File

@ -3,6 +3,7 @@ from __future__ import annotations
from datetime import timedelta
from pyobihai import PyObihai
import voluptuous as vol
from homeassistant.components.sensor import (
@ -89,7 +90,7 @@ async def async_setup_entry(
class ObihaiServiceSensors(SensorEntity):
"""Get the status of each Obihai Lines."""
def __init__(self, pyobihai, serial, service_name):
def __init__(self, pyobihai: PyObihai, serial: str, service_name: str) -> None:
"""Initialize monitor sensor."""
self._service_name = service_name
self._state = None