-
-
Notifications
You must be signed in to change notification settings - Fork 37k
Add binary sensor platform to Arcam FMJ #165272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jgus
wants to merge
7
commits into
home-assistant:dev
Choose a base branch
from
jgus:arcam-binary-sensor
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+140
−1
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
44f0360
Add binary sensor platform to Arcam FMJ
jgus 25f1192
Address review feedback for Arcam FMJ binary sensor platform
jgus 16c458b
Merge commit '2d2c6d676d71bfe0fca608caa6d1d10b3ac11b0e' into arcam-bi…
jgus 7d9062a
Merge branch 'dev' into arcam-binary-sensor
jgus 714d863
Remove coordinator fixtures, test only externally observable behavior
jgus 608b892
Merge remote-tracking branch 'origin/dev' into arcam-binary-sensor
jgus b166d3f
Address review: use coordinator.device_info, drop zone prefixes, enab…
jgus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,89 @@ | ||
| """Arcam binary sensors for incoming stream info.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
|
|
||
| from arcam.fmj.state import State | ||
|
|
||
| from homeassistant.components.binary_sensor import ( | ||
| BinarySensorEntity, | ||
| BinarySensorEntityDescription, | ||
| ) | ||
| from homeassistant.const import EntityCategory | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback | ||
| from homeassistant.helpers.update_coordinator import CoordinatorEntity | ||
|
|
||
| from .coordinator import ArcamFmjConfigEntry, ArcamFmjCoordinator | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class ArcamFmjBinarySensorEntityDescription(BinarySensorEntityDescription): | ||
| """Describes an Arcam FMJ binary sensor entity.""" | ||
|
|
||
| value_fn: Callable[[State], bool | None] | ||
|
|
||
|
|
||
| BINARY_SENSORS: tuple[ArcamFmjBinarySensorEntityDescription, ...] = ( | ||
| ArcamFmjBinarySensorEntityDescription( | ||
| key="incoming_video_interlaced", | ||
| translation_key="incoming_video_interlaced", | ||
| entity_category=EntityCategory.DIAGNOSTIC, | ||
| value_fn=lambda state: ( | ||
| vp.interlaced | ||
| if (vp := state.get_incoming_video_parameters()) is not None | ||
| else None | ||
| ), | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistant, | ||
| config_entry: ArcamFmjConfigEntry, | ||
| async_add_entities: AddConfigEntryEntitiesCallback, | ||
| ) -> None: | ||
| """Set up Arcam FMJ binary sensors from a config entry.""" | ||
| coordinators = config_entry.runtime_data.coordinators | ||
|
|
||
| entities: list[ArcamFmjBinarySensorEntity] = [] | ||
| for zone in (1, 2): | ||
| coordinator = coordinators[zone] | ||
| entities.extend( | ||
| ArcamFmjBinarySensorEntity( | ||
| coordinator=coordinator, | ||
| description=description, | ||
| ) | ||
| for description in BINARY_SENSORS | ||
| ) | ||
| async_add_entities(entities) | ||
|
|
||
|
|
||
| class ArcamFmjBinarySensorEntity( | ||
| CoordinatorEntity[ArcamFmjCoordinator], BinarySensorEntity | ||
| ): | ||
jgus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Representation of an Arcam FMJ binary sensor.""" | ||
|
|
||
| entity_description: ArcamFmjBinarySensorEntityDescription | ||
| _attr_has_entity_name = True | ||
|
|
||
| def __init__( | ||
| self, | ||
| coordinator: ArcamFmjCoordinator, | ||
| description: ArcamFmjBinarySensorEntityDescription, | ||
| ) -> None: | ||
| """Initialize the binary sensor.""" | ||
| super().__init__(coordinator) | ||
| self.entity_description = description | ||
| self._attr_unique_id = ( | ||
| f"{coordinator.config_entry.unique_id or coordinator.config_entry.entry_id}" | ||
| f"-{coordinator.state.zn}-{description.key}" | ||
| ) | ||
| self._attr_device_info = coordinator.device_info | ||
|
|
||
| @property | ||
| def is_on(self) -> bool | None: | ||
| """Return the binary sensor value.""" | ||
| return self.entity_description.value_fn(self.coordinator.state) | ||
This file contains hidden or 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 hidden or 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,43 @@ | ||
| """Tests for Arcam FMJ binary sensor entities.""" | ||
|
|
||
| import pytest | ||
|
|
||
| from homeassistant.const import STATE_UNKNOWN | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers import entity_registry as er | ||
|
|
||
| from .conftest import MOCK_UUID | ||
|
|
||
|
|
||
| def _get_entity_id(entity_registry: er.EntityRegistry, zone: int) -> str: | ||
| """Get entity_id for the interlaced binary sensor by unique_id.""" | ||
| unique_id = f"{MOCK_UUID}-{zone}-incoming_video_interlaced" | ||
| entity_id = entity_registry.async_get_entity_id( | ||
| "binary_sensor", "arcam_fmj", unique_id | ||
| ) | ||
| assert entity_id is not None, f"Missing binary sensor: zone {zone}" | ||
| return entity_id | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("player_setup") | ||
| async def test_binary_sensors_created( | ||
| hass: HomeAssistant, | ||
| entity_registry: er.EntityRegistry, | ||
| ) -> None: | ||
| """Test that binary sensor entities are created for both zones.""" | ||
| for zone in (1, 2): | ||
| entity_id = _get_entity_id(entity_registry, zone) | ||
| entry = entity_registry.async_get(entity_id) | ||
| assert entry is not None | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("player_setup") | ||
| async def test_binary_sensor_none( | ||
| hass: HomeAssistant, | ||
| entity_registry: er.EntityRegistry, | ||
| ) -> None: | ||
| """Test binary sensor when video parameters are None.""" | ||
| entity_id = _get_entity_id(entity_registry, 1) | ||
| state = hass.states.get(entity_id) | ||
| assert state is not None | ||
| assert state.state == STATE_UNKNOWN |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.