Skip to content

Commit 2631c77

Browse files
authored
add platform binary_sensor to fressnapf_tracker (home-assistant#157753)
1 parent c67247b commit 2631c77

File tree

5 files changed

+213
-1
lines changed

5 files changed

+213
-1
lines changed

homeassistant/components/fressnapf_tracker/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
FressnapfTrackerDataUpdateCoordinator,
1313
)
1414

15-
PLATFORMS: list[Platform] = [Platform.DEVICE_TRACKER, Platform.SENSOR]
15+
PLATFORMS: list[Platform] = [
16+
Platform.BINARY_SENSOR,
17+
Platform.DEVICE_TRACKER,
18+
Platform.SENSOR,
19+
]
1620

1721

1822
async def async_setup_entry(
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""Binary Sensor platform for fressnapf_tracker."""
2+
3+
from collections.abc import Callable
4+
from dataclasses import dataclass
5+
6+
from fressnapftracker import Tracker
7+
8+
from homeassistant.components.binary_sensor import (
9+
BinarySensorDeviceClass,
10+
BinarySensorEntity,
11+
BinarySensorEntityDescription,
12+
)
13+
from homeassistant.const import EntityCategory
14+
from homeassistant.core import HomeAssistant
15+
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
16+
17+
from . import FressnapfTrackerConfigEntry
18+
from .entity import FressnapfTrackerEntity
19+
20+
21+
@dataclass(frozen=True, kw_only=True)
22+
class FressnapfTrackerBinarySensorDescription(BinarySensorEntityDescription):
23+
"""Class describing Fressnapf Tracker binary_sensor entities."""
24+
25+
value_fn: Callable[[Tracker], bool]
26+
27+
28+
BINARY_SENSOR_ENTITY_DESCRIPTIONS: tuple[
29+
FressnapfTrackerBinarySensorDescription, ...
30+
] = (
31+
FressnapfTrackerBinarySensorDescription(
32+
key="charging",
33+
device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
34+
entity_category=EntityCategory.DIAGNOSTIC,
35+
value_fn=lambda data: data.charging,
36+
),
37+
FressnapfTrackerBinarySensorDescription(
38+
translation_key="deep_sleep",
39+
key="deep_sleep_value",
40+
device_class=BinarySensorDeviceClass.POWER,
41+
entity_category=EntityCategory.DIAGNOSTIC,
42+
value_fn=lambda data: bool(data.deep_sleep_value),
43+
),
44+
)
45+
46+
47+
async def async_setup_entry(
48+
hass: HomeAssistant,
49+
entry: FressnapfTrackerConfigEntry,
50+
async_add_entities: AddConfigEntryEntitiesCallback,
51+
) -> None:
52+
"""Set up the Fressnapf Tracker binary_sensors."""
53+
54+
async_add_entities(
55+
FressnapfTrackerBinarySensor(coordinator, sensor_description)
56+
for sensor_description in BINARY_SENSOR_ENTITY_DESCRIPTIONS
57+
for coordinator in entry.runtime_data
58+
)
59+
60+
61+
class FressnapfTrackerBinarySensor(FressnapfTrackerEntity, BinarySensorEntity):
62+
"""Fressnapf Tracker binary_sensor for general information."""
63+
64+
entity_description: FressnapfTrackerBinarySensorDescription
65+
66+
@property
67+
def is_on(self) -> bool:
68+
"""Return True if the binary sensor is on."""
69+
return self.entity_description.value_fn(self.coordinator.data)

homeassistant/components/fressnapf_tracker/strings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,12 @@
4545
}
4646
}
4747
}
48+
},
49+
"entity": {
50+
"binary_sensor": {
51+
"deep_sleep": {
52+
"name": "Deep Sleep"
53+
}
54+
}
4855
}
4956
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# serializer version: 1
2+
# name: test_state_entity_device_snapshots[binary_sensor.fluffy_charging-entry]
3+
EntityRegistryEntrySnapshot({
4+
'aliases': set({
5+
}),
6+
'area_id': None,
7+
'capabilities': None,
8+
'config_entry_id': <ANY>,
9+
'config_subentry_id': <ANY>,
10+
'device_class': None,
11+
'device_id': <ANY>,
12+
'disabled_by': None,
13+
'domain': 'binary_sensor',
14+
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
15+
'entity_id': 'binary_sensor.fluffy_charging',
16+
'has_entity_name': True,
17+
'hidden_by': None,
18+
'icon': None,
19+
'id': <ANY>,
20+
'labels': set({
21+
}),
22+
'name': None,
23+
'options': dict({
24+
}),
25+
'original_device_class': <BinarySensorDeviceClass.BATTERY_CHARGING: 'battery_charging'>,
26+
'original_icon': None,
27+
'original_name': 'Charging',
28+
'platform': 'fressnapf_tracker',
29+
'previous_unique_id': None,
30+
'suggested_object_id': None,
31+
'supported_features': 0,
32+
'translation_key': None,
33+
'unique_id': 'ABC123456_charging',
34+
'unit_of_measurement': None,
35+
})
36+
# ---
37+
# name: test_state_entity_device_snapshots[binary_sensor.fluffy_charging-state]
38+
StateSnapshot({
39+
'attributes': ReadOnlyDict({
40+
'device_class': 'battery_charging',
41+
'friendly_name': 'Fluffy Charging',
42+
}),
43+
'context': <ANY>,
44+
'entity_id': 'binary_sensor.fluffy_charging',
45+
'last_changed': <ANY>,
46+
'last_reported': <ANY>,
47+
'last_updated': <ANY>,
48+
'state': 'off',
49+
})
50+
# ---
51+
# name: test_state_entity_device_snapshots[binary_sensor.fluffy_deep_sleep-entry]
52+
EntityRegistryEntrySnapshot({
53+
'aliases': set({
54+
}),
55+
'area_id': None,
56+
'capabilities': None,
57+
'config_entry_id': <ANY>,
58+
'config_subentry_id': <ANY>,
59+
'device_class': None,
60+
'device_id': <ANY>,
61+
'disabled_by': None,
62+
'domain': 'binary_sensor',
63+
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
64+
'entity_id': 'binary_sensor.fluffy_deep_sleep',
65+
'has_entity_name': True,
66+
'hidden_by': None,
67+
'icon': None,
68+
'id': <ANY>,
69+
'labels': set({
70+
}),
71+
'name': None,
72+
'options': dict({
73+
}),
74+
'original_device_class': <BinarySensorDeviceClass.POWER: 'power'>,
75+
'original_icon': None,
76+
'original_name': 'Deep Sleep',
77+
'platform': 'fressnapf_tracker',
78+
'previous_unique_id': None,
79+
'suggested_object_id': None,
80+
'supported_features': 0,
81+
'translation_key': 'deep_sleep',
82+
'unique_id': 'ABC123456_deep_sleep_value',
83+
'unit_of_measurement': None,
84+
})
85+
# ---
86+
# name: test_state_entity_device_snapshots[binary_sensor.fluffy_deep_sleep-state]
87+
StateSnapshot({
88+
'attributes': ReadOnlyDict({
89+
'device_class': 'power',
90+
'friendly_name': 'Fluffy Deep Sleep',
91+
}),
92+
'context': <ANY>,
93+
'entity_id': 'binary_sensor.fluffy_deep_sleep',
94+
'last_changed': <ANY>,
95+
'last_reported': <ANY>,
96+
'last_updated': <ANY>,
97+
'state': 'off',
98+
})
99+
# ---
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Test the Fressnapf Tracker binary_sensor platform."""
2+
3+
from collections.abc import AsyncGenerator
4+
from unittest.mock import patch
5+
6+
import pytest
7+
from syrupy.assertion import SnapshotAssertion
8+
9+
from homeassistant.const import Platform
10+
from homeassistant.core import HomeAssistant
11+
from homeassistant.helpers import entity_registry as er
12+
13+
from tests.common import MockConfigEntry, snapshot_platform
14+
15+
16+
@pytest.fixture(autouse=True)
17+
async def platforms() -> AsyncGenerator[None]:
18+
"""Return the platforms to be loaded for this test."""
19+
with patch(
20+
"homeassistant.components.fressnapf_tracker.PLATFORMS", [Platform.BINARY_SENSOR]
21+
):
22+
yield
23+
24+
25+
@pytest.mark.usefixtures("init_integration")
26+
async def test_state_entity_device_snapshots(
27+
hass: HomeAssistant,
28+
entity_registry: er.EntityRegistry,
29+
mock_config_entry: MockConfigEntry,
30+
snapshot: SnapshotAssertion,
31+
) -> None:
32+
"""Test binary_sensor entity is created correctly."""
33+
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)

0 commit comments

Comments
 (0)