add platform binary_sensor to fressnapf_tracker (#157753)

This commit is contained in:
Kevin Stillhammer
2025-12-02 20:05:34 +01:00
committed by GitHub
parent c67247bf32
commit 2631c77bee
5 changed files with 213 additions and 1 deletions

View File

@@ -12,7 +12,11 @@ from .coordinator import (
FressnapfTrackerDataUpdateCoordinator,
)
PLATFORMS: list[Platform] = [Platform.DEVICE_TRACKER, Platform.SENSOR]
PLATFORMS: list[Platform] = [
Platform.BINARY_SENSOR,
Platform.DEVICE_TRACKER,
Platform.SENSOR,
]
async def async_setup_entry(

View File

@@ -0,0 +1,69 @@
"""Binary Sensor platform for fressnapf_tracker."""
from collections.abc import Callable
from dataclasses import dataclass
from fressnapftracker import Tracker
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import FressnapfTrackerConfigEntry
from .entity import FressnapfTrackerEntity
@dataclass(frozen=True, kw_only=True)
class FressnapfTrackerBinarySensorDescription(BinarySensorEntityDescription):
"""Class describing Fressnapf Tracker binary_sensor entities."""
value_fn: Callable[[Tracker], bool]
BINARY_SENSOR_ENTITY_DESCRIPTIONS: tuple[
FressnapfTrackerBinarySensorDescription, ...
] = (
FressnapfTrackerBinarySensorDescription(
key="charging",
device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: data.charging,
),
FressnapfTrackerBinarySensorDescription(
translation_key="deep_sleep",
key="deep_sleep_value",
device_class=BinarySensorDeviceClass.POWER,
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda data: bool(data.deep_sleep_value),
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: FressnapfTrackerConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the Fressnapf Tracker binary_sensors."""
async_add_entities(
FressnapfTrackerBinarySensor(coordinator, sensor_description)
for sensor_description in BINARY_SENSOR_ENTITY_DESCRIPTIONS
for coordinator in entry.runtime_data
)
class FressnapfTrackerBinarySensor(FressnapfTrackerEntity, BinarySensorEntity):
"""Fressnapf Tracker binary_sensor for general information."""
entity_description: FressnapfTrackerBinarySensorDescription
@property
def is_on(self) -> bool:
"""Return True if the binary sensor is on."""
return self.entity_description.value_fn(self.coordinator.data)

View File

@@ -45,5 +45,12 @@
}
}
}
},
"entity": {
"binary_sensor": {
"deep_sleep": {
"name": "Deep Sleep"
}
}
}
}

View File

@@ -0,0 +1,99 @@
# serializer version: 1
# name: test_state_entity_device_snapshots[binary_sensor.fluffy_charging-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'binary_sensor.fluffy_charging',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.BATTERY_CHARGING: 'battery_charging'>,
'original_icon': None,
'original_name': 'Charging',
'platform': 'fressnapf_tracker',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': 'ABC123456_charging',
'unit_of_measurement': None,
})
# ---
# name: test_state_entity_device_snapshots[binary_sensor.fluffy_charging-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'battery_charging',
'friendly_name': 'Fluffy Charging',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.fluffy_charging',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_state_entity_device_snapshots[binary_sensor.fluffy_deep_sleep-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'binary_sensor.fluffy_deep_sleep',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.POWER: 'power'>,
'original_icon': None,
'original_name': 'Deep Sleep',
'platform': 'fressnapf_tracker',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'deep_sleep',
'unique_id': 'ABC123456_deep_sleep_value',
'unit_of_measurement': None,
})
# ---
# name: test_state_entity_device_snapshots[binary_sensor.fluffy_deep_sleep-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'power',
'friendly_name': 'Fluffy Deep Sleep',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.fluffy_deep_sleep',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---

View File

@@ -0,0 +1,33 @@
"""Test the Fressnapf Tracker binary_sensor platform."""
from collections.abc import AsyncGenerator
from unittest.mock import patch
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from tests.common import MockConfigEntry, snapshot_platform
@pytest.fixture(autouse=True)
async def platforms() -> AsyncGenerator[None]:
"""Return the platforms to be loaded for this test."""
with patch(
"homeassistant.components.fressnapf_tracker.PLATFORMS", [Platform.BINARY_SENSOR]
):
yield
@pytest.mark.usefixtures("init_integration")
async def test_state_entity_device_snapshots(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_config_entry: MockConfigEntry,
snapshot: SnapshotAssertion,
) -> None:
"""Test binary_sensor entity is created correctly."""
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)