-
-
Notifications
You must be signed in to change notification settings - Fork 37k
Add sensor platform to Arcam FMJ #165271
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
Open
jgus
wants to merge
11
commits into
home-assistant:dev
Choose a base branch
from
jgus:arcam-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.
+456
−7
Open
Add sensor platform to Arcam FMJ #165271
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
061f696
Add sensor platform to Arcam FMJ
jgus fc24b88
Address review feedback for Arcam FMJ sensor platform
jgus e4c5dd0
Verify all video sensors handle None parameters in test
jgus c145aa3
Merge commit '2d2c6d676d71bfe0fca608caa6d1d10b3ac11b0e' into arcam-se…
jgus 6b23750
Merge branch 'dev' into arcam-sensor
jgus f5f9b69
Remove coordinator fixtures, test only externally observable behavior
jgus 967d403
Merge remote-tracking branch 'origin/dev' into arcam-sensor
jgus 23f7d3c
Address review: use coordinator.device_info, drop zone prefixes, enab…
jgus 32d10ac
Add base entity, wire up client listener for test state updates
jgus 0dee73f
Update homeassistant/components/arcam_fmj/sensor.py
jgus eecc2fe
Address review: iterate coordinators, map sample rate 0 to None
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,18 @@ | ||
| """Base entity for Arcam FMJ integration.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from homeassistant.helpers.update_coordinator import CoordinatorEntity | ||
|
|
||
| from .coordinator import ArcamFmjCoordinator | ||
|
|
||
|
|
||
| class ArcamFmjEntity(CoordinatorEntity[ArcamFmjCoordinator]): | ||
| """Base entity for Arcam FMJ.""" | ||
|
|
||
| _attr_has_entity_name = True | ||
|
|
||
| def __init__(self, coordinator: ArcamFmjCoordinator) -> None: | ||
| """Initialize the entity.""" | ||
| super().__init__(coordinator) | ||
| self._attr_device_info = coordinator.device_info |
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,180 @@ | ||
| """Arcam sensors for incoming stream info.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
|
|
||
| from arcam.fmj import IncomingVideoAspectRatio, IncomingVideoColorspace | ||
| from arcam.fmj.state import IncomingAudioConfig, IncomingAudioFormat, State | ||
|
|
||
| from homeassistant.components.sensor import ( | ||
| SensorDeviceClass, | ||
| SensorEntity, | ||
| SensorEntityDescription, | ||
| SensorStateClass, | ||
| ) | ||
| from homeassistant.const import EntityCategory, UnitOfFrequency | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback | ||
|
|
||
| from .coordinator import ArcamFmjConfigEntry, ArcamFmjCoordinator | ||
| from .entity import ArcamFmjEntity | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class ArcamFmjSensorEntityDescription(SensorEntityDescription): | ||
| """Describes an Arcam FMJ sensor entity.""" | ||
|
|
||
| value_fn: Callable[[State], int | float | str | None] | ||
|
|
||
|
|
||
| SENSORS: tuple[ArcamFmjSensorEntityDescription, ...] = ( | ||
| ArcamFmjSensorEntityDescription( | ||
| key="incoming_video_horizontal_resolution", | ||
| translation_key="incoming_video_horizontal_resolution", | ||
| entity_category=EntityCategory.DIAGNOSTIC, | ||
| state_class=SensorStateClass.MEASUREMENT, | ||
| native_unit_of_measurement="px", | ||
| suggested_display_precision=0, | ||
| value_fn=lambda state: ( | ||
| vp.horizontal_resolution | ||
| if (vp := state.get_incoming_video_parameters()) is not None | ||
| else None | ||
| ), | ||
| ), | ||
| ArcamFmjSensorEntityDescription( | ||
| key="incoming_video_vertical_resolution", | ||
| translation_key="incoming_video_vertical_resolution", | ||
| entity_category=EntityCategory.DIAGNOSTIC, | ||
| state_class=SensorStateClass.MEASUREMENT, | ||
| native_unit_of_measurement="px", | ||
| suggested_display_precision=0, | ||
| value_fn=lambda state: ( | ||
| vp.vertical_resolution | ||
| if (vp := state.get_incoming_video_parameters()) is not None | ||
| else None | ||
| ), | ||
| ), | ||
| ArcamFmjSensorEntityDescription( | ||
| key="incoming_video_refresh_rate", | ||
| translation_key="incoming_video_refresh_rate", | ||
| entity_category=EntityCategory.DIAGNOSTIC, | ||
| device_class=SensorDeviceClass.FREQUENCY, | ||
| state_class=SensorStateClass.MEASUREMENT, | ||
| native_unit_of_measurement=UnitOfFrequency.HERTZ, | ||
| suggested_display_precision=0, | ||
| value_fn=lambda state: ( | ||
| vp.refresh_rate | ||
| if (vp := state.get_incoming_video_parameters()) is not None | ||
| else None | ||
| ), | ||
| ), | ||
| ArcamFmjSensorEntityDescription( | ||
| key="incoming_video_aspect_ratio", | ||
| translation_key="incoming_video_aspect_ratio", | ||
| entity_category=EntityCategory.DIAGNOSTIC, | ||
| device_class=SensorDeviceClass.ENUM, | ||
| options=[member.name.lower() for member in IncomingVideoAspectRatio], | ||
| value_fn=lambda state: ( | ||
| vp.aspect_ratio.name.lower() | ||
| if (vp := state.get_incoming_video_parameters()) is not None | ||
| else None | ||
| ), | ||
| ), | ||
| ArcamFmjSensorEntityDescription( | ||
| key="incoming_video_colorspace", | ||
| translation_key="incoming_video_colorspace", | ||
| entity_category=EntityCategory.DIAGNOSTIC, | ||
| device_class=SensorDeviceClass.ENUM, | ||
| options=[member.name.lower() for member in IncomingVideoColorspace], | ||
| value_fn=lambda state: ( | ||
| vp.colorspace.name.lower() | ||
| if (vp := state.get_incoming_video_parameters()) is not None | ||
| else None | ||
| ), | ||
| ), | ||
| ArcamFmjSensorEntityDescription( | ||
| key="incoming_audio_format", | ||
| translation_key="incoming_audio_format", | ||
| entity_category=EntityCategory.DIAGNOSTIC, | ||
| device_class=SensorDeviceClass.ENUM, | ||
| options=[member.name.lower() for member in IncomingAudioFormat], | ||
| value_fn=lambda state: ( | ||
| result.name.lower() | ||
| if (result := state.get_incoming_audio_format()[0]) is not None | ||
| else None | ||
| ), | ||
| ), | ||
| ArcamFmjSensorEntityDescription( | ||
| key="incoming_audio_config", | ||
| translation_key="incoming_audio_config", | ||
| entity_category=EntityCategory.DIAGNOSTIC, | ||
| device_class=SensorDeviceClass.ENUM, | ||
| options=[member.name.lower() for member in IncomingAudioConfig], | ||
| value_fn=lambda state: ( | ||
| result.name.lower() | ||
| if (result := state.get_incoming_audio_format()[1]) is not None | ||
| else None | ||
| ), | ||
| ), | ||
jgus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ArcamFmjSensorEntityDescription( | ||
| key="incoming_audio_sample_rate", | ||
| translation_key="incoming_audio_sample_rate", | ||
| entity_category=EntityCategory.DIAGNOSTIC, | ||
| device_class=SensorDeviceClass.FREQUENCY, | ||
| state_class=SensorStateClass.MEASUREMENT, | ||
| native_unit_of_measurement=UnitOfFrequency.HERTZ, | ||
| suggested_display_precision=0, | ||
| value_fn=lambda state: ( | ||
| None | ||
| if (sample_rate := state.get_incoming_audio_sample_rate()) == 0 | ||
| else sample_rate | ||
| ), | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistant, | ||
| config_entry: ArcamFmjConfigEntry, | ||
| async_add_entities: AddConfigEntryEntitiesCallback, | ||
| ) -> None: | ||
| """Set up Arcam FMJ sensors from a config entry.""" | ||
| coordinators = config_entry.runtime_data.coordinators | ||
|
|
||
| entities: list[ArcamFmjSensorEntity] = [] | ||
| for coordinator in coordinators.values(): | ||
| entities.extend( | ||
| ArcamFmjSensorEntity( | ||
| coordinator=coordinator, | ||
| description=description, | ||
| ) | ||
| for description in SENSORS | ||
| ) | ||
| async_add_entities(entities) | ||
|
|
||
|
|
||
| class ArcamFmjSensorEntity(ArcamFmjEntity, SensorEntity): | ||
| """Representation of an Arcam FMJ sensor.""" | ||
|
|
||
| entity_description: ArcamFmjSensorEntityDescription | ||
|
|
||
| def __init__( | ||
| self, | ||
| coordinator: ArcamFmjCoordinator, | ||
| description: ArcamFmjSensorEntityDescription, | ||
| ) -> None: | ||
| """Initialize the 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}" | ||
| ) | ||
jgus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self._attr_entity_registry_enabled_default = coordinator.state.zn == 1 | ||
|
|
||
| @property | ||
| def native_value(self) -> int | float | str | None: | ||
| """Return the 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
Oops, something went wrong.
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.