-
-
Notifications
You must be signed in to change notification settings - Fork 28.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add recording status for Philips TV (#94691)
- Loading branch information
Showing
5 changed files
with
300 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,105 @@ | ||
"""Philips TV binary sensors.""" | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
|
||
from haphilipsjs import PhilipsTV | ||
|
||
from homeassistant.components.binary_sensor import ( | ||
BinarySensorEntity, | ||
BinarySensorEntityDescription, | ||
) | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant, callback | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
||
from . import PhilipsTVDataUpdateCoordinator | ||
from .const import DOMAIN | ||
from .entity import PhilipsJsEntity | ||
|
||
|
||
@dataclass | ||
class PhilipsTVBinarySensorEntityDescription(BinarySensorEntityDescription): | ||
"""A entity description for Philips TV binary sensor.""" | ||
|
||
def __init__(self, recording_value, *args, **kwargs) -> None: | ||
"""Set up a binary sensor entity description and add additional attributes.""" | ||
super().__init__(*args, **kwargs) | ||
self.recording_value: str = recording_value | ||
|
||
|
||
DESCRIPTIONS = ( | ||
PhilipsTVBinarySensorEntityDescription( | ||
key="recording_ongoing", | ||
translation_key="recording_ongoing", | ||
icon="mdi:record-rec", | ||
recording_value="RECORDING_ONGOING", | ||
), | ||
PhilipsTVBinarySensorEntityDescription( | ||
key="recording_new", | ||
translation_key="recording_new", | ||
icon="mdi:new-box", | ||
recording_value="RECORDING_NEW", | ||
), | ||
) | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
config_entry: ConfigEntry, | ||
async_add_entities: AddEntitiesCallback, | ||
) -> None: | ||
"""Set up the configuration entry.""" | ||
coordinator: PhilipsTVDataUpdateCoordinator = hass.data[DOMAIN][ | ||
config_entry.entry_id | ||
] | ||
|
||
if ( | ||
coordinator.api.json_feature_supported("recordings", "List") | ||
and coordinator.api.api_version == 6 | ||
): | ||
async_add_entities( | ||
PhilipsTVBinarySensorEntityRecordingType(coordinator, description) | ||
for description in DESCRIPTIONS | ||
) | ||
|
||
|
||
def _check_for_recording_entry(api: PhilipsTV, entry: str, value: str) -> bool: | ||
"""Return True if at least one specified value is available within entry of list.""" | ||
for rec in api.recordings_list["recordings"]: | ||
if rec.get(entry) == value: | ||
return True | ||
return False | ||
|
||
|
||
class PhilipsTVBinarySensorEntityRecordingType(PhilipsJsEntity, BinarySensorEntity): | ||
"""A Philips TV binary sensor class, which allows multiple entities given by a BinarySensorEntityDescription.""" | ||
|
||
entity_description: PhilipsTVBinarySensorEntityDescription | ||
|
||
def __init__( | ||
self, | ||
coordinator: PhilipsTVDataUpdateCoordinator, | ||
description: PhilipsTVBinarySensorEntityDescription, | ||
) -> None: | ||
"""Initialize entity class.""" | ||
self.entity_description = description | ||
self._attr_unique_id = f"{coordinator.unique_id}_{description.key}" | ||
self._attr_device_info = coordinator.device_info | ||
self._attr_is_on = _check_for_recording_entry( | ||
coordinator.api, | ||
"RecordingType", | ||
description.recording_value, | ||
) | ||
|
||
super().__init__(coordinator) | ||
|
||
@callback | ||
def _handle_coordinator_update(self) -> None: | ||
"""Handle updated data from the coordinator and set is_on true if one specified value is available within given entry of list.""" | ||
self._attr_is_on = _check_for_recording_entry( | ||
self.coordinator.api, | ||
"RecordingType", | ||
self.entity_description.recording_value, | ||
) | ||
super()._handle_coordinator_update() |
This file contains 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 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 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,60 @@ | ||
"""The tests for philips_js binary_sensor.""" | ||
import pytest | ||
|
||
from homeassistant.const import STATE_ON | ||
from homeassistant.core import HomeAssistant | ||
|
||
from . import MOCK_NAME, MOCK_RECORDINGS_LIST | ||
|
||
ID_RECORDING_AVAILABLE = ( | ||
"binary_sensor." + MOCK_NAME.replace(" ", "_").lower() + "_new_recording_available" | ||
) | ||
ID_RECORDING_ONGOING = ( | ||
"binary_sensor." + MOCK_NAME.replace(" ", "_").lower() + "_recording_ongoing" | ||
) | ||
|
||
|
||
@pytest.fixture | ||
async def mock_tv_api_invalid(mock_tv): | ||
"""Set up a invalid mock_tv with should not create sensors.""" | ||
mock_tv.secured_transport = True | ||
mock_tv.api_version = 1 | ||
mock_tv.recordings_list = None | ||
return mock_tv | ||
|
||
|
||
@pytest.fixture | ||
async def mock_tv_api_valid(mock_tv): | ||
"""Set up a valid mock_tv with should create sensors.""" | ||
mock_tv.secured_transport = True | ||
mock_tv.api_version = 6 | ||
mock_tv.recordings_list = MOCK_RECORDINGS_LIST | ||
return mock_tv | ||
|
||
|
||
async def test_recordings_list_invalid( | ||
mock_tv_api_invalid, mock_config_entry, hass: HomeAssistant | ||
) -> None: | ||
"""Test if sensors are not created if mock_tv is invalid.""" | ||
|
||
assert await hass.config_entries.async_setup(mock_config_entry.entry_id) | ||
|
||
state = hass.states.get(ID_RECORDING_AVAILABLE) | ||
assert state is None | ||
|
||
state = hass.states.get(ID_RECORDING_ONGOING) | ||
assert state is None | ||
|
||
|
||
async def test_recordings_list_valid( | ||
mock_tv_api_valid, mock_config_entry, hass: HomeAssistant | ||
) -> None: | ||
"""Test if sensors are created correctly.""" | ||
|
||
assert await hass.config_entries.async_setup(mock_config_entry.entry_id) | ||
|
||
state = hass.states.get(ID_RECORDING_AVAILABLE) | ||
assert state.state is STATE_ON | ||
|
||
state = hass.states.get(ID_RECORDING_ONGOING) | ||
assert state.state is STATE_ON |