mirror of
https://github.com/home-assistant/core.git
synced 2025-07-28 15:47:12 +00:00
Add sauna light control in Huum (#149169)
This commit is contained in:
parent
ecb6cc50b9
commit
79dd91ebc6
@ -4,4 +4,8 @@ from homeassistant.const import Platform
|
||||
|
||||
DOMAIN = "huum"
|
||||
|
||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.CLIMATE]
|
||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.CLIMATE, Platform.LIGHT]
|
||||
|
||||
CONFIG_STEAMER = 1
|
||||
CONFIG_LIGHT = 2
|
||||
CONFIG_STEAMER_AND_LIGHT = 3
|
||||
|
62
homeassistant/components/huum/light.py
Normal file
62
homeassistant/components/huum/light.py
Normal file
@ -0,0 +1,62 @@
|
||||
"""Control for light."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.light import ColorMode, LightEntity
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .const import CONFIG_LIGHT, CONFIG_STEAMER_AND_LIGHT
|
||||
from .coordinator import HuumConfigEntry, HuumDataUpdateCoordinator
|
||||
from .entity import HuumBaseEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: HuumConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up light if applicable."""
|
||||
coordinator = config_entry.runtime_data
|
||||
|
||||
# Light is configured for this sauna.
|
||||
if coordinator.data.config in [CONFIG_LIGHT, CONFIG_STEAMER_AND_LIGHT]:
|
||||
async_add_entities([HuumLight(coordinator)])
|
||||
|
||||
|
||||
class HuumLight(HuumBaseEntity, LightEntity):
|
||||
"""Representation of a light."""
|
||||
|
||||
_attr_name = "Light"
|
||||
_attr_supported_color_modes = {ColorMode.ONOFF}
|
||||
_attr_color_mode = ColorMode.ONOFF
|
||||
|
||||
def __init__(self, coordinator: HuumDataUpdateCoordinator) -> None:
|
||||
"""Initialize the light."""
|
||||
super().__init__(coordinator)
|
||||
|
||||
self._attr_unique_id = coordinator.config_entry.entry_id
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return the current light status."""
|
||||
return self.coordinator.data.light == 1
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn device on."""
|
||||
if not self.is_on:
|
||||
await self._toggle_light()
|
||||
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Turn device off."""
|
||||
if self.is_on:
|
||||
await self._toggle_light()
|
||||
|
||||
async def _toggle_light(self) -> None:
|
||||
await self.coordinator.huum.toggle_light()
|
||||
await self.coordinator.async_refresh()
|
@ -29,8 +29,13 @@ def mock_huum() -> Generator[AsyncMock]:
|
||||
"homeassistant.components.huum.coordinator.Huum.turn_on",
|
||||
return_value=huum,
|
||||
) as turn_on,
|
||||
patch(
|
||||
"homeassistant.components.huum.coordinator.Huum.toggle_light",
|
||||
return_value=huum,
|
||||
) as toggle_light,
|
||||
):
|
||||
huum.status = SaunaStatus.ONLINE_NOT_HEATING
|
||||
huum.config = 3
|
||||
huum.door_closed = True
|
||||
huum.temperature = 30
|
||||
huum.sauna_name = 123456
|
||||
@ -45,6 +50,7 @@ def mock_huum() -> Generator[AsyncMock]:
|
||||
huum.sauna_config.max_timer = 0
|
||||
huum.sauna_config.min_timer = 0
|
||||
huum.turn_on = turn_on
|
||||
huum.toggle_light = toggle_light
|
||||
|
||||
yield huum
|
||||
|
||||
|
58
tests/components/huum/snapshots/test_light.ambr
Normal file
58
tests/components/huum/snapshots/test_light.ambr
Normal file
@ -0,0 +1,58 @@
|
||||
# serializer version: 1
|
||||
# name: test_light[light.huum_sauna_light-entry]
|
||||
EntityRegistryEntrySnapshot({
|
||||
'aliases': set({
|
||||
}),
|
||||
'area_id': None,
|
||||
'capabilities': dict({
|
||||
'supported_color_modes': list([
|
||||
<ColorMode.ONOFF: 'onoff'>,
|
||||
]),
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'light',
|
||||
'entity_category': None,
|
||||
'entity_id': 'light.huum_sauna_light',
|
||||
'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': 'Light',
|
||||
'platform': 'huum',
|
||||
'previous_unique_id': None,
|
||||
'suggested_object_id': None,
|
||||
'supported_features': 0,
|
||||
'translation_key': None,
|
||||
'unique_id': 'AABBCC112233',
|
||||
'unit_of_measurement': None,
|
||||
})
|
||||
# ---
|
||||
# name: test_light[light.huum_sauna_light-state]
|
||||
StateSnapshot({
|
||||
'attributes': ReadOnlyDict({
|
||||
'color_mode': <ColorMode.ONOFF: 'onoff'>,
|
||||
'friendly_name': 'Huum sauna Light',
|
||||
'supported_color_modes': list([
|
||||
<ColorMode.ONOFF: 'onoff'>,
|
||||
]),
|
||||
'supported_features': <LightEntityFeature: 0>,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'light.huum_sauna_light',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'on',
|
||||
})
|
||||
# ---
|
76
tests/components/huum/test_light.py
Normal file
76
tests/components/huum/test_light.py
Normal file
@ -0,0 +1,76 @@
|
||||
"""Tests for the Huum light entity."""
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from syrupy.assertion import SnapshotAssertion
|
||||
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
Platform,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import setup_with_selected_platforms
|
||||
|
||||
from tests.common import MockConfigEntry, snapshot_platform
|
||||
|
||||
ENTITY_ID = "light.huum_sauna_light"
|
||||
|
||||
|
||||
async def test_light(
|
||||
hass: HomeAssistant,
|
||||
mock_huum: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
) -> None:
|
||||
"""Test the initial parameters."""
|
||||
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.LIGHT])
|
||||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
|
||||
|
||||
|
||||
async def test_light_turn_off(
|
||||
hass: HomeAssistant,
|
||||
mock_huum: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test turning off light."""
|
||||
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.LIGHT])
|
||||
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state.state == STATE_ON
|
||||
|
||||
await hass.services.async_call(
|
||||
Platform.LIGHT,
|
||||
SERVICE_TURN_OFF,
|
||||
{ATTR_ENTITY_ID: ENTITY_ID},
|
||||
blocking=True,
|
||||
)
|
||||
mock_huum.toggle_light.assert_called_once()
|
||||
|
||||
|
||||
async def test_light_turn_on(
|
||||
hass: HomeAssistant,
|
||||
mock_huum: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test turning on light."""
|
||||
mock_huum.light = 0
|
||||
|
||||
await setup_with_selected_platforms(hass, mock_config_entry, [Platform.LIGHT])
|
||||
|
||||
state = hass.states.get(ENTITY_ID)
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
await hass.services.async_call(
|
||||
Platform.LIGHT,
|
||||
SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: ENTITY_ID},
|
||||
blocking=True,
|
||||
)
|
||||
mock_huum.toggle_light.assert_called_once()
|
Loading…
x
Reference in New Issue
Block a user