-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Add binary sensor platform to PlayStation Network Integration #147639
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
651c44f
Add Binary Sensor Platform to PlayStation_Network
JackJPowell 6a3c893
Improve doc text
JackJPowell 2c5a6e8
Refactor entity base class
JackJPowell 51a744e
Shift more to entity base class
JackJPowell 17aa834
Rebase changes
JackJPowell b7aacb6
Rebase changes
JackJPowell 958bd73
revert entity base class change
JackJPowell af712bf
Remove unused snapshot
JackJPowell 4a7829b
Update snapshots
JackJPowell 288028c
Remove stale icon definition for binary sensor
JackJPowell 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
71 changes: 71 additions & 0 deletions
71
homeassistant/components/playstation_network/binary_sensor.py
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,71 @@ | ||
| """Binary Sensor platform for PlayStation Network integration.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
| from enum import StrEnum | ||
|
|
||
| from homeassistant.components.binary_sensor import ( | ||
| BinarySensorEntity, | ||
| BinarySensorEntityDescription, | ||
| ) | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback | ||
|
|
||
| from .coordinator import PlaystationNetworkConfigEntry, PlaystationNetworkData | ||
| from .entity import PlaystationNetworkServiceEntity | ||
|
|
||
| PARALLEL_UPDATES = 0 | ||
|
|
||
|
|
||
| @dataclass(kw_only=True, frozen=True) | ||
| class PlaystationNetworkBinarySensorEntityDescription(BinarySensorEntityDescription): | ||
| """PlayStation Network binary sensor description.""" | ||
|
|
||
| is_on_fn: Callable[[PlaystationNetworkData], bool] | ||
|
|
||
|
|
||
| class PlaystationNetworkBinarySensor(StrEnum): | ||
| """PlayStation Network binary sensors.""" | ||
|
|
||
| PS_PLUS_STATUS = "ps_plus_status" | ||
|
|
||
|
|
||
| BINARY_SENSOR_DESCRIPTIONS: tuple[ | ||
| PlaystationNetworkBinarySensorEntityDescription, ... | ||
| ] = ( | ||
| PlaystationNetworkBinarySensorEntityDescription( | ||
| key=PlaystationNetworkBinarySensor.PS_PLUS_STATUS, | ||
| translation_key=PlaystationNetworkBinarySensor.PS_PLUS_STATUS, | ||
| is_on_fn=lambda psn: psn.profile["isPlus"], | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistant, | ||
| config_entry: PlaystationNetworkConfigEntry, | ||
| async_add_entities: AddConfigEntryEntitiesCallback, | ||
| ) -> None: | ||
| """Set up the binary sensor platform.""" | ||
| coordinator = config_entry.runtime_data | ||
| async_add_entities( | ||
| PlaystationNetworkBinarySensorEntity(coordinator, description) | ||
| for description in BINARY_SENSOR_DESCRIPTIONS | ||
| ) | ||
|
|
||
|
|
||
| class PlaystationNetworkBinarySensorEntity( | ||
| PlaystationNetworkServiceEntity, | ||
| BinarySensorEntity, | ||
| ): | ||
| """Representation of a PlayStation Network binary sensor entity.""" | ||
|
|
||
| entity_description: PlaystationNetworkBinarySensorEntityDescription | ||
|
|
||
| @property | ||
| def is_on(self) -> bool: | ||
| """Return the state of the binary sensor.""" | ||
|
|
||
| return self.entity_description.is_on_fn(self.coordinator.data) | ||
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,36 @@ | ||
| """Base entity for PlayStation Network Integration.""" | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo | ||
| from homeassistant.helpers.entity import EntityDescription | ||
| from homeassistant.helpers.update_coordinator import CoordinatorEntity | ||
|
|
||
| from .const import DOMAIN | ||
| from .coordinator import PlaystationNetworkCoordinator | ||
|
|
||
|
|
||
| class PlaystationNetworkServiceEntity(CoordinatorEntity[PlaystationNetworkCoordinator]): | ||
| """Common entity class for PlayStationNetwork Service entities.""" | ||
|
|
||
| _attr_has_entity_name = True | ||
|
JackJPowell marked this conversation as resolved.
|
||
|
|
||
| def __init__( | ||
| self, | ||
| coordinator: PlaystationNetworkCoordinator, | ||
| entity_description: EntityDescription, | ||
| ) -> None: | ||
| """Initialize PlayStation Network Service Entity.""" | ||
| super().__init__(coordinator) | ||
| if TYPE_CHECKING: | ||
| assert coordinator.config_entry.unique_id | ||
| self.entity_description = entity_description | ||
| self._attr_unique_id = ( | ||
| f"{coordinator.config_entry.unique_id}_{entity_description.key}" | ||
| ) | ||
| self._attr_device_info = DeviceInfo( | ||
| identifiers={(DOMAIN, coordinator.config_entry.unique_id)}, | ||
| name=coordinator.data.username, | ||
| entry_type=DeviceEntryType.SERVICE, | ||
| manufacturer="Sony Interactive Entertainment", | ||
| ) | ||
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
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
49 changes: 49 additions & 0 deletions
49
tests/components/playstation_network/snapshots/test_binary_sensor.ambr
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,49 @@ | ||
| # serializer version: 1 | ||
| # name: test_sensors[binary_sensor.testuser_subscribed_to_playstation_plus-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': None, | ||
| 'entity_id': 'binary_sensor.testuser_subscribed_to_playstation_plus', | ||
| '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': 'Subscribed to PlayStation Plus', | ||
| 'platform': 'playstation_network', | ||
| 'previous_unique_id': None, | ||
| 'suggested_object_id': None, | ||
| 'supported_features': 0, | ||
| 'translation_key': <PlaystationNetworkBinarySensor.PS_PLUS_STATUS: 'ps_plus_status'>, | ||
| 'unique_id': 'my-psn-id_ps_plus_status', | ||
| 'unit_of_measurement': None, | ||
| }) | ||
| # --- | ||
| # name: test_sensors[binary_sensor.testuser_subscribed_to_playstation_plus-state] | ||
| StateSnapshot({ | ||
| 'attributes': ReadOnlyDict({ | ||
| 'friendly_name': 'testuser Subscribed to PlayStation Plus', | ||
| }), | ||
| 'context': <ANY>, | ||
| 'entity_id': 'binary_sensor.testuser_subscribed_to_playstation_plus', | ||
| 'last_changed': <ANY>, | ||
| 'last_reported': <ANY>, | ||
| 'last_updated': <ANY>, | ||
| 'state': 'on', | ||
| }) | ||
| # --- |
42 changes: 42 additions & 0 deletions
42
tests/components/playstation_network/test_binary_sensor.py
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,42 @@ | ||
| """Test the Playstation Network binary sensor platform.""" | ||
|
|
||
| from collections.abc import Generator | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
| from syrupy.assertion import SnapshotAssertion | ||
|
|
||
| from homeassistant.config_entries import ConfigEntryState | ||
| 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) | ||
| def binary_sensor_only() -> Generator[None]: | ||
| """Enable only the binary sensor platform.""" | ||
| with patch( | ||
| "homeassistant.components.playstation_network.PLATFORMS", | ||
| [Platform.BINARY_SENSOR], | ||
| ): | ||
| yield | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("mock_psnawpapi") | ||
| async def test_sensors( | ||
| hass: HomeAssistant, | ||
| config_entry: MockConfigEntry, | ||
| snapshot: SnapshotAssertion, | ||
| entity_registry: er.EntityRegistry, | ||
| ) -> None: | ||
| """Test setup of the PlayStation Network binary sensor platform.""" | ||
|
|
||
| config_entry.add_to_hass(hass) | ||
| await hass.config_entries.async_setup(config_entry.entry_id) | ||
| await hass.async_block_till_done() | ||
|
|
||
| assert config_entry.state is ConfigEntryState.LOADED | ||
|
|
||
| await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) |
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.