2019-02-14 04:35:12 +00:00
|
|
|
"""Support for monitoring OctoPrint binary sensors."""
|
2021-10-22 13:25:12 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from abc import abstractmethod
|
2016-05-04 01:35:11 +00:00
|
|
|
import logging
|
2016-09-02 10:26:23 +00:00
|
|
|
|
2021-10-22 13:25:12 +00:00
|
|
|
from pyoctoprintapi import OctoprintPrinterInfo
|
2016-05-04 01:35:11 +00:00
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
from homeassistant.components.binary_sensor import BinarySensorEntity
|
2021-10-22 13:25:12 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
)
|
2016-09-02 10:26:23 +00:00
|
|
|
|
2021-10-22 13:25:12 +00:00
|
|
|
from .const import DOMAIN as COMPONENT_DOMAIN
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2016-09-02 10:26:23 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-05-04 01:35:11 +00:00
|
|
|
|
|
|
|
|
2021-10-22 13:25:12 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the available OctoPrint binary sensors."""
|
2021-10-22 13:25:12 +00:00
|
|
|
coordinator: DataUpdateCoordinator = hass.data[COMPONENT_DOMAIN][
|
|
|
|
config_entry.entry_id
|
|
|
|
]["coordinator"]
|
|
|
|
device_id = config_entry.unique_id
|
|
|
|
|
|
|
|
assert device_id is not None
|
|
|
|
|
|
|
|
entities: list[BinarySensorEntity] = [
|
|
|
|
OctoPrintPrintingBinarySensor(coordinator, device_id),
|
|
|
|
OctoPrintPrintingErrorBinarySensor(coordinator, device_id),
|
|
|
|
]
|
|
|
|
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
|
|
|
|
class OctoPrintBinarySensorBase(CoordinatorEntity, BinarySensorEntity):
|
2016-05-07 01:03:18 +00:00
|
|
|
"""Representation an OctoPrint binary sensor."""
|
2016-05-04 01:35:11 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def __init__(
|
2021-10-22 13:25:12 +00:00
|
|
|
self,
|
|
|
|
coordinator: DataUpdateCoordinator,
|
|
|
|
sensor_type: str,
|
|
|
|
device_id: str,
|
|
|
|
) -> None:
|
2016-05-04 01:35:11 +00:00
|
|
|
"""Initialize a new OctoPrint sensor."""
|
2021-10-22 13:25:12 +00:00
|
|
|
super().__init__(coordinator)
|
|
|
|
self._device_id = device_id
|
2021-10-29 11:21:57 +00:00
|
|
|
self._attr_name = f"OctoPrint {sensor_type}"
|
2021-10-22 19:41:06 +00:00
|
|
|
self._attr_unique_id = f"{sensor_type}-{device_id}"
|
2021-10-22 13:25:12 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Device info."""
|
|
|
|
return {
|
|
|
|
"identifiers": {(COMPONENT_DOMAIN, self._device_id)},
|
2021-10-29 11:21:57 +00:00
|
|
|
"manufacturer": "OctoPrint",
|
|
|
|
"name": "OctoPrint",
|
2021-10-22 13:25:12 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:35:11 +00:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if binary sensor is on."""
|
2021-10-22 13:25:12 +00:00
|
|
|
printer = self.coordinator.data["printer"]
|
|
|
|
if not printer:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return bool(self._get_flag_state(printer))
|
2016-05-04 01:35:11 +00:00
|
|
|
|
|
|
|
@property
|
2021-10-22 13:25:12 +00:00
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if entity is available."""
|
|
|
|
return self.coordinator.last_update_success and self.coordinator.data["printer"]
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def _get_flag_state(self, printer_info: OctoprintPrinterInfo) -> bool | None:
|
|
|
|
"""Return the value of the sensor flag."""
|
|
|
|
|
|
|
|
|
|
|
|
class OctoPrintPrintingBinarySensor(OctoPrintBinarySensorBase):
|
|
|
|
"""Representation an OctoPrint binary sensor."""
|
|
|
|
|
|
|
|
def __init__(self, coordinator: DataUpdateCoordinator, device_id: str) -> None:
|
|
|
|
"""Initialize a new OctoPrint sensor."""
|
|
|
|
super().__init__(coordinator, "Printing", device_id)
|
|
|
|
|
|
|
|
def _get_flag_state(self, printer_info: OctoprintPrinterInfo) -> bool | None:
|
|
|
|
return bool(printer_info.state.flags.printing)
|
|
|
|
|
|
|
|
|
|
|
|
class OctoPrintPrintingErrorBinarySensor(OctoPrintBinarySensorBase):
|
|
|
|
"""Representation an OctoPrint binary sensor."""
|
|
|
|
|
|
|
|
def __init__(self, coordinator: DataUpdateCoordinator, device_id: str) -> None:
|
|
|
|
"""Initialize a new OctoPrint sensor."""
|
|
|
|
super().__init__(coordinator, "Printing Error", device_id)
|
|
|
|
|
|
|
|
def _get_flag_state(self, printer_info: OctoprintPrinterInfo) -> bool | None:
|
|
|
|
return bool(printer_info.state.flags.error)
|