Use entity class attributes for Bmp280 (#53036)
parent
c9eab10134
commit
6c476b5c1e
|
@ -77,36 +77,8 @@ class Bmp280Sensor(SensorEntity):
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._bmp280 = bmp280
|
self._bmp280 = bmp280
|
||||||
self._name = name
|
self._attr_name = name
|
||||||
self._unit_of_measurement = unit_of_measurement
|
self._attr_unit_of_measurement = unit_of_measurement
|
||||||
self._device_class = device_class
|
|
||||||
self._state = None
|
|
||||||
self._errored = False
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the sensor."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def state(self):
|
|
||||||
"""Return the state of the sensor."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unit_of_measurement(self):
|
|
||||||
"""Return the unit of measurement."""
|
|
||||||
return self._unit_of_measurement
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self):
|
|
||||||
"""Return the device class."""
|
|
||||||
return self._device_class
|
|
||||||
|
|
||||||
@property
|
|
||||||
def available(self) -> bool:
|
|
||||||
"""Return if the device is currently available."""
|
|
||||||
return not self._errored
|
|
||||||
|
|
||||||
|
|
||||||
class Bmp280TemperatureSensor(Bmp280Sensor):
|
class Bmp280TemperatureSensor(Bmp280Sensor):
|
||||||
|
@ -122,16 +94,16 @@ class Bmp280TemperatureSensor(Bmp280Sensor):
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Fetch new state data for the sensor."""
|
"""Fetch new state data for the sensor."""
|
||||||
try:
|
try:
|
||||||
self._state = round(self._bmp280.temperature, 1)
|
self._attr_state = round(self._bmp280.temperature, 1)
|
||||||
if self._errored:
|
if not self.available:
|
||||||
_LOGGER.warning("Communication restored with temperature sensor")
|
_LOGGER.warning("Communication restored with temperature sensor")
|
||||||
self._errored = False
|
self._attr_available = True
|
||||||
except OSError:
|
except OSError:
|
||||||
# this is thrown when a working sensor is unplugged between two updates
|
# this is thrown when a working sensor is unplugged between two updates
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Unable to read temperature data due to a communication problem"
|
"Unable to read temperature data due to a communication problem"
|
||||||
)
|
)
|
||||||
self._errored = True
|
self._attr_available = False
|
||||||
|
|
||||||
|
|
||||||
class Bmp280PressureSensor(Bmp280Sensor):
|
class Bmp280PressureSensor(Bmp280Sensor):
|
||||||
|
@ -147,13 +119,13 @@ class Bmp280PressureSensor(Bmp280Sensor):
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Fetch new state data for the sensor."""
|
"""Fetch new state data for the sensor."""
|
||||||
try:
|
try:
|
||||||
self._state = round(self._bmp280.pressure)
|
self._attr_state = round(self._bmp280.pressure)
|
||||||
if self._errored:
|
if not self.available:
|
||||||
_LOGGER.warning("Communication restored with pressure sensor")
|
_LOGGER.warning("Communication restored with pressure sensor")
|
||||||
self._errored = False
|
self._attr_available = True
|
||||||
except OSError:
|
except OSError:
|
||||||
# this is thrown when a working sensor is unplugged between two updates
|
# this is thrown when a working sensor is unplugged between two updates
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Unable to read pressure data due to a communication problem"
|
"Unable to read pressure data due to a communication problem"
|
||||||
)
|
)
|
||||||
self._errored = True
|
self._attr_available = False
|
||||||
|
|
Loading…
Reference in New Issue