Skip to content

Commit

Permalink
Add Time platform
Browse files Browse the repository at this point in the history
  • Loading branch information
boswelja committed Feb 17, 2024
1 parent 1247518 commit e2ff208
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
1 change: 1 addition & 0 deletions homeassistant/components/starlink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Platform.DEVICE_TRACKER,
Platform.SENSOR,
Platform.SWITCH,
Platform.TIME,
]


Expand Down
10 changes: 9 additions & 1 deletion homeassistant/components/starlink/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@
"name": "Stowed"
},
"sleep_schedule": {
"name": "Sleep Schedule"
"name": "Sleep schedule"
}
},
"time": {
"sleep_start": {
"name": "Sleep start"
},
"sleep_end": {
"name": "Sleep end"
}
}
}
Expand Down
97 changes: 97 additions & 0 deletions homeassistant/components/starlink/time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"""Contains time pickers exposed by the Starlink integration."""

from __future__ import annotations

from collections.abc import Awaitable, Callable
from dataclasses import dataclass
from datetime import UTC, time
import math

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

from .const import DOMAIN
from .coordinator import StarlinkData, StarlinkUpdateCoordinator
from .entity import StarlinkEntity


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up all binary sensors for this entry."""
coordinator = hass.data[DOMAIN][entry.entry_id]

async_add_entities(
StarlinkTimeEntity(coordinator, description) for description in TIMES
)


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

value_fn: Callable[[StarlinkData], time | None]
update_fn: Callable[[StarlinkUpdateCoordinator, time], Awaitable[None]]
available_fn: Callable[[StarlinkData], bool]


@dataclass
class StarlinkTimeEntityDescription(

Check failure on line 41 in homeassistant/components/starlink/time.py

View workflow job for this annotation

GitHub Actions / Check mypy

Cannot inherit non-frozen dataclass from a frozen one [misc]

Check failure on line 41 in homeassistant/components/starlink/time.py

View workflow job for this annotation

GitHub Actions / Check mypy

Cannot inherit frozen dataclass from a non-frozen one [misc]
TimeEntityDescription, StarlinkTimeEntityDescriptionMixin
):
"""Describes a Starlink switch entity."""


class StarlinkTimeEntity(StarlinkEntity, TimeEntity):
"""A TimeEntity for Starlink devices. Handles creating unique IDs."""

entity_description: StarlinkTimeEntityDescription

@property
def native_value(self) -> time | None:
"""Return the value reported by the time."""
return self.entity_description.value_fn(self.coordinator.data)

@property
def available(self) -> bool:
"""Return True if entity is available."""
return self.entity_description.available_fn(self.coordinator.data)

async def async_set_value(self, value: time) -> None:
"""Change the time."""
return await self.entity_description.update_fn(self.coordinator, value)


def _utc_minutes_to_time(utc_minutes: int) -> time:
hour = math.floor(utc_minutes / 60)
minute = utc_minutes % 60
utc = time(hour=hour, minute=minute, tzinfo=UTC)
return utc


def _time_to_utc_minutes(t: time) -> int:
return (t.hour * 60) + t.minute


TIMES = [
StarlinkTimeEntityDescription(
key="sleep_start",
translation_key="sleep_start",
value_fn=lambda data: _utc_minutes_to_time(data.sleep[0]),
update_fn=lambda coordinator, time: coordinator.async_set_sleep_start(
_time_to_utc_minutes(time)
),
available_fn=lambda data: data.sleep[2],
),
StarlinkTimeEntityDescription(
key="sleep_end",
translation_key="sleep_end",
value_fn=lambda data: _utc_minutes_to_time(data.sleep[0] + data.sleep[1]),
update_fn=lambda coordinator, time: coordinator.async_set_sleep_duration(
_time_to_utc_minutes(time)
),
available_fn=lambda data: data.sleep[2],
),
]

0 comments on commit e2ff208

Please sign in to comment.