Skip to content

Commit

Permalink
Add binary sensor to Tile (#134153)
Browse files Browse the repository at this point in the history
  • Loading branch information
joostlek authored Dec 28, 2024
1 parent 0376f75 commit 80dbce1
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 1 deletion.
2 changes: 1 addition & 1 deletion homeassistant/components/tile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

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
Expand Down
69 changes: 69 additions & 0 deletions homeassistant/components/tile/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 5 additions & 0 deletions homeassistant/components/tile/icons.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"entity": {
"binary_sensor": {
"lost": {
"default": "mdi:map-marker-remove"
}
},
"device_tracker": {
"tile": {
"default": "mdi:view-grid"
Expand Down
7 changes: 7 additions & 0 deletions homeassistant/components/tile/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,12 @@
}
}
}
},
"entity": {
"binary_sensor": {
"lost": {
"name": "Lost"
}
}
}
}
47 changes: 47 additions & 0 deletions tests/components/tile/snapshots/test_binary_sensor.ambr
Original file line number Diff line number Diff line change
@@ -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',
})
# ---
27 changes: 27 additions & 0 deletions tests/components/tile/test_binary_sensor.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 80dbce1

Please sign in to comment.