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

WIP: Update to aiohue v4.x #13

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Hue2MQTT lets you control your Hue setup using MQTT and publishes the current st
- Control your lights using MQTT
- Receive live events (i.e button pushes, motion sensors) in real-time.
- No polling your Hue Bridge for changes
- Only supports Hue Bridge v2 (square-shaped). The Hue Bridge v1 is End of Life since June 2020.
- IPv6 Support

## Configuration
Expand Down
41 changes: 16 additions & 25 deletions hue2mqtt/hue2mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from typing import Match, Optional

import aiohue
from aiohttp.client import ClientSession
from pydantic import ValidationError

from hue2mqtt import __version__
Expand Down Expand Up @@ -98,15 +97,16 @@ async def run(self) -> None:
await self._mqtt.connect()
LOGGER.info("Connected to MQTT Broker")

async with ClientSession() as websession:
try:
await self._setup_bridge(websession)
except aiohue.errors.Unauthorized:
LOGGER.error("Bridge rejected username. Please use --discover")
self.halt()
return
await self._publish_bridge_status()
await self.main(websession)
LOGGER.info(f"Connecting to Hue Bridge at {self.config.hue.ip}")
try:
async with aiohue.HueBridgeV2(self.config.hue.ip, self.config.hue.username) as bridge:
self._bridge = bridge
await self._publish_bridge_status()
await self.main()
except aiohue.errors.Unauthorized:
LOGGER.error("Bridge rejected credentials. Please use --discover")
self.halt()
return

LOGGER.info("Disconnecting from MQTT Broker")
await self._publish_bridge_status(online=False)
Expand All @@ -116,27 +116,17 @@ def halt(self) -> None:
"""Stop the component."""
sys.exit(-1)

async def _setup_bridge(self, websession: ClientSession) -> None:
"""Connect to the Hue Bridge."""
self._bridge = aiohue.Bridge(
self.config.hue.ip,
websession,
username=self.config.hue.username,
)
LOGGER.info(f"Connecting to Hue Bridge at {self.config.hue.ip}")
await self._bridge.initialize()

async def _publish_bridge_status(self, *, online: bool = True) -> None:
"""Publish info about the Hue Bridge."""
if online:
LOGGER.info(f"Bridge Name: {self._bridge.config.name}")
LOGGER.info(f"Bridge MAC: {self._bridge.config.mac}")
LOGGER.info(f"API Version: {self._bridge.config.apiversion}")
LOGGER.info(f"Bridge MAC: {self._bridge.config.mac_address}")
LOGGER.info(f"API Version: {self._bridge.config.software_version}")

info = BridgeInfo(
name=self._bridge.config.name,
mac_address=self._bridge.config.mac,
api_version=self._bridge.config.apiversion,
mac_address=self._bridge.config.mac_address,
api_version=self._bridge.config.software_version,
)
message = Hue2MQTTStatus(online=online, bridge=info)
else:
Expand Down Expand Up @@ -195,9 +185,10 @@ async def handle_set_group(self, match: Match[str], payload: str) -> None:
except ValidationError as e:
LOGGER.warning(f"Invalid light state: {e}")

async def main(self, websession: ClientSession) -> None:
async def main(self) -> None:
"""Main method of the data component."""
# Publish initial info about lights
breakpoint()
for id, light_raw in self._bridge.lights._items.items():
light = LightInfo(id=id, **light_raw.raw)
self.publish_light(light)
Expand Down
31 changes: 24 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ classifiers = [
[tool.poetry.dependencies]
python = "^3.8"
gmqtt = "^0.6.10"
aiohue = "^2.5.1"
aiohue = "^4.5.0"
pydantic = "^1.9.2"
click = "^8.1.3"
aiohttp = "^3.8.1"
Expand Down