Add Vallox filter replacement date (#111391)

* Vallox: reset filter button

* Better names

* Change from button to date platform

* Review

* Fix

* Drop ValloxDateEntityDescription

* Stale docstrings

* Stale docstring
pull/111930/head
Jevgeni Kiski 2024-03-01 11:02:50 +02:00 committed by GitHub
parent 0d0b64d351
commit ce3d774222
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 126 additions and 0 deletions

View File

@ -47,6 +47,7 @@ CONFIG_SCHEMA = vol.Schema(
PLATFORMS: list[str] = [
Platform.BINARY_SENSOR,
Platform.DATE,
Platform.FAN,
Platform.NUMBER,
Platform.SENSOR,

View File

@ -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"]
)
]
)

View File

@ -84,6 +84,11 @@
"bypass_locked": {
"name": "Bypass locked"
}
},
"date": {
"filter_change_date": {
"name": "Filter change date"
}
}
},
"services": {

View File

@ -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")

View File

@ -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))