Skip to content

Commit

Permalink
Adds entity category
Browse files Browse the repository at this point in the history
  • Loading branch information
JohNan committed Aug 14, 2024
1 parent 663a4c4 commit a8eeada
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 34 deletions.
49 changes: 30 additions & 19 deletions custom_components/wellbeing/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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
)
]

Expand Down Expand Up @@ -184,7 +192,8 @@ def _create_entities(data):
),
ApplianceSensor(
name="Mode",
attr='Workmode'
attr='Workmode',
device_class=SensorDeviceClass.ENUM
),
ApplianceBinary(
name="Ionizer",
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions custom_components/wellbeing/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
]
)

Expand Down
10 changes: 8 additions & 2 deletions custom_components/wellbeing/entity.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
8 changes: 4 additions & 4 deletions custom_components/wellbeing/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
]
)

Expand All @@ -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]:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion custom_components/wellbeing/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
13 changes: 8 additions & 5 deletions custom_components/wellbeing/sensor.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
homeassistant==2024.8.0
aiohttp
pyelectroluxgroup==0.2.0
pyelectroluxgroup==0.2.1

0 comments on commit a8eeada

Please sign in to comment.