Skip to content
This repository has been archived by the owner on Apr 30, 2023. It is now read-only.

Commit

Permalink
Device info (#12)
Browse files Browse the repository at this point in the history
* Use DeviceInfo class

* Update license

* Update requirements-test file

* Fix mypy error

* Make _extract_attributes function

* Black
  • Loading branch information
bieniu authored Nov 1, 2021
1 parent 61ee459 commit b9e4a46
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ dmypy.json
# Home Assistant
*.yaml
*.log
*.db
*.log.*
*.db*
.cloud
.storage
.vscode
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
48 changes: 23 additions & 25 deletions custom_components/zadnego_ale/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
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,
SensorEntity,
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
Expand Down Expand Up @@ -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__(
Expand All @@ -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)}
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Żadnego Ale",
"homeassistant": "2021.9.0",
"homeassistant": "2021.11.0",
"iot_class": "Cloud Polling",
"domains": ["sensor"],
"zip_release": true,
Expand Down
22 changes: 12 additions & 10 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
aiohttp_cors
black
flake8
isort
mypy
pylint
pytest
pytest-cov
pytest-homeassistant-custom-component
zadnegoale
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

0 comments on commit b9e4a46

Please sign in to comment.