* Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
131 lines
4.3 KiB
Python
131 lines
4.3 KiB
Python
"""
|
|
Support for the Meraki CMX location service.
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
https://home-assistant.io/components/device_tracker.meraki/
|
|
|
|
"""
|
|
import logging
|
|
import json
|
|
|
|
import voluptuous as vol
|
|
import homeassistant.helpers.config_validation as cv
|
|
from homeassistant.const import (HTTP_BAD_REQUEST, HTTP_UNPROCESSABLE_ENTITY)
|
|
from homeassistant.core import callback
|
|
from homeassistant.components.http import HomeAssistantView
|
|
from homeassistant.components.device_tracker import (
|
|
PLATFORM_SCHEMA, SOURCE_TYPE_ROUTER)
|
|
|
|
CONF_VALIDATOR = 'validator'
|
|
CONF_SECRET = 'secret'
|
|
DEPENDENCIES = ['http']
|
|
URL = '/api/meraki'
|
|
VERSION = '2.0'
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
vol.Required(CONF_VALIDATOR): cv.string,
|
|
vol.Required(CONF_SECRET): cv.string
|
|
})
|
|
|
|
|
|
async def async_setup_scanner(hass, config, async_see, discovery_info=None):
|
|
"""Set up an endpoint for the Meraki tracker."""
|
|
hass.http.register_view(
|
|
MerakiView(config, async_see))
|
|
|
|
return True
|
|
|
|
|
|
class MerakiView(HomeAssistantView):
|
|
"""View to handle Meraki requests."""
|
|
|
|
url = URL
|
|
name = 'api:meraki'
|
|
|
|
def __init__(self, config, async_see):
|
|
"""Initialize Meraki URL endpoints."""
|
|
self.async_see = async_see
|
|
self.validator = config[CONF_VALIDATOR]
|
|
self.secret = config[CONF_SECRET]
|
|
|
|
async def get(self, request):
|
|
"""Meraki message received as GET."""
|
|
return self.validator
|
|
|
|
async def post(self, request):
|
|
"""Meraki CMX message received."""
|
|
try:
|
|
data = await request.json()
|
|
except ValueError:
|
|
return self.json_message('Invalid JSON', HTTP_BAD_REQUEST)
|
|
_LOGGER.debug("Meraki Data from Post: %s", json.dumps(data))
|
|
if not data.get('secret', False):
|
|
_LOGGER.error("secret invalid")
|
|
return self.json_message('No secret', HTTP_UNPROCESSABLE_ENTITY)
|
|
if data['secret'] != self.secret:
|
|
_LOGGER.error("Invalid Secret received from Meraki")
|
|
return self.json_message('Invalid secret',
|
|
HTTP_UNPROCESSABLE_ENTITY)
|
|
if data['version'] != VERSION:
|
|
_LOGGER.error("Invalid API version: %s", data['version'])
|
|
return self.json_message('Invalid version',
|
|
HTTP_UNPROCESSABLE_ENTITY)
|
|
_LOGGER.debug('Valid Secret')
|
|
if data['type'] not in ('DevicesSeen', 'BluetoothDevicesSeen'):
|
|
_LOGGER.error("Unknown Device %s", data['type'])
|
|
return self.json_message('Invalid device type',
|
|
HTTP_UNPROCESSABLE_ENTITY)
|
|
_LOGGER.debug("Processing %s", data['type'])
|
|
if not data["data"]["observations"]:
|
|
_LOGGER.debug("No observations found")
|
|
return
|
|
self._handle(request.app['hass'], data)
|
|
|
|
@callback
|
|
def _handle(self, hass, data):
|
|
for i in data["data"]["observations"]:
|
|
data["data"]["secret"] = "hidden"
|
|
|
|
lat = i["location"]["lat"]
|
|
lng = i["location"]["lng"]
|
|
try:
|
|
accuracy = int(float(i["location"]["unc"]))
|
|
except ValueError:
|
|
accuracy = 0
|
|
|
|
mac = i["clientMac"]
|
|
_LOGGER.debug("clientMac: %s", mac)
|
|
|
|
if lat == "NaN" or lng == "NaN":
|
|
_LOGGER.debug(
|
|
"No coordinates received, skipping location for: %s", mac)
|
|
gps_location = None
|
|
accuracy = None
|
|
else:
|
|
gps_location = (lat, lng)
|
|
|
|
attrs = {}
|
|
if i.get('os', False):
|
|
attrs['os'] = i['os']
|
|
if i.get('manufacturer', False):
|
|
attrs['manufacturer'] = i['manufacturer']
|
|
if i.get('ipv4', False):
|
|
attrs['ipv4'] = i['ipv4']
|
|
if i.get('ipv6', False):
|
|
attrs['ipv6'] = i['ipv6']
|
|
if i.get('seenTime', False):
|
|
attrs['seenTime'] = i['seenTime']
|
|
if i.get('ssid', False):
|
|
attrs['ssid'] = i['ssid']
|
|
hass.async_create_task(self.async_see(
|
|
gps=gps_location,
|
|
mac=mac,
|
|
source_type=SOURCE_TYPE_ROUTER,
|
|
gps_accuracy=accuracy,
|
|
attributes=attrs
|
|
))
|