Add type hints to rest switch (#81307)

This commit is contained in:
epenet 2022-11-07 14:02:49 +01:00 committed by GitHub
parent 934cec9778
commit f479b2385e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -81,8 +81,8 @@ async def async_setup_platform(
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Set up the RESTful switch.""" """Set up the RESTful switch."""
resource = config.get(CONF_RESOURCE) resource: str = config[CONF_RESOURCE]
unique_id = config.get(CONF_UNIQUE_ID) unique_id: str | None = config.get(CONF_UNIQUE_ID)
try: try:
switch = RestSwitch(hass, config, unique_id) switch = RestSwitch(hass, config, unique_id)
@ -106,10 +106,10 @@ class RestSwitch(TemplateEntity, SwitchEntity):
def __init__( def __init__(
self, self,
hass, hass: HomeAssistant,
config, config: ConfigType,
unique_id, unique_id: str | None,
): ) -> None:
"""Initialize the REST switch.""" """Initialize the REST switch."""
TemplateEntity.__init__( TemplateEntity.__init__(
self, self,
@ -119,30 +119,30 @@ class RestSwitch(TemplateEntity, SwitchEntity):
unique_id=unique_id, unique_id=unique_id,
) )
auth = None auth: aiohttp.BasicAuth | None = None
username: str | None = None
if username := config.get(CONF_USERNAME): if username := config.get(CONF_USERNAME):
auth = aiohttp.BasicAuth(username, password=config[CONF_PASSWORD]) password: str = config[CONF_PASSWORD]
auth = aiohttp.BasicAuth(username, password=password)
self._resource = config.get(CONF_RESOURCE) self._resource: str = config[CONF_RESOURCE]
self._state_resource = config.get(CONF_STATE_RESOURCE) or self._resource self._state_resource: str = config.get(CONF_STATE_RESOURCE) or self._resource
self._method = config.get(CONF_METHOD) self._method: str = config[CONF_METHOD]
self._headers = config.get(CONF_HEADERS) self._headers: dict[str, template.Template] | None = config.get(CONF_HEADERS)
self._params = config.get(CONF_PARAMS) self._params: dict[str, template.Template] | None = config.get(CONF_PARAMS)
self._auth = auth self._auth = auth
self._body_on = config.get(CONF_BODY_ON) self._body_on: template.Template = config[CONF_BODY_ON]
self._body_off = config.get(CONF_BODY_OFF) self._body_off: template.Template = config[CONF_BODY_OFF]
self._is_on_template = config.get(CONF_IS_ON_TEMPLATE) self._is_on_template: template.Template | None = config.get(CONF_IS_ON_TEMPLATE)
self._timeout = config.get(CONF_TIMEOUT) self._timeout: int = config[CONF_TIMEOUT]
self._verify_ssl = config.get(CONF_VERIFY_SSL) self._verify_ssl: bool = config[CONF_VERIFY_SSL]
self._attr_device_class = config.get(CONF_DEVICE_CLASS) self._attr_device_class = config.get(CONF_DEVICE_CLASS)
self._body_on.hass = hass
self._body_off.hass = hass
if (is_on_template := self._is_on_template) is not None: if (is_on_template := self._is_on_template) is not None:
is_on_template.hass = hass is_on_template.hass = hass
if (body_on := self._body_on) is not None:
body_on.hass = hass
if (body_off := self._body_off) is not None:
body_off.hass = hass
template.attach(hass, self._headers) template.attach(hass, self._headers)
template.attach(hass, self._params) template.attach(hass, self._params)
@ -178,7 +178,7 @@ class RestSwitch(TemplateEntity, SwitchEntity):
except (asyncio.TimeoutError, aiohttp.ClientError): except (asyncio.TimeoutError, aiohttp.ClientError):
_LOGGER.error("Error while switching off %s", self._resource) _LOGGER.error("Error while switching off %s", self._resource)
async def set_device_state(self, body): async def set_device_state(self, body: Any) -> aiohttp.ClientResponse:
"""Send a state update to the device.""" """Send a state update to the device."""
websession = async_get_clientsession(self.hass, self._verify_ssl) websession = async_get_clientsession(self.hass, self._verify_ssl)
@ -186,7 +186,7 @@ class RestSwitch(TemplateEntity, SwitchEntity):
rendered_params = template.render_complex(self._params) rendered_params = template.render_complex(self._params)
async with async_timeout.timeout(self._timeout): async with async_timeout.timeout(self._timeout):
req = await getattr(websession, self._method)( req: aiohttp.ClientResponse = await getattr(websession, self._method)(
self._resource, self._resource,
auth=self._auth, auth=self._auth,
data=bytes(body, "utf-8"), data=bytes(body, "utf-8"),
@ -204,7 +204,7 @@ class RestSwitch(TemplateEntity, SwitchEntity):
except aiohttp.ClientError as err: except aiohttp.ClientError as err:
_LOGGER.exception("Error while fetching data: %s", err) _LOGGER.exception("Error while fetching data: %s", err)
async def get_device_state(self, hass): async def get_device_state(self, hass: HomeAssistant) -> aiohttp.ClientResponse:
"""Get the latest data from REST API and update the state.""" """Get the latest data from REST API and update the state."""
websession = async_get_clientsession(hass, self._verify_ssl) websession = async_get_clientsession(hass, self._verify_ssl)