Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 .strict-typing
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# to enable strict mypy checks.

homeassistant.components
homeassistant.components.acer_projector.*
homeassistant.components.airly.*
homeassistant.components.automation.*
homeassistant.components.binary_sensor.*
Expand Down
52 changes: 34 additions & 18 deletions homeassistant/components/acer_projector/switch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""Use serial protocol of Acer projector to obtain state of the projector."""
from __future__ import annotations

import logging
import re
from typing import Any

import serial
import voluptuous as vol
Expand All @@ -14,7 +17,10 @@
STATE_ON,
STATE_UNKNOWN,
)
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -59,7 +65,12 @@
)


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."""
serial_port = config[CONF_FILENAME]
name = config[CONF_NAME]
Expand All @@ -72,7 +83,14 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class AcerSwitch(SwitchEntity):
"""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,
**kwargs: Any,
Comment thread
mib1185 marked this conversation as resolved.
Outdated
) -> None:
"""Init of the Acer projector."""
self.ser = serial.Serial(
port=serial_port, timeout=timeout, write_timeout=write_timeout, **kwargs
Expand All @@ -87,7 +105,7 @@ def __init__(self, serial_port, name, timeout, write_timeout, **kwargs):
ECO_MODE: STATE_UNKNOWN,
}

def _write_read(self, msg):
def _write_read(self, msg: str) -> str | None:
Comment thread
mib1185 marked this conversation as resolved.
Outdated
"""Write to the projector and read the return."""
ret = ""
# Sometimes the projector won't answer for no reason or the projector
Expand All @@ -96,8 +114,7 @@ def _write_read(self, msg):
try:
if not self.ser.is_open:
self.ser.open()
msg = msg.encode("utf-8")
self.ser.write(msg)
self.ser.write(msg.encode("utf-8"))
# 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
Expand All @@ -107,39 +124,38 @@ def _write_read(self, msg):
self.ser.close()
return ret

def _write_read_format(self, msg):
def _write_read_format(self, msg: str) -> str:
"""Write msg, obtain answer and format output."""
# answers are formatted as ***\answer\r***
awns = self._write_read(msg)
awns = str(self._write_read(msg))
Comment thread
mib1185 marked this conversation as resolved.
Outdated
match = re.search(r"\r(.+)\r", awns)
if match:
return match.group(1)
return STATE_UNKNOWN

@property
def available(self):
def available(self) -> bool:
"""Return if projector is available."""
return self._available

@property
def name(self):
def name(self) -> str:
"""Return name of the projector."""
return self._name

@property
def is_on(self):
def is_on(self) -> bool:
"""Return if the projector is turned on."""
return self._state

@property
def extra_state_attributes(self):
def extra_state_attributes(self) -> dict[str, str]:
"""Return state attributes."""
return self._attributes

def update(self):
def update(self) -> None:
"""Get the latest state from the projector."""
msg = CMD_DICT[LAMP]
awns = self._write_read_format(msg)
awns = self._write_read_format(CMD_DICT[LAMP])
if awns == "Lamp 1":
self._state = True
self._available = True
Expand All @@ -155,14 +171,14 @@ def update(self):
awns = self._write_read_format(msg)
self._attributes[key] = awns

def turn_on(self, **kwargs):
def turn_on(self, **kwargs: Any) -> None:
"""Turn the projector on."""
msg = CMD_DICT[STATE_ON]
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."""
msg = CMD_DICT[STATE_OFF]
self._write_read(msg)
self._state = STATE_OFF
self._state = False
11 changes: 11 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ no_implicit_optional = true
warn_return_any = 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.*]
check_untyped_defs = true
disallow_incomplete_defs = true
Expand Down