Provide charging indicator for mychevy (#19348)

* Provide charging indicator for mychevy

This expands the mychevy sensor for the battery level to also know if
the system is currently charging to get the correct icon.

* address review feedback
This commit is contained in:
Sean Dague 2018-12-17 17:56:49 -05:00 committed by Charles Garwood
parent 57ccd8283d
commit e9c19462d6
3 changed files with 18 additions and 5 deletions

View file

@ -16,7 +16,7 @@ from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import discovery
from homeassistant.util import Throttle
REQUIREMENTS = ["mychevy==1.0.1"]
REQUIREMENTS = ["mychevy==1.1.0"]
DOMAIN = 'mychevy'
UPDATE_TOPIC = DOMAIN
@ -44,10 +44,12 @@ CONFIG_SCHEMA = vol.Schema({
class EVSensorConfig:
"""The EV sensor configuration."""
def __init__(self, name, attr, unit_of_measurement=None, icon=None):
def __init__(self, name, attr, unit_of_measurement=None, icon=None,
extra_attrs=None):
"""Create new sensor configuration."""
self.name = name
self.attr = attr
self.extra_attrs = extra_attrs or []
self.unit_of_measurement = unit_of_measurement
self.icon = icon

View file

@ -24,7 +24,8 @@ SENSORS = [
"mdi:speedometer"),
EVSensorConfig("Charged By", "estimatedFullChargeBy"),
EVSensorConfig("Charge Mode", "chargeMode"),
EVSensorConfig("Battery Level", BATTERY_SENSOR, "%", "mdi:battery")
EVSensorConfig("Battery Level", BATTERY_SENSOR, "%", "mdi:battery",
["charging"])
]
_LOGGER = logging.getLogger(__name__)
@ -117,9 +118,11 @@ class EVSensor(Entity):
self._conn = connection
self._name = config.name
self._attr = config.attr
self._extra_attrs = config.extra_attrs
self._unit_of_measurement = config.unit_of_measurement
self._icon = config.icon
self._state = None
self._state_attributes = {}
self._car_vid = car_vid
self.entity_id = ENTITY_ID_FORMAT.format(
@ -141,7 +144,8 @@ class EVSensor(Entity):
def icon(self):
"""Return the icon."""
if self._attr == BATTERY_SENSOR:
return icon_for_battery_level(self.state)
charging = self.state_attributes.get("charging", False)
return icon_for_battery_level(self.state, charging)
return self._icon
@property
@ -154,6 +158,8 @@ class EVSensor(Entity):
"""Update state."""
if self._car is not None:
self._state = getattr(self._car, self._attr, None)
for attr in self._extra_attrs:
self._state_attributes[attr] = getattr(self._car, attr)
self.async_schedule_update_ha_state()
@property
@ -161,6 +167,11 @@ class EVSensor(Entity):
"""Return the state."""
return self._state
@property
def device_state_attributes(self):
"""Return all the state attributes."""
return self._state_attributes
@property
def unit_of_measurement(self):
"""Return the unit of measurement the state is expressed in."""

View file

@ -669,7 +669,7 @@ motorparts==1.0.2
mutagen==1.41.1
# homeassistant.components.mychevy
mychevy==1.0.1
mychevy==1.1.0
# homeassistant.components.mycroft
mycroftapi==2.0