core/homeassistant/components/drop_connect/entity.py

55 lines
1.7 KiB
Python
Raw Normal View History

Add DROP integration (#104319) * Add DROP integration * Remove all but one platform for first PR * Simplify initialization of hass.data[] structure * Remove unnecessary mnemonic 'DROP_' prefix from DOMAIN constants * Remove unnecessary whitespace * Clarify configuration 'confirm' step description * Remove unnecessary whitespace * Use device class where applicable * Remove unnecessary constructor and change its elements to class variables * Change base entity inheritance to CoordinatorEntity * Make sensor definitions more concise * Rename HA domain from drop to drop_connect * Remove underscores from class and function names * Remove duplicate temperature sensor * Change title capitalization * Refactor using SensorEntityDescription * Remove unnecessary intermediate dict layer * Remove generated translations file * Remove currently unused string values * Use constants in sensor definitions * Replace values with constants * Move translation keys * Remove unnecessary unique ID and config entry references * Clean up DROPEntity initialization * Clean up sensors * Rename vars and functions according to style * Remove redundant self references * Clean up DROPSensor initializer * Add missing state classes * Simplify detection of configured devices * Change entity identifiers to create device linkage * Move device_info to coordinator * Remove unnecessary properties * Correct hub device IDs * Remove redundant attribute * Replace optional UID with assert * Remove redundant attribute * Correct coordinator initialization * Fix mypy error * Move API functionality to 3rd party library * Abstract device to sensor map into a dict * Unsubscribe MQTT on unload * Move entity device information * Make type checking for mypy conditional * Bump dropmqttapi to 1.0.1 * Freeze dataclass to match parent class * Fix race condition in MQTT unsubscribe setup * Ensure unit tests begin with invalid MQTT state * Change unit tests to reflect device firmware * Move MQTT subscription out of the coordinator * Tidy up initializer * Move entirety of MQTT subscription out of the coordinator * Make drop_api a class property * Remove unnecessary type checks * Simplify some unit test asserts * Remove argument matching default * Add entity category to battery and cartridge life sensors
2023-12-22 13:24:08 +00:00
"""Base entity class for DROP entities."""
Add DROP integration (#104319) * Add DROP integration * Remove all but one platform for first PR * Simplify initialization of hass.data[] structure * Remove unnecessary mnemonic 'DROP_' prefix from DOMAIN constants * Remove unnecessary whitespace * Clarify configuration 'confirm' step description * Remove unnecessary whitespace * Use device class where applicable * Remove unnecessary constructor and change its elements to class variables * Change base entity inheritance to CoordinatorEntity * Make sensor definitions more concise * Rename HA domain from drop to drop_connect * Remove underscores from class and function names * Remove duplicate temperature sensor * Change title capitalization * Refactor using SensorEntityDescription * Remove unnecessary intermediate dict layer * Remove generated translations file * Remove currently unused string values * Use constants in sensor definitions * Replace values with constants * Move translation keys * Remove unnecessary unique ID and config entry references * Clean up DROPEntity initialization * Clean up sensors * Rename vars and functions according to style * Remove redundant self references * Clean up DROPSensor initializer * Add missing state classes * Simplify detection of configured devices * Change entity identifiers to create device linkage * Move device_info to coordinator * Remove unnecessary properties * Correct hub device IDs * Remove redundant attribute * Replace optional UID with assert * Remove redundant attribute * Correct coordinator initialization * Fix mypy error * Move API functionality to 3rd party library * Abstract device to sensor map into a dict * Unsubscribe MQTT on unload * Move entity device information * Make type checking for mypy conditional * Bump dropmqttapi to 1.0.1 * Freeze dataclass to match parent class * Fix race condition in MQTT unsubscribe setup * Ensure unit tests begin with invalid MQTT state * Change unit tests to reflect device firmware * Move MQTT subscription out of the coordinator * Tidy up initializer * Move entirety of MQTT subscription out of the coordinator * Make drop_api a class property * Remove unnecessary type checks * Simplify some unit test asserts * Remove argument matching default * Add entity category to battery and cartridge life sensors
2023-12-22 13:24:08 +00:00
from __future__ import annotations
from typing import TYPE_CHECKING
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import (
CONF_DEVICE_DESC,
CONF_DEVICE_NAME,
CONF_DEVICE_OWNER_ID,
CONF_DEVICE_TYPE,
CONF_HUB_ID,
DEV_HUB,
DOMAIN,
)
from .coordinator import DROPDeviceDataUpdateCoordinator
class DROPEntity(CoordinatorEntity[DROPDeviceDataUpdateCoordinator]):
"""Representation of a DROP device entity."""
_attr_has_entity_name = True
def __init__(
self, entity_type: str, coordinator: DROPDeviceDataUpdateCoordinator
) -> None:
"""Init DROP entity."""
super().__init__(coordinator)
if TYPE_CHECKING:
assert coordinator.config_entry.unique_id is not None
unique_id = coordinator.config_entry.unique_id
self._attr_unique_id = f"{unique_id}_{entity_type}"
entry_data = coordinator.config_entry.data
model: str = entry_data[CONF_DEVICE_DESC]
if entry_data[CONF_DEVICE_TYPE] == DEV_HUB:
model = f"Hub {entry_data[CONF_HUB_ID]}"
self._attr_device_info = DeviceInfo(
manufacturer="Chandler Systems, Inc.",
model=model,
name=entry_data[CONF_DEVICE_NAME],
identifiers={(DOMAIN, unique_id)},
)
if entry_data[CONF_DEVICE_TYPE] != DEV_HUB:
self._attr_device_info.update(
{
"via_device": (
DOMAIN,
entry_data[CONF_DEVICE_OWNER_ID],
)
}
)