Skip to content
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

Add time entity to Renson #105031

Merged
merged 13 commits into from
Jan 6, 2024
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,7 @@ omit =
homeassistant/components/renson/fan.py
homeassistant/components/renson/binary_sensor.py
homeassistant/components/renson/number.py
homeassistant/components/renson/time.py
homeassistant/components/raspyrfm/*
homeassistant/components/recollect_waste/sensor.py
homeassistant/components/recorder/repack.py
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/renson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Platform.FAN,
Platform.NUMBER,
Platform.SENSOR,
Platform.TIME,
]


Expand Down
16 changes: 0 additions & 16 deletions homeassistant/components/renson/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
CURRENT_AIRFLOW_INGOING_FIELD,
CURRENT_LEVEL_FIELD,
DAY_POLLUTION_FIELD,
DAYTIME_FIELD,
FILTER_REMAIN_FIELD,
HUMIDITY_FIELD,
INDOOR_TEMP_FIELD,
MANUAL_LEVEL_FIELD,
NIGHT_POLLUTION_FIELD,
NIGHTTIME_FIELD,
OUTDOOR_TEMP_FIELD,
FieldEnum,
)
Expand Down Expand Up @@ -185,20 +183,6 @@ class RensonSensorEntityDescription(
device_class=SensorDeviceClass.ENUM,
options=["off", "level1", "level2", "level3", "level4", "breeze"],
),
RensonSensorEntityDescription(
key="DAYTIME_FIELD",
translation_key="start_day_time",
field=DAYTIME_FIELD,
raw_format=False,
entity_registry_enabled_default=False,
),
RensonSensorEntityDescription(
key="NIGHTTIME_FIELD",
translation_key="start_night_time",
field=NIGHTTIME_FIELD,
raw_format=False,
entity_registry_enabled_default=False,
),
RensonSensorEntityDescription(
key="DAY_POLLUTION_FIELD",
translation_key="day_pollution_level",
Expand Down
14 changes: 8 additions & 6 deletions homeassistant/components/renson/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
"name": "Reset filter counter"
}
},
"time": {
"day_time": {
"name": "Start time of the day"
},
"night_time": {
"name": "Start time of the night"
}
},
"number": {
"filter_change": {
"name": "Filter clean/replacement"
Expand Down Expand Up @@ -125,12 +133,6 @@
"breeze": "[%key:component::renson::entity::sensor::ventilation_level::state::breeze%]"
}
},
"start_day_time": {
"name": "Start day time"
},
"start_night_time": {
"name": "Start night time"
},
"day_pollution_level": {
"name": "Day pollution level",
"state": {
Expand Down
100 changes: 100 additions & 0 deletions homeassistant/components/renson/time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"""Renson ventilation unit time."""
from __future__ import annotations

from collections.abc import Callable
from dataclasses import dataclass
from datetime import datetime, time

from renson_endura_delta.field_enum import DAYTIME_FIELD, NIGHTTIME_FIELD, FieldEnum
from renson_endura_delta.renson import RensonVentilation

from homeassistant.components.time import TimeEntity, TimeEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import RensonData
from .const import DOMAIN
from .coordinator import RensonCoordinator
from .entity import RensonEntity


@dataclass(kw_only=True, frozen=True)
class RensonTimeEntityDescription(TimeEntityDescription):
"""Class describing Renson time entity."""

action_fn: Callable[[RensonVentilation, str], None]
field: FieldEnum


ENTITY_DESCRIPTIONS: tuple[RensonTimeEntityDescription, ...] = (
RensonTimeEntityDescription(
key="day_time",
entity_category=EntityCategory.CONFIG,
translation_key="day_time",
action_fn=lambda api, time: api.set_day_time(time),
field=DAYTIME_FIELD,
),
RensonTimeEntityDescription(
key="night_time",
translation_key="night_time",
entity_category=EntityCategory.CONFIG,
action_fn=lambda api, time: api.set_night_time(time),
field=NIGHTTIME_FIELD,
),
)


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Renson time platform."""

data: RensonData = hass.data[DOMAIN][config_entry.entry_id]

entities = [
RensonTime(description, data.coordinator) for description in ENTITY_DESCRIPTIONS
]

async_add_entities(entities)


class RensonTime(RensonEntity, TimeEntity):
"""Representation of a Renson time entity."""

entity_description: RensonTimeEntityDescription
_attr_has_entity_name = True

def __init__(
self,
description: RensonTimeEntityDescription,
coordinator: RensonCoordinator,
) -> None:
"""Initialize class."""
super().__init__(description.key, coordinator.api, coordinator)

self.entity_description = description

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""

all_data = self.coordinator.data

value = self.api.get_field_value(all_data, self.entity_description.field.name)

self._attr_native_value = datetime.strptime(
value,
"%H:%M",
).time()

super()._handle_coordinator_update()

def set_value(self, value: time) -> None:
"""Triggers the action."""

string_value = value.strftime("%H:%M")
self.entity_description.action_fn(self.api, string_value)