mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Use attr** in meteoalarm (#61895)
Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
parent
863a139b6f
commit
e20029d87f
@ -6,11 +6,11 @@ from meteoalertapi import Meteoalert
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import (
|
from homeassistant.components.binary_sensor import (
|
||||||
DEVICE_CLASS_SAFETY,
|
|
||||||
PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA,
|
||||||
|
BinarySensorDeviceClass,
|
||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.const import ATTR_ATTRIBUTION, CONF_NAME
|
from homeassistant.const import CONF_NAME
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
@ -56,43 +56,23 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
class MeteoAlertBinarySensor(BinarySensorEntity):
|
class MeteoAlertBinarySensor(BinarySensorEntity):
|
||||||
"""Representation of a MeteoAlert binary sensor."""
|
"""Representation of a MeteoAlert binary sensor."""
|
||||||
|
|
||||||
|
_attr_attribution = ATTRIBUTION
|
||||||
|
_attr_device_class = BinarySensorDeviceClass.SAFETY
|
||||||
|
|
||||||
def __init__(self, api, name):
|
def __init__(self, api, name):
|
||||||
"""Initialize the MeteoAlert binary sensor."""
|
"""Initialize the MeteoAlert binary sensor."""
|
||||||
self._name = name
|
self._attr_name = name
|
||||||
self._attributes = {}
|
|
||||||
self._state = None
|
|
||||||
self._api = api
|
self._api = api
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the binary sensor."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def is_on(self):
|
|
||||||
"""Return the status of the binary sensor."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
|
||||||
def extra_state_attributes(self):
|
|
||||||
"""Return the state attributes."""
|
|
||||||
self._attributes[ATTR_ATTRIBUTION] = ATTRIBUTION
|
|
||||||
return self._attributes
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self):
|
|
||||||
"""Return the device class of this binary sensor."""
|
|
||||||
return DEVICE_CLASS_SAFETY
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update device state."""
|
"""Update device state."""
|
||||||
self._attributes = {}
|
self._attr_extra_state_attributes = None
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
|
|
||||||
if alert := self._api.get_alert():
|
if alert := self._api.get_alert():
|
||||||
expiration_date = dt_util.parse_datetime(alert["expires"])
|
expiration_date = dt_util.parse_datetime(alert["expires"])
|
||||||
now = dt_util.utcnow()
|
now = dt_util.utcnow()
|
||||||
|
|
||||||
if expiration_date > now:
|
if expiration_date > now:
|
||||||
self._attributes = alert
|
self._attr_extra_state_attributes = alert
|
||||||
self._state = True
|
self._attr_is_on = True
|
||||||
|
Loading…
x
Reference in New Issue
Block a user