Skip to content

Commit

Permalink
feat: Add Integration service support
Browse files Browse the repository at this point in the history
BREAKING CHANGE: tracker_id is not longer supplied for service calls
  • Loading branch information
eifinger committed Oct 18, 2021
1 parent 9023901 commit c87c2df
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 173 deletions.
21 changes: 2 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,41 +30,24 @@ Set the tracker update interval.

Name | Description | Example
-- | -- | --
`tracker_id` | `The tracker id.` | `10000`
`update_interval` | `The update interval. Possible values are 30S 1M 5M 10M 30M 1H.` | `30M`

### weeenct.activate_super_live

Activate the super live mode.

Name | Description | Example
-- | -- | --
`tracker_id` | `The tracker id.` | `10000`

### weeenct.refresh_location

Request a location update.

Name | Description | Example
-- | -- | --
`tracker_id` | `The tracker id.` | `10000`

### weeenct.ring

Let the tracker ring.

Name | Description | Example
-- | -- | --
`tracker_id` | `The tracker id.` | `10000`

### weeenct.vibrate

Let the tracker vibrate.

Name | Description | Example
-- | -- | --
`tracker_id` | `The tracker id.` | `10000`

## Installation

### HACS
Expand Down Expand Up @@ -117,7 +100,7 @@ automation:
action:
- service: weenect.set_update_interval
data:
tracker_id: !secret naya_tracker_id
entity_id: device_tracker.naya
update_interval: "1M"
- id: 652b4b69-c951-4861-8b7d-3cbb15fc8b79
alias: "Setze Nayas Tracker Updaterate auf 60M wenn wir zu Hause sind"
Expand All @@ -132,7 +115,7 @@ automation:
action:
- service: weenect.set_update_interval
data:
tracker_id: !secret naya_tracker_id
entity_id: device_tracker.naya
update_interval: "60M"
````

Expand Down
8 changes: 1 addition & 7 deletions custom_components/weenect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,17 @@
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .util import parse_duration

from .const import (
CONF_PASSWORD,
CONF_UPDATE_RATE,
CONF_USERNAME,
DEFAULT_UPDATE_RATE,
DOMAIN,
PLATFORMS,
STARTUP_MESSAGE,
TRACKER_ADDED,
)
from .services import async_setup_services, async_unload_services
from .util import parse_duration

_LOGGER: logging.Logger = logging.getLogger(__package__)

Expand All @@ -37,7 +35,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up this integration using UI."""
if hass.data.get(DOMAIN) is None:
hass.data.setdefault(DOMAIN, {})
await async_setup_services(hass)
_LOGGER.info(STARTUP_MESSAGE)

username = entry.data.get(CONF_USERNAME)
Expand Down Expand Up @@ -133,9 +130,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if unloaded:
hass.data[DOMAIN].pop(entry.entry_id)

# If there is no instance of this integration registered anymore
if not hass.data[DOMAIN]:
await async_unload_services(hass)
return unloaded


Expand Down
6 changes: 1 addition & 5 deletions custom_components/weenect/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
from homeassistant.helpers.aiohttp_client import async_create_clientsession
import voluptuous as vol

from .const import (
CONF_PASSWORD,
CONF_USERNAME,
DOMAIN,
)
from .const import CONF_PASSWORD, CONF_USERNAME, DOMAIN

_LOGGER: logging.Logger = logging.getLogger(__package__)

Expand Down
90 changes: 83 additions & 7 deletions custom_components/weenect/device_tracker.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
"""Device tracker platform for weenect."""
import logging
from typing import List

from homeassistant.components.device_tracker import SOURCE_TYPE_GPS
from homeassistant.components.device_tracker.config_entry import TrackerEntity
from homeassistant.core import callback
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.helpers import entity_platform
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from custom_components.weenect.services import (
SERVICE_ACTIVATE_SUPER_LIVE,
SERVICE_REFRESH_LOCATION,
SERVICE_RING,
SERVICE_SCHEMA,
SERVICE_SET_UPDATE_INTERVAL,
SERVICE_SET_UPDATE_INTERVAL_SCHEMA,
SERVICE_VIBRATE,
UPDATE_INTERVAL,
async_activate_super_live,
async_refresh_location,
async_ring,
async_set_update_interval,
async_vibrate,
)

from .const import DOMAIN, TRACKER_ADDED
from .entity import WeenectEntity

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the weenect device_trackers."""

coordinator = hass.data[DOMAIN][config_entry.entry_id]
platform = entity_platform.async_get_current_platform()

coordinator = hass.data[DOMAIN][entry.entry_id]

@callback
def async_add_device_trackers(
Expand All @@ -37,13 +59,67 @@ def async_add_device_trackers(

unsub_dispatcher = async_dispatcher_connect(
hass,
f"{config_entry.entry_id}_{TRACKER_ADDED}",
f"{entry.entry_id}_{TRACKER_ADDED}",
async_add_device_trackers,
)
coordinator.unsub_dispatchers.append(unsub_dispatcher)
if len(coordinator.data) > 0:
async_add_device_trackers(coordinator.data.keys())

async def async_call_service(service_call: ServiceCall) -> None:
"""Handle dispatched services."""
assert platform is not None
entities = await platform.async_extract_from_service(service_call)

tracker_ids = []
for entity in entities:
assert isinstance(entity, WeenectEntity)
tracker_ids.append(entity.id)
for tracker_id in set(tracker_ids):
if service_call.service == SERVICE_SET_UPDATE_INTERVAL:
await async_set_update_interval(
hass, tracker_id, service_call.data[UPDATE_INTERVAL]
)
if service_call.service == SERVICE_ACTIVATE_SUPER_LIVE:
await async_activate_super_live(hass, tracker_id)
if service_call.service == SERVICE_REFRESH_LOCATION:
await async_refresh_location(hass, tracker_id)
if service_call.service == SERVICE_RING:
await async_ring(hass, tracker_id)
if service_call.service == SERVICE_VIBRATE:
await async_vibrate(hass, tracker_id)

hass.services.async_register(
DOMAIN,
SERVICE_SET_UPDATE_INTERVAL,
async_call_service,
schema=SERVICE_SET_UPDATE_INTERVAL_SCHEMA,
)
hass.services.async_register(
DOMAIN,
SERVICE_ACTIVATE_SUPER_LIVE,
async_call_service,
schema=SERVICE_SCHEMA,
)
hass.services.async_register(
DOMAIN,
SERVICE_REFRESH_LOCATION,
async_call_service,
schema=SERVICE_SCHEMA,
)
hass.services.async_register(
DOMAIN,
SERVICE_RING,
async_call_service,
schema=SERVICE_SCHEMA,
)
hass.services.async_register(
DOMAIN,
SERVICE_VIBRATE,
async_call_service,
schema=SERVICE_SCHEMA,
)


class WeenectDeviceTracker(WeenectEntity, TrackerEntity):
"""weenect device tracker."""
Expand Down
17 changes: 10 additions & 7 deletions custom_components/weenect/sensor.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
"""Sensor platform for weenect."""
import logging
from typing import Any, Dict, List

from homeassistant.core import callback
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

from .const import DOMAIN, LOCATION_SENSOR_TYPES, SENSOR_TYPES, TRACKER_ADDED
from .entity import WeenectEntity

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the weenect sensors."""

coordinator = hass.data[DOMAIN][config_entry.entry_id]
coordinator = hass.data[DOMAIN][entry.entry_id]

@callback
def async_add_sensors(
Expand All @@ -36,7 +39,7 @@ def async_add_sensors(

unsub_dispatcher = async_dispatcher_connect(
hass,
f"{config_entry.entry_id}_{TRACKER_ADDED}",
f"{entry.entry_id}_{TRACKER_ADDED}",
async_add_sensors,
)
coordinator.unsub_dispatchers.append(unsub_dispatcher)
Expand Down
Loading

0 comments on commit c87c2df

Please sign in to comment.