Change DiffuserRoomSize number entity to select entity (#51993)

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
Milan Meulemans 2021-06-28 20:20:32 +02:00 committed by GitHub
parent a1c741a46d
commit 6f41168616
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 13 deletions

View File

@ -847,6 +847,7 @@ omit =
homeassistant/components/rituals_perfume_genie/binary_sensor.py
homeassistant/components/rituals_perfume_genie/entity.py
homeassistant/components/rituals_perfume_genie/number.py
homeassistant/components/rituals_perfume_genie/select.py
homeassistant/components/rituals_perfume_genie/sensor.py
homeassistant/components/rituals_perfume_genie/switch.py
homeassistant/components/rituals_perfume_genie/__init__.py

View File

@ -13,7 +13,7 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import ACCOUNT_HASH, COORDINATORS, DEVICES, DOMAIN, HUBLOT
PLATFORMS = ["binary_sensor", "number", "sensor", "switch"]
PLATFORMS = ["binary_sensor", "number", "select", "sensor", "switch"]
EMPTY_CREDENTIALS = ""

View File

@ -1,8 +1,6 @@
"""Support for Rituals Perfume Genie numbers."""
from __future__ import annotations
import logging
from pyrituals import Diffuser
from homeassistant.components.number import NumberEntity
@ -14,12 +12,8 @@ from . import RitualsDataUpdateCoordinator
from .const import ATTRIBUTES, COORDINATORS, DEVICES, DOMAIN, SPEED
from .entity import DiffuserEntity
_LOGGER = logging.getLogger(__name__)
MIN_PERFUME_AMOUNT = 1
MAX_PERFUME_AMOUNT = 3
MIN_ROOM_SIZE = 1
MAX_ROOM_SIZE = 4
PERFUME_AMOUNT_SUFFIX = " Perfume Amount"
@ -40,7 +34,7 @@ async def async_setup_entry(
async_add_entities(entities)
class DiffuserPerfumeAmount(NumberEntity, DiffuserEntity):
class DiffuserPerfumeAmount(DiffuserEntity, NumberEntity):
"""Representation of a diffuser perfume amount number."""
def __init__(
@ -74,9 +68,7 @@ class DiffuserPerfumeAmount(NumberEntity, DiffuserEntity):
if value.is_integer() and MIN_PERFUME_AMOUNT <= value <= MAX_PERFUME_AMOUNT:
await self._diffuser.set_perfume_amount(int(value))
else:
_LOGGER.warning(
"Can't set the perfume amount to %s. Perfume amount must be an integer between %s and %s, inclusive",
value,
MIN_PERFUME_AMOUNT,
MAX_PERFUME_AMOUNT,
raise ValueError(
f"Can't set the perfume amount to {value}. "
f"Perfume amount must be an integer between {self.min_value} and {self.max_value}, inclusive"
)

View File

@ -0,0 +1,71 @@
"""Support for Rituals Perfume Genie numbers."""
from __future__ import annotations
from pyrituals import Diffuser
from homeassistant.components.select import SelectEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import AREA_SQUARE_METERS
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import RitualsDataUpdateCoordinator
from .const import ATTRIBUTES, COORDINATORS, DEVICES, DOMAIN, ROOM
from .entity import DiffuserEntity
ROOM_SIZE_SUFFIX = " Room Size"
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the diffuser select entities."""
diffusers = hass.data[DOMAIN][config_entry.entry_id][DEVICES]
coordinators = hass.data[DOMAIN][config_entry.entry_id][COORDINATORS]
async_add_entities(
DiffuserRoomSize(diffuser, coordinators[hublot])
for hublot, diffuser in diffusers.items()
)
class DiffuserRoomSize(DiffuserEntity, SelectEntity):
"""Representation of a diffuser room size select entity."""
_attr_icon = "mdi:ruler-square"
_attr_unit_of_measurement = AREA_SQUARE_METERS
_attr_options = ["15", "30", "60", "100"]
def __init__(
self, diffuser: Diffuser, coordinator: RitualsDataUpdateCoordinator
) -> None:
"""Initialize the diffuser room size select entity."""
super().__init__(diffuser, coordinator, ROOM_SIZE_SUFFIX)
self._attr_entity_registry_enabled_default = diffuser.has_battery
@property
def current_option(self) -> str:
"""Return the diffuser room size."""
return {
"1": "15",
"2": "30",
"3": "60",
"4": "100",
}[self._diffuser.hub_data[ATTRIBUTES][ROOM]]
async def async_select_option(self, option: str) -> None:
"""Change the diffuser room size."""
if option in self.options:
await self._diffuser.set_room_size(
{
"15": 1,
"30": 2,
"60": 3,
"100": 4,
}[option]
)
else:
raise ValueError(
f"Can't set the room size to {option}. Allowed room sizes are: {self.options}"
)