1 """Lannouncer platform for notify component."""
3 from __future__
import annotations
7 from urllib.parse
import urlencode
9 import voluptuous
as vol
13 PLATFORM_SCHEMA
as NOTIFY_PLATFORM_SCHEMA,
14 BaseNotificationService,
21 ATTR_METHOD =
"method"
22 ATTR_METHOD_DEFAULT =
"speak"
23 ATTR_METHOD_ALLOWED = [
"speak",
"alarm"]
27 PLATFORM_SCHEMA = NOTIFY_PLATFORM_SCHEMA.extend(
29 vol.Required(CONF_HOST): cv.string,
30 vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
34 _LOGGER = logging.getLogger(__name__)
40 discovery_info: DiscoveryInfoType |
None =
None,
41 ) -> LannouncerNotificationService:
42 """Get the Lannouncer notification service."""
43 host = config.get(CONF_HOST)
44 port = config.get(CONF_PORT)
50 """Implementation of a notification service for Lannouncer."""
53 """Initialize the service."""
59 """Send a message to Lannouncer."""
60 data = kwargs.get(ATTR_DATA)
61 if data
is not None and ATTR_METHOD
in data:
62 method = data.get(ATTR_METHOD)
64 method = ATTR_METHOD_DEFAULT
66 if method
not in ATTR_METHOD_ALLOWED:
67 _LOGGER.error(
"Unknown method %s", method)
74 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
76 sock.connect((self.
_host_host, self.
_port_port))
79 _LOGGER.debug(
"Sending message: %s", cmd)
80 sock.sendall(cmd.encode())
81 sock.sendall(b
"&@DONE@\n")
84 buffer = sock.recv(1024)
85 if buffer != b
"LANnouncer: OK":
86 _LOGGER.error(
"Error sending data to Lannnouncer: %s", buffer.decode())
90 except socket.gaierror:
91 _LOGGER.error(
"Unable to connect to host %s", self.
_host_host)
93 _LOGGER.exception(
"Failed to send data to Lannnouncer")
def send_message(self, message="", **kwargs)
def __init__(self, hass, host, port)
LannouncerNotificationService get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)