Use _attr_* in dte_energy_bridge (#61353)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2021-12-09 11:55:42 +01:00 committed by GitHub
parent 50e034d4f0
commit 487c44e11d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,10 +7,10 @@ import voluptuous as vol
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
STATE_CLASS_MEASUREMENT,
SensorEntity, SensorEntity,
SensorStateClass,
) )
from homeassistant.const import CONF_NAME from homeassistant.const import CONF_NAME, POWER_KILO_WATT
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -46,7 +46,9 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class DteEnergyBridgeSensor(SensorEntity): class DteEnergyBridgeSensor(SensorEntity):
"""Implementation of the DTE Energy Bridge sensors.""" """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): def __init__(self, ip_address, name, version):
"""Initialize the sensor.""" """Initialize the sensor."""
@ -57,29 +59,7 @@ class DteEnergyBridgeSensor(SensorEntity):
elif self._version == 2: elif self._version == 2:
self._url = f"http://{ip_address}:8888/zigbee/se/instantaneousdemand" self._url = f"http://{ip_address}:8888/zigbee/se/instantaneousdemand"
self._name = name self._attr_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
def update(self): def update(self):
"""Get the energy usage data from the DTE energy bridge.""" """Get the energy usage data from the DTE energy bridge."""
@ -87,7 +67,7 @@ class DteEnergyBridgeSensor(SensorEntity):
response = requests.get(self._url, timeout=5) response = requests.get(self._url, timeout=5)
except (requests.exceptions.RequestException, ValueError): except (requests.exceptions.RequestException, ValueError):
_LOGGER.warning( _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 return
@ -95,7 +75,7 @@ class DteEnergyBridgeSensor(SensorEntity):
_LOGGER.warning( _LOGGER.warning(
"Invalid status_code from DTE Energy Bridge: %s (%s)", "Invalid status_code from DTE Energy Bridge: %s (%s)",
response.status_code, response.status_code,
self._name, self._attr_name,
) )
return return
@ -105,7 +85,7 @@ class DteEnergyBridgeSensor(SensorEntity):
_LOGGER.warning( _LOGGER.warning(
'Invalid response from DTE Energy Bridge: "%s" (%s)', 'Invalid response from DTE Energy Bridge: "%s" (%s)',
response.text, response.text,
self._name, self._attr_name,
) )
return return
@ -118,6 +98,6 @@ class DteEnergyBridgeSensor(SensorEntity):
# values in the format 000000.000 kW, but the scaling is Watts # values in the format 000000.000 kW, but the scaling is Watts
# NOT kWatts # NOT kWatts
if self._version == 1 and "." in response_split[0]: if self._version == 1 and "." in response_split[0]:
self._state = val self._attr_native_value = val
else: else:
self._state = val / 1000 self._attr_native_value = val / 1000