8000 Improve Home Connect oven cavity temperature sensor by Diegorro98 · Pull Request #139355 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Improve Home Connect oven cavity temperature sensor #139355

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
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
9 changes: 9 additions & 0 deletions homeassistant/components/home_connect/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from aiohomeconnect.model import EventKey, OptionKey, ProgramKey, SettingKey, StatusKey

from homeassistant.const import UnitOfTemperature, UnitOfTime, UnitOfVolume

from .utils import bsh_key_to_translation_key

DOMAIN = "home_connect"
Expand All @@ -21,6 +23,13 @@
"WasherDryer",
)

UNIT_MAP = {
"seconds": UnitOfTime.SECONDS,
"ml": UnitOfVolume.MILLILITERS,
"°C": UnitOfTemperature.CELSIUS,
"°F": UnitOfTemperature.FAHRENHEIT,
}


BSH_POWER_ON = "BSH.Common.EnumType.PowerState.On"
BSH_POWER_OFF = "BSH.Common.EnumType.PowerState.Off"
Expand Down
9 changes: 1 addition & 8 deletions homeassistant/components/home_connect/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
NumberEntity,
NumberEntityDescription,
)
from homeassistant.const import UnitOfTemperature, UnitOfTime, UnitOfVolume
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
Expand All @@ -23,6 +22,7 @@
SVE_TRANSLATION_PLACEHOLDER_ENTITY_ID,
SVE_TRANSLATION_PLACEHOLDER_KEY,
SVE_TRANSLATION_PLACEHOLDER_VALUE,
UNIT_MAP,
)
from .coordinator import HomeConnectApplianceData, HomeConnectConfigEntry
from .entity import HomeConnectEntity, HomeConnectOptionEntity
Expand All @@ -32,13 +32,6 @@

PARALLEL_UPDATES = 1

UNIT_MAP = {
"seconds": UnitOfTime.SECONDS,
"ml": UnitOfVolume.MILLILITERS,
"°C": UnitOfTemperature.CELSIUS,
"°F": UnitOfTemperature.FAHRENHEIT,
}

NUMBERS = (
NumberEntityDescription(
key=SettingKey.REFRIGERATION_FRIDGE_FREEZER_SETPOINT_TEMPERATURE_REFRIGERATOR,
Expand Down
30 changes: 29 additions & 1 deletion homeassistant/components/home_connect/sensor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Provides a sensor for Home Connect."""

import contextlib
from dataclasses import dataclass
from datetime import timedelta
from typing import cast

from aiohomeconnect.model import EventKey, StatusKey
from aiohomeconnect.model.error import HomeConnectError

from homeassistant.components.sensor import (
SensorDeviceClass,
Expand All @@ -23,6 +25,7 @@
BSH_OPERATION_STATE_FINISHED,
BSH_OPERATION_STATE_PAUSE,
BSH_OPERATION_STATE_RUN,
UNIT_MAP,
)
from .coordinator import HomeConnectApplianceData, HomeConnectConfigEntry
from .entity import HomeConnectEntity
Expand All @@ -40,6 +43,7 @@ class HomeConnectSensorEntityDescription(

default_value: str | None = None
appliance_types: tuple[str, ...] | None = None
fetch_unit: bool = False


BSH_PROGRAM_SENSORS = (
Expand Down Expand Up @@ -183,7 +187,8 @@ class HomeConnectSensorEntityDescription(
key=StatusKey.COOKING_OVEN_CURRENT_CAVITY_TEMPERATURE,
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
translation_key="current_cavity_temperature",
translation_key="oven_current_cavity_temperature",
fetch_unit=True,
),
)

Expand Down Expand Up @@ -318,6 +323,29 @@ def _update_native_value(self, status: str | float) -> None:
case _:
self._attr_native_value = status

async def async_added_to_hass(self) -> None:
"""When entity is added to hass."""
await super().async_added_to_hass()
if self.entity_description.fetch_unit:
data = self.appliance.status[cast(StatusKey, self.bsh_key)]
if data.unit:
self._attr_native_unit_of_measurement = UNIT_MAP.get(
data.unit, data.unit
)
else:
await self.fetch_unit()

async def fetch_unit(self) -> None:
"""Fetch the unit of measurement."""
with contextlib.suppress(HomeConnectError):
data = await self.coordinator.client.get_status_value(
self.appliance.info.ha_id, status_key=cast(StatusKey, self.bsh_key)
)
if data.unit:
self._attr_native_unit_of_measurement = UNIT_MAP.get(
data.unit, data.unit
)


class HomeConnectProgramSensor(HomeConnectSensor):
"""Sensor class for Home Connect sensors that reports information related to the running program."""
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/home_connect/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -1529,8 +1529,8 @@
"map3": "Map 3"
}
},
"current_cavity_temperature": {
"name": "Current cavity temperature"
"oven_current_cavity_temperature": {
"name": "Current oven cavity temperature"
},
"freezer_door_alarm": {
"name": "Freezer door alarm",
Expand Down
83 changes: 83 additions & 0 deletions tests/components/home_connect/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from aiohomeconnect.model import (
ArrayOfEvents,
ArrayOfStatus,
Event,
EventKey,
EventMessage,
Expand Down Expand Up @@ -565,3 +566,85 @@ async def test_sensors_states(
)
await hass.async_block_till_done()
assert hass.states.is_state(entity_id, expected)


@pytest.mark.parametrize(
(
"appliance_ha_id",
"entity_id",
"status_key",
"unit_get_status",
"unit_get_status_value",
"get_status_value_call_count",
),
[
(
"Oven",
"sensor.oven_current_oven_cavity_temperature",
StatusKey.COOKING_OVEN_CURRENT_CAVITY_TEMPERATURE,
"°C",
None,
0,
),
(
"Oven",
"sensor.oven_current_oven_cavity_temperature",
StatusKey.COOKING_OVEN_CURRENT_CAVITY_TEMPERATURE,
None,
"°C",
1,
),
],
indirect=["appliance_ha_id"],
)
async def test_sensor_unit_fetching(
appliance_ha_id: str,
entity_id: str,
status_key: StatusKey,
unit_get_status: str | None,
unit_get_status_value: str | None,
get_status_value_call_count: int,
hass: HomeAssistant,
config_entry: MockConfigEntry,
integration_setup: Callable[[MagicMock], Awaitable[bool]],
setup_credentials: None,
client: MagicMock,
) -> None:
"""Test that the sensor entities are capable of fetching units."""

async def get_status_mock(ha_id: str) -> ArrayOfStatus:
if ha_id != appliance_ha_id:
return ArrayOfStatus([])
return ArrayOfStatus(
[
Status(
key=status_key,
raw_key=status_key.value,
value=0,
unit=unit_get_status,
)
]
)

client.get_status = AsyncMock(side_effect=get_status_mock)
client.get_status_value = AsyncMock(
return_value=Status(
key=status_key,
raw_key=status_key.value,
value=0,
unit=unit_get_status_value,
)
)

assert config_entry.state == ConfigEntryState.NOT_LOADED
assert await integration_setup(client)
assert config_entry.state == ConfigEntryState.LOADED

entity_state = hass.states.get(entity_id)
assert entity_state
assert (
entity_state.attributes["unit_of_measurement"] == unit_get_status
or unit_get_status_value
)

assert client.get_status_value.call_count == get_status_value_call_count
0