mirror of
https://github.com/natekspencer/hacs-oasis_mini.git
synced 2025-11-15 00:23:51 -05:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4186755a92 | ||
|
|
7c8ca361ba | ||
|
|
07446f56da | ||
|
|
bd5b2e876d | ||
|
|
36da0249b7 | ||
|
|
bcc8547e3e | ||
|
|
e678b20990 | ||
|
|
cda435070d | ||
|
|
9b85d939c4 | ||
|
|
4eb86c5541 | ||
|
|
e35ae0d4fa | ||
|
|
21105e497a |
@@ -3,7 +3,7 @@
|
|||||||
"name": "Home Assistant integration development",
|
"name": "Home Assistant integration development",
|
||||||
"image": "mcr.microsoft.com/devcontainers/python:1-3.12-bullseye",
|
"image": "mcr.microsoft.com/devcontainers/python:1-3.12-bullseye",
|
||||||
"postCreateCommand": "sudo apt-get update && sudo apt-get install libturbojpeg0",
|
"postCreateCommand": "sudo apt-get update && sudo apt-get install libturbojpeg0",
|
||||||
"postAttachCommand": ".devcontainer/setup",
|
"postAttachCommand": "scripts/setup",
|
||||||
"forwardPorts": [8123],
|
"forwardPorts": [8123],
|
||||||
"customizations": {
|
"customizations": {
|
||||||
"vscode": {
|
"vscode": {
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ PLATFORMS = [
|
|||||||
Platform.SELECT,
|
Platform.SELECT,
|
||||||
Platform.SENSOR,
|
Platform.SENSOR,
|
||||||
Platform.SWITCH,
|
Platform.SWITCH,
|
||||||
|
Platform.UPDATE,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any, Coroutine
|
from dataclasses import dataclass
|
||||||
|
import random
|
||||||
|
from typing import Awaitable, Callable
|
||||||
|
|
||||||
from homeassistant.components.button import (
|
from homeassistant.components.button import (
|
||||||
ButtonDeviceClass,
|
ButtonDeviceClass,
|
||||||
@@ -11,31 +13,68 @@ from homeassistant.components.button import (
|
|||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity import EntityDescription
|
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import OasisMiniCoordinator
|
from .coordinator import OasisMiniCoordinator
|
||||||
from .entity import OasisMiniEntity
|
from .entity import OasisMiniEntity
|
||||||
|
from .pyoasismini import OasisMini
|
||||||
from .pyoasismini.const import TRACKS
|
from .pyoasismini.const import TRACKS
|
||||||
|
|
||||||
|
|
||||||
class OasisMiniButtonEntity(OasisMiniEntity, ButtonEntity):
|
|
||||||
"""Oasis Mini button entity."""
|
|
||||||
|
|
||||||
async def async_press(self) -> None:
|
|
||||||
"""Press the button."""
|
|
||||||
await self.device.async_reboot()
|
|
||||||
|
|
||||||
|
|
||||||
DESCRIPTOR = ButtonEntityDescription(
|
|
||||||
key="reboot", device_class=ButtonDeviceClass.RESTART
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Oasis Mini button using config entry."""
|
"""Set up Oasis Mini button using config entry."""
|
||||||
coordinator: OasisMiniCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator: OasisMiniCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
async_add_entities([OasisMiniButtonEntity(coordinator, entry, DESCRIPTOR)])
|
async_add_entities(
|
||||||
|
[
|
||||||
|
OasisMiniButtonEntity(coordinator, entry, descriptor)
|
||||||
|
for descriptor in DESCRIPTORS
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def play_random_track(device: OasisMini) -> None:
|
||||||
|
"""Play random track."""
|
||||||
|
track = int(random.choice(list(TRACKS)))
|
||||||
|
if track not in device.playlist:
|
||||||
|
await device.async_add_track_to_playlist(track)
|
||||||
|
|
||||||
|
# Move track to next item in the playlist and then select it
|
||||||
|
if (idx := device.playlist.index(track)) != (next_idx := device.playlist_index + 1):
|
||||||
|
await device.async_move_track(idx, next_idx)
|
||||||
|
await device.async_change_track(next_idx)
|
||||||
|
await device.async_play()
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True, kw_only=True)
|
||||||
|
class OasisMiniButtonEntityDescription(ButtonEntityDescription):
|
||||||
|
"""Oasis Mini button entity description."""
|
||||||
|
|
||||||
|
press_fn: Callable[[OasisMini], Awaitable[None]]
|
||||||
|
|
||||||
|
|
||||||
|
DESCRIPTORS = (
|
||||||
|
OasisMiniButtonEntityDescription(
|
||||||
|
key="reboot",
|
||||||
|
device_class=ButtonDeviceClass.RESTART,
|
||||||
|
press_fn=lambda device: device.async_reboot(),
|
||||||
|
),
|
||||||
|
OasisMiniButtonEntityDescription(
|
||||||
|
key="random_track",
|
||||||
|
name="Play random track",
|
||||||
|
press_fn=play_random_track,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class OasisMiniButtonEntity(OasisMiniEntity, ButtonEntity):
|
||||||
|
"""Oasis Mini button entity."""
|
||||||
|
|
||||||
|
entity_description: OasisMiniButtonEntityDescription
|
||||||
|
|
||||||
|
async def async_press(self) -> None:
|
||||||
|
"""Press the button."""
|
||||||
|
await self.entity_description.press_fn(self.device)
|
||||||
|
await self.coordinator.async_request_refresh()
|
||||||
|
|||||||
@@ -10,10 +10,9 @@ from aiohttp import ClientConnectorError
|
|||||||
from httpx import ConnectError, HTTPStatusError
|
from httpx import ConnectError, HTTPStatusError
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry, ConfigFlow
|
from homeassistant.config_entries import ConfigEntry, ConfigFlow, ConfigFlowResult
|
||||||
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_EMAIL, CONF_HOST, CONF_PASSWORD
|
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_EMAIL, CONF_HOST, CONF_PASSWORD
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.data_entry_flow import FlowResult
|
|
||||||
from homeassistant.helpers.schema_config_entry_flow import (
|
from homeassistant.helpers.schema_config_entry_flow import (
|
||||||
SchemaCommonFlowHandler,
|
SchemaCommonFlowHandler,
|
||||||
SchemaFlowError,
|
SchemaFlowError,
|
||||||
@@ -30,16 +29,14 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
STEP_USER_DATA_SCHEMA = vol.Schema({vol.Required(CONF_HOST): str})
|
STEP_USER_DATA_SCHEMA = vol.Schema({vol.Required(CONF_HOST): str})
|
||||||
OPTIONS_SCHEMA = vol.Schema(
|
OPTIONS_SCHEMA = vol.Schema(
|
||||||
{
|
{vol.Required(CONF_EMAIL): str, vol.Required(CONF_PASSWORD): str}
|
||||||
vol.Optional(CONF_EMAIL): str,
|
|
||||||
vol.Optional(CONF_PASSWORD): str,
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def cloud_login(
|
async def cloud_login(
|
||||||
handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
|
handler: SchemaCommonFlowHandler, user_input: dict[str, Any]
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
|
"""Cloud login."""
|
||||||
coordinator: OasisMiniCoordinator = handler.parent_handler.hass.data[DOMAIN][
|
coordinator: OasisMiniCoordinator = handler.parent_handler.hass.data[DOMAIN][
|
||||||
handler.parent_handler.config_entry.entry_id
|
handler.parent_handler.config_entry.entry_id
|
||||||
]
|
]
|
||||||
@@ -49,8 +46,8 @@ async def cloud_login(
|
|||||||
email=user_input[CONF_EMAIL], password=user_input[CONF_PASSWORD]
|
email=user_input[CONF_EMAIL], password=user_input[CONF_PASSWORD]
|
||||||
)
|
)
|
||||||
user_input[CONF_ACCESS_TOKEN] = coordinator.device.access_token
|
user_input[CONF_ACCESS_TOKEN] = coordinator.device.access_token
|
||||||
except:
|
except Exception as ex:
|
||||||
raise SchemaFlowError("invalid_auth")
|
raise SchemaFlowError("invalid_auth") from ex
|
||||||
|
|
||||||
del user_input[CONF_PASSWORD]
|
del user_input[CONF_PASSWORD]
|
||||||
return user_input
|
return user_input
|
||||||
@@ -75,7 +72,7 @@ class OasisMiniConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
"""Get the options flow for this handler."""
|
"""Get the options flow for this handler."""
|
||||||
return SchemaOptionsFlowHandler(config_entry, OPTIONS_FLOW)
|
return SchemaOptionsFlowHandler(config_entry, OPTIONS_FLOW)
|
||||||
|
|
||||||
# async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
|
# async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> ConfigFlowResult:
|
||||||
# """Handle dhcp discovery."""
|
# """Handle dhcp discovery."""
|
||||||
# self.host = discovery_info.ip
|
# self.host = discovery_info.ip
|
||||||
# self.name = discovery_info.hostname
|
# self.name = discovery_info.hostname
|
||||||
@@ -85,13 +82,29 @@ class OasisMiniConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
async def async_step_user(
|
async def async_step_user(
|
||||||
self, user_input: dict[str, Any] | None = None
|
self, user_input: dict[str, Any] | None = None
|
||||||
) -> FlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Handle the initial step."""
|
"""Handle the initial step."""
|
||||||
return await self._async_step("user", STEP_USER_DATA_SCHEMA, user_input)
|
return await self._async_step("user", STEP_USER_DATA_SCHEMA, user_input)
|
||||||
|
|
||||||
|
async def async_step_reconfigure(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> ConfigFlowResult:
|
||||||
|
"""Handle reconfiguration."""
|
||||||
|
entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])
|
||||||
|
assert entry
|
||||||
|
|
||||||
|
suggested_values = user_input or entry.data
|
||||||
|
return await self._async_step(
|
||||||
|
"reconfigure", STEP_USER_DATA_SCHEMA, user_input, suggested_values
|
||||||
|
)
|
||||||
|
|
||||||
async def _async_step(
|
async def _async_step(
|
||||||
self, step_id: str, schema: vol.Schema, user_input: dict[str, Any] | None = None
|
self,
|
||||||
) -> FlowResult:
|
step_id: str,
|
||||||
|
schema: vol.Schema,
|
||||||
|
user_input: dict[str, Any] | None = None,
|
||||||
|
suggested_values: dict[str, Any] | None = None,
|
||||||
|
) -> ConfigFlowResult:
|
||||||
"""Handle step setup."""
|
"""Handle step setup."""
|
||||||
if abort := self._abort_if_configured(user_input):
|
if abort := self._abort_if_configured(user_input):
|
||||||
return abort
|
return abort
|
||||||
@@ -108,21 +121,26 @@ class OasisMiniConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
existing_entry, data=data
|
existing_entry, data=data
|
||||||
)
|
)
|
||||||
await self.hass.config_entries.async_reload(existing_entry.entry_id)
|
await self.hass.config_entries.async_reload(existing_entry.entry_id)
|
||||||
return self.async_abort(reason="reauth_successful")
|
return self.async_abort(reason="reconfigure_successful")
|
||||||
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=f"Oasis Mini {self.serial_number}",
|
title=f"Oasis Mini {self.serial_number}",
|
||||||
data=data,
|
data=data,
|
||||||
)
|
)
|
||||||
|
|
||||||
return self.async_show_form(step_id=step_id, data_schema=schema, errors=errors)
|
return self.async_show_form(
|
||||||
|
step_id=step_id,
|
||||||
|
data_schema=self.add_suggested_values_to_schema(schema, suggested_values),
|
||||||
|
errors=errors,
|
||||||
|
)
|
||||||
|
|
||||||
async def validate_client(self, user_input: dict[str, Any]) -> dict[str, str]:
|
async def validate_client(self, user_input: dict[str, Any]) -> dict[str, str]:
|
||||||
"""Validate client setup."""
|
"""Validate client setup."""
|
||||||
errors = {}
|
errors = {}
|
||||||
try:
|
try:
|
||||||
client = create_client({"host": self.host} | user_input)
|
async with asyncio.timeout(10):
|
||||||
self.serial_number = await client.async_get_serial_number()
|
client = create_client({"host": self.host} | user_input)
|
||||||
|
self.serial_number = await client.async_get_serial_number()
|
||||||
if not self.serial_number:
|
if not self.serial_number:
|
||||||
errors["base"] = "invalid_host"
|
errors["base"] = "invalid_host"
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
@@ -143,7 +161,7 @@ class OasisMiniConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
@callback
|
@callback
|
||||||
def _abort_if_configured(
|
def _abort_if_configured(
|
||||||
self, user_input: dict[str, Any] | None
|
self, user_input: dict[str, Any] | None
|
||||||
) -> FlowResult | None:
|
) -> ConfigFlowResult | None:
|
||||||
"""Abort if configured."""
|
"""Abort if configured."""
|
||||||
if self.host or user_input:
|
if self.host or user_input:
|
||||||
data = {CONF_HOST: self.host, **(user_input or {})}
|
data = {CONF_HOST: self.host, **(user_input or {})}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ STATUS_CODE_MAP = {
|
|||||||
4: "running",
|
4: "running",
|
||||||
5: "paused",
|
5: "paused",
|
||||||
9: "error",
|
9: "error",
|
||||||
|
11: "updating",
|
||||||
13: "downloading",
|
13: "downloading",
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,7 +60,6 @@ LED_EFFECTS: Final[dict[str, str]] = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
CLOUD_BASE_URL = "https://app.grounded.so"
|
CLOUD_BASE_URL = "https://app.grounded.so"
|
||||||
CLOUD_API_URL = f"{CLOUD_BASE_URL}/api"
|
|
||||||
|
|
||||||
|
|
||||||
class OasisMini:
|
class OasisMini:
|
||||||
@@ -72,6 +72,7 @@ class OasisMini:
|
|||||||
|
|
||||||
brightness: int
|
brightness: int
|
||||||
color: str
|
color: str
|
||||||
|
download_progress: int
|
||||||
led_effect: str
|
led_effect: str
|
||||||
led_speed: int
|
led_speed: int
|
||||||
max_brightness: int
|
max_brightness: int
|
||||||
@@ -127,6 +128,11 @@ class OasisMini:
|
|||||||
"""Return the url."""
|
"""Return the url."""
|
||||||
return f"http://{self._host}/"
|
return f"http://{self._host}/"
|
||||||
|
|
||||||
|
async def async_add_track_to_playlist(self, track: int) -> None:
|
||||||
|
"""Add track to playlist."""
|
||||||
|
await self._async_command(params={"ADDJOBLIST": track})
|
||||||
|
self.playlist.append(track)
|
||||||
|
|
||||||
async def async_change_track(self, index: int) -> None:
|
async def async_change_track(self, index: int) -> None:
|
||||||
"""Change the track."""
|
"""Change the track."""
|
||||||
if index >= len(self.playlist):
|
if index >= len(self.playlist):
|
||||||
@@ -156,6 +162,10 @@ class OasisMini:
|
|||||||
setattr(self, attr, value)
|
setattr(self, attr, value)
|
||||||
return status
|
return status
|
||||||
|
|
||||||
|
async def async_move_track(self, _from: int, _to: int) -> None:
|
||||||
|
"""Move a track in the playlist."""
|
||||||
|
await self._async_command(params={"MOVEJOB": f"{_from};{_to}"})
|
||||||
|
|
||||||
async def async_pause(self) -> None:
|
async def async_pause(self) -> None:
|
||||||
"""Send pause command."""
|
"""Send pause command."""
|
||||||
await self._async_command(params={"CMDPAUSE": ""})
|
await self._async_command(params={"CMDPAUSE": ""})
|
||||||
@@ -213,25 +223,16 @@ class OasisMini:
|
|||||||
)
|
)
|
||||||
|
|
||||||
async def async_set_pause_between_tracks(self, pause: bool) -> None:
|
async def async_set_pause_between_tracks(self, pause: bool) -> None:
|
||||||
"""Set the Oasis Mini pause between tracks."""
|
"""Set pause between tracks."""
|
||||||
await self._async_command(params={"WRIWAITAFTER": 1 if pause else 0})
|
await self._async_command(params={"WRIWAITAFTER": 1 if pause else 0})
|
||||||
|
|
||||||
async def async_set_repeat_playlist(self, repeat: bool) -> None:
|
async def async_set_repeat_playlist(self, repeat: bool) -> None:
|
||||||
"""Set the Oasis Mini repeat playlist."""
|
"""Set repeat playlist."""
|
||||||
await self._async_command(params={"WRIREPEATJOB": 1 if repeat else 0})
|
await self._async_command(params={"WRIREPEATJOB": 1 if repeat else 0})
|
||||||
|
|
||||||
async def _async_command(self, **kwargs: Any) -> str | None:
|
async def async_upgrade(self, beta: bool = False) -> None:
|
||||||
"""Send a command request."""
|
"""Trigger a software upgrade."""
|
||||||
result = await self._async_get(**kwargs)
|
await self._async_command(params={"CMDUPGRADE": 1 if beta else 0})
|
||||||
_LOGGER.debug("Result: %s", result)
|
|
||||||
|
|
||||||
async def _async_get(self, **kwargs: Any) -> str | None:
|
|
||||||
"""Perform a GET request."""
|
|
||||||
response = await self._session.get(self.url, **kwargs)
|
|
||||||
if response.status == 200:
|
|
||||||
text = await response.text()
|
|
||||||
return text
|
|
||||||
return None
|
|
||||||
|
|
||||||
async def async_cloud_login(self, email: str, password: str) -> None:
|
async def async_cloud_login(self, email: str, password: str) -> None:
|
||||||
"""Login via the cloud."""
|
"""Login via the cloud."""
|
||||||
@@ -244,34 +245,21 @@ class OasisMini:
|
|||||||
|
|
||||||
async def async_cloud_logout(self) -> None:
|
async def async_cloud_logout(self) -> None:
|
||||||
"""Login via the cloud."""
|
"""Login via the cloud."""
|
||||||
if not self.access_token:
|
await self._async_cloud_request("GET", "api/auth/logout")
|
||||||
return
|
|
||||||
await self._async_request(
|
|
||||||
"GET",
|
|
||||||
urljoin(CLOUD_BASE_URL, "api/auth/logout"),
|
|
||||||
headers={"Authorization": f"Bearer {self.access_token}"},
|
|
||||||
)
|
|
||||||
|
|
||||||
async def async_cloud_get_track_info(self, track_id: int) -> None:
|
async def async_cloud_get_track_info(self, track_id: int) -> dict[str, Any]:
|
||||||
"""Get cloud track info."""
|
"""Get cloud track info."""
|
||||||
if not self.access_token:
|
return await self._async_cloud_request("GET", f"api/track/{track_id}")
|
||||||
return
|
|
||||||
|
|
||||||
response = await self._async_request(
|
async def async_cloud_get_tracks(self, tracks: list[int]) -> dict:
|
||||||
"GET",
|
"""Get tracks info from the cloud"""
|
||||||
urljoin(CLOUD_BASE_URL, f"api/track/{track_id}"),
|
return await self._async_cloud_request(
|
||||||
headers={"Authorization": f"Bearer {self.access_token}"},
|
"GET", "api/track", params={"ids[]": tracks}
|
||||||
)
|
)
|
||||||
return response
|
|
||||||
|
|
||||||
async def _async_request(self, method: str, url: str, **kwargs) -> Any:
|
async def async_cloud_get_latest_software_details(self) -> dict[str, int | str]:
|
||||||
"""Login via the cloud."""
|
"""Get the latest software details from the cloud."""
|
||||||
response = await self._session.request(method, url, **kwargs)
|
return await self._async_cloud_request("GET", "api/software/last-version")
|
||||||
if response.status == 200:
|
|
||||||
if response.headers.get("Content-Type") == "application/json":
|
|
||||||
return await response.json()
|
|
||||||
return await response.text()
|
|
||||||
response.raise_for_status()
|
|
||||||
|
|
||||||
async def async_get_current_track_details(self) -> dict:
|
async def async_get_current_track_details(self) -> dict:
|
||||||
"""Get current track info, refreshing if needed."""
|
"""Get current track info, refreshing if needed."""
|
||||||
@@ -283,3 +271,39 @@ class OasisMini:
|
|||||||
self._current_track_details = await self.async_cloud_get_track_info(
|
self._current_track_details = await self.async_cloud_get_track_info(
|
||||||
self.current_track_id
|
self.current_track_id
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def async_get_playlist_details(self) -> dict:
|
||||||
|
"""Get playlist info."""
|
||||||
|
return await self.async_cloud_get_tracks(self.playlist)
|
||||||
|
|
||||||
|
async def _async_cloud_request(self, method: str, url: str, **kwargs: Any) -> Any:
|
||||||
|
"""Perform a cloud request."""
|
||||||
|
if not self.access_token:
|
||||||
|
return
|
||||||
|
|
||||||
|
return await self._async_request(
|
||||||
|
method,
|
||||||
|
urljoin(CLOUD_BASE_URL, url),
|
||||||
|
headers={"Authorization": f"Bearer {self.access_token}"},
|
||||||
|
**kwargs,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _async_command(self, **kwargs: Any) -> str | None:
|
||||||
|
"""Send a command to the device."""
|
||||||
|
result = await self._async_get(**kwargs)
|
||||||
|
_LOGGER.debug("Result: %s", result)
|
||||||
|
|
||||||
|
async def _async_get(self, **kwargs: Any) -> str | None:
|
||||||
|
"""Perform a GET request."""
|
||||||
|
return await self._async_request("GET", self.url, **kwargs)
|
||||||
|
|
||||||
|
async def _async_request(self, method: str, url: str, **kwargs) -> Any:
|
||||||
|
"""Perform a request."""
|
||||||
|
response = await self._session.request(method, url, **kwargs)
|
||||||
|
if response.status == 200:
|
||||||
|
if response.content_type == "application/json":
|
||||||
|
return await response.json()
|
||||||
|
if response.content_type == "text/plain":
|
||||||
|
return await response.text()
|
||||||
|
return None
|
||||||
|
response.raise_for_status()
|
||||||
|
|||||||
@@ -8,4 +8,4 @@ from typing import Final
|
|||||||
|
|
||||||
__TRACKS_FILE = os.path.join(os.path.dirname(__file__), "tracks.json")
|
__TRACKS_FILE = os.path.join(os.path.dirname(__file__), "tracks.json")
|
||||||
with open(__TRACKS_FILE, "r", encoding="utf8") as file:
|
with open(__TRACKS_FILE, "r", encoding="utf8") as file:
|
||||||
TRACKS: Final[dict[int, dict[str, str]]] = json.load(file)
|
TRACKS: Final[dict[str, dict[str, str]]] = json.load(file)
|
||||||
|
|||||||
@@ -27,15 +27,7 @@ class OasisMiniSelectEntity(OasisMiniEntity, SelectEntity):
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Construct an Oasis Mini select entity."""
|
"""Construct an Oasis Mini select entity."""
|
||||||
super().__init__(coordinator, entry, description)
|
super().__init__(coordinator, entry, description)
|
||||||
self._attr_options = [
|
self._handle_coordinator_update()
|
||||||
TRACKS.get(str(track), {}).get("name", str(track))
|
|
||||||
for track in self.device.playlist
|
|
||||||
]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def current_option(self) -> str:
|
|
||||||
"""Return the selected entity option to represent the entity state."""
|
|
||||||
return self.options[self.device.playlist_index]
|
|
||||||
|
|
||||||
async def async_select_option(self, option: str) -> None:
|
async def async_select_option(self, option: str) -> None:
|
||||||
"""Change the selected option."""
|
"""Change the selected option."""
|
||||||
@@ -44,11 +36,15 @@ class OasisMiniSelectEntity(OasisMiniEntity, SelectEntity):
|
|||||||
|
|
||||||
def _handle_coordinator_update(self) -> None:
|
def _handle_coordinator_update(self) -> None:
|
||||||
"""Handle updated data from the coordinator."""
|
"""Handle updated data from the coordinator."""
|
||||||
self._attr_options = [
|
options = [
|
||||||
TRACKS.get(str(track), {}).get("name", str(track))
|
TRACKS.get(str(track), {}).get("name", str(track))
|
||||||
for track in self.device.playlist
|
for track in self.device.playlist
|
||||||
]
|
]
|
||||||
return super()._handle_coordinator_update()
|
self._attr_options = options
|
||||||
|
index = min(self.device.playlist_index, len(options) - 1)
|
||||||
|
self._attr_current_option = options[index]
|
||||||
|
if self.hass:
|
||||||
|
return super()._handle_coordinator_update()
|
||||||
|
|
||||||
|
|
||||||
DESCRIPTOR = SelectEntityDescription(key="playlist", name="Playlist")
|
DESCRIPTOR = SelectEntityDescription(key="playlist", name="Playlist")
|
||||||
|
|||||||
@@ -2,9 +2,6 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass
|
|
||||||
from typing import Any, Callable
|
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
@@ -18,27 +15,19 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import OasisMiniCoordinator
|
from .coordinator import OasisMiniCoordinator
|
||||||
from .entity import OasisMiniEntity
|
from .entity import OasisMiniEntity
|
||||||
from .pyoasismini import OasisMini
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
async def async_setup_entry(
|
||||||
class OasisMiniSensorEntityDescription(SensorEntityDescription):
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
"""Oasis Mini sensor entity description."""
|
) -> None:
|
||||||
|
"""Set up Oasis Mini sensors using config entry."""
|
||||||
lookup_fn: Callable[[OasisMini], Any] | None = None
|
coordinator: OasisMiniCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
async_add_entities(
|
||||||
|
[
|
||||||
class OasisMiniSensorEntity(OasisMiniEntity, SensorEntity):
|
OasisMiniSensorEntity(coordinator, entry, descriptor)
|
||||||
"""Oasis Mini sensor entity."""
|
for descriptor in DESCRIPTORS
|
||||||
|
]
|
||||||
entity_description: OasisMiniSensorEntityDescription | SensorEntityDescription
|
)
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self) -> str | None:
|
|
||||||
"""Return the value reported by the sensor."""
|
|
||||||
if lookup_fn := getattr(self.entity_description, "lookup_fn", None):
|
|
||||||
return lookup_fn(self.device)
|
|
||||||
return getattr(self.device, self.entity_description.key)
|
|
||||||
|
|
||||||
|
|
||||||
DESCRIPTORS = {
|
DESCRIPTORS = {
|
||||||
@@ -49,9 +38,7 @@ DESCRIPTORS = {
|
|||||||
name="Download progress",
|
name="Download progress",
|
||||||
state_class=SensorStateClass.TOTAL_INCREASING,
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
||||||
),
|
),
|
||||||
}
|
} | {
|
||||||
|
|
||||||
OTHERS = {
|
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
key=key,
|
key=key,
|
||||||
name=key.replace("_", " ").capitalize(),
|
name=key.replace("_", " ").capitalize(),
|
||||||
@@ -68,14 +55,10 @@ OTHERS = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
class OasisMiniSensorEntity(OasisMiniEntity, SensorEntity):
|
||||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
"""Oasis Mini sensor entity."""
|
||||||
) -> None:
|
|
||||||
"""Set up Oasis Mini sensors using config entry."""
|
@property
|
||||||
coordinator: OasisMiniCoordinator = hass.data[DOMAIN][entry.entry_id]
|
def native_value(self) -> str | None:
|
||||||
async_add_entities(
|
"""Return the value reported by the sensor."""
|
||||||
[
|
return getattr(self.device, self.entity_description.key)
|
||||||
OasisMiniSensorEntity(coordinator, entry, descriptor)
|
|
||||||
for descriptor in DESCRIPTORS | OTHERS
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|||||||
@@ -6,6 +6,11 @@
|
|||||||
"host": "[%key:common::config_flow::data::host%]"
|
"host": "[%key:common::config_flow::data::host%]"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"reconfigure": {
|
||||||
|
"data": {
|
||||||
|
"host": "[%key:common::config_flow::data::host%]"
|
||||||
|
}
|
||||||
|
},
|
||||||
"reauth_confirm": {
|
"reauth_confirm": {
|
||||||
"data": {}
|
"data": {}
|
||||||
}
|
}
|
||||||
@@ -17,7 +22,9 @@
|
|||||||
"unknown": "[%key:common::config_flow::error::unknown%]"
|
"unknown": "[%key:common::config_flow::error::unknown%]"
|
||||||
},
|
},
|
||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
|
||||||
|
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
|
||||||
|
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"options": {
|
"options": {
|
||||||
|
|||||||
@@ -6,6 +6,11 @@
|
|||||||
"host": "Host"
|
"host": "Host"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"reconfigure": {
|
||||||
|
"data": {
|
||||||
|
"host": "Host"
|
||||||
|
}
|
||||||
|
},
|
||||||
"reauth_confirm": {
|
"reauth_confirm": {
|
||||||
"data": {}
|
"data": {}
|
||||||
}
|
}
|
||||||
@@ -17,7 +22,9 @@
|
|||||||
"unknown": "Unexpected error"
|
"unknown": "Unexpected error"
|
||||||
},
|
},
|
||||||
"abort": {
|
"abort": {
|
||||||
"already_configured": "Device is already configured"
|
"already_configured": "Device is already configured",
|
||||||
|
"reauth_successful": "Re-authentication was successful",
|
||||||
|
"reconfigure_successful": "Re-configuration was successful"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"options": {
|
"options": {
|
||||||
|
|||||||
76
custom_components/oasis_mini/update.py
Normal file
76
custom_components/oasis_mini/update.py
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
"""Oasis Mini update entity."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from datetime import timedelta
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from homeassistant.components.update import (
|
||||||
|
UpdateDeviceClass,
|
||||||
|
UpdateEntity,
|
||||||
|
UpdateEntityDescription,
|
||||||
|
UpdateEntityFeature,
|
||||||
|
)
|
||||||
|
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 OasisMiniCoordinator
|
||||||
|
from .entity import OasisMiniEntity
|
||||||
|
|
||||||
|
SCAN_INTERVAL = timedelta(hours=6)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
|
) -> None:
|
||||||
|
"""Set up Oasis Mini updates using config entry."""
|
||||||
|
coordinator: OasisMiniCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
if coordinator.device.access_token:
|
||||||
|
async_add_entities(
|
||||||
|
[OasisMiniUpdateEntity(coordinator, entry, DESCRIPTOR)], True
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
DESCRIPTOR = UpdateEntityDescription(
|
||||||
|
key="software", device_class=UpdateDeviceClass.FIRMWARE
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class OasisMiniUpdateEntity(OasisMiniEntity, UpdateEntity):
|
||||||
|
"""Oasis Mini update entity."""
|
||||||
|
|
||||||
|
_attr_supported_features = (
|
||||||
|
UpdateEntityFeature.INSTALL | UpdateEntityFeature.PROGRESS
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def in_progress(self) -> bool | int:
|
||||||
|
"""Update installation progress."""
|
||||||
|
if self.device.status_code == 11:
|
||||||
|
return self.device.download_progress
|
||||||
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def installed_version(self) -> str:
|
||||||
|
"""Version installed and in use."""
|
||||||
|
return self.device.software_version
|
||||||
|
|
||||||
|
@property
|
||||||
|
def should_poll(self) -> bool:
|
||||||
|
"""Set polling to True."""
|
||||||
|
return True
|
||||||
|
|
||||||
|
async def async_install(
|
||||||
|
self, version: str | None, backup: bool, **kwargs: Any
|
||||||
|
) -> None:
|
||||||
|
"""Install an update."""
|
||||||
|
await self.device.async_upgrade()
|
||||||
|
|
||||||
|
async def async_update(self) -> None:
|
||||||
|
"""Update the entity."""
|
||||||
|
software = await self.device.async_cloud_get_latest_software_details()
|
||||||
|
self._attr_latest_version = software["version"]
|
||||||
|
self._attr_release_summary = software["description"]
|
||||||
|
self._attr_release_url = f"https://app.grounded.so/software/{software['id']}"
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "Oasis Mini",
|
"name": "Oasis Mini",
|
||||||
"homeassistant": "2024.7.0",
|
"homeassistant": "2024.4.0",
|
||||||
"render_readme": true,
|
"render_readme": true,
|
||||||
"zip_release": true,
|
"zip_release": true,
|
||||||
"filename": "oasis_mini.zip"
|
"filename": "oasis_mini.zip"
|
||||||
|
|||||||
@@ -4,3 +4,10 @@ known-first-party = ["homeassistant", "tests"]
|
|||||||
forced-separate = ["tests"]
|
forced-separate = ["tests"]
|
||||||
combine-as-imports = true
|
combine-as-imports = true
|
||||||
split-on-trailing-comma = false
|
split-on-trailing-comma = false
|
||||||
|
|
||||||
|
[tool.pylint."MESSAGES CONTROL"]
|
||||||
|
# abstract-method - with intro of async there are always methods missing
|
||||||
|
disable = [
|
||||||
|
"abstract-method",
|
||||||
|
"unexpected-keyword-arg",
|
||||||
|
]
|
||||||
Reference in New Issue
Block a user