Add laundrify integration (#65090)
* First version of laundrify integration * Code cleanup * Code cleanup after review #2 * Move coordinator to its own file * Save devices as dict and implement available prop as fn * Validate token on init, abort if already configured * Some more cleanup after review * Add strict type hints * Minor changes after code review * Remove OptionsFlow (use default poll interval instead) * Fix CODEOWNERS to pass hassfest job * Fix formatting to pass prettier job * Fix mypy typing error * Update internal device property after fetching data * Call parental update handler and remove obsolete code * Add coordinator tests and fix some config flow tests * Refactor tests * Refactor fixtures * Device unavailable if polling fails
This commit is contained in:
parent
3f8c896cb2
commit
abf9aab18f
22 changed files with 709 additions and 0 deletions
84
homeassistant/components/laundrify/binary_sensor.py
Normal file
84
homeassistant/components/laundrify/binary_sensor.py
Normal file
|
@ -0,0 +1,84 @@
|
|||
"""Platform for binary sensor integration."""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorDeviceClass,
|
||||
BinarySensorEntity,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN, MANUFACTURER, MODEL
|
||||
from .coordinator import LaundrifyUpdateCoordinator
|
||||
from .model import LaundrifyDevice
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up sensors from a config entry created in the integrations UI."""
|
||||
|
||||
coordinator = hass.data[DOMAIN][config.entry_id]["coordinator"]
|
||||
|
||||
async_add_entities(
|
||||
LaundrifyPowerPlug(coordinator, device) for device in coordinator.data.values()
|
||||
)
|
||||
|
||||
|
||||
class LaundrifyPowerPlug(
|
||||
CoordinatorEntity[LaundrifyUpdateCoordinator], BinarySensorEntity
|
||||
):
|
||||
"""Representation of a laundrify Power Plug."""
|
||||
|
||||
_attr_device_class = BinarySensorDeviceClass.RUNNING
|
||||
_attr_icon = "mdi:washing-machine"
|
||||
|
||||
def __init__(
|
||||
self, coordinator: LaundrifyUpdateCoordinator, device: LaundrifyDevice
|
||||
) -> None:
|
||||
"""Pass coordinator to CoordinatorEntity."""
|
||||
super().__init__(coordinator)
|
||||
self._device = device
|
||||
self._attr_unique_id = device["_id"]
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Configure the Device of this Entity."""
|
||||
return DeviceInfo(
|
||||
identifiers={(DOMAIN, self._device["_id"])},
|
||||
name=self.name,
|
||||
manufacturer=MANUFACTURER,
|
||||
model=MODEL,
|
||||
sw_version=self._device["firmwareVersion"],
|
||||
)
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Check if the device is available."""
|
||||
return (
|
||||
self.unique_id in self.coordinator.data
|
||||
and self.coordinator.last_update_success
|
||||
)
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Name of the entity."""
|
||||
return self._device["name"]
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return entity state."""
|
||||
return self._device["status"] == "ON"
|
||||
|
||||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
"""Handle updated data from the coordinator."""
|
||||
self._device = self.coordinator.data[self.unique_id]
|
||||
super()._handle_coordinator_update()
|
Loading…
Add table
Add a link
Reference in a new issue