From 6c476b5c1e47a6d14eb11df1adb5e7d1120f0485 Mon Sep 17 00:00:00 2001 From: Robert Hillis Date: Thu, 15 Jul 2021 06:35:47 -0400 Subject: [PATCH] Use entity class attributes for Bmp280 (#53036) --- homeassistant/components/bmp280/sensor.py | 48 +++++------------------ 1 file changed, 10 insertions(+), 38 deletions(-) diff --git a/homeassistant/components/bmp280/sensor.py b/homeassistant/components/bmp280/sensor.py index ac607578299..7bf355bb736 100644 --- a/homeassistant/components/bmp280/sensor.py +++ b/homeassistant/components/bmp280/sensor.py @@ -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