mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 08:47:10 +00:00
Add strict type annotations to acer_projector (#50657)
This commit is contained in:
parent
ad7be91b6a
commit
562e0d785d
@ -8,7 +8,7 @@ omit =
|
|||||||
homeassistant/scripts/*.py
|
homeassistant/scripts/*.py
|
||||||
|
|
||||||
# omit pieces of code that rely on external devices being present
|
# omit pieces of code that rely on external devices being present
|
||||||
homeassistant/components/acer_projector/switch.py
|
homeassistant/components/acer_projector/*
|
||||||
homeassistant/components/actiontec/device_tracker.py
|
homeassistant/components/actiontec/device_tracker.py
|
||||||
homeassistant/components/acmeda/__init__.py
|
homeassistant/components/acmeda/__init__.py
|
||||||
homeassistant/components/acmeda/base.py
|
homeassistant/components/acmeda/base.py
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
# to enable strict mypy checks.
|
# to enable strict mypy checks.
|
||||||
|
|
||||||
homeassistant.components
|
homeassistant.components
|
||||||
|
homeassistant.components.acer_projector.*
|
||||||
homeassistant.components.airly.*
|
homeassistant.components.airly.*
|
||||||
homeassistant.components.automation.*
|
homeassistant.components.automation.*
|
||||||
homeassistant.components.binary_sensor.*
|
homeassistant.components.binary_sensor.*
|
||||||
|
34
homeassistant/components/acer_projector/const.py
Normal file
34
homeassistant/components/acer_projector/const.py
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
"""Use serial protocol of Acer projector to obtain state of the projector."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import Final
|
||||||
|
|
||||||
|
from homeassistant.const import STATE_OFF, STATE_ON
|
||||||
|
|
||||||
|
CONF_WRITE_TIMEOUT: Final = "write_timeout"
|
||||||
|
|
||||||
|
DEFAULT_NAME: Final = "Acer Projector"
|
||||||
|
DEFAULT_TIMEOUT: Final = 1
|
||||||
|
DEFAULT_WRITE_TIMEOUT: Final = 1
|
||||||
|
|
||||||
|
ECO_MODE: Final = "ECO Mode"
|
||||||
|
|
||||||
|
ICON: Final = "mdi:projector"
|
||||||
|
|
||||||
|
INPUT_SOURCE: Final = "Input Source"
|
||||||
|
|
||||||
|
LAMP: Final = "Lamp"
|
||||||
|
LAMP_HOURS: Final = "Lamp Hours"
|
||||||
|
|
||||||
|
MODEL: Final = "Model"
|
||||||
|
|
||||||
|
# Commands known to the projector
|
||||||
|
CMD_DICT: Final[dict[str, str]] = {
|
||||||
|
LAMP: "* 0 Lamp ?\r",
|
||||||
|
LAMP_HOURS: "* 0 Lamp\r",
|
||||||
|
INPUT_SOURCE: "* 0 Src ?\r",
|
||||||
|
ECO_MODE: "* 0 IR 052\r",
|
||||||
|
MODEL: "* 0 IR 035\r",
|
||||||
|
STATE_ON: "* 0 IR 001\r",
|
||||||
|
STATE_OFF: "* 0 IR 002\r",
|
||||||
|
}
|
@ -1,6 +1,9 @@
|
|||||||
"""Use serial protocol of Acer projector to obtain state of the projector."""
|
"""Use serial protocol of Acer projector to obtain state of the projector."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
import serial
|
import serial
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
@ -14,39 +17,26 @@ from homeassistant.const import (
|
|||||||
STATE_ON,
|
STATE_ON,
|
||||||
STATE_UNKNOWN,
|
STATE_UNKNOWN,
|
||||||
)
|
)
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
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,
|
||||||
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
CONF_WRITE_TIMEOUT = "write_timeout"
|
|
||||||
|
|
||||||
DEFAULT_NAME = "Acer Projector"
|
|
||||||
DEFAULT_TIMEOUT = 1
|
|
||||||
DEFAULT_WRITE_TIMEOUT = 1
|
|
||||||
|
|
||||||
ECO_MODE = "ECO Mode"
|
|
||||||
|
|
||||||
ICON = "mdi:projector"
|
|
||||||
|
|
||||||
INPUT_SOURCE = "Input Source"
|
|
||||||
|
|
||||||
LAMP = "Lamp"
|
|
||||||
LAMP_HOURS = "Lamp Hours"
|
|
||||||
|
|
||||||
MODEL = "Model"
|
|
||||||
|
|
||||||
# Commands known to the projector
|
|
||||||
CMD_DICT = {
|
|
||||||
LAMP: "* 0 Lamp ?\r",
|
|
||||||
LAMP_HOURS: "* 0 Lamp\r",
|
|
||||||
INPUT_SOURCE: "* 0 Src ?\r",
|
|
||||||
ECO_MODE: "* 0 IR 052\r",
|
|
||||||
MODEL: "* 0 IR 035\r",
|
|
||||||
STATE_ON: "* 0 IR 001\r",
|
|
||||||
STATE_OFF: "* 0 IR 002\r",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
vol.Required(CONF_FILENAME): cv.isdevice,
|
vol.Required(CONF_FILENAME): cv.isdevice,
|
||||||
@ -59,7 +49,12 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config: ConfigType,
|
||||||
|
add_entities: AddEntitiesCallback,
|
||||||
|
discovery_info: DiscoveryInfoType,
|
||||||
|
) -> None:
|
||||||
"""Connect with serial port and return Acer Projector."""
|
"""Connect with serial port and return Acer Projector."""
|
||||||
serial_port = config[CONF_FILENAME]
|
serial_port = config[CONF_FILENAME]
|
||||||
name = config[CONF_NAME]
|
name = config[CONF_NAME]
|
||||||
@ -72,10 +67,16 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
class AcerSwitch(SwitchEntity):
|
class AcerSwitch(SwitchEntity):
|
||||||
"""Represents an Acer Projector as a switch."""
|
"""Represents an Acer Projector as a switch."""
|
||||||
|
|
||||||
def __init__(self, serial_port, name, timeout, write_timeout, **kwargs):
|
def __init__(
|
||||||
|
self,
|
||||||
|
serial_port: str,
|
||||||
|
name: str,
|
||||||
|
timeout: int,
|
||||||
|
write_timeout: int,
|
||||||
|
) -> None:
|
||||||
"""Init of the Acer projector."""
|
"""Init of the Acer projector."""
|
||||||
self.ser = serial.Serial(
|
self.ser = serial.Serial(
|
||||||
port=serial_port, timeout=timeout, write_timeout=write_timeout, **kwargs
|
port=serial_port, timeout=timeout, write_timeout=write_timeout
|
||||||
)
|
)
|
||||||
self._serial_port = serial_port
|
self._serial_port = serial_port
|
||||||
self._name = name
|
self._name = name
|
||||||
@ -87,7 +88,7 @@ class AcerSwitch(SwitchEntity):
|
|||||||
ECO_MODE: STATE_UNKNOWN,
|
ECO_MODE: STATE_UNKNOWN,
|
||||||
}
|
}
|
||||||
|
|
||||||
def _write_read(self, msg):
|
def _write_read(self, msg: str) -> str:
|
||||||
"""Write to the projector and read the return."""
|
"""Write to the projector and read the return."""
|
||||||
ret = ""
|
ret = ""
|
||||||
# Sometimes the projector won't answer for no reason or the projector
|
# Sometimes the projector won't answer for no reason or the projector
|
||||||
@ -96,8 +97,7 @@ class AcerSwitch(SwitchEntity):
|
|||||||
try:
|
try:
|
||||||
if not self.ser.is_open:
|
if not self.ser.is_open:
|
||||||
self.ser.open()
|
self.ser.open()
|
||||||
msg = msg.encode("utf-8")
|
self.ser.write(msg.encode("utf-8"))
|
||||||
self.ser.write(msg)
|
|
||||||
# Size is an experience value there is no real limit.
|
# Size is an experience value there is no real limit.
|
||||||
# AFAIK there is no limit and no end character so we will usually
|
# AFAIK there is no limit and no end character so we will usually
|
||||||
# need to wait for timeout
|
# need to wait for timeout
|
||||||
@ -107,7 +107,7 @@ class AcerSwitch(SwitchEntity):
|
|||||||
self.ser.close()
|
self.ser.close()
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def _write_read_format(self, msg):
|
def _write_read_format(self, msg: str) -> str:
|
||||||
"""Write msg, obtain answer and format output."""
|
"""Write msg, obtain answer and format output."""
|
||||||
# answers are formatted as ***\answer\r***
|
# answers are formatted as ***\answer\r***
|
||||||
awns = self._write_read(msg)
|
awns = self._write_read(msg)
|
||||||
@ -117,29 +117,33 @@ class AcerSwitch(SwitchEntity):
|
|||||||
return STATE_UNKNOWN
|
return STATE_UNKNOWN
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self):
|
def available(self) -> bool:
|
||||||
"""Return if projector is available."""
|
"""Return if projector is available."""
|
||||||
return self._available
|
return self._available
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self) -> str:
|
||||||
"""Return name of the projector."""
|
"""Return name of the projector."""
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def icon(self) -> str:
|
||||||
|
"""Return the icon."""
|
||||||
|
return ICON
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self) -> bool:
|
||||||
"""Return if the projector is turned on."""
|
"""Return if the projector is turned on."""
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self):
|
def extra_state_attributes(self) -> dict[str, str]:
|
||||||
"""Return state attributes."""
|
"""Return state attributes."""
|
||||||
return self._attributes
|
return self._attributes
|
||||||
|
|
||||||
def update(self):
|
def update(self) -> None:
|
||||||
"""Get the latest state from the projector."""
|
"""Get the latest state from the projector."""
|
||||||
msg = CMD_DICT[LAMP]
|
awns = self._write_read_format(CMD_DICT[LAMP])
|
||||||
awns = self._write_read_format(msg)
|
|
||||||
if awns == "Lamp 1":
|
if awns == "Lamp 1":
|
||||||
self._state = True
|
self._state = True
|
||||||
self._available = True
|
self._available = True
|
||||||
@ -155,14 +159,14 @@ class AcerSwitch(SwitchEntity):
|
|||||||
awns = self._write_read_format(msg)
|
awns = self._write_read_format(msg)
|
||||||
self._attributes[key] = awns
|
self._attributes[key] = awns
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the projector on."""
|
"""Turn the projector on."""
|
||||||
msg = CMD_DICT[STATE_ON]
|
msg = CMD_DICT[STATE_ON]
|
||||||
self._write_read(msg)
|
self._write_read(msg)
|
||||||
self._state = STATE_ON
|
self._state = True
|
||||||
|
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs: Any) -> None:
|
||||||
"""Turn the projector off."""
|
"""Turn the projector off."""
|
||||||
msg = CMD_DICT[STATE_OFF]
|
msg = CMD_DICT[STATE_OFF]
|
||||||
self._write_read(msg)
|
self._write_read(msg)
|
||||||
self._state = STATE_OFF
|
self._state = False
|
||||||
|
11
mypy.ini
11
mypy.ini
@ -44,6 +44,17 @@ no_implicit_optional = true
|
|||||||
warn_return_any = true
|
warn_return_any = true
|
||||||
warn_unreachable = true
|
warn_unreachable = true
|
||||||
|
|
||||||
|
[mypy-homeassistant.components.acer_projector.*]
|
||||||
|
check_untyped_defs = true
|
||||||
|
disallow_incomplete_defs = true
|
||||||
|
disallow_subclassing_any = true
|
||||||
|
disallow_untyped_calls = true
|
||||||
|
disallow_untyped_decorators = true
|
||||||
|
disallow_untyped_defs = true
|
||||||
|
no_implicit_optional = true
|
||||||
|
warn_return_any = true
|
||||||
|
warn_unreachable = true
|
||||||
|
|
||||||
[mypy-homeassistant.components.airly.*]
|
[mypy-homeassistant.components.airly.*]
|
||||||
check_untyped_defs = true
|
check_untyped_defs = true
|
||||||
disallow_incomplete_defs = true
|
disallow_incomplete_defs = true
|
||||||
|
Loading…
x
Reference in New Issue
Block a user