Skip to content

Commit

Permalink
Add time component to Melnor Bluetooth integration
Browse files Browse the repository at this point in the history
  • Loading branch information
vanstinator committed May 30, 2023
1 parent c678664 commit 7a06644
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 3 deletions.
1 change: 1 addition & 0 deletions homeassistant/components/melnor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Platform.NUMBER,
Platform.SENSOR,
Platform.SWITCH,
Platform.TIME,
]


Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/melnor/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"dependencies": ["bluetooth_adapters"],
"documentation": "https://www.home-assistant.io/integrations/melnor",
"iot_class": "local_polling",
"requirements": ["melnor-bluetooth==0.0.21"]
"requirements": ["melnor-bluetooth==0.0.22"]
}
92 changes: 92 additions & 0 deletions homeassistant/components/melnor/time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""Number support for Melnor Bluetooth water timer."""

from __future__ import annotations

from collections.abc import Callable, Coroutine
from dataclasses import dataclass
from datetime import time
from typing import Any

from melnor_bluetooth.device import Valve

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

from .const import DOMAIN
from .models import (
MelnorDataUpdateCoordinator,
MelnorZoneEntity,
get_entities_for_valves,
)


@dataclass
class MelnorZoneTimeEntityDescriptionMixin:
"""Mixin for required keys."""

set_time_fn: Callable[[Valve, time], Coroutine[Any, Any, None]]
state_fn: Callable[[Valve], Any]


@dataclass
class MelnorZoneTimeEntityDescription(
TimeEntityDescription, MelnorZoneTimeEntityDescriptionMixin
):
"""Describes Melnor number entity."""


ZONE_ENTITY_DESCRIPTIONS: list[MelnorZoneTimeEntityDescription] = [
MelnorZoneTimeEntityDescription(
entity_category=EntityCategory.CONFIG,
icon="mdi:calendar-clock-outline",
key="frequency_start_time",
name="Schedule Start Time",
set_time_fn=lambda valve, value: valve.set_frequency_start_time(value),
state_fn=lambda valve: valve.frequency.start_time,
),
]


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

coordinator: MelnorDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]

async_add_entities(
get_entities_for_valves(
coordinator,
ZONE_ENTITY_DESCRIPTIONS,
lambda valve, description: MelnorZoneTime(coordinator, description, valve),
)
)


class MelnorZoneTime(MelnorZoneEntity, TimeEntity):
"""A number implementation for a melnor device."""

entity_description: MelnorZoneTimeEntityDescription

def __init__(
self,
coordinator: MelnorDataUpdateCoordinator,
entity_description: MelnorZoneTimeEntityDescription,
valve: Valve,
) -> None:
"""Initialize a number for a melnor device."""
super().__init__(coordinator, entity_description, valve)

@property
def native_value(self) -> time | None:
"""Return the current value."""
return self.entity_description.state_fn(self._valve)

async def async_set_value(self, value: time) -> None:
"""Update the current value."""
await self.entity_description.set_time_fn(self._valve, value)
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ mcstatus==6.0.0
meater-python==0.0.8

# homeassistant.components.melnor
melnor-bluetooth==0.0.21
melnor-bluetooth==0.0.22

# homeassistant.components.message_bird
messagebird==1.2.0
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ mcstatus==6.0.0
meater-python==0.0.8

# homeassistant.components.melnor
melnor-bluetooth==0.0.21
melnor-bluetooth==0.0.22

# homeassistant.components.meteo_france
meteofrance-api==1.2.0
Expand Down
50 changes: 50 additions & 0 deletions tests/components/melnor/test_time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Test the Melnor sensors."""
from __future__ import annotations

from datetime import time

from homeassistant.core import HomeAssistant
import homeassistant.util.dt as dt_util

from .conftest import (
mock_config_entry,
patch_async_ble_device_from_address,
patch_async_register_callback,
patch_melnor_device,
)

from tests.common import async_fire_time_changed


async def test_schedule_start_time(hass: HomeAssistant) -> None:
"""Test the frequency schedule start time."""

now = dt_util.now()

entry = mock_config_entry(hass)

with patch_async_ble_device_from_address(), patch_melnor_device() as device_patch, patch_async_register_callback():
device = device_patch.return_value

assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()

time_entity = hass.states.get("time.zone_1_schedule_start_time")

assert time_entity is not None
assert time_entity.state == device.zone1.frequency.start_time.isoformat()

await hass.services.async_call(
"time",
"set_value",
{"entity_id": "time.zone_1_schedule_start_time", "time": time(1, 0)},
blocking=True,
)

async_fire_time_changed(hass, now + dt_util.dt.timedelta(seconds=10))
await hass.async_block_till_done()

time_entity = hass.states.get("time.zone_1_schedule_start_time")

assert time_entity is not None
assert time_entity.state == time(1, 0).isoformat()

0 comments on commit 7a06644

Please sign in to comment.