- 
                Notifications
    
You must be signed in to change notification settings  - Fork 902
 
Add support for Wenzhi MTD085-ZB mmWave presence sensor #4424
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
          
     Open
      
      
            fradaloisio
  wants to merge
  2
  commits into
  zigpy:dev
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
fradaloisio:add-wenzhi-mtd085-support
  
      
      
   
  
    
  
  
  
 
  
      
    base: dev
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from 1 commit
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| """Tests for Wenzhi quirks.""" | ||
| 
     | 
||
| import contextlib | ||
| 
     | 
||
| import pytest | ||
| from zigpy.zcl.clusters.security import IasZone | ||
| 
     | 
||
| import zhaquirks | ||
| import zhaquirks.wenzhi.mtd085_motion | ||
| 
     | 
||
| zhaquirks.setup() | ||
| 
     | 
||
| 
     | 
||
| @pytest.mark.parametrize( | ||
| "manufacturer,model", | ||
| [ | ||
| ("_TZ321C_fkzihax8", "TS0225"), | ||
| ("_TZ321C_4slreunp", "TS0225"), | ||
| ], | ||
| ) | ||
| def test_mtd085_signature(assert_signature_matches_quirk, manufacturer, model): | ||
| """Test Wenzhi MTD085-ZB signature is matched to its quirk.""" | ||
| signature = { | ||
| "node_descriptor": "NodeDescriptor(logical_type=<LogicalType.Router: 1>, complex_descriptor_available=0, user_descriptor_available=0, reserved=0, aps_flags=0, frequency_band=<FrequencyBand.Freq2400MHz: 8>, mac_capability_flags=<MACCapabilityFlags.AllocateAddress|RxOnWhenIdle|MainsPowered|FullFunctionDevice: 142>, manufacturer_code=4098, maximum_buffer_size=82, maximum_incoming_transfer_size=82, server_mask=11264, maximum_outgoing_transfer_size=82, descriptor_capability_field=<DescriptorCapability.NONE: 0>, *allocate_address=True, *is_alternate_pan_coordinator=False, *is_coordinator=False, *is_end_device=False, *is_full_function_device=True, *is_mains_powered=True, *is_receiver_on_when_idle=True, *is_router=True, *is_security_capable=False)", | ||
| "endpoints": { | ||
| "1": { | ||
| "profile_id": 260, | ||
| "device_type": "0x0402", | ||
| "in_clusters": ["0x0000", "0x0004", "0x0005", "0x0500"], | ||
| "out_clusters": ["0x000a", "0x0019"], | ||
| }, | ||
| "242": { | ||
| "profile_id": 41440, | ||
| "device_type": "0x0061", | ||
| "in_clusters": [], | ||
| "out_clusters": ["0x0021"], | ||
| }, | ||
| }, | ||
| "manufacturer": manufacturer, | ||
| "model": model, | ||
| "class": "zhaquirks.wenzhi.mtd085_motion.WenzhiMTD085_ZB", | ||
| } | ||
| assert_signature_matches_quirk( | ||
| zhaquirks.wenzhi.mtd085_motion.WenzhiMTD085_ZB, signature | ||
| ) | ||
| 
     | 
||
| 
     | 
||
| @pytest.mark.parametrize( | ||
| "manufacturer,model", | ||
| [ | ||
| ("_TZ321C_fkzihax8", "TS0225"), | ||
| ("_TZ321C_4slreunp", "TS0225"), | ||
| ], | ||
| ) | ||
| @pytest.mark.asyncio | ||
| async def test_mtd085_quirk_applies(zigpy_device_from_quirk, manufacturer, model): | ||
| """Test that the MTD085-ZB quirk is applied correctly.""" | ||
| # Create the quirk with the correct signature | ||
| quirked_device = zigpy_device_from_quirk( | ||
| zhaquirks.wenzhi.mtd085_motion.WenzhiMTD085_ZB | ||
| ) | ||
| 
     | 
||
| # Check that the quirk was applied | ||
| assert isinstance(quirked_device, zhaquirks.wenzhi.mtd085_motion.WenzhiMTD085_ZB) | ||
| 
     | 
||
| # Check endpoint 1 exists and has IAS Zone cluster | ||
| ep = quirked_device.endpoints[1] | ||
| assert ep is not None | ||
| assert IasZone.cluster_id in ep.in_clusters | ||
| 
     | 
||
| # Verify IAS Zone cluster is properly instantiated | ||
| ias_zone = ep.in_clusters[IasZone.cluster_id] | ||
| assert isinstance(ias_zone, IasZone) | ||
| 
     | 
||
| 
     | 
||
| @pytest.mark.parametrize( | ||
| "manufacturer,model", | ||
| [ | ||
| ("_TZ321C_fkzihax8", "TS0225"), | ||
| ("_TZ321C_4slreunp", "TS0225"), | ||
| ], | ||
| ) | ||
| @pytest.mark.asyncio | ||
| async def test_mtd085_magic_packet(zigpy_device_from_quirk, manufacturer, model): | ||
| """Test that the magic packet configuration is callable without errors.""" | ||
| quirked_device = zigpy_device_from_quirk( | ||
| zhaquirks.wenzhi.mtd085_motion.WenzhiMTD085_ZB | ||
| ) | ||
| 
     | 
||
| # Test that apply_custom_configuration exists and can be called | ||
| assert hasattr(quirked_device, "apply_custom_configuration") | ||
| 
     | 
||
| # This should not raise an exception even if the read fails | ||
| # (the real device will respond, but the mock won't) | ||
| with contextlib.suppress(Exception): | ||
| # Exception is acceptable in test environment | ||
| # The real test is that the method exists and is properly defined | ||
| await quirked_device.apply_custom_configuration() | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1 @@ | ||
| """Wenzhi/LeapMMW devices.""" | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| """Wenzhi/LeapMMW MTD085-ZB mmWave radar presence sensor. | ||
| 
     | 
||
| This device operates in basic IAS Zone mode when paired directly with ZHA. | ||
| The advanced features (illuminance, distance measurement, configuration parameters) | ||
| require the Tuya MCU cluster (0xEF00) which is not exposed in this mode. | ||
| 
     | 
||
| Current functionality: | ||
| - Motion detection via IAS Zone (binary_sensor.motion) | ||
| - Occupancy clear when leaving the room | ||
| - Basic device info | ||
| """ | ||
| 
     | 
||
| from zigpy.quirks import CustomDevice | ||
| from zigpy.zcl.clusters.general import Basic, Groups, Scenes | ||
| from zigpy.zcl.clusters.security import IasZone | ||
| 
     | 
||
| from zhaquirks.const import ( | ||
| DEVICE_TYPE, | ||
| ENDPOINTS, | ||
| INPUT_CLUSTERS, | ||
| MODELS_INFO, | ||
| OUTPUT_CLUSTERS, | ||
| PROFILE_ID, | ||
| ) | ||
| 
     | 
||
| 
     | 
||
| class WenzhiMTD085_ZB(CustomDevice): | ||
| """Wenzhi MTD085-ZB 24GHz mmWave radar presence sensor. | ||
| 
     | 
||
| Basic IAS Zone motion sensor mode. Requires "magic packet" initialization | ||
| to properly report occupancy state changes. | ||
| 
     | 
||
| Manufacturer: _TZ321C_fkzihax8 / _TZ321C_4slreunp | ||
| Model: TS0225 | ||
| """ | ||
| 
     | 
||
| async def apply_custom_configuration(self, *args, **kwargs): | ||
| """Send magic packet to initialize proper IAS Zone reporting.""" | ||
| try: | ||
| # Read Basic cluster attributes to initialize device | ||
| # This enables proper occupancy state reporting | ||
| basic_cluster = self.endpoints[1].in_clusters[Basic.cluster_id] | ||
| await basic_cluster.read_attributes( | ||
| [0, 1, 4, 5, 7, 0xFFFE], | ||
| allow_cache=False, | ||
| ) | ||
| except Exception: | ||
| # Ignore errors - device may not support all attributes | ||
| pass | ||
| 
     | 
||
| await super().apply_custom_configuration(*args, **kwargs) | ||
| 
     | 
||
| signature = { | ||
| MODELS_INFO: [ | ||
| ("_TZ321C_fkzihax8", "TS0225"), | ||
| ("_TZ321C_4slreunp", "TS0225"), | ||
| ], | ||
| ENDPOINTS: { | ||
| # <SimpleDescriptor endpoint=1 profile=260 device_type=1026 | ||
| # device_version=0 | ||
| # input_clusters=[0, 4, 5, 1280] | ||
| # output_clusters=[10, 25]> | ||
| 1: { | ||
| PROFILE_ID: 260, # ZHA profile | ||
| DEVICE_TYPE: 0x0402, # IAS_ZONE = 1026 decimal = 0x0402 hex | ||
| INPUT_CLUSTERS: [ | ||
| 0x0000, # Basic | ||
| 0x0004, # Groups | ||
| 0x0005, # Scenes | ||
| 0x0500, # IasZone | ||
| ], | ||
| OUTPUT_CLUSTERS: [ | ||
| 0x000A, # Time | ||
| 0x0019, # OTA | ||
| ], | ||
| }, | ||
| # <SimpleDescriptor endpoint=242 profile=41440 device_type=97 | ||
| # device_version=0 | ||
| # input_clusters=[] | ||
| # output_clusters=[33]> | ||
| 242: { | ||
| PROFILE_ID: 0xA1E0, # Green Power profile | ||
| DEVICE_TYPE: 0x0061, # Proxy Basic | ||
| INPUT_CLUSTERS: [], | ||
| OUTPUT_CLUSTERS: [ | ||
| 0x0021, # Green Power | ||
| ], | ||
| }, | ||
| }, | ||
| } | ||
| 
     | 
||
| replacement = { | ||
| ENDPOINTS: { | ||
| 1: { | ||
| PROFILE_ID: 260, | ||
| DEVICE_TYPE: 0x0402, | ||
| INPUT_CLUSTERS: [ | ||
| Basic.cluster_id, | ||
| Groups.cluster_id, | ||
| Scenes.cluster_id, | ||
| IasZone.cluster_id, | ||
| ], | ||
| OUTPUT_CLUSTERS: [ | ||
| 0x000A, # Time | ||
| 0x0019, # OTA | ||
| ], | ||
| }, | ||
| 242: { | ||
| PROFILE_ID: 0xA1E0, | ||
| DEVICE_TYPE: 0x0061, | ||
| INPUT_CLUSTERS: [], | ||
| OUTPUT_CLUSTERS: [ | ||
| 0x0021, | ||
| ], | ||
| }, | ||
| }, | ||
| } | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have
tuya_enchantmentdoing this for v2 quirks usingTuyaQuirkBuilder(andEnchantedDevicefor v1 quirks).Please look at existing mm-wave sensors using
TuyaQuirkBuilderor existing quirks for usingtuya_enchantment: https://github.com/search?q=repo%3Azigpy%2Fzha-device-handlers%20tuya_enchantment&type=code