Add binary sensor to Tile (#134153)
parent
0376f75ee3
commit
80dbce14ec
|
@ -13,7 +13,7 @@ from homeassistant.util.async_ import gather_with_limited_concurrency
|
|||
|
||||
from .coordinator import TileConfigEntry, TileCoordinator
|
||||
|
||||
PLATFORMS = [Platform.DEVICE_TRACKER]
|
||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.DEVICE_TRACKER]
|
||||
DEVICE_TYPES = ["PHONE", "TILE"]
|
||||
|
||||
DEFAULT_INIT_TASK_LIMIT = 2
|
||||
|
|
|
@ -0,0 +1,69 @@
|
|||
"""Support for Tile binary sensors."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
|
||||
from pytile.tile import Tile
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .coordinator import TileConfigEntry, TileCoordinator
|
||||
from .entity import TileEntity
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class TileBinarySensorEntityDescription(BinarySensorEntityDescription):
|
||||
"""Describes Tile binary sensor entity."""
|
||||
|
||||
is_on_fn: Callable[[Tile], bool]
|
||||
|
||||
|
||||
ENTITIES: tuple[TileBinarySensorEntityDescription, ...] = (
|
||||
TileBinarySensorEntityDescription(
|
||||
key="lost",
|
||||
translation_key="lost",
|
||||
is_on_fn=lambda tile: tile.lost,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: TileConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up Tile binary sensors."""
|
||||
|
||||
async_add_entities(
|
||||
TileBinarySensor(coordinator, entity_description)
|
||||
for entity_description in ENTITIES
|
||||
for coordinator in entry.runtime_data.values()
|
||||
)
|
||||
|
||||
|
||||
class TileBinarySensor(TileEntity, BinarySensorEntity):
|
||||
"""Representation of a Tile binary sensor."""
|
||||
|
||||
entity_description: TileBinarySensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: TileCoordinator,
|
||||
description: TileBinarySensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
super().__init__(coordinator)
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = (
|
||||
f"{coordinator.username}_{self._tile.uuid}_{description.key}"
|
||||
)
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return True if the binary sensor is on."""
|
||||
return self.entity_description.is_on_fn(self._tile)
|
|
@ -1,5 +1,10 @@
|
|||
{
|
||||
"entity": {
|
||||
"binary_sensor": {
|
||||
"lost": {
|
||||
"default": "mdi:map-marker-remove"
|
||||
}
|
||||
},
|
||||
"device_tracker": {
|
||||
"tile": {
|
||||
"default": "mdi:view-grid"
|
||||
|
|
|
@ -33,5 +33,12 @@
|
|||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"entity": {
|
||||
"binary_sensor": {
|
||||
"lost": {
|
||||
"name": "Lost"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
# serializer version: 1
|
||||
# name: test_all_entities[binary_sensor.wallet_lost-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': None,
|
||||
'config_entry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'binary_sensor',
|
||||
'entity_category': None,
|
||||
'entity_id': 'binary_sensor.wallet_lost',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'labels': set({
|
||||
}),
|
||||
'name': None,
|
||||
'options': dict({
|
||||
}),
|
||||
'original_device_class': None,
|
||||
'original_icon': None,
|
||||
'original_name': 'Lost',
|
||||
'platform': 'tile',
|
||||
'previous_unique_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': 'lost',
|
||||
'unique_id': 'user@host.com_19264d2dffdbca32_lost',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_all_entities[binary_sensor.wallet_lost-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'friendly_name': 'Wallet Lost',
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'binary_sensor.wallet_lost',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'off',
|
||||
})
|
||||
# ---
|
|
@ -0,0 +1,27 @@
|
|||
"""Tests for the Tile Binary sensor platform."""
|
||||
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_integration
|
||||
|
||||
from tests.common import MockConfigEntry, snapshot_platform
|
||||
|
||||
|
||||
async def test_all_entities(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
mock_pytile: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test all entities."""
|
||||
with patch("homeassistant.components.tile.PLATFORMS", [Platform.BINARY_SENSOR]):
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
Loading…
Reference in New Issue