2019-04-03 15:40:03 +00:00
|
|
|
"""Use serial protocol of Acer projector to obtain state of the projector."""
|
2021-05-15 17:55:28 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2016-05-04 01:23:38 +00:00
|
|
|
import logging
|
|
|
|
import re
|
2021-05-15 17:55:28 +00:00
|
|
|
from typing import Any
|
2016-05-04 01:23:38 +00:00
|
|
|
|
2019-12-08 12:45:33 +00:00
|
|
|
import serial
|
2016-09-02 13:42:38 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
|
2016-09-02 13:42:38 +00:00
|
|
|
from homeassistant.const import (
|
2019-12-08 12:45:33 +00:00
|
|
|
CONF_FILENAME,
|
|
|
|
CONF_NAME,
|
2021-02-22 01:31:09 +00:00
|
|
|
CONF_TIMEOUT,
|
2019-07-31 19:25:30 +00:00
|
|
|
STATE_OFF,
|
2019-12-08 12:45:33 +00:00
|
|
|
STATE_ON,
|
2019-07-31 19:25:30 +00:00
|
|
|
STATE_UNKNOWN,
|
|
|
|
)
|
2021-05-15 17:55:28 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2016-09-02 13:42:38 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2021-05-15 17:55:28 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
|
|
|
|
|
|
|
from .const import (
|
|
|
|
CMD_DICT,
|
|
|
|
CONF_WRITE_TIMEOUT,
|
|
|
|
DEFAULT_NAME,
|
|
|
|
DEFAULT_TIMEOUT,
|
|
|
|
DEFAULT_WRITE_TIMEOUT,
|
|
|
|
ECO_MODE,
|
|
|
|
ICON,
|
|
|
|
INPUT_SOURCE,
|
|
|
|
LAMP,
|
|
|
|
LAMP_HOURS,
|
|
|
|
)
|
2016-09-02 13:42:38 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_FILENAME): cv.isdevice,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_WRITE_TIMEOUT, default=DEFAULT_WRITE_TIMEOUT
|
|
|
|
): cv.positive_int,
|
|
|
|
}
|
|
|
|
)
|
2016-05-04 01:23:38 +00:00
|
|
|
|
|
|
|
|
2021-05-15 17:55:28 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType,
|
|
|
|
) -> None:
|
2016-05-04 01:23:38 +00:00
|
|
|
"""Connect with serial port and return Acer Projector."""
|
2020-04-08 10:59:38 +00:00
|
|
|
serial_port = config[CONF_FILENAME]
|
|
|
|
name = config[CONF_NAME]
|
|
|
|
timeout = config[CONF_TIMEOUT]
|
|
|
|
write_timeout = config[CONF_WRITE_TIMEOUT]
|
2016-05-04 01:23:38 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([AcerSwitch(serial_port, name, timeout, write_timeout)], True)
|
2016-05-04 01:23:38 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class AcerSwitch(SwitchEntity):
|
2018-01-27 19:58:27 +00:00
|
|
|
"""Represents an Acer Projector as a switch."""
|
2016-05-04 01:23:38 +00:00
|
|
|
|
2021-07-20 09:13:59 +00:00
|
|
|
_attr_icon = ICON
|
|
|
|
|
2021-05-15 17:55:28 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
serial_port: str,
|
|
|
|
name: str,
|
|
|
|
timeout: int,
|
|
|
|
write_timeout: int,
|
|
|
|
) -> None:
|
2016-05-04 01:23:38 +00:00
|
|
|
"""Init of the Acer projector."""
|
2016-09-02 13:42:38 +00:00
|
|
|
self.ser = serial.Serial(
|
2021-05-15 17:55:28 +00:00
|
|
|
port=serial_port, timeout=timeout, write_timeout=write_timeout
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-05-04 01:23:38 +00:00
|
|
|
self._serial_port = serial_port
|
2021-07-20 09:13:59 +00:00
|
|
|
self._attr_name = name
|
2016-05-04 01:23:38 +00:00
|
|
|
self._attributes = {
|
|
|
|
LAMP_HOURS: STATE_UNKNOWN,
|
|
|
|
INPUT_SOURCE: STATE_UNKNOWN,
|
|
|
|
ECO_MODE: STATE_UNKNOWN,
|
|
|
|
}
|
|
|
|
|
2021-05-15 17:55:28 +00:00
|
|
|
def _write_read(self, msg: str) -> str:
|
2016-05-04 01:23:38 +00:00
|
|
|
"""Write to the projector and read the return."""
|
|
|
|
ret = ""
|
2016-09-02 13:42:38 +00:00
|
|
|
# Sometimes the projector won't answer for no reason or the projector
|
|
|
|
# was disconnected during runtime.
|
|
|
|
# This way the projector can be reconnected and will still work
|
2016-05-04 01:23:38 +00:00
|
|
|
try:
|
|
|
|
if not self.ser.is_open:
|
|
|
|
self.ser.open()
|
2021-05-15 17:55:28 +00:00
|
|
|
self.ser.write(msg.encode("utf-8"))
|
2016-09-02 13:42:38 +00:00
|
|
|
# Size is an experience value there is no real limit.
|
|
|
|
# AFAIK there is no limit and no end character so we will usually
|
|
|
|
# need to wait for timeout
|
2019-07-31 19:25:30 +00:00
|
|
|
ret = self.ser.read_until(size=20).decode("utf-8")
|
2016-05-04 01:23:38 +00:00
|
|
|
except serial.SerialException:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Problem communicating with %s", self._serial_port)
|
2016-05-04 01:23:38 +00:00
|
|
|
self.ser.close()
|
|
|
|
return ret
|
|
|
|
|
2021-05-15 17:55:28 +00:00
|
|
|
def _write_read_format(self, msg: str) -> str:
|
2018-01-29 22:37:19 +00:00
|
|
|
"""Write msg, obtain answer and format output."""
|
|
|
|
# answers are formatted as ***\answer\r***
|
2016-05-04 01:23:38 +00:00
|
|
|
awns = self._write_read(msg)
|
2019-07-31 19:25:30 +00:00
|
|
|
match = re.search(r"\r(.+)\r", awns)
|
2016-05-04 01:23:38 +00:00
|
|
|
if match:
|
|
|
|
return match.group(1)
|
|
|
|
return STATE_UNKNOWN
|
|
|
|
|
2021-05-15 17:55:28 +00:00
|
|
|
def update(self) -> None:
|
2016-05-04 01:23:38 +00:00
|
|
|
"""Get the latest state from the projector."""
|
2021-05-15 17:55:28 +00:00
|
|
|
awns = self._write_read_format(CMD_DICT[LAMP])
|
2019-07-31 19:25:30 +00:00
|
|
|
if awns == "Lamp 1":
|
2021-07-20 09:13:59 +00:00
|
|
|
self._attr_is_on = True
|
|
|
|
self._attr_available = True
|
2019-07-31 19:25:30 +00:00
|
|
|
elif awns == "Lamp 0":
|
2021-07-20 09:13:59 +00:00
|
|
|
self._attr_is_on = False
|
|
|
|
self._attr_available = True
|
2016-05-04 01:23:38 +00:00
|
|
|
else:
|
2021-07-20 09:13:59 +00:00
|
|
|
self._attr_available = False
|
2016-05-04 01:23:38 +00:00
|
|
|
|
2016-07-08 01:54:16 +00:00
|
|
|
for key in self._attributes:
|
2021-10-18 13:54:38 +00:00
|
|
|
if msg := CMD_DICT.get(key):
|
2016-05-04 01:23:38 +00:00
|
|
|
awns = self._write_read_format(msg)
|
|
|
|
self._attributes[key] = awns
|
2021-07-20 09:13:59 +00:00
|
|
|
self._attr_extra_state_attributes = self._attributes
|
2016-05-04 01:23:38 +00:00
|
|
|
|
2021-05-15 17:55:28 +00:00
|
|
|
def turn_on(self, **kwargs: Any) -> None:
|
2016-05-04 01:23:38 +00:00
|
|
|
"""Turn the projector on."""
|
|
|
|
msg = CMD_DICT[STATE_ON]
|
|
|
|
self._write_read(msg)
|
2021-07-20 09:13:59 +00:00
|
|
|
self._attr_is_on = True
|
2016-05-04 01:23:38 +00:00
|
|
|
|
2021-05-15 17:55:28 +00:00
|
|
|
def turn_off(self, **kwargs: Any) -> None:
|
2016-05-04 01:23:38 +00:00
|
|
|
"""Turn the projector off."""
|
|
|
|
msg = CMD_DICT[STATE_OFF]
|
|
|
|
self._write_read(msg)
|
2021-07-20 09:13:59 +00:00
|
|
|
self._attr_is_on = False
|