Skip to content

Commit 8793269

Browse files
authored
Add composite: tz_finder config option (pnbruckner#11)
Bump version to 2.0.0.
1 parent b3398d2 commit 8793269

File tree

5 files changed

+95
-21
lines changed

5 files changed

+95
-21
lines changed

README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ See [HACS](https://github.com/custom-components/hacs).
2828
Alternatively, place a copy of:
2929
3030
[`__init__.py`](custom_components/composite/__init__.py) at `<config>/custom_components/composite/__init__.py`
31+
[`const.py`](custom_components/composite/const.py) at `<config>/custom_components/composite/const.py`
3132
[`device_tracker.py`](custom_components/composite/device_tracker.py) at `<config>/custom_components/composite/device_tracker.py`
3233
[`manifest.json`](custom_components/composite/manifest.json) at `<config>/custom_components/composite/manifest.json`
3334

@@ -37,13 +38,21 @@ where `<config>` is your Home Assistant configuration directory.
3738

3839
### numpy on Raspberry Pi
3940

40-
To determine time zone from GPS coordinates (see `time_as` configuration variable below) the package [timezonefinderL](https://pypi.org/project/timezonefinderL/) is used. That package requires the package [numpy](https://pypi.org/project/numpy/). These will both be installed automatically by HA. Note, however, that numpy on Pi _usually_ requires libatlas to be installed. (See [this web page](https://www.raspberrypi.org/forums/viewtopic.php?t=207058) for more details.) It can be installed using this command:
41+
To determine time zone from GPS coordinates (see `time_as` configuration variable below) the package [timezonefinderL](https://pypi.org/project/timezonefinderL/) (by default) is used. That package requires the package [numpy](https://pypi.org/project/numpy/). These will both be installed automatically by HA. Note, however, that numpy on Pi _usually_ requires libatlas to be installed. (See [this web page](https://www.raspberrypi.org/forums/viewtopic.php?t=207058) for more details.) It can be installed using this command:
4142
```
4243
sudo apt install libatlas3-base
4344
```
4445
>Note: This is the same step that would be required if using a standard HA component that uses numpy (such as the [Trend Binary Sensor](https://www.home-assistant.io/components/binary_sensor.trend/)), and is only required if you use `device_or_utc` or `device_or_local` for `time_as`.
4546

4647
## Configuration variables
48+
### `composite` integration
49+
50+
- **tz_finder** (*Optional*): Specifies which `timezonefinder` package, and possibly version, to install. Must be formatted as required by `pip`. Default is `timezonefinderL==4.0.2`. Other common values:
51+
52+
`timezonefinderL==2.0.1`
53+
`timezonefinder`
54+
`timezonefinder==4.2.0`
55+
### `device_tracker` platform
4756

4857
- **entity_id**: Entity IDs of watched device tracker devices. Can be a single entity ID, a list of entity IDs, or a string containing multiple entity IDs separated by commas.
4958
- **name**: Object ID (i.e., part of entity ID after the dot) of composite device. For example, `NAME` would result in an entity ID of `device_tracker.NAME`.
@@ -84,6 +93,20 @@ source_type | Source of current location information: `binary_sensor`, `bluetoot
8493
time_zone | The name of the time zone in which the device is located, or `unknown` if it cannot be determined. Only exists if `device_or_utc` or `device_or_local` is chosen for `time_as`.
8594

8695
## Examples
96+
### Example Full Config
97+
```yaml
98+
composite:
99+
tz_finder: timezonefinderL==2.0.1
100+
device_tracker:
101+
- platform: composite
102+
name: me
103+
time_as: device_or_local
104+
require_movement: true
105+
entity_id:
106+
- device_tracker.platform1_me
107+
- device_tracker.platform2_me
108+
```
109+
87110
### Time zone examples
88111

89112
This example assumes `time_as` is set to `device_or_utc` or `device_or_local`. It determines the difference between the time zone in which the device is located and the `time_zone` in HA's configuration. A positive value means the device's time zone is ahead of (or later than, or east of) the local time zone.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,53 @@
11
"""Composite Device Tracker."""
2+
import asyncio
3+
import logging
4+
5+
import voluptuous as vol
6+
7+
from homeassistant.requirements import (
8+
async_process_requirements, RequirementsNotFound)
9+
from homeassistant.components.device_tracker import DOMAIN as DT_DOMAIN
10+
from homeassistant.const import CONF_PLATFORM
11+
import homeassistant.helpers.config_validation as cv
12+
13+
from .const import CONF_TIME_AS, DOMAIN, TZ_DEVICE_LOCAL, TZ_DEVICE_UTC
14+
15+
__version__ = '2.0.0'
16+
17+
CONF_TZ_FINDER = 'tz_finder'
18+
DEFAULT_TZ_FINDER = 'timezonefinderL==4.0.2'
19+
20+
CONFIG_SCHEMA = vol.Schema({
21+
vol.Optional(DOMAIN, default=dict): vol.Schema({
22+
vol.Optional(CONF_TZ_FINDER, default=DEFAULT_TZ_FINDER):
23+
cv.string,
24+
}),
25+
}, extra=vol.ALLOW_EXTRA)
26+
27+
_LOGGER = logging.getLogger(__name__)
28+
29+
30+
def setup(hass, config):
31+
if (any(conf[CONF_TIME_AS] in (TZ_DEVICE_UTC, TZ_DEVICE_LOCAL)
32+
for conf in (config.get(DT_DOMAIN) or [])
33+
if conf[CONF_PLATFORM] == DOMAIN)):
34+
pkg = config[DOMAIN][CONF_TZ_FINDER]
35+
try:
36+
asyncio.run_coroutine_threadsafe(
37+
async_process_requirements(
38+
hass, '{}.{}'.format(DOMAIN, DT_DOMAIN), [pkg]),
39+
hass.loop
40+
).result()
41+
except RequirementsNotFound:
42+
_LOGGER.debug('Process requirements failed: %s', pkg)
43+
return False
44+
else:
45+
_LOGGER.debug('Process requirements suceeded: %s', pkg)
46+
47+
if pkg.split('==')[0].strip().endswith('L'):
48+
from timezonefinderL import TimezoneFinder
49+
else:
50+
from timezonefinder import TimezoneFinder
51+
hass.data[DOMAIN] = TimezoneFinder()
52+
53+
return True
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""Constants for Composite Integration."""
2+
DOMAIN = 'composite'
3+
4+
CONF_REQ_MOVEMENT = 'require_movement'
5+
CONF_TIME_AS = 'time_as'
6+
7+
TZ_UTC = 'utc'
8+
TZ_LOCAL = 'local'
9+
TZ_DEVICE_UTC = 'device_or_utc'
10+
TZ_DEVICE_LOCAL = 'device_or_local'
11+
# First item in list is default.
12+
TIME_AS_OPTS = [TZ_UTC, TZ_LOCAL, TZ_DEVICE_UTC, TZ_DEVICE_LOCAL]

custom_components/composite/device_tracker.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,11 @@
2424
import homeassistant.util.dt as dt_util
2525
from homeassistant.util.location import distance
2626

27-
_LOGGER = logging.getLogger(__name__)
28-
29-
__version__ = '1.11.2'
27+
from .const import (
28+
CONF_REQ_MOVEMENT, CONF_TIME_AS, DOMAIN, TIME_AS_OPTS, TZ_DEVICE_LOCAL,
29+
TZ_DEVICE_UTC, TZ_LOCAL, TZ_UTC)
3030

31-
CONF_TIME_AS = 'time_as'
32-
CONF_REQ_MOVEMENT = 'require_movement'
33-
34-
TZ_UTC = 'utc'
35-
TZ_LOCAL = 'local'
36-
TZ_DEVICE_UTC = 'device_or_utc'
37-
TZ_DEVICE_LOCAL = 'device_or_local'
38-
# First item in list is default.
39-
TIME_AS_OPTS = [TZ_UTC, TZ_LOCAL, TZ_DEVICE_UTC, TZ_DEVICE_LOCAL]
31+
_LOGGER = logging.getLogger(__name__)
4032

4133
ATTR_CHARGING = 'charging'
4234
ATTR_LAST_SEEN = 'last_seen'
@@ -95,8 +87,7 @@ def __init__(self, hass, config, see):
9587
self._entity_id = ENTITY_ID_FORMAT.format(self._dev_id)
9688
self._time_as = config[CONF_TIME_AS]
9789
if self._time_as in [TZ_DEVICE_UTC, TZ_DEVICE_LOCAL]:
98-
from timezonefinderL import TimezoneFinder
99-
self._tf = TimezoneFinder()
90+
self._tf = hass.data[DOMAIN]
10091
self._req_movement = config[CONF_REQ_MOVEMENT]
10192
self._lock = threading.Lock()
10293
self._prev_seen = None

custom_components/composite/manifest.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
"domain": "composite",
33
"name": "Composite",
44
"documentation": "https://github.com/pnbruckner/homeassistant-config/blob/master/docs/composite.md",
5-
"requirements": [
6-
"timezonefinderL==4.0.2"
7-
],
5+
"requirements": [],
86
"dependencies": [],
9-
"codeowners": [
10-
"@pnbruckner"
11-
]
7+
"codeowners": ["@pnbruckner"]
128
}

0 commit comments

Comments
 (0)