8000 Get temperature data appropriate for hass.config.unit by LG-ThinQ-Integration · Pull Request #137626 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Get temperature data appropriate for hass.config.unit #137626

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conv 8000 ersations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion homeassistant/components/lg_thinq/climate.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ def __init__(
self._attr_hvac_modes = [HVACMode.OFF]
self._attr_hvac_mode = HVACMode.OFF
self._attr_preset_modes = []
self._attr_temperature_unit = UnitOfTemperature.CELSIUS
self._attr_temperature_unit = (
self._get_unit_of_measurement(self.data.unit) or UnitOfTemperature.CELSIUS
)
self._requested_hvac_mode: str | None = None

# Set up HVAC modes.
Expand Down Expand Up @@ -182,6 +184,11 @@ def _update_status(self) -> None:
self._attr_target_temperature_high = self.data.target_temp_high
self._attr_target_temperature_low = self.data.target_temp_low

# Update unit.
self._attr_temperature_unit = (
self._get_unit_of_measurement(self.data.unit) or UnitOfTemperature.CELSIUS
)

_LOGGER.debug(
"[%s:%s] update status: c:%s, t:%s, l:%s, h:%s, hvac:%s, unit:%s, step:%s",
self.coordinator.device_name,
Expand Down
9 changes: 9 additions & 0 deletions homeassistant/components/lg_thinq/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from datetime import timedelta
from typing import Final

from homeassistant.const import UnitOfTemperature

# Config flow
DOMAIN = "lg_thinq"
COMPANY = "LGE"
Expand All @@ -18,3 +20,10 @@
# MQTT: Message types
DEVICE_PUSH_MESSAGE: Final = "DEVICE_PUSH"
DEVICE_STATUS_MESSAGE: Final = "DEVICE_STATUS"

# Unit conversion map
DEVICE_UNIT_TO_HA: dict[str, str] = {
"F": UnitOfTemperature.FAHRENHEIT,
"C": UnitOfTemperature.CELSIUS,
}
REVERSE_DEVICE_UNIT_TO_HA = {v: k for k, v in DEVICE_UNIT_TO_HA.items()}
40 changes: 38 additions & 2 deletions homeassistant/components/lg_thinq/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

from __future__ import annotations

from collections.abc import Mapping
import logging
from typing import TYPE_CHECKING, Any

from thinqconnect import ThinQAPIException
from thinqconnect.integration import HABridge

from homeassistant.core import HomeAssistant
from homeassistant.const import EVENT_CORE_CONFIG_UPDATE
from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

if TYPE_CHECKING:
from . import ThinqConfigEntry

from .const import DOMAIN
from .const import DOMAIN, REVERSE_DEVICE_UNIT_TO_HA

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -54,6 +56,40 @@
f"{self.device_id}_{self.sub_id}" if self.sub_id else self.device_id
)

# Set your preferred temperature unit. This will allow us to retrieve
# temperature values from the API in a converted value corresponding to
# preferred unit.
self._update_preferred_temperature_unit()

# Add a callback to handle core config update.
self.unit_system: str | None = None
self.hass.bus.async_listen(
event_type=EVENT_CORE_CONFIG_UPDATE,
listener=self._handle_update_config,
event_filter=self.async_config_update_filter,
)

async def _handle_update_config(self, _: Event) -> None:
"""Handle update core config."""
self._update_preferred_temperature_unit()

Check warning on line 74 in homeassistant/components/lg_thinq/coordinator.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/lg_thinq/coordinator.py#L74

Added line #L74 was not covered by tests

await self.async_refresh()

Check warning on line 76 in homeassistant/components/lg_thinq/coordinator.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/lg_thinq/coordinator.py#L76

Added line #L76 was not covered by tests

@callback
def async_config_update_filter(self, event_data: Mapping[str, Any]) -> bool:
"""Filter out unwanted events."""
if (unit_system := event_data.get("unit_system")) != self.unit_system:
self.unit_system = unit_system
return True

Check warning on line 83 in homeassistant/components/lg_thinq/coordinator.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/lg_thinq/coordinator.py#L81-L83

Added lines #L81 - L83 were not covered by tests

return False

Check warning on line 85 in homeassistant/components/lg_thinq/coordinator.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/lg_thinq/coordinator.py#L85

Added line #L85 was not covered by tests

def _update_preferred_temperature_unit(self) -> None:
"""Update preferred temperature unit."""
self.api.set_preferred_temperature_unit(
REVERSE_DEVICE_UNIT_TO_HA.get(self.hass.config.units.temperature_unit)
)

async def _async_update_data(self) -> dict[str, Any]:
"""Request to the server to update the status from full response data."""
try:
Expand Down
10 changes: 2 additions & 8 deletions homeassistant/components/lg_thinq/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,19 @@
from thinqconnect.devices.const import Location
from thinqconnect.integration import PropertyState

from homeassistant.const import UnitOfTemperature
from homeassistant.core import callback
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.entity import EntityDescription
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import COMPANY, DOMAIN
from .const import COMPANY, DEVICE_UNIT_TO_HA, DOMAIN
from .coordinator import DeviceDataUpdateCoordinator

_LOGGER = logging.getLogger(__name__)

EMPTY_STATE = PropertyState()

UNIT_CONVERSION_MAP: dict[str, str] = {
"F": UnitOfTemperature.FAHRENHEIT,
"C": UnitOfTemperature.CELSIUS,
}


class ThinQEntity(CoordinatorEntity[DeviceDataUpdateCoordinator]):
"""The base implementation of all lg thinq entities."""
Expand Down Expand Up @@ -75,7 +69,7 @@ def _get_unit_of_measurement(self, unit: str | None) -> str | None:
if unit is None:
return None

return UNIT_CONVERSION_MAP.get(unit)
return DEVICE_UNIT_TO_HA.get(unit)

def _update_status(self) -> None:
"""Update status itself.
Expand Down
16 changes: 8 additions & 8 deletions tests/components/lg_thinq/snapshots/test_climate.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<HVACMode.COOL: 'cool'>,
<HVACMode.DRY: 'dry'>,
]),
'max_temp': 30,
'min_temp': 18,
'max_temp': 86,
'min_temp': 64,
'preset_modes': list([
'air_clean',
]),
Expand All @@ -28,7 +28,7 @@
'on',
'off',
]),
'target_temp_step': 1,
'target_temp_step': 2,
}),
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
Expand Down Expand Up @@ -62,7 +62,7 @@
StateSnapshot({
'attributes': ReadOnlyDict({
'current_humidity': 40,
'current_temperature': 25,
'current_temperature': 77,
'fan_mode': 'mid',
'fan_modes': list([
'low',
Expand All @@ -75,8 +75,8 @@
<HVACMode.COOL: 'cool'>,
<HVACMode.DRY: 'dry'>,
]),
'max_temp': 30,
'min_temp': 18,
'max_temp': 86,
'min_temp': 64,
'preset_mode': None,
'preset_modes': list([
'air_clean',
Expand All @@ -94,8 +94,8 @@
]),
'target_temp_high': None,
'target_temp_low': None,
'target_temp_step': 1,
'temperature': 19,
'target_temp_step': 2,
'temperature': 66,
}),
'context': <ANY>,
'entity_id': 'climate.test_air_conditioner',
Expand Down
3 changes: 2 additions & 1 deletion tests/components/lg_thinq/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
from syrupy import SnapshotAssertion

from homeassistant.const import Platform
from homeassistant.const import Platform, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er

Expand All @@ -23,6 +23,7 @@ async def test_all_entities(
entity_registry: er.EntityRegistry,
) -> None:
"""Test all entities."""
hass.config.units.temperature_unit = UnitOfTemperature.FAHRENHEIT
with patch("homeassistant.components.lg_thinq.PLATFORMS", [Platform.CLIMATE]):
await setup_integration(hass, mock_config_entry)

Expand Down
Loading
0