diff --git a/custom_components/wellbeing/api.py b/custom_components/wellbeing/api.py index ac92d5e..7779193 100644 --- a/custom_components/wellbeing/api.py +++ b/custom_components/wellbeing/api.py @@ -2,16 +2,15 @@ import logging from enum import Enum -from homeassistant.components.binary_sensor import BinarySensorDeviceClass -from homeassistant.components.sensor import SensorDeviceClass +from homeassistant.components.binary_sensor import BinarySensorDeviceClass, BinarySensorEntity +from homeassistant.components.fan import FanEntity +from homeassistant.components.sensor import SensorDeviceClass, SensorEntity from homeassistant.const import UnitOfTemperature, PERCENTAGE, CONCENTRATION_PARTS_PER_MILLION, \ - CONCENTRATION_PARTS_PER_BILLION, CONCENTRATION_MICROGRAMS_PER_CUBIC_METER + CONCENTRATION_PARTS_PER_BILLION, CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, Platform, EntityCategory +from homeassistant.helpers.typing import UNDEFINED from pyelectroluxgroup.api import ElectroluxHubAPI from pyelectroluxgroup.appliance import Appliance as ApiAppliance -from custom_components.wellbeing.const import SENSOR, FAN, BINARY_SENSOR - - FILTER_TYPE = { 48: "BREEZE Complete air filter", 49: "CLEAN Ultrafine particle filter", @@ -41,10 +40,17 @@ class Mode(str, Enum): class ApplianceEntity: entity_type: int = None - def __init__(self, name, attr, device_class=None) -> None: + def __init__( + self, + name, + attr, + device_class=None, + entity_category: EntityCategory = UNDEFINED + ) -> None: self.attr = attr self.name = name self.device_class = device_class + self.entity_category = entity_category self._state = None def setup(self, data): @@ -60,25 +66,25 @@ def state(self): class ApplianceSensor(ApplianceEntity): - entity_type: int = SENSOR + entity_type: int = Platform.SENSOR - def __init__(self, name, attr, unit="", device_class=None) -> None: - super().__init__(name, attr, device_class) + def __init__(self, name, attr, unit="", device_class=None, entity_category: EntityCategory = UNDEFINED) -> None: + super().__init__(name, attr, device_class, entity_category) self.unit = unit class ApplianceFan(ApplianceEntity): - entity_type: int = FAN + entity_type: int = Platform.FAN def __init__(self, name, attr) -> None: super().__init__(name, attr) class ApplianceBinary(ApplianceEntity): - entity_type: int = BINARY_SENSOR + entity_type: int = Platform.BINARY_SENSOR - def __init__(self, name, attr, device_class=None) -> None: - super().__init__(name, attr, device_class) + def __init__(self, name, attr, device_class=None, entity_category: EntityCategory = UNDEFINED) -> None: + super().__init__(name, attr, device_class, entity_category) @property def state(self): @@ -114,11 +120,13 @@ def _create_entities(data): ), ApplianceSensor( name='State', - attr='State' + attr='State', + entity_category=EntityCategory.DIAGNOSTIC ), ApplianceBinary( name='PM Sensor State', - attr='PMSensState' + attr='PMSensState', + entity_category=EntityCategory.DIAGNOSTIC ) ] @@ -184,7 +192,8 @@ def _create_entities(data): ), ApplianceSensor( name="Mode", - attr='Workmode' + attr='Workmode', + device_class=SensorDeviceClass.ENUM ), ApplianceBinary( name="Ionizer", @@ -197,11 +206,13 @@ def _create_entities(data): ApplianceBinary( name="Connection State", attr='connectionState', - device_class=BinarySensorDeviceClass.CONNECTIVITY + device_class=BinarySensorDeviceClass.CONNECTIVITY, + entity_category=EntityCategory.DIAGNOSTIC ), ApplianceBinary( name="Status", - attr='status' + attr='status', + entity_category=EntityCategory.DIAGNOSTIC ), ApplianceBinary( name="Safety Lock", diff --git a/custom_components/wellbeing/binary_sensor.py b/custom_components/wellbeing/binary_sensor.py index 75deec3..3d3c7ed 100644 --- a/custom_components/wellbeing/binary_sensor.py +++ b/custom_components/wellbeing/binary_sensor.py @@ -1,7 +1,7 @@ """Binary sensor platform for Wellbeing.""" from homeassistant.components.binary_sensor import BinarySensorEntity +from homeassistant.const import Platform -from .const import BINARY_SENSOR from .const import DOMAIN from .entity import WellbeingEntity @@ -16,7 +16,7 @@ async def async_setup_entry(hass, entry, async_add_devices): async_add_devices( [ WellbeingBinarySensor(coordinator, entry, pnc_id, entity.entity_type, entity.attr) - for entity in appliance.entities if entity.entity_type == BINARY_SENSOR + for entity in appliance.entities if entity.entity_type == Platform.BINARY_SENSOR ] ) diff --git a/custom_components/wellbeing/entity.py b/custom_components/wellbeing/entity.py index 0b348ca..fd9981b 100644 --- a/custom_components/wellbeing/entity.py +++ b/custom_components/wellbeing/entity.py @@ -1,5 +1,6 @@ """WellbeingEntity class""" from homeassistant.components.sensor import ENTITY_ID_FORMAT +from homeassistant.const import EntityCategory from homeassistant.helpers.update_coordinator import CoordinatorEntity from . import WellbeingDataUpdateCoordinator from .api import Appliance, ApplianceEntity @@ -55,6 +56,11 @@ def extra_state_attributes(self): } @property - def device_class(self): - """Return de device class of the sensor.""" + def device_class(self) -> str | None: + """Return the device class of the sensor.""" return self.get_entity.device_class + + @property + def entity_category(self) -> EntityCategory | None: + """Return the entity category.""" + return self.get_entity.entity_category diff --git a/custom_components/wellbeing/fan.py b/custom_components/wellbeing/fan.py index c60f165..2983789 100644 --- a/custom_components/wellbeing/fan.py +++ b/custom_components/wellbeing/fan.py @@ -4,16 +4,16 @@ import math from homeassistant.components.fan import FanEntity, FanEntityFeature +from homeassistant.const import Platform from homeassistant.util.percentage import percentage_to_ranged_value, ranged_value_to_percentage from . import WellbeingDataUpdateCoordinator from .api import Mode from .const import DOMAIN -from .const import FAN from .entity import WellbeingEntity _LOGGER: logging.Logger = logging.getLogger(__package__) -SUPPORTED_FEATURES = FanEntityFeature.SET_SPEED | FanEntityFeature.PRESET_MODE +SUPPORTED_FEATURES = FanEntityFeature.SET_SPEED | FanEntityFeature.PRESET_MODE | FanEntityFeature.TURN_OFF | FanEntityFeature.TURN_ON PRESET_MODES = [ Mode.OFF, @@ -32,7 +32,7 @@ async def async_setup_entry(hass, entry, async_add_devices): async_add_devices( [ WellbeingFan(coordinator, entry, pnc_id, entity.entity_type, entity.attr) - for entity in appliance.entities if entity.entity_type == FAN + for entity in appliance.entities if entity.entity_type == Platform.FAN ] ) @@ -44,6 +44,7 @@ def __init__(self, coordinator: WellbeingDataUpdateCoordinator, config_entry, pn super().__init__(coordinator, config_entry, pnc_id, entity_type, entity_attr) self._preset_mode = str(self.get_appliance.mode) self._speed = self.get_entity.state + self.supported_features @property def _speed_range(self) -> tuple[float, float]: @@ -136,7 +137,6 @@ async def async_turn_on(self, speed: str = None, percentage: int = None, preset_ await asyncio.sleep(10) await self.coordinator.async_request_refresh() - async def async_turn_off(self, **kwargs) -> None: """Turn off the entity.""" self._preset_mode = Mode.OFF diff --git a/custom_components/wellbeing/manifest.json b/custom_components/wellbeing/manifest.json index e0ac55f..8c7ba25 100644 --- a/custom_components/wellbeing/manifest.json +++ b/custom_components/wellbeing/manifest.json @@ -7,6 +7,6 @@ "documentation": "https://github.com/JohNan/homeassistant-wellbeing", "iot_class": "cloud_polling", "issue_tracker": "https://github.com/JohNan/homeassistant-wellbeing/issues", - "requirements": ["pyelectroluxgroup==0.2.0"], + "requirements": ["pyelectroluxgroup==0.2.1"], "version": "v0.0.0" } diff --git a/custom_components/wellbeing/sensor.py b/custom_components/wellbeing/sensor.py index 6c2e458..b9ecf08 100644 --- a/custom_components/wellbeing/sensor.py +++ b/custom_components/wellbeing/sensor.py @@ -1,9 +1,12 @@ """Sensor platform for Wellbeing.""" +from sys import platform from typing import cast +from homeassistant.components.sensor import SensorEntity +from homeassistant.const import Platform + from .api import ApplianceSensor from .const import DOMAIN -from .const import SENSOR from .entity import WellbeingEntity @@ -17,19 +20,19 @@ async def async_setup_entry(hass, entry, async_add_devices): async_add_devices( [ WellbeingSensor(coordinator, entry, pnc_id, entity.entity_type, entity.attr) - for entity in appliance.entities if entity.entity_type == SENSOR + for entity in appliance.entities if entity.entity_type == Platform.SENSOR ] ) -class WellbeingSensor(WellbeingEntity): +class WellbeingSensor(WellbeingEntity, SensorEntity): """wellbeing Sensor class.""" @property - def state(self): + def native_value(self): """Return the state of the sensor.""" return self.get_entity.state @property - def unit_of_measurement(self): + def native_unit_of_measurement(self): return cast(ApplianceSensor, self.get_entity).unit diff --git a/requirements.txt b/requirements.txt index ef0148d..dae17b2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ homeassistant==2024.8.0 aiohttp -pyelectroluxgroup==0.2.0 +pyelectroluxgroup==0.2.1