Files
homeassistant-dkncloudna/custom_components/dkncloudna/sensor.py
T
Kydoimosandanthropic/claude-opus-5 706580fab2
CI / Validate integration (pull_request) Successful in 40s
CI / Lint (pull_request) Successful in 49s
chore: drop HACS distribution and move CI to Gitea
The GitHub mirror is gone, so the HACS packaging and the GitHub-only CI
no longer have anything to run against.

Remove hacs.json and the .github/ workflow. Both of its jobs (hassfest
and hacs/action) were gated on `github.server_url == 'https://github.com'`
and are GitHub-hosted actions, so neither can run on the Gitea runner.

Replace them with .gitea/workflows/ci.yml:
  - lint: ruff check + ruff format --check, version pinned because ruff's
    default rule set and formatter output move between releases
  - validate: byte-compile, then scripts/validate_integration.py, which
    covers the part of hassfest that matters for a manually installed
    custom component (manifest keys, domain/directory agreement, and
    translations matching strings.json key for key)

README now documents manual installation only, and points at the local
CI commands. manifest.json documentation and issue_tracker point at the
Gitea repo; NOTICE keeps its upstream attribution.

Adopting ruff surfaced two real defects, fixed here:
  - climate.async_set_hvac_mode raised HomeAssistantError for an
    unsupported mode from inside a try that catches Exception, so the
    message was re-wrapped as "Failed to set HVAC mode: Unsupported HVAC
    mode: ...". The validation is now hoisted above the try.
  - config_flow.async_step_reauth_confirm swallowed unexpected exceptions
    into a bare "unknown" error with no log, unlike the user step. It now
    logs via _LOGGER.exception.

Remaining changes are mechanical: import ordering, docstrings on public
methods, ClassVar on mutable class attributes, contextlib.suppress, and
asyncio.TimeoutError -> TimeoutError (an alias since 3.11). The smoke
test now reads the new DknCloudNaClient.socket_connected property rather
than reaching into _socket.

Co-authored-by: anthropic/claude-opus-5
2026-09-19 10:09:11 -03:00

107 lines
3.5 KiB
Python

"""Sensor entities for DKN Cloud NA."""
from __future__ import annotations
from dataclasses import dataclass
from typing import Any
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import SIGNAL_STRENGTH_DECIBELS_MILLIWATT, UnitOfTemperature
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN
from .coordinator import DknCoordinator
from .entity import DknEntity
from .model import current_temperature, exterior_temperature
@dataclass(frozen=True, kw_only=True)
class DknSensorEntityDescription(SensorEntityDescription):
"""Extend SensorEntityDescription with a device_data key."""
data_key: str = ""
SENSOR_DESCRIPTIONS: tuple[DknSensorEntityDescription, ...] = (
DknSensorEntityDescription(
key="room_temperature",
translation_key="room_temperature",
data_key="work_temp",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
DknSensorEntityDescription(
key="exterior_temperature",
translation_key="exterior_temperature",
data_key="ext_temp",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
entity_registry_enabled_default=False,
),
DknSensorEntityDescription(
key="wifi_signal",
translation_key="wifi_signal",
data_key="stat_rssi",
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
entity_category=EntityCategory.DIAGNOSTIC,
),
DknSensorEntityDescription(
key="error_code",
translation_key="error_code",
data_key="error_ascii1",
entity_category=EntityCategory.DIAGNOSTIC,
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up sensor entities."""
coordinator: DknCoordinator = hass.data[DOMAIN][entry.entry_id]["coordinator"]
async_add_entities(
DknSensorEntity(coordinator, mac, desc)
for mac in (coordinator.data or {})
for desc in SENSOR_DESCRIPTIONS
)
class DknSensorEntity(DknEntity, SensorEntity):
"""A single sensor for one property of a DKN device."""
entity_description: DknSensorEntityDescription
def __init__(
self,
coordinator: DknCoordinator,
mac: str,
description: DknSensorEntityDescription,
) -> None:
"""Initialise the sensor from its description."""
super().__init__(coordinator, mac)
self.entity_description = description
self._attr_unique_id = f"{DOMAIN}_{mac}_{description.key}"
@property
def native_value(self) -> Any:
"""Return the described device value, converted where needed."""
if self.entity_description.data_key == "work_temp":
return current_temperature(self._device_data)
if self.entity_description.data_key == "ext_temp":
return exterior_temperature(self._device_data)
return self._device_data.get(self.entity_description.data_key)