Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle sensor string value #71

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 0.8.0

- Allow sensor with strings values

## 0.7.1

- Fix mixup between device ID and device address in english for send_command service
Expand Down
2 changes: 1 addition & 1 deletion custom_components/rfplayer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
vol.Required(CONF_COMMAND): cv.string,
vol.Optional(CONF_DEVICE_ADDRESS): cv.string,
vol.Optional(CONF_DEVICE_ID): cv.string,
vol.Required(CONF_AUTOMATIC_ADD, default=False): cv.boolean, # type: ignore
vol.Required(CONF_AUTOMATIC_ADD, default=False): cv.boolean,
}
)

Expand Down
14 changes: 10 additions & 4 deletions custom_components/rfplayer/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
import voluptuous as vol

from homeassistant import exceptions
from homeassistant.config_entries import HANDLERS, ConfigEntry, ConfigFlow, OptionsFlow
from homeassistant.config_entries import (
HANDLERS,
ConfigEntry,
ConfigFlow,
ConfigFlowResult,
OptionsFlow,
)
from homeassistant.const import CONF_DEVICE, CONF_DEVICES
from homeassistant.core import callback
from homeassistant.data_entry_flow import AbortFlow, FlowResult
from homeassistant.data_entry_flow import AbortFlow

from .const import (
CONF_AUTOMATIC_ADD,
Expand All @@ -29,7 +35,7 @@ class RfplayerConfigFlow(ConfigFlow, domain=DOMAIN):

async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Config flow started from UI."""
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")
Expand Down Expand Up @@ -95,7 +101,7 @@ def __init__(self, config_entry: ConfigEntry) -> None:

async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Manage the options."""
if user_input is None:
config = self.config_entry.data
Expand Down
2 changes: 2 additions & 0 deletions custom_components/rfplayer/rflib/rfpparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def valid_packet(packet: str) -> bool:
# pylint: disable-next=too-many-branches too-many-statements
def decode_packet(packet: str) -> list:
"""Decode packet."""
log.debug("decode_packet: %s", packet)
packets_found = []
data = cast(PacketType, {"node": PacketHeader.GATEWAY.name})

Expand Down Expand Up @@ -155,6 +156,7 @@ def decode_packet(packet: str) -> list:

def encode_packet(packet: PacketType) -> str:
"""Construct packet string from packet dictionary."""
log.debug("encode_packet: %s", packet)
command = str(packet["command"]).upper()
protocol = str(packet["protocol"]).upper()
if "id" in packet:
Expand Down
13 changes: 9 additions & 4 deletions custom_components/rfplayer/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import logging
from typing import Any

from homeassistant.components.sensor import RestoreSensor
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_DEVICES
from homeassistant.core import HomeAssistant
from homeassistant.components.sensor import RestoreSensor
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType

from . import RfplayerDevice
from .const import (
Expand Down Expand Up @@ -56,8 +57,6 @@ async def add_new_device(device_info):
class RfplayerSensor(RfplayerDevice, RestoreSensor):
"""Representation of a Rfplayer sensor."""

_attr_native_value: float | None = None

# pylint: disable-next=too-many-arguments
def __init__(
self,
Expand All @@ -70,6 +69,7 @@ def __init__(
) -> None:
"""Handle sensor specific args and super init."""
self._attr_native_unit_of_measurement = unit_of_measurement
self._event_value: StateType = None
super().__init__(
protocol=protocol,
device_id=device_id,
Expand All @@ -90,4 +90,9 @@ async def async_added_to_hass(self) -> None:

def _handle_event(self, event: dict[str, Any]) -> None:
"""Domain specific event handler."""
self._attr_native_value = float(event["value"])
self._event_value = event["value"]

@property
def native_value(self) -> StateType:
"""Return the value of the event."""
return self._event_value