mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 01:38:02 +00:00
Refactor mysensors device tracker (#84747)
This commit is contained in:
parent
b2388b74d9
commit
df2d0cd3e3
@ -63,27 +63,24 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
hass.data[DOMAIN][MYSENSORS_GATEWAYS][entry.entry_id] = gateway
|
||||
|
||||
# Connect notify discovery as that integration doesn't support entry forwarding.
|
||||
# Allow loading device tracker platform via discovery
|
||||
# until refactor to config entry is done.
|
||||
|
||||
for platform in (Platform.DEVICE_TRACKER, Platform.NOTIFY):
|
||||
load_discovery_platform = partial(
|
||||
async_load_platform,
|
||||
hass,
|
||||
platform,
|
||||
DOMAIN,
|
||||
hass_config=hass.data[DOMAIN][DATA_HASS_CONFIG],
|
||||
)
|
||||
load_discovery_platform = partial(
|
||||
async_load_platform,
|
||||
hass,
|
||||
Platform.NOTIFY,
|
||||
DOMAIN,
|
||||
hass_config=hass.data[DOMAIN][DATA_HASS_CONFIG],
|
||||
)
|
||||
|
||||
on_unload(
|
||||
on_unload(
|
||||
hass,
|
||||
entry.entry_id,
|
||||
async_dispatcher_connect(
|
||||
hass,
|
||||
entry.entry_id,
|
||||
async_dispatcher_connect(
|
||||
hass,
|
||||
MYSENSORS_DISCOVERY.format(entry.entry_id, platform),
|
||||
load_discovery_platform,
|
||||
),
|
||||
)
|
||||
MYSENSORS_DISCOVERY.format(entry.entry_id, Platform.NOTIFY),
|
||||
load_discovery_platform,
|
||||
),
|
||||
)
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(
|
||||
entry, PLATFORMS_WITH_ENTRY_SUPPORT
|
||||
|
@ -163,5 +163,4 @@ for platform, platform_types in PLATFORM_TYPES.items():
|
||||
|
||||
PLATFORMS_WITH_ENTRY_SUPPORT = set(PLATFORM_TYPES.keys()) - {
|
||||
Platform.NOTIFY,
|
||||
Platform.DEVICE_TRACKER,
|
||||
}
|
||||
|
@ -1,94 +1,76 @@
|
||||
"""Support for tracking MySensors devices."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, cast
|
||||
|
||||
from homeassistant.components.device_tracker import AsyncSeeCallback
|
||||
from homeassistant.components.device_tracker import SourceType, TrackerEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
from homeassistant.util import slugify
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .. import mysensors
|
||||
from .const import ATTR_GATEWAY_ID, DevId, DiscoveryInfo, GatewayId
|
||||
from . import setup_mysensors_platform
|
||||
from .const import MYSENSORS_DISCOVERY, DiscoveryInfo
|
||||
from .device import MySensorsEntity
|
||||
from .helpers import on_unload
|
||||
|
||||
|
||||
async def async_setup_scanner(
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
async_see: AsyncSeeCallback,
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> bool:
|
||||
"""Set up the MySensors device scanner."""
|
||||
if not discovery_info:
|
||||
return False
|
||||
|
||||
new_devices = mysensors.setup_mysensors_platform(
|
||||
hass,
|
||||
Platform.DEVICE_TRACKER,
|
||||
cast(DiscoveryInfo, discovery_info),
|
||||
MySensorsDeviceScanner,
|
||||
device_args=(hass, async_see),
|
||||
)
|
||||
if not new_devices:
|
||||
return False
|
||||
|
||||
for device in new_devices:
|
||||
gateway_id: GatewayId = discovery_info[ATTR_GATEWAY_ID]
|
||||
dev_id: DevId = (gateway_id, device.node_id, device.child_id, device.value_type)
|
||||
on_unload(
|
||||
hass,
|
||||
gateway_id,
|
||||
async_dispatcher_connect(
|
||||
hass,
|
||||
mysensors.const.CHILD_CALLBACK.format(*dev_id),
|
||||
device.async_update_callback,
|
||||
),
|
||||
)
|
||||
on_unload(
|
||||
hass,
|
||||
gateway_id,
|
||||
async_dispatcher_connect(
|
||||
hass,
|
||||
mysensors.const.NODE_CALLBACK.format(gateway_id, device.node_id),
|
||||
device.async_update_callback,
|
||||
),
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
class MySensorsDeviceScanner(mysensors.device.MySensorsDevice):
|
||||
"""Represent a MySensors scanner."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
async_see: AsyncSeeCallback,
|
||||
*args: Any,
|
||||
) -> None:
|
||||
"""Set up instance."""
|
||||
super().__init__(*args)
|
||||
self.async_see = async_see
|
||||
self.hass = hass
|
||||
config_entry: ConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up this platform for a specific ConfigEntry(==Gateway)."""
|
||||
|
||||
@callback
|
||||
def _async_update_callback(self) -> None:
|
||||
"""Update the device."""
|
||||
self._async_update()
|
||||
def async_discover(discovery_info: DiscoveryInfo) -> None:
|
||||
"""Discover and add a MySensors device tracker."""
|
||||
setup_mysensors_platform(
|
||||
hass,
|
||||
Platform.DEVICE_TRACKER,
|
||||
discovery_info,
|
||||
MySensorsDeviceTracker,
|
||||
async_add_entities=async_add_entities,
|
||||
)
|
||||
|
||||
on_unload(
|
||||
hass,
|
||||
config_entry.entry_id,
|
||||
async_dispatcher_connect(
|
||||
hass,
|
||||
MYSENSORS_DISCOVERY.format(config_entry.entry_id, Platform.DEVICE_TRACKER),
|
||||
async_discover,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class MySensorsDeviceTracker(MySensorsEntity, TrackerEntity):
|
||||
"""Represent a MySensors device tracker."""
|
||||
|
||||
_latitude: float | None = None
|
||||
_longitude: float | None = None
|
||||
|
||||
@property
|
||||
def latitude(self) -> float | None:
|
||||
"""Return latitude value of the device."""
|
||||
return self._latitude
|
||||
|
||||
@property
|
||||
def longitude(self) -> float | None:
|
||||
"""Return longitude value of the device."""
|
||||
return self._longitude
|
||||
|
||||
@property
|
||||
def source_type(self) -> SourceType:
|
||||
"""Return the source type of the device."""
|
||||
return SourceType.GPS
|
||||
|
||||
@callback
|
||||
def _async_update(self) -> None:
|
||||
"""Update the controller with the latest value from a device."""
|
||||
super()._async_update()
|
||||
node = self.gateway.sensors[self.node_id]
|
||||
child = node.children[self.child_id]
|
||||
position = child.values[self.value_type]
|
||||
position: str = child.values[self.value_type]
|
||||
latitude, longitude, _ = position.split(",")
|
||||
|
||||
self.hass.async_create_task(
|
||||
self.async_see(
|
||||
dev_id=slugify(self.name),
|
||||
host_name=self.name,
|
||||
gps=(latitude, longitude),
|
||||
battery=node.battery_level,
|
||||
attributes=self._extra_attributes,
|
||||
)
|
||||
)
|
||||
self._latitude = float(latitude)
|
||||
self._longitude = float(longitude)
|
||||
|
@ -12,7 +12,6 @@ from mysensors.persistence import MySensorsJSONDecoder
|
||||
from mysensors.sensor import Sensor
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.device_tracker.legacy import Device
|
||||
from homeassistant.components.mqtt import DOMAIN as MQTT_DOMAIN
|
||||
from homeassistant.components.mysensors.config_flow import DEFAULT_BAUD_RATE
|
||||
from homeassistant.components.mysensors.const import (
|
||||
@ -29,13 +28,6 @@ from homeassistant.setup import async_setup_component
|
||||
from tests.common import MockConfigEntry, load_fixture
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def device_tracker_storage(mock_device_tracker_conf: list[Device]) -> list[Device]:
|
||||
"""Mock out device tracker known devices storage."""
|
||||
devices = mock_device_tracker_conf
|
||||
return devices
|
||||
|
||||
|
||||
@pytest.fixture(name="mqtt")
|
||||
def mock_mqtt_fixture(hass: HomeAssistant) -> None:
|
||||
"""Mock the MQTT integration."""
|
||||
|
Loading…
x
Reference in New Issue
Block a user