diff --git a/.gitignore b/.gitignore index 9d9e2e7..50f5484 100644 --- a/.gitignore +++ b/.gitignore @@ -131,7 +131,8 @@ dmypy.json # Home Assistant *.yaml *.log -*.db +*.log.* +*.db* .cloud .storage .vscode diff --git a/LICENSE b/LICENSE index 261eeb9..dddcdd5 100644 --- a/LICENSE +++ b/LICENSE @@ -186,7 +186,7 @@ same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright [yyyy] [name of copyright owner] + Copyright 2021 Maciej Bieniek Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/custom_components/zadnego_ale/sensor.py b/custom_components/zadnego_ale/sensor.py index a7bac18..430c4f3 100644 --- a/custom_components/zadnego_ale/sensor.py +++ b/custom_components/zadnego_ale/sensor.py @@ -2,7 +2,9 @@ from __future__ import annotations import logging -from typing import Any, cast +from typing import Any + +from zadnegoale.model import Allergen from homeassistant.components.sensor import ( DOMAIN as PLATFORM, @@ -10,11 +12,10 @@ SensorEntityDescription, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ATTR_ATTRIBUTION from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import entity_registry +from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.typing import StateType from homeassistant.helpers.update_coordinator import CoordinatorEntity from . import ZadnegoAleDataUpdateCoordinator @@ -65,6 +66,7 @@ async def async_setup_entry( class ZadnegoAleSensor(CoordinatorEntity, SensorEntity): """Define an Zadnego Ale sensor.""" + _attr_attribution = ATTRIBUTION coordinator: ZadnegoAleDataUpdateCoordinator def __init__( @@ -75,32 +77,28 @@ def __init__( """Initialize.""" super().__init__(coordinator) - self._attr_device_info = { - "identifiers": {(DOMAIN, str(coordinator.region))}, - "name": REGIONS[coordinator.region - 1], - "manufacturer": "Żadnego Ale", - "entry_type": "service", - } + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, str(coordinator.region))}, + name=REGIONS[coordinator.region - 1], + manufacturer="Żadnego Ale", + entry_type="service", + ) self._attr_unique_id = f"{coordinator.region}-{description.key}" - self._attrs = {ATTR_ATTRIBUTION: ATTRIBUTION} - self._sensor_data = getattr(coordinator.data, description.key) + sensor_data = getattr(coordinator.data, description.key) + self._attr_native_value = sensor_data.level + self._attr_extra_state_attributes = _extract_attributes(sensor_data) self.entity_description = description - @property - def native_value(self) -> StateType: - """Return the state.""" - return cast(StateType, self._sensor_data.level) - - @property - def extra_state_attributes(self) -> dict[str, Any]: - """Return the state attributes.""" - for item in (ATTR_TREND, ATTR_VALUE): - self._attrs[item] = getattr(self._sensor_data, item) - - return self._attrs - @callback def _handle_coordinator_update(self) -> None: """Handle updated data from the coordinator.""" - self._sensor_data = getattr(self.coordinator.data, self.entity_description.key) + sensor_data = getattr(self.coordinator.data, self.entity_description.key) + self._attr_native_value = sensor_data.level + self._attr_extra_state_attributes = _extract_attributes(sensor_data) self.async_write_ha_state() + + +@callback +def _extract_attributes(sensor_data: Allergen) -> dict[str, Any]: + """Extract attributes from sensor data.""" + return {item: getattr(sensor_data, item) for item in (ATTR_TREND, ATTR_VALUE)} diff --git a/hacs.json b/hacs.json index d6a4bd4..99130c3 100644 --- a/hacs.json +++ b/hacs.json @@ -1,6 +1,6 @@ { "name": "Żadnego Ale", - "homeassistant": "2021.9.0", + "homeassistant": "2021.11.0", "iot_class": "Cloud Polling", "domains": ["sensor"], "zip_release": true, diff --git a/requirements-test.txt b/requirements-test.txt index ccde874..5224b2f 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -1,10 +1,12 @@ -aiohttp_cors -black -flake8 -isort -mypy -pylint -pytest -pytest-cov -pytest-homeassistant-custom-component -zadnegoale \ No newline at end of file +aiohttp_cors==0.7.0 +black==21.10b0 +coverage==6.0.2 +flake8==4.0.1 +homeassistant>=2021.11.0b0 +isort==5.9.3 +mypy==0.910 +pylint==2.11.1 +pytest-cov==2.12.1 +pytest-homeassistant-custom-component==0.4.6 +pytest==6.2.5 +zadnegoale==0.6.0 \ No newline at end of file