mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 02:37:08 +00:00
Use attrs instead of properties in Brother (#51742)
* Use attrs instead of properties * Use get() for device_class
This commit is contained in:
parent
7d03b02192
commit
343e0e0933
@ -4,7 +4,12 @@ from __future__ import annotations
|
|||||||
from typing import Final
|
from typing import Final
|
||||||
|
|
||||||
from homeassistant.components.sensor import ATTR_STATE_CLASS, STATE_CLASS_MEASUREMENT
|
from homeassistant.components.sensor import ATTR_STATE_CLASS, STATE_CLASS_MEASUREMENT
|
||||||
from homeassistant.const import ATTR_ICON, PERCENTAGE
|
from homeassistant.const import (
|
||||||
|
ATTR_DEVICE_CLASS,
|
||||||
|
ATTR_ICON,
|
||||||
|
DEVICE_CLASS_TIMESTAMP,
|
||||||
|
PERCENTAGE,
|
||||||
|
)
|
||||||
|
|
||||||
from .model import SensorDescription
|
from .model import SensorDescription
|
||||||
|
|
||||||
@ -247,5 +252,6 @@ SENSOR_TYPES: Final[dict[str, SensorDescription]] = {
|
|||||||
ATTR_UNIT: None,
|
ATTR_UNIT: None,
|
||||||
ATTR_ENABLED: False,
|
ATTR_ENABLED: False,
|
||||||
ATTR_STATE_CLASS: None,
|
ATTR_STATE_CLASS: None,
|
||||||
|
ATTR_DEVICE_CLASS: DEVICE_CLASS_TIMESTAMP,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ from __future__ import annotations
|
|||||||
from typing import TypedDict
|
from typing import TypedDict
|
||||||
|
|
||||||
|
|
||||||
class SensorDescription(TypedDict):
|
class SensorDescription(TypedDict, total=False):
|
||||||
"""Sensor description class."""
|
"""Sensor description class."""
|
||||||
|
|
||||||
icon: str | None
|
icon: str | None
|
||||||
@ -12,3 +12,4 @@ class SensorDescription(TypedDict):
|
|||||||
unit: str | None
|
unit: str | None
|
||||||
enabled: bool
|
enabled: bool
|
||||||
state_class: str | None
|
state_class: str | None
|
||||||
|
device_class: str | None
|
||||||
|
@ -5,7 +5,7 @@ from typing import Any
|
|||||||
|
|
||||||
from homeassistant.components.sensor import ATTR_STATE_CLASS, SensorEntity
|
from homeassistant.components.sensor import ATTR_STATE_CLASS, SensorEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import ATTR_ICON, DEVICE_CLASS_TIMESTAMP
|
from homeassistant.const import ATTR_DEVICE_CLASS, ATTR_ICON
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
@ -60,18 +60,17 @@ class BrotherPrinterSensor(CoordinatorEntity, SensorEntity):
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize."""
|
"""Initialize."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._description = SENSOR_TYPES[kind]
|
description = SENSOR_TYPES[kind]
|
||||||
self._name = f"{coordinator.data.model} {self._description[ATTR_LABEL]}"
|
|
||||||
self._unique_id = f"{coordinator.data.serial.lower()}_{kind}"
|
|
||||||
self._device_info = device_info
|
|
||||||
self.kind = kind
|
|
||||||
self._attrs: dict[str, Any] = {}
|
self._attrs: dict[str, Any] = {}
|
||||||
self._attr_state_class = self._description[ATTR_STATE_CLASS]
|
self._attr_device_class = description.get(ATTR_DEVICE_CLASS)
|
||||||
|
self._attr_device_info = device_info
|
||||||
@property
|
self._attr_entity_registry_enabled_default = description[ATTR_ENABLED]
|
||||||
def name(self) -> str:
|
self._attr_icon = description[ATTR_ICON]
|
||||||
"""Return the name."""
|
self._attr_name = f"{coordinator.data.model} {description[ATTR_LABEL]}"
|
||||||
return self._name
|
self._attr_state_class = description[ATTR_STATE_CLASS]
|
||||||
|
self._attr_unique_id = f"{coordinator.data.serial.lower()}_{kind}"
|
||||||
|
self._attr_unit_of_measurement = description[ATTR_UNIT]
|
||||||
|
self.kind = kind
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> Any:
|
def state(self) -> Any:
|
||||||
@ -80,13 +79,6 @@ class BrotherPrinterSensor(CoordinatorEntity, SensorEntity):
|
|||||||
return getattr(self.coordinator.data, self.kind).isoformat()
|
return getattr(self.coordinator.data, self.kind).isoformat()
|
||||||
return getattr(self.coordinator.data, self.kind)
|
return getattr(self.coordinator.data, self.kind)
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self) -> str | None:
|
|
||||||
"""Return the class of this sensor."""
|
|
||||||
if self.kind == ATTR_UPTIME:
|
|
||||||
return DEVICE_CLASS_TIMESTAMP
|
|
||||||
return None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self) -> dict[str, Any]:
|
def extra_state_attributes(self) -> dict[str, Any]:
|
||||||
"""Return the state attributes."""
|
"""Return the state attributes."""
|
||||||
@ -97,28 +89,3 @@ class BrotherPrinterSensor(CoordinatorEntity, SensorEntity):
|
|||||||
)
|
)
|
||||||
self._attrs[ATTR_COUNTER] = getattr(self.coordinator.data, drum_counter)
|
self._attrs[ATTR_COUNTER] = getattr(self.coordinator.data, drum_counter)
|
||||||
return self._attrs
|
return self._attrs
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self) -> str | None:
|
|
||||||
"""Return the icon."""
|
|
||||||
return self._description[ATTR_ICON]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self) -> str:
|
|
||||||
"""Return a unique_id for this entity."""
|
|
||||||
return self._unique_id
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unit_of_measurement(self) -> str | None:
|
|
||||||
"""Return the unit the value is expressed in."""
|
|
||||||
return self._description[ATTR_UNIT]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_info(self) -> DeviceInfo:
|
|
||||||
"""Return the device info."""
|
|
||||||
return self._device_info
|
|
||||||
|
|
||||||
@property
|
|
||||||
def entity_registry_enabled_default(self) -> bool:
|
|
||||||
"""Return if the entity should be enabled when first added to the entity registry."""
|
|
||||||
return self._description[ATTR_ENABLED]
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user