mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 20:57:21 +00:00
Refactor mysensors device tracker (#84747)
This commit is contained in:
parent
b2388b74d9
commit
df2d0cd3e3
@ -63,14 +63,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
hass.data[DOMAIN][MYSENSORS_GATEWAYS][entry.entry_id] = gateway
|
hass.data[DOMAIN][MYSENSORS_GATEWAYS][entry.entry_id] = gateway
|
||||||
|
|
||||||
# Connect notify discovery as that integration doesn't support entry forwarding.
|
# 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(
|
load_discovery_platform = partial(
|
||||||
async_load_platform,
|
async_load_platform,
|
||||||
hass,
|
hass,
|
||||||
platform,
|
Platform.NOTIFY,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
hass_config=hass.data[DOMAIN][DATA_HASS_CONFIG],
|
hass_config=hass.data[DOMAIN][DATA_HASS_CONFIG],
|
||||||
)
|
)
|
||||||
@ -80,7 +77,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
entry.entry_id,
|
entry.entry_id,
|
||||||
async_dispatcher_connect(
|
async_dispatcher_connect(
|
||||||
hass,
|
hass,
|
||||||
MYSENSORS_DISCOVERY.format(entry.entry_id, platform),
|
MYSENSORS_DISCOVERY.format(entry.entry_id, Platform.NOTIFY),
|
||||||
load_discovery_platform,
|
load_discovery_platform,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@ -163,5 +163,4 @@ for platform, platform_types in PLATFORM_TYPES.items():
|
|||||||
|
|
||||||
PLATFORMS_WITH_ENTRY_SUPPORT = set(PLATFORM_TYPES.keys()) - {
|
PLATFORMS_WITH_ENTRY_SUPPORT = set(PLATFORM_TYPES.keys()) - {
|
||||||
Platform.NOTIFY,
|
Platform.NOTIFY,
|
||||||
Platform.DEVICE_TRACKER,
|
|
||||||
}
|
}
|
||||||
|
@ -1,94 +1,76 @@
|
|||||||
"""Support for tracking MySensors devices."""
|
"""Support for tracking MySensors devices."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from typing import Any, cast
|
from homeassistant.components.device_tracker import SourceType, TrackerEntity
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.components.device_tracker import AsyncSeeCallback
|
|
||||||
from homeassistant.const import Platform
|
from homeassistant.const import Platform
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.util import slugify
|
|
||||||
|
|
||||||
from .. import mysensors
|
from . import setup_mysensors_platform
|
||||||
from .const import ATTR_GATEWAY_ID, DevId, DiscoveryInfo, GatewayId
|
from .const import MYSENSORS_DISCOVERY, DiscoveryInfo
|
||||||
|
from .device import MySensorsEntity
|
||||||
from .helpers import on_unload
|
from .helpers import on_unload
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_scanner(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config: ConfigType,
|
config_entry: ConfigEntry,
|
||||||
async_see: AsyncSeeCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
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:
|
) -> None:
|
||||||
"""Set up instance."""
|
"""Set up this platform for a specific ConfigEntry(==Gateway)."""
|
||||||
super().__init__(*args)
|
|
||||||
self.async_see = async_see
|
|
||||||
self.hass = hass
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_update_callback(self) -> None:
|
def async_discover(discovery_info: DiscoveryInfo) -> None:
|
||||||
"""Update the device."""
|
"""Discover and add a MySensors device tracker."""
|
||||||
self._async_update()
|
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]
|
node = self.gateway.sensors[self.node_id]
|
||||||
child = node.children[self.child_id]
|
child = node.children[self.child_id]
|
||||||
position = child.values[self.value_type]
|
position: str = child.values[self.value_type]
|
||||||
latitude, longitude, _ = position.split(",")
|
latitude, longitude, _ = position.split(",")
|
||||||
|
self._latitude = float(latitude)
|
||||||
self.hass.async_create_task(
|
self._longitude = float(longitude)
|
||||||
self.async_see(
|
|
||||||
dev_id=slugify(self.name),
|
|
||||||
host_name=self.name,
|
|
||||||
gps=(latitude, longitude),
|
|
||||||
battery=node.battery_level,
|
|
||||||
attributes=self._extra_attributes,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
@ -12,7 +12,6 @@ from mysensors.persistence import MySensorsJSONDecoder
|
|||||||
from mysensors.sensor import Sensor
|
from mysensors.sensor import Sensor
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.device_tracker.legacy import Device
|
|
||||||
from homeassistant.components.mqtt import DOMAIN as MQTT_DOMAIN
|
from homeassistant.components.mqtt import DOMAIN as MQTT_DOMAIN
|
||||||
from homeassistant.components.mysensors.config_flow import DEFAULT_BAUD_RATE
|
from homeassistant.components.mysensors.config_flow import DEFAULT_BAUD_RATE
|
||||||
from homeassistant.components.mysensors.const import (
|
from homeassistant.components.mysensors.const import (
|
||||||
@ -29,13 +28,6 @@ from homeassistant.setup import async_setup_component
|
|||||||
from tests.common import MockConfigEntry, load_fixture
|
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")
|
@pytest.fixture(name="mqtt")
|
||||||
def mock_mqtt_fixture(hass: HomeAssistant) -> None:
|
def mock_mqtt_fixture(hass: HomeAssistant) -> None:
|
||||||
"""Mock the MQTT integration."""
|
"""Mock the MQTT integration."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user