Skip to content

Commit

Permalink
Import changes from core
Browse files Browse the repository at this point in the history
  • Loading branch information
postlund committed Nov 3, 2021
1 parent b3c1fd4 commit c7a50e2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
11 changes: 10 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
FROM python:3.7
FROM python:3.9

ARG USER=postlund
ARG UID=1000
ARG GID=1000
ARG PW=ha

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
Expand All @@ -16,6 +21,10 @@ RUN apt-get update \

#WORKDIR /

RUN useradd -m ${USER} --uid=${UID} && echo "${USER}:${PW}" | \
chpasswd
USER ${UID}:${GID}

# TODO: Should clone specific commit here
#RUN git clone --depth 1 https://github.com/home-assistant/home-assistant ha
WORKDIR /ha
Expand Down
7 changes: 4 additions & 3 deletions custom_components/apple_tv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ async def _connect(self, conf):
self._dispatch_send(SIGNAL_CONNECTED, self.atv)
self._address_updated(str(conf.address))

await self._async_setup_device_registry()
self._async_setup_device_registry()

self._connection_attempts = 0
if self._connection_was_lost:
Expand All @@ -317,7 +317,8 @@ async def _connect(self, conf):
)
self._connection_was_lost = False

async def _async_setup_device_registry(self):
@callback
def _async_setup_device_registry(self):
attrs = {
ATTR_IDENTIFIERS: {(DOMAIN, self.config_entry.unique_id)},
ATTR_MANUFACTURER: "Apple",
Expand All @@ -343,7 +344,7 @@ async def _async_setup_device_registry(self):
if dev_info.mac:
attrs[ATTR_CONNECTIONS] = {(dr.CONNECTION_NETWORK_MAC, dev_info.mac)}

device_registry = await dr.async_get_registry(self.hass)
device_registry = dr.async_get(self.hass)
device_registry.async_get_or_create(
config_entry_id=self.config_entry.entry_id, **attrs
)
Expand Down
19 changes: 11 additions & 8 deletions custom_components/apple_tv/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
import voluptuous as vol

from homeassistant import config_entries, data_entry_flow
from homeassistant.components.zeroconf import async_get_instance
from homeassistant.const import CONF_ADDRESS, CONF_NAME, CONF_PIN, CONF_TYPE
from homeassistant.components import zeroconf
from homeassistant.const import CONF_ADDRESS, CONF_NAME, CONF_PIN
from homeassistant.core import callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.typing import DiscoveryInfoType

from .const import CONF_CREDENTIALS, CONF_IDENTIFIERS, CONF_START_OFF, DOMAIN

Expand Down Expand Up @@ -128,7 +129,7 @@ async def async_step_user(self, user_input=None):
except DeviceNotFound:
errors["base"] = "no_devices_found"
except DeviceAlreadyConfigured:
errors["base"] = "already_configured_device"
errors["base"] = "already_configured"
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
Expand All @@ -145,11 +146,13 @@ async def async_step_user(self, user_input=None):
errors=errors,
)

async def async_step_zeroconf(self, discovery_info):
async def async_step_zeroconf(
self, discovery_info: DiscoveryInfoType
) -> data_entry_flow.FlowResult:
"""Handle device found via zeroconf."""
service_type = discovery_info[CONF_TYPE][:-1] # Remove leading .
name = discovery_info[CONF_NAME].replace(f".{service_type}.", "")
properties = discovery_info["properties"]
service_type = discovery_info[zeroconf.ATTR_TYPE][:-1] # Remove leading .
name = discovery_info[zeroconf.ATTR_NAME].replace(f".{service_type}.", "")
properties = discovery_info[zeroconf.ATTR_PROPERTIES]

# Extract unique identifier from service
self.scan_filter = get_unique_id(service_type, name, properties)
Expand Down Expand Up @@ -311,7 +314,7 @@ async def async_pair_next_protocol(self):
pair_args = {}
if self.protocol == Protocol.DMAP:
pair_args["name"] = "Home Assistant"
pair_args["zeroconf"] = await async_get_instance(self.hass)
pair_args["zeroconf"] = await zeroconf.async_get_instance(self.hass)

# Initiate the pairing process
abort_reason = None
Expand Down
25 changes: 13 additions & 12 deletions tests/apple_tv/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
from pyatv.const import PairingRequirement, Protocol
import pytest

from custom_components.apple_tv.const import CONF_START_OFF, DOMAIN
from homeassistant import config_entries, data_entry_flow
from homeassistant.components import zeroconf
from homeassistant.components.apple_tv.const import CONF_START_OFF, DOMAIN

from .common import airplay_service, create_conf, mrp_service

from tests.common import MockConfigEntry

DMAP_SERVICE = {
"type": "_touch-able._tcp.local.",
"name": "dmapid",
"properties": {"CtlN": "Apple TV"},
}
DMAP_SERVICE = zeroconf.HaServiceInfo(
type="_touch-able._tcp.local.",
name="dmapid._touch-able._tcp.local.",
properties={"CtlN": "Apple TV"},
)


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -239,7 +240,7 @@ async def test_user_adds_existing_device(hass, mrp_device):
{"device_input": "127.0.0.1"},
)
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result2["errors"] == {"base": "already_configured_device"}
assert result2["errors"] == {"base": "already_configured"}


async def test_user_connection_failed(hass, mrp_device, pairing_mock):
Expand Down Expand Up @@ -466,11 +467,11 @@ async def test_zeroconf_unsupported_service_aborts(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_ZEROCONF},
data={
"type": "_dummy._tcp.local.",
"name": "dummy",
"properties": {},
},
data=zeroconf.HaServiceInfo(
type="_dummy._tcp.local.",
name="test",
properties={},
),
)
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "unknown"
Expand Down

0 comments on commit c7a50e2

Please sign in to comment.