2023-12-29 12:40:35 +01:00
|
|
|
"""The Easy Dualboot Computer Manager integration."""
|
2023-04-04 13:44:07 +02:00
|
|
|
|
2023-12-30 21:22:32 +01:00
|
|
|
# Some snippets of code are from the official wake_on_lan integration (inspiration for this custom component)
|
2023-04-04 13:44:07 +02:00
|
|
|
|
|
|
|
from __future__ import annotations
|
2024-06-09 11:42:28 +02:00
|
|
|
|
2023-04-04 13:44:07 +02:00
|
|
|
import logging
|
|
|
|
from functools import partial
|
|
|
|
|
2023-12-30 20:21:37 +01:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2023-04-04 13:44:07 +02:00
|
|
|
import voluptuous as vol
|
|
|
|
import wakeonlan
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import CONF_BROADCAST_ADDRESS, CONF_BROADCAST_PORT, CONF_MAC
|
2023-04-04 14:34:44 +02:00
|
|
|
from homeassistant.core import HomeAssistant, ServiceCall
|
2023-12-30 20:21:37 +01:00
|
|
|
|
2023-12-28 22:40:29 +01:00
|
|
|
from .const import DOMAIN, SERVICE_SEND_MAGIC_PACKET, SERVICE_CHANGE_MONITORS_CONFIG
|
2023-04-04 13:44:07 +02:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2023-12-31 17:30:27 +01:00
|
|
|
WAKE_ON_LAN_SEND_MAGIC_PACKET_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_MAC): cv.string,
|
|
|
|
vol.Optional(CONF_BROADCAST_ADDRESS): cv.string,
|
|
|
|
vol.Optional(CONF_BROADCAST_PORT): cv.port,
|
|
|
|
})
|
2023-04-04 13:44:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2023-12-30 21:22:32 +01:00
|
|
|
"""Set up the Easy Dualboot Computer Manager integration."""
|
2023-04-04 13:44:07 +02:00
|
|
|
|
|
|
|
async def send_magic_packet(call: ServiceCall) -> None:
|
2023-12-30 21:22:32 +01:00
|
|
|
"""Send a magic packet to wake up a device."""
|
2023-04-04 13:44:07 +02:00
|
|
|
mac_address = call.data.get(CONF_MAC)
|
|
|
|
broadcast_address = call.data.get(CONF_BROADCAST_ADDRESS)
|
|
|
|
broadcast_port = call.data.get(CONF_BROADCAST_PORT)
|
|
|
|
|
|
|
|
service_kwargs = {}
|
|
|
|
if broadcast_address is not None:
|
|
|
|
service_kwargs["ip_address"] = broadcast_address
|
|
|
|
if broadcast_port is not None:
|
|
|
|
service_kwargs["port"] = broadcast_port
|
|
|
|
|
|
|
|
_LOGGER.info(
|
2023-12-30 21:22:32 +01:00
|
|
|
"Sending magic packet to MAC %s (broadcast: %s, port: %s)",
|
2023-04-04 13:44:07 +02:00
|
|
|
mac_address,
|
|
|
|
broadcast_address,
|
|
|
|
broadcast_port,
|
|
|
|
)
|
|
|
|
|
|
|
|
await hass.async_add_executor_job(
|
|
|
|
partial(wakeonlan.send_magic_packet, mac_address, **service_kwargs)
|
|
|
|
)
|
|
|
|
|
2023-12-31 17:30:27 +01:00
|
|
|
# Register the wake on lan service
|
2023-04-04 13:44:07 +02:00
|
|
|
hass.services.async_register(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_SEND_MAGIC_PACKET,
|
|
|
|
send_magic_packet,
|
|
|
|
schema=WAKE_ON_LAN_SEND_MAGIC_PACKET_SCHEMA,
|
|
|
|
)
|
|
|
|
|
2023-04-04 14:13:47 +02:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(
|
|
|
|
entry, "switch"
|
|
|
|
)
|
|
|
|
)
|
2023-04-04 13:44:07 +02:00
|
|
|
|
|
|
|
return True
|
2023-12-29 11:06:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2023-12-30 21:22:32 +01:00
|
|
|
"""Unload the Easy Dualboot Computer Manager integration."""
|
2023-12-29 11:06:48 +01:00
|
|
|
return await hass.config_entries.async_forward_entry_unload(
|
|
|
|
entry, "switch"
|
|
|
|
)
|