mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Add a switch to Starlink for stow/unstow (#85730)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
d1ecc418bb
commit
ca885f3fab
@ -8,7 +8,12 @@ from homeassistant.core import HomeAssistant
|
||||
from .const import DOMAIN
|
||||
from .coordinator import StarlinkUpdateCoordinator
|
||||
|
||||
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.SENSOR]
|
||||
PLATFORMS: list[Platform] = [
|
||||
Platform.BINARY_SENSOR,
|
||||
Platform.BUTTON,
|
||||
Platform.SENSOR,
|
||||
Platform.SWITCH,
|
||||
]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
|
@ -61,6 +61,6 @@ BUTTONS = [
|
||||
name="Reboot",
|
||||
device_class=ButtonDeviceClass.RESTART,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
press_fn=lambda coordinator: coordinator.reboot_starlink(),
|
||||
press_fn=lambda coordinator: coordinator.async_reboot_starlink(),
|
||||
)
|
||||
]
|
||||
|
@ -13,6 +13,7 @@ from starlink_grpc import (
|
||||
ObstructionDict,
|
||||
StatusDict,
|
||||
reboot,
|
||||
set_stow_state,
|
||||
status_data,
|
||||
)
|
||||
|
||||
@ -56,7 +57,17 @@ class StarlinkUpdateCoordinator(DataUpdateCoordinator[StarlinkData]):
|
||||
except GrpcError as exc:
|
||||
raise UpdateFailed from exc
|
||||
|
||||
async def reboot_starlink(self):
|
||||
async def async_stow_starlink(self, stow: bool):
|
||||
"""Set whether Starlink system tied to this coordinator should be stowed."""
|
||||
async with async_timeout.timeout(4):
|
||||
try:
|
||||
await self.hass.async_add_executor_job(
|
||||
set_stow_state, not stow, self.channel_context
|
||||
)
|
||||
except GrpcError as exc:
|
||||
raise HomeAssistantError from exc
|
||||
|
||||
async def async_reboot_starlink(self):
|
||||
"""Reboot the Starlink system tied to this coordinator."""
|
||||
async with async_timeout.timeout(4):
|
||||
try:
|
||||
|
78
homeassistant/components/starlink/switch.py
Normal file
78
homeassistant/components/starlink/switch.py
Normal file
@ -0,0 +1,78 @@
|
||||
"""Contains switches exposed by the Starlink integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Awaitable, Callable
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.switch import (
|
||||
SwitchDeviceClass,
|
||||
SwitchEntity,
|
||||
SwitchEntityDescription,
|
||||
)
|
||||
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(
|
||||
StarlinkSwitchEntity(coordinator, description) for description in SWITCHES
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class StarlinkSwitchEntityDescriptionMixin:
|
||||
"""Mixin for required keys."""
|
||||
|
||||
value_fn: Callable[[StarlinkData], bool | None]
|
||||
turn_on_fn: Callable[[StarlinkUpdateCoordinator], Awaitable[None]]
|
||||
turn_off_fn: Callable[[StarlinkUpdateCoordinator], Awaitable[None]]
|
||||
|
||||
|
||||
@dataclass
|
||||
class StarlinkSwitchEntityDescription(
|
||||
SwitchEntityDescription, StarlinkSwitchEntityDescriptionMixin
|
||||
):
|
||||
"""Describes a Starlink switch entity."""
|
||||
|
||||
|
||||
class StarlinkSwitchEntity(StarlinkEntity, SwitchEntity):
|
||||
"""A SwitchEntity for Starlink devices. Handles creating unique IDs."""
|
||||
|
||||
entity_description: StarlinkSwitchEntityDescription
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return True if entity is on."""
|
||||
return self.entity_description.value_fn(self.coordinator.data)
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn the entity on."""
|
||||
return await self.entity_description.turn_on_fn(self.coordinator)
|
||||
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Turn the entity off."""
|
||||
return await self.entity_description.turn_off_fn(self.coordinator)
|
||||
|
||||
|
||||
SWITCHES = [
|
||||
StarlinkSwitchEntityDescription(
|
||||
key="stowed",
|
||||
name="Stowed",
|
||||
device_class=SwitchDeviceClass.SWITCH,
|
||||
value_fn=lambda data: data.status["state"] == "STOWED",
|
||||
turn_on_fn=lambda coordinator: coordinator.async_stow_starlink(True),
|
||||
turn_off_fn=lambda coordinator: coordinator.async_stow_starlink(False),
|
||||
)
|
||||
]
|
Loading…
x
Reference in New Issue
Block a user