Use entity class attributes for Bmp280 (#53036)

pull/53075/head
Robert Hillis 2021-07-15 06:35:47 -04:00 committed by GitHub
parent c9eab10134
commit 6c476b5c1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 38 deletions

View File

@ -77,36 +77,8 @@ class Bmp280Sensor(SensorEntity):
) -> None:
"""Initialize the sensor."""
self._bmp280 = bmp280
self._name = name
self._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
self._attr_name = name
self._attr_unit_of_measurement = unit_of_measurement
class Bmp280TemperatureSensor(Bmp280Sensor):
@ -122,16 +94,16 @@ class Bmp280TemperatureSensor(Bmp280Sensor):
def update(self):
"""Fetch new state data for the sensor."""
try:
self._state = round(self._bmp280.temperature, 1)
if self._errored:
self._attr_state = round(self._bmp280.temperature, 1)
if not self.available:
_LOGGER.warning("Communication restored with temperature sensor")
self._errored = False
self._attr_available = True
except OSError:
# this is thrown when a working sensor is unplugged between two updates
_LOGGER.warning(
"Unable to read temperature data due to a communication problem"
)
self._errored = True
self._attr_available = False
class Bmp280PressureSensor(Bmp280Sensor):
@ -147,13 +119,13 @@ class Bmp280PressureSensor(Bmp280Sensor):
def update(self):
"""Fetch new state data for the sensor."""
try:
self._state = round(self._bmp280.pressure)
if self._errored:
self._attr_state = round(self._bmp280.pressure)
if not self.available:
_LOGGER.warning("Communication restored with pressure sensor")
self._errored = False
self._attr_available = True
except OSError:
# this is thrown when a working sensor is unplugged between two updates
_LOGGER.warning(
"Unable to read pressure data due to a communication problem"
)
self._errored = True
self._attr_available = False