-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
156 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,5 +33,12 @@ | |
} | ||
} | ||
} | ||
}, | ||
"entity": { | ||
"binary_sensor": { | ||
"lost": { | ||
"name": "Lost" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}) | ||
# --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |