mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Add binary sensor platform to Dremel 3D Printer (#93881)
Add binary sensor platform to Dremel
This commit is contained in:
parent
198608906c
commit
226647f625
@ -12,7 +12,7 @@ from homeassistant.exceptions import ConfigEntryNotReady
|
|||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import Dremel3DPrinterDataUpdateCoordinator
|
from .coordinator import Dremel3DPrinterDataUpdateCoordinator
|
||||||
|
|
||||||
PLATFORMS = [Platform.SENSOR]
|
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||||
|
74
homeassistant/components/dremel_3d_printer/binary_sensor.py
Normal file
74
homeassistant/components/dremel_3d_printer/binary_sensor.py
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
"""Support for monitoring Dremel 3D Printer binary sensors."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from dremel3dpy import Dremel3DPrinter
|
||||||
|
|
||||||
|
from homeassistant.components.binary_sensor import (
|
||||||
|
BinarySensorDeviceClass,
|
||||||
|
BinarySensorEntity,
|
||||||
|
BinarySensorEntityDescription,
|
||||||
|
)
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
from .const import DOMAIN
|
||||||
|
from .entity import Dremel3DPrinterEntity
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Dremel3DPrinterBinarySensorEntityMixin:
|
||||||
|
"""Mixin for Dremel 3D Printer binary sensor."""
|
||||||
|
|
||||||
|
value_fn: Callable[[Dremel3DPrinter], bool]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Dremel3DPrinterBinarySensorEntityDescription(
|
||||||
|
BinarySensorEntityDescription, Dremel3DPrinterBinarySensorEntityMixin
|
||||||
|
):
|
||||||
|
"""Describes a Dremel 3D Printer binary sensor."""
|
||||||
|
|
||||||
|
|
||||||
|
BINARY_SENSOR_TYPES: tuple[Dremel3DPrinterBinarySensorEntityDescription, ...] = (
|
||||||
|
Dremel3DPrinterBinarySensorEntityDescription(
|
||||||
|
key="door",
|
||||||
|
name="Door",
|
||||||
|
device_class=BinarySensorDeviceClass.DOOR,
|
||||||
|
value_fn=lambda api: api.is_door_open(),
|
||||||
|
),
|
||||||
|
Dremel3DPrinterBinarySensorEntityDescription(
|
||||||
|
key="running",
|
||||||
|
name="Running",
|
||||||
|
device_class=BinarySensorDeviceClass.RUNNING,
|
||||||
|
value_fn=lambda api: api.is_running(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config_entry: ConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
|
) -> None:
|
||||||
|
"""Set up the available Dremel binary sensors."""
|
||||||
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
||||||
|
|
||||||
|
async_add_entities(
|
||||||
|
Dremel3DPrinterBinarySensor(coordinator, description)
|
||||||
|
for description in BINARY_SENSOR_TYPES
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Dremel3DPrinterBinarySensor(Dremel3DPrinterEntity, BinarySensorEntity):
|
||||||
|
"""Representation of a Dremel 3D Printer door binary sensor."""
|
||||||
|
|
||||||
|
entity_description: Dremel3DPrinterBinarySensorEntityDescription
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self) -> bool:
|
||||||
|
"""Return True if door is open."""
|
||||||
|
return self.entity_description.value_fn(self._api)
|
27
tests/components/dremel_3d_printer/test_binary_sensor.py
Normal file
27
tests/components/dremel_3d_printer/test_binary_sensor.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
"""Binary sensor tests for the Dremel 3D Printer integration."""
|
||||||
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
|
from homeassistant.components.binary_sensor import BinarySensorDeviceClass
|
||||||
|
from homeassistant.components.dremel_3d_printer.const import DOMAIN
|
||||||
|
from homeassistant.const import ATTR_DEVICE_CLASS, STATE_OFF, STATE_ON
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
|
||||||
|
async def test_binary_sensors(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
connection,
|
||||||
|
config_entry: MockConfigEntry,
|
||||||
|
entity_registry_enabled_by_default: AsyncMock,
|
||||||
|
) -> None:
|
||||||
|
"""Test we get binary sensor data."""
|
||||||
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
|
assert await async_setup_component(hass, DOMAIN, {})
|
||||||
|
state = hass.states.get("binary_sensor.dremel_3d45_door")
|
||||||
|
assert state.attributes.get(ATTR_DEVICE_CLASS) == BinarySensorDeviceClass.DOOR
|
||||||
|
assert state.state == STATE_OFF
|
||||||
|
state = hass.states.get("binary_sensor.dremel_3d45_running")
|
||||||
|
assert state.state == STATE_ON
|
||||||
|
assert state.attributes.get(ATTR_DEVICE_CLASS) == BinarySensorDeviceClass.RUNNING
|
Loading…
x
Reference in New Issue
Block a user