Skip to content

Commit

Permalink
Provide charging indicator for mychevy (home-assistant#19348)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sdague authored and dshokouhi committed Dec 25, 2018
1 parent 183f4dc commit f411491
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
6 changes: 4 additions & 2 deletions homeassistant/components/mychevy.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
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
Expand Down Expand Up @@ -44,10 +44,12 @@
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

Expand Down
15 changes: 13 additions & 2 deletions homeassistant/components/sensor/mychevy.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"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__)
Expand Down Expand Up @@ -117,9 +118,11 @@ def __init__(self, connection, config, car_vid):
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(
Expand All @@ -141,7 +144,8 @@ def _car(self):
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
Expand All @@ -154,13 +158,20 @@ def async_update_callback(self):
"""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
def state(self):
"""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."""
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f411491

Please sign in to comment.