Small tweaks to Rituals Perfume Genie (#52269)

This commit is contained in:
Franck Nijhof 2021-06-29 08:39:00 +02:00 committed by GitHub
parent 94a0259743
commit d37018cf87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 97 deletions

View File

@ -60,10 +60,10 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
class RitualsDataUpdateCoordinator(DataUpdateCoordinator): class RitualsDataUpdateCoordinator(DataUpdateCoordinator):
"""Class to manage fetching Rituals Perufme Genie device data from single endpoint.""" """Class to manage fetching Rituals Perfume Genie device data from single endpoint."""
def __init__(self, hass: HomeAssistant, device: Diffuser) -> None: def __init__(self, hass: HomeAssistant, device: Diffuser) -> None:
"""Initialize global Rituals Perufme Genie data updater.""" """Initialize global Rituals Perfume Genie data updater."""
self._device = device self._device = device
super().__init__( super().__init__(
hass, hass,

View File

@ -26,18 +26,19 @@ async def async_setup_entry(
"""Set up the diffuser binary sensors.""" """Set up the diffuser binary sensors."""
diffusers = hass.data[DOMAIN][config_entry.entry_id][DEVICES] diffusers = hass.data[DOMAIN][config_entry.entry_id][DEVICES]
coordinators = hass.data[DOMAIN][config_entry.entry_id][COORDINATORS] coordinators = hass.data[DOMAIN][config_entry.entry_id][COORDINATORS]
entities = []
for hublot, diffuser in diffusers.items():
if diffuser.has_battery:
coordinator = coordinators[hublot]
entities.append(DiffuserBatteryChargingBinarySensor(diffuser, coordinator))
async_add_entities(entities) async_add_entities(
DiffuserBatteryChargingBinarySensor(diffuser, coordinators[hublot])
for hublot, diffuser in diffusers.items()
if diffuser.has_battery
)
class DiffuserBatteryChargingBinarySensor(DiffuserEntity, BinarySensorEntity): class DiffuserBatteryChargingBinarySensor(DiffuserEntity, BinarySensorEntity):
"""Representation of a diffuser battery charging binary sensor.""" """Representation of a diffuser battery charging binary sensor."""
_attr_device_class = DEVICE_CLASS_BATTERY_CHARGING
def __init__( def __init__(
self, diffuser: Diffuser, coordinator: RitualsDataUpdateCoordinator self, diffuser: Diffuser, coordinator: RitualsDataUpdateCoordinator
) -> None: ) -> None:
@ -48,8 +49,3 @@ class DiffuserBatteryChargingBinarySensor(DiffuserEntity, BinarySensorEntity):
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return the state of the battery charging binary sensor.""" """Return the state of the battery charging binary sensor."""
return self._diffuser.charging return self._diffuser.charging
@property
def device_class(self) -> str:
"""Return the device class of the battery charging binary sensor."""
return DEVICE_CLASS_BATTERY_CHARGING

View File

@ -3,7 +3,6 @@ from __future__ import annotations
from pyrituals import Diffuser from pyrituals import Diffuser
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import RitualsDataUpdateCoordinator from . import RitualsDataUpdateCoordinator
@ -35,32 +34,21 @@ class DiffuserEntity(CoordinatorEntity):
"""Init from config, hookup diffuser and coordinator.""" """Init from config, hookup diffuser and coordinator."""
super().__init__(coordinator) super().__init__(coordinator)
self._diffuser = diffuser self._diffuser = diffuser
self._entity_suffix = entity_suffix
self._hublot = self._diffuser.hub_data[HUBLOT]
self._hubname = self._diffuser.hub_data[ATTRIBUTES][ROOMNAME]
@property hublot = self._diffuser.hub_data[HUBLOT]
def unique_id(self) -> str: hubname = self._diffuser.hub_data[ATTRIBUTES][ROOMNAME]
"""Return the unique ID of the entity."""
return f"{self._hublot}{self._entity_suffix}"
@property self._attr_name = f"{hubname}{entity_suffix}"
def name(self) -> str: self._attr_unique_id = f"{hublot}{entity_suffix}"
"""Return the name of the entity.""" self._attr_device_info = {
return f"{self._hubname}{self._entity_suffix}" "name": hubname,
"identifiers": {(DOMAIN, hublot)},
"manufacturer": MANUFACTURER,
"model": MODEL if diffuser.has_battery else MODEL2,
"sw_version": diffuser.hub_data[SENSORS][VERSION],
}
@property @property
def available(self) -> bool: def available(self) -> bool:
"""Return if the entity is available.""" """Return if the entity is available."""
return super().available and self._diffuser.hub_data[STATUS] == AVAILABLE_STATE return super().available and self._diffuser.hub_data[STATUS] == AVAILABLE_STATE
@property
def device_info(self) -> DeviceInfo:
"""Return information about the device."""
return {
"name": self._hubname,
"identifiers": {(DOMAIN, self._hublot)},
"manufacturer": MANUFACTURER,
"model": MODEL if self._diffuser.has_battery else MODEL2,
"sw_version": self._diffuser.hub_data[SENSORS][VERSION],
}

View File

@ -37,32 +37,21 @@ async def async_setup_entry(
class DiffuserPerfumeAmount(DiffuserEntity, NumberEntity): class DiffuserPerfumeAmount(DiffuserEntity, NumberEntity):
"""Representation of a diffuser perfume amount number.""" """Representation of a diffuser perfume amount number."""
_attr_icon = "mdi:gauge"
_attr_max_value = MAX_PERFUME_AMOUNT
_attr_min_value = MIN_PERFUME_AMOUNT
def __init__( def __init__(
self, diffuser: Diffuser, coordinator: RitualsDataUpdateCoordinator self, diffuser: Diffuser, coordinator: RitualsDataUpdateCoordinator
) -> None: ) -> None:
"""Initialize the diffuser perfume amount number.""" """Initialize the diffuser perfume amount number."""
super().__init__(diffuser, coordinator, PERFUME_AMOUNT_SUFFIX) super().__init__(diffuser, coordinator, PERFUME_AMOUNT_SUFFIX)
@property
def icon(self) -> str:
"""Return the icon of the perfume amount entity."""
return "mdi:gauge"
@property @property
def value(self) -> int: def value(self) -> int:
"""Return the current perfume amount.""" """Return the current perfume amount."""
return self._diffuser.perfume_amount return self._diffuser.perfume_amount
@property
def min_value(self) -> int:
"""Return the minimum perfume amount."""
return MIN_PERFUME_AMOUNT
@property
def max_value(self) -> int:
"""Return the maximum perfume amount."""
return MAX_PERFUME_AMOUNT
async def async_set_value(self, value: float) -> None: async def async_set_value(self, value: float) -> None:
"""Set the perfume amount.""" """Set the perfume amount."""
if value.is_integer() and MIN_PERFUME_AMOUNT <= value <= MAX_PERFUME_AMOUNT: if value.is_integer() and MIN_PERFUME_AMOUNT <= value <= MAX_PERFUME_AMOUNT:

View File

@ -58,12 +58,9 @@ class DiffuserPerfumeSensor(DiffuserEntity):
"""Initialize the perfume sensor.""" """Initialize the perfume sensor."""
super().__init__(diffuser, coordinator, PERFUME_SUFFIX) super().__init__(diffuser, coordinator, PERFUME_SUFFIX)
@property self._attr_icon = "mdi:tag-text"
def icon(self) -> str: if diffuser.hub_data[SENSORS][PERFUME][ID] == PERFUME_NO_CARTRIDGE_ID:
"""Return the perfume sensor icon.""" self._attr_icon = "mdi:tag-remove"
if self._diffuser.hub_data[SENSORS][PERFUME][ID] == PERFUME_NO_CARTRIDGE_ID:
return "mdi:tag-remove"
return "mdi:tag-text"
@property @property
def state(self) -> str: def state(self) -> str:
@ -96,6 +93,9 @@ class DiffuserFillSensor(DiffuserEntity):
class DiffuserBatterySensor(DiffuserEntity): class DiffuserBatterySensor(DiffuserEntity):
"""Representation of a diffuser battery sensor.""" """Representation of a diffuser battery sensor."""
_attr_device_class = DEVICE_CLASS_BATTERY
_attr_unit_of_measurement = PERCENTAGE
def __init__( def __init__(
self, diffuser: Diffuser, coordinator: RitualsDataUpdateCoordinator self, diffuser: Diffuser, coordinator: RitualsDataUpdateCoordinator
) -> None: ) -> None:
@ -107,20 +107,13 @@ class DiffuserBatterySensor(DiffuserEntity):
"""Return the state of the battery sensor.""" """Return the state of the battery sensor."""
return self._diffuser.battery_percentage return self._diffuser.battery_percentage
@property
def device_class(self) -> str:
"""Return the class of the battery sensor."""
return DEVICE_CLASS_BATTERY
@property
def unit_of_measurement(self) -> str:
"""Return the battery unit of measurement."""
return PERCENTAGE
class DiffuserWifiSensor(DiffuserEntity): class DiffuserWifiSensor(DiffuserEntity):
"""Representation of a diffuser wifi sensor.""" """Representation of a diffuser wifi sensor."""
_attr_device_class = DEVICE_CLASS_SIGNAL_STRENGTH
_attr_unit_of_measurement = PERCENTAGE
def __init__( def __init__(
self, diffuser: Diffuser, coordinator: RitualsDataUpdateCoordinator self, diffuser: Diffuser, coordinator: RitualsDataUpdateCoordinator
) -> None: ) -> None:
@ -131,13 +124,3 @@ class DiffuserWifiSensor(DiffuserEntity):
def state(self) -> int: def state(self) -> int:
"""Return the state of the wifi sensor.""" """Return the state of the wifi sensor."""
return self._diffuser.wifi_percentage return self._diffuser.wifi_percentage
@property
def device_class(self) -> str:
"""Return the class of the wifi sensor."""
return DEVICE_CLASS_SIGNAL_STRENGTH
@property
def unit_of_measurement(self) -> str:
"""Return the wifi unit of measurement."""
return PERCENTAGE

View File

@ -14,10 +14,6 @@ from . import RitualsDataUpdateCoordinator
from .const import COORDINATORS, DEVICES, DOMAIN from .const import COORDINATORS, DEVICES, DOMAIN
from .entity import DiffuserEntity from .entity import DiffuserEntity
FAN = "fanc"
ON_STATE = "1"
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
@ -38,46 +34,37 @@ async def async_setup_entry(
class DiffuserSwitch(SwitchEntity, DiffuserEntity): class DiffuserSwitch(SwitchEntity, DiffuserEntity):
"""Representation of a diffuser switch.""" """Representation of a diffuser switch."""
_attr_icon = "mdi:fan"
def __init__( def __init__(
self, diffuser: Diffuser, coordinator: RitualsDataUpdateCoordinator self, diffuser: Diffuser, coordinator: RitualsDataUpdateCoordinator
) -> None: ) -> None:
"""Initialize the diffuser switch.""" """Initialize the diffuser switch."""
super().__init__(diffuser, coordinator, "") super().__init__(diffuser, coordinator, "")
self._is_on = self._diffuser.is_on self._attr_is_on = self._diffuser.is_on
@property
def icon(self) -> str:
"""Return the icon of the device."""
return "mdi:fan"
@property @property
def extra_state_attributes(self) -> dict[str, Any]: def extra_state_attributes(self) -> dict[str, Any]:
"""Return the device state attributes.""" """Return the device state attributes."""
attributes = { return {
"fan_speed": self._diffuser.perfume_amount, "fan_speed": self._diffuser.perfume_amount,
"room_size": self._diffuser.room_size, "room_size": self._diffuser.room_size,
} }
return attributes
@property
def is_on(self) -> bool:
"""If the device is currently on or off."""
return self._is_on
async def async_turn_on(self, **kwargs: Any) -> None: async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the device on.""" """Turn the device on."""
await self._diffuser.turn_on() await self._diffuser.turn_on()
self._is_on = True self._attr_is_on = True
self.async_write_ha_state() self.async_write_ha_state()
async def async_turn_off(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the device off.""" """Turn the device off."""
await self._diffuser.turn_off() await self._diffuser.turn_off()
self._is_on = False self._attr_is_on = False
self.async_write_ha_state() self.async_write_ha_state()
@callback @callback
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._is_on = self._diffuser.is_on self._attr_is_on = self._diffuser.is_on
self.async_write_ha_state() self.async_write_ha_state()