Use properties instead of raw data in the rituals integration (#52587)

This commit is contained in:
Milan Meulemans 2021-07-12 20:40:16 +02:00 committed by GitHub
parent 3e09787d85
commit 0a3aab935a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 26 additions and 45 deletions

View file

@ -11,7 +11,7 @@ from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import ACCOUNT_HASH, COORDINATORS, DEVICES, DOMAIN, HUBLOT
from .const import ACCOUNT_HASH, COORDINATORS, DEVICES, DOMAIN
PLATFORMS = ["binary_sensor", "number", "select", "sensor", "switch"]
@ -23,8 +23,7 @@ UPDATE_INTERVAL = timedelta(seconds=30)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Rituals Perfume Genie from a config entry."""
session = async_get_clientsession(hass)
account = Account(session=session)
account.data = {ACCOUNT_HASH: entry.data.get(ACCOUNT_HASH)}
account = Account(session=session, account_hash=entry.data[ACCOUNT_HASH])
try:
account_devices = await account.get_devices()
@ -37,7 +36,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
}
for device in account_devices:
hublot = device.hub_data[HUBLOT]
hublot = device.hublot
coordinator = RitualsDataUpdateCoordinator(hass, device)
await coordinator.async_refresh()
@ -68,7 +67,7 @@ class RitualsDataUpdateCoordinator(DataUpdateCoordinator):
super().__init__(
hass,
_LOGGER,
name=f"{DOMAIN}-{device.hub_data[HUBLOT]}",
name=f"{DOMAIN}-{device.hublot}",
update_interval=UPDATE_INTERVAL,
)

View file

@ -47,12 +47,12 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
else:
await self.async_set_unique_id(account.data[CONF_EMAIL])
await self.async_set_unique_id(account.email)
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=account.data[CONF_EMAIL],
data={ACCOUNT_HASH: account.data[ACCOUNT_HASH]},
title=account.email,
data={ACCOUNT_HASH: account.account_hash},
)
return self.async_show_form(

View file

@ -1,9 +1,7 @@
"""Constants for the Rituals Perfume Genie integration."""
DOMAIN = "rituals_perfume_genie"
ACCOUNT_HASH = "account_hash"
COORDINATORS = "coordinators"
DEVICES = "devices"
ACCOUNT_HASH = "account_hash"
HUBLOT = "hublot"
SENSORS = "sensors"

View file

@ -6,19 +6,12 @@ from pyrituals import Diffuser
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import RitualsDataUpdateCoordinator
from .const import DOMAIN, HUBLOT, SENSORS
from .const import DOMAIN
MANUFACTURER = "Rituals Cosmetics"
MODEL = "The Perfume Genie"
MODEL2 = "The Perfume Genie 2.0"
ATTRIBUTES = "attributes"
ROOMNAME = "roomnamec"
STATUS = "status"
VERSION = "versionc"
AVAILABLE_STATE = 1
class DiffuserEntity(CoordinatorEntity):
"""Representation of a diffuser entity."""
@ -35,8 +28,8 @@ class DiffuserEntity(CoordinatorEntity):
super().__init__(coordinator)
self._diffuser = diffuser
hublot = self._diffuser.hub_data[HUBLOT]
hubname = self._diffuser.hub_data[ATTRIBUTES][ROOMNAME]
hublot = self._diffuser.hublot
hubname = self._diffuser.name
self._attr_name = f"{hubname}{entity_suffix}"
self._attr_unique_id = f"{hublot}{entity_suffix}"
@ -45,10 +38,10 @@ class DiffuserEntity(CoordinatorEntity):
"identifiers": {(DOMAIN, hublot)},
"manufacturer": MANUFACTURER,
"model": MODEL if diffuser.has_battery else MODEL2,
"sw_version": diffuser.hub_data[SENSORS][VERSION],
"sw_version": diffuser.version,
}
@property
def available(self) -> bool:
"""Return if the entity is available."""
return super().available and self._diffuser.hub_data[STATUS] == AVAILABLE_STATE
return super().available and self._diffuser.is_online

View file

@ -3,7 +3,7 @@
"name": "Rituals Perfume Genie",
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/rituals_perfume_genie",
"requirements": ["pyrituals==0.0.4"],
"requirements": ["pyrituals==0.0.5"],
"codeowners": ["@milanmeu"],
"iot_class": "cloud_polling"
}

View file

@ -13,16 +13,9 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import RitualsDataUpdateCoordinator
from .const import COORDINATORS, DEVICES, DOMAIN, SENSORS
from .const import COORDINATORS, DEVICES, DOMAIN
from .entity import DiffuserEntity
ID = "id"
PERFUME = "rfidc"
FILL = "fillc"
PERFUME_NO_CARTRIDGE_ID = 19
FILL_NO_CARTRIDGE_ID = 12
BATTERY_SUFFIX = " Battery"
PERFUME_SUFFIX = " Perfume"
FILL_SUFFIX = " Fill"
@ -58,9 +51,9 @@ class DiffuserPerfumeSensor(DiffuserEntity):
"""Initialize the perfume sensor."""
super().__init__(diffuser, coordinator, PERFUME_SUFFIX)
self._attr_icon = "mdi:tag-text"
if diffuser.hub_data[SENSORS][PERFUME][ID] == PERFUME_NO_CARTRIDGE_ID:
self._attr_icon = "mdi:tag-remove"
self._attr_icon = "mdi:tag-remove"
if diffuser.has_cartridge:
self._attr_icon = "mdi:tag-text"
@property
def state(self) -> str:
@ -77,12 +70,9 @@ class DiffuserFillSensor(DiffuserEntity):
"""Initialize the fill sensor."""
super().__init__(diffuser, coordinator, FILL_SUFFIX)
@property
def icon(self) -> str:
"""Return the fill sensor icon."""
if self._diffuser.hub_data[SENSORS][FILL][ID] == FILL_NO_CARTRIDGE_ID:
return "mdi:beaker-question"
return "mdi:beaker"
self._attr_icon = "mdi:beaker-question"
if diffuser.has_cartridge:
self.attr_icon = "mdi:beaker"
@property
def state(self) -> str:

View file

@ -1706,7 +1706,7 @@ pyrepetier==3.0.5
pyrisco==0.3.1
# homeassistant.components.rituals_perfume_genie
pyrituals==0.0.4
pyrituals==0.0.5
# homeassistant.components.ruckus_unleashed
pyruckus==0.12

View file

@ -969,7 +969,7 @@ pyqwikswitch==0.93
pyrisco==0.3.1
# homeassistant.components.rituals_perfume_genie
pyrituals==0.0.4
pyrituals==0.0.5
# homeassistant.components.ruckus_unleashed
pyruckus==0.12

View file

@ -16,7 +16,8 @@ WRONG_PASSWORD = "wrong-passw0rd"
def _mock_account(*_):
account = MagicMock()
account.authenticate = AsyncMock()
account.data = {CONF_EMAIL: TEST_EMAIL, ACCOUNT_HASH: "any"}
account.account_hash = "any"
account.email = TEST_EMAIL
return account