From ed54494b699818ec0953d0e8a833be2188837c12 Mon Sep 17 00:00:00 2001 From: Milan Meulemans Date: Wed, 14 Apr 2021 23:10:35 +0200 Subject: [PATCH] Add binary sensor platform to Rituals Perfume Genie Integration (#49207) * Add binary sensor platform to Rituals * Sort platforms --- .coveragerc | 1 + .../rituals_perfume_genie/__init__.py | 2 +- .../rituals_perfume_genie/binary_sensor.py | 44 +++++++++++++++++++ .../components/rituals_perfume_genie/const.py | 3 ++ .../rituals_perfume_genie/entity.py | 3 +- .../rituals_perfume_genie/sensor.py | 16 +------ 6 files changed, 52 insertions(+), 17 deletions(-) create mode 100644 homeassistant/components/rituals_perfume_genie/binary_sensor.py diff --git a/.coveragerc b/.coveragerc index 2f93792a3f6..d3eee9c9f60 100644 --- a/.coveragerc +++ b/.coveragerc @@ -826,6 +826,7 @@ omit = homeassistant/components/rest/switch.py homeassistant/components/ring/camera.py homeassistant/components/ripple/sensor.py + homeassistant/components/rituals_perfume_genie/binary_sensor.py homeassistant/components/rituals_perfume_genie/entity.py homeassistant/components/rituals_perfume_genie/sensor.py homeassistant/components/rituals_perfume_genie/switch.py diff --git a/homeassistant/components/rituals_perfume_genie/__init__.py b/homeassistant/components/rituals_perfume_genie/__init__.py index 610700e8fe5..93e5619f446 100644 --- a/homeassistant/components/rituals_perfume_genie/__init__.py +++ b/homeassistant/components/rituals_perfume_genie/__init__.py @@ -14,7 +14,7 @@ from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from .const import ACCOUNT_HASH, COORDINATORS, DEVICES, DOMAIN, HUB, HUBLOT -PLATFORMS = ["switch", "sensor"] +PLATFORMS = ["binary_sensor", "sensor", "switch"] EMPTY_CREDENTIALS = "" diff --git a/homeassistant/components/rituals_perfume_genie/binary_sensor.py b/homeassistant/components/rituals_perfume_genie/binary_sensor.py new file mode 100644 index 00000000000..39c8cb8415a --- /dev/null +++ b/homeassistant/components/rituals_perfume_genie/binary_sensor.py @@ -0,0 +1,44 @@ +"""Support for Rituals Perfume Genie binary sensors.""" +from homeassistant.components.binary_sensor import ( + DEVICE_CLASS_BATTERY_CHARGING, + BinarySensorEntity, +) + +from .const import BATTERY, COORDINATORS, DEVICES, DOMAIN, HUB, ID +from .entity import SENSORS, DiffuserEntity + +CHARGING_SUFFIX = " Battery Charging" +BATTERY_CHARGING_ID = 21 + + +async def async_setup_entry(hass, config_entry, async_add_entities): + """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 BATTERY in diffuser.data[HUB][SENSORS]: + coordinator = coordinators[hublot] + entities.append(DiffuserBatteryChargingBinarySensor(diffuser, coordinator)) + + async_add_entities(entities) + + +class DiffuserBatteryChargingBinarySensor(DiffuserEntity, BinarySensorEntity): + """Representation of a diffuser battery charging binary sensor.""" + + def __init__(self, diffuser, coordinator): + """Initialize the battery charging binary sensor.""" + super().__init__(diffuser, coordinator, CHARGING_SUFFIX) + + @property + def is_on(self): + """Return the state of the battery charging binary sensor.""" + return bool( + self.coordinator.data[HUB][SENSORS][BATTERY][ID] == BATTERY_CHARGING_ID + ) + + @property + def device_class(self): + """Return the device class of the battery charging binary sensor.""" + return DEVICE_CLASS_BATTERY_CHARGING diff --git a/homeassistant/components/rituals_perfume_genie/const.py b/homeassistant/components/rituals_perfume_genie/const.py index 16189c8335e..fef16b7f6f6 100644 --- a/homeassistant/components/rituals_perfume_genie/const.py +++ b/homeassistant/components/rituals_perfume_genie/const.py @@ -6,5 +6,8 @@ DEVICES = "devices" ACCOUNT_HASH = "account_hash" ATTRIBUTES = "attributes" +BATTERY = "battc" HUB = "hub" HUBLOT = "hublot" +ID = "id" +SENSORS = "sensors" diff --git a/homeassistant/components/rituals_perfume_genie/entity.py b/homeassistant/components/rituals_perfume_genie/entity.py index ba8f583d042..4f89856ad08 100644 --- a/homeassistant/components/rituals_perfume_genie/entity.py +++ b/homeassistant/components/rituals_perfume_genie/entity.py @@ -1,12 +1,11 @@ """Base class for Rituals Perfume Genie diffuser entity.""" from homeassistant.helpers.update_coordinator import CoordinatorEntity -from .const import ATTRIBUTES, DOMAIN, HUB, HUBLOT +from .const import ATTRIBUTES, DOMAIN, HUB, HUBLOT, SENSORS MANUFACTURER = "Rituals Cosmetics" MODEL = "Diffuser" -SENSORS = "sensors" ROOMNAME = "roomnamec" VERSION = "versionc" diff --git a/homeassistant/components/rituals_perfume_genie/sensor.py b/homeassistant/components/rituals_perfume_genie/sensor.py index 4a3ac34cc58..87c2da21bc7 100644 --- a/homeassistant/components/rituals_perfume_genie/sensor.py +++ b/homeassistant/components/rituals_perfume_genie/sensor.py @@ -1,24 +1,20 @@ """Support for Rituals Perfume Genie sensors.""" from homeassistant.const import ( - ATTR_BATTERY_CHARGING, ATTR_BATTERY_LEVEL, DEVICE_CLASS_BATTERY, DEVICE_CLASS_SIGNAL_STRENGTH, PERCENTAGE, ) -from .const import COORDINATORS, DEVICES, DOMAIN, HUB -from .entity import SENSORS, DiffuserEntity +from .const import BATTERY, COORDINATORS, DEVICES, DOMAIN, HUB, ID, SENSORS +from .entity import DiffuserEntity -ID = "id" TITLE = "title" ICON = "icon" WIFI = "wific" -BATTERY = "battc" PERFUME = "rfidc" FILL = "fillc" -BATTERY_CHARGING_ID = 21 PERFUME_NO_CARTRIDGE_ID = 19 FILL_NO_CARTRIDGE_ID = 12 @@ -106,13 +102,6 @@ class DiffuserBatterySensor(DiffuserEntity): "battery-low.png": 10, }[self.coordinator.data[HUB][SENSORS][BATTERY][ICON]] - @property - def _charging(self): - """Return battery charging state.""" - return bool( - self.coordinator.data[HUB][SENSORS][BATTERY][ID] == BATTERY_CHARGING_ID - ) - @property def device_class(self): """Return the class of the battery sensor.""" @@ -123,7 +112,6 @@ class DiffuserBatterySensor(DiffuserEntity): """Return the battery state attributes.""" return { ATTR_BATTERY_LEVEL: self.coordinator.data[HUB][SENSORS][BATTERY][TITLE], - ATTR_BATTERY_CHARGING: self._charging, } @property