-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Add battery sensor to iCloud #29818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MartinHjelmare
merged 4 commits into
home-assistant:dev
from
Quentame:icloud/add-battery-sensor
Dec 14, 2019
Merged
Add battery sensor to iCloud #29818
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| """Support for iCloud sensors.""" | ||
| import logging | ||
| from typing import Dict | ||
|
|
||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.const import CONF_USERNAME, DEVICE_CLASS_BATTERY | ||
| from homeassistant.helpers.entity import Entity | ||
| from homeassistant.helpers.icon import icon_for_battery_level | ||
| from homeassistant.helpers.typing import HomeAssistantType | ||
|
|
||
| from . import IcloudDevice | ||
| from .const import DOMAIN | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistantType, entry: ConfigEntry, async_add_entities | ||
| ) -> None: | ||
| """Set up iCloud devices sensors based on a config entry.""" | ||
| username = entry.data[CONF_USERNAME] | ||
|
|
||
| entities = [] | ||
| for device in hass.data[DOMAIN][username].devices.values(): | ||
| if device.battery_level is not None: | ||
| _LOGGER.debug("Adding battery sensor for %s", device.name) | ||
| entities.append(IcloudDeviceBatterySensor(device)) | ||
|
|
||
| async_add_entities(entities, True) | ||
|
|
||
|
|
||
| class IcloudDeviceBatterySensor(Entity): | ||
| """Representation of a iCloud device battery sensor.""" | ||
|
|
||
| def __init__(self, device: IcloudDevice): | ||
| """Initialize the battery sensor.""" | ||
| self._device = device | ||
|
|
||
| @property | ||
| def unique_id(self) -> str: | ||
| """Return a unique ID.""" | ||
| return f"{self._device.unique_id}_battery" | ||
|
|
||
| @property | ||
| def name(self) -> str: | ||
| """Sensor name.""" | ||
| return f"{self._device.name} battery state" | ||
|
|
||
| @property | ||
| def device_class(self) -> str: | ||
| """Return the device class of the sensor.""" | ||
| return DEVICE_CLASS_BATTERY | ||
|
|
||
| @property | ||
| def state(self) -> int: | ||
| """Battery state percentage.""" | ||
| return self._device.battery_level | ||
|
|
||
| @property | ||
| def unit_of_measurement(self) -> str: | ||
| """Battery state measured in percentage.""" | ||
| return "%" | ||
|
|
||
| @property | ||
| def icon(self) -> str: | ||
| """Battery state icon handling.""" | ||
| return icon_for_battery_level( | ||
| battery_level=self._device.battery_level, | ||
| charging=self._device.battery_status == "Charging", | ||
| ) | ||
|
|
||
| @property | ||
| def device_state_attributes(self) -> Dict[str, any]: | ||
| """Return default attributes for the iCloud device entity.""" | ||
| return self._device.state_attributes | ||
|
|
||
| @property | ||
| def device_info(self) -> Dict[str, any]: | ||
| """Return the device information.""" | ||
| return { | ||
| "identifiers": {(DOMAIN, self._device.unique_id)}, | ||
| "name": self._device.name, | ||
| "manufacturer": "Apple", | ||
| "model": self._device.device_model, | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.