diff --git a/homeassistant/components/vallox/__init__.py b/homeassistant/components/vallox/__init__.py index d12f7b4ffa1..ec2904385df 100644 --- a/homeassistant/components/vallox/__init__.py +++ b/homeassistant/components/vallox/__init__.py @@ -47,6 +47,7 @@ CONFIG_SCHEMA = vol.Schema( PLATFORMS: list[str] = [ Platform.BINARY_SENSOR, + Platform.DATE, Platform.FAN, Platform.NUMBER, Platform.SENSOR, diff --git a/homeassistant/components/vallox/date.py b/homeassistant/components/vallox/date.py new file mode 100644 index 00000000000..43297006599 --- /dev/null +++ b/homeassistant/components/vallox/date.py @@ -0,0 +1,65 @@ +"""Support for Vallox date platform.""" +from __future__ import annotations + +from datetime import date + +from vallox_websocket_api import Vallox + +from homeassistant.components.date import DateEntity +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import EntityCategory +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import ValloxDataUpdateCoordinator, ValloxEntity +from .const import DOMAIN + + +class ValloxFilterChangeDateEntity(ValloxEntity, DateEntity): + """Representation of a Vallox filter change date entity.""" + + _attr_entity_category = EntityCategory.CONFIG + _attr_translation_key = "filter_change_date" + _attr_icon = "mdi:air-filter" + + def __init__( + self, + name: str, + coordinator: ValloxDataUpdateCoordinator, + client: Vallox, + ) -> None: + """Initialize the Vallox date.""" + super().__init__(name, coordinator) + + self._attr_unique_id = f"{self._device_uuid}-filter_change_date" + self._client = client + + @property + def native_value(self) -> date | None: + """Return the latest value.""" + + return self.coordinator.data.filter_change_date + + async def async_set_value(self, value: date) -> None: + """Change the date.""" + + await self._client.set_filter_change_date(value) + await self.coordinator.async_request_refresh() + + +async def async_setup_entry( + hass: HomeAssistant, + entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up Vallox filter change date entity.""" + + data = hass.data[DOMAIN][entry.entry_id] + + async_add_entities( + [ + ValloxFilterChangeDateEntity( + data["name"], data["coordinator"], data["client"] + ) + ] + ) diff --git a/homeassistant/components/vallox/strings.json b/homeassistant/components/vallox/strings.json index e3ade9a55c4..d23d54c75cb 100644 --- a/homeassistant/components/vallox/strings.json +++ b/homeassistant/components/vallox/strings.json @@ -84,6 +84,11 @@ "bypass_locked": { "name": "Bypass locked" } + }, + "date": { + "filter_change_date": { + "name": "Filter change date" + } } }, "services": { diff --git a/tests/components/vallox/conftest.py b/tests/components/vallox/conftest.py index 25d631b4351..08c020c1982 100644 --- a/tests/components/vallox/conftest.py +++ b/tests/components/vallox/conftest.py @@ -104,3 +104,8 @@ def patch_set_fan_speed(): def patch_set_values(): """Patch the Vallox metrics set values.""" return patch("homeassistant.components.vallox.Vallox.set_values") + + +def patch_set_filter_change_date(): + """Patch the Vallox metrics set filter change date.""" + return patch("homeassistant.components.vallox.Vallox.set_filter_change_date") diff --git a/tests/components/vallox/test_date.py b/tests/components/vallox/test_date.py new file mode 100644 index 00000000000..1572e9b205c --- /dev/null +++ b/tests/components/vallox/test_date.py @@ -0,0 +1,50 @@ +"""Tests for Vallox date platform.""" + +from datetime import date + +from vallox_websocket_api import MetricData + +from homeassistant.components.date.const import DOMAIN as DATE_DOMAIN, SERVICE_SET_VALUE +from homeassistant.const import ATTR_DATE, ATTR_ENTITY_ID +from homeassistant.core import HomeAssistant + +from .conftest import patch_set_filter_change_date + +from tests.common import MockConfigEntry + + +async def test_set_filter_change_date( + mock_entry: MockConfigEntry, + hass: HomeAssistant, + setup_fetch_metric_data_mock, +) -> None: + """Test set filter change date.""" + + entity_id = "date.vallox_filter_change_date" + + class MockMetricData(MetricData): + @property + def filter_change_date(self): + return date(2024, 1, 1) + + setup_fetch_metric_data_mock(metric_data_class=MockMetricData) + + with patch_set_filter_change_date() as set_filter_change_date: + await hass.config_entries.async_setup(mock_entry.entry_id) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state + + assert state.state == "2024-01-01" + + await hass.services.async_call( + DATE_DOMAIN, + SERVICE_SET_VALUE, + service_data={ + ATTR_ENTITY_ID: entity_id, + ATTR_DATE: "2024-02-25", + }, + ) + await hass.async_block_till_done() + set_filter_change_date.assert_called_once_with(date(2024, 2, 25))