Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion homeassistant/components/lyric/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import PERCENTAGE, UnitOfTemperature
from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.typing import StateType
Expand Down Expand Up @@ -189,6 +189,14 @@ async def async_setup_entry(
if accessory_sensor.suitable_fn(room, accessory)
)

async_add_entities(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

although this integration was already doing it, usually async_add_entities is called once, with the full list of entities to add.
I think it is ok to keep it, but would be nice to change it later for consistency

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed — this follows the pattern already established in the file (two separate async_add_entities calls existed before this PR for DEVICE_SENSORS/ACCESSORY_SENSORS). Happy to consolidate all three into a single call in a small follow-up cleanup PR if that's useful, rather than expanding this one's diff. Let me know if you'd rather I just do it here.

LyricPriorityStatusSensor(coordinator, location, device)
for location in coordinator.data.locations
for device in location.devices
if device.device_class == "Thermostat"
and coordinator.data.rooms_dict.get(device.mac_id)
)


class LyricSensor(LyricDeviceEntity, SensorEntity):
"""Define a Honeywell Lyric sensor."""
Expand Down Expand Up @@ -258,3 +266,33 @@ def __init__(
def native_value(self) -> StateType | datetime:
"""Return the state."""
return self.entity_description.value_fn(self.room, self.accessory)


class LyricPriorityStatusSensor(LyricDeviceEntity, SensorEntity):
"""Define a Honeywell Lyric room priority hold status sensor."""

_attr_entity_category = EntityCategory.DIAGNOSTIC
_attr_translation_key = "priority_status"
Comment on lines +274 to +275

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address this one

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lyric binary sensor and new sensors docs
#46986


def __init__(
self,
coordinator: LyricDataUpdateCoordinator,
location: LyricLocation,
device: LyricDevice,
) -> None:
"""Initialize."""
super().__init__(
coordinator,
location,
device,
f"{device.mac_id}_priority_status",
)

@property
@override
def native_value(self) -> StateType:
"""Return the state."""
priority = self.coordinator.data.priorities_dict.get(self._mac_id)
if priority is None:
return None
return priority.status
Comment on lines +295 to +298
3 changes: 3 additions & 0 deletions homeassistant/components/lyric/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
"outdoor_temperature": {
"name": "Outdoor temperature"
},
"priority_status": {
"name": "Priority status"
},
"room_humidity": {
"name": "Room humidity"
},
Expand Down
12 changes: 12 additions & 0 deletions tests/components/lyric/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
"""Tests for the Honeywell Lyric integration."""

from homeassistant.core import HomeAssistant

from tests.common import MockConfigEntry


async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None:
"""Set up the Lyric integration for tests."""
config_entry.add_to_hass(hass)

await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
88 changes: 88 additions & 0 deletions tests/components/lyric/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"""Fixtures for the Honeywell Lyric integration tests."""

from collections.abc import Generator
from time import time
from unittest.mock import MagicMock, patch

from aiolyric.objects.location import LyricLocation
import pytest

from homeassistant.components.application_credentials import (
DOMAIN as APPLICATION_CREDENTIALS_DOMAIN,
ClientCredential,
async_import_client_credential,
)
from homeassistant.components.lyric.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component

from tests.common import MockConfigEntry, load_json_array_fixture

CLIENT_ID = "1234"
CLIENT_SECRET = "5678"

# Matches the values baked into fixtures/locations.json.
LOCATION_ID = 35202000168931
DEVICE_ID = "LCC-7f86b153-8480-f111-b78f-6045bdb25006"
MAC_ID = "5CFCE1B67035"
# Second device: has room data but no priority data yet, exercising the
# defensive "no priority entry" branch of LyricPriorityStatusSensor.
NO_PRIORITY_DATA_MAC_ID = "5CFCE1B67036"


@pytest.fixture
async def setup_credentials(hass: HomeAssistant) -> None:
"""Register lyric application credentials."""
assert await async_setup_component(hass, APPLICATION_CREDENTIALS_DOMAIN, {})

await async_import_client_credential(
hass, DOMAIN, ClientCredential(CLIENT_ID, CLIENT_SECRET)
)


@pytest.fixture
def mock_config_entry() -> MockConfigEntry:
"""Return an already-authenticated Lyric config entry."""
return MockConfigEntry(
domain=DOMAIN,
data={
"auth_implementation": DOMAIN,
"token": {
"access_token": "mock-access-token",
"refresh_token": "mock-refresh-token",
"expires_at": time() + 3600,
"token_type": "Bearer",
},
},
)


@pytest.fixture
def mock_lyric_api() -> Generator[MagicMock]:
"""Mock the aiolyric client, backed by a real Location and directly-set priority data.

rooms_dict/priorities_dict are set directly rather than parsed from
real LyricPriority objects: LyricPriority.status/current_priority
currently read the wrong JSON keys (aiolyric#165), and that parsing
bug is aiolyric's own test suite's concern, not this integration's.
The second device has room data but no priority entry, exercising
both branches of LyricPriorityStatusSensor.native_value.
"""
with patch("homeassistant.components.lyric.Lyric", autospec=True) as mock_lyric_cls:
lyric = mock_lyric_cls.return_value

locations_json = load_json_array_fixture("locations.json", DOMAIN)
lyric.locations = [
LyricLocation(MagicMock(), location) for location in locations_json
]
lyric.locations_dict = {
location.location_id: location for location in lyric.locations
}

lyric.priorities_dict = {MAC_ID: MagicMock(status="NoHold")}
lyric.rooms_dict = {
MAC_ID: {1: MagicMock()},
NO_PRIORITY_DATA_MAC_ID: {1: MagicMock()},
}

yield lyric
35 changes: 35 additions & 0 deletions tests/components/lyric/fixtures/locations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
{
"locationID": 35202000168931,
"name": "Ocala P01",
"devices": [
{
"vacationHold": { "Enabled": true },
"scheduleStatus": "Resume",
"settings": { "devicePairingEnabled": true },
"deviceClass": "Thermostat",
"deviceType": "Thermostat",
"deviceID": "LCC-7f86b153-8480-f111-b78f-6045bdb25006",
"name": "Ocala",
"macID": "5CFCE1B67035",
"units": "Fahrenheit",
"indoorTemperature": 79,
"deviceModel": "T9-T10"
},
{
"vacationHold": { "Enabled": false },
"scheduleStatus": "Resume",
"settings": { "devicePairingEnabled": true },
"deviceClass": "Thermostat",
"deviceType": "Thermostat",
"deviceID": "LCC-8f86b153-8480-f111-b78f-6045bdb25007",
"name": "Bedroom",
"macID": "5CFCE1B67036",
"units": "Fahrenheit",
"indoorTemperature": 72,
"deviceModel": "T9-T10"
}
],
"users": []
}
]
Loading
Loading