From d37018cf87d12192cdb1d991c6b46aa52db925f5 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Tue, 29 Jun 2021 08:39:00 +0200 Subject: [PATCH] Small tweaks to Rituals Perfume Genie (#52269) --- .../rituals_perfume_genie/__init__.py | 4 +-- .../rituals_perfume_genie/binary_sensor.py | 18 ++++------ .../rituals_perfume_genie/entity.py | 34 ++++++------------ .../rituals_perfume_genie/number.py | 19 +++------- .../rituals_perfume_genie/sensor.py | 35 +++++-------------- .../rituals_perfume_genie/switch.py | 27 ++++---------- 6 files changed, 40 insertions(+), 97 deletions(-) diff --git a/homeassistant/components/rituals_perfume_genie/__init__.py b/homeassistant/components/rituals_perfume_genie/__init__.py index 114c7f6d400..84fc5ed2cf5 100644 --- a/homeassistant/components/rituals_perfume_genie/__init__.py +++ b/homeassistant/components/rituals_perfume_genie/__init__.py @@ -60,10 +60,10 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): 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: - """Initialize global Rituals Perufme Genie data updater.""" + """Initialize global Rituals Perfume Genie data updater.""" self._device = device super().__init__( hass, diff --git a/homeassistant/components/rituals_perfume_genie/binary_sensor.py b/homeassistant/components/rituals_perfume_genie/binary_sensor.py index 8446d899480..a529ff3dca6 100644 --- a/homeassistant/components/rituals_perfume_genie/binary_sensor.py +++ b/homeassistant/components/rituals_perfume_genie/binary_sensor.py @@ -26,18 +26,19 @@ async def async_setup_entry( """Set up the diffuser binary sensors.""" diffusers = hass.data[DOMAIN][config_entry.entry_id][DEVICES] 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): """Representation of a diffuser battery charging binary sensor.""" + _attr_device_class = DEVICE_CLASS_BATTERY_CHARGING + def __init__( self, diffuser: Diffuser, coordinator: RitualsDataUpdateCoordinator ) -> None: @@ -48,8 +49,3 @@ class DiffuserBatteryChargingBinarySensor(DiffuserEntity, BinarySensorEntity): def is_on(self) -> bool: """Return the state of the battery charging binary sensor.""" 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 diff --git a/homeassistant/components/rituals_perfume_genie/entity.py b/homeassistant/components/rituals_perfume_genie/entity.py index d03450609e7..19c3f3cd424 100644 --- a/homeassistant/components/rituals_perfume_genie/entity.py +++ b/homeassistant/components/rituals_perfume_genie/entity.py @@ -3,7 +3,6 @@ from __future__ import annotations from pyrituals import Diffuser -from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.update_coordinator import CoordinatorEntity from . import RitualsDataUpdateCoordinator @@ -35,32 +34,21 @@ class DiffuserEntity(CoordinatorEntity): """Init from config, hookup diffuser and coordinator.""" super().__init__(coordinator) self._diffuser = diffuser - self._entity_suffix = entity_suffix - self._hublot = self._diffuser.hub_data[HUBLOT] - self._hubname = self._diffuser.hub_data[ATTRIBUTES][ROOMNAME] - @property - def unique_id(self) -> str: - """Return the unique ID of the entity.""" - return f"{self._hublot}{self._entity_suffix}" + hublot = self._diffuser.hub_data[HUBLOT] + hubname = self._diffuser.hub_data[ATTRIBUTES][ROOMNAME] - @property - def name(self) -> str: - """Return the name of the entity.""" - return f"{self._hubname}{self._entity_suffix}" + self._attr_name = f"{hubname}{entity_suffix}" + self._attr_unique_id = f"{hublot}{entity_suffix}" + self._attr_device_info = { + "name": hubname, + "identifiers": {(DOMAIN, hublot)}, + "manufacturer": MANUFACTURER, + "model": MODEL if diffuser.has_battery else MODEL2, + "sw_version": diffuser.hub_data[SENSORS][VERSION], + } @property def available(self) -> bool: """Return if the entity is available.""" 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], - } diff --git a/homeassistant/components/rituals_perfume_genie/number.py b/homeassistant/components/rituals_perfume_genie/number.py index bc9ac7c9f31..26ae393b071 100644 --- a/homeassistant/components/rituals_perfume_genie/number.py +++ b/homeassistant/components/rituals_perfume_genie/number.py @@ -37,32 +37,21 @@ async def async_setup_entry( class DiffuserPerfumeAmount(DiffuserEntity, NumberEntity): """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__( self, diffuser: Diffuser, coordinator: RitualsDataUpdateCoordinator ) -> None: """Initialize the diffuser perfume amount number.""" super().__init__(diffuser, coordinator, PERFUME_AMOUNT_SUFFIX) - @property - def icon(self) -> str: - """Return the icon of the perfume amount entity.""" - return "mdi:gauge" - @property def value(self) -> int: """Return the current 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: """Set the perfume amount.""" if value.is_integer() and MIN_PERFUME_AMOUNT <= value <= MAX_PERFUME_AMOUNT: diff --git a/homeassistant/components/rituals_perfume_genie/sensor.py b/homeassistant/components/rituals_perfume_genie/sensor.py index 110a58fcd13..2965371733b 100644 --- a/homeassistant/components/rituals_perfume_genie/sensor.py +++ b/homeassistant/components/rituals_perfume_genie/sensor.py @@ -58,12 +58,9 @@ class DiffuserPerfumeSensor(DiffuserEntity): """Initialize the perfume sensor.""" super().__init__(diffuser, coordinator, PERFUME_SUFFIX) - @property - def icon(self) -> str: - """Return the perfume sensor icon.""" - if self._diffuser.hub_data[SENSORS][PERFUME][ID] == PERFUME_NO_CARTRIDGE_ID: - return "mdi:tag-remove" - return "mdi:tag-text" + self._attr_icon = "mdi:tag-text" + if diffuser.hub_data[SENSORS][PERFUME][ID] == PERFUME_NO_CARTRIDGE_ID: + self._attr_icon = "mdi:tag-remove" @property def state(self) -> str: @@ -96,6 +93,9 @@ class DiffuserFillSensor(DiffuserEntity): class DiffuserBatterySensor(DiffuserEntity): """Representation of a diffuser battery sensor.""" + _attr_device_class = DEVICE_CLASS_BATTERY + _attr_unit_of_measurement = PERCENTAGE + def __init__( self, diffuser: Diffuser, coordinator: RitualsDataUpdateCoordinator ) -> None: @@ -107,20 +107,13 @@ class DiffuserBatterySensor(DiffuserEntity): """Return the state of the battery sensor.""" 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): """Representation of a diffuser wifi sensor.""" + _attr_device_class = DEVICE_CLASS_SIGNAL_STRENGTH + _attr_unit_of_measurement = PERCENTAGE + def __init__( self, diffuser: Diffuser, coordinator: RitualsDataUpdateCoordinator ) -> None: @@ -131,13 +124,3 @@ class DiffuserWifiSensor(DiffuserEntity): def state(self) -> int: """Return the state of the wifi sensor.""" 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 diff --git a/homeassistant/components/rituals_perfume_genie/switch.py b/homeassistant/components/rituals_perfume_genie/switch.py index 364b29ef1fb..924a38dfde8 100644 --- a/homeassistant/components/rituals_perfume_genie/switch.py +++ b/homeassistant/components/rituals_perfume_genie/switch.py @@ -14,10 +14,6 @@ from . import RitualsDataUpdateCoordinator from .const import COORDINATORS, DEVICES, DOMAIN from .entity import DiffuserEntity -FAN = "fanc" - -ON_STATE = "1" - async def async_setup_entry( hass: HomeAssistant, @@ -38,46 +34,37 @@ async def async_setup_entry( class DiffuserSwitch(SwitchEntity, DiffuserEntity): """Representation of a diffuser switch.""" + _attr_icon = "mdi:fan" + def __init__( self, diffuser: Diffuser, coordinator: RitualsDataUpdateCoordinator ) -> None: """Initialize the diffuser switch.""" super().__init__(diffuser, coordinator, "") - self._is_on = self._diffuser.is_on - - @property - def icon(self) -> str: - """Return the icon of the device.""" - return "mdi:fan" + self._attr_is_on = self._diffuser.is_on @property def extra_state_attributes(self) -> dict[str, Any]: """Return the device state attributes.""" - attributes = { + return { "fan_speed": self._diffuser.perfume_amount, "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: """Turn the device on.""" await self._diffuser.turn_on() - self._is_on = True + self._attr_is_on = True self.async_write_ha_state() async def async_turn_off(self, **kwargs: Any) -> None: """Turn the device off.""" await self._diffuser.turn_off() - self._is_on = False + self._attr_is_on = False self.async_write_ha_state() @callback def _handle_coordinator_update(self) -> None: """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()