Fix nina warning state (#76354)

* Fix warning state

* Improve data handling

* Remove duplicate code
This commit is contained in:
Maximilian 2022-10-02 06:22:18 +02:00 committed by GitHub
parent 205ce2bac5
commit 7ae942a62b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 118 additions and 41 deletions

View file

@ -12,7 +12,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import NINADataUpdateCoordinator
from . import NINADataUpdateCoordinator, NinaWarningData
from .const import (
ATTR_DESCRIPTION,
ATTR_EXPIRES,
@ -72,25 +72,28 @@ class NINAMessage(CoordinatorEntity[NINADataUpdateCoordinator], BinarySensorEnti
@property
def is_on(self) -> bool:
"""Return the state of the sensor."""
return len(self.coordinator.data[self._region]) > self._warning_index
if not len(self.coordinator.data[self._region]) > self._warning_index:
return False
data: NinaWarningData = self.coordinator.data[self._region][self._warning_index]
return data.is_valid
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return extra attributes of the sensor."""
if (
not len(self.coordinator.data[self._region]) > self._warning_index
) or not self.is_on:
if not self.is_on:
return {}
data: dict[str, Any] = self.coordinator.data[self._region][self._warning_index]
data: NinaWarningData = self.coordinator.data[self._region][self._warning_index]
return {
ATTR_HEADLINE: data[ATTR_HEADLINE],
ATTR_DESCRIPTION: data[ATTR_DESCRIPTION],
ATTR_SENDER: data[ATTR_SENDER],
ATTR_SEVERITY: data[ATTR_SEVERITY],
ATTR_ID: data[ATTR_ID],
ATTR_SENT: data[ATTR_SENT],
ATTR_START: data[ATTR_START],
ATTR_EXPIRES: data[ATTR_EXPIRES],
ATTR_HEADLINE: data.headline,
ATTR_DESCRIPTION: data.description,
ATTR_SENDER: data.sender,
ATTR_SEVERITY: data.severity,
ATTR_ID: data.id,
ATTR_SENT: data.sent,
ATTR_START: data.start,
ATTR_EXPIRES: data.expires,
}