Skip to content

Commit

Permalink
Add select platform to IronOS (home-assistant#132218)
Browse files Browse the repository at this point in the history
  • Loading branch information
tr4nt0r authored Dec 18, 2024
1 parent 53ef96c commit 70ad4ee
Show file tree
Hide file tree
Showing 7 changed files with 963 additions and 4 deletions.
1 change: 1 addition & 0 deletions homeassistant/components/iron_os/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
PLATFORMS: list[Platform] = [
Platform.BINARY_SENSOR,
Platform.NUMBER,
Platform.SELECT,
Platform.SENSOR,
Platform.UPDATE,
]
Expand Down
32 changes: 29 additions & 3 deletions homeassistant/components/iron_os/icons.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,39 @@
"min_voltage_per_cell": {
"default": "mdi:fuel-cell"
},
"min_dc_voltage_cells": {
"default": "mdi:battery-arrow-down"
},
"power_limit": {
"default": "mdi:flash-alert"
}
},
"select": {
"locking_mode": {
"default": "mdi:download-lock"
},
"orientation_mode": {
"default": "mdi:screen-rotation"
},
"autostart_mode": {
"default": "mdi:power-standby"
},
"animation_speed": {
"default": "mdi:image-refresh"
},
"min_dc_voltage_cells": {
"default": "mdi:fuel-cell"
},
"temp_unit": {
"default": "mdi:temperature-celsius",
"state": {
"fahrenheit": "mdi:temperature-fahrenheit"
}
},
"desc_scroll_speed": {
"default": "mdi:message-text-fast"
},
"logo_duration": {
"default": "mdi:clock-digital"
}
},
"sensor": {
"live_temperature": {
"default": "mdi:soldering-iron"
Expand Down
208 changes: 208 additions & 0 deletions homeassistant/components/iron_os/select.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
"""Select platform for IronOS integration."""

from __future__ import annotations

from collections.abc import Callable
from dataclasses import dataclass
from enum import Enum, StrEnum
from typing import Any

from pynecil import (
AnimationSpeed,
AutostartMode,
BatteryType,
CharSetting,
CommunicationError,
LockingMode,
LogoDuration,
ScreenOrientationMode,
ScrollSpeed,
SettingsDataResponse,
TempUnit,
)

from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ServiceValidationError
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import IronOSConfigEntry
from .const import DOMAIN
from .coordinator import IronOSCoordinators
from .entity import IronOSBaseEntity

PARALLEL_UPDATES = 0


@dataclass(frozen=True, kw_only=True)
class IronOSSelectEntityDescription(SelectEntityDescription):
"""Describes IronOS select entity."""

value_fn: Callable[[SettingsDataResponse], str | None]
characteristic: CharSetting
raw_value_fn: Callable[[str], Any] | None = None


class PinecilSelect(StrEnum):
"""Select controls for Pinecil device."""

MIN_DC_VOLTAGE_CELLS = "min_dc_voltage_cells"
ORIENTATION_MODE = "orientation_mode"
ANIMATION_SPEED = "animation_speed"
AUTOSTART_MODE = "autostart_mode"
TEMP_UNIT = "temp_unit"
DESC_SCROLL_SPEED = "desc_scroll_speed"
LOCKING_MODE = "locking_mode"
LOGO_DURATION = "logo_duration"


def enum_to_str(enum: Enum | None) -> str | None:
"""Convert enum name to lower-case string."""
return enum.name.lower() if isinstance(enum, Enum) else None


PINECIL_SELECT_DESCRIPTIONS: tuple[IronOSSelectEntityDescription, ...] = (
IronOSSelectEntityDescription(
key=PinecilSelect.MIN_DC_VOLTAGE_CELLS,
translation_key=PinecilSelect.MIN_DC_VOLTAGE_CELLS,
characteristic=CharSetting.MIN_DC_VOLTAGE_CELLS,
value_fn=lambda x: enum_to_str(x.get("min_dc_voltage_cells")),
raw_value_fn=lambda value: BatteryType[value.upper()],
options=[x.name.lower() for x in BatteryType],
entity_category=EntityCategory.CONFIG,
entity_registry_enabled_default=False,
),
IronOSSelectEntityDescription(
key=PinecilSelect.ORIENTATION_MODE,
translation_key=PinecilSelect.ORIENTATION_MODE,
characteristic=CharSetting.ORIENTATION_MODE,
value_fn=lambda x: enum_to_str(x.get("orientation_mode")),
raw_value_fn=lambda value: ScreenOrientationMode[value.upper()],
options=[x.name.lower() for x in ScreenOrientationMode],
entity_category=EntityCategory.CONFIG,
),
IronOSSelectEntityDescription(
key=PinecilSelect.ANIMATION_SPEED,
translation_key=PinecilSelect.ANIMATION_SPEED,
characteristic=CharSetting.ANIMATION_SPEED,
value_fn=lambda x: enum_to_str(x.get("animation_speed")),
raw_value_fn=lambda value: AnimationSpeed[value.upper()],
options=[x.name.lower() for x in AnimationSpeed],
entity_category=EntityCategory.CONFIG,
entity_registry_enabled_default=False,
),
IronOSSelectEntityDescription(
key=PinecilSelect.AUTOSTART_MODE,
translation_key=PinecilSelect.AUTOSTART_MODE,
characteristic=CharSetting.AUTOSTART_MODE,
value_fn=lambda x: enum_to_str(x.get("autostart_mode")),
raw_value_fn=lambda value: AutostartMode[value.upper()],
options=[x.name.lower() for x in AutostartMode],
entity_category=EntityCategory.CONFIG,
),
IronOSSelectEntityDescription(
key=PinecilSelect.TEMP_UNIT,
translation_key=PinecilSelect.TEMP_UNIT,
characteristic=CharSetting.TEMP_UNIT,
value_fn=lambda x: enum_to_str(x.get("temp_unit")),
raw_value_fn=lambda value: TempUnit[value.upper()],
options=[x.name.lower() for x in TempUnit],
entity_category=EntityCategory.CONFIG,
entity_registry_enabled_default=False,
),
IronOSSelectEntityDescription(
key=PinecilSelect.DESC_SCROLL_SPEED,
translation_key=PinecilSelect.DESC_SCROLL_SPEED,
characteristic=CharSetting.DESC_SCROLL_SPEED,
value_fn=lambda x: enum_to_str(x.get("desc_scroll_speed")),
raw_value_fn=lambda value: ScrollSpeed[value.upper()],
options=[x.name.lower() for x in ScrollSpeed],
entity_category=EntityCategory.CONFIG,
entity_registry_enabled_default=False,
),
IronOSSelectEntityDescription(
key=PinecilSelect.LOCKING_MODE,
translation_key=PinecilSelect.LOCKING_MODE,
characteristic=CharSetting.LOCKING_MODE,
value_fn=lambda x: enum_to_str(x.get("locking_mode")),
raw_value_fn=lambda value: LockingMode[value.upper()],
options=[x.name.lower() for x in LockingMode],
entity_category=EntityCategory.CONFIG,
),
IronOSSelectEntityDescription(
key=PinecilSelect.LOGO_DURATION,
translation_key=PinecilSelect.LOGO_DURATION,
characteristic=CharSetting.LOGO_DURATION,
value_fn=lambda x: enum_to_str(x.get("logo_duration")),
raw_value_fn=lambda value: LogoDuration[value.upper()],
options=[x.name.lower() for x in LogoDuration],
entity_category=EntityCategory.CONFIG,
entity_registry_enabled_default=False,
),
)


async def async_setup_entry(
hass: HomeAssistant,
entry: IronOSConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up select entities from a config entry."""
coordinator = entry.runtime_data

async_add_entities(
IronOSSelectEntity(coordinator, description)
for description in PINECIL_SELECT_DESCRIPTIONS
)


class IronOSSelectEntity(IronOSBaseEntity, SelectEntity):
"""Implementation of a IronOS select entity."""

entity_description: IronOSSelectEntityDescription

def __init__(
self,
coordinator: IronOSCoordinators,
entity_description: IronOSSelectEntityDescription,
) -> None:
"""Initialize the select entity."""
super().__init__(
coordinator.live_data, entity_description, entity_description.characteristic
)

self.settings = coordinator.settings

@property
def current_option(self) -> str | None:
"""Return the selected entity option to represent the entity state."""

return self.entity_description.value_fn(self.settings.data)

async def async_select_option(self, option: str) -> None:
"""Change the selected option."""

if raw_value_fn := self.entity_description.raw_value_fn:
value = raw_value_fn(option)
try:
await self.coordinator.device.write(
self.entity_description.characteristic, value
)
except CommunicationError as e:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="submit_setting_failed",
) from e
await self.settings.async_request_refresh()

async def async_added_to_hass(self) -> None:
"""Run when entity about to be added to hass."""

await super().async_added_to_hass()
self.async_on_remove(
self.settings.async_add_listener(
self._handle_coordinator_update, self.entity_description.characteristic
)
)
await self.settings.async_request_refresh()
76 changes: 76 additions & 0 deletions homeassistant/components/iron_os/strings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"common": {
"slow": "Slow",
"fast": "Fast"
},
"config": {
"step": {
"user": {
Expand Down Expand Up @@ -84,6 +88,78 @@
"name": "Long-press temperature step"
}
},
"select": {
"min_dc_voltage_cells": {
"name": "Power source",
"state": {
"no_battery": "External power supply (DC)",
"battery_3s": "3S (3 cells)",
"battery_4s": "4S (4 cells)",
"battery_5s": "5S (5 cells)",
"battery_6s": "6S (6 cells)"
}
},
"orientation_mode": {
"name": "Display orientation mode",
"state": {
"right_handed": "Right-handed",
"left_handed": "Left-handed",
"auto": "Auto"
}
},
"animation_speed": {
"name": "Animation speed",
"state": {
"off": "[%key:common::state::off%]",
"slow": "[%key:component::iron_os::common::slow%]",
"medium": "Medium",
"fast": "[%key:component::iron_os::common::fast%]"
}
},
"autostart_mode": {
"name": "Start-up behavior",
"state": {
"disabled": "[%key:common::state::disabled%]",
"soldering": "Soldering mode",
"sleeping": "Sleeping mode",
"idle": "Idle mode"
}
},
"temp_unit": {
"name": "Temperature display unit",
"state": {
"celsius": "Celsius (C°)",
"fahrenheit": "Fahrenheit (F°)"
}
},
"desc_scroll_speed": {
"name": "Scrolling speed",
"state": {
"slow": "[%key:component::iron_os::common::slow%]",
"fast": "[%key:component::iron_os::common::fast%]"
}
},
"locking_mode": {
"name": "Button locking mode",
"state": {
"off": "[%key:common::state::off%]",
"boost_only": "Boost only",
"full_locking": "Full locking"
}
},
"logo_duration": {
"name": "Boot logo duration",
"state": {
"off": "[%key:common::state::off%]",
"seconds_1": "1 second",
"seconds_2": "2 second",
"seconds_3": "3 second",
"seconds_4": "4 second",
"seconds_5": "5 second",
"loop": "Loop"
}
}
},
"sensor": {
"live_temperature": {
"name": "Tip temperature"
Expand Down
17 changes: 16 additions & 1 deletion tests/components/iron_os/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@
from bleak.backends.device import BLEDevice
from habluetooth import BluetoothServiceInfoBleak
from pynecil import (
AnimationSpeed,
AutostartMode,
BatteryType,
DeviceInfoResponse,
LatestRelease,
LiveDataResponse,
LockingMode,
LogoDuration,
OperatingMode,
PowerSource,
ScreenOrientationMode,
ScrollSpeed,
SettingsDataResponse,
TempUnit,
)
import pytest

Expand Down Expand Up @@ -151,7 +159,7 @@ def mock_pynecil() -> Generator[AsyncMock]:
client.get_settings.return_value = SettingsDataResponse(
sleep_temp=150,
sleep_timeout=5,
min_dc_voltage_cells=0,
min_dc_voltage_cells=BatteryType.BATTERY_3S,
min_volltage_per_cell=3.3,
qc_ideal_voltage=9.0,
accel_sensitivity=7,
Expand All @@ -168,6 +176,13 @@ def mock_pynecil() -> Generator[AsyncMock]:
hall_sensitivity=7,
pd_negotiation_timeout=2.0,
display_brightness=3,
orientation_mode=ScreenOrientationMode.RIGHT_HANDED,
animation_speed=AnimationSpeed.MEDIUM,
autostart_mode=AutostartMode.IDLE,
temp_unit=TempUnit.CELSIUS,
desc_scroll_speed=ScrollSpeed.FAST,
logo_duration=LogoDuration.LOOP,
locking_mode=LockingMode.FULL_LOCKING,
)
client.get_live_data.return_value = LiveDataResponse(
live_temp=298,
Expand Down
Loading

0 comments on commit 70ad4ee

Please sign in to comment.