Skip to content

Commit

Permalink
Add Life360 Location Update Button (#99559)
Browse files Browse the repository at this point in the history
Co-authored-by: Robert Resch <[email protected]>
Co-authored-by: alexyao2015 <[email protected]>
  • Loading branch information
3 people authored Sep 12, 2023
1 parent da13afb commit 5ba573a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ omit =
homeassistant/components/lg_soundbar/__init__.py
homeassistant/components/lg_soundbar/media_player.py
homeassistant/components/life360/__init__.py
homeassistant/components/life360/button.py
homeassistant/components/life360/coordinator.py
homeassistant/components/life360/device_tracker.py
homeassistant/components/lightwave/*
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/life360/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
)
from .coordinator import Life360DataUpdateCoordinator, MissingLocReason

PLATFORMS = [Platform.DEVICE_TRACKER]
PLATFORMS = [Platform.DEVICE_TRACKER, Platform.BUTTON]

CONF_ACCOUNTS = "accounts"

Expand Down
56 changes: 56 additions & 0 deletions homeassistant/components/life360/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""Support for Life360 buttons."""
from homeassistant.components.button import ButtonEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from . import Life360DataUpdateCoordinator
from .const import DOMAIN


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Life360 buttons."""
coordinator: Life360DataUpdateCoordinator = hass.data[DOMAIN].coordinators[
config_entry.entry_id
]
for member_id, member in coordinator.data.members.items():
async_add_entities(
[
Life360UpdateLocationButton(coordinator, member.circle_id, member_id),
]
)


class Life360UpdateLocationButton(
CoordinatorEntity[Life360DataUpdateCoordinator], ButtonEntity
):
"""Represent an Life360 Update Location button."""

_attr_has_entity_name = True
_attr_translation_key = "update_location"

def __init__(
self,
coordinator: Life360DataUpdateCoordinator,
circle_id: str,
member_id: str,
) -> None:
"""Initialize a new Life360 Update Location button."""
super().__init__(coordinator)
self._circle_id = circle_id
self._member_id = member_id
self._attr_unique_id = f"{member_id}-update-location"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, member_id)},
name=coordinator.data.members[member_id].name,
)

async def async_press(self) -> None:
"""Handle the button press."""
await self.coordinator.update_location(self._circle_id, self._member_id)
6 changes: 6 additions & 0 deletions homeassistant/components/life360/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Life360Member:
at_loc_since: datetime
battery_charging: bool
battery_level: int
circle_id: str
driving: bool
entity_picture: str
gps_accuracy: int
Expand Down Expand Up @@ -118,6 +119,10 @@ async def _retrieve_data(self, func: str, *args: Any) -> list[dict[str, Any]]:
LOGGER.debug("%s: %s", exc.__class__.__name__, exc)
raise UpdateFailed(exc) from exc

async def update_location(self, circle_id: str, member_id: str) -> None:
"""Update location for given Circle and Member."""
await self._retrieve_data("update_location", circle_id, member_id)

async def _async_update_data(self) -> Life360Data:
"""Get & process data from Life360."""

Expand Down Expand Up @@ -214,6 +219,7 @@ async def _async_update_data(self) -> Life360Data:
dt_util.utc_from_timestamp(int(loc["since"])),
bool(int(loc["charge"])),
int(float(loc["battery"])),
circle_id,
bool(int(loc["isDriving"])),
member["avatar"],
# Life360 reports accuracy in feet, but Device Tracker expects
Expand Down
7 changes: 7 additions & 0 deletions homeassistant/components/life360/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
}
},
"entity": {
"button": {
"update_location": {
"name": "Update Location"
}
}
},
"options": {
"step": {
"init": {
Expand Down

0 comments on commit 5ba573a

Please sign in to comment.