Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Snooz BLE devices #78790

Merged
merged 42 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
cbdaeea
Add Snooz component
AustinBrunkhorst Sep 19, 2022
6876c3d
Merge branch 'dev' into snooz
AustinBrunkhorst Sep 28, 2022
f0e46f9
Remove sensor entities, trim changes
AustinBrunkhorst Oct 1, 2022
14e8182
Update pysnooz to 0.7.8
AustinBrunkhorst Oct 1, 2022
c5021f9
Merge branch 'dev' into snooz
AustinBrunkhorst Oct 1, 2022
095e703
Remove linter comment
AustinBrunkhorst Oct 1, 2022
6f2fbcb
Remove services
AustinBrunkhorst Oct 1, 2022
b7c7b45
Remove service tests
AustinBrunkhorst Oct 1, 2022
34e2744
Remove public disconnect service method
AustinBrunkhorst Oct 1, 2022
cf55689
Raise HomeAssistantError instead of updating attribute
AustinBrunkhorst Oct 2, 2022
77f4c76
Remove loop from Event
AustinBrunkhorst Oct 2, 2022
f3c6cbb
Update homeassistant/components/snooz/fan.py
AustinBrunkhorst Oct 2, 2022
c47bd6e
Update homeassistant/components/snooz/fan.py
AustinBrunkhorst Oct 2, 2022
b287192
PR feedback
AustinBrunkhorst Oct 2, 2022
d141270
Remove attribute from tests
AustinBrunkhorst Oct 2, 2022
3e8e93d
Merge branch 'snooz' of https://github.com/AustinBrunkhorst/core into…
AustinBrunkhorst Oct 2, 2022
a5d4471
Add callback import
AustinBrunkhorst Oct 2, 2022
4298f55
Merge branch 'home-assistant:dev' into snooz
AustinBrunkhorst Oct 6, 2022
1a274ae
Update homeassistant/components/snooz/fan.py
AustinBrunkhorst Oct 7, 2022
54f1cff
PR feedback
AustinBrunkhorst Oct 7, 2022
23154a5
Update homeassistant/components/snooz/config_flow.py
AustinBrunkhorst Oct 7, 2022
78cef5a
PR feedback
AustinBrunkhorst Oct 7, 2022
6900d23
PR feedback
AustinBrunkhorst Oct 7, 2022
e537d28
PR feedback
AustinBrunkhorst Oct 7, 2022
a23b88b
Update tests
AustinBrunkhorst Oct 7, 2022
d395cca
PR feedback
AustinBrunkhorst Oct 8, 2022
7aeb167
Make cancelled command a warning
AustinBrunkhorst Oct 8, 2022
5ab8d98
Update homeassistant/components/snooz/fan.py
AustinBrunkhorst Oct 8, 2022
616ef52
Remove log for cancelled command
AustinBrunkhorst Oct 8, 2022
b72fe81
Add test coverage
AustinBrunkhorst Oct 8, 2022
14c8ad7
Update .coveragerc
AustinBrunkhorst Oct 8, 2022
85daa02
Fix flake8 lint
AustinBrunkhorst Oct 8, 2022
e7dfbdb
Fix 0 percentage turn off
AustinBrunkhorst Oct 9, 2022
8efead9
Update homeassistant/components/snooz/fan.py
AustinBrunkhorst Oct 9, 2022
c76ddca
Update homeassistant/components/snooz/fan.py
AustinBrunkhorst Oct 9, 2022
c055f97
Update homeassistant/components/snooz/fan.py
AustinBrunkhorst Oct 9, 2022
0e7531b
Update translations
AustinBrunkhorst Oct 9, 2022
f2be643
Update tests
AustinBrunkhorst Oct 9, 2022
844c127
Update .strict-typing
AustinBrunkhorst Oct 9, 2022
1dcf1a3
Merge with upstream/dev
AustinBrunkhorst Oct 9, 2022
168978a
Update tests
AustinBrunkhorst Oct 10, 2022
35cd35e
Update pysnooz to 0.8.2
AustinBrunkhorst Oct 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove sensor entities, trim changes
  • Loading branch information
AustinBrunkhorst committed Oct 1, 2022
commit f0e46f91b5bb5d6b19dd3f21e260b28aa2d1403f
36 changes: 6 additions & 30 deletions homeassistant/components/snooz/__init__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
"""The Snooz component."""
from __future__ import annotations

import logging

from pysnooz.advertisement import SnoozAdvertisementData
from pysnooz.device import SnoozDevice

from homeassistant.components.bluetooth import (
BluetoothScanningMode,
async_ble_device_from_address,
)
from homeassistant.components.bluetooth.passive_update_processor import (
PassiveBluetoothProcessorCoordinator,
)
from homeassistant.components.bluetooth import async_ble_device_from_address
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ADDRESS, CONF_TOKEN, Platform
from homeassistant.core import HomeAssistant
Expand All @@ -21,41 +12,26 @@
from .const import DOMAIN
from .models import SnoozConfigurationData

PLATFORMS: list[Platform] = [Platform.FAN, Platform.SENSOR]

_LOGGER = logging.getLogger(__name__)
PLATFORMS: list[Platform] = [Platform.FAN]
AustinBrunkhorst marked this conversation as resolved.
Show resolved Hide resolved


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Snooz device from a config entry."""
address = entry.data.get(CONF_ADDRESS)
assert address

token = entry.data.get(CONF_TOKEN)
assert token
address: str = entry.data[CONF_ADDRESS]
token: str = entry.data[CONF_TOKEN]

if not (ble_device := async_ble_device_from_address(hass, address.upper())):
if not (ble_device := async_ble_device_from_address(hass, address)):
raise ConfigEntryNotReady(
f"Could not find Snooz with address {address}. Try power cycling the device"
)

data = SnoozAdvertisementData()
coordinator = PassiveBluetoothProcessorCoordinator(
hass,
_LOGGER,
address=address,
mode=BluetoothScanningMode.ACTIVE,
update_method=data.update,
)

device = SnoozDevice(ble_device, token, hass.loop)
AustinBrunkhorst marked this conversation as resolved.
Show resolved Hide resolved

hass.data.setdefault(DOMAIN, {})[entry.entry_id] = SnoozConfigurationData(
ble_device, device, coordinator
ble_device, device
)

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(coordinator.async_start())

return True

Expand Down
35 changes: 15 additions & 20 deletions homeassistant/components/snooz/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import asyncio
from typing import Any

from pysnooz.advertisement import SnoozAdvertisementData, get_snooz_display_name
from pysnooz.advertisement import SnoozAdvertisementData
import voluptuous as vol

from homeassistant.components.bluetooth import (
Expand All @@ -23,7 +23,7 @@
WAIT_FOR_PAIRING_TIMEOUT = 30


class DeviceDiscovery:
class DiscoveredSnooz:
AustinBrunkhorst marked this conversation as resolved.
Show resolved Hide resolved
"""Represents a discovered Snooz device."""

info: BluetoothServiceInfo
Expand All @@ -44,8 +44,8 @@ class SnoozConfigFlow(ConfigFlow, domain=DOMAIN):

def __init__(self) -> None:
"""Initialize the config flow."""
self._discovery: DeviceDiscovery | None = None
self._discovered_devices: dict[str, DeviceDiscovery] = {}
self._discovery: DiscoveredSnooz | None = None
self._discovered_devices: dict[str, DiscoveredSnooz] = {}
self._pairing_task: asyncio.Task | None = None

async def async_step_bluetooth(
Expand All @@ -57,7 +57,7 @@ async def async_step_bluetooth(
device = SnoozAdvertisementData()
if not device.supported(discovery_info):
return self.async_abort(reason="not_supported")
self._discovery = DeviceDiscovery(discovery_info, device)
self._discovery = DiscoveredSnooz(discovery_info, device)
return await self.async_step_bluetooth_confirm()

async def async_step_bluetooth_confirm(
Expand All @@ -73,11 +73,8 @@ async def async_step_bluetooth_confirm(
return self._create_snooz_entry(self._discovery)

self._set_confirm_only()
placeholders = {
"name": get_snooz_display_name(
self._discovery.device.title or "Snooz", self._discovery.info.address
)
}
assert self._discovery.device.display_name
placeholders = {"name": self._discovery.device.display_name}
self.context["title_placeholders"] = placeholders
return self.async_show_form(
step_id="bluetooth_confirm", description_placeholders=placeholders
Expand Down Expand Up @@ -112,9 +109,10 @@ async def async_step_user(
continue
device = SnoozAdvertisementData()
if device.supported(info):
self._discovered_devices[
get_snooz_display_name(device.title or "Snooz", info.address)
] = DeviceDiscovery(info, device)
assert device.display_name
self._discovered_devices[device.display_name] = DiscoveredSnooz(
info, device
)

if not self._discovered_devices:
return self.async_abort(reason="no_devices_found")
Expand All @@ -125,9 +123,7 @@ async def async_step_user(
{
vol.Required(CONF_NAME): vol.In(
[
get_snooz_display_name(
d.device.title or "Snooz", d.info.address
)
d.device.display_name
for d in self._discovered_devices.values()
]
)
Expand Down Expand Up @@ -181,11 +177,10 @@ async def async_step_pairing_timeout(
self._set_confirm_only()
return self.async_show_form(step_id="pairing_timeout")

def _create_snooz_entry(self, discovery: DeviceDiscovery) -> FlowResult:
def _create_snooz_entry(self, discovery: DiscoveredSnooz) -> FlowResult:
assert discovery.device.display_name
return self.async_create_entry(
title=get_snooz_display_name(
discovery.device.title or "Snooz", discovery.info.address
),
title=discovery.device.display_name,
data={
CONF_ADDRESS: discovery.info.address,
CONF_TOKEN: discovery.device.pairing_token,
Expand Down
12 changes: 1 addition & 11 deletions homeassistant/components/snooz/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,11 @@
from bleak.backends.device import BLEDevice
from pysnooz.device import SnoozDevice

from homeassistant.components.bluetooth.passive_update_processor import (
PassiveBluetoothProcessorCoordinator,
)


class SnoozConfigurationData:
"""Configuration data for Snooz."""

def __init__(
self,
ble_device: BLEDevice,
device: SnoozDevice,
coordinator: PassiveBluetoothProcessorCoordinator,
) -> None:
def __init__(self, ble_device: BLEDevice, device: SnoozDevice) -> None:
"""Initialize configuration data."""
self.ble_device = ble_device
self.device = device
self.coordinator = coordinator
174 changes: 0 additions & 174 deletions homeassistant/components/snooz/sensor.py

This file was deleted.

9 changes: 5 additions & 4 deletions homeassistant/components/snooz/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
"abort": {
"already_configured": "Device is already configured",
"already_in_progress": "Configuration flow is already in progress",
"no_devices_found": "No SNOOZ devices discovered."
"no_devices_found": "No devices found on the network"
},
"error": {},
AustinBrunkhorst marked this conversation as resolved.
Show resolved Hide resolved
"flow_title": "{name}",
"progress": {
"wait_for_pairing_mode": "To complete setup, put this device in pairing mode.\n\n### How to enter pairing mode\n1. Force quit SNOOZ mobile apps.\n2. Press and hold the power button on the device. Release when the lights start blinking (approximately 5 seconds)."
},
"step": {
"bluetooth_confirm": {
"description": "Do you want to setup {name}?"
Expand All @@ -19,9 +23,6 @@
},
"description": "Choose a device to setup"
}
},
"progress": {
"wait_for_pairing_mode": "To complete setup, put this device in pairing mode.\n\n### How to enter pairing mode\n1. Force quit SNOOZ mobile apps.\n2. Press and hold the power button on the device.\n3. Release when the lights start blinking (approximately 5 seconds)."
}
}
}
Loading