From 487c44e11d2001a86593b9380b35ede32004b0ec Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 9 Dec 2021 11:55:42 +0100 Subject: [PATCH] Use _attr_* in dte_energy_bridge (#61353) Co-authored-by: epenet --- .../components/dte_energy_bridge/sensor.py | 42 +++++-------------- 1 file changed, 11 insertions(+), 31 deletions(-) diff --git a/homeassistant/components/dte_energy_bridge/sensor.py b/homeassistant/components/dte_energy_bridge/sensor.py index d443047a171..9dd7cd79ac7 100644 --- a/homeassistant/components/dte_energy_bridge/sensor.py +++ b/homeassistant/components/dte_energy_bridge/sensor.py @@ -7,10 +7,10 @@ import voluptuous as vol from homeassistant.components.sensor import ( PLATFORM_SCHEMA, - STATE_CLASS_MEASUREMENT, SensorEntity, + SensorStateClass, ) -from homeassistant.const import CONF_NAME +from homeassistant.const import CONF_NAME, POWER_KILO_WATT import homeassistant.helpers.config_validation as cv _LOGGER = logging.getLogger(__name__) @@ -46,7 +46,9 @@ def setup_platform(hass, config, add_entities, discovery_info=None): class DteEnergyBridgeSensor(SensorEntity): """Implementation of the DTE Energy Bridge sensors.""" - _attr_state_class = STATE_CLASS_MEASUREMENT + _attr_icon = ICON + _attr_native_unit_of_measurement = POWER_KILO_WATT + _attr_state_class = SensorStateClass.MEASUREMENT def __init__(self, ip_address, name, version): """Initialize the sensor.""" @@ -57,29 +59,7 @@ class DteEnergyBridgeSensor(SensorEntity): elif self._version == 2: self._url = f"http://{ip_address}:8888/zigbee/se/instantaneousdemand" - self._name = name - self._unit_of_measurement = "kW" - self._state = None - - @property - def name(self): - """Return the name of th sensor.""" - return self._name - - @property - def native_value(self): - """Return the state of the sensor.""" - return self._state - - @property - def native_unit_of_measurement(self): - """Return the unit of measurement of this entity, if any.""" - return self._unit_of_measurement - - @property - def icon(self): - """Icon to use in the frontend, if any.""" - return ICON + self._attr_name = name def update(self): """Get the energy usage data from the DTE energy bridge.""" @@ -87,7 +67,7 @@ class DteEnergyBridgeSensor(SensorEntity): response = requests.get(self._url, timeout=5) except (requests.exceptions.RequestException, ValueError): _LOGGER.warning( - "Could not update status for DTE Energy Bridge (%s)", self._name + "Could not update status for DTE Energy Bridge (%s)", self._attr_name ) return @@ -95,7 +75,7 @@ class DteEnergyBridgeSensor(SensorEntity): _LOGGER.warning( "Invalid status_code from DTE Energy Bridge: %s (%s)", response.status_code, - self._name, + self._attr_name, ) return @@ -105,7 +85,7 @@ class DteEnergyBridgeSensor(SensorEntity): _LOGGER.warning( 'Invalid response from DTE Energy Bridge: "%s" (%s)', response.text, - self._name, + self._attr_name, ) return @@ -118,6 +98,6 @@ class DteEnergyBridgeSensor(SensorEntity): # values in the format 000000.000 kW, but the scaling is Watts # NOT kWatts if self._version == 1 and "." in response_split[0]: - self._state = val + self._attr_native_value = val else: - self._state = val / 1000 + self._attr_native_value = val / 1000