diff --git a/.coveragerc b/.coveragerc index 5da330bb20a..ce80bef0a93 100644 --- a/.coveragerc +++ b/.coveragerc @@ -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 diff --git a/homeassistant/components/obihai/button.py b/homeassistant/components/obihai/button.py new file mode 100644 index 00000000000..0b84d40f4d2 --- /dev/null +++ b/homeassistant/components/obihai/button.py @@ -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!") diff --git a/homeassistant/components/obihai/connectivity.py b/homeassistant/components/obihai/connectivity.py index 4a5c25b2101..93eeccd1bb7 100644 --- a/homeassistant/components/obihai/connectivity.py +++ b/homeassistant/components/obihai/connectivity.py @@ -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 = [] diff --git a/homeassistant/components/obihai/const.py b/homeassistant/components/obihai/const.py index 90bcd7736f8..764534d4791 100644 --- a/homeassistant/components/obihai/const.py +++ b/homeassistant/components/obihai/const.py @@ -12,4 +12,4 @@ OBIHAI = "Obihai" LOGGER = logging.getLogger(__package__) -PLATFORMS: Final = [Platform.SENSOR] +PLATFORMS: Final = [Platform.BUTTON, Platform.SENSOR] diff --git a/homeassistant/components/obihai/sensor.py b/homeassistant/components/obihai/sensor.py index 7524fbc7d47..61411b0ce27 100644 --- a/homeassistant/components/obihai/sensor.py +++ b/homeassistant/components/obihai/sensor.py @@ -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